Debian Codegraph @

debian/tests/initrd-boot

raw
1 #!/bin/bash
2
3 set -exu
4
5 TMPDIR="${AUTOPKGTEST_TMP:-/tmp}"
6 OUTDIR="${AUTOPKGTEST_ARTIFACTS:-/tmp}"
7 # We need TMPDIR to be world-readable for unshare to read it (used in creation
8 # of our VM image below)
9 chmod a+rx "$TMPDIR"
10
11 eval "$(grep "^ID=" /etc/os-release)"
12 if [ "$ID" = "ubuntu" ]; then
13 COMPONENTS=main,universe
14 INCLUDES=e2fsprogs,zstd,openssh-server
15 EXTRAS=--keyring=/usr/share/keyrings/ubuntu-archive-keyring.gpg
16 else
17 COMPONENTS=main
18 INCLUDES=openssh-server
19 EXTRAS=
20 fi
21
22 # Generate an SSH key which we'll use to access the VM remotely
23 ssh-keygen -q -t rsa -f "$TMPDIR"/id_rsa -N ""
24
25 # Construct a VM image of the "current" (i.e. this chroot's) release, including
26 # the SSH key we generated, and include the nbd-client package. We use a hook
27 # to ensure our apt sources-and-preferences match the new VM's
28 IMAGE="$TMPDIR"/nbd.img
29
30 # Check if the architecture is armhf, and only attempt to enable swap if it's not
31 if ! [ $(uname -m) = "armv7l" ]; then
32 # Allocate some swap
33 fallocate -l 2G /swapfile
34 chmod 600 /swapfile
35 mkswap /swapfile
36 swapon /swapfile
37 else
38 echo "Skipping swap activation on armhf architecture"
39 fi
40
41 # If this fails locally, allocate more space to /tmp
42 debvm-create \
43 --output="$IMAGE" \
44 --release="" \
45 --size=4G \
46 --sshkey="$TMPDIR"/id_rsa.pub \
47 --skip=usrmerge \
48 -- \
49 --mode=unshare \
50 --hook-dir=/usr/share/mmdebstrap/hooks/copy-host-apt-sources-and-preferences \
51 --hook-dir=/usr/share/mmdebstrap/hooks/file-mirror-automount \
52 --hook-dir=/usr/share/mmdebstrap/hooks/maybe-merged-usr \
53 --include="$INCLUDES${INCLUDES:+,}nbd-client" \
54 --components="$COMPONENTS" \
55 $EXTRAS \
56 ""
57
58 # Configure and start up the nbd-server pointing at the new VM image
59 cat > "$TMPDIR"/myvm.conf << EOF
60 [myvm]
61 exportname = $IMAGE
62 EOF
63 cp "$TMPDIR"/myvm.conf /etc/nbd-server/conf.d/
64 chown nbd:nbd "$IMAGE"
65 systemctl restart nbd-server.service
66
67 # Start a VM from the template, skipping the root device and cmdline
68 # definitions. This will use the kernel and initrd from the template (extracted
69 # via debugfs), but not the image itself. The rootfs will come from the nbd
70 # server (at least, that's what we hope!)
71 (
72 timeout -k 120s 1800s debvm-run \
73 --image="$IMAGE" \
74 --sshport=8022 \
75 --skip=root \
76 --append="nbdroot=10.0.2.2/myvm root=/dev/nbd0" \
77 -- -vga none
78 ) </dev/null >"$OUTDIR"/debvm.log 2>&1 &
79
80 # the default of 60 seconds can be too slow for salsaci
81 # debvm-waitssh sometimes fails even when the ssh server is up
82 # so keeping the timeout here lower than debvm-run
83 # Also, on some architectures (ppc64el on noble), debvm-waitssh
84 # times out even when an ssh-server is up. So if the timeout
85 # should not be considered a failure, as a genuine failure
86 # would eventually fail in later steps
87 debvm-waitssh -t 1200 8022 || true
88
89 # the default ssh command does not store known hosts and even ignores host keys
90 # it identifies itself with the rsa key generated above
91 # pseudo terminal allocation is disabled or otherwise, programs executed via
92 # ssh might wait for input on stdin of the ssh process
93 ssh="ssh -o ConnectTimeout=20 -o NoHostAuthenticationForLocalhost=yes -i $TMPDIR/id_rsa -T -p 8022"
94
95 # Check (and show!) the root device is /dev/nbd0
96 $ssh root@localhost -- "cat /proc/mounts" > "$OUTDIR"/mounts
97 cat "$OUTDIR"/mounts
98 awk '$1=="/dev/nbd0" && $2=="/" { print; }' "$OUTDIR"/mounts | grep "/"
99
100 # shut the system off
101 trap - EXIT
102 $ssh root@localhost -- systemctl poweroff || :