File: test-common

package info (click to toggle)
initramfs-tools 0.150
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 672 kB
  • sloc: sh: 2,300; ansic: 457; python: 189; makefile: 24
file content (271 lines) | stat: -rw-r--r-- 7,875 bytes parent folder | download | duplicates (2)
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
# -*- mode: sh -*-
# shellcheck shell=sh

# Find kernel flavour and release
KVER=
for flavour in $SUPPORTED_FLAVOURS; do
	KVER="$(dpkg-query -Wf '${Depends}' "linux-image-${flavour}" 2>/dev/null | tr ',' '\n' | sed -n 's/^ *linux-image-\([-a-z0-9+.]*\).*/\1/p')"
	if [ "$KVER" ]; then
		break
	fi
done
if [ -z "$KVER" ]; then
	echo >&2 "E: Test must set SUPPORTED_FLAVOURS and depend on those flavours"
	exit 2
fi

# If any built-in drivers request firmware, create dummy files to
# avoid warnings for this (see #1078168).
tr '\0' '\n' < "/lib/modules/$KVER/modules.builtin.modinfo" \
| sed -n -e 's/^[^=]*\.firmware=//p' \
| while read -r fw_file; do
	mkdir -p "/lib/firmware/$(dirname "$fw_file")"
	touch "/lib/firmware/$fw_file"
done

case "$(dpkg --print-architecture)" in
arm64)
	# The Ubuntu arm64 autopkgtest runs rarely into the 1200 seconds timeout.
	QEMU_TIMEOUT=1800
	;;
armhf)
	# qemu-busybox on Ubuntu armhf runs into the 300 seconds timeout.
	QEMU_TIMEOUT=600
	;;
ppc64el)
	# Slowest execution seen in Ubuntu ppc64el autopkgtest: 230 seconds
	QEMU_TIMEOUT=600
	;;
*)
	QEMU_TIMEOUT=120
esac

if [ -n "${AUTOPKGTEST_TMP-}" ]; then
	export TMPDIR="${AUTOPKGTEST_TMP}"
fi

BASEDIR="$(mktemp -d -t initramfs-test.XXXXXXXXXX)"

# Skeleton configuration directory
CONFDIR="${BASEDIR}/config"
mkdir -p "${CONFDIR}"
cp conf/initramfs.conf "${CONFDIR}/initramfs.conf"
echo "RESUME=none" >>"${CONFDIR}/initramfs.conf"
mkdir "${CONFDIR}/hooks"
touch "${CONFDIR}/modules"
mkdir "${CONFDIR}/scripts"

# initramfs image file
INITRAMFS="${BASEDIR}/initrd.img"

# root disk image file
ROOTDISK="${BASEDIR}/rootdisk.raw"

# root disk interface type (for qemu) and device name (for Linux)
test -n "${ROOTDISK_QEMU_IF}" || ROOTDISK_QEMU_IF=virtio
test -n "${ROOTDISK_LINUX_NAME}" || ROOTDISK_LINUX_NAME=vda

# Create a root fs with a trivial userspace
ROOTDIR="${BASEDIR}/rootdir"
INIT_MESSAGE='root fs init system started successfully'
for subdir in "" dev proc run sys usr usr/bin usr/lib usr/lib64 usr/sbin; do
	mkdir "${ROOTDIR}/${subdir}"
done
for subdir in bin lib lib64 sbin; do
	ln -s "usr/$subdir" "${ROOTDIR}/${subdir}"
done
cat >"${ROOTDIR}/sbin/init" <<EOF
#!/bin/sh
set -e
test -b /dev/${ROOTDISK_LINUX_NAME}
test -d /proc/1
test -d /run/initramfs
test -d /sys/class
test -d /usr/bin
echo '${INIT_MESSAGE}'
poweroff
EOF
chmod a+x "${ROOTDIR}/sbin/init"
cp /usr/lib/klibc/bin/sh "${ROOTDIR}/bin/sh"
cp /usr/lib/klibc/bin/poweroff "${ROOTDIR}/bin/poweroff"
cp "$(dpkg -L libklibc | grep '/klibc-.*\.so$')" "${ROOTDIR}/lib/"

# VM output file
OUTPUT="${BASEDIR}/output.log"

prepare_network_dumping_rootfs() {
	local root_dir="$ROOTDIR"
	echo "I: Preparing network dumping rootfs in $root_dir..."
	cat >"${root_dir}/usr/sbin/init" <<EOF
#!/bin/sh
echo "I: Executing /usr/sbin/init from root fs"
# Stop the kernel from spamming the output
current_printk=\$(sysctl kernel.printk)
sysctl -w kernel.printk="4 4 1 7"
# Run twice, once for the human, once for the test harness
echo "I: ip addr"
ip addr
echo "I: ip route"
ip route
echo "I: ip -6 route"
ip -6 route
for file in /run/net*.conf; do
	[ -f \$file ] || continue;
	echo "########## \$file ##########"
	cat \$file
	echo "########################################"
done
echo "########## hostname ##########"
cat /proc/sys/kernel/hostname
echo "########################################"
echo "########## ip -json addr ##########"
ip -json addr
echo "########################################"
echo "########## ip -json route ##########"
ip -json route
echo "########################################"
echo "########## ip -json -6 route ##########"
ip -json -6 route
echo "########################################"
echo "########## ps -ww aux ##########"
ps -ww aux
echo "########################################"
sysctl -w "\${current_printk}"
echo '${INIT_MESSAGE}'
poweroff
EOF

	. /usr/share/initramfs-tools/hook-functions
	for binary in /usr/bin/cat /usr/bin/ip /usr/bin/ps /usr/sbin/sysctl; do
		verbose=y DESTDIR="$root_dir" copy_exec "$binary"
	done
}

build_initramfs() {
	echo "build_initramfs: /usr/sbin/mkinitramfs -d ${CONFDIR} -o ${INITRAMFS} ${KVER}"
	/usr/sbin/mkinitramfs -d "${CONFDIR}" -o "${INITRAMFS}" "${KVER}"
}

build_fs_ext2() {
	local blocks inodes
	local dir="${1}"
	local disk="${2}"
	local label="${3-}"

	# Get directory size
	blocks="$(du --summarize "${dir}" | cut -f 1)"
	inodes="$(du --summarize --inodes "${dir}" | cut -f 1)"

	# Add fudge factor
	blocks="$((blocks + 28 + blocks / 4))"
	inodes="$((inodes + 10))"

	set -- -b "${blocks}" -N "${inodes}" ${label:+-L "$label"} -U -d "${dir}" "${disk}"
	echo "build_fs_ext2: genext2fs $*"
	# genext2fs writes status messages to stderr; hide that from
	# autopkgtest
	genext2fs 2>&1 "$@"
}

build_rootfs_ext2() {
	build_fs_ext2 "${ROOTDIR}" "${ROOTDISK}"
}

_run_qemu() {
	local append="$1"
	shift

	echo "I: Running qemu (with a timeout of $QEMU_TIMEOUT seconds)..."
	timeout --foreground "$QEMU_TIMEOUT" \
		debian/tests/run-qemu /boot/vmlinu*-"${KVER}" "${INITRAMFS}" "${append}" \
		-nographic -chardev stdio,id=char0 -serial chardev:char0 "$@" \
		| tee "${OUTPUT}"
}

# Run QEMU with default block and network devices
_run_qemu_default_devices() {
	local extra_append="$1"
	_run_qemu "root=/dev/${ROOTDISK_LINUX_NAME} ${extra_append}" \
		-drive "file=${ROOTDISK},if=${ROOTDISK_QEMU_IF},media=disk,format=raw" \
		${USRDISK:+-drive "file=${USRDISK},if=${USRDISK_QEMU_IF},media=disk,format=raw"} \
		-device "virtio-net-pci,netdev=lan0,mac=52:54:00:65:43:21" \
		-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" \
		-device "virtio-net-pci,netdev=lan1,mac=52:54:00:12:34:56" \
		-netdev "user,id=lan1,hostname=goulash,dnssearch=example,dnssearch=example.net,domainname=test,bootfile=/path/to/bootfile"
}

run_qemu_in_background() {
	local rootdisk="$1"
	local pidfile="$2"
	local logfile="$3"
	shift 3

	echo "I: Running qemu in background..."
	debian/tests/run-qemu /boot/vmlinu*-"${KVER}" "${INITRAMFS}" \
		"root=/dev/${ROOTDISK_LINUX_NAME} panic=-1" -display none \
		-drive "file=${rootdisk},if=${ROOTDISK_QEMU_IF},media=disk,format=raw" \
		-device "virtio-net-pci,netdev=lan9,mac=52:54:00:12:33:21" \
		-netdev "socket,id=lan9,listen=localhost:1234" \
		-pidfile "${pidfile}" -serial "file:${logfile}" -daemonize "$@"
}

run_qemu_nocheck() {
	# hide error messages from autopkgtest
	_run_qemu_default_devices 2>&1 "${1-}"
}

run_qemu() {
	_run_qemu_default_devices "panic=-1 ${1-}"
	grep -qF "${INIT_MESSAGE}" "${OUTPUT}"
}

run_qemu_diskless_with_socket_network() {
	local append="$1"
	shift
	_run_qemu "panic=-1 $append" \
		-device "virtio-net-pci,netdev=lan0,mac=52:54:00:65:43:21" \
		-netdev "socket,id=lan0,connect=localhost:1234" "$@"
	grep -qF "${INIT_MESSAGE}" "${OUTPUT}"
}

check_no_output() {
	local msg="$1"
	if grep -qF "${msg}" "${OUTPUT}"; then
		echo >&2 "E: Message '${msg}' found in log output '${OUTPUT}."
		exit 1
	fi
}

check_output() {
	local msg="$1"
	if ! grep -qF "${msg}" "${OUTPUT}"; then
		echo >&2 "E: Message '${msg}' not found in log output '${OUTPUT}."
		exit 1
	fi
}

check_no_network_configuration() {
	check_no_output "Waiting up to 180 secs for"
}

wait_for_server_startup() {
	local expected_log_line="$1"
	local qemu_logfile="$2"
	local server_pid="$3"
	local timeout="$QEMU_TIMEOUT"

	echo "Waiting up to $QEMU_TIMEOUT seconds for server (QEMU process $server_pid) to get started..."
	while ! grep -q "$expected_log_line" "$qemu_logfile"; do
		if ! test -d "/proc/$server_pid"; then
			echo >&2 "Error: QEMU server process $server_pid disappeared."
			return 1
		fi
		timeout=$((timeout - 1))
		if test "$timeout" -le 0; then
			echo >&2 "Error: server was not started within $QEMU_TIMEOUT seconds."
			return 1
		fi
		sleep 1
	done
	echo "Server startup took $((QEMU_TIMEOUT - timeout)) seconds."
}