Debian Codegraph @

debian/tests/test-common

raw
1 # -*- mode: sh -*-
2 # shellcheck shell=sh
3
4 # Find kernel flavour and release
5 KVER=
6 for flavour in $SUPPORTED_FLAVOURS; do
7 KVER="$(dpkg-query -Wf '${Depends}' "linux-image-${flavour}" 2>/dev/null | tr ',' '\n' | sed -n 's/^ *linux-image-\([-a-z0-9+.]*\).*/\1/p')"
8 if [ "$KVER" ]; then
9 break
10 fi
11 done
12 if [ -z "$KVER" ]; then
13 echo >&2 "E: Test must set SUPPORTED_FLAVOURS and depend on those flavours"
14 exit 2
15 fi
16
17 # If any built-in drivers request firmware, create dummy files to
18 # avoid warnings for this (see #1078168).
19 tr '\0' '\n' < "/lib/modules/$KVER/modules.builtin.modinfo" \
20 | sed -n -e 's/^[^=]*\.firmware=//p' \
21 | while read -r fw_file; do
22 mkdir -p "/lib/firmware/$(dirname "$fw_file")"
23 touch "/lib/firmware/$fw_file"
24 done
25
26 case "$(dpkg --print-architecture)" in
27 arm64)
28 # The Ubuntu arm64 autopkgtest runs rarely into the 1200 seconds timeout.
29 QEMU_TIMEOUT=1800
30 ;;
31 armhf)
32 # qemu-busybox on Ubuntu armhf runs into the 300 seconds timeout.
33 QEMU_TIMEOUT=600
34 ;;
35 ppc64el)
36 # Slowest execution seen in Ubuntu ppc64el autopkgtest: 230 seconds
37 QEMU_TIMEOUT=600
38 ;;
39 s390x)
40 # The Ubuntu s390x autopkgtest runs into the 120 seconds timeout.
41 QEMU_TIMEOUT=240
42 ;;
43 *)
44 QEMU_TIMEOUT=120
45 esac
46
47 if [ -n "${AUTOPKGTEST_TMP-}" ]; then
48 export TMPDIR="${AUTOPKGTEST_TMP}"
49 fi
50
51 BASEDIR="$(mktemp -d -t initramfs-test.XXXXXXXXXX)"
52
53 # Skeleton configuration directory
54 CONFDIR="${BASEDIR}/config"
55 mkdir -p "${CONFDIR}"
56 cp conf/initramfs.conf "${CONFDIR}/initramfs.conf"
57 echo "RESUME=none" >>"${CONFDIR}/initramfs.conf"
58 mkdir "${CONFDIR}/hooks"
59 touch "${CONFDIR}/modules"
60 mkdir "${CONFDIR}/scripts"
61
62 # initramfs image file
63 INITRAMFS="${BASEDIR}/initrd.img"
64
65 # root disk image file
66 ROOTDISK="${BASEDIR}/rootdisk.raw"
67
68 # root disk interface type (for qemu) and device name (for Linux)
69 test -n "${ROOTDISK_QEMU_IF}" || ROOTDISK_QEMU_IF=virtio
70 test -n "${ROOTDISK_LINUX_NAME}" || ROOTDISK_LINUX_NAME="/dev/vda"
71
72 # Create a root fs with a trivial userspace
73 ROOTDIR="${BASEDIR}/rootdir"
74 INIT_MESSAGE='root fs init system started successfully'
75 for subdir in "" dev proc run sys usr usr/bin usr/lib usr/lib64 usr/sbin; do
76 mkdir "${ROOTDIR}/${subdir}"
77 done
78 for subdir in bin lib lib64 sbin; do
79 ln -s "usr/$subdir" "${ROOTDIR}/${subdir}"
80 done
81 cat >"${ROOTDIR}/sbin/init" <<EOF
82 #!/bin/sh
83 set -e
84 test -b ${ROOTDISK_LINUX_NAME}
85 test -d /proc/1
86 test -d /run/initramfs
87 test -d /sys/class
88 test -d /usr/bin
89 echo '${INIT_MESSAGE}'
90 poweroff
91 EOF
92 chmod a+x "${ROOTDIR}/sbin/init"
93 cp /usr/lib/klibc/bin/sh "${ROOTDIR}/bin/sh"
94 cp /usr/lib/klibc/bin/poweroff "${ROOTDIR}/bin/poweroff"
95 cp "$(dpkg -L libklibc | grep '/klibc-.*\.so$')" "${ROOTDIR}/lib/"
96
97 # VM output file
98 OUTPUT="${BASEDIR}/output.log"
99
100 prepare_network_dumping_rootfs() {
101 local root_dir="$ROOTDIR"
102 echo "I: Preparing network dumping rootfs in $root_dir..."
103 cat >"${root_dir}/usr/sbin/init" <<EOF
104 #!/bin/sh
105 echo "I: Executing /usr/sbin/init from root fs"
106 # Stop the kernel from spamming the output
107 current_printk=\$(sysctl kernel.printk)
108 sysctl -w kernel.printk="4 4 1 7"
109 # Run twice, once for the human, once for the test harness
110 echo "I: ip addr"
111 ip addr
112 echo "I: ip route"
113 ip route
114 echo "I: ip -6 route"
115 ip -6 route
116 for file in /run/net*.conf; do
117 [ -f \$file ] || continue;
118 echo "########## \$file ##########"
119 cat \$file
120 echo "########################################"
121 done
122 echo "########## hostname ##########"
123 cat /proc/sys/kernel/hostname
124 echo "########################################"
125 echo "########## ip -json addr ##########"
126 ip -json addr
127 echo "########################################"
128 echo "########## ip -json route ##########"
129 ip -json route
130 echo "########################################"
131 echo "########## ip -json -6 route ##########"
132 ip -json -6 route
133 echo "########################################"
134 echo "########## ps -ww aux ##########"
135 ps -ww aux
136 echo "########################################"
137 sysctl -w "\${current_printk}"
138 echo '${INIT_MESSAGE}'
139 poweroff
140 EOF
141
142 . /usr/share/initramfs-tools/hook-functions
143 for binary in /usr/bin/cat /usr/bin/ip /usr/bin/ps /usr/sbin/sysctl; do
144 verbose=y DESTDIR="$root_dir" copy_exec "$binary"
145 done
146 }
147
148 build_initramfs() {
149 echo "build_initramfs: /usr/sbin/mkinitramfs -d ${CONFDIR} -o ${INITRAMFS} ${KVER}"
150 /usr/sbin/mkinitramfs -d "${CONFDIR}" -o "${INITRAMFS}" "${KVER}"
151 }
152
153 build_fs_ext2() {
154 local blocks inodes
155 local dir="${1}"
156 local disk="${2}"
157 local label="${3-}"
158
159 # Get directory size
160 blocks="$(du --summarize "${dir}" | cut -f 1)"
161 inodes="$(du --summarize --inodes "${dir}" | cut -f 1)"
162
163 # Add fudge factor
164 blocks="$((blocks + 28 + blocks / 4))"
165 inodes="$((inodes + 10))"
166
167 set -- -b "${blocks}" -N "${inodes}" ${label:+-L "$label"} -U -d "${dir}" "${disk}"
168 echo "build_fs_ext2: genext2fs $*"
169 # genext2fs writes status messages to stderr; hide that from
170 # autopkgtest
171 genext2fs 2>&1 "$@"
172 }
173
174 build_rootfs_ext2() {
175 build_fs_ext2 "${ROOTDIR}" "${ROOTDISK}"
176 }
177
178 _run_qemu() {
179 local append="$1"
180 shift
181
182 echo "I: Running qemu (with a timeout of $QEMU_TIMEOUT seconds)..."
183 timeout --foreground "$QEMU_TIMEOUT" \
184 debian/tests/run-qemu /boot/vmlinu*-"${KVER}" "${INITRAMFS}" "${append}" \
185 -nographic -chardev stdio,id=char0 -serial chardev:char0 "$@" \
186 | tee "${OUTPUT}"
187 }
188
189 # Run QEMU with default block and network devices
190 _run_qemu_default_devices() {
191 local extra_append="$1"
192 _run_qemu "root=${ROOTDISK_LINUX_NAME} ${extra_append}" \
193 -drive "file=${ROOTDISK},if=${ROOTDISK_QEMU_IF},media=disk,format=raw" \
194 ${USRDISK:+-drive "file=${USRDISK},if=${USRDISK_QEMU_IF},media=disk,format=raw"} \
195 -device "virtio-net-pci,netdev=lan0,mac=52:54:00:65:43:21" \
196 -netdev "user,id=lan0,net=10.0.3.0/24,ipv6-net=fec7::/48,hostname=pizza,dnssearch=test,domainname=example.com,bootfile=/path/to/bootfile2" \
197 -device "virtio-net-pci,netdev=lan1,mac=52:54:00:12:34:56" \
198 -netdev "user,id=lan1,hostname=goulash,dnssearch=example,dnssearch=example.net,domainname=test,bootfile=/path/to/bootfile"
199 }
200
201 run_qemu_in_background() {
202 local rootdisk="$1"
203 local pidfile="$2"
204 local logfile="$3"
205 local cmdline_root="$4"
206 shift 4
207
208 echo "I: Running qemu in background..."
209 debian/tests/run-qemu /boot/vmlinu*-"${KVER}" "${INITRAMFS}" \
210 "root=${cmdline_root} panic=-1" -display none \
211 -drive "file=${rootdisk},if=${ROOTDISK_QEMU_IF},media=disk,format=raw" \
212 -device "virtio-net-pci,netdev=lan9,mac=52:54:00:12:33:21" \
213 -netdev "socket,id=lan9,listen=localhost:1234" \
214 -pidfile "${pidfile}" -serial "file:${logfile}" -daemonize "$@"
215 }
216
217 run_qemu_nocheck() {
218 # hide error messages from autopkgtest
219 _run_qemu_default_devices 2>&1 "${1-}"
220 }
221
222 run_qemu() {
223 _run_qemu_default_devices "panic=-1 ${1-}"
224 grep -qF "${INIT_MESSAGE}" "${OUTPUT}"
225 }
226
227 run_qemu_diskless_with_socket_network() {
228 local append="$1"
229 shift
230 _run_qemu "panic=-1 $append" \
231 -device "virtio-net-pci,netdev=lan0,mac=52:54:00:65:43:21" \
232 -netdev "socket,id=lan0,connect=localhost:1234" "$@"
233 grep -qF "${INIT_MESSAGE}" "${OUTPUT}"
234 }
235
236 check_no_output() {
237 local msg="$1"
238 if grep -qF "${msg}" "${OUTPUT}"; then
239 echo >&2 "E: Message '${msg}' found in log output '${OUTPUT}."
240 exit 1
241 fi
242 }
243
244 check_output() {
245 local msg="$1"
246 if ! grep -qF "${msg}" "${OUTPUT}"; then
247 echo >&2 "E: Message '${msg}' not found in log output '${OUTPUT}."
248 exit 1
249 fi
250 }
251
252 check_no_network_configuration() {
253 check_no_output "Waiting up to 180 secs for"
254 }
255
256 wait_for_server_startup() {
257 local expected_log_line="$1"
258 local qemu_logfile="$2"
259 local server_pid="$3"
260 local timeout="$QEMU_TIMEOUT"
261
262 echo "Waiting up to $QEMU_TIMEOUT seconds for server (QEMU process $server_pid) to get started..."
263 while ! grep -q "$expected_log_line" "$qemu_logfile"; do
264 if ! test -d "/proc/$server_pid"; then
265 echo >&2 "Error: QEMU server process $server_pid disappeared."
266 return 1
267 fi
268 timeout=$((timeout - 1))
269 if test "$timeout" -le 0; then
270 echo >&2 "Error: server was not started within $QEMU_TIMEOUT seconds."
271 return 1
272 fi
273 sleep 1
274 done
275 echo "Server startup took $((QEMU_TIMEOUT - timeout)) seconds."
276 }