On 08/30/2012 05:47 AM, Yu Mingfei wrote:
Signed-off-by: Yu Mingfei<[email protected]> --- client/virt/libvirt_xml.py | 30 +++++++++++++++++++++++++++++- 1 files changed, 29 insertions(+), 1 deletions(-)diff --git a/client/virt/libvirt_xml.py b/client/virt/libvirt_xml.py index 863022b..f551df8 100644 --- a/client/virt/libvirt_xml.py +++ b/client/virt/libvirt_xml.py @@ -11,5 +11,33 @@ class LibvirtXMLVMNameError(LibvirtXMLError): pass -class LibvirtXML(xml_utils.XMLBase): +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 rename vm from %s to %s", + name, new_name ) + logging.debug("Sucessfully renamed %s to %s.", name, new_name) -- 1.7.1
FYI: I've been working on this. It brought to light some "issues" I had not forseen with libvirt_xml and xml_utils. However, I think I've got a fix. I'll post my proposal for changes back to you on this thread.
-- Chris Evich, RHCA, RHCE, RHCDS, RHCSS Quality Assurance Engineer e-mail: cevich + `@' + redhat.com o: 1-888-RED-HAT1 x44214 _______________________________________________ Autotest-kernel mailing list [email protected] https://www.redhat.com/mailman/listinfo/autotest-kernel
