]> git.ipfire.org Git - thirdparty/dracut.git/blame - test/TEST-40-NBD/test.sh
network: add support for netroot=...
[thirdparty/dracut.git] / test / TEST-40-NBD / test.sh
CommitLineData
9ecbe2e4
DD
1#!/bin/bash
2TEST_DESCRIPTION="root filesystem on NBD"
3
4KVERSION=${KVERSION-$(uname -r)}
5
6# Uncomment this to debug failures
a29f15a5 7#DEBUGFAIL="rdinitdebug rdnetdebug rdbreak"
9ecbe2e4
DD
8
9run_server() {
10 # Start server first
b2c5f5dc 11 echo "NBD TEST SETUP: Starting DHCP/NBD server"
9ecbe2e4
DD
12
13 $testdir/run-qemu -hda server.ext2 -hdb nbd.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
29client_test() {
30 local test_name="$1"
31 local mac=$2
32 local cmdline="$3"
a29f15a5
DD
33 local fstype=$4
34 local fsopt=$5
35 local found opts nbdinfo
36
37 [[ $fstype ]] || fstype=ext3
38 [[ $fsopt ]] || fsopt="errors=continue"
9ecbe2e4
DD
39
40 echo "CLIENT TEST START: $test_name"
41
42 # Clear out the flags for each test
43 if ! dd if=/dev/zero of=flag.img bs=1M count=1; then
44 echo "Unable to make client sda image" 1>&2
45 return 1
46 fi
47
48 $testdir/run-qemu -hda flag.img -m 512M -nographic \
49 -net nic,macaddr=$mac,model=e1000 \
50 -net socket,mcast=230.0.0.1:1234 \
51 -kernel /boot/vmlinuz-$KVERSION \
52 -append "$cmdline $DEBUGFAIL ro quiet console=ttyS0,115200n81" \
53 -initrd initramfs.testing
54
55 if [[ $? -ne 0 ]] || ! grep -m 1 -q nbd-OK flag.img; then
56 echo "CLIENT TEST END: $test_name [FAILED - BAD EXIT]"
57 return 1
58 fi
59
a29f15a5
DD
60 # nbdinfo=( fstype fsoptions )
61 nbdinfo=($(awk '{print $2, $3; exit}' flag.img))
62
63 if [[ "${nbdinfo[0]}" != "$fstype" ]]; then
64 echo "CLIENT TEST END: $test_name [FAILED - WRONG FS TYPE]"
65 return 1
66 fi
67
68 opts=${nbdinfo[1]},
69 while [[ $opts ]]; do
70 if [[ ${opts%%,*} == $fsopt ]]; then
71 found=1
72 break
73 fi
74 opts=${opts#*,}
75 done
76
77 if [[ ! $found ]]; then
78 echo "CLIENT TEST END: $test_name [FAILED - BAD FS OPTS]"
79 return 1
80 fi
81
9ecbe2e4
DD
82 echo "CLIENT TEST END: $test_name [OK]"
83}
84
85test_run() {
86 if ! run_server; then
87 echo "Failed to start server" 1>&2
88 return 1
89 fi
90
a29f15a5
DD
91 # The default is ext3,errors=continue so use that to determine
92 # if our options were parsed and used
93
94 client_test "NBD root=nbd:IP:port" 52:54:00:12:34:00 \
9ecbe2e4
DD
95 "root=nbd:192.168.50.1:2000" || return 1
96
a29f15a5
DD
97 client_test "NBD root=nbd:IP:port:fstype" 52:54:00:12:34:00 \
98 "root=nbd:192.168.50.1:2000:ext2" ext2 || return 1
99
100 client_test "NBD root=nbd:IP:port::fsopts" 52:54:00:12:34:00 \
101 "root=nbd:192.168.50.1:2000::errors=panic" \
102 ext3 errors=panic || return 1
103
104 client_test "NBD root=nbd:IP:port:fstype:fsopts" 52:54:00:12:34:00 \
105 "root=nbd:192.168.50.1:2000:ext2:errors=panic" \
106 ext2 errors=panic || return 1
107
108 # There doesn't seem to be a good way to validate the NBD options, so
109 # just check that we don't screw up the other options
110
111 client_test "NBD root=nbd:IP:port:::NBD opts" 52:54:00:12:34:00 \
112 "root=nbd:192.168.50.1:2000:::bs=2048" || return 1
113
114 client_test "NBD root=nbd:IP:port:fstype::NBD opts" 52:54:00:12:34:00 \
115 "root=nbd:192.168.50.1:2000:ext2::bs=2048" ext2 || return 1
116
117 client_test "NBD root=nbd:IP:port:fstype:fsopts:NBD opts" \
118 52:54:00:12:34:00 \
119 "root=nbd:192.168.50.1:2000:ext2:errors=panic:bs=2048" \
120 ext2 errors=panic || return 1
121
122 # Check legacy parsing
123
9ecbe2e4
DD
124 client_test "NBD root=nbd nbdroot=srv:port" 52:54:00:12:34:00 \
125 "root=nbd nbdroot=192.168.50.1:2000" || return 1
126
127 client_test "NBD root=dhcp nbdroot=srv:port" 52:54:00:12:34:00 \
128 "root=dhcp nbdroot=192.168.50.1:2000" || return 1
129
130 client_test "NBD root=nbd nbdroot=srv,port" 52:54:00:12:34:00 \
131 "root=nbd nbdroot=192.168.50.1,2000" || return 1
132
133 client_test "NBD root=dhcp nbdroot=srv,port" 52:54:00:12:34:00 \
134 "root=dhcp nbdroot=192.168.50.1,2000" || return 1
135
a29f15a5
DD
136 # DHCP root-path parsing
137
9ecbe2e4
DD
138 client_test "NBD root=dhcp DHCP root-path nbd:srv:port" 52:54:00:12:34:01 \
139 "root=dhcp" || return 1
a29f15a5
DD
140
141 client_test "NBD root=dhcp DHCP root-path nbd:srv:port:fstype" \
142 52:54:00:12:34:02 "root=dhcp" ext2 || return 1
143
144 client_test "NBD root=dhcp DHCP root-path nbd:srv:port::fsopts" \
145 52:54:00:12:34:03 "root=dhcp" ext3 errors=panic || return 1
146
147 client_test "NBD root=dhcp DHCP root-path nbd:srv:port:fstype:fsopts" \
148 52:54:00:12:34:04 "root=dhcp" ext2 errors=panic || return 1
aec48753
DD
149
150 # netroot handling
151
152 client_test "NBD netroot=nbd:IP:port" 52:54:00:12:34:00 \
153 "netroot=nbd:192.168.50.1:2000" || return 1
154
155 client_test "NBD netroot=dhcp DHCP root-path nbd:srv:port:fstype:fsopts" \
156 52:54:00:12:34:04 "netroot=dhcp" ext2 errors=panic || return 1
9ecbe2e4
DD
157}
158
159make_client_root() {
160 dd if=/dev/zero of=nbd.ext2 bs=1M count=30
a29f15a5 161 mke2fs -F -j nbd.ext2
9ecbe2e4
DD
162 mkdir mnt
163 sudo mount -o loop nbd.ext2 mnt
164
165 kernel=$KVERSION
166 (
167 initdir=mnt
168 . $basedir/dracut-functions
169 dracut_install sh ls shutdown poweroff stty cat ps ln ip \
a29f15a5 170 /lib/terminfo/l/linux dmesg mkdir cp ping
9ecbe2e4
DD
171 inst ./client-init /sbin/init
172 (
173 cd "$initdir";
174 mkdir -p dev sys proc etc var/run tmp
175 )
176 inst /etc/nsswitch.conf /etc/nsswitch.conf
177 inst /etc/passwd /etc/passwd
178 inst /etc/group /etc/group
179 for i in /lib*/libnss_files**;do
180 inst_library $i
181 done
182
183 ldconfig -n -r "$initdir" /lib* /usr/lib*
184 )
185
186 sudo umount mnt
187 rm -fr mnt
188}
189
190make_server_root() {
191 dd if=/dev/zero of=server.ext2 bs=1M count=30
192 mke2fs -F server.ext2
193 mkdir mnt
194 sudo mount -o loop server.ext2 mnt
195
196 kernel=$KVERSION
197 (
198 initdir=mnt
199 . $basedir/dracut-functions
200 dracut_install sh ls shutdown poweroff stty cat ps ln ip \
201 /lib/terminfo/l/linux dmesg mkdir cp ping grep dhcpd \
202 sleep nbd-server
203 inst ./server-init /sbin/init
204 inst ./hosts /etc/hosts
205 inst ./dhcpd.conf /etc/dhcpd.conf
206 (
207 cd "$initdir";
208 mkdir -p dev sys proc etc var/run var/lib/dhcpd tmp
209 )
210 inst /etc/nsswitch.conf /etc/nsswitch.conf
211 inst /etc/passwd /etc/passwd
212 inst /etc/group /etc/group
213 for i in /lib*/libnss_files**;do
214 inst_library $i
215 done
216
217 ldconfig -n -r "$initdir" /lib* /usr/lib*
218 )
219
220 sudo umount mnt
221 rm -fr mnt
222}
223
224test_setup() {
225 make_client_root || return 1
226 make_server_root || return 1
227
228 # Make the test image
229 (
230 initdir=overlay
231 . $basedir/dracut-functions
232 dracut_install poweroff shutdown
233 inst_simple ./hard-off.sh /emergency/01hard-off.sh
234 )
235
236 sudo $basedir/dracut -l -i overlay / \
237 -m "dash crypt lvm mdraid udev-rules rootfs-block base debug" \
238 -d "ata_piix ext2 sd_mod e1000" \
239 -f initramfs.server $KVERSION || return 1
240
241 sudo $basedir/dracut -l -i overlay / \
242 -m "dash crypt lvm mdraid udev-rules base rootfs-block nbd debug" \
a29f15a5 243 -d "ata_piix ext2 ext3 sd_mod e1000" \
9ecbe2e4
DD
244 -f initramfs.testing $KVERSION || return 1
245}
246
247test_cleanup() {
248 if [[ -s server.pid ]]; then
249 sudo kill -TERM $(cat server.pid)
250 rm -f server.pid
251 fi
252 rm -fr overlay mnt
253 rm -f flag.img server.ext2 nbd.ext2 initramfs.server initramfs.testing
254}
255
256. $testdir/test-functions