#!/usr/bin/env python3 # Retrieve the name of the monitor socket from subprocess import check_output from subprocess import CalledProcessError try: socket = 'unix:%s,server,nowait' % (check_output(["sockpath"], text=True)) except CalledProcessError as e: exit(e.returncode) # Set number of vCPUs to use to the environment variable QEMUSH_NPROC or # half of total vCPUs installed if it fails from os import environ 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 command = [ 'qemu-system-x86_64', '-enable-kvm', '-daemonize', '-monitor', socket, '-M', 'q35', '-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)