Disk edition improve

This commit is contained in:
root
2024-03-26 13:58:07 +01:00
parent a2683789d1
commit cf6fe76352
2 changed files with 46 additions and 12 deletions

View File

@@ -51,17 +51,50 @@ def get_disks_info(vm_name):
return disks
def create_attached_disk(vm_name,disk_name,disk_size,disk_id):
cmd = "qemu-img create -f qcow2 "+str(disk_path)+str(disk_name)+".qcow2 "+str(disk_size)+"G"
subprocess.call(cmd, shell=True)
pool = conn.storagePoolLookupByName('disks')
disk_full = disk_path+disk_name+".qcow2"
xml_desc = f"""
<volume>
<name>{disk_name}.qcow2</name>
<capacity unit="G">{disk_size}</capacity>
<allocation unit="G">{disk_size}</allocation>
<target>
<format type="qcow2"/>
<path>{disk_full}</path>
</target>
</volume>
"""
pool.createXML(xml_desc, 0)
dom = conn.lookupByName(vm_name)
if not dom.isActive():
dom.create()
disk_full = disk_path+disk_name+".qcow2"
cmd = "virsh attach-disk --domain "+vm_name+" "+disk_full+" --target "+disk_id+" --persistent --config --live"
subprocess.call(cmd, shell=True)
flags = (libvirt.VIR_DOMAIN_AFFECT_CONFIG |
libvirt.VIR_DOMAIN_AFFECT_LIVE |
libvirt.VIR_DOMAIN_AFFECT_CURRENT)
disk_xml = """
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='{source}'/>
<target dev='{target}'/>
</disk>
""".format(source=disk_full, target=disk_id)
dom.attachDeviceFlags(disk_xml, flags)
refresh_pool()
def resize_disk(disk_file,actual_size,new_size,vm_name,disk_id):
dom = conn.lookupByName(vm_name)
flags = (libvirt.VIR_DOMAIN_AFFECT_CONFIG |
libvirt.VIR_DOMAIN_AFFECT_LIVE |
libvirt.VIR_DOMAIN_AFFECT_CURRENT)
disk_xml = """
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='{source}'/>
<target dev='{target}'/>
</disk>
""".format(source=disk_file, target=disk_id)
cmd = "virsh detach-disk '"+vm_name+"' "+disk_file+" --persistent --config --live"
subprocess.call(cmd, shell=True)
if int(actual_size) < int(new_size):
@@ -70,8 +103,7 @@ def resize_disk(disk_file,actual_size,new_size,vm_name,disk_id):
else:
cmd = "qemu-img resize -f qcow2 --shrink "+str(disk_file)+" "+str(new_size)+"G"
subprocess.call(cmd, shell=True)
cmd = "virsh attach-disk '"+str(vm_name)+"' "+str(disk_file)+" "+str(disk_id)+" --cache none --persistent --config --live"
subprocess.call(cmd, shell=True)
dom.attachDeviceFlags(disk_xml, flags)
refresh_pool()
def detach_disk(vm_name,diskfile):