Repository navigation
Expand file tree
/
Copy pathroot_image
More file actions
executable file
·425 lines (343 loc) · 11.2 KB
/
Copy pathroot_image
File metadata and controls
executable file
·425 lines (343 loc) · 11.2 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env bash
#
# Create a VM image suitable for running automated tests
# Output: vm_image
set -o nounset
set -o errexit
set -o errtrace
ktest_dir=$(dirname "$(readlink -f "$0")")
debootstrap=$ktest_dir/debootstrap/debootstrap
. "$ktest_dir/lib/util.sh"
. "$ktest_dir/lib/common.sh"
if [[ $(id -u) != 0 && "$1" != "sync" ]] ; then
echo this script must be run as root
exit 1
fi
checkdep fallocate util-linux
checkdep mkfs.ext4 e2fsprogs
checkdep curl
IMAGE_SIZE="16G"
MIRROR=https://deb.debian.org/debian/
RELEASE=sid
usage()
{
echo "root_image: create/update virtual machine root images for ktest"
echo "Usage: root_image cmd [options]"
echo " create Create a new image"
echo " update Update an existing image"
echo " sync Copy directory to an existing image"
echo
echo "options:"
echo " -h Display this help and exit"
echo " -a <arch> Architecture for vm image"
echo " -m <mirror> Debian mirror"
echo ' -i <image> Image to create/update, defaults to /var/lib/ktest/root.$arch'
}
if [[ $# = 0 ]]; then
usage
exit 1
fi
ktest_image=""
CMD="cmd_$1"
shift
while getopts "ha:m:i:" arg; do
case $arg in
h)
usage
exit 0
;;
a)
ktest_arch=$OPTARG
;;
m)
MIRROR=$OPTARG
;;
i)
ktest_image=$OPTARG
;;
esac
done
shift $(( OPTIND - 1 ))
parse_arch "$ktest_arch"
[[ -z $ktest_image ]] && ktest_image=/var/lib/ktest/root.$DEBIAN_ARCH
mkdir -p "$(dirname "$ktest_image")"
PACKAGES=(kexec-tools less psmisc openssh-server curl \
pciutils \
pkg-config libtool-bin \
gdb strace linux-perf trace-cmd blktrace sysstat iotop htop \
hdparm mdadm lvm2 \
btrfs-progs jfsutils nilfs-tools f2fs-tools \
bc attr gawk acl rsync git python3-docutils \
stress-ng lsof xxd zstd ripgrep)
# BPF tooling isn't usable on i386, and apt installs the list in one go, so
# either name fails the whole install and no image is created:
#
# bpftrace not built for i386 at all
# bpfcc-tools present, but uninstallable - python3-bpfcc needs libbpfcc,
# which isn't built for i386 either
#
# Those two are the only problems in this list, established by walking the
# Depends closure of every package here against dists/sid/main/binary-i386
# /Packages (and binary-amd64 as a control, which comes back clean). Checking
# that the *names* exist is not enough - it's what missed bpfcc-tools, whose
# name is present and whose dependency isn't.
#
# An `if`, not `[[ ]] && ...`: this file runs under errexit, so the && form
# would exit 1 on i386 - the one arch it exists for.
if [[ $DEBIAN_ARCH != i386 ]]; then
PACKAGES+=(bpftrace bpfcc-tools)
fi
# build tools:
PACKAGES+=(build-essential make gcc g++ clang lld)
PACKAGES+=(autoconf automake autopoint bison flex pahole)
PACKAGES+=(cargo mold)
PACKAGES+=(rustc rust-src rust-analyzer librust-bindgen-dev bindgen)
PACKAGES+=(devscripts debhelper sbuild dh-sequence-dkms dkms)
# stress testing:
PACKAGES+=(fio dbench bonnie++ fsmark)
# bcachefs-tools build dependencies:
PACKAGES+=(libblkid-dev uuid-dev libscrypt-dev libsodium-dev)
PACKAGES+=(libkeyutils-dev liburcu-dev libudev-dev zlib1g-dev libattr1-dev systemd-dev)
PACKAGES+=(libaio-dev libzstd-dev liblz4-dev libfuse3-dev valgrind)
PACKAGES+=(llvm libclang-dev libunwind-dev libelf-dev)
# The boot splash, so that what mount.bcachefs draws on it can be looked at:
# plymouth-label is not optional here, it is the plugin that renders text, and
# text is the entire thing we want to see.
PACKAGES+=(plymouth plymouth-themes plymouth-label)
# quota tools (linuxquota's rquota/RPC support needs the tirpc headers):
PACKAGES+=(libudev-dev libldap2-dev libtirpc-dev)
# xfstests:
PACKAGES+=(acct bsdextrautils xfsprogs xfslibs-dev quota libcap2-bin)
PACKAGES+=(libattr1-dev libaio-dev libgdbm-dev libacl1-dev gettext)
PACKAGES+=(libssl-dev libgdbm-dev libgdbm-compat-dev liburing-dev)
PACKAGES+=(duperemove fsverity)
# xfsprogs:
PACKAGES+=(libinih-dev)
# bcachefs:
# Not currently packaged by debian:
#PACKAGES+=(bcachefs-tools)
# nfs testing:
PACKAGES+=(nfs-kernel-server)
# ocfs2 testing:
PACKAGES+=(ocfs2-tools)
# afs
PACKAGES+=(openafs-dbserver openafs-fileserver)
# nbd testing
PACKAGES+=(nbd-client nbd-server)
# dm testing:
PACKAGES+=(cryptsetup)
# weird block layer crap
PACKAGES+=(multipath-tools sg3-utils srptools)
# PuzzleFS support
PACKAGES+=(capnproto jq)
PACKAGES+=(erofs-utils squashfs-tools-ng)
# ZFS support
#PACKAGES+=("linux-headers-generic" dkms zfsutils-linux zfs-dkms)
# Lustre support
PACKAGES+=(libyaml-0-2 libyaml-dev)
# suspend testing:
# [[ $KERNEL_ARCH = x86 ]] && PACKAGES+=(uswsusp)
EXCLUDE=(dmidecode nano rsyslog logrotate cron \
iptables nfacct \
debconf-i18n info gnupg libpam-systemd)
SYSTEMD_MASK=(dev-hvc0.device \
getty.target \
getty-static.service \
avahi-daemon.service \
crond.service \
exim4.service \
kdump.service \
hdparm.service \
cdrom.mount \
mdadm-raid.service \
lvm2-activation-early.service \
aoetools.service \
sysstat.service \
kexec-load.service \
kexec.service \
systemd-ask-password-console.path \
systemd-ask-password-wall.path \
systemd-update-utmp-runlevel.service \
systemd-update-utmp.service \
time-sync.target \
multipathd.service)
export DEBIAN_FRONTEND=noninteractive
export DEBCONF_NONINTERACTIVE_SEEN=true
export LC_ALL=C
export LANGUAGE=C
export LANG=C
# We compute it here to avoid on hosts systems (e.g. NixOS)
# that does not possess `chroot` in the isolated `PATH`
# to fail miserably.
export CHROOT=$(which chroot)
_chroot()
{
# Pin TMPDIR/TMP too: NixOS/nix-shell points them at a per-session dir
# under /tmp that doesn't exist inside the chroot, so apt/dpkg tempfiles
# blow up. /tmp always exists in a debootstrap'd rootfs.
PATH=/usr/sbin:/usr/bin:/sbin:/bin TMPDIR=/tmp TMP=/tmp "$CHROOT" "$@"
}
update_files()
{
install -m0644 "$ktest_dir/lib/fstab" "$MNT/etc/fstab"
install -m0755 "$ktest_dir/lib/testrunner.wrapper" "$MNT/sbin/testrunner.wrapper"
install -m0644 "$ktest_dir/lib/testrunner.service" "$MNT/lib/systemd/system/testrunner.service"
ln -sf /lib/systemd/system/testrunner.service "$MNT/etc/systemd/system/multi-user.target.wants/testrunner.service"
touch "$MNT/etc/resolv.conf"
chmod 644 "$MNT/etc/resolv.conf"
mkdir -p "$MNT/root/"
install -m0644 "$MNT/etc/skel/.bashrc" "$MNT/root/"
install -m0644 "$MNT/etc/skel/.profile" "$MNT/root/"
cat >> "$MNT/root/.bashrc" <<-ZZ
export PROMPT_COMMAND="history -a; \$PROMPT_COMMAND"
ZZ
mkdir -p "$MNT/var/log/core"
chmod 777 "$MNT/var/log/core"
# Disable systemd/udev stuff we don't need:
# systemctl mask doesn't work for foreign archs
#_chroot "$MNT" systemctl mask "${SYSTEMD_MASK[@]}"
for i in "${SYSTEMD_MASK[@]}"; do
(cd "$MNT/etc/systemd/system"; ln -sf /dev/null "$i")
done
cat > "$MNT/etc/systemd/journald.conf" <<-ZZ
[Journal]
Storage=none
ForwardToConsole=no
MaxLevelConsole=emerg
ZZ
mkdir -p "$MNT/etc/network"
cat > "$MNT/etc/network/interfaces" <<-ZZ
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
ZZ
# disable network interface renaming - it's unreliable
mkdir -p "$MNT/etc/udev/rules.d/"
ln -sf /dev/null "$MNT/etc/udev/rules.d/80-net-setup-link.rules"
rm -f "$MNT/lib/udev/rules.d/*persistent*"
rm -f "$MNT/lib/udev/rules.d/*lvm*"
rm -f "$MNT/lib/udev/rules.d/*dm*"
rm -f "$MNT/lib/udev/rules.d/*md-raid*"
rm -f "$MNT/lib/udev/rules.d/*btrfs*"
rm -f "$MNT/lib/udev/rules.d/*hdparm*"
echo $(hostname)-kvm > "$MNT/etc/hostname"
ln -sf /ktest-out/.bash_history "$MNT/root/.bash_history"
# Host tools built on a nix host - kbuild's fixdep, objtool and friends -
# have /nix/store/.../ld-linux baked in as their ELF interpreter. The guest
# reaches them through /host, so the binaries are there but the interpreter
# path isn't, and exec fails as a bare "not found" naming the binary rather
# than the loader.
#
# /host is the whole host root, so the store is already mounted; this just
# makes the absolute path resolve. Dangling and harmless on a non-nix host.
ln -sf /host/nix "$MNT/nix"
}
update_packages()
{
# systemd... !?
mkdir -p "$MNT"/run/user/0
cp /etc/resolv.conf "$MNT/etc/resolv.conf"
mkdir -p "$MNT"/etc/dpkg/dpkg.cfg.d/
echo force-unsafe-io > "$MNT"/etc/dpkg/dpkg.cfg.d/dpkg-unsafe
_chroot "$MNT" mount -t proc none /proc
_chroot "$MNT" apt-get -qq update
_chroot "$MNT" apt-get -qq upgrade
_chroot "$MNT" apt-get -qq install --no-install-recommends "${PACKAGES[@]}"
rm -f "$MNT/var/cache/apt/archives/*.deb"
}
_umount_image()
{
umount --recursive "$MNT"
rmdir "$MNT"
trap '' EXIT
}
umount_image()
{
_umount_image
e2fsck -y -f "$ktest_image".new
resize2fs -M "$ktest_image".new # shrinks the file
resize2fs "$ktest_image".new "$IMAGE_SIZE" # re-grows as sparse
mv "$ktest_image".new "$ktest_image"
}
mount_image()
{
MNT=$(mktemp --tmpdir -d $(basename "$0")-XXXXXXXXXX)
trap '_umount_image; rm -f "$ktest_image".new' EXIT
cp "$ktest_image" "$ktest_image".new
mount "$ktest_image".new "$MNT"
}
cmd_update()
{
if [[ ! -e $ktest_image ]]; then
echo "$ktest_image does not exist"
exit 1
fi
mount_image
update_packages
update_files
umount_image
}
cmd_update_files()
{
if [[ ! -e $ktest_image ]]; then
echo "$ktest_image does not exist"
exit 1
fi
mount_image
update_files
umount_image
}
cmd_sync() {
local source="${1:-$(pwd)/}"
local source_dir="$(basename "$source")"
local new_img="/tmp/ktest-out/$(basename $ktest_image).new"
cp "$ktest_image" "$new_img"
# Remove old directory
guestfish -a "$new_img" -m /dev/sda:/ rm-rf "/workspace/"
guestfish -a "$new_img" -m /dev/sda:/ mkdir-p "/workspace/lustre-release"
# Copy new directory in
virt-copy-in -a "$new_img" "$source/." /workspace/lustre-release
}
cmd_init()
{
(cd "$ktest_dir"; git submodule update --init debootstrap)
}
cmd_create()
{
if ! [[ -f $debootstrap ]]; then
echo "Run root_image init to prep debootstrap"
exit 1
fi
if [[ -e $ktest_image ]]; then
echo "$ktest_image already exists"
exit 1
fi
MNT=$(mktemp --tmpdir -d $(basename "$0")-XXXXXXXXXX)
trap '_umount_image; rm -f "$ktest_image".new' EXIT
fallocate -l "$IMAGE_SIZE" "$ktest_image".new
chmod 644 "$ktest_image".new
mkfs.ext4 -F "$ktest_image".new
mount "$ktest_image".new "$MNT"
mkdir -p "$MNT"/etc/dpkg/dpkg.cfg.d/
echo force-unsafe-io > "$MNT"/etc/dpkg/dpkg.cfg.d/dpkg-unsafe
DEBOOTSTRAP_DIR=$ktest_dir/debootstrap $debootstrap \
--no-check-gpg \
--arch="$DEBIAN_ARCH" \
--exclude=$(join_by , "${EXCLUDE[@]}") \
--no-merged-usr \
--foreign \
$RELEASE "$MNT" "$MIRROR"
mkdir -p "$MNT"/etc/dpkg/dpkg.cfg.d/
echo force-unsafe-io > "$MNT"/etc/dpkg/dpkg.cfg.d/dpkg-unsafe
_chroot "$MNT" /debootstrap/debootstrap --second-stage
_chroot "$MNT" dpkg --configure -a
update_packages
update_files
umount_image
}
if [[ $(type -t "$CMD") != function ]]; then
usage
exit 1
fi
$CMD "$@"