Adding Network and Small corrections

This commit is contained in:
root
2024-04-07 17:42:39 +02:00
parent a3b7ef628e
commit 843555a9d5
18 changed files with 507 additions and 9 deletions

Binary file not shown.

View File

@@ -67,8 +67,10 @@ def create_attached_disk(vm_name,disk_name,disk_size,disk_id):
pool.createXML(xml_desc, 0)
dom = conn.lookupByName(vm_name)
if not dom.isActive():
dom.create()
try:
dom.resume
except:
pass
disk_full = disk_path+disk_name+".qcow2"
flags = (libvirt.VIR_DOMAIN_AFFECT_CONFIG |
libvirt.VIR_DOMAIN_AFFECT_LIVE |

99
functions/fedit.py.ori Normal file
View File

@@ -0,0 +1,99 @@
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
def set_memory(vm_name,memory_new):
dom = conn.lookupByName(vm_name)
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.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()
imageType = dom.screenshot(stream,0)
file = "tmp_screen_" + dom.name()
fileHandler = open(file, 'wb')
streamBytes = stream.recv(262120)
while streamBytes != b'':
fileHandler.write(streamBytes)
streamBytes = stream.recv(262120)
fileHandler.close()
stream.finish()
with open(file, "rb") as f:
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

@@ -61,3 +61,21 @@ def destroy_lxc(lxc_name):
container=lxc.Container(lxc_name)
container.destroy()
## RESSOURCES
def get_lxc_ressources(lxc_name):
container=lxc.Container(lxc_name)
mem_max = container.get_config_item('lxc.cgroup2.memory.max')
mem = container.get_config_item('lxc.cgroup2.memory.high')
swap_max = container.get_config_item('lxc.cgroup2.memory.swap.max')
vcpu = container.get_config_item('lxc.cgroup2.cpuset.cpus')
return vcpu, mem, mem_max, swap_max
def set_lxc_ressources(lxc_name,lxc_item,val):
container=lxc.Container(lxc_name)
container.set_config_item(lxc_item,val)
container.save_config()

70
functions/fnet.py Normal file
View File

@@ -0,0 +1,70 @@
from app import *
import ipaddress
from xml.dom import minidom
def diff_net(min_dhcp,max_dhcp):
ip1 = ipaddress.IPv4Address(min_dhcp)
ip2 = ipaddress.IPv4Address(max_dhcp)
nb_ip = int(ip2) - int(ip1) + 1
return nb_ip
def get_int(net_name):
network = conn.networkLookupByName(net_name)
net_int = network.bridgeName()
return net_int
def create_vswitch_int(net_int):
cmd="ovs-vsctl add-br "+net_int
subprocess.call(cmd, shell=True)
def delete_network(net_name,net_int):
cmd = "ovs-vsctl del-br "+net_int
subprocess.call(cmd, shell=True)
network = conn.networkLookupByName(net_name) #Lapin
network.destroy()
network.undefine()
cmd2="kill $(cat /run/dhypecp_"+net_int+".pid)"
subprocess.call(cmd2, shell=True)
cmd3="rm -f /run/dhypecp_"+net_int+".pid"
subprocess.call(cmd3, shell=True)
def create_network(net_name,net_int):
create_vswitch_int(net_int)
xml = f"""
<network>
<name>{ net_name }</name>
<forward mode='bridge'/>
<bridge name='{ net_int }'/>
<virtualport type='openvswitch'/>
</network>
"""
conn.networkDefineXML(xml)
net_use = conn.networkLookupByName(net_name)
net_use.create()
net_use.setAutostart(True)
def list_network_VM():
net_tree = []
net_na = []
#Host Interfaces
net_hard_int = conn.listInterfaces()
#Virtual Interfaces
net_list = conn.listAllNetworks()
for net_name in net_list:
if net_name.isActive():
net_sub_tree=[]
net_sub_tree.append(net_name.name())
net_use = conn.networkLookupByName(net_name.name())
ports = net_use.listAllPorts()
net_ports = []
for port in ports:
raw_xml = port.XMLDesc()
net_xml = minidom.parseString(raw_xml)
net_element = net_xml.documentElement
net_VM = net_element.getElementsByTagName("name")[0]
net_ports.append(net_VM.firstChild.data)
net_sub_tree.append(net_ports)
net_tree.append(net_sub_tree)
else:
net_na.append(net_name.name())
return net_hard_int,net_tree,net_na

View File

@@ -9,6 +9,14 @@ def get_vm_activ():
activ_vm.append(dom.name())
return activ_vm
#Set a VM as active
def is_active(vm_name):
dom = conn.lookupByName(vm_name)
try:
dom.resume()
except:
pass
def get_vm_inactiv():
inactiv_vm=conn.listDefinedDomains()
return inactiv_vm