]> git.ipfire.org Git - thirdparty/dracut.git/blob - test/TEST-04-FULL-SYSTEMD/test.sh
ci: run the integration tests on more distributions
[thirdparty/dracut.git] / test / TEST-04-FULL-SYSTEMD / test.sh
1 #!/bin/bash
2
3 # shellcheck disable=SC2034
4 TEST_DESCRIPTION="Full systemd serialization/deserialization test with /usr mount"
5
6 export KVERSION=${KVERSION-$(uname -r)}
7
8 # Uncomment this to debug failures
9 #DEBUGFAIL="rd.shell rd.break"
10 #DEBUGFAIL="rd.shell"
11 #DEBUGOUT="quiet systemd.log_level=debug systemd.log_target=console loglevel=77 rd.info rd.debug"
12 DEBUGOUT="loglevel=0 "
13 client_run() {
14 local test_name="$1"
15 shift
16 local client_opts="$*"
17
18 echo "CLIENT TEST START: $test_name"
19
20 dd if=/dev/zero of="$TESTDIR"/marker.img bs=1MiB count=1
21 declare -a disk_args=()
22 # shellcheck disable=SC2034
23 declare -i disk_index=0
24 qemu_add_drive_args disk_index disk_args "$TESTDIR"/marker.img marker
25 qemu_add_drive_args disk_index disk_args "$TESTDIR"/root.btrfs root
26 qemu_add_drive_args disk_index disk_args "$TESTDIR"/usr.btrfs usr
27
28 "$testdir"/run-qemu \
29 "${disk_args[@]}" \
30 -append "panic=1 oops=panic softlockup_panic=1 systemd.crash_reboot root=LABEL=dracut $client_opts rd.retry=3 console=ttyS0,115200n81 selinux=0 $DEBUGOUT rd.shell=0 $DEBUGFAIL" \
31 -initrd "$TESTDIR"/initramfs.testing || return 1
32
33 if ! grep -U --binary-files=binary -F -m 1 -q dracut-root-block-success "$TESTDIR"/marker.img; then
34 echo "CLIENT TEST END: $test_name [FAILED]"
35 return 1
36 fi
37 echo "CLIENT TEST END: $test_name [OK]"
38
39 }
40
41 test_run() {
42 client_run "no option specified" || return 1
43 client_run "readonly root" "ro" || return 1
44 client_run "writeable root" "rw" || return 1
45 return 0
46 }
47
48 test_setup() {
49 # shellcheck disable=SC2064
50 trap "$(shopt -p nullglob globstar)" RETURN
51 shopt -q -s nullglob globstar
52
53 export kernel=$KVERSION
54 # Create what will eventually be our root filesystem onto an overlay
55 (
56 # shellcheck disable=SC2030
57 export initdir=$TESTDIR/overlay/source
58 mkdir -p "$initdir"
59 # shellcheck disable=SC1090
60 . "$basedir"/dracut-init.sh
61
62 for d in usr/bin usr/sbin bin etc lib "$libdir" sbin tmp usr var var/log dev proc sys sysroot root run; do
63 if [ -L "/$d" ]; then
64 inst_symlink "/$d"
65 else
66 inst_dir "/$d"
67 fi
68 done
69
70 ln -sfn /run "$initdir/var/run"
71 ln -sfn /run/lock "$initdir/var/lock"
72
73 inst_multiple sh df free ls shutdown poweroff stty cat ps ln ip \
74 mount dmesg mkdir cp ping dd \
75 umount strace less setsid systemctl reset sync
76
77 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
78 [ -f ${_terminfodir}/l/linux ] && break
79 done
80 inst_multiple -o ${_terminfodir}/l/linux
81 inst_multiple grep
82 inst_simple ./fstab /etc/fstab
83 if type -P rpm &> /dev/null; then
84 rpm -ql systemd | xargs -r "$DRACUT_INSTALL" ${initdir:+-D "$initdir"} -o -a -l
85 elif type -P pacman &> /dev/null; then
86 pacman -Q -l systemd | while read -r _ a; do printf -- "%s\0" "$a"; done | xargs -0 -r "$DRACUT_INSTALL" ${initdir:+-D "$initdir"} -o -a -l
87 rm "$initdir"/usr/lib/systemd/system/sysinit.target.wants/systemd-firstboot.service
88 else
89 echo "Can't install systemd base"
90 return 1
91 fi
92 inst /sbin/init
93 inst_multiple -o {,/usr}/lib/systemd/system/"dracut*"
94
95 inst_simple "${basedir}/modules.d/99base/dracut-lib.sh" "/lib/dracut-lib.sh"
96 inst_binary "${basedir}/dracut-util" "/usr/bin/dracut-util"
97 ln -s dracut-util "${initdir}/usr/bin/dracut-getarg"
98 ln -s dracut-util "${initdir}/usr/bin/dracut-getargs"
99
100 # make a journal directory
101 mkdir -p "$initdir"/var/log/journal
102
103 # install some basic config files
104 inst_multiple -o \
105 /etc/machine-id \
106 /etc/adjtime \
107 /etc/passwd \
108 /etc/shadow \
109 /etc/group \
110 /etc/shells \
111 {,/usr}/etc/nsswitch.conf \
112 /etc/pam.conf \
113 /etc/securetty \
114 /etc/os-release \
115 /etc/localtime
116
117 # we want an empty environment
118 : > "$initdir"/etc/environment
119
120 # setup the testsuite target
121 mkdir -p "$initdir"/etc/systemd/system
122 cat > "$initdir"/etc/systemd/system/testsuite.target << EOF
123 [Unit]
124 Description=Testsuite target
125 Requires=basic.target
126 After=basic.target
127 Conflicts=rescue.target
128 AllowIsolate=yes
129 EOF
130
131 inst ./test-init.sh /sbin/test-init
132
133 # setup the testsuite service
134 cat > "$initdir"/etc/systemd/system/testsuite.service << EOF
135 [Unit]
136 Description=Testsuite service
137 After=basic.target
138
139 [Service]
140 ExecStart=/sbin/test-init
141 Type=oneshot
142 StandardInput=tty
143 StandardOutput=tty
144 EOF
145 mkdir -p "$initdir"/etc/systemd/system/testsuite.target.wants
146 ln -fs ../testsuite.service "$initdir"/etc/systemd/system/testsuite.target.wants/testsuite.service
147
148 # make the testsuite the default target
149 systemctl --root="$initdir" set-default testsuite.target
150
151 # install basic tools needed
152 inst_multiple sh bash setsid loadkeys setfont \
153 login sulogin gzip sleep echo mount umount
154 inst_multiple modprobe
155
156 # install libnss_files for login
157 inst_libdir_file "libnss_files*"
158
159 # install dbus and pam
160 inst_multiple -o \
161 /etc/dbus-1/** \
162 /etc/pam.d/** \
163 /etc/security/** \
164 /lib64/security/** \
165 /lib/security/**
166
167 # install dbus socket and service file
168 inst_multiple -o \
169 /usr/lib/systemd/system/dbus.socket \
170 /usr/lib/systemd/system/dbus.service \
171 /usr/lib/systemd/system/dbus-broker.service \
172 /usr/lib/systemd/system/dbus-daemon.service
173
174 (
175 echo "FONT=eurlatgr"
176 echo "KEYMAP=us"
177 ) > "$initrd"/etc/vconsole.conf
178
179 # install basic keyboard maps and fonts
180 for i in \
181 /usr/lib/kbd/consolefonts/eurlatgr* \
182 /usr/lib/kbd/keymaps/{legacy/,/}include/* \
183 /usr/lib/kbd/keymaps/{legacy/,/}i386/include/* \
184 /usr/lib/kbd/keymaps/{legacy/,/}i386/qwerty/us.*; do
185 [[ -f $i ]] || continue
186 inst "$i"
187 done
188
189 # some basic terminfo files
190 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
191 [ -f ${_terminfodir}/l/linux ] && break
192 done
193 inst_multiple -o ${_terminfodir}/l/linux
194
195 # softlink mtab
196 ln -fs /proc/self/mounts "$initdir"/etc/mtab
197
198 # install any Execs from the service files
199 grep -Eho '^Exec[^ ]*=[^ ]+' "$initdir"{,/usr}/lib/systemd/system/*.service \
200 | while read -r i || [ -n "$i" ]; do
201 i=${i##Exec*=}
202 i=${i##-}
203 inst_multiple -o "$i"
204 done
205
206 # some helper tools for debugging
207 [[ $DEBUGTOOLS ]] && inst_multiple "$DEBUGTOOLS"
208
209 # install ld.so.conf* and run ldconfig
210 cp -a /etc/ld.so.conf* "$initdir"/etc
211 ldconfig -r "$initdir"
212 ddebug "Strip binaeries"
213 find "$initdir" -perm /0111 -type f -print0 | xargs -0 -r strip --strip-unneeded | ddebug
214
215 hostonly='' instmods fuse
216
217 # copy depmod files
218 inst /lib/modules/"$kernel"/modules.order
219 inst /lib/modules/"$kernel"/modules.builtin
220 # generate module dependencies
221 if [[ -d $initdir/lib/modules/$kernel ]] \
222 && ! depmod -a -b "$initdir" "$kernel"; then
223 dfatal "\"depmod -a $kernel\" failed."
224 exit 1
225 fi
226 # disable some services
227 systemctl --root "$initdir" mask systemd-update-utmp
228 systemctl --root "$initdir" mask systemd-tmpfiles-setup
229 )
230
231 # second, install the files needed to make the root filesystem
232 (
233 # shellcheck disable=SC2030
234 # shellcheck disable=SC2031
235 export initdir=$TESTDIR/overlay
236 # shellcheck disable=SC1090
237 . "$basedir"/dracut-init.sh
238 inst_multiple sfdisk mkfs.btrfs btrfs poweroff cp umount sync dd
239 inst_hook initqueue 01 ./create-root.sh
240 inst_hook initqueue/finished 01 ./finished-false.sh
241 )
242
243 # create an initramfs that will create the target root filesystem.
244 # We do it this way so that we do not risk trashing the host mdraid
245 # devices, volume groups, encrypted partitions, etc.
246 "$basedir"/dracut.sh -l -i "$TESTDIR"/overlay / \
247 -m "bash udev-rules btrfs base rootfs-block fs-lib kernel-modules qemu" \
248 -d "piix ide-gd_mod ata_piix btrfs sd_mod" \
249 --nomdadmconf \
250 --nohardlink \
251 --no-hostonly-cmdline -N \
252 -f "$TESTDIR"/initramfs.makeroot "$KVERSION" || return 1
253 rm -rf -- "$TESTDIR"/overlay
254
255 # Create the blank file to use as a root filesystem
256 dd if=/dev/zero of="$TESTDIR"/root.btrfs bs=1MiB count=160
257 dd if=/dev/zero of="$TESTDIR"/usr.btrfs bs=1MiB count=160
258 dd if=/dev/zero of="$TESTDIR"/marker.img bs=1MiB count=1
259 declare -a disk_args=()
260 # shellcheck disable=SC2034
261 declare -i disk_index=0
262 qemu_add_drive_args disk_index disk_args "$TESTDIR"/marker.img marker
263 qemu_add_drive_args disk_index disk_args "$TESTDIR"/root.btrfs root
264 qemu_add_drive_args disk_index disk_args "$TESTDIR"/usr.btrfs usr
265
266 # Invoke KVM and/or QEMU to actually create the target filesystem.
267 "$testdir"/run-qemu \
268 "${disk_args[@]}" \
269 -append "root=/dev/fakeroot rw rootfstype=btrfs quiet console=ttyS0,115200n81 selinux=0" \
270 -initrd "$TESTDIR"/initramfs.makeroot || return 1
271
272 if ! grep -U --binary-files=binary -F -m 1 -q dracut-root-block-created "$TESTDIR"/marker.img; then
273 echo "Could not create root filesystem"
274 return 1
275 fi
276
277 (
278 # shellcheck disable=SC2031
279 export initdir=$TESTDIR/overlay
280 # shellcheck disable=SC1090
281 . "$basedir"/dracut-init.sh
282 inst_multiple poweroff shutdown dd
283 inst_hook shutdown-emergency 000 ./hard-off.sh
284 inst_hook emergency 000 ./hard-off.sh
285 )
286
287 [ -e /etc/machine-id ] && EXTRA_MACHINE="/etc/machine-id"
288 [ -e /etc/machine-info ] && EXTRA_MACHINE+=" /etc/machine-info"
289
290 "$basedir"/dracut.sh -l -i "$TESTDIR"/overlay / \
291 -a "debug systemd i18n qemu" \
292 ${EXTRA_MACHINE:+-I "$EXTRA_MACHINE"} \
293 -o "dash network plymouth lvm mdraid resume crypt caps dm terminfo usrmount kernel-network-modules rngd" \
294 -d "piix ide-gd_mod ata_piix btrfs sd_mod i6300esb ib700wdt" \
295 --no-hostonly-cmdline -N \
296 -f "$TESTDIR"/initramfs.testing "$KVERSION" || return 1
297
298 rm -rf -- "$TESTDIR"/overlay
299 }
300
301 test_cleanup() {
302 return 0
303 }
304
305 # shellcheck disable=SC1090
306 . "$testdir"/test-functions