forked from koverstreet/ktest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root_image
executable file
·438 lines (369 loc) · 14 KB
/
root_image
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
426
427
428
429
430
431
432
433
434
435
436
437
438
#!/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 ]] ; then
echo this script must be run as root
exit 1
fi
checkdep fallocate util-linux
checkdep mkfs.ext4 e2fsprogs
checkdep curl
IMAGE_SIZE="10G"
#http is preferred over https here: on emulated CPUs, the encryption simply isn't worth it
MIRROR=http://deb.debian.org/debian/
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
echo "options:"
echo " -h Display this help and exit"
echo " -a <arch> Architecture for vm (x86_64,armhf,i386,aarch64,sparc64,s390x,ppc64,riscv64)"
echo " -m <mirror> Debian mirror"
echo ' -i <image> Image to create/update, defaults to /var/lib/ktest/root.$arch'
}
intervene()
{
local yn="N";
echo "Installing $2 failed: both binary install and $1";
echo "You can start a shell to check whether any dependency issues can be fixed"
echo "if you do (y), and exit the shell, it is assumed to be fixed and installing will continue"
echo "if you don't, we give up."
echo "you can also choose to skip package installation (s), though this is not recommended"
read -p "Do you wish to start a shell to investigate? (y/s/N)" yn
case $yn in
[Yy]* )
_chroot "$MNT" /tmpbuild/executer.sh /bin/bash;
;;
[sS]* )
return 0;
;;
* ) return 1;;
esac
}
install_fix_deps()
{
trap 'echo "Could not install packages! consider switching to a supported architecture"; umount_image; rm "$ktest_image"' EXIT
#debian has a separate dist for "experimental" / broken packages, in debian-ports it is called "unreleased".
experimental_dist="experimental" && [[ ${MIRROR} == *"debian-ports"* ]] && experimental_dist=unreleased;
SNAPSHOT_MIRROR="https://snapshot.debian.org/archive/debian" && [[ ${MIRROR} == *"debian-ports"* ]] && SNAPSHOT_MIRROR+="-ports";
#get necessary certificates / pgp keyrings to work with unofficial and outdated snapshots
_chroot "$MNT" apt-get -qq install debian-ports-archive-keyring ca-certificates
#disable unwanted checks - we know the snapshots are not valid
echo "Acquire::Check-Valid-Until false;" > $MNT/etc/apt/apt.conf.d/10-nocheckvalid
#create a sources.list with more options to work around missing packages
echo "deb [trusted=yes] $MIRROR sid main contrib" > $MNT/etc/apt/sources.list
echo "deb-src [trusted=yes] $MIRROR sid main contrib" >> $MNT/etc/apt/sources.list
echo "deb [trusted=yes] $MIRROR ${experimental_dist} main contrib" >> $MNT/etc/apt/sources.list
echo "deb-src [trusted=yes] $MIRROR ${experimental_dist} main contrib" >> $MNT/etc/apt/sources.list
#add snapshots specified in cross.conf
for i in "${SID_SNAPSHOTS[@]}"; do
echo "deb [trusted=yes] ${SNAPSHOT_MIRROR}/$i sid main" >> $MNT/etc/apt/sources.list
echo "deb-src [trusted=yes] ${SNAPSHOT_MIRROR}/$i sid main" >> $MNT/etc/apt/sources.list
done
#when working with debian-ports, add the general sources as a last hope ... there *may* be a package compiling for this archwe need
[[ ${MIRROR} == *"debian-ports"* ]] && echo "deb-src [trusted=yes] http://deb.debian.org/debian sid main contrib" >> $MNT/etc/apt/sources.list
touch $MNT/etc/passwd
touch $MNT/etc/shadow
_chroot "$MNT" apt-get -qq --allow-unauthenticated --allow-insecure-repositories update --fix-missing
_chroot "$MNT" apt-get -qq --allow-unauthenticated upgrade
_chroot "$MNT" apt-get -qq install -f
_chroot "$MNT" apt-get -qq install build-essential
#if we can install them all at once, do so, otherwise, try one by one
_chroot "$MNT" apt-get -qq install -m --allow-unauthenticated --no-install-recommends "${PACKAGES[@]}" || for i in "${PACKAGES[@]}"; do
if [[ ! $(_chroot "$MNT" apt-cache show $i | grep Version) == "" ]]; then
_chroot "$MNT" apt-get -qq install --allow-unauthenticated --no-install-recommends $i && continue;
fi
#installing binary failed. Try to install from source. Do it in a tmpfs folder so the image doesn't get overwhelmed:
[[ ! -d ${MNT}/tmpbuild ]] && mkdir ${MNT}/tmpbuild;
mount -t tmpfs none ${MNT}/tmpbuild;
echo '#!/bin/bash' > "$MNT"/tmpbuild/executer.sh
echo 'cd /tmpbuild/; $@' >> "$MNT"/tmpbuild/executer.sh
chmod ago+x "$MNT"/tmpbuild/executer.sh
_chroot "$MNT" /tmpbuild/executer.sh apt-get -qq build-dep --allow-unauthenticated --no-install-recommends $i || intervene install-deps $i;
#in case the intervention also installed it, continue:
if [[ $(_chroot "$MNT" dpkg -l | grep "^ii $i") == "" ]]; then
_chroot "$MNT" /tmpbuild/executer.sh apt-get -qq -b source --allow-unauthenticated $i || intervene build $i;
[ -f "${MNT}/tmpbuild/${i}*.deb" ] && _chroot "$MNT" apt-get -qq install /tmpbuild/${i}*.deb;
fi
umount ${MNT}/tmpbuild;
rmdir ${MNT}/tmpbuild;
done
}
if [[ $# = 0 ]]; then
usage
exit 1
fi
ktest_image=""
ktest_arch="$(uname -m)"
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 \
build-essential make gcc g++ \
autoconf automake autopoint bison \
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)
# 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)
PACKAGES+=(libaio-dev libzstd-dev liblz4-dev libfuse3-dev valgrind)
PACKAGES+=(llvm libclang-dev)
# quota tools:
PACKAGES+=(libudev-dev libldap2-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)
# nfs testing:
PACKAGES+=(nfs-kernel-server)
# nbd testing
PACKAGES+=(nbd-client nbd-server)
# dm testing:
PACKAGES+=(cryptsetup)
# weird block layer crap
PACKAGES+=(multipath-tools sg3-utils srptools)
# ZFS support
PACKAGES+=("linux-headers-generic")
# unless no other option when cross-compiling, ignore ZFS
# DKMS needs to cross-compile the module,
# against a different kernel on a different CPUarchitecture.
# this has to cause errors
[[ -z ${CROSS_COMPILE} ]] && PACKAGES+=(dkms zfsutils-linux zfs-dkms)
# suspend testing:
# [[ $KERNEL_ARCH = x86 ]] && PACKAGES+=(uswsusp)
EXCLUDE=(dmidecode nano rsyslog logrotate cron \
iptables nfacct vim-tiny \
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()
{
PATH=/usr/sbin:/usr/bin:/sbin:/bin "$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/"
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"
}
update_packages()
{
NO_RUSTUP=
# systemd... !?
mkdir -p "$MNT"/run/user/0
cp /etc/resolv.conf "$MNT/etc/resolv.conf"
#don't do rustup on unsupported architectures, just try to install from debian repositories
[[ -z $(curl -qq -I "https://static.rust-lang.org/rustup/dist/${RUST_TRIPLE}/rustup-init" 2>/dev/null | grep "HTTP/2 200") ]] && NO_RUSTUP=1
[[ -n $NO_RUSTUP ]] && PACKAGES+=(rustc rustc-dbgsym rustfmt rustfmt-dbgsym)
_chroot "$MNT" mount -t proc none /proc
_chroot "$MNT" mount -t devpts none /dev/pts
if [[ $MIRROR == *"debian-ports"* || $1 == "sid" ]]; then
install_fix_deps
else
_chroot "$MNT" apt-get -qq --allow-unauthenticated --allow-insecure-repositories update --fix-missing
_chroot "$MNT" apt-get -qq --allow-unauthenticated upgrade
_chroot "$MNT" apt-get -qq install -f
_chroot "$MNT" apt-get -qq install -m --allow-unauthenticated --no-install-recommends "${PACKAGES[@]}"
fi
rm -f "$MNT/var/cache/apt/archives/*.deb"
if [[ -z $NO_RUSTUP ]]; then
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > "$MNT"/tmp/rustup.sh
chmod 755 "$MNT"/tmp/rustup.sh
_chroot "$MNT" /tmp/rustup.sh --default-host ${RUST_TRIPLE} -y
echo 'export PATH="$HOME/.cargo/bin:$PATH"' > $MNT/etc/profile.d/rustup.sh
fi;
}
trim_image()
{
e2fsck -f "$1"
resize2fs -M "$1" # shrinks the file
resize2fs "$1" "$IMAGE_SIZE" # re-grows as sparse
}
umount_image()
{
# Unmount everything under $MNT
awk '{print $2}' /proc/mounts|
grep "^$MNT"|
sort -r|
xargs umount
rmdir "$MNT"
trap '' EXIT
}
cmd_update()
{
if [[ ! -e $ktest_image ]]; then
echo "$ktest_image does not exist"
exit 1
fi
MNT=$(mktemp --tmpdir -d $(basename "$0")-XXXXXXXXXX)
trap 'umount_image' EXIT
cp "$ktest_image" "$ktest_image".new
mount "$ktest_image".new "$MNT"
update_packages none
update_files
umount_image
trim_image "$ktest_image".new
mv "$ktest_image".new "$ktest_image"
}
cmd_create()
{
if [[ -e $ktest_image ]]; then
echo "$ktest_image already exists"
exit 1
fi
(cd "$ktest_dir"; git submodule update --init debootstrap)
MNT=$(mktemp --tmpdir -d $(basename "$0")-XXXXXXXXXX)
trap '/bin/bash; umount_image; rm "$ktest_image"' EXIT
fallocate -l "$IMAGE_SIZE" "$ktest_image"
mkfs.ext4 -F "$ktest_image"
mount "$ktest_image" "$MNT"
local debian_release="trixie" #general release
local keyring=""
if [[ $MIRROR == *"debian-ports"* ]]; then
debian_release="sid" #unofficial ports don't have named releases
echo ""
echo "WARNING: $ktest_arch is unsupported, using SID release for packages"
echo "*******************************************************************"
echo "PLEASE NOTE: this often has dependency problems between packages, "
echo "and can prevent the install due to a dependency conflict "
echo "If so, contact the debian maintainer for this architecture to fix it:"
echo "${ktest_arch} : ${DEBIAN_ARCH}@buildd.debian.org"
echo "*******************************************************************"
echo ""
fi
#fallback: if the architecture can't be found, try official sid:
if [[ -z $(curl -qq -I "${MIRROR}/dists/${debian_release}/main/binary-${DEBIAN_ARCH}/Release" 2>/dev/null | grep "HTTP/2 200") ]]; then
echo "WARNING: $DEBIAN_ARCH $debian_release could not be found at ${MIRROR} (where it should be). Falling back to standard sid"
MIRROR="http://deb.debian.org/debian/"
debian_release=sid
fi
DEBOOTSTRAP_DIR=$ktest_dir/debootstrap $debootstrap \
--no-check-gpg \
--arch="$DEBIAN_ARCH" \
--exclude=$(join_by , "${EXCLUDE[@]}") \
$keyring \
--foreign \
--components='main,contrib,non-free' \
$debian_release "$MNT" "$MIRROR"
if [[ -n ${CROSS_COMPILE} ]]; then
statichelper=$(which qemu-${ktest_arch}) || statichelper=$(which qemu-${ktest_arch}-static)
cp ${statichelper} ${MNT}${statichelper}
fi
_chroot "$MNT" /debootstrap/debootstrap --second-stage
_chroot "$MNT" dpkg --configure -a
update_packages $debian_release
update_files
umount_image
trim_image "$ktest_image"
}
if [[ $(type -t "$CMD") != function ]]; then
usage
exit 1
fi
$CMD "$@"