-
Notifications
You must be signed in to change notification settings - Fork 30
/
mkboot
executable file
·173 lines (137 loc) · 4.46 KB
/
mkboot
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
#!/usr/bin/python3
import argparse
import contextlib
import os
import pycdlib
import shutil
import subprocess
import sys
import tempfile
from string import Template
@contextlib.contextmanager
def mount_iso(iso, path):
subprocess.run(["mount", "-t", "iso9660", "-o", "loop,ro", iso, path],
check=True)
try:
yield path
finally:
subprocess.run(["umount", path])
def delete_generated(path, verbose):
for root, dirs, files in os.walk(path):
for f in files:
if f in ["TRANS.TBL", "boot.cat", "boot.catalog"]:
t = os.path.join(root, f)
if verbose:
print(f"unlink {t}")
os.unlink(t)
def make_iso(path, label, output, workdir, verbose):
cmd = [
"/usr/bin/xorrisofs"
]
if verbose:
cmd += ["-verbose"]
else:
cmd += ["-quiet"]
cmd += [
"-V", label,
]
cmd += [
"-b", "isolinux/isolinux.bin",
"-c", "isolinux/boot.cat",
"-no-emul-boot",
"-boot-load-size", "4",
"-boot-info-table"
]
cmd += [
"-rock", "-joliet"
]
cmd += [
"-eltorito-alt-boot",
"-e", "images/efiboot.img",
"-no-emul-boot",
"-isohybrid-gpt-basdat",
]
if os.path.exists("/usr/share/syslinux/isohdpfx.bin"):
cmd += [
"-isohybrid-mbr", "/usr/share/syslinux/isohdpfx.bin",
]
cmd += [
'-o', output,
path
]
if verbose:
print(cmd)
stdout = sys.stdout
else:
stdout = subprocess.DEVNULL
subprocess.run(cmd,
cwd=workdir,
stdout=stdout,
stderr=stdout,
check=True)
def get_label(path):
try:
iso = pycdlib.PyCdlib()
iso.open(path)
label = iso.pvd.volume_identifier.decode("UTF-8").strip()
except pycdlib.PyCdlibException as e:
label = ""
return label
def write_template(source, dest, **subst):
with open(source, "r") as f:
tpl = Template(f.read())
data = tpl.substitute(**subst)
with open(dest, "w") as f:
f.write(data)
def main():
parser = argparse.ArgumentParser(description="Modify boot iso")
parser.add_argument("iso", metavar="ISO", type=os.path.abspath,
help="original ISO to modify")
parser.add_argument("--output", metavar="FILENAME", type=os.path.abspath,
default="bootiso.iso",
help="Name of the output file")
parser.add_argument("--kickstart", metavar="FILENAME", type=os.path.abspath,
default="edge.ks",
help="Name of the kickstart file to embed")
parser.add_argument("--verbose", default=False, action="store_true",
help="Show more information")
parser.add_argument("--kargs", metavar="ID", action="append", type=str, default=["quiet"],
help="Additional kernel command line args")
args = parser.parse_args(sys.argv[1:])
bootiso = args.iso
output = args.output
ks_path = args.kickstart
ks_name = os.path.basename(ks_path)
kargs = " ".join(args.kargs)
label = get_label(bootiso)
print(f"Label: {label}")
print(f"Kickstart: {ks_name} ({ks_path})")
print(f"Kargs: {kargs}")
with tempfile.TemporaryDirectory(dir="/var/tmp") as tmp:
mountpath = os.path.join(tmp, "mount")
isopath = os.path.join(tmp, "iso")
os.makedirs(mountpath)
os.makedirs(isopath)
with mount_iso(bootiso, mountpath) as path:
subprocess.run(["cp", "-a", f"{path}/.", f"{isopath}/"],
check=True)
delete_generated(isopath, args.verbose)
bootcfg = {
"isolinux.cfg" : "isolinux",
"grub.cfg": "EFI/BOOT",
}
subst = {
"label": label,
"kickstart": ks_name,
"kargs": kargs
}
for filename, path in bootcfg.items():
src = os.path.join("boot", filename)
dst = os.path.join(isopath, path, filename)
write_template(src, dst, **subst)
# write the kickstart
dst = os.path.join(isopath, ks_name)
write_template(ks_path, dst, **subst)
make_iso(isopath, label, output, tmp, args.verbose)
if __name__ == "__main__":
main()