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