Skip to content

Commit 266df35

Browse files
committed
Add optional -m <memory> and -disable-kvm flags to build_image.py
1 parent 71ae0dd commit 266df35

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

build_image.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python3
2+
import argparse
23
import errno
34
import hashlib
45
import git
@@ -14,6 +15,9 @@
1415
from pathlib import Path
1516
from shutil import which
1617

18+
if sys.version_info[0] < 3:
19+
raise Exception("Python 3 or a more recent version is required.")
20+
1721
NeededApplications = [
1822
"git",
1923
"qemu-img",
@@ -165,12 +169,6 @@ def CreateHostVMImage(RootFSDir, LinuxImage):
165169
def Cleanup(RootFSDir):
166170
shutil.rmtree(RootFSDir, ignore_errors=True)
167171

168-
if sys.version_info[0] < 3:
169-
raise Exception("Python 3 or a more recent version is required.")
170-
171-
if (len(sys.argv) < 4):
172-
sys.exit("usage: %s <Config.json> <Cache directory> <RootFS Dir>" % (sys.argv[0]))
173-
174172
def Stage0(CacheDir, RootFSDir, config_json):
175173
Guest_CacheDir = CacheDir + "/Guest"
176174
Host_CacheDir = CacheDir + "/Host"
@@ -236,16 +234,18 @@ def Stage1(CacheDir, RootFSDir, config_json):
236234
'-drive',
237235
'file=' + RootFSDir + '/VMData.img' + ',format=qcow2',
238236
'-m',
239-
'32G',
237+
memory,
240238
'-smp',
241239
NumCores,
242-
'-enable-kvm',
243240
'-nographic',
244241
'-nic',
245242
'user,model=virtio-net-pci',
246243
'-serial',
247244
'telnet:localhost:{},server'.format(TelnetPort)
248245
]
246+
# Add -enable-kvm unless -disable-kvm is passed
247+
if not disable_kvm:
248+
QEmuCommand.append('-enable-kvm')
249249

250250
process = subprocess.Popen(QEmuCommand, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin = subprocess.PIPE)
251251
for line in process.stderr:
@@ -501,14 +501,31 @@ def CheckPrograms():
501501
Missing = True
502502
return not Missing
503503

504+
# Argument parser setup
505+
parser = argparse.ArgumentParser(
506+
description="Script to configure and build a RootFS using QEMU with specified settings.",
507+
usage="%(prog)s [-m <memory>] [-disable-kvm] <Config.json> <Cache directory> <RootFS Dir>"
508+
)
509+
parser.add_argument("config", type=str, help="Path to a RootFS config .json file")
510+
parser.add_argument("cache_dir", type=str, help="Cache directory")
511+
parser.add_argument("rootfs_dir", type=str, help="RootFS directory")
512+
parser.add_argument("-m", type=str, help="Memory size for QEMU (e.g., 2G, 512M, etc.)", default="32G")
513+
parser.add_argument("-disable-kvm", action="store_true", help="Disable KVM in QEMU")
514+
515+
args = parser.parse_args()
516+
517+
# Only after parsing the args we check the programs, so that users are able to call -h to get help
504518
if CheckPrograms() == False:
505519
sys.exit(1)
506520

507-
CacheDir = sys.argv[2]
508-
RootFSDir = sys.argv[3]
521+
# Extracting arguments
522+
CacheDir = args.cache_dir
523+
RootFSDir = args.rootfs_dir
524+
memory = args.m
525+
disable_kvm = args.disable_kvm
509526

510527
# Load our json file
511-
config_file = open(sys.argv[1], "r")
528+
config_file = open(args.config, "r")
512529
config_text = config_file.read()
513530
config_file.close()
514531

0 commit comments

Comments
 (0)