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