]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/test-fstab-generator.sh
test-fstab-generator: fix test on systemd with systemd-boot
[thirdparty/systemd.git] / test / test-fstab-generator.sh
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 set -e
4 shopt -s nullglob
5 shopt -s globstar
6
7 if [[ -n "$1" ]]; then
8 generator=$1
9 elif [[ -x /usr/lib/systemd/system-generators/systemd-fstab-generator ]]; then
10 generator=/usr/lib/systemd/system-generators/systemd-fstab-generator
11 elif [[ -x /lib/systemd/system-generators/systemd-fstab-generator ]]; then
12 generator=/lib/systemd/system-generators/systemd-fstab-generator
13 else
14 exit 1
15 fi
16
17 src="$(dirname "$0")/testdata/test-fstab-generator"
18
19 # fsck(8) is located in /usr/sbin on Debian
20 PATH=$PATH:/usr/sbin
21
22 # systemd-pcrfs@.service could be enabled or not, depending on the host state
23 # of the host system. Override the measurement to avoid the issue.
24 export SYSTEMD_FORCE_MEASURE=0
25
26 for f in "$src"/test-*.input; do
27 echo "*** Running $f"
28
29 (
30 out=$(mktemp --tmpdir --directory "test-fstab-generator.XXXXXXXXXX")
31 # shellcheck disable=SC2064
32 trap "rm -rf '$out'" EXIT INT QUIT PIPE
33
34 exp="${f%.input}.expected"
35 if [[ "${f##*/}" =~ swap ]] && systemd-detect-virt --container >/dev/null; then
36 exp="${exp}.container"
37 fi
38
39 if [[ "${f##*/}" =~ \.fstab\.input ]]; then
40 SYSTEMD_LOG_LEVEL=debug SYSTEMD_IN_INITRD=yes SYSTEMD_SYSFS_CHECK=no SYSTEMD_PROC_CMDLINE="fstab=yes root=fstab" SYSTEMD_FSTAB="$f" SYSTEMD_SYSROOT_FSTAB="/dev/null" $generator "$out" "$out" "$out"
41 else
42 SYSTEMD_LOG_LEVEL=debug SYSTEMD_IN_INITRD=yes SYSTEMD_SYSFS_CHECK=no SYSTEMD_PROC_CMDLINE="fstab=no $(cat "$f")" $generator "$out" "$out" "$out"
43 fi
44
45 # The option x-systemd.growfs creates symlink to system's systemd-growfs@.service in .mount.wants directory.
46 # The system that the test is currently running on may not have or may have outdated unit file.
47 # Let's replace the symlink with an empty file.
48 for i in "$out"/*/systemd-growfs@*.service; do
49 [[ -L "$i" ]] || continue
50 rm "$i"
51 touch "$i"
52 done
53
54 # For split-usr system
55 for i in "$out"/systemd-*.service; do
56 sed -i -e 's:ExecStart=/lib/systemd/:ExecStart=/usr/lib/systemd/:' "$i"
57 done
58
59 if [[ "${f##*/}" =~ \.fstab\.input ]]; then
60 for i in "$out"/*.{automount,mount,swap}; do
61 sed -i -e 's:SourcePath=.*$:SourcePath=/etc/fstab:' "$i"
62 done
63 fi
64
65 # .deb packager seems to dislike files named with backslash. So, as a workaround, we store files
66 # without backslash in .expected.
67 for i in "$out"/**/*\\*.{mount,swap}; do
68 k="${i//\\/}"
69 if [[ "$i" != "$k" ]]; then
70 if [[ -f "$i" ]]; then
71 mv "$i" "$k"
72 elif [[ -L "$i" ]]; then
73 dest=$(readlink "$i")
74 rm "$i"
75 ln -s "${dest//\\/}" "$k"
76 fi
77 fi
78 done
79
80 # We store empty files rather than dead symlinks, so that they don't get pruned when packaged up, so compare
81 # the list of filenames rather than their content
82 if ! diff -u <(find "$out" -printf '%P\n' | sort) <(find "$exp" -printf '%P\n' | sort); then
83 echo "**** Unexpected output for $f"
84 exit 1
85 fi
86
87 # Check the main units.
88 if ! diff -u "$out" "$exp"; then
89 echo "**** Unexpected output for $f"
90 exit 1
91 fi
92
93 # Also check drop-ins.
94 for i in "$out"/*; do
95 [[ -d "$i" ]] || continue
96
97 dir="${i##*/}"
98
99 for j in "$i"/*; do
100 fname="${j##*/}"
101 expf="$exp/$dir/$fname"
102
103 if [[ -L "$j" && ! -e "$j" ]]; then
104 # For dead symlink, we store an empty file.
105 if [[ ! -e "$expf" || -n "$(cat "$expf")" ]]; then
106 echo "**** Unexpected symlink $j created by $f"
107 exit 1
108 fi
109 continue
110 fi
111
112 if ! diff -u "$j" "$expf"; then
113 echo "**** Unexpected output in $j for $f"
114 exit 1
115 fi
116 done
117 done
118 ) || exit 1
119 done