|
1 | 1 | #!/usr/bin/env python3 |
| 2 | +import argparse |
2 | 3 | import errno |
3 | 4 | import hashlib |
4 | 5 | import git |
|
14 | 15 | from pathlib import Path |
15 | 16 | from shutil import which |
16 | 17 |
|
| 18 | +if sys.version_info[0] < 3: |
| 19 | + raise Exception("Python 3 or a more recent version is required.") |
| 20 | + |
17 | 21 | NeededApplications = [ |
18 | 22 | "git", |
19 | 23 | "qemu-img", |
@@ -165,12 +169,6 @@ def CreateHostVMImage(RootFSDir, LinuxImage): |
165 | 169 | def Cleanup(RootFSDir): |
166 | 170 | shutil.rmtree(RootFSDir, ignore_errors=True) |
167 | 171 |
|
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 | | - |
174 | 172 | def Stage0(CacheDir, RootFSDir, config_json): |
175 | 173 | Guest_CacheDir = CacheDir + "/Guest" |
176 | 174 | Host_CacheDir = CacheDir + "/Host" |
@@ -236,16 +234,18 @@ def Stage1(CacheDir, RootFSDir, config_json): |
236 | 234 | '-drive', |
237 | 235 | 'file=' + RootFSDir + '/VMData.img' + ',format=qcow2', |
238 | 236 | '-m', |
239 | | - '32G', |
| 237 | + memory, |
240 | 238 | '-smp', |
241 | 239 | NumCores, |
242 | | - '-enable-kvm', |
243 | 240 | '-nographic', |
244 | 241 | '-nic', |
245 | 242 | 'user,model=virtio-net-pci', |
246 | 243 | '-serial', |
247 | 244 | 'telnet:localhost:{},server'.format(TelnetPort) |
248 | 245 | ] |
| 246 | + # Add -enable-kvm unless -disable-kvm is passed |
| 247 | + if not disable_kvm: |
| 248 | + QEmuCommand.append('-enable-kvm') |
249 | 249 |
|
250 | 250 | process = subprocess.Popen(QEmuCommand, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin = subprocess.PIPE) |
251 | 251 | for line in process.stderr: |
@@ -501,14 +501,31 @@ def CheckPrograms(): |
501 | 501 | Missing = True |
502 | 502 | return not Missing |
503 | 503 |
|
| 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 |
504 | 518 | if CheckPrograms() == False: |
505 | 519 | sys.exit(1) |
506 | 520 |
|
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 |
509 | 526 |
|
510 | 527 | # Load our json file |
511 | | -config_file = open(sys.argv[1], "r") |
| 528 | +config_file = open(args.config, "r") |
512 | 529 | config_text = config_file.read() |
513 | 530 | config_file.close() |
514 | 531 |
|
|
0 commit comments