launchers/kvm : python -> sh

This commit is contained in:
Ahurac 2024-03-09 23:28:50 +01:00
parent 5a2233bd04
commit 1e7f5c43b8

View file

@ -1,34 +1,21 @@
#!/usr/bin/env python3
#!/usr/bin/env sh
set -x
# Set number of vCPUs to use to the environment variable QEMUSH_NPROC or
# half of total vCPUs installed if it fails
try:
from os import environ
nproc = environ['QEMUSH_NPROC']
except KeyError:
from multiprocessing import cpu_count
nproc = str(cpu_count() // 2)
# How many CPU cores to use
nproc=$QEMUSH_NPROC
[ -z "$nproc" ] && nproc=$(($(nproc) / 2))
# 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))
# How much RAM to use
ram=$QEMUSH_RAM
[ -z "$ram" ] && ram=$(($(free | grep '^Mem:\s' | awk '{ print $NF }') / 2))K
# 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
command = [
'qemu-system-x86_64',
'-enable-kvm',
'-M', 'q35',
'-cpu', 'host', '-smp', nproc,
'-m', ram,
] + argv[1:]
# Set the arguments
set -- qemu-system-x86_64 \
-enable-kvm \
-M q35 \
-cpu host -smp "$nproc" \
-m "$ram" \
"$@"
# Print the final command and replace the main process with it
print(command)
from os import execvp
execvp(command[0], command)
# Start the machine
exec "$@"