This patch adds 2 functions to libvirt_vm.
    1) attach-device()
    2) detach-device()

Signed-off-by: Tang Chen <[email protected]>
---
 client/virt/libvirt_vm.py |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/client/virt/libvirt_vm.py b/client/virt/libvirt_vm.py
index 4b391c1..d7eb7b0 100644
--- a/client/virt/libvirt_vm.py
+++ b/client/virt/libvirt_vm.py
@@ -374,6 +374,29 @@ def virsh_migrate(options, name, dest_uri, extra, uri = 
""):
         return False
     return True
 
+def virsh_attach_device(name, xml_file, extra = "", uri = ""):
+    """
+    Attach a device to VM.
+    """
+    cmd = "attach-device --domain %s --file %s %s" % (name, xml_file, extra)
+    try:
+        virsh_cmd(cmd, uri)
+        return True
+    except error.CmdError:
+        logging.error("Attaching device to VM %s failed." % name)
+        return False
+
+def virsh_detach_device(name, xml_file, extra = "", uri = ""):
+    """
+    Detach a device from VM.
+    """
+    cmd = "detach-device --domain %s --file %s %s" % (name, xml_file, extra)
+    try:
+        virsh_cmd(cmd, uri)
+        return True
+    except error.CmdError:
+        logging.error("Detaching device from VM %s failed." % name)
+        return False
 
 class VM(virt_vm.BaseVM):
     """
@@ -1126,6 +1149,17 @@ class VM(virt_vm.BaseVM):
             self.connect_uri = dest_uri
         return result
 
+    def attach_device(self, xml_file, extra = ""):
+        """
+        Attach a device to VM.
+        """
+        return virsh_attach_device(self.name, xml_file, extra, 
self.connect_uri)
+
+    def detach_device(self, xml_file, extra = ""):
+        """
+        Detach a device from VM.
+        """
+        return virsh_detach_device(self.name, xml_file, extra, 
self.connect_uri)
 
     def destroy(self, gracefully=True, free_mac_addresses=True):
         """
-- 
1.7.3.1

-- 
Best Regards,
Tang chen
_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to