]> git.ipfire.org Git - thirdparty/dracut.git/blob - test/TEST-20-NFS/test.sh
6334e2290acc0206299be6c7a8fda92c8ede1027
[thirdparty/dracut.git] / test / TEST-20-NFS / test.sh
1 #!/bin/bash
2 TEST_DESCRIPTION="root filesystem on NFS"
3
4 KVERSION=${KVERSION-$(uname -r)}
5
6 # Uncomment this to debug failures
7 #DEBUGFAIL="rd.shell"
8 #SERIAL="tcp:127.0.0.1:9999"
9
10 run_server() {
11 # Start server first
12 echo "NFS TEST SETUP: Starting DHCP/NFS server"
13
14 fsck -a $TESTDIR/server.ext3 || return 1
15 $testdir/run-qemu \
16 -drive format=raw,index=0,media=disk,file=$TESTDIR/server.ext3 \
17 -m 512M -smp 2 \
18 -display none \
19 -net socket,listen=127.0.0.1:12320 \
20 -net nic,macaddr=52:54:00:12:34:56,model=e1000 \
21 ${SERIAL:+-serial "$SERIAL"} \
22 ${SERIAL:--serial file:"$TESTDIR"/server.log} \
23 -watchdog i6300esb -watchdog-action poweroff \
24 -no-reboot \
25 -append "panic=1 systemd.crash_reboot rd.debug loglevel=77 root=/dev/sda rootfstype=ext3 rw console=ttyS0,115200n81 selinux=0" \
26 -initrd $TESTDIR/initramfs.server \
27 -pidfile $TESTDIR/server.pid -daemonize || return 1
28 sudo chmod 644 $TESTDIR/server.pid || return 1
29
30 # Cleanup the terminal if we have one
31 tty -s && stty sane
32
33 if ! [[ $SERIAL ]]; then
34 echo "Waiting for the server to startup"
35 while : ; do
36 grep Serving "$TESTDIR"/server.log && break
37 sleep 1
38 done
39 else
40 echo Sleeping 10 seconds to give the server a head start
41 sleep 10
42 fi
43 }
44
45 client_test() {
46 local test_name="$1"
47 local mac=$2
48 local cmdline="$3"
49 local server="$4"
50 local check_opt="$5"
51 local nfsinfo opts found expected
52
53 echo "CLIENT TEST START: $test_name"
54
55 # Need this so kvm-qemu will boot (needs non-/dev/zero local disk)
56 if ! dd if=/dev/zero of=$TESTDIR/client.img bs=1M count=1; then
57 echo "Unable to make client sda image" 1>&2
58 return 1
59 fi
60
61 $testdir/run-qemu \
62 -drive format=raw,index=0,media=disk,file=$TESTDIR/client.img \
63 -m 512M -smp 2 -nographic \
64 -net nic,macaddr=$mac,model=e1000 \
65 -net socket,connect=127.0.0.1:12320 \
66 -watchdog i6300esb -watchdog-action poweroff \
67 -no-reboot \
68 -append "panic=1 systemd.crash_reboot rd.shell=0 $cmdline $DEBUGFAIL rd.debug rd.retry=10 rd.info quiet ro console=ttyS0,115200n81 selinux=0" \
69 -initrd $TESTDIR/initramfs.testing
70
71 if [[ $? -ne 0 ]] || ! grep -F -m 1 -q nfs-OK $TESTDIR/client.img; then
72 echo "CLIENT TEST END: $test_name [FAILED - BAD EXIT]"
73 return 1
74 fi
75
76 # nfsinfo=( server:/path nfs{,4} options )
77 nfsinfo=($(awk '{print $2, $3, $4; exit}' $TESTDIR/client.img))
78
79 if [[ "${nfsinfo[0]%%:*}" != "$server" ]]; then
80 echo "CLIENT TEST INFO: got server: ${nfsinfo[0]%%:*}"
81 echo "CLIENT TEST INFO: expected server: $server"
82 echo "CLIENT TEST END: $test_name [FAILED - WRONG SERVER]"
83 return 1
84 fi
85
86 found=0
87 expected=1
88 if [[ ${check_opt:0:1} = '-' ]]; then
89 expected=0
90 check_opt=${check_opt:1}
91 fi
92
93 opts=${nfsinfo[2]},
94 while [[ $opts ]]; do
95 if [[ ${opts%%,*} = $check_opt ]]; then
96 found=1
97 break
98 fi
99 opts=${opts#*,}
100 done
101
102 if [[ $found -ne $expected ]]; then
103 echo "CLIENT TEST INFO: got options: ${nfsinfo[2]%%:*}"
104 if [[ $expected -eq 0 ]]; then
105 echo "CLIENT TEST INFO: did not expect: $check_opt"
106 echo "CLIENT TEST END: $test_name [FAILED - UNEXPECTED OPTION]"
107 else
108 echo "CLIENT TEST INFO: missing: $check_opt"
109 echo "CLIENT TEST END: $test_name [FAILED - MISSING OPTION]"
110 fi
111 return 1
112 fi
113
114 echo "CLIENT TEST END: $test_name [OK]"
115 return 0
116 }
117
118 test_nfsv3() {
119 # MAC numbering scheme:
120 # NFSv3: last octect starts at 0x00 and works up
121 # NFSv4: last octect starts at 0x80 and works up
122
123 client_test "NFSv3 root=dhcp DHCP path only" 52:54:00:12:34:00 \
124 "root=dhcp" 192.168.50.1 -wsize=4096 || return 1
125
126 if [[ "$(systemctl --version)" != *"systemd 230"* ]] 2>/dev/null; then
127 client_test "NFSv3 Legacy root=/dev/nfs nfsroot=IP:path" 52:54:00:12:34:01 \
128 "root=/dev/nfs nfsroot=192.168.50.1:/nfs/client" 192.168.50.1 -wsize=4096 || return 1
129
130 client_test "NFSv3 Legacy root=/dev/nfs DHCP path only" 52:54:00:12:34:00 \
131 "root=/dev/nfs" 192.168.50.1 -wsize=4096 || return 1
132
133 client_test "NFSv3 Legacy root=/dev/nfs DHCP IP:path" 52:54:00:12:34:01 \
134 "root=/dev/nfs" 192.168.50.2 -wsize=4096 || return 1
135 fi
136
137 client_test "NFSv3 root=dhcp DHCP IP:path" 52:54:00:12:34:01 \
138 "root=dhcp" 192.168.50.2 -wsize=4096 || return 1
139
140 client_test "NFSv3 root=dhcp DHCP proto:IP:path" 52:54:00:12:34:02 \
141 "root=dhcp" 192.168.50.3 -wsize=4096 || return 1
142
143 client_test "NFSv3 root=dhcp DHCP proto:IP:path:options" 52:54:00:12:34:03 \
144 "root=dhcp" 192.168.50.3 wsize=4096 || return 1
145
146 client_test "NFSv3 root=nfs:..." 52:54:00:12:34:04 \
147 "root=nfs:192.168.50.1:/nfs/client" 192.168.50.1 -wsize=4096 || return 1
148
149 client_test "NFSv3 Bridge root=nfs:..." 52:54:00:12:34:04 \
150 "root=nfs:192.168.50.1:/nfs/client bridge net.ifnames=0" 192.168.50.1 -wsize=4096 || return 1
151
152 client_test "NFSv3 Legacy root=IP:path" 52:54:00:12:34:04 \
153 "root=192.168.50.1:/nfs/client" 192.168.50.1 -wsize=4096 || return 1
154
155 # This test must fail: nfsroot= requires root=/dev/nfs
156 client_test "NFSv3 Invalid root=dhcp nfsroot=/nfs/client" 52:54:00:12:34:04 \
157 "root=dhcp nfsroot=/nfs/client failme rd.debug" 192.168.50.1 -wsize=4096 && return 1
158
159 client_test "NFSv3 root=dhcp DHCP path,options" \
160 52:54:00:12:34:05 "root=dhcp" 192.168.50.1 wsize=4096 || return 1
161
162 client_test "NFSv3 Bridge Customized root=dhcp DHCP path,options" \
163 52:54:00:12:34:05 "root=dhcp bridge=foobr0:ens3" 192.168.50.1 wsize=4096 || return 1
164
165 client_test "NFSv3 root=dhcp DHCP IP:path,options" \
166 52:54:00:12:34:06 "root=dhcp" 192.168.50.2 wsize=4096 || return 1
167
168 client_test "NFSv3 root=dhcp DHCP proto:IP:path,options" \
169 52:54:00:12:34:07 "root=dhcp" 192.168.50.3 wsize=4096 || return 1
170
171 return 0
172 }
173
174 test_nfsv4() {
175 # There is a mandatory 90 second recovery when starting the NFSv4
176 # server, so put these later in the list to avoid a pause when doing
177 # switch_root
178
179 client_test "NFSv4 root=dhcp DHCP proto:IP:path" 52:54:00:12:34:82 \
180 "root=dhcp" 192.168.50.3 -wsize=4096 || return 1
181
182 client_test "NFSv4 root=dhcp DHCP proto:IP:path:options" 52:54:00:12:34:83 \
183 "root=dhcp" 192.168.50.3 wsize=4096 || return 1
184
185 client_test "NFSv4 root=nfs4:..." 52:54:00:12:34:84 \
186 "root=nfs4:192.168.50.1:/client" 192.168.50.1 \
187 -wsize=4096 || return 1
188
189 client_test "NFSv4 root=dhcp DHCP proto:IP:path,options" \
190 52:54:00:12:34:87 "root=dhcp" 192.168.50.3 wsize=4096 || return 1
191
192 return 0
193 }
194
195 test_run() {
196 if [[ -s server.pid ]]; then
197 sudo kill -TERM $(cat $TESTDIR/server.pid)
198 rm -f -- $TESTDIR/server.pid
199 fi
200
201 if ! run_server; then
202 echo "Failed to start server" 1>&2
203 return 1
204 fi
205
206 test_nfsv3 && \
207 test_nfsv4
208
209 ret=$?
210
211 if [[ -s $TESTDIR/server.pid ]]; then
212 sudo kill -TERM $(cat $TESTDIR/server.pid)
213 rm -f -- $TESTDIR/server.pid
214 fi
215
216 return $ret
217 }
218
219 test_setup() {
220 # Make server root
221 dd if=/dev/null of=$TESTDIR/server.ext3 bs=1M seek=120
222 mke2fs -j -F $TESTDIR/server.ext3
223 mkdir $TESTDIR/mnt
224 sudo mount -o loop $TESTDIR/server.ext3 $TESTDIR/mnt
225
226
227 export kernel=$KVERSION
228 export srcmods="/lib/modules/$kernel/"
229 # Detect lib paths
230
231 (
232 export initdir=$TESTDIR/mnt
233 . $basedir/dracut-init.sh
234
235 for _f in modules.builtin.bin modules.builtin; do
236 [[ $srcmods/$_f ]] && break
237 done || {
238 dfatal "No modules.builtin.bin and modules.builtin found!"
239 return 1
240 }
241
242 for _f in modules.builtin.bin modules.builtin modules.order; do
243 [[ $srcmods/$_f ]] && inst_simple "$srcmods/$_f" "/lib/modules/$kernel/$_f"
244 done
245
246 inst_multiple sh ls shutdown poweroff stty cat ps ln ip \
247 dmesg mkdir cp ping exportfs \
248 modprobe rpc.nfsd rpc.mountd showmount tcpdump \
249 /etc/services sleep mount chmod rm
250 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
251 [ -f ${_terminfodir}/l/linux ] && break
252 done
253 inst_multiple -o ${_terminfodir}/l/linux
254 type -P portmap >/dev/null && inst_multiple portmap
255 type -P rpcbind >/dev/null && inst_multiple rpcbind
256 [ -f /etc/netconfig ] && inst_multiple /etc/netconfig
257 type -P dhcpd >/dev/null && inst_multiple dhcpd
258 [ -x /usr/sbin/dhcpd3 ] && inst /usr/sbin/dhcpd3 /usr/sbin/dhcpd
259 instmods nfsd sunrpc ipv6 lockd af_packet
260 inst ./server-init.sh /sbin/init
261 inst_simple /etc/os-release
262 inst ./hosts /etc/hosts
263 inst ./exports /etc/exports
264 inst ./dhcpd.conf /etc/dhcpd.conf
265 inst_multiple /etc/nsswitch.conf /etc/rpc /etc/protocols
266 inst_multiple rpc.idmapd /etc/idmapd.conf
267
268 inst_libdir_file 'libnfsidmap_nsswitch.so*'
269 inst_libdir_file 'libnfsidmap/*.so*'
270 inst_libdir_file 'libnfsidmap*.so*'
271
272 _nsslibs=$(sed -e '/^#/d' -e 's/^.*://' -e 's/\[NOTFOUND=return\]//' /etc/nsswitch.conf \
273 | tr -s '[:space:]' '\n' | sort -u | tr -s '[:space:]' '|')
274 _nsslibs=${_nsslibs#|}
275 _nsslibs=${_nsslibs%|}
276
277 inst_libdir_file -n "$_nsslibs" 'libnss_*.so*'
278
279 (
280 cd "$initdir";
281 mkdir -p dev sys proc run etc var/run tmp var/lib/{dhcpd,rpcbind}
282 mkdir -p var/lib/nfs/{v4recovery,rpc_pipefs}
283 chmod 777 var/lib/rpcbind var/lib/nfs
284 )
285 inst /etc/nsswitch.conf /etc/nsswitch.conf
286
287 inst /etc/passwd /etc/passwd
288 inst /etc/group /etc/group
289
290 cp -a /etc/ld.so.conf* $initdir/etc
291 sudo ldconfig -r "$initdir"
292 dracut_kernel_post
293 )
294
295
296 # Make client root inside server root
297 (
298 export initdir=$TESTDIR/mnt/nfs/client
299 . $basedir/dracut-init.sh
300
301 inst_multiple sh shutdown poweroff stty cat ps ln ip \
302 mount dmesg mkdir cp ping grep setsid ls vi /etc/virc less cat
303 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
304 [ -f ${_terminfodir}/l/linux ] && break
305 done
306 inst_multiple -o ${_terminfodir}/l/linux
307 inst ./client-init.sh /sbin/init
308 inst_simple /etc/os-release
309 (
310 cd "$initdir"
311 mkdir -p dev sys proc etc run
312 mkdir -p var/lib/nfs/rpc_pipefs
313 mkdir -p root usr/bin usr/lib usr/lib64 usr/sbin
314 for i in bin sbin lib lib64; do
315 ln -sfnr usr/$i $i
316 done
317 )
318 inst /etc/nsswitch.conf /etc/nsswitch.conf
319 inst /etc/passwd /etc/passwd
320 inst /etc/group /etc/group
321
322 inst_libdir_file 'libnfsidmap_nsswitch.so*'
323 inst_libdir_file 'libnfsidmap/*.so*'
324 inst_libdir_file 'libnfsidmap*.so*'
325
326 _nsslibs=$(sed -e '/^#/d' -e 's/^.*://' -e 's/\[NOTFOUND=return\]//' /etc/nsswitch.conf \
327 | tr -s '[:space:]' '\n' | sort -u | tr -s '[:space:]' '|')
328 _nsslibs=${_nsslibs#|}
329 _nsslibs=${_nsslibs%|}
330
331 inst_libdir_file -n "$_nsslibs" 'libnss_*.so*'
332
333 cp -a /etc/ld.so.conf* $initdir/etc
334 sudo ldconfig -r "$initdir"
335 )
336
337 mkdir -p $TESTDIR/mnt/nfs/nfs3-5
338 mkdir -p $TESTDIR/mnt/nfs/ip/192.168.50.101
339 mkdir -p $TESTDIR/mnt/nfs/tftpboot/nfs4-5
340
341 sudo umount $TESTDIR/mnt
342 rm -fr -- $TESTDIR/mnt
343
344 # Make an overlay with needed tools for the test harness
345 (
346 export initdir=$TESTDIR/overlay
347 . $basedir/dracut-init.sh
348 mkdir $TESTDIR/overlay
349 inst_multiple poweroff shutdown
350 inst_hook shutdown-emergency 000 ./hard-off.sh
351 inst_hook emergency 000 ./hard-off.sh
352 inst_simple ./99-idesymlinks.rules /etc/udev/rules.d/99-idesymlinks.rules
353 inst_simple ./99-default.link /etc/systemd/network/99-default.link
354 )
355
356 # Make server's dracut image
357 $basedir/dracut.sh -l -i $TESTDIR/overlay / \
358 -m "dash udev-rules base rootfs-block fs-lib debug kernel-modules watchdog qemu" \
359 -d "af_packet piix ide-gd_mod ata_piix ext3 sd_mod e1000 i6300esb" \
360 --no-hostonly-cmdline -N \
361 -f $TESTDIR/initramfs.server $KVERSION || return 1
362
363 # Make client's dracut image
364 $basedir/dracut.sh -l -i $TESTDIR/overlay / \
365 -o "plymouth dash" \
366 -a "debug watchdog" \
367 -d "af_packet piix ide-gd_mod ata_piix sd_mod e1000 nfs sunrpc i6300esb" \
368 --no-hostonly-cmdline -N \
369 -f $TESTDIR/initramfs.testing $KVERSION || return 1
370 }
371
372 test_cleanup() {
373 if [[ -s $TESTDIR/server.pid ]]; then
374 sudo kill -TERM $(cat $TESTDIR/server.pid)
375 rm -f -- $TESTDIR/server.pid
376 fi
377 }
378
379 . $testdir/test-functions