-
Notifications
You must be signed in to change notification settings - Fork 30
/
vm
executable file
·144 lines (111 loc) · 4.28 KB
/
vm
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
#!/usr/bin/env python3
import argparse
import os
import platform
import shutil
import subprocess
import sys
RESET = "\033[0m"
BOLD = "\033[1m"
def genisoimage(rundir, datdir):
args = [
"genisoimage",
"-input-charset", "utf-8",
"-output", f"{rundir}/cloudinit.iso",
"-volid", "cidata",
"-joliet",
"-rock",
"-quiet",
f"{datdir}/user-data",
f"{datdir}/meta-data"]
subprocess.run(args, check=True)
def main():
parser = argparse.ArgumentParser(description="Boot virtual machine images")
parser.add_argument('--memory', default=2048, help='Memory of the machine')
parser.add_argument('--persist', default=False, action="store_true")
parser.add_argument('--verbose', default=False, action="store_true")
parser.add_argument('--cloud-init', default="cloud-init")
parser.add_argument('image', type=str, help="The image to boot")
parser.set_defaults(architecture='x86_64')
subparsers = parser.add_subparsers(help='commands')
parser_sub = subparsers.add_parser("x86_64", help='x64 architecture',
aliases=['x64'])
parser_sub.add_argument('-U', '--uefi', dest="uefi", action='store_true', default=False,
help='Boot via UEFI')
parser_sub.add_argument('-S', '--secureboot', dest="secureboot", action='store_true', default=False,
help='Boot via UEFI and enable SecureBoot')
parser_sub.set_defaults(architecture='x86_64')
parser_sub = subparsers.add_parser("aarch64", help='ARM 64 architecture')
parser_sub.set_defaults(architecture='aarch64')
argv, extra = parser.parse_known_args()
argv = vars(argv)
runtimedir = os.getenv("XDG_RUNTIME_DIR", default="/tmp")
cfgdir = os.path.abspath(argv["cloud_init"])
rundir = os.path.join(runtimedir, "vm-cloud-init")
datdir = os.path.join(rundir, "data")
if argv["verbose"]:
print(f"cfgdir: {cfgdir}")
print(f"rundir: {rundir}")
shutil.rmtree(rundir, ignore_errors=True)
os.makedirs(rundir, exist_ok=True)
shutil.copytree(cfgdir, datdir)
genisoimage(rundir, datdir)
arch = argv["architecture"]
if arch == "x86_64":
args = ['qemu-system-x86_64']
if argv.get("secureboot", False):
args += ["-drive",
"file=/usr/share/OVMF/OVMF_CODE.secboot.fd,if=pflash,format=raw,unit=0,readonly=on",
"-drive",
"file=/usr/share/OVMF/OVMF_VARS.secboot.fd,if=pflash,format=raw,unit=1,readonly=off",
"-machine",
"q35"]
elif argv.get("uefi", False):
args += ["-drive", "file=/usr/share/OVMF/OVMF_CODE.fd,if=pflash,format=raw,unit=0,readonly=on"]
elif arch == "aarch64":
args = ['qemu-system-ppc64',
"-M", "virt",
"-bios", "/usr/share/edk2/aarch64/QEMU_EFI.fd",
"-boot", "efi",
"-cpu", "cortex-a57"]
else:
print("unsupported architecture", file=sys.stderr)
sys.exit(1)
if platform.processor() == arch:
args += ['-enable-kvm']
if not argv["persist"]:
args += ["-snapshot"]
portfwd = {
2222:22,
9091:9090,
8000:8000,
8080:8080,
8081:8081,
8082:8082,
8083:8083
}
for local, remote in portfwd.items():
print(f"port {BOLD}{local}{RESET} → {BOLD}{remote}{RESET}")
fwds = [f"hostfwd=tcp::{h}-:{g}" for h, g in portfwd.items()]
# create a new mac address based on our machine id
with open("/etc/machine-id", "r") as f:
data = f.read().strip()
maclst = ["FE"] + [data[x:x+2] for x in range(-12, -2, 2)]
macstr = ":".join(maclst)
print(f"MAC: {BOLD}{macstr}{RESET}")
args += [
"-m", str(argv["memory"]),
f"-cdrom", f"{rundir}/cloudinit.iso",
"-device", f"virtio-net-pci,netdev=n0,mac={macstr}",
"-netdev", "user,id=n0,net=10.0.2.0/24," + ",".join(fwds),
] + extra + [argv["image"]]
if argv["verbose"]:
print(" ".join(args))
try:
res = subprocess.run(args, check=False)
except KeyboardInterrupt:
print("Aborted")
return -1
return res.returncode
if __name__ == "__main__":
sys.exit(main())