]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/test-functions
Merge pull request #11409 from yuwata/udev-synthetic-errno
[thirdparty/systemd.git] / test / test-functions
CommitLineData
898720b7
HH
1#!/bin/bash
2# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3# ex: ts=8 sw=4 sts=4 et filetype=sh
4PATH=/sbin:/bin:/usr/sbin:/usr/bin
5export PATH
6
c2f32f61
SLM
7LOOKS_LIKE_DEBIAN=$(source /etc/os-release && [[ "$ID" = "debian" || " $ID_LIKE " = *" debian "* ]] && echo yes || true)
8LOOKS_LIKE_ARCH=$(source /etc/os-release && [[ "$ID" = "arch" || " $ID_LIKE " = *" arch "* ]] && echo yes || true)
9LOOKS_LIKE_SUSE=$(source /etc/os-release && [[ " $ID_LIKE " = *" suse "* ]] && echo yes || true)
0d6e798a
HH
10KERNEL_VER=${KERNEL_VER-$(uname -r)}
11KERNEL_MODS="/lib/modules/$KERNEL_VER/"
91f9f8f1 12QEMU_TIMEOUT="${QEMU_TIMEOUT:-infinity}"
43bbb8f0 13NSPAWN_TIMEOUT="${NSPAWN_TIMEOUT:-infinity}"
b2ecd099 14TIMED_OUT= # will be 1 after run_* if *_TIMEOUT is set and test timed out
4b742c8a 15[[ "$LOOKS_LIKE_SUSE" ]] && FSTYPE="${FSTYPE:-btrfs}" || FSTYPE="${FSTYPE:-ext4}"
22f1f8f2 16UNIFIED_CGROUP_HIERARCHY="${UNIFIED_CGROUP_HIERARCHY:-default}"
7624e721 17EFI_MOUNT="$(bootctl -p 2>/dev/null || echo /boot)"
3071b3ff 18QEMU_MEM="${QEMU_MEM:-512M}"
898720b7 19
3486cb6c
MP
20if ! ROOTLIBDIR=$(pkg-config --variable=systemdutildir systemd); then
21 echo "WARNING! Cannot determine rootlibdir from pkg-config, assuming /usr/lib/systemd" >&2
22 ROOTLIBDIR=/usr/lib/systemd
23fi
24
016fa3b9
EV
25PATH_TO_INIT=$ROOTLIBDIR/systemd
26
1786fae3 27BASICTOOLS="test sh bash setsid loadkeys setfont login sulogin gzip sleep echo mount umount cryptsetup date dmsetup modprobe sed cmp tee rm true false chmod chown ln xargs"
09f6f45a 28DEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort hostname find"
889a9042 29
22077c9c
MP
30STATEDIR="${BUILD_DIR:-.}/test/$(basename $(dirname $(realpath $0)))"
31STATEFILE="$STATEDIR/.testdir"
32TESTLOG="$STATEDIR/test.log"
33
ec9181d2
EV
34is_built_with_asan() {
35 if ! type -P objdump >/dev/null; then
36 ddebug "Failed to find objdump. Assuming systemd hasn't been built with ASAN."
37 return 1
38 fi
39
40 # Borrowed from https://github.com/google/oss-fuzz/blob/cd9acd02f9d3f6e80011cc1e9549be526ce5f270/infra/base-images/base-runner/bad_build_check#L182
b4a450d8 41 local _asan_calls=$(objdump -dC $BUILD_DIR/systemd-journald | egrep "callq\s+[0-9a-f]+\s+<__asan" -c)
ec9181d2
EV
42 if (( $_asan_calls < 1000 )); then
43 return 1
44 else
45 return 0
46 fi
47}
48
49IS_BUILT_WITH_ASAN=$(is_built_with_asan && echo yes || echo no)
50
51if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
52 STRIP_BINARIES=no
016fa3b9
EV
53 SKIP_INITRD=yes
54 PATH_TO_INIT=$ROOTLIBDIR/systemd-under-asan
670bec2b 55 QEMU_MEM="1536M"
5b2172ee 56 QEMU_SMP=4
ec9181d2
EV
57fi
58
c6a77179 59function find_qemu_bin() {
3e7aa2ed
LP
60 # SUSE and Red Hat call the binary qemu-kvm. Debian and Gentoo call it kvm.
61 # Either way, only use this version if we aren't running in KVM, because
62 # nested KVM is flaky still.
63 if [ `systemd-detect-virt -v` != kvm ] ; then
64 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a kvm qemu-kvm 2>/dev/null | grep '^/' -m1)
65 fi
c6a77179
RC
66
67 [ "$ARCH" ] || ARCH=$(uname -m)
68 case $ARCH in
69 x86_64)
70 # QEMU's own build system calls it qemu-system-x86_64
71 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-x86_64 2>/dev/null | grep '^/' -m1)
72 ;;
73 i*86)
74 # new i386 version of QEMU
75 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-i386 2>/dev/null | grep '^/' -m1)
76
77 # i386 version of QEMU
78 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu 2>/dev/null | grep '^/' -m1)
79 ;;
cf5f9bb8
ZJS
80 ppc64*)
81 [ "$QEMU_BIN" ] || QEMU_BIN=$(which -a qemu-system-$ARCH 2>/dev/null | grep '^/' -m1)
82 ;;
c6a77179
RC
83 esac
84
85 if [ ! -e "$QEMU_BIN" ]; then
86 echo "Could not find a suitable QEMU binary" >&2
87 return 1
88 fi
89}
90
b2ecd099
MP
91# Return 0 if QEMU did run (then you must check the result state/logs for actual
92# success), or 1 if QEMU is not available.
889a9042 93run_qemu() {
b6f0c419
HH
94 if [ -f /etc/machine-id ]; then
95 read MACHINE_ID < /etc/machine-id
906bbac4
ZJS
96 [ -z "$INITRD" ] && [ -e "$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/initrd" ] \
97 && INITRD="$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/initrd"
98 [ -z "$KERNEL_BIN" ] && [ -e "$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/linux" ] \
99 && KERNEL_BIN="$EFI_MOUNT/$MACHINE_ID/$KERNEL_VER/linux"
b6f0c419
HH
100 fi
101
e1a27318
EV
102 if [[ ! "$KERNEL_BIN" ]]; then
103 if [[ "$LOOKS_LIKE_ARCH" ]]; then
104 KERNEL_BIN=/boot/vmlinuz-linux
105 else
106 KERNEL_BIN=/boot/vmlinuz-$KERNEL_VER
107 fi
108 fi
109
61fea35e
EV
110 default_fedora_initrd=/boot/initramfs-${KERNEL_VER}.img
111 default_debian_initrd=/boot/initrd.img-${KERNEL_VER}
e1a27318 112 default_arch_initrd=/boot/initramfs-linux.img
85393d8f 113 default_suse_initrd=/boot/initrd-${KERNEL_VER}
e1a27318
EV
114 if [[ ! "$INITRD" ]]; then
115 if [[ -e "$default_fedora_initrd" ]]; then
116 INITRD="$default_fedora_initrd"
117 elif [[ "$LOOKS_LIKE_DEBIAN" && -e "$default_debian_initrd" ]]; then
118 INITRD="$default_debian_initrd"
119 elif [[ "$LOOKS_LIKE_ARCH" && -e "$default_arch_initrd" ]]; then
120 INITRD="$default_arch_initrd"
85393d8f
TB
121 elif [[ "$LOOKS_LIKE_SUSE" && -e "$default_suse_initrd" ]]; then
122 INITRD="$default_suse_initrd"
e1a27318
EV
123 fi
124 fi
125
c6a77179
RC
126 [ "$QEMU_SMP" ] || QEMU_SMP=1
127
128 find_qemu_bin || return 1
129
22f1f8f2
EV
130 local _cgroup_args
131 if [[ "$UNIFIED_CGROUP_HIERARCHY" = "yes" ]]; then
132 _cgroup_args="systemd.unified_cgroup_hierarchy=yes"
133 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "no" ]]; then
134 _cgroup_args="systemd.unified_cgroup_hierarchy=no systemd.legacy_systemd_cgroup_controller=yes"
135 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "hybrid" ]]; then
136 _cgroup_args="systemd.unified_cgroup_hierarchy=no systemd.legacy_systemd_cgroup_controller=no"
137 elif [[ "$UNIFIED_CGROUP_HIERARCHY" != "default" ]]; then
138 dfatal "Unknown UNIFIED_CGROUP_HIERARCHY. Got $UNIFIED_CGROUP_HIERARCHY, expected [yes|no|hybrid|default]"
139 exit 1
140 fi
141
85393d8f
TB
142if [[ "$LOOKS_LIKE_SUSE" ]]; then
143 PARAMS+="rd.hostonly=0"
144else
145 PARAMS+="ro"
146fi
147
148KERNEL_APPEND="$PARAMS \
149root=/dev/sda1 \
c6a77179
RC
150raid=noautodetect \
151loglevel=2 \
016fa3b9 152init=$PATH_TO_INIT \
c6a77179
RC
153console=ttyS0 \
154selinux=0 \
b2ad25d3 155printk.devkmsg=on \
22f1f8f2 156$_cgroup_args \
c6a77179
RC
157$KERNEL_APPEND \
158"
159
dbf43a42 160 QEMU_OPTIONS="-smp $QEMU_SMP \
c6a77179 161-net none \
3071b3ff 162-m $QEMU_MEM \
c6a77179
RC
163-nographic \
164-kernel $KERNEL_BIN \
5b23cef0 165-drive format=raw,cache=unsafe,file=${TESTDIR}/rootdisk.img \
c6a77179
RC
166"
167
91f9f8f1 168 if [[ "$INITRD" && "$SKIP_INITRD" != "yes" ]]; then
c6a77179
RC
169 QEMU_OPTIONS="$QEMU_OPTIONS -initrd $INITRD"
170 fi
171
3e7aa2ed
LP
172 # Let's use KVM if it is available, but let's avoid using nested KVM as that is still flaky
173 if [ -c /dev/kvm -a `systemd-detect-virt -v` != kvm ]; then
dbf43a42
DM
174 QEMU_OPTIONS="$QEMU_OPTIONS -machine accel=kvm -enable-kvm -cpu host"
175 fi
176
91f9f8f1
EV
177 if [[ "$QEMU_TIMEOUT" != "infinity" ]]; then
178 QEMU_BIN="timeout --foreground $QEMU_TIMEOUT $QEMU_BIN"
179 fi
b2ecd099
MP
180 (set -x; $QEMU_BIN $QEMU_OPTIONS -append "$KERNEL_APPEND")
181 rc=$?
182 if [ "$rc" = 124 ] && [ "$QEMU_TIMEOUT" != "infinity" ]; then
183 derror "test timed out after $QEMU_TIMEOUT s"
184 TIMED_OUT=1
185 else
186 [ "$rc" != 0 ] && derror "QEMU failed with exit code $rc"
187 fi
188 return 0
889a9042
RC
189}
190
7cad32bb
MP
191# Return 0 if nspawn did run (then you must check the result state/logs for actual
192# success), or 1 if nspawn is not available.
889a9042 193run_nspawn() {
7cad32bb
MP
194 [[ -d /run/systemd/system ]] || return 1
195
746fbd9c 196 local _nspawn_cmd="$BUILD_DIR/systemd-nspawn $NSPAWN_ARGUMENTS --register=no --kill-signal=SIGKILL --directory=$TESTDIR/$1 $PATH_TO_INIT $KERNEL_APPEND"
43bbb8f0
EV
197 if [[ "$NSPAWN_TIMEOUT" != "infinity" ]]; then
198 _nspawn_cmd="timeout --foreground $NSPAWN_TIMEOUT $_nspawn_cmd"
199 fi
856ca72b 200
22f1f8f2
EV
201 if [[ "$UNIFIED_CGROUP_HIERARCHY" = "hybrid" ]]; then
202 dwarn "nspawn doesn't support UNIFIED_CGROUP_HIERARCHY=hybrid, skipping"
203 exit
204 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "yes" || "$UNIFIED_CGROUP_HIERARCHY" = "no" ]]; then
205 _nspawn_cmd="env UNIFIED_CGROUP_HIERARCHY=$UNIFIED_CGROUP_HIERARCHY $_nspawn_cmd"
206 elif [[ "$UNIFIED_CGROUP_HIERARCHY" = "default" ]]; then
207 _nspawn_cmd="env --unset=UNIFIED_CGROUP_HIERARCHY $_nspawn_cmd"
208 else
209 dfatal "Unknown UNIFIED_CGROUP_HIERARCHY. Got $UNIFIED_CGROUP_HIERARCHY, expected [yes|no|hybrid|default]"
210 exit 1
211 fi
856ca72b 212
b2ecd099
MP
213 (set -x; $_nspawn_cmd)
214 rc=$?
215 if [ "$rc" = 124 ] && [ "$NSPAWN_TIMEOUT" != "infinity" ]; then
216 derror "test timed out after $NSPAWN_TIMEOUT s"
217 TIMED_OUT=1
218 else
219 [ "$rc" != 0 ] && derror "nspawn failed with exit code $rc"
220 fi
221 return 0
889a9042
RC
222}
223
224setup_basic_environment() {
225 # create the basic filesystem layout
226 setup_basic_dirs
227
228 install_systemd
229 install_missing_libraries
230 install_config_files
231 create_rc_local
232 install_basic_tools
233 install_libnss
234 install_pam
235 install_dbus
236 install_fonts
237 install_keymaps
238 install_terminfo
239 install_execs
9974ff63 240 install_fsck
889a9042
RC
241 install_plymouth
242 install_debug_tools
243 install_ld_so_conf
e3ce42e7 244 setup_selinux
889a9042
RC
245 strip_binaries
246 install_depmod_files
247 generate_module_dependencies
ec9181d2
EV
248 if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
249 create_asan_wrapper
250 fi
889a9042
RC
251}
252
e3ce42e7
EV
253setup_selinux() {
254 # don't forget KERNEL_APPEND='... selinux=1 ...'
255 if [[ "$SETUP_SELINUX" != "yes" ]]; then
256 ddebug "Don't setup SELinux"
257 return 0
258 fi
259 ddebug "Setup SELinux"
260 local _conf_dir=/etc/selinux
261 local _fixfiles_tools="bash uname cat sort uniq awk grep egrep head expr find rm secon setfiles"
262
263 rm -rf $initdir/$_conf_dir
264 if ! cp -ar $_conf_dir $initdir/$_conf_dir; then
265 dfatal "Failed to copy $_conf_dir"
266 exit 1
267 fi
268
269 cat <<EOF >$initdir/etc/systemd/system/autorelabel.service
270[Unit]
271Description=Relabel all filesystems
272DefaultDependencies=no
273Requires=local-fs.target
274Conflicts=shutdown.target
275After=local-fs.target
276Before=sysinit.target shutdown.target
277ConditionSecurity=selinux
278ConditionPathExists=|/.autorelabel
279
280[Service]
281ExecStart=/bin/sh -x -c 'echo 0 >/sys/fs/selinux/enforce && fixfiles -f -F relabel && rm /.autorelabel && systemctl --force reboot'
282Type=oneshot
283TimeoutSec=0
284RemainAfterExit=yes
285EOF
286
287 touch $initdir/.autorelabel
288 mkdir -p $initdir/etc/systemd/system/basic.target.wants
289 ln -fs autorelabel.service $initdir/etc/systemd/system/basic.target.wants/autorelabel.service
290
291 dracut_install $_fixfiles_tools
292 dracut_install fixfiles
293 dracut_install sestatus
294}
295
a2fbff31
EV
296install_valgrind() {
297 if ! type -p valgrind; then
298 dfatal "Failed to install valgrind"
299 exit 1
300 fi
301
302 local _valgrind_bins=$(strace -e execve valgrind /bin/true 2>&1 >/dev/null | perl -lne 'print $1 if /^execve\("([^"]+)"/')
303 dracut_install $_valgrind_bins
304
305 local _valgrind_libs=$(LD_DEBUG=files valgrind /bin/true 2>&1 >/dev/null | perl -lne 'print $1 if m{calling init: (/.*vgpreload_.*)}')
306 dracut_install $_valgrind_libs
307
308 local _valgrind_dbg_and_supp=$(
309 strace -e open valgrind /bin/true 2>&1 >/dev/null |
310 perl -lne 'if (my ($fname) = /^open\("([^"]+).*= (?!-)\d+/) { print $fname if $fname =~ /debug|\.supp$/ }'
311 )
312 dracut_install $_valgrind_dbg_and_supp
313}
314
cb2f9d3f
EV
315create_valgrind_wrapper() {
316 local _valgrind_wrapper=$initdir/$ROOTLIBDIR/systemd-under-valgrind
317 ddebug "Create $_valgrind_wrapper"
318 cat >$_valgrind_wrapper <<EOF
319#!/bin/bash
320
23cabb68 321mount -t proc proc /proc
cb2f9d3f
EV
322exec valgrind --leak-check=full --log-file=/valgrind.out $ROOTLIBDIR/systemd "\$@"
323EOF
324 chmod 0755 $_valgrind_wrapper
325}
326
1786fae3
EV
327create_asan_wrapper() {
328 local _asan_wrapper=$initdir/$ROOTLIBDIR/systemd-under-asan
329 ddebug "Create $_asan_wrapper"
330 cat >$_asan_wrapper <<EOF
331#!/bin/bash
332
333set -x
334
335DEFAULT_ASAN_OPTIONS=strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1
c1342d55 336DEFAULT_UBSAN_OPTIONS=print_stacktrace=1:print_summary=1
2614d83a 337DEFAULT_ENVIRONMENT="ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS UBSAN_OPTIONS=\$DEFAULT_UBSAN_OPTIONS:halt_on_error=1"
1786fae3
EV
338
339mount -t proc proc /proc
340mount -t sysfs sysfs /sys
341mount -o remount,rw /
342
343PATH_TO_ASAN=\$(find / -name '*libasan*' | sed 1q)
344if [[ "\$PATH_TO_ASAN" ]]; then
345 # A lot of services (most notably dbus) won't start without preloading libasan
346 # See https://github.com/systemd/systemd/issues/5004
347 DEFAULT_ENVIRONMENT="\$DEFAULT_ENVIRONMENT LD_PRELOAD=\$PATH_TO_ASAN"
348fi
349echo DefaultEnvironment=\$DEFAULT_ENVIRONMENT >>/etc/systemd/system.conf
9688fccc 350echo DefaultTimeoutStartSec=180s >>/etc/systemd/system.conf
1786fae3
EV
351
352# ASAN and syscall filters aren't compatible with each other.
353find / -name '*.service' -type f | xargs sed -i 's/^\\(MemoryDeny\\|SystemCall\\)/#\\1/'
354
88ed0f26
EV
355# The redirection of ASAN reports to a file prevents them from ending up in /dev/null.
356# But, apparently, sometimes it doesn't work: https://github.com/google/sanitizers/issues/886.
357JOURNALD_CONF_DIR=/etc/systemd/system/systemd-journald.service.d
358mkdir -p "\$JOURNALD_CONF_DIR"
359printf "[Service]\nEnvironment=ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS:log_path=/systemd-journald.asan.log\n" >"\$JOURNALD_CONF_DIR/env.conf"
360
082bcdca
EV
361# 90s isn't enough for some services to finish when literally everything is run
362# under ASan+UBSan in containers, which, in turn, are run in VMs.
d7283fc1 363# Let's limit which environments such services should be executed in.
082bcdca 364mkdir -p /etc/systemd/system/systemd-hwdb-update.service.d
d7283fc1 365printf "[Unit]\nConditionVirtualization=container\n\n[Service]\nTimeoutSec=180s\n" >/etc/systemd/system/systemd-hwdb-update.service.d/env-override.conf
082bcdca 366
c1342d55 367export ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS:log_path=/systemd.asan.log UBSAN_OPTIONS=\$DEFAULT_UBSAN_OPTIONS
1786fae3
EV
368exec $ROOTLIBDIR/systemd "\$@"
369EOF
370
371 chmod 0755 $_asan_wrapper
372}
373
45dbd7b6
EV
374create_strace_wrapper() {
375 local _strace_wrapper=$initdir/$ROOTLIBDIR/systemd-under-strace
376 ddebug "Create $_strace_wrapper"
377 cat >$_strace_wrapper <<EOF
378#!/bin/bash
379
380exec strace -D -o /strace.out $ROOTLIBDIR/systemd "\$@"
381EOF
382 chmod 0755 $_strace_wrapper
383}
384
9974ff63
EV
385install_fsck() {
386 dracut_install /sbin/fsck*
387 dracut_install -o /bin/fsck*
331fb4ca
EV
388
389 # fskc.reiserfs calls reiserfsck. so, install it
390 dracut_install -o reiserfsck
9974ff63
EV
391}
392
889a9042
RC
393install_dmevent() {
394 instmods dm_crypt =crypto
395 type -P dmeventd >/dev/null && dracut_install dmeventd
396 inst_libdir_file "libdevmapper-event.so*"
ac289ce3
EV
397 if [[ "$LOOKS_LIKE_DEBIAN" ]]; then
398 # dmsetup installs 55-dm and 60-persistent-storage-dm on Debian/Ubuntu
9c869ff6
DJL
399 # and since buster/bionic 95-dm-notify.rules
400 # see https://gitlab.com/debian-lvm/lvm2/blob/master/debian/patches/udev.patch
401 inst_rules 55-dm.rules 60-persistent-storage-dm.rules 95-dm-notify.rules
ac289ce3
EV
402 else
403 inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
404 fi
889a9042
RC
405}
406
407install_systemd() {
408 # install compiled files
ca992ecf
EV
409 local _ninja_bin=$(type -P ninja || type -P ninja-build)
410 if [[ -z "$_ninja_bin" ]]; then
411 dfatal "ninja was not found"
412 exit 1
413 fi
414 (set -x; DESTDIR=$initdir "$_ninja_bin" -C $BUILD_DIR install)
889a9042 415 # remove unneeded documentation
23756070 416 rm -fr $initdir/usr/share/{man,doc}
889a9042
RC
417 # we strip binaries since debug symbols increase binaries size a lot
418 # and it could fill the available space
419 strip_binaries
61b480b6 420
85393d8f
TB
421 [[ "$LOOKS_LIKE_SUSE" ]] && setup_suse
422
61b480b6
ZJS
423 # enable debug logging in PID1
424 echo LogLevel=debug >> $initdir/etc/systemd/system.conf
889a9042
RC
425}
426
d7a4278d
FS
427get_ldpath() {
428 local _bin="$1"
429 objdump -p "$_bin" 2>/dev/null | awk "/R(UN)?PATH/ { print \"$initdir\" \$2 }" | paste -sd :
430}
431
889a9042
RC
432install_missing_libraries() {
433 # install possible missing libraries
e3d3dada 434 for i in $initdir{,/usr}/{sbin,bin}/* $initdir{,/usr}/lib/systemd/{,tests/{,manual/,unsafe/}}*; do
d7a4278d 435 LD_LIBRARY_PATH=$(get_ldpath $i) inst_libs $i
889a9042
RC
436 done
437}
438
439create_empty_image() {
28c7474e
EV
440 local _size=500
441 if [[ "$STRIP_BINARIES" = "no" ]]; then
442 _size=$((2*_size))
443 fi
739d81dd 444 rm -f "$TESTDIR/rootdisk.img"
889a9042 445 # Create the blank file to use as a root filesystem
28c7474e 446 dd if=/dev/null of="$TESTDIR/rootdisk.img" bs=1M seek="$_size"
889a9042 447 LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
739d81dd 448 [ -b "$LOOPDEV" ] || return 1
889a9042 449 echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
edbced8a 450 sfdisk "$LOOPDEV" <<EOF
28c7474e 451,$((_size-10))M
889a9042
RC
452,
453EOF
454
053edc5b
EV
455 udevadm settle
456
331fb4ca
EV
457 local _label="-L systemd"
458 # mkfs.reiserfs doesn't know -L. so, use --label instead
459 [[ "$FSTYPE" == "reiserfs" ]] && _label="--label systemd"
460 if ! mkfs -t "${FSTYPE}" ${_label} "${LOOPDEV}p1" -q; then
461 dfatal "Failed to mkfs -t ${FSTYPE}"
462 exit 1
463 fi
889a9042
RC
464}
465
0d6e61d6
EV
466check_asan_reports() {
467 local ret=0
468 local root="$1"
469
7e11a95e 470 if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
0d6e61d6
EV
471 ls -l "$root"
472 if [[ -e "$root/systemd.asan.log.1" ]]; then
473 cat "$root/systemd.asan.log.1"
7e11a95e
EV
474 ret=$(($ret+1))
475 fi
998445fd 476
0d6e61d6 477 journald_report=$(find "$root" -name "systemd-journald.asan.log*" -exec cat {} \;)
998445fd
EV
478 if [[ ! -z "$journald_report" ]]; then
479 printf "%s" "$journald_report"
480 ret=$(($ret+1))
481 fi
ed4f303f 482
0d6e61d6 483 pids=$("$BUILD_DIR/journalctl" -D "$root/var/log/journal" | perl -alne 'print $1 if /\[(\d+)\]:\s*SUMMARY:\s+\w+Sanitizer/')
ed4f303f
EV
484 if [[ ! -z "$pids" ]]; then
485 ret=$(($ret+1))
486 for pid in $pids; do
0d6e61d6 487 "$BUILD_DIR/journalctl" -D "$root/var/log/journal" _PID=$pid --no-pager
ed4f303f
EV
488 done
489 fi
7e11a95e
EV
490 fi
491
889a9042
RC
492 return $ret
493}
494
0d6e61d6
EV
495check_result_nspawn() {
496 local ret=1
497 local journald_report=""
498 local pids=""
499 [[ -e $TESTDIR/$1/testok ]] && ret=0
500 [[ -f $TESTDIR/$1/failed ]] && cp -a $TESTDIR/$1/failed $TESTDIR
501 cp -a $TESTDIR/$1/var/log/journal $TESTDIR
502 [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
503 ls -l $TESTDIR/journal/*/*.journal
504 test -s $TESTDIR/failed && ret=$(($ret+1))
505 [ -n "$TIMED_OUT" ] && ret=$(($ret+1))
506 check_asan_reports "$TESTDIR/$1" || ret=$(($ret+1))
507 return $ret
508}
509
054ee249
MP
510# can be overridden in specific test
511check_result_qemu() {
0013fac2 512 local ret=1
054ee249
MP
513 mkdir -p $TESTDIR/root
514 mount ${LOOPDEV}p1 $TESTDIR/root
515 [[ -e $TESTDIR/root/testok ]] && ret=0
516 [[ -f $TESTDIR/root/failed ]] && cp -a $TESTDIR/root/failed $TESTDIR
517 cp -a $TESTDIR/root/var/log/journal $TESTDIR
0d6e61d6 518 check_asan_reports "$TESTDIR/root" || ret=$(($ret+1))
054ee249
MP
519 umount $TESTDIR/root
520 [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
521 ls -l $TESTDIR/journal/*/*.journal
522 test -s $TESTDIR/failed && ret=$(($ret+1))
523 [ -n "$TIMED_OUT" ] && ret=$(($ret+1))
524 return $ret
525}
526
889a9042 527strip_binaries() {
5a613464
EV
528 if [[ "$STRIP_BINARIES" = "no" ]]; then
529 ddebug "Don't strip binaries"
530 return 0
531 fi
889a9042
RC
532 ddebug "Strip binaries"
533 find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
534}
535
536create_rc_local() {
537 mkdir -p $initdir/etc/rc.d
538 cat >$initdir/etc/rc.d/rc.local <<EOF
539#!/bin/bash
540exit 0
541EOF
542 chmod 0755 $initdir/etc/rc.d/rc.local
543}
544
545install_execs() {
c7eda013
EV
546 ddebug "install any Execs from the service files"
547 (
209f4b9e 548 export PKG_CONFIG_PATH=$BUILD_DIR/src/core/
c7eda013
EV
549 systemdsystemunitdir=$(pkg-config --variable=systemdsystemunitdir systemd)
550 systemduserunitdir=$(pkg-config --variable=systemduserunitdir systemd)
fe4bd4e5 551 sed -r -n 's|^Exec[a-zA-Z]*=[@+!-]*([^ ]+).*|\1|gp' $initdir/{$systemdsystemunitdir,$systemduserunitdir}/*.service \
e180bdb5 552 | sort -u | while read i; do
818567fc
MP
553 # some {rc,halt}.local scripts and programs are okay to not exist, the rest should
554 inst $i || [ "${i%.local}" != "$i" ] || [ "${i%systemd-update-done}" != "$i" ]
c7eda013
EV
555 done
556 )
889a9042
RC
557}
558
559generate_module_dependencies() {
560 if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
561 ! depmod -a -b "$initdir" $KERNEL_VER; then
562 dfatal "\"depmod -a $KERNEL_VER\" failed."
563 exit 1
564 fi
565}
566
567install_depmod_files() {
568 inst /lib/modules/$KERNEL_VER/modules.order
569 inst /lib/modules/$KERNEL_VER/modules.builtin
570}
571
572install_plymouth() {
573 # install plymouth, if found... else remove plymouth service files
574 # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
575 # PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
576 # /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
577 # dracut_install plymouth plymouthd
578 # else
579 rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
580 # fi
581}
582
583install_ld_so_conf() {
584 cp -a /etc/ld.so.conf* $initdir/etc
585 ldconfig -r "$initdir"
586}
587
588install_config_files() {
818567fc 589 inst /etc/sysconfig/init || true
889a9042
RC
590 inst /etc/passwd
591 inst /etc/shadow
bf3a947c 592 inst /etc/login.defs
889a9042
RC
593 inst /etc/group
594 inst /etc/shells
595 inst /etc/nsswitch.conf
818567fc
MP
596 inst /etc/pam.conf || true
597 inst /etc/securetty || true
889a9042
RC
598 inst /etc/os-release
599 inst /etc/localtime
600 # we want an empty environment
601 > $initdir/etc/environment
602 > $initdir/etc/machine-id
603 # set the hostname
604 echo systemd-testsuite > $initdir/etc/hostname
605 # fstab
85393d8f
TB
606 if [[ "$LOOKS_LIKE_SUSE" ]]; then
607 ROOTMOUNT="/dev/sda1 / ${FSTYPE} rw 0 1"
608 else
609 ROOTMOUNT="LABEL=systemd / ${FSTYPE} rw 0 1"
610 fi
611
889a9042 612 cat >$initdir/etc/fstab <<EOF
85393d8f 613$ROOTMOUNT
889a9042
RC
614EOF
615}
616
617install_basic_tools() {
618 [[ $BASICTOOLS ]] && dracut_install $BASICTOOLS
4be4833e 619 dracut_install -o sushell
7d023341
MP
620 # in Debian ldconfig is just a shell script wrapper around ldconfig.real
621 dracut_install -o ldconfig.real
889a9042
RC
622}
623
624install_debug_tools() {
625 [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
626}
627
628install_libnss() {
629 # install libnss_files for login
cffae62b 630 NSS_LIBS=$(LD_DEBUG=files getent passwd 2>&1 >/dev/null |sed -n '/calling init: .*libnss_/ {s!^.* /!/!; p}')
99877b7e 631 dracut_install $NSS_LIBS
889a9042
RC
632}
633
634install_dbus() {
3486cb6c 635 inst $ROOTLIBDIR/system/dbus.socket
a978c9f2
FS
636
637 # Fedora rawhide replaced dbus.service with dbus-daemon.service
638 if [ -f $ROOTLIBDIR/system/dbus-daemon.service ]; then
639 inst $ROOTLIBDIR/system/dbus-daemon.service
640 # Alias symlink
641 inst_symlink /etc/systemd/system/dbus.service
642 else
643 inst $ROOTLIBDIR/system/dbus.service
644 fi
889a9042
RC
645
646 find \
e63b61be 647 /etc/dbus-1 /usr/share/dbus-1 -xtype f \
889a9042
RC
648 | while read file; do
649 inst $file
650 done
651}
652
653install_pam() {
0fe15dc8 654 (
818567fc
MP
655 if [[ "$LOOKS_LIKE_DEBIAN" ]] && type -p dpkg-architecture &>/dev/null; then
656 find "/lib/$(dpkg-architecture -qDEB_HOST_MULTIARCH)/security" -xtype f
657 else
658 find /lib*/security -xtype f
659 fi
660 find /etc/pam.d /etc/security -xtype f
0fe15dc8 661 ) | while read file; do
889a9042
RC
662 inst $file
663 done
417491f1 664
d5172c79
EV
665 # pam_unix depends on unix_chkpwd.
666 # see http://www.linux-pam.org/Linux-PAM-html/sag-pam_unix.html
667 dracut_install -o unix_chkpwd
668
417491f1
EV
669 [[ "$LOOKS_LIKE_DEBIAN" ]] &&
670 cp /etc/pam.d/systemd-user $initdir/etc/pam.d/
e14b866b
ZJS
671
672 # set empty root password for easy debugging
673 sed -i 's/^root:x:/root::/' $initdir/etc/passwd
889a9042
RC
674}
675
676install_keymaps() {
83a7051e
YW
677 # The first three paths may be deprecated.
678 # It seems now the last two paths are used by many distributions.
889a9042
RC
679 for i in \
680 /usr/lib/kbd/keymaps/include/* \
681 /usr/lib/kbd/keymaps/i386/include/* \
83a7051e
YW
682 /usr/lib/kbd/keymaps/i386/qwerty/us.* \
683 /usr/lib/kbd/keymaps/legacy/include/* \
684 /usr/lib/kbd/keymaps/legacy/i386/qwerty/us.*; do
889a9042
RC
685 [[ -f $i ]] || continue
686 inst $i
687 done
ad931fee
YW
688
689 # When it takes any argument, then install more keymaps.
690 if [[ -n $1 ]]; then
691 for i in \
692 /usr/lib/kbd/keymaps/i386/*/* \
693 /usr/lib/kbd/keymaps/legacy/i386/*/*; do
694 [[ -f $i ]] || continue
695 inst $i
696 done
697 fi
889a9042
RC
698}
699
7d10ec1c
YW
700install_zoneinfo() {
701 for i in /usr/share/zoneinfo/{,*/,*/*/}*; do
702 [[ -f $i ]] || continue
703 inst $i
704 done
705}
706
889a9042
RC
707install_fonts() {
708 for i in \
25b47f96 709 /usr/lib/kbd/consolefonts/eurlatgr* \
889a9042
RC
710 /usr/lib/kbd/consolefonts/latarcyrheb-sun16*; do
711 [[ -f $i ]] || continue
712 inst $i
713 done
714}
715
716install_terminfo() {
717 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
718 [ -f ${_terminfodir}/l/linux ] && break
719 done
720 dracut_install -o ${_terminfodir}/l/linux
721}
722
723setup_testsuite() {
53d90f95 724 cp $TEST_BASE_DIR/testsuite.target $initdir/etc/systemd/system/
5c404f1a 725 cp $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/
889a9042
RC
726
727 mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
728 ln -fs $TEST_BASE_DIR/testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
729 ln -fs $TEST_BASE_DIR/end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
730
731 # make the testsuite the default target
732 ln -fs testsuite.target $initdir/etc/systemd/system/default.target
733}
734
735setup_nspawn_root() {
736 rm -fr $TESTDIR/nspawn-root
737 ddebug "cp -ar $initdir $TESTDIR/nspawn-root"
738 cp -ar $initdir $TESTDIR/nspawn-root
739 # we don't mount in the nspawn root
740 rm -f $TESTDIR/nspawn-root/etc/fstab
746fbd9c
EV
741 if [[ "$RUN_IN_UNPRIVILEGED_CONTAINER" = "yes" ]]; then
742 cp -ar $TESTDIR/nspawn-root $TESTDIR/unprivileged-nspawn-root
743 fi
889a9042
RC
744}
745
0d6e798a 746setup_basic_dirs() {
889a9042
RC
747 mkdir -p $initdir/run
748 mkdir -p $initdir/etc/systemd/system
749 mkdir -p $initdir/var/log/journal
750
0d6e798a 751 for d in usr/bin usr/sbin bin etc lib "$libdir" sbin tmp usr var var/log dev proc sys sysroot root run run/lock run/initramfs; do
898720b7
HH
752 if [ -L "/$d" ]; then
753 inst_symlink "/$d"
754 else
0d6e798a 755 inst_dir "/$d"
898720b7
HH
756 fi
757 done
758
759 ln -sfn /run "$initdir/var/run"
760 ln -sfn /run/lock "$initdir/var/lock"
761}
762
763inst_libs() {
764 local _bin=$1
765 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
766 local _file _line
767
768 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
769 [[ $_line = 'not a dynamic executable' ]] && break
770
771 if [[ $_line =~ $_so_regex ]]; then
772 _file=${BASH_REMATCH[1]}
773 [[ -e ${initdir}/$_file ]] && continue
774 inst_library "$_file"
775 continue
776 fi
777
778 if [[ $_line =~ not\ found ]]; then
779 dfatal "Missing a shared library required by $_bin."
780 dfatal "Run \"ldd $_bin\" to find out what it is."
781 dfatal "$_line"
782 dfatal "dracut cannot create an initrd."
783 exit 1
784 fi
785 done
786}
787
788import_testdir() {
898720b7
HH
789 [[ -e $STATEFILE ]] && . $STATEFILE
790 if [[ -z "$TESTDIR" ]] || [[ ! -d "$TESTDIR" ]]; then
791 TESTDIR=$(mktemp --tmpdir=/var/tmp -d -t systemd-test.XXXXXX)
792 echo "TESTDIR=\"$TESTDIR\"" > $STATEFILE
793 export TESTDIR
794 fi
795}
796
889a9042
RC
797import_initdir() {
798 initdir=$TESTDIR/root
799 export initdir
800}
801
898720b7
HH
802## @brief Converts numeric logging level to the first letter of level name.
803#
804# @param lvl Numeric logging level in range from 1 to 6.
805# @retval 1 if @a lvl is out of range.
806# @retval 0 if @a lvl is correct.
807# @result Echoes first letter of level name.
808_lvl2char() {
809 case "$1" in
810 1) echo F;;
811 2) echo E;;
812 3) echo W;;
813 4) echo I;;
814 5) echo D;;
815 6) echo T;;
816 *) return 1;;
817 esac
818}
819
820## @brief Internal helper function for _do_dlog()
821#
822# @param lvl Numeric logging level.
823# @param msg Message.
824# @retval 0 It's always returned, even if logging failed.
825#
826# @note This function is not supposed to be called manually. Please use
827# dtrace(), ddebug(), or others instead which wrap this one.
828#
829# This function calls _do_dlog() either with parameter msg, or if
830# none is given, it will read standard input and will use every line as
831# a message.
832#
833# This enables:
834# dwarn "This is a warning"
835# echo "This is a warning" | dwarn
20fc56c0 836LOG_LEVEL=${LOG_LEVEL:-4}
898720b7
HH
837
838dlog() {
839 [ -z "$LOG_LEVEL" ] && return 0
840 [ $1 -le $LOG_LEVEL ] || return 0
841 local lvl="$1"; shift
842 local lvlc=$(_lvl2char "$lvl") || return 0
843
844 if [ $# -ge 1 ]; then
845 echo "$lvlc: $*"
846 else
847 while read line; do
848 echo "$lvlc: " "$line"
849 done
850 fi
851}
852
853## @brief Logs message at TRACE level (6)
854#
855# @param msg Message.
856# @retval 0 It's always returned, even if logging failed.
857dtrace() {
858 set +x
859 dlog 6 "$@"
860 [ -n "$debug" ] && set -x || :
861}
862
863## @brief Logs message at DEBUG level (5)
864#
865# @param msg Message.
866# @retval 0 It's always returned, even if logging failed.
867ddebug() {
0d6e798a 868# set +x
898720b7 869 dlog 5 "$@"
0d6e798a 870# [ -n "$debug" ] && set -x || :
898720b7
HH
871}
872
873## @brief Logs message at INFO level (4)
874#
875# @param msg Message.
876# @retval 0 It's always returned, even if logging failed.
877dinfo() {
878 set +x
879 dlog 4 "$@"
880 [ -n "$debug" ] && set -x || :
881}
882
883## @brief Logs message at WARN level (3)
884#
885# @param msg Message.
886# @retval 0 It's always returned, even if logging failed.
887dwarn() {
888 set +x
889 dlog 3 "$@"
890 [ -n "$debug" ] && set -x || :
891}
892
893## @brief Logs message at ERROR level (2)
894#
895# @param msg Message.
896# @retval 0 It's always returned, even if logging failed.
897derror() {
0d6e798a 898# set +x
898720b7 899 dlog 2 "$@"
0d6e798a 900# [ -n "$debug" ] && set -x || :
898720b7
HH
901}
902
903## @brief Logs message at FATAL level (1)
904#
905# @param msg Message.
906# @retval 0 It's always returned, even if logging failed.
907dfatal() {
908 set +x
909 dlog 1 "$@"
910 [ -n "$debug" ] && set -x || :
911}
912
913
914# Generic substring function. If $2 is in $1, return 0.
915strstr() { [ "${1#*$2*}" != "$1" ]; }
916
917# normalize_path <path>
918# Prints the normalized path, where it removes any duplicated
919# and trailing slashes.
920# Example:
921# $ normalize_path ///test/test//
922# /test/test
923normalize_path() {
924 shopt -q -s extglob
925 set -- "${1//+(\/)//}"
926 shopt -q -u extglob
927 echo "${1%/}"
928}
929
930# convert_abs_rel <from> <to>
931# Prints the relative path, when creating a symlink to <to> from <from>.
932# Example:
933# $ convert_abs_rel /usr/bin/test /bin/test-2
934# ../../bin/test-2
935# $ ln -s $(convert_abs_rel /usr/bin/test /bin/test-2) /usr/bin/test
936convert_abs_rel() {
937 local __current __absolute __abssize __cursize __newpath
938 local -i __i __level
939
940 set -- "$(normalize_path "$1")" "$(normalize_path "$2")"
941
942 # corner case #1 - self looping link
943 [[ "$1" == "$2" ]] && { echo "${1##*/}"; return; }
944
945 # corner case #2 - own dir link
946 [[ "${1%/*}" == "$2" ]] && { echo "."; return; }
947
948 IFS="/" __current=($1)
949 IFS="/" __absolute=($2)
950
951 __abssize=${#__absolute[@]}
952 __cursize=${#__current[@]}
953
954 while [[ ${__absolute[__level]} == ${__current[__level]} ]]
955 do
956 (( __level++ ))
957 if (( __level > __abssize || __level > __cursize ))
958 then
959 break
960 fi
961 done
962
963 for ((__i = __level; __i < __cursize-1; __i++))
964 do
965 if ((__i > __level))
966 then
967 __newpath=$__newpath"/"
968 fi
969 __newpath=$__newpath".."
970 done
971
972 for ((__i = __level; __i < __abssize; __i++))
973 do
974 if [[ -n $__newpath ]]
975 then
976 __newpath=$__newpath"/"
977 fi
978 __newpath=$__newpath${__absolute[__i]}
979 done
980
981 echo "$__newpath"
982}
983
984
985# Install a directory, keeping symlinks as on the original system.
986# Example: if /lib points to /lib64 on the host, "inst_dir /lib/file"
987# will create ${initdir}/lib64, ${initdir}/lib64/file,
988# and a symlink ${initdir}/lib -> lib64.
989inst_dir() {
990 [[ -e ${initdir}/"$1" ]] && return 0 # already there
991
992 local _dir="$1" _part="${1%/*}" _file
993 while [[ "$_part" != "${_part%/*}" ]] && ! [[ -e "${initdir}/${_part}" ]]; do
994 _dir="$_part $_dir"
995 _part=${_part%/*}
996 done
997
998 # iterate over parent directories
999 for _file in $_dir; do
1000 [[ -e "${initdir}/$_file" ]] && continue
1001 if [[ -L $_file ]]; then
1002 inst_symlink "$_file"
1003 else
1004 # create directory
1005 mkdir -m 0755 -p "${initdir}/$_file" || return 1
1006 [[ -e "$_file" ]] && chmod --reference="$_file" "${initdir}/$_file"
1007 chmod u+w "${initdir}/$_file"
1008 fi
1009 done
1010}
1011
1012# $1 = file to copy to ramdisk
1013# $2 (optional) Name for the file on the ramdisk
1014# Location of the image dir is assumed to be $initdir
1015# We never overwrite the target if it exists.
1016inst_simple() {
1017 [[ -f "$1" ]] || return 1
1018 strstr "$1" "/" || return 1
1019
1020 local _src=$1 target="${2:-$1}"
1021 if ! [[ -d ${initdir}/$target ]]; then
1022 [[ -e ${initdir}/$target ]] && return 0
1023 [[ -L ${initdir}/$target ]] && return 0
1024 [[ -d "${initdir}/${target%/*}" ]] || inst_dir "${target%/*}"
1025 fi
1026 # install checksum files also
1027 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
1028 inst "${_src%/*}/.${_src##*/}.hmac" "${target%/*}/.${target##*/}.hmac"
1029 fi
1030 ddebug "Installing $_src"
1031 cp --sparse=always -pfL "$_src" "${initdir}/$target"
1032}
1033
1034# find symlinks linked to given library file
1035# $1 = library file
1036# Function searches for symlinks by stripping version numbers appended to
1037# library filename, checks if it points to the same target and finally
1038# prints the list of symlinks to stdout.
1039#
1040# Example:
1041# rev_lib_symlinks libfoo.so.8.1
1042# output: libfoo.so.8 libfoo.so
1043# (Only if libfoo.so.8 and libfoo.so exists on host system.)
1044rev_lib_symlinks() {
1045 [[ ! $1 ]] && return 0
1046
1047 local fn="$1" orig="$(readlink -f "$1")" links=''
1048
1049 [[ ${fn} =~ .*\.so\..* ]] || return 1
1050
1051 until [[ ${fn##*.} == so ]]; do
1052 fn="${fn%.*}"
1053 [[ -L ${fn} && $(readlink -f "${fn}") == ${orig} ]] && links+=" ${fn}"
1054 done
1055
1056 echo "${links}"
1057}
1058
1059# Same as above, but specialized to handle dynamic libraries.
1060# It handles making symlinks according to how the original library
1061# is referenced.
1062inst_library() {
1063 local _src="$1" _dest=${2:-$1} _lib _reallib _symlink
1064 strstr "$1" "/" || return 1
1065 [[ -e $initdir/$_dest ]] && return 0
1066 if [[ -L $_src ]]; then
1067 # install checksum files also
1068 if [[ -e "${_src%/*}/.${_src##*/}.hmac" ]]; then
1069 inst "${_src%/*}/.${_src##*/}.hmac" "${_dest%/*}/.${_dest##*/}.hmac"
1070 fi
1071 _reallib=$(readlink -f "$_src")
1072 inst_simple "$_reallib" "$_reallib"
1073 inst_dir "${_dest%/*}"
1074 [[ -d "${_dest%/*}" ]] && _dest=$(readlink -f "${_dest%/*}")/${_dest##*/}
1075 ln -sfn $(convert_abs_rel "${_dest}" "${_reallib}") "${initdir}/${_dest}"
1076 else
1077 inst_simple "$_src" "$_dest"
1078 fi
1079
1080 # Create additional symlinks. See rev_symlinks description.
1081 for _symlink in $(rev_lib_symlinks $_src) $(rev_lib_symlinks $_reallib); do
818567fc 1082 [[ -e $initdir/$_symlink ]] || {
898720b7
HH
1083 ddebug "Creating extra symlink: $_symlink"
1084 inst_symlink $_symlink
1085 }
1086 done
1087}
1088
1089# find a binary. If we were not passed the full path directly,
1090# search in the usual places to find the binary.
1091find_binary() {
1092 if [[ -z ${1##/*} ]]; then
1093 if [[ -x $1 ]] || { strstr "$1" ".so" && ldd $1 &>/dev/null; }; then
1094 echo $1
1095 return 0
1096 fi
1097 fi
1098
1099 type -P $1
1100}
1101
1102# Same as above, but specialized to install binary executables.
1103# Install binary executable, and all shared library dependencies, if any.
1104inst_binary() {
1105 local _bin _target
1106 _bin=$(find_binary "$1") || return 1
1107 _target=${2:-$_bin}
1108 [[ -e $initdir/$_target ]] && return 0
1109 [[ -L $_bin ]] && inst_symlink $_bin $_target && return 0
1110 local _file _line
1111 local _so_regex='([^ ]*/lib[^/]*/[^ ]*\.so[^ ]*)'
1112 # I love bash!
1113 LC_ALL=C ldd "$_bin" 2>/dev/null | while read _line; do
1114 [[ $_line = 'not a dynamic executable' ]] && break
1115
1116 if [[ $_line =~ $_so_regex ]]; then
1117 _file=${BASH_REMATCH[1]}
1118 [[ -e ${initdir}/$_file ]] && continue
1119 inst_library "$_file"
1120 continue
1121 fi
1122
1123 if [[ $_line =~ not\ found ]]; then
1124 dfatal "Missing a shared library required by $_bin."
1125 dfatal "Run \"ldd $_bin\" to find out what it is."
1126 dfatal "$_line"
1127 dfatal "dracut cannot create an initrd."
1128 exit 1
1129 fi
1130 done
1131 inst_simple "$_bin" "$_target"
1132}
1133
1134# same as above, except for shell scripts.
1135# If your shell script does not start with shebang, it is not a shell script.
1136inst_script() {
1137 local _bin
1138 _bin=$(find_binary "$1") || return 1
1139 shift
1140 local _line _shebang_regex
1141 read -r -n 80 _line <"$_bin"
1142 # If debug is set, clean unprintable chars to prevent messing up the term
1143 [[ $debug ]] && _line=$(echo -n "$_line" | tr -c -d '[:print:][:space:]')
1144 _shebang_regex='(#! *)(/[^ ]+).*'
1145 [[ $_line =~ $_shebang_regex ]] || return 1
1146 inst "${BASH_REMATCH[2]}" && inst_simple "$_bin" "$@"
1147}
1148
1149# same as above, but specialized for symlinks
1150inst_symlink() {
1151 local _src=$1 _target=${2:-$1} _realsrc
1152 strstr "$1" "/" || return 1
1153 [[ -L $1 ]] || return 1
1154 [[ -L $initdir/$_target ]] && return 0
1155 _realsrc=$(readlink -f "$_src")
1156 if ! [[ -e $initdir/$_realsrc ]]; then
1157 if [[ -d $_realsrc ]]; then
1158 inst_dir "$_realsrc"
1159 else
1160 inst "$_realsrc"
1161 fi
1162 fi
1163 [[ ! -e $initdir/${_target%/*} ]] && inst_dir "${_target%/*}"
1164 [[ -d ${_target%/*} ]] && _target=$(readlink -f ${_target%/*})/${_target##*/}
1165 ln -sfn $(convert_abs_rel "${_target}" "${_realsrc}") "$initdir/$_target"
1166}
1167
1168# attempt to install any programs specified in a udev rule
1169inst_rule_programs() {
1170 local _prog _bin
1171
1172 if grep -qE 'PROGRAM==?"[^ "]+' "$1"; then
1173 for _prog in $(grep -E 'PROGRAM==?"[^ "]+' "$1" | sed -r 's/.*PROGRAM==?"([^ "]+).*/\1/'); do
1174 if [ -x /lib/udev/$_prog ]; then
1175 _bin=/lib/udev/$_prog
1176 else
1177 _bin=$(find_binary "$_prog") || {
1178 dinfo "Skipping program $_prog using in udev rule $(basename $1) as it cannot be found"
1179 continue;
1180 }
1181 fi
1182
1183 #dinfo "Installing $_bin due to it's use in the udev rule $(basename $1)"
1184 dracut_install "$_bin"
1185 done
1186 fi
1187}
1188
1189# udev rules always get installed in the same place, so
1190# create a function to install them to make life simpler.
1191inst_rules() {
1192 local _target=/etc/udev/rules.d _rule _found
1193
1194 inst_dir "/lib/udev/rules.d"
1195 inst_dir "$_target"
1196 for _rule in "$@"; do
1197 if [ "${rule#/}" = "$rule" ]; then
1198 for r in /lib/udev/rules.d /etc/udev/rules.d; do
1199 if [[ -f $r/$_rule ]]; then
1200 _found="$r/$_rule"
1201 inst_simple "$_found"
1202 inst_rule_programs "$_found"
1203 fi
1204 done
1205 fi
1206 for r in '' ./ $dracutbasedir/rules.d/; do
1207 if [[ -f ${r}$_rule ]]; then
1208 _found="${r}$_rule"
1209 inst_simple "$_found" "$_target/${_found##*/}"
1210 inst_rule_programs "$_found"
1211 fi
1212 done
1213 [[ $_found ]] || dinfo "Skipping udev rule: $_rule"
31ce89e7 1214 _found=
898720b7
HH
1215 done
1216}
1217
1218# general purpose installation function
1219# Same args as above.
1220inst() {
1221 local _x
1222
1223 case $# in
1224 1) ;;
1225 2) [[ ! $initdir && -d $2 ]] && export initdir=$2
1226 [[ $initdir = $2 ]] && set $1;;
1227 3) [[ -z $initdir ]] && export initdir=$2
1228 set $1 $3;;
1229 *) dfatal "inst only takes 1 or 2 or 3 arguments"
1230 exit 1;;
1231 esac
1232 for _x in inst_symlink inst_script inst_binary inst_simple; do
1233 $_x "$@" && return 0
1234 done
1235 return 1
1236}
1237
1238# install any of listed files
1239#
1240# If first argument is '-d' and second some destination path, first accessible
1241# source is installed into this path, otherwise it will installed in the same
1242# path as source. If none of listed files was installed, function return 1.
1243# On first successful installation it returns with 0 status.
1244#
1245# Example:
1246#
1247# inst_any -d /bin/foo /bin/bar /bin/baz
1248#
1249# Lets assume that /bin/baz exists, so it will be installed as /bin/foo in
1250# initramfs.
1251inst_any() {
1252 local to f
1253
1254 [[ $1 = '-d' ]] && to="$2" && shift 2
1255
1256 for f in "$@"; do
1257 if [[ -e $f ]]; then
1258 [[ $to ]] && inst "$f" "$to" && return 0
1259 inst "$f" && return 0
1260 fi
1261 done
1262
1263 return 1
1264}
1265
1266# dracut_install [-o ] <file> [<file> ... ]
1267# Install <file> to the initramfs image
1268# -o optionally install the <file> and don't fail, if it is not there
1269dracut_install() {
1270 local _optional=no
1271 if [[ $1 = '-o' ]]; then
1272 _optional=yes
1273 shift
1274 fi
1275 while (($# > 0)); do
1276 if ! inst "$1" ; then
1277 if [[ $_optional = yes ]]; then
1278 dinfo "Skipping program $1 as it cannot be found and is" \
1279 "flagged to be optional"
1280 else
1281 dfatal "Failed to install $1"
1282 exit 1
1283 fi
1284 fi
1285 shift
1286 done
1287}
1288
0d6e798a
HH
1289# Install a single kernel module along with any firmware it may require.
1290# $1 = full path to kernel module to install
1291install_kmod_with_fw() {
1292 # no need to go further if the module is already installed
1293
1294 [[ -e "${initdir}/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" ]] \
1295 && return 0
1296
1297 [[ -e "$initdir/.kernelmodseen/${1##*/}" ]] && return 0
1298
1299 if [[ $omit_drivers ]]; then
1300 local _kmod=${1##*/}
1301 _kmod=${_kmod%.ko}
1302 _kmod=${_kmod/-/_}
1303 if [[ "$_kmod" =~ $omit_drivers ]]; then
1304 dinfo "Omitting driver $_kmod"
1305 return 1
1306 fi
1307 if [[ "${1##*/lib/modules/$KERNEL_VER/}" =~ $omit_drivers ]]; then
1308 dinfo "Omitting driver $_kmod"
1309 return 1
1310 fi
1311 fi
1312
1313 [ -d "$initdir/.kernelmodseen" ] && \
1314 > "$initdir/.kernelmodseen/${1##*/}"
1315
1316 inst_simple "$1" "/lib/modules/$KERNEL_VER/${1##*/lib/modules/$KERNEL_VER/}" \
1317 || return $?
1318
1319 local _modname=${1##*/} _fwdir _found _fw
1320 _modname=${_modname%.ko*}
1321 for _fw in $(modinfo -k $KERNEL_VER -F firmware $1 2>/dev/null); do
1322 _found=''
1323 for _fwdir in $fw_dir; do
1324 if [[ -d $_fwdir && -f $_fwdir/$_fw ]]; then
1325 inst_simple "$_fwdir/$_fw" "/lib/firmware/$_fw"
1326 _found=yes
1327 fi
1328 done
1329 if [[ $_found != yes ]]; then
1330 if ! grep -qe "\<${_modname//-/_}\>" /proc/modules; then
1331 dinfo "Possible missing firmware \"${_fw}\" for kernel module" \
1332 "\"${_modname}.ko\""
1333 else
1334 dwarn "Possible missing firmware \"${_fw}\" for kernel module" \
1335 "\"${_modname}.ko\""
1336 fi
1337 fi
1338 done
1339 return 0
1340}
1341
1342# Do something with all the dependencies of a kernel module.
1343# Note that kernel modules depend on themselves using the technique we use
1344# $1 = function to call for each dependency we find
1345# It will be passed the full path to the found kernel module
1346# $2 = module to get dependencies for
1347# rest of args = arguments to modprobe
1348# _fderr specifies FD passed from surrounding scope
1349for_each_kmod_dep() {
1350 local _func=$1 _kmod=$2 _cmd _modpath _options _found=0
1351 shift 2
1352 modprobe "$@" --ignore-install --show-depends $_kmod 2>&${_fderr} | (
1353 while read _cmd _modpath _options; do
1354 [[ $_cmd = insmod ]] || continue
1355 $_func ${_modpath} || exit $?
1356 _found=1
1357 done
1358 [[ $_found -eq 0 ]] && exit 1
1359 exit 0
1360 )
1361}
1362
1363# filter kernel modules to install certain modules that meet specific
1364# requirements.
1365# $1 = search only in subdirectory of /kernel/$1
1366# $2 = function to call with module name to filter.
1367# This function will be passed the full path to the module to test.
c5315881 1368# The behavior of this function can vary depending on whether $hostonly is set.
0d6e798a
HH
1369# If it is, we will only look at modules that are already in memory.
1370# If it is not, we will look at all kernel modules
1371# This function returns the full filenames of modules that match $1
1372filter_kernel_modules_by_path () (
1373 local _modname _filtercmd
1374 if ! [[ $hostonly ]]; then
1375 _filtercmd='find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra"'
1376 _filtercmd+=' "$KERNEL_MODS/weak-updates" -name "*.ko" -o -name "*.ko.gz"'
1377 _filtercmd+=' -o -name "*.ko.xz"'
1378 _filtercmd+=' 2>/dev/null'
1379 else
1380 _filtercmd='cut -d " " -f 1 </proc/modules|xargs modinfo -F filename '
1381 _filtercmd+='-k $KERNEL_VER 2>/dev/null'
1382 fi
1383 for _modname in $(eval $_filtercmd); do
1384 case $_modname in
1385 *.ko) "$2" "$_modname" && echo "$_modname";;
1386 *.ko.gz) gzip -dc "$_modname" > $initdir/$$.ko
1387 $2 $initdir/$$.ko && echo "$_modname"
1388 rm -f $initdir/$$.ko
1389 ;;
1390 *.ko.xz) xz -dc "$_modname" > $initdir/$$.ko
1391 $2 $initdir/$$.ko && echo "$_modname"
1392 rm -f $initdir/$$.ko
1393 ;;
1394 esac
1395 done
1396)
1397find_kernel_modules_by_path () (
1398 if ! [[ $hostonly ]]; then
1399 find "$KERNEL_MODS/kernel/$1" "$KERNEL_MODS/extra" "$KERNEL_MODS/weak-updates" \
1400 -name "*.ko" -o -name "*.ko.gz" -o -name "*.ko.xz" 2>/dev/null
1401 else
1402 cut -d " " -f 1 </proc/modules \
1403 | xargs modinfo -F filename -k $KERNEL_VER 2>/dev/null
1404 fi
1405)
1406
1407filter_kernel_modules () {
1408 filter_kernel_modules_by_path drivers "$1"
1409}
1410
1411find_kernel_modules () {
1412 find_kernel_modules_by_path drivers
1413}
1414
1415# instmods [-c] <kernel module> [<kernel module> ... ]
1416# instmods [-c] <kernel subsystem>
1417# install kernel modules along with all their dependencies.
1418# <kernel subsystem> can be e.g. "=block" or "=drivers/usb/storage"
1419instmods() {
1420 [[ $no_kernel = yes ]] && return
1421 # called [sub]functions inherit _fderr
1422 local _fderr=9
1423 local _check=no
1424 if [[ $1 = '-c' ]]; then
1425 _check=yes
1426 shift
1427 fi
1428
1429 function inst1mod() {
1430 local _ret=0 _mod="$1"
1431 case $_mod in
1432 =*)
1433 if [ -f $KERNEL_MODS/modules.${_mod#=} ]; then
1434 ( [[ "$_mpargs" ]] && echo $_mpargs
1435 cat "${KERNEL_MODS}/modules.${_mod#=}" ) \
1436 | instmods
1437 else
1438 ( [[ "$_mpargs" ]] && echo $_mpargs
1439 find "$KERNEL_MODS" -path "*/${_mod#=}/*" -printf '%f\n' ) \
1440 | instmods
1441 fi
1442 ;;
1443 --*) _mpargs+=" $_mod" ;;
1444 i2o_scsi) return ;; # Do not load this diagnostic-only module
1445 *)
1446 _mod=${_mod##*/}
1447 # if we are already installed, skip this module and go on
1448 # to the next one.
1449 [[ -f "$initdir/.kernelmodseen/${_mod%.ko}.ko" ]] && return
1450
1451 if [[ $omit_drivers ]] && [[ "$1" =~ $omit_drivers ]]; then
1452 dinfo "Omitting driver ${_mod##$KERNEL_MODS}"
1453 return
1454 fi
1455 # If we are building a host-specific initramfs and this
1456 # module is not already loaded, move on to the next one.
1457 [[ $hostonly ]] && ! grep -qe "\<${_mod//-/_}\>" /proc/modules \
1458 && ! echo $add_drivers | grep -qe "\<${_mod}\>" \
1459 && return
1460
1461 # We use '-d' option in modprobe only if modules prefix path
1462 # differs from default '/'. This allows us to use Dracut with
1463 # old version of modprobe which doesn't have '-d' option.
1464 local _moddirname=${KERNEL_MODS%%/lib/modules/*}
1465 [[ -n ${_moddirname} ]] && _moddirname="-d ${_moddirname}/"
1466
1467 # ok, load the module, all its dependencies, and any firmware
1468 # it may require
1469 for_each_kmod_dep install_kmod_with_fw $_mod \
1470 --set-version $KERNEL_VER ${_moddirname} $_mpargs
1471 ((_ret+=$?))
1472 ;;
1473 esac
1474 return $_ret
1475 }
1476
1477 function instmods_1() {
1478 local _mod _mpargs
1479 if (($# == 0)); then # filenames from stdin
1480 while read _mod; do
1481 inst1mod "${_mod%.ko*}" || {
1482 if [ "$_check" = "yes" ]; then
1483 dfatal "Failed to install $_mod"
1484 return 1
1485 fi
1486 }
1487 done
1488 fi
1489 while (($# > 0)); do # filenames as arguments
1490 inst1mod ${1%.ko*} || {
1491 if [ "$_check" = "yes" ]; then
1492 dfatal "Failed to install $1"
1493 return 1
1494 fi
1495 }
1496 shift
1497 done
1498 return 0
1499 }
1500
1501 local _ret _filter_not_found='FATAL: Module .* not found.'
1502 set -o pipefail
1503 # Capture all stderr from modprobe to _fderr. We could use {var}>...
1504 # redirections, but that would make dracut require bash4 at least.
1505 eval "( instmods_1 \"\$@\" ) ${_fderr}>&1" \
1506 | while read line; do [[ "$line" =~ $_filter_not_found ]] && echo $line || echo $line >&2 ;done | derror
1507 _ret=$?
1508 set +o pipefail
1509 return $_ret
1510}
898720b7
HH
1511
1512# inst_libdir_file [-n <pattern>] <file> [<file>...]
1513# Install a <file> located on a lib directory to the initramfs image
1514# -n <pattern> install non-matching files
1515inst_libdir_file() {
1516 if [[ "$1" == "-n" ]]; then
1517 local _pattern=$1
1518 shift 2
1519 for _dir in $libdirs; do
1520 for _i in "$@"; do
1521 for _f in "$_dir"/$_i; do
1522 [[ "$_i" =~ $_pattern ]] || continue
1523 [[ -e "$_i" ]] && dracut_install "$_i"
1524 done
1525 done
1526 done
1527 else
1528 for _dir in $libdirs; do
1529 for _i in "$@"; do
1530 for _f in "$_dir"/$_i; do
1531 [[ -e "$_f" ]] && dracut_install "$_f"
1532 done
1533 done
1534 done
1535 fi
1536}
1537
85393d8f 1538setup_suse() {
caced732
FB
1539 ln -fs ../usr/bin/systemctl $initdir/bin/
1540 ln -fs ../usr/lib/systemd $initdir/lib/
85393d8f
TB
1541 inst_simple "/usr/lib/systemd/system/haveged.service"
1542}
1543
054ee249
MP
1544# can be overridden in specific test
1545test_cleanup() {
1546 umount $TESTDIR/root 2>/dev/null || true
818567fc 1547 [[ $LOOPDEV ]] && losetup -d $LOOPDEV || true
054ee249
MP
1548 return 0
1549}
1550
1551test_run() {
1552 if [ -z "$TEST_NO_QEMU" ]; then
1553 if run_qemu; then
1554 check_result_qemu || return 1
1555 else
1556 dwarn "can't run QEMU, skipping"
1557 fi
1558 fi
1559 if [ -z "$TEST_NO_NSPAWN" ]; then
746fbd9c
EV
1560 if run_nspawn "nspawn-root"; then
1561 check_result_nspawn "nspawn-root" || return 1
054ee249
MP
1562 else
1563 dwarn "can't run systemd-nspawn, skipping"
1564 fi
746fbd9c
EV
1565
1566 if [[ "$RUN_IN_UNPRIVILEGED_CONTAINER" = "yes" ]]; then
1567 if NSPAWN_ARGUMENTS="-U --private-network $NSPAWN_ARGUMENTS" run_nspawn "unprivileged-nspawn-root"; then
1568 check_result_nspawn "unprivileged-nspawn-root" || return 1
1569 else
1570 dwarn "can't run systemd-nspawn, skipping"
1571 fi
1572 fi
054ee249
MP
1573 fi
1574 return 0
1575}
1576
898720b7 1577do_test() {
33a5e20f
HH
1578 if [[ $UID != "0" ]]; then
1579 echo "TEST: $TEST_DESCRIPTION [SKIPPED]: not root" >&2
1580 exit 0
1581 fi
1582
898720b7
HH
1583# Detect lib paths
1584 [[ $libdir ]] || for libdir in /lib64 /lib; do
1585 [[ -d $libdir ]] && libdirs+=" $libdir" && break
1586 done
1587
1588 [[ $usrlibdir ]] || for usrlibdir in /usr/lib64 /usr/lib; do
1589 [[ -d $usrlibdir ]] && libdirs+=" $usrlibdir" && break
1590 done
1591
22077c9c
MP
1592 mkdir -p "$STATEDIR"
1593
898720b7 1594 import_testdir
889a9042 1595 import_initdir
898720b7
HH
1596
1597 while (($# > 0)); do
1598 case $1 in
1599 --run)
1600 echo "TEST RUN: $TEST_DESCRIPTION"
0013fac2
YW
1601 test_run
1602 ret=$?
1603 if (( $ret == 0 )); then
898720b7
HH
1604 echo "TEST RUN: $TEST_DESCRIPTION [OK]"
1605 else
1606 echo "TEST RUN: $TEST_DESCRIPTION [FAILED]"
1607 fi
1608 exit $ret;;
1609 --setup)
1610 echo "TEST SETUP: $TEST_DESCRIPTION"
1611 test_setup
818567fc 1612 ;;
898720b7
HH
1613 --clean)
1614 echo "TEST CLEANUP: $TEST_DESCRIPTION"
1615 test_cleanup
1616 rm -fr "$TESTDIR"
22077c9c 1617 rm -f "$STATEFILE"
818567fc 1618 ;;
898720b7 1619 --all)
818567fc 1620 ret=0
898720b7
HH
1621 echo -n "TEST: $TEST_DESCRIPTION ";
1622 (
1623 test_setup && test_run
1624 ret=$?
1625 test_cleanup
1626 rm -fr "$TESTDIR"
22077c9c 1627 rm -f "$STATEFILE"
898720b7 1628 exit $ret
818567fc 1629 ) </dev/null >"$TESTLOG" 2>&1 || ret=$?
898720b7 1630 if [ $ret -eq 0 ]; then
22077c9c 1631 rm "$TESTLOG"
898720b7
HH
1632 echo "[OK]"
1633 else
1634 echo "[FAILED]"
22077c9c 1635 echo "see $TESTLOG"
898720b7
HH
1636 fi
1637 exit $ret;;
1638 *) break ;;
1639 esac
1640 shift
1641 done
1642}