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