]> git.ipfire.org Git - thirdparty/systemd.git/blame - test/test-fstab-generator.sh
Merge pull request #32249 from CodethinkLabs/vmspawn/predicatable_tap_names
[thirdparty/systemd.git] / test / test-fstab-generator.sh
CommitLineData
8cc8a073
YW
1#!/usr/bin/env bash
2# SPDX-License-Identifier: LGPL-2.1-or-later
7f8c67c9 3set -eux
70bf9f62
YW
4shopt -s nullglob
5shopt -s globstar
8cc8a073 6
7f8c67c9 7if [[ -n "${1:-}" ]]; then
8cc8a073
YW
8 generator=$1
9elif [[ -x /usr/lib/systemd/system-generators/systemd-fstab-generator ]]; then
10 generator=/usr/lib/systemd/system-generators/systemd-fstab-generator
11elif [[ -x /lib/systemd/system-generators/systemd-fstab-generator ]]; then
12 generator=/lib/systemd/system-generators/systemd-fstab-generator
13else
14 exit 1
15fi
16
17src="$(dirname "$0")/testdata/test-fstab-generator"
18
a45efc9e
CG
19# fsck(8) is located in /usr/sbin on Debian
20PATH=$PATH:/usr/sbin
21
088d8c99
ZJS
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.
24export SYSTEMD_FORCE_MEASURE=0
25
7f8c67c9 26test_one() (
aeded6b0 27 local initrd input out exp i j k dir fname expf
7f8c67c9
YW
28
29 input=${1?}
aeded6b0 30 initrd=${2?}
7f8c67c9 31
aeded6b0 32 : "*** Running $input (initrd=$initrd)"
7f8c67c9
YW
33
34 out=$(mktemp --tmpdir --directory "test-fstab-generator.XXXXXXXXXX")
35 # shellcheck disable=SC2064
36 trap "rm -rf '$out'" EXIT INT QUIT PIPE
37
38 exp="${input%.input}.expected"
39 if [[ "${input##*/}" =~ swap ]] && systemd-detect-virt --container >/dev/null; then
40 exp="${exp}.container"
41 fi
aeded6b0
YW
42 if [[ "$initrd" == no ]]; then
43 exp="${exp}.sysroot"
44 fi
7f8c67c9
YW
45
46 if [[ "${input##*/}" =~ \.fstab\.input ]]; then
aeded6b0 47 SYSTEMD_LOG_LEVEL=debug SYSTEMD_IN_INITRD="$initrd" SYSTEMD_SYSFS_CHECK=no SYSTEMD_PROC_CMDLINE="fstab=yes root=fstab" SYSTEMD_FSTAB="$input" SYSTEMD_SYSROOT_FSTAB="/dev/null" $generator "$out" "$out" "$out"
7f8c67c9 48 else
aeded6b0 49 SYSTEMD_LOG_LEVEL=debug SYSTEMD_IN_INITRD="$initrd" SYSTEMD_SYSFS_CHECK=no SYSTEMD_PROC_CMDLINE="fstab=no $(cat "$input")" $generator "$out" "$out" "$out"
7f8c67c9
YW
50 fi
51
52 # The option x-systemd.growfs creates symlink to system's systemd-growfs@.service in .mount.wants directory.
aeded6b0 53 # Also, when $initrd is no, symlink to systemd-remount-fs.service is created.
7f8c67c9
YW
54 # The system that the test is currently running on may not have or may have outdated unit file.
55 # Let's replace the symlink with an empty file.
aeded6b0 56 for i in "$out"/*/systemd-growfs@*.service "$out"/local-fs.target.wants/systemd-remount-fs.service; do
7f8c67c9
YW
57 [[ -L "$i" ]] || continue
58 rm "$i"
59 touch "$i"
60 done
61
7f8c67c9
YW
62 if [[ "${input##*/}" =~ \.fstab\.input ]]; then
63 for i in "$out"/*.{automount,mount,swap}; do
64 sed -i -e 's:SourcePath=.*$:SourcePath=/etc/fstab:' "$i"
82c29dbe 65 done
7f8c67c9
YW
66 fi
67
68 # .deb packager seems to dislike files named with backslash. So, as a workaround, we store files
69 # without backslash in .expected.
70 for i in "$out"/**/*\\*.{mount,swap}; do
71 k="${i//\\/}"
72 if [[ "$i" != "$k" ]]; then
73 if [[ -f "$i" ]]; then
74 mv "$i" "$k"
75 elif [[ -L "$i" ]]; then
76 dest=$(readlink "$i")
77 rm "$i"
78 ln -s "${dest//\\/}" "$k"
79 fi
8cc8a073 80 fi
7f8c67c9
YW
81 done
82
aeded6b0
YW
83 # We do not store empty directory.
84 if [[ -z "$(ls -A "$out")" && ! -d "$exp" ]]; then
85 return 0
86 fi
87
7f8c67c9
YW
88 # We store empty files rather than dead symlinks, so that they don't get pruned when packaged up, so compare
89 # the list of filenames rather than their content
90 if ! diff -u <(find "$out" -printf '%P\n' | sort) <(find "$exp" -printf '%P\n' | sort); then
aeded6b0 91 : "**** Unexpected output for $input (initrd=$initrd)"
7f8c67c9
YW
92 return 1
93 fi
94
95 # Check the main units.
96 if ! diff -u "$out" "$exp"; then
aeded6b0 97 : "**** Unexpected output for $input (initrd=$initrd)"
7f8c67c9
YW
98 return 1
99 fi
100
101 # Also check drop-ins.
102 for i in "$out"/*; do
103 [[ -d "$i" ]] || continue
104
105 dir="${i##*/}"
106
107 for j in "$i"/*; do
108 fname="${j##*/}"
109 expf="$exp/$dir/$fname"
110
111 if [[ -L "$j" && ! -e "$j" ]]; then
112 # For dead symlink, we store an empty file.
113 if [[ ! -e "$expf" || -n "$(cat "$expf")" ]]; then
aeded6b0 114 : "**** Unexpected symlink $j created by $input (initrd=$initrd)"
7f8c67c9 115 return 1
93ec924c 116 fi
7f8c67c9 117 continue
93ec924c 118 fi
93ec924c 119
7f8c67c9 120 if ! diff -u "$j" "$expf"; then
aeded6b0 121 : "**** Unexpected output in $j for $input (initrd=$initrd)"
7f8c67c9
YW
122 return 1
123 fi
124 done
125 done
70bf9f62 126
7f8c67c9
YW
127 return 0
128)
70bf9f62 129
7f8c67c9 130for f in "$src"/test-*.input; do
3570ee36
DB
131 # If /mnt is a symlink, then the expected output from this
132 # test scenario will not match the actual output
133 if test "$f" = "$src/test-18-options.fstab.input" -a "$(readlink /mnt)" != "/mnt"
134 then
135 echo "Skip $f because /mnt is a symlink"
136 continue
137 fi
138
aeded6b0
YW
139 test_one "$f" yes
140 test_one "$f" no
8cc8a073 141done