@@ -76,6 +76,8 @@ def __init__(
7676 ram = 4096 ,
7777 driveif = "ide" ,
7878 provision_pci_bus = True ,
79+ cpu = "host" ,
80+ smp = 1 ,
7981 ):
8082 self .logger = logging .getLogger ()
8183
@@ -91,6 +93,10 @@ def __init__(
9193 self .p = None
9294 self .tn = None
9395
96+ self ._ram = ram
97+ self ._cpu = cpu
98+ self ._smp = smp
99+
94100 # various settings
95101 self .uuid = None
96102 self .fake_start_date = None
@@ -135,20 +141,30 @@ def __init__(
135141 ]
136142 )
137143
138- self .qemu_args = ["qemu-system-x86_64" , "-display" , "none" , "-machine" , "pc" ]
139- self .qemu_args .extend (
140- ["-monitor" , "tcp:0.0.0.0:40%02d,server,nowait" % self .num ]
141- )
142- self .qemu_args .extend (
143- [
144- "-m" ,
145- str (ram ),
146- "-serial" ,
147- "telnet:0.0.0.0:50%02d,server,nowait" % self .num ,
148- "-drive" ,
149- f"if={ driveif } ,file={ overlay_disk_image } " ,
150- ]
151- )
144+ self .qemu_args = [
145+ "qemu-system-x86_64" ,
146+ "-display" ,
147+ "none" ,
148+ "-machine" ,
149+ "pc" ,
150+ "-monitor" ,
151+ f"tcp:0.0.0.0:40{ self .num :02d} ,server,nowait" ,
152+ "-serial" ,
153+ f"telnet:0.0.0.0:50{ self .num :02d} ,server,nowait" ,
154+ "-m" , # memory
155+ str (self .ram ),
156+ "-cpu" , # cpu type
157+ self .cpu ,
158+ "-smp" ,
159+ self .smp , # cpu core configuration
160+ "-drive" ,
161+ f"if={ driveif } ,file={ overlay_disk_image } " ,
162+ ]
163+
164+ # add additional qemu args if they were provided
165+ if self .qemu_additional_args :
166+ self .qemu_args .extend (self .qemu_additional_args )
167+
152168 # enable hardware assist if KVM is available
153169 if os .path .exists ("/dev/kvm" ):
154170 self .qemu_args .insert (1 , "-enable-kvm" )
@@ -706,7 +722,6 @@ def check_qemu(self):
706722 self .stop ()
707723 self .start ()
708724
709-
710725 @property
711726 def version (self ):
712727 """Read version number from VERSION environment variable
@@ -719,6 +734,57 @@ def version(self):
719734 return version
720735 raise ValueError ("The VERSION environment variable is not set" )
721736
737+ @property
738+ def ram (self ):
739+ """
740+ Read memory size from the QEMU_MEMORY environment variable and use it in the qemu parameters for the VM.
741+ If the QEMU_MEMORY environment variable is not set, use the default value.
742+ Should be provided as a number of MB. e.g. 4096.
743+ """
744+
745+ if "QEMU_MEMORY" in os .environ :
746+ return get_digits (str (os .getenv ("QEMU_MEMORY" )))
747+
748+ return self ._ram
749+
750+ @property
751+ def cpu (self ):
752+ """
753+ Read the CPU type the QEMU_CPU environment variable and use it in the qemu parameters for the VM.
754+ If the QEMU_CPU environment variable is not set, use the default value.
755+ """
756+
757+ if "QEMU_CPU" in os .environ :
758+ return str (os .getenv ("QEMU_CPU" ))
759+
760+ return str (self ._cpu )
761+
762+ @property
763+ def smp (self ):
764+ """
765+ Read SMP parameter (e.g. number of CPU cores) from the QEMU_SMP environment variable.
766+ If the QEMU_SMP parameter is not set, the default value is used.
767+ Should be provided as a number, e.g. 2
768+ """
769+
770+ if "QEMU_SMP" in os .environ :
771+ return str (os .getenv ("QEMU_SMP" ))
772+
773+ return str (self ._smp )
774+
775+ @property
776+ def qemu_additional_args (self ):
777+ """
778+ Read additional qemu arguments (e.g. number of CPU cores) from the QEMU_ADDITIONAL_ARGS environment variable.
779+ If the QEMU_ADDITIONAL_ARGS parameter is not set, nothing is added to the default args set.
780+ Should be provided as a space separated list of arguments, e.g. "-machine pc -display none"
781+ """
782+
783+ if "QEMU_ADDITIONAL_ARGS" in os .environ :
784+ s = str (os .getenv ("QEMU_ADDITIONAL_ARGS" ))
785+ if s :
786+ return s .split ()
787+
722788
723789class VR :
724790 def __init__ (self , username , password ):
@@ -777,49 +843,10 @@ class QemuBroken(Exception):
777843 """Our Qemu instance is somehow broken"""
778844
779845
780- # getMem returns the RAM size (in Mb) for a given VM mode.
781- # RAM can be specified in the variant dict, provided by a user via the custom type definition,
782- # or set via env vars.
783- # If set via env vars, the getMem will return this value as the most specific one.
784- # Otherwise, the ram provided to this function will be converted to Mb and returned.
785- def getMem (vmMode : str , ram : int ) -> int :
786- if vmMode == "integrated" :
787- # Integrated VM can use both MEMORY and CP_MEMORY env vars
788- if "MEMORY" in os .environ :
789- return 1024 * get_digits (os .getenv ("MEMORY" ))
790- if "CP_MEMORY" in os .environ :
791- return 1024 * get_digits (os .getenv ("CP_MEMORY" ))
792- if vmMode == "cp" :
793- if "CP_MEMORY" in os .environ :
794- return 1024 * get_digits (os .getenv ("CP_MEMORY" ))
795- if vmMode == "lc" :
796- if "LC_MEMORY" in os .environ :
797- return 1024 * get_digits (os .getenv ("LC_MEMORY" ))
798- return 1024 * int (ram )
799-
800-
801- # getCpu returns the number of cpu cores for a given VM mode.
802- # Cpu can be specified in the variant dict, provided by a user via the custom type definition,
803- # or set via env vars.
804- # If set via env vars, the function will return this value as the most specific one.
805- # Otherwise, the number provided to this function via cpu param returned.
806- def getCpu (vsimMode : str , cpu : int ) -> int :
807- if vsimMode == "integrated" :
808- # Integrated VM can use both MEMORY and CP_MEMORY env vars
809- if "CPU" in os .environ :
810- return int (os .getenv ("CPU" ))
811- if "CP_CPU" in os .environ :
812- return int (os .getenv ("CP_CPU" ))
813- if vsimMode == "cp" :
814- if "CP_CPU" in os .environ :
815- return int (os .getenv ("CP_CPU" ))
816- if vsimMode == "lc" :
817- if "LC_CPU" in os .environ :
818- return int (os .getenv ("LC_CPU" ))
819- return cpu
820-
821-
822- # strip all non-numeric characters from a string
823846def get_digits (input_str : str ) -> int :
847+ """
848+ Strip all non-numeric characters from a string
849+ """
850+
824851 non_string_chars = re .findall (r"\d" , input_str )
825852 return int ("" .join (non_string_chars ))
0 commit comments