]> git.ipfire.org Git - thirdparty/dracut.git/blame - test/TEST-40-NBD/test.sh
Makefile: s/dracut-initramfs-backup.sh/dracut-initramfs-restore.sh/
[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 \
196 /lib/terminfo/l/linux mount dmesg mkdir cp ping
197 inst ./client-init /sbin/init
198 find_binary plymouth >/dev/null && dracut_install plymouth
199 (cd "$initdir"; mkdir -p dev sys proc etc var/run tmp )
200 )
201
202 # second, install the files needed to make the root filesystem
203 (
0be1785a 204 initdir=$TESTDIR/overlay
8bd5873f 205 . $basedir/dracut-functions
75e8f476 206 dracut_install mke2fs poweroff cp umount tune2fs
0b53ca70 207 inst_hook initqueue 01 ./create-root.sh
778d2ba2 208 inst_simple ./99-idesymlinks.rules /etc/udev/rules.d/99-idesymlinks.rules
8bd5873f
DD
209 )
210
211 # create an initramfs that will create the target root filesystem.
212 # We do it this way so that we do not risk trashing the host mdraid
213 # devices, volume groups, encrypted partitions, etc.
0be1785a 214 $basedir/dracut -l -i $TESTDIR/overlay / \
e2dbd86f 215 -m "dash crypt lvm mdraid udev-rules base rootfs-block kernel-modules" \
778d2ba2 216 -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod" \
0be1785a
HH
217 -f $TESTDIR/initramfs.makeroot $KVERSION || return 1
218 rm -rf $TESTDIR/overlay
8bd5873f
DD
219
220 # Invoke KVM and/or QEMU to actually create the target filesystem.
0be1785a
HH
221 $testdir/run-qemu \
222 -hda $TESTDIR/flag.img \
223 -hdb $TESTDIR/encrypted.ext2 \
224 -m 256M \
8bd5873f
DD
225 -nographic -net none \
226 -kernel "/boot/vmlinuz-$kernel" \
8eb16b08 227 -append "root=/dev/dracut/root rw quiet console=ttyS0,115200n81 selinux=0" \
0be1785a
HH
228 -initrd $TESTDIR/initramfs.makeroot || return 1
229 grep -m 1 -q dracut-root-block-created $TESTDIR/flag.img || return 1
9ecbe2e4
DD
230}
231
232make_client_root() {
0be1785a
HH
233 dd if=/dev/null of=$TESTDIR/nbd.ext2 bs=1M seek=30
234 mke2fs -F -j $TESTDIR/nbd.ext2
235 mkdir $TESTDIR/mnt
236 sudo mount -o loop $TESTDIR/nbd.ext2 $TESTDIR/mnt
9ecbe2e4
DD
237
238 kernel=$KVERSION
239 (
0be1785a 240 initdir=$TESTDIR/mnt
9ecbe2e4
DD
241 . $basedir/dracut-functions
242 dracut_install sh ls shutdown poweroff stty cat ps ln ip \
a29f15a5 243 /lib/terminfo/l/linux dmesg mkdir cp ping
9ecbe2e4
DD
244 inst ./client-init /sbin/init
245 (
246 cd "$initdir";
247 mkdir -p dev sys proc etc var/run tmp
248 )
249 inst /etc/nsswitch.conf /etc/nsswitch.conf
250 inst /etc/passwd /etc/passwd
251 inst /etc/group /etc/group
252 for i in /lib*/libnss_files**;do
253 inst_library $i
254 done
255
8a080127
PS
256 cp -a /etc/ld.so.conf* $initdir/etc
257 sudo ldconfig -r "$initdir"
9ecbe2e4
DD
258 )
259
0be1785a
HH
260 sudo umount $TESTDIR/mnt
261 rm -fr $TESTDIR/mnt
9ecbe2e4
DD
262}
263
264make_server_root() {
0be1785a
HH
265 dd if=/dev/null of=$TESTDIR/server.ext2 bs=1M seek=30
266 mke2fs -F $TESTDIR/server.ext2
267 mkdir $TESTDIR/mnt
268 sudo mount -o loop $TESTDIR/server.ext2 $TESTDIR/mnt
9ecbe2e4
DD
269
270 kernel=$KVERSION
271 (
0be1785a 272 initdir=$TESTDIR/mnt
9ecbe2e4
DD
273 . $basedir/dracut-functions
274 dracut_install sh ls shutdown poweroff stty cat ps ln ip \
45630db1
PS
275 /lib/terminfo/l/linux dmesg mkdir cp ping grep \
276 sleep nbd-server chmod
f3af7bd6 277 type -P dhcpd >/dev/null && dracut_install dhcpd
45630db1 278 [ -x /usr/sbin/dhcpd3 ] && inst /usr/sbin/dhcpd3 /usr/sbin/dhcpd
9ecbe2e4
DD
279 inst ./server-init /sbin/init
280 inst ./hosts /etc/hosts
281 inst ./dhcpd.conf /etc/dhcpd.conf
282 (
283 cd "$initdir";
284 mkdir -p dev sys proc etc var/run var/lib/dhcpd tmp
285 )
286 inst /etc/nsswitch.conf /etc/nsswitch.conf
287 inst /etc/passwd /etc/passwd
288 inst /etc/group /etc/group
289 for i in /lib*/libnss_files**;do
290 inst_library $i
291 done
292
8a080127
PS
293 cp -a /etc/ld.so.conf* $initdir/etc
294 sudo ldconfig -r "$initdir"
9ecbe2e4
DD
295 )
296
0be1785a
HH
297 sudo umount $TESTDIR/mnt
298 rm -fr $TESTDIR/mnt
9ecbe2e4
DD
299}
300
301test_setup() {
ebcfda6c
HH
302
303 modinfo nbd &>/dev/null || { echo "Kernel does not support nbd"; exit 1; }
304
8bd5873f 305 make_encrypted_root || return 1
9ecbe2e4
DD
306 make_client_root || return 1
307 make_server_root || return 1
308
309 # Make the test image
310 (
0be1785a 311 initdir=$TESTDIR/overlay
9ecbe2e4
DD
312 . $basedir/dracut-functions
313 dracut_install poweroff shutdown
0b53ca70 314 inst_hook emergency 000 ./hard-off.sh
778d2ba2 315 inst_simple ./99-idesymlinks.rules /etc/udev/rules.d/99-idesymlinks.rules
8bd5873f 316 inst ./cryptroot-ask /sbin/cryptroot-ask
9ecbe2e4
DD
317 )
318
0be1785a 319 sudo $basedir/dracut -l -i $TESTDIR/overlay / \
e2dbd86f 320 -m "dash udev-rules rootfs-block base debug kernel-modules" \
778d2ba2 321 -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod e1000" \
0be1785a 322 -f $TESTDIR/initramfs.server $KVERSION || return 1
9ecbe2e4 323
0be1785a 324 sudo $basedir/dracut -l -i $TESTDIR/overlay / \
5db73403
HH
325 -o "plymouth" \
326 -a "debug" \
778d2ba2 327 -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod e1000" \
0be1785a 328 -f $TESTDIR/initramfs.testing $KVERSION || return 1
9ecbe2e4
DD
329}
330
97add1b3 331kill_server() {
0be1785a
HH
332 if [[ -s $TESTDIR/server.pid ]]; then
333 sudo kill -TERM $(cat $TESTDIR/server.pid)
334 rm -f $TESTDIR/server.pid
9ecbe2e4 335 fi
97add1b3
HH
336}
337
338test_cleanup() {
339 kill_server
9ecbe2e4
DD
340}
341
342. $testdir/test-functions