def delete_vm_and_disk(vm_name):
LI('Begin to delete VM %s', vm_name)
conn = libvirt.open('qemu:///system')
vm = None
for item in conn.listAllDomains():
if vm_name == item.name():
vm = item
break
if vm is None:
conn.close()
LI('VM %s is not found' % vm_name)
return
output = vm.XMLDesc()
root = ET.fromstring(output)
if vm.isActive():
vm.destroy()
LI('Destroy VM %s' % vm.name())
vm.undefine()
for disk_file in get_disk_file(root):
if os.path.isfile(disk_file):
status, output = commands.getstatusoutput('rm -f %s' % disk_file)
if status:
LW('Failed to delete the VM disk file %s' % disk_file)
conn.close()
LI('VM %s is removed' % vm_name)
评论列表
文章目录