-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcreate-initrd
executable file
·334 lines (286 loc) · 10.1 KB
/
create-initrd
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
#!/usr/bin/env python3
import argparse
import os
from config import CONFIG as cfg
from util import (
command,
download_tar,
ensure_root,
get_consent,
initrd_write,
list_files_in_packages,
mount_tmpfs,
umount,
warn,
)
ensure_root()
cli = argparse.ArgumentParser()
cli.add_argument('--debian-mirror', default='http://mirror.stusta.de/debian/')
cli.add_argument('--out', default=cfg.path.cpio, help="output cpio file (%(default)s)")
cli.add_argument('--compressor', default=cfg.packing.compressor)
cli.add_argument('--skip-setup', action='store_true')
cli.add_argument('--update', action='store_true')
cli.add_argument('--tmp-work', action='store_true',
help='use a tmpfs for work and cachedir')
args = cli.parse_args()
# Some random sanity checks
selected_system_modules = sum(
[mod in ('system-debian', 'system-arch', 'system-arch-dracut', 'system-gentoo') for mod in cfg.modules])
if selected_system_modules != 1:
warn("Please select exactly one system-* module")
raise SystemExit(1)
if command('uname', '-r', capture_stdout=True).decode()[:-1] not in os.listdir('/lib/modules'):
warn('Modules for running kernel not found in /lib/modules. If you just updated your arch linux, you probably '
'should reboot before continuing.\nIf you are not running arch, I don\'t know how you triggered this '
'message.\nDo you want to continue?')
if not get_consent():
raise SystemExit(1)
if not args.skip_setup:
# discard existing ramdisk content
umount(cfg.path.initrd_devel)
umount(cfg.path.work)
os.makedirs(cfg.path.work, exist_ok=True)
os.makedirs(cfg.path.cache, exist_ok=True)
# provide the various tmpfses
if args.tmp_work:
mount_tmpfs(cfg.path.work)
mount_tmpfs(cfg.path.cache)
deb_packages = [
'ifrename', # needed by the payload scripts
'iproute2', # needed by the payload scripts
'kexec-tools', # needed for booting the payload system
'linux-image-amd64', # needed for booting the stiefel system
'python3', # all payload scripts are written in Python3
'python3-pycryptodome',
'python3-aiohttp',
'systemd-container', # to allow launching inside nspawn during this script
'dbus-broker', # apparently required for systemd-container
'systemd-sysv', # to provide symlinks in /sbin: init, poweroff, ...
]
if 'nbd' in cfg.modules:
deb_packages.extend(['nbd-server'])
if 'lvm' in cfg.modules:
deb_packages.append('lvm2')
if 'i915' in cfg.modules:
deb_packages.append('firmware-misc-nonfree')
if cfg.boot.luks_block:
deb_packages.append('cryptsetup')
deb_packages.extend(cfg.initrd.include_packages)
# perform initial setup
# this logs to $workdir/$workdir_subpath_initrd/debootstrap/debootstrap.log
# -> usually "workdir/initrd.nspawn/debootstrap/debootstrap.log"
print(f"running debootstrap with log {cfg.path.initrd}/debootstrap/debootstrap.log")
command(
'debootstrap',
'--include=' + ','.join(deb_packages),
'--cache-dir=' + os.path.abspath(cfg.path.cache),
'--variant=minbase',
'--components=main,contrib,non-free',
'--merged-usr',
'--verbose',
'--merged-usr',
'stable',
cfg.path.initrd,
args.debian_mirror
)
# install our overlay
command('cp', '-RT', 'overlays/initrd', cfg.path.initrd)
# install the AES key
if not os.path.exists('aes-key'):
print('generating new AES key')
with open('aes-key', 'wb') as fileobj:
fileobj.write(os.urandom(16))
command('cp', 'aes-key', cfg.path.initrd)
if not args.skip_setup:
# set root password
command(
'chpasswd',
nspawn=cfg.path.initrd,
stdin=f'root:{cfg.initrd.password}\n'
)
# set root shell
command(
'chsh', '-s',
cfg.initrd.shell,
nspawn=cfg.path.initrd
)
# enable login from systemd-nspawn
initrd_write('/etc/securetty', 'pts/0', append=True)
# set the hostname
initrd_write('/etc/hostname', 'stiefelsystem')
# disable lidswitch handling...
initrd_write('/etc/systemd/logind.conf',
'HandleSuspendKey=ignore',
'HandleHibernateKey=ignore',
'HandleLidSwitch=ignore',
'HandleLidSwitchExternalPower=ignore',
'HandleLidSwitchDocked=ignore',
append=True
)
# systemd configuration
command('systemctl', 'set-default',
'multi-user.target',
nspawn=cfg.path.initrd
)
command('systemctl', 'enable',
'fake-entropy.service',
nspawn=cfg.path.initrd
)
command('systemctl', 'disable',
'kexec.service',
'kexec-load.service',
'machines.target',
'remote-fs.target',
nspawn=cfg.path.initrd
)
command('systemctl', 'mask',
'apt-daily.timer',
'apt-daily-upgrade.timer',
'systemd-journal-flush.service',
'systemd-timedated.service',
'systemd-timesyncd.service',
'systemd-tmpfiles-clean.timer',
'systemd-update-utmp.service',
'systemd-update-utmp-runlevel.service',
'time-sync.target',
nspawn=cfg.path.initrd
)
# stiefel configuration
if 'nbd' in cfg.modules:
command('systemctl', 'disable', 'nbd-server.service', nspawn=cfg.path.initrd)
# the nbd server configuration will be generated on-the-fly
# kernel name
kernel_name = os.readlink(cfg.path.initrd + '/vmlinuz')[13:]
# create devel overlay system which we'll use to compile a few things
os.makedirs(cfg.path.initrd_devel, exist_ok=True)
os.makedirs(cfg.path.initrd_devel + "-overlayfs-upperdir", exist_ok=True)
os.makedirs(cfg.path.initrd_devel + "-overlayfs-workdir", exist_ok=True)
command(
"mount",
"-t", "overlay",
"overlay",
"-o", ",".join([
f"lowerdir={os.path.abspath(cfg.path.initrd)}",
f"upperdir={os.path.abspath(cfg.path.initrd_devel + '-overlayfs-upperdir')}",
f"workdir={os.path.abspath(cfg.path.initrd_devel + '-overlayfs-workdir')}",
]),
os.path.abspath(cfg.path.initrd_devel),
)
command('apt', 'update', nspawn=cfg.path.initrd_devel)
command('apt', 'upgrade', nspawn=cfg.path.initrd_devel)
command('apt', 'install', '-y',
'build-essential',
'git',
'linux-headers-amd64',
'kmod',
nspawn=cfg.path.initrd_devel
)
# compile and install the userland driver for the clevo fan controller
if 'clevo-fancontrol' in cfg.modules:
download_tar(
cfg.mod_config["clevo-fancontrol"].url,
os.path.join(cfg.path.initrd_devel, "root/fancontrol")
)
command('make', '-C', '/root/fancontrol', nspawn=cfg.path.initrd_devel)
command(
"cp",
cfg.path.initrd_devel + "/root/fancontrol/clevo-fancontrol",
cfg.path.initrd + "/usr/local/bin",
)
command(
"cp",
cfg.path.initrd_devel + "/root/fancontrol/clevo-fancontrol.service",
cfg.path.initrd + "/etc/systemd/system",
)
command('systemctl', 'enable',
'clevo-fancontrol.service',
nspawn=cfg.path.initrd
)
if 'r8152' in cfg.modules:
download_tar(
cfg.mod_config["r8152"].url,
os.path.join(cfg.path.initrd_devel, "root/r8152")
)
command(
'make',
'-C', '/root/r8152',
f'KERNELDIR=/lib/modules/{kernel_name}/build',
'modules',
nspawn=cfg.path.initrd_devel
)
command(
"cp",
cfg.path.initrd_devel + "/root/r8152/r8152.ko",
cfg.path.initrd + f"/lib/modules/{kernel_name}/kernel/drivers/net/usb/",
)
os.makedirs(cfg.path.initrd + "/etc/udev/rules.d", exist_ok=True)
command(
"cp",
cfg.path.initrd_devel + "/root/r8152/50-usb-realtek-net.rules",
cfg.path.initrd + "/etc/udev/rules.d/"
)
command(
"depmod",
"-a",
kernel_name,
nspawn=cfg.path.initrd,
)
if args.update:
command('apt', 'update', nspawn=cfg.path.initrd_devel)
command('apt', 'upgrade', nspawn=cfg.path.initrd_devel)
if 'debug' in cfg.modules:
command('updatedb', nspawn=cfg.path.initrd)
if not args.skip_setup:
if 'debug' not in cfg.modules:
# strip kernel modules
count = 0
for path, _, files in os.walk(cfg.path.initrd + '/lib/modules'):
for filename in files:
if filename.endswith('.ko'):
full_path = os.path.join(path, filename)
command('strip', '--strip-debug', full_path, silent=count)
count += 1
if count > 1:
print(f'$ ... (stripped {count - 1} more modules)')
# the folder is ready, now we can pack and compress the initrd
paths_to_exclude = set(
list_files_in_packages(cfg.packing.exclude_packages, cfg.path.initrd)
)
paths_to_exclude.update(
path.encode() for path in cfg.packing.exclude_paths
)
old_cwd = os.getcwd()
os.chdir(cfg.path.initrd)
target_files = []
def scan_path(path):
for name in os.listdir(path):
full = os.path.normpath(os.path.join(path, name))
if full in paths_to_exclude:
continue
if full.endswith(b'__pycache__'):
continue
yield full
# recurse into directories (don't follow links)
if os.path.isdir(full) and not os.path.islink(full):
yield from scan_path(full)
print(f'packing {cfg.path.initrd}')
archive = command(
"bsdcpio", "-0", "-o", "-H", "newc",
stdin=b'\0'.join(scan_path(b'.')) + b'\0',
capture_stdout=True,
env={"LANG": "C"},
)
os.chdir(old_cwd)
del old_cwd
print(f"uncompressed CPIO: {len(archive)} bytes")
compressed = command(
f"pv -s {len(archive)} | {args.compressor}",
shell=True,
stdin=archive,
capture_stdout=True
)
print(f"compressed CPIO: {len(compressed)} bytes")
with open(args.out, "wb") as cpiofile:
cpiofile.write(compressed)