On Friday 10 August 2012 03:37 PM, Yu Mingfei wrote:
Signed-off-by: Yu Mingfei<[email protected]>
---
client/virt/libvirt_xml.py | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 42 insertions(+), 0 deletions(-)
 create mode 100644 client/virt/libvirt_xml.py

diff --git a/client/virt/libvirt_xml.py b/client/virt/libvirt_xml.py
new file mode 100644
index 0000000..03aa76c
--- /dev/null
+++ b/client/virt/libvirt_xml.py
@@ -0,0 +1,42 @@
+import logging, os.path
+from autotest.client.shared import error, xml_utils
+from autotest.client.virt import libvirt_vm
+
+
+class LibvirtXMLError(Exception):
+    pass
+
+
+class LibvirtXMLVMNameError(LibvirtXMLError):
+    pass
+
+
+class LibvirtXML(xml_utils.XMLBase):
+    pass
+
+
+def vm_rename(name, new_name, uri=""):
+    """
+    Rename a vm within libvirt
+
+    @param name: The current name for the vm.
+    @param new_name: The new name for the vm.
+    """
+ if name == new_name or libvirt_vm.virsh_domain_exists(new_name, uri): + raise LibvirtXMLVMNameError("Cannot rename a VM to an existing name")
+
+    vm_xml = LibvirtXML(libvirt_vm.virsh_dumpxml(name, uri=uri))
+    vm_xml.find('name').text = new_name
+    vm_xml.remove(vm_xml.find('uuid'))
+    vm_xml.write()
+
+    if not libvirt_vm.virsh_undefine(name, uri):
+        logging.error("Undefine %s failed.", name)
+        # backup and temporary files removed automatically
+        raise LibvirtXMLVMNameError("Failed to remove VM for renaming")
+    if not libvirt_vm.virsh_define(vm_xml.name, uri):
+        if not libvirt_vm.virsh_define(vm_xml.sourcefilename):
+ raise LibvirtXMLVMNameError("Failed to restore original VM on "
+                                        "rename exception.")
+ raise LibvirtXMLVMNameError("Failed to define renamed vm from XML")

above LibvirtXMLVMNameError() will be raised if restore is successful as well. right ?

Thanks,
Gowri Shankar

+    logging.debug("Sucessfully renamed %s to %s.", name, new_name)
-- 1.7.1


_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel

Reply via email to