Disk edition improve

main
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):

View File

@ -47,6 +47,7 @@
Disk(s) of {{ vm_name }}
</div>
<div class="card-body">
Actual disks<hr>
<div class="row">
<div class="col-sm">
{% for disk in disks %}
@ -60,6 +61,7 @@
{% endfor %}
</div>
<div class="col-sm">
<small>Be aware that detached disks, still exist ! You should clean unused disks on Pool tab.</small><br>
<small><i class="fa-solid fa-triangle-exclamation"></i> Shrinking a disk size, can corrupt or delete your data <i class="fa-solid fa-triangle-exclamation"></i></small><br><br>
<table style="width:100%;">
{% for disk in disks %}
@ -71,7 +73,7 @@
<input type="hidden" name="diskfile" value="{{ disk[0] }}" />
<input type="text" name="new_size" class="form-control form-control-sm" placeholder="{{ disk[5] }}" value="{{ new_size }}" />
</td><td> G </td><td>
<button type="submit" class="btn btn-outline-info btn-hype">Resize</button>
<button type="submit" class="btn btn-outline-info btn-hype"><i class="fa-solid fa-up-right-and-down-left-from-center"></i></button>
</form>
</td><td>
<form action="/detachdisk" method="post">
@ -87,17 +89,17 @@
</div>
</div>
<br>
Add a disk<hr>
Add and attach a new disk<hr>
<div class="row">
<div class="col-sm">
<form action="/adddisk" method="post">
<input type="hidden" name="vm_name" value="{{ vm_name }}">
Name </br></br></br>Size </br></br>Disk target<br>
New disk name </br></br></br>Size </br></br>Disk target (system name)<br>
</div>
<div class="col-sm">
<input type="text" name="disk_name" class="form-control form-control-sm" ><br>
<input type="text" name="disk_size" class="form-control form-control-smplaceholder="10"" > G<br>
<input type="text" name="disk_id" class="form-control form-control-sm" placeholder="vda"><br>
<input type="text" name="disk_name" class="form-control form-control-sm" required><br>
<table><tr><td><input type="text" name="disk_size" class="form-control form-control-smplaceholder="10"" required></td><td> G</td></tr></table><br>
<input type="text" name="disk_id" class="form-control form-control-sm" placeholder="vda" required><br>
</div>
<div class="col-sm">
</div>