[Kimchi-devel] [PATCH v12 4/6] Host device passthrough: List VMs that are holding a host device

Zhou Zheng Sheng zhshzhou at linux.vnet.ibm.com
Wed Oct 8 09:08:41 UTC 2014


Add a "vm_holders" sub-collection under host device resource, so the
front-end can determine if a device is busy or not, and the user can
know which VMs are holding the device.

This patch scans all VM XML to check if a device is hold by a VM.

Example
  curl -k -u root -H "Content-Type: application/json" \
    -H "Accept: application/json" \
    'https://127.0.0.1:8001/host/devices/usb_1_1_6/vm_holders'
Should output a list like following.
  [
    {
      "state":"shutoff",
      "name":"fedora20"
    },
    {
      "state":"running",
      "name":"f20xfce-slave"
    }
  ]

If there is no VM holding the device, it prints an empty list [].

v5:
  When assigning a device to VM, check if there are other VMs holding
the device and raise an exception. Move the VMHoldersModel to
vmhostdevs.py to avoid circular import problem.

v11:
  Allow Guests to Share a Host PCI Device. A PCI device can be shared
  by guests, as long as there is only one guest active.

Signed-off-by: Zhou Zheng Sheng <zhshzhou at linux.vnet.ibm.com>
---
 src/kimchi/control/host.py     |  7 +++++++
 src/kimchi/model/vmhostdevs.py | 19 +++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/src/kimchi/control/host.py b/src/kimchi/control/host.py
index 1eb6350..7bcae72 100644
--- a/src/kimchi/control/host.py
+++ b/src/kimchi/control/host.py
@@ -113,11 +113,18 @@ class Devices(Collection):
         self.resource = Device
 
 
+class VMHolders(SimpleCollection):
+    def __init__(self, model, device_id):
+        super(VMHolders, self).__init__(model)
+        self.model_args = (device_id, )
+
+
 class Device(Resource):
     def __init__(self, model, id):
         self.role_key = 'storage'
         self.admin_methods = ['GET']
         super(Device, self).__init__(model, id)
+        self.vm_holders = VMHolders(self.model, id)
 
     @property
     def data(self):
diff --git a/src/kimchi/model/vmhostdevs.py b/src/kimchi/model/vmhostdevs.py
index d9e7a05..1098f88 100644
--- a/src/kimchi/model/vmhostdevs.py
+++ b/src/kimchi/model/vmhostdevs.py
@@ -293,3 +293,22 @@ class VMHostDevModel(object):
                 xmlstr = etree.tostring(e)
                 dom.detachDeviceFlags(
                     xmlstr, get_vm_config_flag(dom, mode='all'))
+
+
+class VMHoldersModel(object):
+    def __init__(self, **kargs):
+        self.conn = kargs['conn']
+
+    def get_list(self, device_id):
+        devsmodel = VMHostDevsModel(conn=self.conn)
+
+        conn = self.conn.get()
+        doms = conn.listAllDomains(0)
+
+        res = []
+        for dom in doms:
+            dom_name = dom.name()
+            if device_id in devsmodel.get_list(dom_name):
+                state = DOM_STATE_MAP[dom.info()[0]]
+                res.append({"name": dom_name, "state": state})
+        return res
-- 
1.9.3




More information about the Kimchi-devel mailing list