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

Zhou Zheng Sheng zhshzhou at linux.vnet.ibm.com
Tue May 27 06:15:53 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 VM is 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 [].

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

diff --git a/src/kimchi/control/host.py b/src/kimchi/control/host.py
index 15f2343..efc31ea 100644
--- a/src/kimchi/control/host.py
+++ b/src/kimchi/control/host.py
@@ -110,11 +110,18 @@ class PassthroughAffectedDevices(Collection):
         self.model_args = (device_id, )
 
 
+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):
         super(Device, self).__init__(model, id)
         self.passthrough_affected_devices = \
             PassthroughAffectedDevices(self.model, id)
+        self.vm_holders = VMHolders(self.model, id)
 
     @property
     def data(self):
diff --git a/src/kimchi/model/host.py b/src/kimchi/model/host.py
index 7022566..14ccf54 100644
--- a/src/kimchi/model/host.py
+++ b/src/kimchi/model/host.py
@@ -294,6 +294,27 @@ class PassthroughAffectedDevicesModel(object):
         return [dev['name'] for dev in affected]
 
 
+class VMHoldersModel(object):
+    def __init__(self, **kargs):
+        self.conn = kargs['conn']
+
+    def get_list(self, device_id):
+        # Avoid curcular import in top level
+        from kimchi.model.vmhostdevs import VMHostDevsModel
+        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
+
+
 class DeviceModel(object):
     def __init__(self, **kargs):
         self.conn = kargs['conn']
-- 
1.9.0




More information about the Kimchi-devel mailing list