- Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathsandbox.py
executable file
·99 lines (86 loc) · 2.67 KB
/
sandbox.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
#!/usr/bin/env python3
# Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# pylint:disable=invalid-name
"""
Run Firecracker in an IPython REPL
"""
importargparse
importjson
importre
frompathlibimportPath
fromframework.artifactsimportdisks, kernels
fromframework.defsimportDEFAULT_BINARY_DIR
fromframework.microvmimportMicroVMFactory
kernels=list(kernels("vmlinux-*"))
rootfs=list(disks("ubuntu*ext4"))
defparse_byte_size(param):
"""
>>> parse_byte_size("1MB")
1048576
"""
unit= {
"MB": 2**20,
"GB": 2**30,
}
match=re.match(r"(?P<val>\d+)(?P<unit>[MG]B)", param.upper())
returnint(match.group("val")) *unit[match.group("unit")]
parser=argparse.ArgumentParser()
parser.add_argument(
"--kernel",
type=Path,
choices=kernels,
default=kernels[-1],
help=f"Kernel to use. [{kernels[-1]}]",
)
parser.add_argument(
"--rootfs",
type=Path,
choices=rootfs,
default=rootfs[-1],
help=f"Rootfs to use. [{rootfs[-1]}]",
)
parser.add_argument("--vcpus", type=int, default=2)
parser.add_argument(
"--guest-mem-size",
type=parse_byte_size,
default=128*2**20, # 128MB
)
parser.add_argument("--rootfs-size", type=parse_byte_size, default=1*2**30) # 1GB
parser.add_argument("--binary-dir", help="Path to the firecracker binaries")
parser.add_argument("--cpu-template-path", help="CPU template to use", type=Path)
args=parser.parse_args()
print(args)
binary_dir=None
ifargs.binary_dir:
binary_dir=Path(args.binary_dir).resolve()
else:
binary_dir=DEFAULT_BINARY_DIR
cpu_template=None
ifargs.cpu_template_pathisnotNone:
cpu_template=json.loads(args.cpu_template_path.read_text())
vmfcty=MicroVMFactory(binary_dir)
print(f"uvm with kernel {args.kernel} ...")
uvm=vmfcty.build(args.kernel, args.rootfs)
uvm.help.enable_console()
uvm.help.resize_disk(uvm.rootfs_file, args.rootfs_size)
uvm.spawn(log_show_level=True)
uvm.help.print_log()
uvm.add_net_iface()
uvm.basic_config(vcpu_count=args.vcpus, mem_size_mib=args.guest_mem_size//2**20)
ifcpu_templateisnotNone:
uvm.api.cpu_config.put(**cpu_template)
print(cpu_template)
uvm.start()
uvm.get_all_metrics()
kernel_dbg_dir=args.kernel.parent/"debug"
kernel_dbg=kernel_dbg_dir/args.kernel.name
print(f"uvm2 with kernel {kernel_dbg} ...")
uvm2=vmfcty.build(kernel_dbg, args.rootfs)
uvm2.spawn()
uvm2.add_net_iface()
uvm2.basic_config(vcpu_count=args.vcpus, mem_size_mib=args.guest_mem_size//2**20)
uvm2.start()
# trace-cmd needs this (DNS resolution?)
uvm2.help.enable_ip_forwarding()
files=uvm2.help.trace_cmd_guest(["-l", "read_msr"], cmd="sleep 5")