Move all hooks to "$hookdir"
[dracut.git] / test / TEST-20-NFS / test.sh
blob6969ddc7fae4ec8f8203e14ec3819e0cd3d4b66d
1 #!/bin/bash
2 TEST_DESCRIPTION="root filesystem on NFS"
4 KVERSION=${KVERSION-$(uname -r)}
6 # Uncomment this to debug failures
7 #DEBUGFAIL="rd.shell"
8 #SERIAL="-serial udp:127.0.0.1:9999"
9 SERIAL="null"
11 run_server() {
12 # Start server first
13 echo "NFS TEST SETUP: Starting DHCP/NFS server"
15 $testdir/run-qemu -hda server.ext2 -m 256M -nographic \
16 -net nic,macaddr=52:54:00:12:34:56,model=e1000 \
17 -net socket,listen=127.0.0.1:12345 \
18 -serial $SERIAL \
19 -kernel /boot/vmlinuz-$KVERSION \
20 -append "root=/dev/sda rw quiet console=ttyS0,115200n81 selinux=0" \
21 -initrd initramfs.server -pidfile server.pid -daemonize || return 1
22 sudo chmod 644 server.pid || return 1
24 # Cleanup the terminal if we have one
25 tty -s && stty sane
27 echo Sleeping 10 seconds to give the server a head start
28 sleep 10
31 client_test() {
32 local test_name="$1"
33 local mac=$2
34 local cmdline="$3"
35 local server="$4"
36 local check_opt="$5"
37 local nfsinfo opts found expected
39 echo "CLIENT TEST START: $test_name"
41 # Need this so kvm-qemu will boot (needs non-/dev/zero local disk)
42 if ! dd if=/dev/zero of=client.img bs=1M count=1; then
43 echo "Unable to make client sda image" 1>&2
44 return 1
47 $testdir/run-qemu -hda client.img -m 256M -nographic \
48 -net nic,macaddr=$mac,model=e1000 \
49 -net socket,connect=127.0.0.1:12345 \
50 -kernel /boot/vmlinuz-$KVERSION \
51 -append "$cmdline $DEBUGFAIL rd.debug rd.retry=10 rd.info quiet ro console=ttyS0,115200n81 selinux=0" \
52 -initrd initramfs.testing
54 if [[ $? -ne 0 ]] || ! grep -m 1 -q nfs-OK client.img; then
55 echo "CLIENT TEST END: $test_name [FAILED - BAD EXIT]"
56 return 1
59 # nfsinfo=( server:/path nfs{,4} options )
60 nfsinfo=($(awk '{print $2, $3, $4; exit}' client.img))
62 if [[ "${nfsinfo[0]%%:*}" != "$server" ]]; then
63 echo "CLIENT TEST INFO: got server: ${nfsinfo[0]%%:*}"
64 echo "CLIENT TEST INFO: expected server: $server"
65 echo "CLIENT TEST END: $test_name [FAILED - WRONG SERVER]"
66 return 1
69 found=0
70 expected=1
71 if [[ ${check_opt:0:1} = '-' ]]; then
72 expected=0
73 check_opt=${check_opt:1}
76 opts=${nfsinfo[2]},
77 while [[ $opts ]]; do
78 if [[ ${opts%%,*} = $check_opt ]]; then
79 found=1
80 break
82 opts=${opts#*,}
83 done
85 if [[ $found -ne $expected ]]; then
86 echo "CLIENT TEST INFO: got options: ${nfsinfo[2]%%:*}"
87 if [[ $expected -eq 0 ]]; then
88 echo "CLIENT TEST INFO: did not expect: $check_opt"
89 echo "CLIENT TEST END: $test_name [FAILED - UNEXPECTED OPTION]"
90 else
91 echo "CLIENT TEST INFO: missing: $check_opt"
92 echo "CLIENT TEST END: $test_name [FAILED - MISSING OPTION]"
94 return 1
97 echo "CLIENT TEST END: $test_name [OK]"
98 return 0
101 test_nfsv3() {
102 # MAC numbering scheme:
103 # NFSv3: last octect starts at 0x00 and works up
104 # NFSv4: last octect starts at 0x80 and works up
106 client_test "NFSv3 root=dhcp DHCP path only" 52:54:00:12:34:00 \
107 "root=dhcp" 192.168.50.1 -wsize=4096 || return 1
109 client_test "NFSv3 Legacy root=/dev/nfs nfsroot=IP:path" 52:54:00:12:34:01 \
110 "root=/dev/nfs nfsroot=192.168.50.1:/nfs/client" 192.168.50.1 -wsize=4096 || return 1
112 client_test "NFSv3 Legacy root=/dev/nfs DHCP path only" 52:54:00:12:34:00 \
113 "root=/dev/nfs" 192.168.50.1 -wsize=4096 || return 1
115 client_test "NFSv3 Legacy root=/dev/nfs DHCP IP:path" 52:54:00:12:34:01 \
116 "root=/dev/nfs" 192.168.50.2 -wsize=4096 || return 1
118 client_test "NFSv3 root=dhcp DHCP IP:path" 52:54:00:12:34:01 \
119 "root=dhcp" 192.168.50.2 -wsize=4096 || return 1
121 client_test "NFSv3 root=dhcp DHCP proto:IP:path" 52:54:00:12:34:02 \
122 "root=dhcp" 192.168.50.3 -wsize=4096 || return 1
124 client_test "NFSv3 root=dhcp DHCP proto:IP:path:options" 52:54:00:12:34:03 \
125 "root=dhcp" 192.168.50.3 wsize=4096 || return 1
127 client_test "NFSv3 root=nfs:..." 52:54:00:12:34:04 \
128 "root=nfs:192.168.50.1:/nfs/client" 192.168.50.1 -wsize=4096 || return 1
130 client_test "NFSv3 Bridge root=nfs:..." 52:54:00:12:34:04 \
131 "root=nfs:192.168.50.1:/nfs/client bridge" 192.168.50.1 -wsize=4096 || return 1
133 client_test "NFSv3 Legacy root=IP:path" 52:54:00:12:34:04 \
134 "root=192.168.50.1:/nfs/client" 192.168.50.1 -wsize=4096 || return 1
136 # This test must fail: nfsroot= requires root=/dev/nfs
137 client_test "NFSv3 Invalid root=dhcp nfsroot=/nfs/client" 52:54:00:12:34:04 \
138 "root=dhcp nfsroot=/nfs/client failme" 192.168.50.1 -wsize=4096 && return 1
140 client_test "NFSv3 root=dhcp DHCP path,options" \
141 52:54:00:12:34:05 "root=dhcp" 192.168.50.1 wsize=4096 || return 1
143 client_test "NFSv3 Bridge Customized root=dhcp DHCP path,options" \
144 52:54:00:12:34:05 "root=dhcp bridge=foobr0:eth0" 192.168.50.1 wsize=4096 || return 1
146 client_test "NFSv3 root=dhcp DHCP IP:path,options" \
147 52:54:00:12:34:06 "root=dhcp" 192.168.50.2 wsize=4096 || return 1
149 client_test "NFSv3 root=dhcp DHCP proto:IP:path,options" \
150 52:54:00:12:34:07 "root=dhcp" 192.168.50.3 wsize=4096 || return 1
153 test_nfsv4() {
154 # There is a mandatory 90 second recovery when starting the NFSv4
155 # server, so put these later in the list to avoid a pause when doing
156 # switch_root
158 client_test "NFSv4 root=dhcp DHCP proto:IP:path" 52:54:00:12:34:82 \
159 "root=dhcp" 192.168.50.3 -wsize=4096 || return 1
161 client_test "NFSv4 root=dhcp DHCP proto:IP:path:options" 52:54:00:12:34:83 \
162 "root=dhcp" 192.168.50.3 wsize=4096 || return 1
164 client_test "NFSv4 root=nfs4:..." 52:54:00:12:34:84 \
165 "root=nfs4:192.168.50.1:/client" 192.168.50.1 \
166 -wsize=4096 || return 1
168 client_test "NFSv4 root=dhcp DHCP proto:IP:path,options" \
169 52:54:00:12:34:87 "root=dhcp" 192.168.50.3 wsize=4096 || return 1
172 test_run() {
173 if [[ -s server.pid ]]; then
174 sudo kill -TERM $(cat server.pid)
175 rm -f server.pid
178 if ! run_server; then
179 echo "Failed to start server" 1>&2
180 return 1
183 test_nfsv3 && \
184 test_nfsv4
186 ret=$?
188 if [[ -s server.pid ]]; then
189 sudo kill -TERM $(cat server.pid)
190 rm -f server.pid
193 return $ret
196 test_setup() {
197 # Make server root
198 dd if=/dev/zero of=server.ext2 bs=1M count=60
199 mke2fs -F server.ext2
200 mkdir mnt
201 sudo mount -o loop server.ext2 mnt
203 kernel=$KVERSION
205 initdir=mnt
206 . $basedir/dracut-functions
207 dracut_install sh ls shutdown poweroff stty cat ps ln ip \
208 /lib/terminfo/l/linux dmesg mkdir cp ping exportfs \
209 modprobe rpc.nfsd rpc.mountd showmount tcpdump \
210 /etc/services sleep mount chmod
211 type -P portmap >/dev/null && dracut_install portmap
212 type -P rpcbind >/dev/null && dracut_install rpcbind
213 [ -f /etc/netconfig ] && dracut_install /etc/netconfig
214 type -P dhcpd >/dev/null && dracut_install dhcpd
215 [ -x /usr/sbin/dhcpd3 ] && inst /usr/sbin/dhcpd3 /usr/sbin/dhcpd
216 instmods nfsd sunrpc ipv6
217 inst ./server-init /sbin/init
218 inst ./hosts /etc/hosts
219 inst ./exports /etc/exports
220 inst ./dhcpd.conf /etc/dhcpd.conf
221 dracut_install /etc/nsswitch.conf /etc/rpc /etc/protocols
222 dracut_install rpc.idmapd /etc/idmapd.conf
223 if ldd $(type -P rpc.idmapd) |grep -q lib64; then
224 LIBDIR="/lib64"
225 else
226 LIBDIR="/lib"
229 dracut_install $(ls {/usr,}$LIBDIR/libnfsidmap*.so* 2>/dev/null )
230 dracut_install $(ls {/usr,}$LIBDIR/libnss*.so 2>/dev/null)
232 cd "$initdir";
233 mkdir -p dev sys proc etc var/run tmp var/lib/{dhcpd,rpcbind}
234 mkdir -p var/lib/nfs/{v4recovery,rpc_pipefs}
235 chmod 777 var/lib/rpcbind var/lib/nfs
237 inst /etc/nsswitch.conf /etc/nsswitch.conf
238 inst /etc/passwd /etc/passwd
239 inst /etc/group /etc/group
240 for i in /lib*/libnss_files**;do
241 inst_library $i
242 done
244 /sbin/depmod -a -b "$initdir" $kernel
245 cp -a /etc/ld.so.conf* $initdir/etc
246 sudo ldconfig -r "$initdir"
249 # Make client root inside server root
250 initdir=mnt/nfs/client
251 mkdir -p $initdir
254 . $basedir/dracut-functions
255 dracut_install sh shutdown poweroff stty cat ps ln ip \
256 /lib/terminfo/l/linux mount dmesg mkdir \
257 cp ping grep
258 inst ./client-init /sbin/init
260 cd "$initdir"
261 mkdir -p dev sys proc etc
262 mkdir -p var/lib/nfs/rpc_pipefs
264 inst /etc/nsswitch.conf /etc/nsswitch.conf
265 inst /etc/passwd /etc/passwd
266 inst /etc/group /etc/group
267 for i in /lib*/libnss_files*;do
268 inst_library $i
269 done
271 cp -a /etc/ld.so.conf* $initdir/etc
272 sudo ldconfig -r "$initdir"
275 mkdir -p mnt/nfs/nfs3-5
276 mkdir -p mnt/nfs/ip/192.168.50.101
277 mkdir -p mnt/nfs/tftpboot/nfs4-5
279 sudo umount mnt
280 rm -fr mnt
282 # Make an overlay with needed tools for the test harness
284 initdir=overlay
285 mkdir overlay
286 . $basedir/dracut-functions
287 dracut_install poweroff shutdown
288 inst_hook emergency 000 ./hard-off.sh
289 inst_simple ./99-idesymlinks.rules /etc/udev/rules.d/99-idesymlinks.rules
292 # Make server's dracut image
293 $basedir/dracut -l -i overlay / \
294 -m "dash udev-rules base rootfs-block debug kernel-modules" \
295 -d "piix ide-gd_mod ata_piix ext2 sd_mod e1000" \
296 -f initramfs.server $KVERSION || return 1
298 # Make client's dracut image
299 $basedir/dracut -l -i overlay / \
300 -o "plymouth" \
301 -a "debug" \
302 -d "piix ide-gd_mod ata_piix sd_mod e1000 nfs sunrpc" \
303 -f initramfs.testing $KVERSION || return 1
306 test_cleanup() {
307 if [[ -s server.pid ]]; then
308 sudo kill -TERM $(cat server.pid)
309 rm -f server.pid
311 rm -rf mnt overlay
312 rm -f server.ext2 client.img initramfs.server initramfs.testing
315 . $testdir/test-functions