]> git.ipfire.org Git - thirdparty/dracut.git/blame - test/TEST-40-NBD/test.sh
TEST-12-RAID-DEG: improve test case
[thirdparty/dracut.git] / test / TEST-40-NBD / test.sh
CommitLineData
9ecbe2e4 1#!/bin/bash
bcf94bba
HH
2# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3# ex: ts=8 sw=4 sts=4 et filetype=sh
9ecbe2e4
DD
4TEST_DESCRIPTION="root filesystem on NBD"
5
6KVERSION=${KVERSION-$(uname -r)}
7
8# Uncomment this to debug failures
bcf94bba 9#DEBUGFAIL="rd.shell rd.retry=10"
51b28ba9
HH
10#SERIAL="udp:127.0.0.1:9999"
11SERIAL="null"
9ecbe2e4
DD
12
13run_server() {
14 # Start server first
b2c5f5dc 15 echo "NBD TEST SETUP: Starting DHCP/NBD server"
9ecbe2e4 16
0be1785a
HH
17 $testdir/run-qemu \
18 -hda $TESTDIR/server.ext2 \
19 -hdb $TESTDIR/nbd.ext2 \
20 -hdc $TESTDIR/encrypted.ext2 \
5831685c 21 -m 256M -nographic \
9ecbe2e4 22 -net nic,macaddr=52:54:00:12:34:56,model=e1000 \
0be1785a 23 -net socket,listen=127.0.0.1:12340 \
51b28ba9 24 -serial $SERIAL \
9ecbe2e4 25 -kernel /boot/vmlinuz-$KVERSION \
8eb16b08 26 -append "root=/dev/sda rw quiet console=ttyS0,115200n81 selinux=0" \
0be1785a
HH
27 -initrd $TESTDIR/initramfs.server -pidfile $TESTDIR/server.pid -daemonize || return 1
28 sudo chmod 644 $TESTDIR/server.pid || return 1
9ecbe2e4
DD
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
37client_test() {
38 local test_name="$1"
39 local mac=$2
40 local cmdline="$3"
a29f15a5
DD
41 local fstype=$4
42 local fsopt=$5
43 local found opts nbdinfo
44
45 [[ $fstype ]] || fstype=ext3
75e8f476 46 [[ $fsopt ]] || fsopt="ro"
9ecbe2e4
DD
47
48 echo "CLIENT TEST START: $test_name"
49
50 # Clear out the flags for each test
0be1785a 51 if ! dd if=/dev/zero of=$TESTDIR/flag.img bs=1M count=1; then
9ecbe2e4
DD
52 echo "Unable to make client sda image" 1>&2
53 return 1
54 fi
55
0be1785a
HH
56 $testdir/run-qemu \
57 -hda $TESTDIR/flag.img \
58 -m 256M -nographic \
9ecbe2e4 59 -net nic,macaddr=$mac,model=e1000 \
0be1785a 60 -net socket,connect=127.0.0.1:12340 \
9ecbe2e4 61 -kernel /boot/vmlinuz-$KVERSION \
fa7ada31 62 -append "$cmdline $DEBUGFAIL rd.debug rd.info ro quiet console=ttyS0,115200n81 selinux=0" \
0be1785a 63 -initrd $TESTDIR/initramfs.testing
9ecbe2e4 64
0be1785a 65 if [[ $? -ne 0 ]] || ! grep -m 1 -q nbd-OK $TESTDIR/flag.img; then
9ecbe2e4
DD
66 echo "CLIENT TEST END: $test_name [FAILED - BAD EXIT]"
67 return 1
68 fi
69
a29f15a5 70 # nbdinfo=( fstype fsoptions )
0be1785a 71 nbdinfo=($(awk '{print $2, $3; exit}' $TESTDIR/flag.img))
a29f15a5
DD
72
73 if [[ "${nbdinfo[0]}" != "$fstype" ]]; then
74 echo "CLIENT TEST END: $test_name [FAILED - WRONG FS TYPE]"
75 return 1
76 fi
77
78 opts=${nbdinfo[1]},
79 while [[ $opts ]]; do
9f786a9d 80 if [[ ${opts%%,*} = $fsopt ]]; then
a29f15a5
DD
81 found=1
82 break
83 fi
84 opts=${opts#*,}
85 done
86
87 if [[ ! $found ]]; then
88 echo "CLIENT TEST END: $test_name [FAILED - BAD FS OPTS]"
89 return 1
90 fi
91
9ecbe2e4
DD
92 echo "CLIENT TEST END: $test_name [OK]"
93}
94
95test_run() {
ebcfda6c 96 modinfo nbd &>/dev/null || { echo "Kernel does not support nbd"; exit 1; }
9ecbe2e4
DD
97 if ! run_server; then
98 echo "Failed to start server" 1>&2
99 return 1
100 fi
97add1b3
HH
101 client_run || { kill_server; return 1; }
102}
103
104client_run() {
9ecbe2e4 105
a29f15a5
DD
106 # The default is ext3,errors=continue so use that to determine
107 # if our options were parsed and used
bcf94bba 108 client_test "NBD root=nbd:IP:port" 52:54:00:12:34:00 \
50e7ff76 109 "root=nbd:192.168.50.1:2000" || return 1
9ecbe2e4 110
bcf94bba 111 client_test "NBD root=nbd:IP:port:fstype" 52:54:00:12:34:00 \
50e7ff76 112 "root=nbd:192.168.50.1:2000:ext2" ext2 || return 1
a29f15a5 113
bcf94bba 114 client_test "NBD root=nbd:IP:port::fsopts" 52:54:00:12:34:00 \
50e7ff76
PS
115 "root=nbd:192.168.50.1:2000::errors=panic" \
116 ext3 errors=panic || return 1
a29f15a5 117
bcf94bba 118 client_test "NBD root=nbd:IP:port:fstype:fsopts" 52:54:00:12:34:00 \
50e7ff76
PS
119 "root=nbd:192.168.50.1:2000:ext2:errors=panic" \
120 ext2 errors=panic || return 1
a29f15a5 121
bcf94bba 122 client_test "NBD Bridge root=nbd:IP:port:fstype:fsopts" 52:54:00:12:34:00 \
beb097d9
WT
123 "root=nbd:192.168.50.1:2000:ext2:errors=panic bridge" \
124 ext2 errors=panic || return 1
125
50e7ff76
PS
126 # There doesn't seem to be a good way to validate the NBD options, so
127 # just check that we don't screw up the other options
a29f15a5 128
bcf94bba 129 client_test "NBD root=nbd:IP:port:::NBD opts" 52:54:00:12:34:00 \
50e7ff76 130 "root=nbd:192.168.50.1:2000:::bs=2048" || return 1
a29f15a5 131
bcf94bba 132 client_test "NBD root=nbd:IP:port:fstype::NBD opts" 52:54:00:12:34:00 \
50e7ff76 133 "root=nbd:192.168.50.1:2000:ext2::bs=2048" ext2 || return 1
a29f15a5 134
bcf94bba 135 client_test "NBD root=nbd:IP:port:fstype:fsopts:NBD opts" \
50e7ff76
PS
136 52:54:00:12:34:00 \
137 "root=nbd:192.168.50.1:2000:ext2:errors=panic:bs=2048" \
138 ext2 errors=panic || return 1
a29f15a5 139
a29f15a5
DD
140 # DHCP root-path parsing
141
9ecbe2e4
DD
142 client_test "NBD root=dhcp DHCP root-path nbd:srv:port" 52:54:00:12:34:01 \
143 "root=dhcp" || return 1
a29f15a5 144
beb097d9
WT
145 client_test "NBD Bridge root=dhcp DHCP root-path nbd:srv:port" 52:54:00:12:34:01 \
146 "root=dhcp bridge" || return 1
147
a29f15a5
DD
148 client_test "NBD root=dhcp DHCP root-path nbd:srv:port:fstype" \
149 52:54:00:12:34:02 "root=dhcp" ext2 || return 1
150
151 client_test "NBD root=dhcp DHCP root-path nbd:srv:port::fsopts" \
152 52:54:00:12:34:03 "root=dhcp" ext3 errors=panic || return 1
153
154 client_test "NBD root=dhcp DHCP root-path nbd:srv:port:fstype:fsopts" \
155 52:54:00:12:34:04 "root=dhcp" ext2 errors=panic || return 1
aec48753
DD
156
157 # netroot handling
158
159 client_test "NBD netroot=nbd:IP:port" 52:54:00:12:34:00 \
160 "netroot=nbd:192.168.50.1:2000" || return 1
161
162 client_test "NBD netroot=dhcp DHCP root-path nbd:srv:port:fstype:fsopts" \
163 52:54:00:12:34:04 "netroot=dhcp" ext2 errors=panic || return 1
8bd5873f
DD
164
165 # Encrypted root handling via LVM/LUKS over NBD
166
167 client_test "NBD root=/dev/dracut/root netroot=nbd:IP:port" \
168 52:54:00:12:34:00 \
169 "root=/dev/dracut/root netroot=nbd:192.168.50.1:2001" || return 1
170
171 # XXX This should be ext2,errors=panic but that doesn't currently
172 # XXX work when you have a real root= line in addition to netroot=
173 # XXX How we should work here needs clarification
174 client_test "NBD root=/dev/dracut/root netroot=dhcp (w/ fstype and opts)" \
175 52:54:00:12:34:05 \
176 "root=/dev/dracut/root netroot=dhcp" || return 1
9ca74ffe
HH
177
178 if [[ -s server.pid ]]; then
0be1785a
HH
179 sudo kill -TERM $(cat $TESTDIR/server.pid)
180 rm -f $TESTDIR/server.pid
9ca74ffe
HH
181 fi
182
8bd5873f
DD
183}
184
185make_encrypted_root() {
186 # Create the blank file to use as a root filesystem
0be1785a
HH
187 dd if=/dev/null of=$TESTDIR/encrypted.ext2 bs=1M seek=20
188 dd if=/dev/null of=$TESTDIR/flag.img bs=1M seek=1
8bd5873f
DD
189
190 kernel=$KVERSION
191 # Create what will eventually be our root filesystem onto an overlay
192 (
0be1785a 193 initdir=$TESTDIR/overlay/source
8bd5873f
DD
194 . $basedir/dracut-functions
195 dracut_install sh df free ls shutdown poweroff stty cat ps ln ip \
96d22bd7
HH
196 mount dmesg mkdir cp ping
197 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
198 [ -f ${_terminfodir}/l/linux ] && break
199 done
200 dracut_install -o ${_terminfodir}/l/linux
8bd5873f
DD
201 inst ./client-init /sbin/init
202 find_binary plymouth >/dev/null && dracut_install plymouth
203 (cd "$initdir"; mkdir -p dev sys proc etc var/run tmp )
204 )
205
206 # second, install the files needed to make the root filesystem
207 (
0be1785a 208 initdir=$TESTDIR/overlay
8bd5873f 209 . $basedir/dracut-functions
75e8f476 210 dracut_install mke2fs poweroff cp umount tune2fs
0b53ca70 211 inst_hook initqueue 01 ./create-root.sh
778d2ba2 212 inst_simple ./99-idesymlinks.rules /etc/udev/rules.d/99-idesymlinks.rules
8bd5873f
DD
213 )
214
215 # create an initramfs that will create the target root filesystem.
216 # We do it this way so that we do not risk trashing the host mdraid
217 # devices, volume groups, encrypted partitions, etc.
0be1785a 218 $basedir/dracut -l -i $TESTDIR/overlay / \
e2dbd86f 219 -m "dash crypt lvm mdraid udev-rules base rootfs-block kernel-modules" \
778d2ba2 220 -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod" \
0be1785a
HH
221 -f $TESTDIR/initramfs.makeroot $KVERSION || return 1
222 rm -rf $TESTDIR/overlay
8bd5873f
DD
223
224 # Invoke KVM and/or QEMU to actually create the target filesystem.
0be1785a
HH
225 $testdir/run-qemu \
226 -hda $TESTDIR/flag.img \
227 -hdb $TESTDIR/encrypted.ext2 \
228 -m 256M \
8bd5873f
DD
229 -nographic -net none \
230 -kernel "/boot/vmlinuz-$kernel" \
8eb16b08 231 -append "root=/dev/dracut/root rw quiet console=ttyS0,115200n81 selinux=0" \
0be1785a
HH
232 -initrd $TESTDIR/initramfs.makeroot || return 1
233 grep -m 1 -q dracut-root-block-created $TESTDIR/flag.img || return 1
9ecbe2e4
DD
234}
235
236make_client_root() {
0be1785a
HH
237 dd if=/dev/null of=$TESTDIR/nbd.ext2 bs=1M seek=30
238 mke2fs -F -j $TESTDIR/nbd.ext2
239 mkdir $TESTDIR/mnt
240 sudo mount -o loop $TESTDIR/nbd.ext2 $TESTDIR/mnt
9ecbe2e4
DD
241
242 kernel=$KVERSION
243 (
0be1785a 244 initdir=$TESTDIR/mnt
9ecbe2e4
DD
245 . $basedir/dracut-functions
246 dracut_install sh ls shutdown poweroff stty cat ps ln ip \
96d22bd7
HH
247 dmesg mkdir cp ping
248 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
249 [ -f ${_terminfodir}/l/linux ] && break
250 done
251 dracut_install -o ${_terminfodir}/l/linux
9ecbe2e4
DD
252 inst ./client-init /sbin/init
253 (
254 cd "$initdir";
255 mkdir -p dev sys proc etc var/run tmp
256 )
257 inst /etc/nsswitch.conf /etc/nsswitch.conf
258 inst /etc/passwd /etc/passwd
259 inst /etc/group /etc/group
260 for i in /lib*/libnss_files**;do
261 inst_library $i
262 done
263
8a080127
PS
264 cp -a /etc/ld.so.conf* $initdir/etc
265 sudo ldconfig -r "$initdir"
9ecbe2e4
DD
266 )
267
0be1785a
HH
268 sudo umount $TESTDIR/mnt
269 rm -fr $TESTDIR/mnt
9ecbe2e4
DD
270}
271
272make_server_root() {
0be1785a
HH
273 dd if=/dev/null of=$TESTDIR/server.ext2 bs=1M seek=30
274 mke2fs -F $TESTDIR/server.ext2
275 mkdir $TESTDIR/mnt
276 sudo mount -o loop $TESTDIR/server.ext2 $TESTDIR/mnt
9ecbe2e4
DD
277
278 kernel=$KVERSION
279 (
0be1785a 280 initdir=$TESTDIR/mnt
9ecbe2e4
DD
281 . $basedir/dracut-functions
282 dracut_install sh ls shutdown poweroff stty cat ps ln ip \
96d22bd7 283 dmesg mkdir cp ping grep \
45630db1 284 sleep nbd-server chmod
96d22bd7
HH
285 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
286 [ -f ${_terminfodir}/l/linux ] && break
287 done
288 dracut_install -o ${_terminfodir}/l/linux
f3af7bd6 289 type -P dhcpd >/dev/null && dracut_install dhcpd
45630db1 290 [ -x /usr/sbin/dhcpd3 ] && inst /usr/sbin/dhcpd3 /usr/sbin/dhcpd
9ecbe2e4
DD
291 inst ./server-init /sbin/init
292 inst ./hosts /etc/hosts
293 inst ./dhcpd.conf /etc/dhcpd.conf
294 (
295 cd "$initdir";
296 mkdir -p dev sys proc etc var/run var/lib/dhcpd tmp
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
8a080127
PS
305 cp -a /etc/ld.so.conf* $initdir/etc
306 sudo ldconfig -r "$initdir"
9ecbe2e4
DD
307 )
308
0be1785a
HH
309 sudo umount $TESTDIR/mnt
310 rm -fr $TESTDIR/mnt
9ecbe2e4
DD
311}
312
313test_setup() {
ebcfda6c
HH
314
315 modinfo nbd &>/dev/null || { echo "Kernel does not support nbd"; exit 1; }
316
8bd5873f 317 make_encrypted_root || return 1
9ecbe2e4
DD
318 make_client_root || return 1
319 make_server_root || return 1
320
321 # Make the test image
322 (
0be1785a 323 initdir=$TESTDIR/overlay
9ecbe2e4
DD
324 . $basedir/dracut-functions
325 dracut_install poweroff shutdown
0b53ca70 326 inst_hook emergency 000 ./hard-off.sh
778d2ba2 327 inst_simple ./99-idesymlinks.rules /etc/udev/rules.d/99-idesymlinks.rules
8bd5873f 328 inst ./cryptroot-ask /sbin/cryptroot-ask
9ecbe2e4
DD
329 )
330
0be1785a 331 sudo $basedir/dracut -l -i $TESTDIR/overlay / \
e2dbd86f 332 -m "dash udev-rules rootfs-block base debug kernel-modules" \
778d2ba2 333 -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod e1000" \
0be1785a 334 -f $TESTDIR/initramfs.server $KVERSION || return 1
9ecbe2e4 335
0be1785a 336 sudo $basedir/dracut -l -i $TESTDIR/overlay / \
5db73403
HH
337 -o "plymouth" \
338 -a "debug" \
778d2ba2 339 -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod e1000" \
0be1785a 340 -f $TESTDIR/initramfs.testing $KVERSION || return 1
9ecbe2e4
DD
341}
342
97add1b3 343kill_server() {
0be1785a
HH
344 if [[ -s $TESTDIR/server.pid ]]; then
345 sudo kill -TERM $(cat $TESTDIR/server.pid)
346 rm -f $TESTDIR/server.pid
9ecbe2e4 347 fi
97add1b3
HH
348}
349
350test_cleanup() {
351 kill_server
9ecbe2e4
DD
352}
353
354. $testdir/test-functions