forked from arighi/virtme-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
1183 lines (1044 loc) · 37.7 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- mode: python -*-
# Copyright 2023 Andrea Righi
"""virtme-ng: main command-line frontend."""
import argparse
import re
import os
import sys
import socket
import shutil
import json
import tempfile
from subprocess import (
check_call,
check_output,
Popen,
DEVNULL,
PIPE,
CalledProcessError,
)
from select import select
from pathlib import Path
try:
from argcomplete import autocomplete
except ModuleNotFoundError:
def autocomplete(*args, **kwargs):
# pylint: disable=unused-argument
pass
from virtme.util import SilentError, uname, get_username
from virtme_ng.utils import CONF_FILE, spinner_decorator
from virtme_ng.mainline import KernelDownloader
from virtme_ng.version import VERSION
def check_call_cmd(command, quiet=False, dry_run=False):
if dry_run:
print(" ".join(command))
return
with Popen(
command,
stdout=PIPE,
stderr=PIPE,
stdin=DEVNULL,
) as process:
process.stdout.flush()
process.stderr.flush()
stdout_fd = process.stdout.fileno()
stderr_fd = process.stderr.fileno()
# Use select to poll for new data in the file descriptors
while process.poll() is None:
ready_to_read, _, _ = select([stdout_fd, stderr_fd], [], [], 1)
for file in ready_to_read:
if file == stdout_fd:
line = process.stdout.readline().decode()
if line and not quiet:
sys.stdout.write(line)
sys.stdout.flush()
if file == stderr_fd:
line = process.stderr.readline().decode()
if line:
sys.stderr.write(line)
sys.stderr.flush()
# Wait for the process to complete and get the return code
return_code = process.wait()
# Trigger a CalledProcessError exception if command failed
if return_code:
raise CalledProcessError(return_code, command)
def make_parser():
"""Main virtme-ng command line parser."""
parser = argparse.ArgumentParser(
description="Build and run kernels inside a virtualized snapshot of your live system",
)
parser.add_argument(
"--version", "-V", action="version", version=f"virtme-ng {VERSION}"
)
g_action = parser.add_argument_group(title="Action").add_mutually_exclusive_group()
g_action.add_argument(
"--run",
"-r",
action="store",
nargs="?",
const=uname.release,
default=None,
help="Run a specified kernel; "
"--run can accept one of the following arguments: 1) nothing (in this "
"case it'll try to boot the same kernel running on the host), 2) a kernel "
"binary (like ./arch/x86/boot/bzImage), 3) a directory (where it'll try "
"to find a valid kernel binary file), 4) an upstream version, for "
"example `vng --run v6.6.17` (in this case vng will download a "
"precompiled upstream kernel from the Ubuntu mainline repository)",
)
g_action.add_argument(
"--build",
"-b",
action="store_true",
help="Build the kernel in the current directory "
"(or remotely if used with --build-host)",
)
g_action.add_argument(
"--clean",
"-x",
action="store_true",
help="Clean the kernel repository (local or remote if used with --build-host)",
)
g_action.add_argument(
"--dump",
"-d",
action="store",
help="Generate a memory dump of the running kernel "
"(instance needs to be started with --debug)",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Only show the commands without actually running them.",
)
parser.add_argument(
"--skip-config",
"-s",
action="store_true",
help="[deprecated] Do not re-generate kernel .config",
)
parser.add_argument(
"--no-virtme-ng-init",
action="store_true",
help="Fallback to the bash virtme-init (useful for debugging/development)",
)
parser.add_argument(
"--snaps", action="store_true", help="Allow to execute snaps inside virtme-ng"
)
parser.add_argument(
"--debug",
action="store_true",
help="Start the instance with debugging enabled (allow to generate crash dumps)",
)
parser.add_argument(
"--kconfig",
"-k",
action="store_true",
help="Only override the kernel .config without building/running anything",
)
parser.add_argument(
"--skip-modules",
"-S",
action="store_true",
help="Run a really fast build by skipping external modules "
"(no external modules support)",
)
parser.add_argument(
"--commit",
"-c",
action="store",
help="Use a kernel identified by a specific commit id, tag or branch",
)
parser.add_argument(
"--config",
"-f",
action="append",
help="Use one (or more) specific kernel .config snippet "
"to override default config settings",
)
parser.add_argument(
"--compiler",
action="store",
help="Compiler to be used as CC when building the kernel",
)
parser.add_argument(
"--busybox",
metavar="PATH_TO_BUSYBOX",
action="store",
help="Use the specified busybox binary",
)
parser.add_argument("--qemu", action="store", help="Use the specified QEMU binary")
parser.add_argument(
"--name",
action="store",
default="virtme-ng",
help="Set guest hostname and qemu -name flag"
)
parser.add_argument(
"--user",
action="store",
help="Change user inside the guest (default is same user as the host)",
)
parser.add_argument(
"--root",
action="store",
help="Pass a specific chroot to use inside the virtualized kernel "
+ "(useful with --arch)",
)
parser.add_argument(
"--root-release",
action="store",
help="Use a target Ubuntu release to create a new chroot (used with --root)",
)
parser.add_argument(
"--rw",
action="store_true",
help="Give the guest read-write access to its root filesystem. "
"WARNING: this can be dangerous for the host filesystem!",
)
parser.add_argument(
"--force-9p", action="store_true", help="Use legacy 9p filesystem as rootfs"
)
parser.add_argument(
"--disable-microvm",
action="store_true",
help='Avoid using the "microvm" QEMU architecture (only on x86_64)',
)
parser.add_argument(
"--disable-kvm",
action="store_true",
help='Avoid using hardware virtualization / KVM',
)
parser.add_argument(
"--cwd",
action="store",
help="Change guest working directory "
+ "(default is current working directory when possible)",
)
parser.add_argument(
"--pwd",
action="store_true",
help="[deprecated] --pwd is set implicitly by default",
)
parser.add_argument(
"--rodir",
action="append",
default=[],
help="Supply a read-only directory to the guest."
+ "Use --rodir=path or --rodir=guestpath=hostpath",
)
parser.add_argument(
"--rwdir",
action="append",
default=[],
help="Supply a read/write directory to the guest."
+ "Use --rwdir=path or --rwdir=guestpath=hostpath",
)
parser.add_argument(
"--overlay-rwdir",
action="append",
default=[],
help="Supply a directory that is r/w to the guest but read-only in the host."
+ "Use --overlay-rwdir=path.",
)
parser.add_argument(
"--cpus", "-p", action="store", help="Set guest CPU count (qemu -smp flag)"
)
parser.add_argument(
"--memory", "-m", action="store", help="Set guest memory size (qemu -m flag)"
)
parser.add_argument(
"--numa",
metavar="MEM[,cpus=FIRST_CPU1[-LAST_CPU1]][,cpus=FIRST_CPU2[-LAST_CPU2]]...",
action="append",
help="Create a NUMA node in the guest. "
+ "Use this option multiple times to create more NUMA nodes. "
+ "The total memory size assigned to NUMA nodes must match the guest memory size (specified with --memory/-m). "
+ "This option implicitly disables the microvm architecture."
)
parser.add_argument(
"--balloon",
action="store_true",
help="Allow the host to ask the guest to release memory",
)
parser.add_argument(
"--network",
"-n",
action="store",
metavar="user|bridge",
help="Enable network access",
)
parser.add_argument(
"--disk",
"-D",
action="append",
metavar="PATH",
help="Add a file as virtio-scsi disk (can be used multiple times)",
)
parser.add_argument(
"--exec",
"-e",
action="store",
help="Execute a command inside the kernel and exit",
)
parser.add_argument(
"--append",
"-a",
action="append",
help="Additional kernel boot options (can be used multiple times)",
)
parser.add_argument(
"--force-initramfs",
action="store_true",
help="Use an initramfs even if unnecessary",
)
parser.add_argument(
"--sound",
action="store_true",
help="Enable audio device (if the architecture supports it)",
)
parser.add_argument(
"--graphics",
"-g",
action="store_true",
help="Show graphical output instead of using a console.",
)
parser.add_argument(
"--verbose",
"-v",
action="store_true",
help="Increase console output verbosity.",
)
parser.add_argument(
"--quiet",
"-q",
action="store_true",
help="Override verbose mode (disable --verbose).",
)
parser.add_argument(
"--qemu-opts",
"-o",
action="append",
help="Additional arguments for QEMU (can be used multiple times)",
)
parser.add_argument(
"--build-host",
action="store",
help="Perform kernel build on a remote server (ssh access required)",
)
parser.add_argument(
"--build-host-exec-prefix",
action="store",
help="Prepend a command (e.g., chroot) "
"to the make command executed on the remote build host",
)
parser.add_argument(
"--build-host-vmlinux",
action="store_true",
help="Copy vmlinux back from the build host",
)
parser.add_argument(
"--arch",
action="store",
help="Generate and test a kernel for a specific architecture "
"(default is host architecture)",
)
parser.add_argument(
"--force",
action="store_true",
help="Force reset git repository to target branch or commit "
"(warning: this may drop uncommitted changes), "
"and force kernel config override",
)
parser.add_argument(
"envs",
metavar="envs",
type=str,
nargs="*",
help="Additional Makefile variables",
)
return parser
_ARGPARSER = make_parser()
def arg_fail(message, show_usage=True):
"""Print an error message and exit, optionally showing usage help."""
sys.stderr.write(message + "\n")
if show_usage:
_ARGPARSER.print_usage()
sys.exit(1)
ARCH_MAPPING = {
"arm64": {
"qemu_name": "aarch64",
"linux_name": "arm64",
"cross_compile": "aarch64-linux-gnu-",
"kernel_target": "Image",
"kernel_image": "Image",
},
"armhf": {
"qemu_name": "arm",
"linux_name": "arm",
"cross_compile": "arm-linux-gnueabihf-",
"kernel_target": "",
"kernel_image": "zImage",
"max-cpus": 4,
},
"ppc64el": {
"qemu_name": "ppc64",
"linux_name": "powerpc",
"cross_compile": "powerpc64le-linux-gnu-",
"kernel_target": "vmlinux",
"kernel_image": "vmlinux",
},
"s390x": {
"qemu_name": "s390x",
"linux_name": "s390",
"cross_compile": "s390x-linux-gnu-",
"kernel_target": "bzImage",
"kernel_image": "bzImage",
},
"riscv64": {
"qemu_name": "riscv64",
"linux_name": "riscv",
"cross_compile": "riscv64-linux-gnu-",
"kernel_target": "Image",
"kernel_image": "Image",
},
}
MAKE_COMMAND = "make LOCALVERSION=-virtme"
REMOTE_BUILD_SCRIPT = """#!/bin/bash
cd ~/.virtme
git reset --hard __virtme__
[ -f debian/rules ] && fakeroot debian/rules clean
{} {}
"""
def create_root(destdir, arch, release):
"""Initialize a rootfs directory, populating files/directory if it doesn't exist."""
if os.path.exists(destdir):
return
# Use Ubuntu's cloud images to create a rootfs, these images are fairly
# small and they provide a nice environment to test kernels.
if release is None:
release = (
check_output("lsb_release -s -c", shell=True)
.decode(sys.stdout.encoding)
.rstrip()
)
url = (
"https://cloud-images.ubuntu.com/"
+ f"{release}/current/{release}-server-cloudimg-{arch}-root.tar.xz"
)
prevdir = os.getcwd()
os.system(f"sudo mkdir -p {destdir}")
os.chdir(destdir)
os.system(f"curl -s {url} | sudo tar xvJ")
os.chdir(prevdir)
class KernelSource:
"""Main class that implement actions to perform on a kernel source directory."""
def __init__(self):
self.virtme_param = {}
conf_path = self.get_conf_file_path()
self.default_opts = []
if conf_path is not None:
with open(conf_path, "r", encoding="utf-8") as conf_fd:
conf_data = json.loads(conf_fd.read())
if "default_opts" in conf_data:
self.default_opts = conf_data["default_opts"]
self.cpus = str(os.cpu_count())
def get_conf_file_path(self):
"""Return virtme-ng main configuration file path."""
# First check if there is a config file in the user's home config
# directory, then check for a single config file in ~/.virtme-ng.conf and
# finally check for /etc/virtme-ng.conf. If none of them exist, report an
# error and exit.
configs = (
CONF_FILE,
Path(Path.home(), ".virtme-ng.conf"),
Path("/etc", "virtme-ng.conf"),
)
for conf in configs:
if conf.exists():
return conf
return None
def _format_cmd(self, cmd):
return list(filter(None, cmd.split(" ")))
def _is_dirty_repo(self):
cmd = "git --no-optional-locks status -uno --porcelain"
if check_output(self._format_cmd(cmd), stderr=DEVNULL, stdin=DEVNULL):
return True
return False
def checkout(self, args):
"""Perform a git checkout operation on a local kernel git repository."""
if not os.path.isdir(".git"):
arg_fail("error: must run from a kernel git repository", show_usage=False)
target = args.commit or "HEAD"
if args.build_host is not None or target != "HEAD":
if not args.force and self._is_dirty_repo():
arg_fail(
"error: you have uncommitted changes in your git repository, "
+ "use --force to drop them",
show_usage=False,
)
check_call_cmd(
["git", "reset", "--hard", target],
quiet=not args.verbose,
dry_run=args.dry_run,
)
def config(self, args):
"""Perform a make config operation on a kernel source directory."""
arch = args.arch
cmd = "virtme-configkernel --defconfig"
if not args.force and not args.kconfig:
cmd += " --no-update"
if arch is not None:
if arch not in ARCH_MAPPING:
arg_fail(f"unsupported architecture: {arch}")
arch = ARCH_MAPPING[arch]["qemu_name"]
cmd += f" --arch {arch}"
user_config = str(Path.home()) + "/.config/virtme-ng/kernel.config"
if args.config:
for conf in args.config:
cmd += f" --custom {conf}"
if os.path.exists(user_config):
cmd += f" --custom {user_config}"
# Propagate additional Makefile variables
for var in args.envs:
cmd += f" {var} "
check_call_cmd(
self._format_cmd(cmd), quiet=not args.verbose, dry_run=args.dry_run
)
def _make_remote(self, args, make_command):
check_call_cmd(
["ssh", args.build_host, "mkdir -p ~/.virtme"],
quiet=not args.verbose,
dry_run=args.dry_run,
)
check_call_cmd(
["ssh", args.build_host, "git init ~/.virtme"],
quiet=not args.verbose,
dry_run=args.dry_run,
)
check_call_cmd(
[
"git",
"push",
"--force",
"--porcelain",
f"{args.build_host}:~/.virtme",
"HEAD:__virtme__",
],
quiet=not args.verbose,
dry_run=args.dry_run,
)
cmd = f"rsync .config {args.build_host}:.virtme/.config"
check_call_cmd(
self._format_cmd(cmd), quiet=not args.verbose, dry_run=args.dry_run
)
# Create remote build script
with tempfile.NamedTemporaryFile(mode="w+t") as tmp:
tmp.write(
REMOTE_BUILD_SCRIPT.format(
args.build_host_exec_prefix or "",
make_command + " -j$(nproc --all)",
)
)
tmp.flush()
cmd = f"rsync {tmp.name} {args.build_host}:.virtme/.kc-build"
check_call_cmd(
self._format_cmd(cmd), quiet=not args.verbose, dry_run=args.dry_run
)
# Execute remote build script
check_call_cmd(
["ssh", args.build_host, "bash", ".virtme/.kc-build"],
quiet=not args.verbose,
dry_run=args.dry_run,
)
# Copy artifacts back to the running host
with tempfile.NamedTemporaryFile(mode="w+t") as tmp:
if args.build_host_vmlinux or args.arch == "ppc64el":
vmlinux = "--include=vmlinux"
else:
vmlinux = ""
if args.skip_modules:
cmd = (
"rsync -azS --progress --exclude=.config --exclude=.git/ "
+ "--include=*/ --include=bzImage --include=zImage --include=Image "
+ f'{vmlinux} --include=*.dtb --exclude="*" {args.build_host}:.virtme/ ./'
)
else:
cmd = (
"rsync -azS --progress --exclude=.config --exclude=.git/ "
+ '--include=*/ --include="*.ko" --include=".dwo" '
+ f"--include=bzImage --include=zImage --include=Image {vmlinux} "
+ "--include=.config --include=modules.* "
+ "--include=System.map --include=Module.symvers --include=module.lds "
+ '--include=*.dtb --include="**/generated/**" --exclude="*" '
+ f"{args.build_host}:.virtme/ ./"
)
tmp.write(cmd)
tmp.flush()
check_call_cmd(
["bash", tmp.name], quiet=not args.verbose, dry_run=args.dry_run
)
if not args.skip_modules:
if os.path.exists("./debian/rules"):
check_call_cmd(
["fakeroot", "debian/rules", "clean"], quiet=not args.verbose
)
check_call_cmd(
self._format_cmd(
make_command + f" -j {self.cpus}" + " modules_prepare"
),
quiet=not args.verbose,
dry_run=args.dry_run,
)
def make(self, args):
"""Perform a make operation on a kernel source directory."""
if not os.path.isdir(".git") and args.build_host is not None:
arg_fail(
"error: --build-host can be used only on a kernel git repository",
show_usage=False,
)
if args.build_host is not None and self._is_dirty_repo():
arg_fail(
"error: you have uncommitted changes in your git repository, "
+ "commit or drop them before building on a remote host",
show_usage=False,
)
arch = args.arch
if arch is not None:
if arch not in ARCH_MAPPING:
arg_fail(f"unsupported architecture: {arch}")
target = ARCH_MAPPING[arch]["kernel_target"]
cross_compile = ARCH_MAPPING[arch]["cross_compile"]
cross_arch = ARCH_MAPPING[arch]["linux_name"]
else:
target = "bzImage"
cross_compile = None
cross_arch = None
make_command = MAKE_COMMAND
if args.compiler:
make_command += f" CC={args.compiler}"
if args.skip_modules:
make_command += f" {target}"
if cross_compile and cross_arch:
make_command += f" CROSS_COMPILE={cross_compile} ARCH={cross_arch}"
# Propagate additional Makefile variables
for var in args.envs:
make_command += f" {var} "
if args.build_host is None:
# Build the kernel locally
check_call_cmd(
self._format_cmd(make_command + " -j" + self.cpus),
quiet=not args.verbose,
dry_run=args.dry_run,
)
else:
# Build the kernel on a remote build host
self._make_remote(args, make_command)
def _get_virtme_name(self, args):
if args.name is not None:
self.virtme_param["name"] = "--name " + args.name
else:
self.virtme_param["name"] = "--name " + socket.gethostname()
def _get_virtme_exec(self, args):
if args.envs:
args.exec = " ".join(args.envs)
if args.exec is not None:
self.virtme_param["exec"] = f'--script-sh "{args.exec}"'
else:
self.virtme_param["exec"] = ""
def _get_virtme_user(self, args):
# Default user for scripts is root, default user for interactive
# sessions is current user.
#
# NOTE: graphic sessions are considered interactive.
if args.exec and not args.graphics:
self.virtme_param["user"] = ""
else:
self.virtme_param["user"] = "--user " + get_username()
# Override default user, if specified by the --user argument.
if args.user is not None:
if args.user != "root":
self.virtme_param["user"] = "--user " + args.user
else:
self.virtme_param["user"] = ""
def _get_virtme_arch(self, args):
if args.arch is not None:
if args.arch not in ARCH_MAPPING:
arg_fail(f"unsupported architecture: {args.arch}")
if "max-cpus" in ARCH_MAPPING[args.arch]:
self.cpus = ARCH_MAPPING[args.arch]["max-cpus"]
self.virtme_param["arch"] = "--arch " + ARCH_MAPPING[args.arch]["qemu_name"]
else:
self.virtme_param["arch"] = ""
def _get_virtme_root(self, args):
if args.root is not None:
create_root(args.root, args.arch or "amd64", args.root_release)
self.virtme_param["root"] = f"--root {args.root}"
else:
self.virtme_param["root"] = ""
def _get_virtme_rw(self, args):
if args.rw:
self.virtme_param["rw"] = "--rw"
else:
self.virtme_param["rw"] = ""
def _get_virtme_cwd(self, args):
if args.cwd is not None:
if args.pwd:
arg_fail("--pwd and --cwd are mutually exclusive")
self.virtme_param["cwd"] = "--cwd " + args.cwd
elif args.root is None:
self.virtme_param["cwd"] = "--pwd"
else:
self.virtme_param["cwd"] = ""
def _get_virtme_rodir(self, args):
self.virtme_param["rodir"] = ""
for item in args.rodir:
self.virtme_param["rodir"] += "--rodir " + item
def _get_virtme_rwdir(self, args):
self.virtme_param["rwdir"] = ""
for item in args.rwdir:
self.virtme_param["rwdir"] += "--rwdir " + item
def _get_virtme_overlay_rwdir(self, args):
# Set default overlays if rootfs is mounted in read-only mode.
if args.rw:
self.virtme_param["overlay_rwdir"] = ""
else:
self.virtme_param["overlay_rwdir"] = " ".join(
f"--overlay-rwdir {d}"
for d in ("/etc", "/lib", "/home", "/opt", "/srv", "/usr", "/var")
)
# Add user-specified overlays.
for item in args.overlay_rwdir:
self.virtme_param["overlay_rwdir"] += " --overlay-rwdir " + item
def _get_virtme_run(self, args):
if args.run is not None:
# If an upstream version is specified (using an upstream tag) fetch
# and run the corresponding kernel from the Ubuntu mainline
# repository.
if re.match(r'^v\d+(\.\d+)*$', args.run):
if args.arch is None:
arch = 'amd64'
else:
arch = args.arch
try:
mainline = KernelDownloader(args.run, arch=arch, verbose=args.verbose)
self.virtme_param["kdir"] = "--kimg " + mainline.target
except FileNotFoundError as exc:
sys.stderr.write(str(exc) + "\n")
sys.exit(1)
else:
self.virtme_param["kdir"] = "--kimg " + args.run
else:
self.virtme_param["kdir"] = "--kdir ./"
def _get_virtme_mods(self, args):
if args.skip_modules:
self.virtme_param["mods"] = "--mods none"
else:
self.virtme_param["mods"] = "--mods auto"
def _get_virtme_dry_run(self, args):
if args.dry_run:
self.virtme_param["dry_run"] = "--show-command --dry-run"
else:
self.virtme_param["dry_run"] = ""
def _get_virtme_no_virtme_ng_init(self, args):
if args.no_virtme_ng_init:
self.virtme_param["no_virtme_ng_init"] = "--no-virtme-ng-init"
else:
self.virtme_param["no_virtme_ng_init"] = ""
def _get_virtme_network(self, args):
if args.network is not None:
self.virtme_param["network"] = f"--net {args.network}"
else:
self.virtme_param["network"] = ""
def _get_virtme_disk(self, args):
if args.disk is not None:
disk_str = ""
for dsk in args.disk:
disk_str += f"--blk-disk {dsk}={dsk} "
self.virtme_param["disk"] = disk_str
else:
self.virtme_param["disk"] = ""
def _get_virtme_sound(self, args):
if args.sound:
self.virtme_param["sound"] = "--sound"
else:
self.virtme_param["sound"] = ""
def _get_virtme_disable_microvm(self, args):
# Automatically disable microvm in debug mode, since it seems to
# produce incomplete memory dumps.
if args.disable_microvm or args.debug:
self.virtme_param["disable_microvm"] = "--disable-microvm"
else:
self.virtme_param["disable_microvm"] = ""
def _get_virtme_disable_kvm(self, args):
if args.disable_kvm:
self.virtme_param["disable_kvm"] = "--disable-kvm"
else:
self.virtme_param["disable_kvm"] = ""
def _get_virtme_9p(self, args):
if args.force_9p:
self.virtme_param["force_9p"] = "--force-9p"
else:
self.virtme_param["force_9p"] = ""
def _get_virtme_initramfs(self, args):
if args.force_initramfs:
self.virtme_param["force_initramfs"] = "--force-initramfs"
else:
self.virtme_param["force_initramfs"] = ""
def _get_virtme_graphics(self, args):
if args.graphics:
self.virtme_param["graphics"] = '--graphics'
else:
self.virtme_param["graphics"] = ""
def _get_virtme_verbose(self, args):
if args.verbose:
self.virtme_param["verbose"] = "--verbose --show-boot-console"
else:
self.virtme_param["verbose"] = ""
def _get_virtme_append(self, args):
if args.append is not None:
append = []
for item in args.append:
split_items = item.split()
for split_item in split_items:
append.append("-a " + split_item)
self.virtme_param["append"] = " ".join(append)
else:
self.virtme_param["append"] = ""
def _get_virtme_memory(self, args):
if args.memory is None:
self.virtme_param["memory"] = "--memory 1G"
else:
self.virtme_param["memory"] = "--memory " + args.memory
def _get_virtme_numa(self, args):
if args.numa is not None:
numa_str = " ".join([f"--numa {numa}" for numa in args.numa])
self.virtme_param["numa"] = numa_str
else:
self.virtme_param["numa"] = ""
def _get_virtme_balloon(self, args):
if args.balloon:
self.virtme_param["balloon"] = "--balloon"
else:
self.virtme_param["balloon"] = ""
def _get_virtme_snaps(self, args):
if args.snaps:
self.virtme_param["snaps"] = "--snaps"
else:
self.virtme_param["snaps"] = ""
def _get_virtme_busybox(self, args):
if args.busybox is not None:
self.virtme_param["busybox"] = "--busybox " + args.busybox
else:
self.virtme_param["busybox"] = ""
def _get_virtme_qemu(self, args):
if args.qemu is not None:
self.virtme_param["qemu"] = "--qemu-bin " + args.qemu
else:
self.virtme_param["qemu"] = ""
def _get_virtme_cpus(self, args):
if args.cpus is None:
cpus = self.cpus
else:
cpus = args.cpus
self.virtme_param["cpus"] = f"--cpus {cpus}"
def _get_virtme_qemu_opts(self, args):
qemu_args = ""
if args.qemu_opts is not None:
qemu_args += " ".join(args.qemu_opts)
if args.debug:
# Enable vmcoreinfo (required by drgn memory dumps)
qemu_args += "-device vmcoreinfo "
# Enable debug mode and QMP (to trigger memory dump via `vng --dump`)
qemu_args += "-s -qmp tcp:localhost:3636,server,nowait "
if qemu_args != "":
self.virtme_param["qemu_opts"] = "--qemu-opts " + qemu_args
else:
self.virtme_param["qemu_opts"] = ""
def run(self, args):
"""Execute a kernel inside virtme-ng."""
self._get_virtme_name(args)
self._get_virtme_exec(args)
self._get_virtme_user(args)
self._get_virtme_arch(args)
self._get_virtme_root(args)
self._get_virtme_rw(args)
self._get_virtme_rodir(args)
self._get_virtme_rwdir(args)
self._get_virtme_overlay_rwdir(args)
self._get_virtme_cwd(args)
self._get_virtme_run(args)
self._get_virtme_dry_run(args)
self._get_virtme_no_virtme_ng_init(args)
self._get_virtme_mods(args)
self._get_virtme_network(args)
self._get_virtme_disk(args)
self._get_virtme_sound(args)
self._get_virtme_disable_microvm(args)
self._get_virtme_disable_kvm(args)
self._get_virtme_9p(args)
self._get_virtme_initramfs(args)
self._get_virtme_graphics(args)
self._get_virtme_verbose(args)
self._get_virtme_append(args)
self._get_virtme_cpus(args)