From: Yu Mingfei <[email protected]> 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/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..6f9396c --- /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.XMLTreeFile): + 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") + logging.debug("Sucessfully renamed %s to %s.", name, new_name) -- 1.7.4.1 _______________________________________________ Autotest-kernel mailing list [email protected] https://www.redhat.com/mailman/listinfo/autotest-kernel
