-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathboot-uml.py
More file actions
executable file
·70 lines (55 loc) · 1.99 KB
/
boot-uml.py
File metadata and controls
executable file
·70 lines (55 loc) · 1.99 KB
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
#!/usr/bin/env python3
# pylint: disable=invalid-name
import argparse
from pathlib import Path
import subprocess
import utils
def parse_arguments():
"""
Parses arguments to script.
Returns:
A Namespace object containing key values from parser.parse_args()
"""
parser = argparse.ArgumentParser()
parser.add_argument(
'-g',
'--gh-json-file',
help='Use file for downloading rootfs images, instead of querying GitHub API directly',
)
parser.add_argument(
"-i",
"--interactive",
action="store_true",
help="Instead of immediately shutting down upon successful boot, pass 'init=/bin/sh' to the UML executable to allow interacting with UML via a shell.",
)
parser.add_argument(
"-k",
"--kernel-location",
required=True,
type=str,
help="Path to UML executable ('linux') or kernel build folder to search for executable in. Can be an absolute or relative path.",
)
return parser.parse_args()
def run_kernel(kernel_image: Path, rootfs: Path, interactive: bool) -> None:
"""
Run UML command with path to rootfs and additional arguments based on user
input.
Parameters:
* kernel_image (Path): kernel Path object containing full path to kernel.
* rootfs (Path): rootfs Path object containing full path to rootfs.
* interactive (bool): Whether or not to run UML interactively.
"""
uml_cmd = [kernel_image, f"ubd0={rootfs}"]
if interactive:
uml_cmd += ["init=/bin/sh"]
print(f"$ {' '.join([str(element) for element in uml_cmd])}")
subprocess.run(uml_cmd, check=True)
if __name__ == '__main__':
args = parse_arguments()
kernel = utils.get_full_kernel_path(args.kernel_location, "linux")
initrd = utils.prepare_initrd(
'x86_64',
rootfs_format='ext4',
gh_json_file=Path(args.gh_json_file).resolve() if args.gh_json_file else None,
)
run_kernel(kernel, initrd, args.interactive)