From f7e1c3997569cf47790341de3e8d0aa4589c1ec1 Mon Sep 17 00:00:00 2001 From: Ahurac Date: Sun, 21 Jan 2024 12:20:07 +0100 Subject: [PATCH] launchers/kvm : utilisation de variables d'environnement pour setup la VM --- qemu/launchers/kvm | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/qemu/launchers/kvm b/qemu/launchers/kvm index 9725a82..6ea6984 100755 --- a/qemu/launchers/kvm +++ b/qemu/launchers/kvm @@ -1,20 +1,35 @@ #!/usr/bin/env python3 -from os import execvp -from multiprocessing import cpu_count -from psutil import virtual_memory +from os import environ + +# Set number of vCPUs to use to the environment variable QEMUSH_NPROC or +# half of total vCPUs installed if it fails +try: + nproc = environ['QEMUSH_NPROC'] +except KeyError: + from multiprocessing import cpu_count + nproc = str(cpu_count() // 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)) + +# 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 - -nproc = cpu_count() // 2 -ram_kilo = virtual_memory().available // 2 // 1024 - command = [ 'qemu-system-x86_64', '-enable-kvm', '-daemonize', '-M', 'q35', - '-cpu', 'host', '-smp', str(nproc), - '-m', '{}K'.format(ram_kilo), + '-cpu', 'host', '-smp', nproc, + '-m', ram, '-net', 'nic' ] + argv[1:] +# Print the final command and replace the main process with it print(command) +from os import execvp execvp(command[0], command)