Disk Edition : Resize and Create attached disks

This commit is contained in:
root
2024-03-25 15:49:12 +01:00
parent 93f2a5379f
commit a2683789d1
22 changed files with 331 additions and 33 deletions

Binary file not shown.

View File

@@ -55,3 +55,11 @@ def del_snap_vm(vm_name,item):
dom=conn.lookupByName(vm_name)
snap_del=dom.snapshotLookupByName(item)
snap_del.delete()
def rest_snap_vm(vm_name,item):
dom=conn.lookupByName(vm_name)
snaps = dom.listAllSnapshots()
for snap in snaps:
if snap.getName() == item:
dom.revertToSnapshot(snap)

81
functions/fdisks.py Normal file
View File

@@ -0,0 +1,81 @@
import os
from config import *
from xml.dom import minidom
import subprocess
from functions.fpool import *
def get_volume_disk(disk_path):
refresh_pool()
vol_disk = conn.storageVolLookupByPath(disk_path)
vol_size = vol_disk.info()[1]
vol_size_G = vol_size / (1024*1024*1024)
return vol_size_G
def get_disks_info(vm_name):
disks = []
dom = conn.lookupByName(vm_name)
raw_xml = dom.XMLDesc(0)
xml = minidom.parseString(raw_xml)
diskTypes = xml.getElementsByTagName('disk')
for diskType in diskTypes:
if diskType.getAttribute('device') == 'disk':
disk_unit = []
disk_id = 'Unknown'
diskNodes = diskType.childNodes
for diskNode in diskNodes:
if diskNode.nodeName == 'target':
for attr in diskNode.attributes.keys():
if diskNode.attributes[attr].name == 'dev':
disk_id = diskNode.attributes[attr].value
if diskNode.nodeName == 'source':
for attr in diskNode.attributes.keys():
if diskNode.attributes[attr].name == 'file':
blkinf = dom.blockInfo(diskNode.attributes[attr].value)
vol_size = os.path.getsize(diskNode.attributes[attr].value)
diskname = os.path.basename(diskNode.attributes[attr].value)
volsize = get_volume_disk(diskNode.attributes[attr].value)
disksize = round(blkinf[0] / 1024**3)
diskused = round(blkinf[1] / 1024**3)
if disksize > 0:
diskpercent = (diskused*100 / disksize)
else:
diskpercent = 0
disk_unit = [diskNode.attributes[attr].value,disksize,diskused,diskpercent,diskname,int(volsize)]
if diskNode.nodeName == 'target':
for attr in diskNode.attributes.keys():
if diskNode.attributes[attr].name == 'dev':
disk_id = diskNode.attributes[attr].value
disk_unit.append(disk_id)
disks.append(disk_unit)
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)
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)
refresh_pool()
def resize_disk(disk_file,actual_size,new_size,vm_name,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):
cmd = "qemu-img resize -f qcow2 "+str(disk_file)+" "+str(new_size)+"G"
subprocess.call(cmd, shell=True)
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)
refresh_pool()
def detach_disk(vm_name,diskfile):
cmd = "virsh detach-disk '"+str(vm_name)+"' "+str(diskfile)+" --persistent --config --live"
subprocess.call(cmd, shell=True)
refresh_pool()

View File

@@ -1,20 +1,71 @@
from config import *
import base64
import time
import lxc
def get_version():
vlxc = lxc.version
vlibvirt = str(libvirt.getVersion()/1000000) #1000000 * major + 1000 * minor + release
vhype = hype_version
return vlxc, vlibvirt, vhype
#memory = Memory in Mo
def set_memory(vm_name,memory_new):
dom = conn.lookupByName(vm_name)
dom.setMemory(memory_new)
dom.shutdown()
time.sleep(3)
alive=0
while alive < 5:
if dom.isActive():
time.sleep(3)
alive+=1
else:
alive=6
if dom.isActive():
dom.destroy()
dom.setMaxMemory(memory_new)
dom.setMemoryFlags(memory_new)
dom.create()
time.sleep(3)
alive=0
while alive < 3:
if dom.isActive():
alive=4
else:
time.sleep(3)
alive+=1
def set_vcpu(vm_name,vcpu_new):
dom = conn.lookupByName(vm_name)
dom.setVcpus(vcpu_new)
dom.shutdown()
time.sleep(3)
alive=0
while alive < 5:
if dom.isActive():
time.sleep(3)
alive+=1
else:
alive=6
if dom.isActive():
dom.destroy()
dom.setVcpusFlags(vcpu_new,libvirt.VIR_DOMAIN_AFFECT_CONFIG | libvirt.VIR_DOMAIN_VCPU_MAXIMUM)
dom.setVcpusFlags(vcpu_new,libvirt.VIR_DOMAIN_AFFECT_CONFIG | libvirt.VIR_DOMAIN_VCPU_CURRENT)
dom.create()
time.sleep(3)
alive=0
while alive < 3:
if dom.isActive():
alive=4
else:
time.sleep(3)
alive+=1
def get_info_vm(vm_name):
dom = conn.lookupByName(vm_name)
state, maxmem, mem, cpus, cput = dom.info()
return state, maxmem, mem, cpus, cput
#Screenshot
def get_screenshot(vm_name):
dom = conn.lookupByName(vm_name)
stream = conn.newStream()
@@ -31,3 +82,18 @@ def get_screenshot(vm_name):
data = base64.b64encode(f.read())
os.remove(file)
return data
#Autostart
def get_autostart(vm_name):
dom = conn.lookupByName(vm_name)
return dom.autostart()
def set_autostart(vm_name):
dom = conn.lookupByName(vm_name)
dom.setAutostart(1)
def unset_autostart(vm_name):
dom = conn.lookupByName(vm_name)
dom.setAutostart(0)

View File

@@ -29,7 +29,6 @@ def get_pool_info(pool_name):
pool_info.append(pool.UUIDString())
pool_info.append(str(pool.isActive()))
pool_info.append(str(human_size(pool.info()[1])))
pool_info.append(str(human_size(pool.info()[2])))
pool_info.append(str(human_size(pool.info()[3])))
if pool.info()[1]==0: