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