This patch adds a libvirt_xml to virt, to put some operations of libvirt xml.
And this patch adds a function to XMLBase: remove().
It can remove a subelement with given element name.
Signed-off-by: Yu Mingfei <[email protected]>
---
client/shared/xml_utils.py | 10 ++++++++++
client/virt/libvirt_xml.py | 42 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+), 0 deletions(-)
create mode 100644 client/virt/libvirt_xml.py
diff --git a/client/shared/xml_utils.py b/client/shared/xml_utils.py
index 6f90aa4..9c78331 100644
--- a/client/shared/xml_utils.py
+++ b/client/shared/xml_utils.py
@@ -140,6 +140,16 @@ class XMLBase(ElementTree.ElementTree, XMLBackup):
except (OSError, IOError):
return False
+ def remove(self, element):
+ """
+ Removes a matching subelement.
+
+ @param element is What to be removed.
+ @return a new element tree.
+ """
+ assert self._root is not None
+ return self._root.remove(element)
+
def write(self, filename=None, encoding="UTF-8"):
"""
Write current XML tree to filename, or self.name if None.
diff --git a/client/virt/libvirt_xml.py b/client/virt/libvirt_xml.py
new file mode 100644
index 0000000..32b63d5
--- /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 as virsh
+
+
+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 virsh.virsh_domain_exists(new_name, uri):
+ raise LibvirtXMLVMNameError("Cannot rename a VM to an existing name")
+
+ vm_xml = LibvirtXML(virsh.virsh_dumpxml(name, uri=uri))
+ vm_xml.find('name').text = new_name
+ vm_xml.remove(vm_xml.find('uuid'))
+ vm_xml.write()
+
+ if not virsh.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 virsh.virsh_define(vm_xml.name, uri):
+ if not virsh.virsh_define(vm_xml.tempsource.name):
+ raise LibvirtXMLVMNameError("Failed to restore original VM on "
+ "rename exception.")
+ raise LibvirtXMLVMNameError("Failed to define renamed vm from XML")
+ 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