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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
#!/usr/bin/python3
import os
import sys
import glob
import subprocess
import tempfile
import shutil
import re
import functools
image = 'unstable.qcow2'
parameters = dict(
release='unstable',
#mirror='http://fr.archive.ubuntu.com/ubuntu/',
mirror='http://apt:9999/debian/',
arch='amd64',
packages='''
apt
aptitude
bash
bash-completion
bind9-host
bmon
busybox
bzip2
curl
ed
grub2
htop
iftop
ifupdown
iotop
iperf
iproute2
iptables
iputils-ping
less
lftp
linux-image-amd64
locales
ncdu
ncurses-base
ncurses-term
net-tools
netbase
netcat
nload
openssh-client
openssh-server
psmisc
python3
ranger
rsync
screen
sed
socat
strace
tar
tcpdump
telnet
tmux
tree
tzdata
vim
vim-nox
vim-runtime
w3m
wget
zsh
'''.split(),
)
open8 = functools.partial(open, encoding='utf8')
numerical_sort = lambda y: [int(x) if x.isdigit() else x
for x in re.split('(\d+)', y)]
def mlstrip(s):
return re.sub(r'^\s*', '', s, flags=re.MULTILINE)
def run(*l, **kw):
print('run:', *l)
return subprocess.run(*l, **kw)
def reexec_root():
# run as root
if os.geteuid() != 0:
os.execvp("sudo", ["sudo"] + sys.argv)
def system_customization(rootdir, bootpartuuid, rootpartuuid, rootfsuuid):
os.unlink(rootdir + '/etc/localtime')
open8(rootdir + '/etc/zoneinfo', 'w').write('Europe/Paris\n')
shutil.copy(rootdir + '/usr/share/zoneinfo/Europe/Paris',
rootdir + '/etc/localtime')
open8(rootdir + '/etc/network/interfaces', 'w').write(mlstrip(
'''\
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
'''))
open8(rootdir + '/etc/hosts', 'w').write(mlstrip(
'''\
127.0.0.1 localhost localhost.localdomain debian
# the following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
'''))
open8(rootdir + '/etc/hostname', 'w').write('debian\n')
open8(rootdir + '/etc/fstab', 'w').write(mlstrip(
'''\
# <device> <mount point> <type> <options> <dump> <pass>
PARTUUID={rid} / ext4 errors=remount-ro,relatime 0 1
PARTUUID={bid} /boot/firmware vfat errors=remount-ro,relatime 0 2
'''.format(bid=bootpartuuid,
rid=rootpartuuid)))
# activate serial console
run([
'ln',
'-s',
'/lib/systemd/system/serial-getty@.service',
rootdir + '/etc/systemd/system/getty.target.wants/serial-getty@ttyS0.service',
], check=True)
os.makedirs(rootdir + '/etc/systemd/system/serial-getty@ttyS0.service.d',
mode=0o755)
open8(rootdir +
'/etc/systemd/system/serial-getty@ttyS0.service.d/autologin.conf',
'w').write(mlstrip(
'''\
[Service]
ExecStart=
ExecStart=-/sbin/agetty --autologin root --login-pause --noclear %I 115200,38400,9600 vt102
'''))
script_dir = os.path.dirname(os.path.realpath(__file__))
os.makedirs(rootdir + '/boot/firmware/efi/boot', mode=0o755)
with tempfile.NamedTemporaryFile(mode='w', encoding='utf8') as f:
f.write(mlstrip(
'''\
search --set=root --no-floppy --fs-uuid {rid} --hint hd0,gpt2
configfile /boot/grub/grub.cfg
'''.format(rid=rootfsuuid)))
f.flush()
run([
script_dir + '/grub-2.02~beta3/grub-mkimage',
'-d', script_dir + '/grub-2.02~beta3/grub-core',
'-O', 'x86_64-efi',
'--output={}/boot/firmware/efi/boot/bootx64.efi'.format(rootdir),
'--prefix=/efi/boot',
'--config=' + f.name,
] + list(glob.glob(script_dir + '/grub-2.02~beta3/grub-core/*.mod')),
check=True)
open8(rootdir + '/etc/default/keyboard', 'w').write(mlstrip(
'''\
XKBMODEL="pc105"
XKBLAYOUT="fr"
XKBVARIANT="bepo"
XKBOPTIONS=""
'''))
open8(rootdir + '/etc/default/locale', 'w').write(mlstrip(
'''\
LANG="en_US.UTF-8"
LC_TIME="en_DK.UTF-8"
LC_PAPER="en_GB.UTF-8"
LC_MEASUREMENT="en_GB.UTF-8"
'''))
for locale in ['fr_FR', 'en_US', 'en_GB', 'en_DK', 'de_DE']:
run([
'localedef',
'--prefix={}'.format(rootdir),
'-f', 'UTF-8',
'-i', locale,
'{}.UTF-8'.format(locale)
], check=True)
open8(rootdir + '/etc/bash.bashrc', 'a').write(mlstrip(
'''\
# enable bash completion in interactive shells
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
alias ls="ls --color=aut"
alias l="ls -CF"
alias ll="l -lh"
alias la="l -a"
alias e="vim"
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
export PAGER=less
export EDITOR=vim
export VISUAL=vim
'''))
open8(rootdir + '/etc/vim/vimrc', 'w').write(mlstrip(
'''\
set nocompatible
filetype plugin indent on
set autoindent
set background=dark
set backspace=2
set hidden
set hlsearch
set ignorecase
set incsearch
set laststatus=2
set modelines=0
set nobackup
set nowritebackup
set ruler
set scrolloff=3
set shiftwidth=4
set showcmd
set showmatch
set statusline=%<%f%h%m%r%=%l,%c\ %P
set ts=4
set whichwrap=<,>,[,]
set wildmode=list:full
syntax on
'''))
open8(rootdir + '/etc/default/grub', 'w').write(mlstrip(
'''\
GRUB_DEFAULT=0
GRUB_TIMEOUT=10
GRUB_DISTRIBUTOR=Debian
GRUB_TERMINAL=console
GRUB_SERIAL_COMMAND="serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1"
GRUB_CMDLINE_LINUX_DEFAULT=""
GRUB_CMDLINE_LINUX="text console=ttyS0,115200n8 console=tty0 net.ifnames=0"
'''))
kernel_params = 'ro text console=ttyS0,115200n8'
kernel_params += ' console=tty0 net.ifnames=0'
open8(rootdir + '/boot/grub/grub.cfg', 'w').write(mlstrip(
'''\
terminal_output console
serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
set default=0
set timeout=3
menuentry 'default' {{
search --set=root --file /boot/grub/grub.cfg --hint hd0,gpt2
linux /vmlinuz root=PARTUUID={rid} {kp}
initrd /initrd.img
}}
'''.format(rid=rootpartuuid, kp=kernel_params)))
def make_image():
device = None
rootdirobj = tempfile.TemporaryDirectory()
rootdir = rootdirobj.name
try:
run(['qemu-img', 'create', '-f', 'qcow2', image, '10G'], check=True)
run(['modprobe', 'nbd', 'max_part=16'])
for d in sorted(glob.glob('/dev/nbd*'), key=numerical_sort):
if run(['qemu-nbd', '-c', d, image]).returncode == 0:
device = d
break
else:
print('No free nbd device found for qemu-nbd')
raise SystemExit(1)
run([
'sgdisk',
'--set-alignment=8192',
'--zap-all',
'--new=1:4M:+300M',
'--change-name=1:boot',
'--typecode=1:EF00',
'--largest-new=2',
'--change-name=2:root',
'--typecode=2:8300',
device,
], check=True)
run(['partprobe', device])
run(['mkfs.fat', '-n', 'boot', device+'p1'], check=True)
run(['mkfs.ext4', '-q', '-L', 'root', device+'p2'], check=True)
run(['mount', device+'p2', rootdir], check=True)
os.makedirs(rootdir + '/boot/firmware', mode=0o755)
run(['mount', device+'p1', rootdir+'/boot/firmware'], check=True)
run([
'qemu-debootstrap',
'--arch=' + parameters['arch'],
'--include=' + ','.join(parameters['packages']),
'--components=main,universe',
parameters['release'],
rootdir,
parameters['mirror'],
], check=True)
rootpartuuid = (run([
'partx',
'--noheadings',
'--show',
'--output',
'UUID',
device+'p2'], check=True, stdout=subprocess.PIPE)
.stdout
.decode('utf8')
.strip())
bootpartuuid = (run([
'partx',
'--noheadings',
'--show',
'--output',
'UUID',
device+'p1'], check=True, stdout=subprocess.PIPE)
.stdout
.decode('utf8')
.strip())
rootfsuuid = [line.split()[-1]
for line in (run([ 'tune2fs', '-l', device+'p2'],
check=True, stdout=subprocess.PIPE)
.stdout
.decode('utf8')
.splitlines())
if "filesystem uuid" in line.lower()][0]
system_customization(rootdir, bootpartuuid, rootpartuuid, rootfsuuid)
finally:
if rootdir:
cmd = ['umount'] + \
['{rootdir}/{}'.format(x, rootdir=rootdir)
for x in 'proc sys dev boot/firmware'.split()]
run(cmd)
run(['umount', rootdir])
if device:
run(['qemu-nbd', '-d', device])
def main():
reexec_root()
make_image()
if __name__ == '__main__':
main()
|