Signed-off-by: Yu Mingfei<[email protected]>
---
 client/virt/libvirt_xml.py |   38 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/client/virt/libvirt_xml.py b/client/virt/libvirt_xml.py
index a0a179d..70580f0 100644
--- a/client/virt/libvirt_xml.py
+++ b/client/virt/libvirt_xml.py
@@ -62,3 +62,41 @@ class LibvirtXML(LibvirtXMLBase):
         # protect against accidental modification
         super(LibvirtXML, self).__init__(self._XML)

+
+class VMXML(LibvirtXMLBase):
+    """
+    VM's configuration of libvirt XML.
+    """
+
+    def __init__(self, xml):
+        """Return copy of vm's configuration XML"""
+        super(VMXML, self).__init__(xml)
+
+
+def vm_rename(name, new_name):
+    """
+    Rename a vm within libvirt
+
+    @param name: The current name for vm.
+    @param new_name: The new name for vm.
+    """
+
+    if name == new_name or virsh.domain_exists(new_name):
+        raise LibvirtXMLError("Cannot rename a VM to an existing name")
+
+    vm_xml = VMXML(virsh.dumpxml(name))
+    vm_xml.find('name').text = new_name
+    vm_xml.remove(vm_xml.find('uuid'))
+    vm_xml.write()
+
+    if not virsh.undefine(name):
+        logging.error("Undefine %s failed.", name)
+        # backup and temporary files removed automatically
+        raise LibvirtXMLError("Failed to remove VM for renaming")
+    if not virsh.define(vm_xml.name):
+        if not virsh.define(vm_xml.sourcefilename):
+            raise LibvirtXMLError("Failed to restore original VM on "
+                                  "rename exception.")
+        raise LibvirtXMLError("Failed to rename vm from %s to %s",
+                               name, new_name )
+    logging.debug("Sucessfully renamed %s to %s.", name, new_name)
-- 1.7.1

--
Best Regards
Yu Mingfei

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

Reply via email to