2023-12-13 12:41:28 +01:00
|
|
|
#!/usr/bin/env python3
|
2024-01-21 22:41:00 +01:00
|
|
|
|
2024-01-21 12:20:07 +01:00
|
|
|
# Set number of vCPUs to use to the environment variable QEMUSH_NPROC or
|
|
|
|
# half of total vCPUs installed if it fails
|
|
|
|
try:
|
2024-01-21 23:41:32 +01:00
|
|
|
from os import environ
|
2024-01-21 12:20:07 +01:00
|
|
|
nproc = environ['QEMUSH_NPROC']
|
|
|
|
except KeyError:
|
|
|
|
from multiprocessing import cpu_count
|
|
|
|
nproc = str(cpu_count() // 2)
|
2023-12-13 12:41:28 +01:00
|
|
|
|
2024-01-21 12:20:07 +01:00
|
|
|
# Set amount of RAM to use to the environment variable QEMUSH_RAM or half
|
|
|
|
# of total RAM installed if it fails
|
|
|
|
try:
|
|
|
|
ram = environ['QEMUSH_RAM']
|
|
|
|
except KeyError:
|
|
|
|
from psutil import virtual_memory
|
|
|
|
ram = str(virtual_memory().available // 2 // (1024 ** 2))
|
2023-12-13 12:41:28 +01:00
|
|
|
|
2024-01-21 12:20:07 +01:00
|
|
|
# Set the command to initialize; all required parameters to make a sane KVM
|
|
|
|
# are included and optional arguments are appended from argv
|
|
|
|
from sys import argv
|
2023-12-13 12:41:28 +01:00
|
|
|
command = [
|
|
|
|
'qemu-system-x86_64',
|
2024-01-23 10:30:54 +01:00
|
|
|
'-enable-kvm',
|
2023-12-13 12:41:28 +01:00
|
|
|
'-M', 'q35',
|
2024-01-21 12:20:07 +01:00
|
|
|
'-cpu', 'host', '-smp', nproc,
|
|
|
|
'-m', ram,
|
2023-12-13 12:41:28 +01:00
|
|
|
] + argv[1:]
|
|
|
|
|
2024-01-21 12:20:07 +01:00
|
|
|
# Print the final command and replace the main process with it
|
2023-12-13 12:41:28 +01:00
|
|
|
print(command)
|
2024-01-21 12:20:07 +01:00
|
|
|
from os import execvp
|
2023-12-13 12:41:28 +01:00
|
|
|
execvp(command[0], command)
|