]> git.ipfire.org Git - thirdparty/dracut.git/blame - test/TEST-50-MULTINIC/test.sh
fix(dracut-util): print error message with trailing newline
[thirdparty/dracut.git] / test / TEST-50-MULTINIC / test.sh
CommitLineData
a71f1b49 1#!/bin/bash
a3f73298
HH
2
3if [[ $NM ]]; then
4 USE_NETWORK="network-manager"
5 OMIT_NETWORK="network-legacy"
6else
7 USE_NETWORK="network-legacy"
8 OMIT_NETWORK="network-manager"
9fi
10
c864f893 11# shellcheck disable=SC2034
a3f73298 12TEST_DESCRIPTION="root filesystem on NFS with multiple nics with $USE_NETWORK"
a71f1b49
PS
13
14KVERSION=${KVERSION-$(uname -r)}
15
16# Uncomment this to debug failures
8b2afb08 17#DEBUGFAIL="loglevel=7 rd.shell rd.break"
67ab4f77 18#SERIAL="tcp:127.0.0.1:9999"
a71f1b49
PS
19
20run_server() {
21 # Start server first
22 echo "MULTINIC TEST SETUP: Starting DHCP/NFS server"
23
b80ee080
HH
24 declare -a disk_args=()
25 # shellcheck disable=SC2034
26 declare -i disk_index=0
27 qemu_add_drive_args disk_index disk_args "$TESTDIR"/server.img root
28
c864f893 29 "$testdir"/run-qemu \
b80ee080 30 "${disk_args[@]}" \
9288d21b
HH
31 -net socket,listen=127.0.0.1:12350 \
32 -net nic,macaddr=52:54:01:12:34:56,model=e1000 \
c864f893 33 -serial "${SERIAL:-"file:$TESTDIR/server.log"}" \
9288d21b 34 -watchdog i6300esb -watchdog-action poweroff \
b2cf61d9 35 -append "panic=1 oops=panic softlockup_panic=1 systemd.crash_reboot root=LABEL=dracut rootfstype=ext3 rw console=ttyS0,115200n81 selinux=0" \
9288d21b
HH
36 -initrd "$TESTDIR"/initramfs.server \
37 -pidfile "$TESTDIR"/server.pid -daemonize || return 1
ca8f1c1b 38
4bd0ab61 39 chmod 644 -- "$TESTDIR"/server.pid || return 1
a71f1b49
PS
40
41 # Cleanup the terminal if we have one
42 tty -s && stty sane
43
3aae122c 44 if ! [[ $SERIAL ]]; then
9a52c3fd 45 while :; do
3aae122c 46 grep Serving "$TESTDIR"/server.log && break
a3f73298 47 echo "Waiting for the server to startup"
0f62da04 48 tail "$TESTDIR"/server.log
3aae122c
HH
49 sleep 1
50 done
51 else
52 echo Sleeping 10 seconds to give the server a head start
53 sleep 10
54 fi
a71f1b49
PS
55}
56
57client_test() {
58 local test_name="$1"
59 local mac1="$2"
60 local mac2="$3"
61 local mac3="$4"
62 local cmdline="$5"
63 local check="$6"
64
65 echo "CLIENT TEST START: $test_name"
66
b80ee080
HH
67 dd if=/dev/zero of="$TESTDIR"/marker.img bs=1MiB count=1
68 declare -a disk_args=()
69 # shellcheck disable=SC2034
70 declare -i disk_index=0
71 qemu_add_drive_args disk_index disk_args "$TESTDIR"/marker.img marker
a71f1b49 72
8b2afb08
HH
73 if dhclient --help 2>&1 | grep -q -F -- '--timeout' 2> /dev/null; then
74 cmdline="$cmdline rd.net.timeout.dhcp=3"
75 fi
76
b80ee080
HH
77 # Invoke KVM and/or QEMU to actually create the target filesystem.
78 "$testdir"/run-qemu \
79 "${disk_args[@]}" \
9a52c3fd 80 -net socket,connect=127.0.0.1:12350 \
c864f893
HH
81 -net nic,macaddr=52:54:00:12:34:"$mac1",model=e1000 \
82 -net nic,macaddr=52:54:00:12:34:"$mac2",model=e1000 \
83 -net nic,macaddr=52:54:00:12:34:"$mac3",model=e1000 \
9a52c3fd
HH
84 -netdev hubport,id=n1,hubid=1 \
85 -netdev hubport,id=n2,hubid=2 \
86 -device e1000,netdev=n1,mac=52:54:00:12:34:98 \
87 -device e1000,netdev=n2,mac=52:54:00:12:34:99 \
88 -watchdog i6300esb -watchdog-action poweroff \
b2cf61d9 89 -append "quiet panic=1 oops=panic softlockup_panic=1 systemd.crash_reboot rd.shell=0 $cmdline $DEBUGFAIL rd.retry=5 ro console=ttyS0,115200n81 selinux=0 init=/sbin/init rd.debug systemd.log_target=console" \
b80ee080 90 -initrd "$TESTDIR"/initramfs.testing || return 1
9a52c3fd
HH
91
92 {
c864f893
HH
93 read -r OK
94 read -r IFACES
b80ee080 95 } < "$TESTDIR"/marker.img
84bc1929 96
75d758e8 97 if [[ $OK != "OK" ]]; then
d6862983
HH
98 echo "CLIENT TEST END: $test_name [FAILED - BAD EXIT]"
99 return 1
a71f1b49
PS
100 fi
101
84bc1929
HH
102 for i in $check; do
103 if [[ " $IFACES " != *\ $i\ * ]]; then
104 echo "$i not in '$IFACES'"
105 echo "CLIENT TEST END: $test_name [FAILED - BAD IF]"
106 return 1
107 fi
108 done
a71f1b49 109
84bc1929
HH
110 for i in $IFACES; do
111 if [[ " $check " != *\ $i\ * ]]; then
112 echo "$i in '$IFACES', but should not be"
d6862983
HH
113 echo "CLIENT TEST END: $test_name [FAILED - BAD IF]"
114 return 1
115 fi
a71f1b49
PS
116 done
117
118 echo "CLIENT TEST END: $test_name [OK]"
119 return 0
120}
121
a71f1b49
PS
122test_run() {
123 if ! run_server; then
d6862983
HH
124 echo "Failed to start server" 1>&2
125 return 1
a71f1b49 126 fi
24a78b26
HH
127 test_client
128 ret=$?
129 kill_server
130 return $ret
6c980807 131}
a71f1b49 132
6c980807 133test_client() {
a71f1b49
PS
134 # Mac Numbering Scheme
135 # ...:00-02 receive IP adresses all others don't
136 # ...:02 receives a dhcp root-path
137
138 # PXE Style BOOTIF=
e0641277 139 client_test "MULTINIC root=nfs BOOTIF=" \
9a52c3fd
HH
140 00 01 02 \
141 "root=nfs:192.168.50.1:/nfs/client BOOTIF=52-54-00-12-34-00" \
b80ee080 142 "enp0s1" || return 1
a71f1b49 143
b80ee080 144 client_test "MULTINIC root=nfs BOOTIF= ip=enp0s3:dhcp" \
9a52c3fd 145 00 01 02 \
b80ee080
HH
146 "root=nfs:192.168.50.1:/nfs/client BOOTIF=52-54-00-12-34-00 ip=enp0s2:dhcp" \
147 "enp0s1 enp0s2" || return 1
84bc1929 148
a71f1b49
PS
149 # PXE Style BOOTIF= with dhcp root-path
150 client_test "MULTINIC root=dhcp BOOTIF=" \
9a52c3fd
HH
151 00 01 02 \
152 "root=dhcp BOOTIF=52-54-00-12-34-02" \
b80ee080 153 "enp0s3" || return 1
a71f1b49
PS
154
155 # Multinic case, where only one nic works
156 client_test "MULTINIC root=nfs ip=dhcp" \
9a52c3fd
HH
157 FF 00 FE \
158 "root=nfs:192.168.50.1:/nfs/client ip=dhcp" \
b80ee080 159 "enp0s2" || return 1
a71f1b49
PS
160
161 # Require two interfaces
b80ee080 162 client_test "MULTINIC root=nfs ip=enp0s2:dhcp ip=enp0s3:dhcp bootdev=enp0s2" \
9a52c3fd 163 00 01 02 \
b80ee080
HH
164 "root=nfs:192.168.50.1:/nfs/client ip=enp0s2:dhcp ip=enp0s3:dhcp bootdev=enp0s2" \
165 "enp0s2 enp0s3" || return 1
a71f1b49
PS
166
167 # Require three interfaces with dhcp root-path
b80ee080 168 client_test "MULTINIC root=dhcp ip=enp0s1:dhcp ip=enp0s2:dhcp ip=enp0s3:dhcp bootdev=enp0s3" \
9a52c3fd 169 00 01 02 \
b80ee080
HH
170 "root=dhcp ip=enp0s1:dhcp ip=enp0s2:dhcp ip=enp0s3:dhcp bootdev=enp0s3" \
171 "enp0s1 enp0s2 enp0s3" || return 1
8f4c0660 172
24a78b26 173 client_test "MULTINIC bonding" \
9a52c3fd 174 00 01 02 \
b80ee080 175 "root=nfs:192.168.50.1:/nfs/client ip=bond0:dhcp bond=bond0:enp0s1,enp0s2,enp0s3:mode=balance-rr" \
9a52c3fd 176 "bond0" || return 1
24a78b26 177
a3f73298 178 # bridge, where only one interface is actually connected
24a78b26 179 client_test "MULTINIC bridging" \
9a52c3fd 180 00 01 02 \
b80ee080 181 "root=nfs:192.168.50.1:/nfs/client ip=bridge0:dhcp bridge=bridge0:enp0s1,enp0s5,enp0s6" \
9a52c3fd 182 "bridge0" || return 1
8f4c0660 183 return 0
a71f1b49
PS
184}
185
186test_setup() {
8b2afb08
HH
187 export kernel=$KVERSION
188 export srcmods="/lib/modules/$kernel/"
189 rm -rf -- "$TESTDIR"/overlay
3eca0cc8 190 (
c864f893
HH
191 mkdir -p "$TESTDIR"/overlay/source
192 # shellcheck disable=SC2030
0f62da04 193 export initdir=$TESTDIR/overlay/source
c864f893
HH
194 # shellcheck disable=SC1090
195 . "$basedir"/dracut-init.sh
0f62da04 196
d6862983 197 (
c864f893 198 cd "$initdir" || exit
8b2afb08
HH
199 mkdir -p dev sys proc run etc var/run tmp var/lib/{dhcpd,rpcbind}
200 mkdir -p var/lib/nfs/{v4recovery,rpc_pipefs}
201 chmod 777 var/lib/rpcbind var/lib/nfs
d6862983
HH
202 )
203
af119460 204 inst_multiple sh ls shutdown poweroff stty cat ps ln ip \
9a52c3fd
HH
205 dmesg mkdir cp ping exportfs \
206 modprobe rpc.nfsd rpc.mountd showmount tcpdump \
8b2afb08 207 sleep mount chmod rm
96d22bd7 208 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
8b2afb08
HH
209 if [ -f "${_terminfodir}"/l/linux ]; then
210 inst_multiple -o "${_terminfodir}"/l/linux
211 break
212 fi
d6862983 213 done
9a52c3fd
HH
214 type -P portmap > /dev/null && inst_multiple portmap
215 type -P rpcbind > /dev/null && inst_multiple rpcbind
af119460 216 [ -f /etc/netconfig ] && inst_multiple /etc/netconfig
9a52c3fd 217 type -P dhcpd > /dev/null && inst_multiple dhcpd
d6862983 218 [ -x /usr/sbin/dhcpd3 ] && inst /usr/sbin/dhcpd3 /usr/sbin/dhcpd
ec897491 219 instmods nfsd sunrpc ipv6 lockd af_packet
d6862983 220 inst ./server-init.sh /sbin/init
8b2afb08 221 inst_simple /etc/os-release
d6862983
HH
222 inst ./hosts /etc/hosts
223 inst ./exports /etc/exports
224 inst ./dhcpd.conf /etc/dhcpd.conf
8b2afb08
HH
225 inst_multiple -o {,/usr}/etc/nsswitch.conf {,/usr}/etc/rpc \
226 {,/usr}/etc/protocols {,/usr}/etc/services
af119460 227 inst_multiple rpc.idmapd /etc/idmapd.conf
d6862983
HH
228
229 inst_libdir_file 'libnfsidmap_nsswitch.so*'
230 inst_libdir_file 'libnfsidmap/*.so*'
231 inst_libdir_file 'libnfsidmap*.so*'
c5ef4b63 232
8b2afb08
HH
233 _nsslibs=$(
234 cat "$dracutsysrootdir"/{,usr/}etc/nsswitch.conf 2> /dev/null \
235 | sed -e '/^#/d' -e 's/^.*://' -e 's/\[NOTFOUND=return\]//' \
236 | tr -s '[:space:]' '\n' | sort -u | tr -s '[:space:]' '|'
237 )
3eca0cc8
HH
238 _nsslibs=${_nsslibs#|}
239 _nsslibs=${_nsslibs%|}
167a320e 240 inst_libdir_file -n "$_nsslibs" 'libnss_*.so*'
c5ef4b63 241
d6862983
HH
242 inst /etc/passwd /etc/passwd
243 inst /etc/group /etc/group
3eca0cc8 244
8b2afb08 245 cp -a /etc/ld.so.conf* "$initdir"/etc
7ecb36ae 246 ldconfig -r "$initdir"
67ab4f77 247 dracut_kernel_post
3eca0cc8 248 )
a71f1b49
PS
249
250 # Make client root inside server root
a71f1b49 251 (
c864f893
HH
252 # shellcheck disable=SC2030
253 # shellcheck disable=SC2031
0f62da04 254 export initdir=$TESTDIR/overlay/source/nfs/client
c864f893
HH
255 # shellcheck disable=SC1090
256 . "$basedir"/dracut-init.sh
8b2afb08 257
06853123 258 (
c864f893 259 cd "$initdir" || exit
8b2afb08 260 mkdir -p dev sys proc etc run root usr var/lib/nfs/rpc_pipefs
06853123 261 )
8b2afb08
HH
262
263 inst_multiple sh shutdown poweroff stty cat ps ln ip dd \
264 mount dmesg mkdir cp ping grep setsid ls vi less cat sync
96d22bd7 265 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
8b2afb08
HH
266 if [ -f "${_terminfodir}"/l/linux ]; then
267 inst_multiple -o "${_terminfodir}"/l/linux
268 break
269 fi
d6862983 270 done
c864f893
HH
271
272 inst_simple "${basedir}/modules.d/99base/dracut-lib.sh" "/lib/dracut-lib.sh"
273 inst_binary "${basedir}/dracut-util" "/usr/bin/dracut-util"
274 ln -s dracut-util "${initdir}/usr/bin/dracut-getarg"
275 ln -s dracut-util "${initdir}/usr/bin/dracut-getargs"
276
d6862983 277 inst ./client-init.sh /sbin/init
8b2afb08
HH
278 inst_simple /etc/os-release
279 inst_multiple -o {,/usr}/etc/nsswitch.conf
d6862983
HH
280 inst /etc/passwd /etc/passwd
281 inst /etc/group /etc/group
282
d6862983
HH
283 inst_libdir_file 'libnfsidmap_nsswitch.so*'
284 inst_libdir_file 'libnfsidmap/*.so*'
285 inst_libdir_file 'libnfsidmap*.so*'
3eca0cc8 286
8b2afb08
HH
287 _nsslibs=$(
288 cat "$dracutsysrootdir"/{,usr/}etc/nsswitch.conf 2> /dev/null \
289 | sed -e '/^#/d' -e 's/^.*://' -e 's/\[NOTFOUND=return\]//' \
290 | tr -s '[:space:]' '\n' | sort -u | tr -s '[:space:]' '|'
291 )
3eca0cc8
HH
292 _nsslibs=${_nsslibs#|}
293 _nsslibs=${_nsslibs%|}
167a320e 294 inst_libdir_file -n "$_nsslibs" 'libnss_*.so*'
a71f1b49 295
8b2afb08 296 cp -a /etc/ld.so.conf* "$initdir"/etc
4bd0ab61 297 ldconfig -r "$initdir"
3eca0cc8
HH
298 )
299
0f62da04
HH
300 # second, install the files needed to make the root filesystem
301 (
c864f893
HH
302 # shellcheck disable=SC2030
303 # shellcheck disable=SC2031
0f62da04 304 export initdir=$TESTDIR/overlay
c864f893
HH
305 # shellcheck disable=SC1090
306 . "$basedir"/dracut-init.sh
0f62da04
HH
307 inst_multiple sfdisk mkfs.ext3 poweroff cp umount sync dd
308 inst_hook initqueue 01 ./create-root.sh
309 inst_hook initqueue/finished 01 ./finished-false.sh
0f62da04
HH
310 )
311
312 # create an initramfs that will create the target root filesystem.
313 # We do it this way so that we do not risk trashing the host mdraid
314 # devices, volume groups, encrypted partitions, etc.
c864f893 315 "$basedir"/dracut.sh -l -i "$TESTDIR"/overlay / \
9a52c3fd
HH
316 -m "bash udev-rules base rootfs-block fs-lib kernel-modules fs-lib qemu" \
317 -d "piix ide-gd_mod ata_piix ext3 sd_mod" \
318 --nomdadmconf \
319 --no-hostonly-cmdline -N \
c864f893 320 -f "$TESTDIR"/initramfs.makeroot "$KVERSION" || return 1
8b2afb08 321 rm -rf -- "$TESTDIR"/overlay
b80ee080
HH
322
323 dd if=/dev/zero of="$TESTDIR"/server.img bs=1MiB count=120
324 dd if=/dev/zero of="$TESTDIR"/marker.img bs=1MiB count=1
325 declare -a disk_args=()
326 # shellcheck disable=SC2034
327 declare -i disk_index=0
328 qemu_add_drive_args disk_index disk_args "$TESTDIR"/marker.img marker
329 qemu_add_drive_args disk_index disk_args "$TESTDIR"/server.img root
0f62da04
HH
330
331 # Invoke KVM and/or QEMU to actually create the target filesystem.
c864f893 332 "$testdir"/run-qemu \
b80ee080 333 "${disk_args[@]}" \
0f62da04 334 -append "root=/dev/dracut/root rw rootfstype=ext3 quiet console=ttyS0,115200n81 selinux=0" \
c864f893 335 -initrd "$TESTDIR"/initramfs.makeroot || return 1
b80ee080 336 grep -U --binary-files=binary -F -m 1 -q dracut-root-block-created "$TESTDIR"/marker.img || return 1
a71f1b49
PS
337
338 # Make an overlay with needed tools for the test harness
339 (
c864f893 340 # shellcheck disable=SC2031
8b2afb08 341 # shellcheck disable=SC2030
b093aa2d 342 export initdir="$TESTDIR"/overlay
8b2afb08 343 mkdir -p "$TESTDIR"/overlay
c864f893 344 # shellcheck disable=SC1090
777f2db0 345 . "$basedir"/dracut-init.sh
af119460 346 inst_multiple poweroff shutdown
4e882b80 347 inst_hook shutdown-emergency 000 ./hard-off.sh
781f1971 348 inst_hook emergency 000 ./hard-off.sh
8b2afb08
HH
349 inst_simple ./client.link /etc/systemd/network/01-client.link
350
351 inst_binary awk
352 inst_hook pre-pivot 85 "$basedir/modules.d/45ifcfg/write-ifcfg.sh"
a71f1b49 353 )
8b2afb08
HH
354 # Make client's dracut image
355 "$basedir"/dracut.sh -l -i "$TESTDIR"/overlay / \
356 -o "ifcfg plymouth dash ${OMIT_NETWORK}" \
357 -a "debug watchdog ${USE_NETWORK}" \
358 --no-hostonly-cmdline -N \
359 -f "$TESTDIR"/initramfs.testing "$KVERSION" || return 1
a71f1b49 360
8b2afb08
HH
361 (
362 # shellcheck disable=SC2031
363 export initdir="$TESTDIR"/overlay
364 # shellcheck disable=SC1090
365 . "$basedir"/dracut-init.sh
366 rm "$initdir"/etc/systemd/network/01-client.link
367 inst_simple ./server.link /etc/systemd/network/01-server.link
368 inst_hook pre-mount 99 ./wait-if-server.sh
369 )
a71f1b49 370 # Make server's dracut image
8b2afb08
HH
371 "$basedir"/dracut.sh -l -i "$TESTDIR"/overlay / \
372 -m "dash udev-rules base rootfs-block fs-lib debug kernel-modules watchdog qemu network network-legacy" \
a8986425 373 -d "af_packet piix ide-gd_mod ata_piix ext3 sd_mod nfsv2 nfsv3 nfsv4 nfs_acl nfs_layout_nfsv41_files nfsd e1000 i6300esb ib700wdt" \
e3e1f406 374 --no-hostonly-cmdline -N \
b093aa2d 375 -f "$TESTDIR"/initramfs.server "$KVERSION" || return 1
a71f1b49 376
a71f1b49
PS
377}
378
6c980807 379kill_server() {
b093aa2d 380 if [[ -s "$TESTDIR"/server.pid ]]; then
c864f893 381 kill -TERM -- "$(cat "$TESTDIR"/server.pid)"
b093aa2d 382 rm -f -- "$TESTDIR"/server.pid
a71f1b49 383 fi
6c980807
HH
384}
385
386test_cleanup() {
0be1785a 387 kill_server
a71f1b49
PS
388}
389
c864f893 390# shellcheck disable=SC1090
b093aa2d 391. "$testdir"/test-functions