]> git.ipfire.org Git - thirdparty/dracut.git/blame - test/TEST-40-NBD/test.sh
test/TEST-99-RPM: fixed test suite
[thirdparty/dracut.git] / test / TEST-40-NBD / test.sh
CommitLineData
9ecbe2e4 1#!/bin/bash
967cc19a 2
9ecbe2e4
DD
3TEST_DESCRIPTION="root filesystem on NBD"
4
5KVERSION=${KVERSION-$(uname -r)}
6
7# Uncomment this to debug failures
b070c1d3
HH
8#DEBUGFAIL="rd.shell rd.break rd.debug"
9SERIAL="tcp:127.0.0.1:9999"
51b28ba9 10SERIAL="null"
9ecbe2e4
DD
11
12run_server() {
13 # Start server first
b2c5f5dc 14 echo "NBD TEST SETUP: Starting DHCP/NBD server"
9ecbe2e4 15
0be1785a 16 $testdir/run-qemu \
4358ace4
HH
17 -drive format=raw,index=0,media=disk,file=$TESTDIR/server.ext2 \
18 -drive format=raw,index=1,media=disk,file=$TESTDIR/nbd.ext2 \
19 -drive format=raw,index=2,media=disk,file=$TESTDIR/encrypted.ext2 \
44f870be
HH
20 -m 256M -smp 2 \
21 -display none \
83691c41
HH
22 -net nic,macaddr=52:54:00:12:34:56,model=e1000 \
23 -net socket,listen=127.0.0.1:12340 \
24 -serial $SERIAL \
83691c41
HH
25 -append "root=/dev/sda rootfstype=ext2 rw quiet console=ttyS0,115200n81 selinux=0" \
26 -initrd $TESTDIR/initramfs.server -pidfile $TESTDIR/server.pid -daemonize || return 1
0be1785a 27 sudo chmod 644 $TESTDIR/server.pid || return 1
9ecbe2e4
DD
28
29 # Cleanup the terminal if we have one
30 tty -s && stty sane
31
32 echo Sleeping 10 seconds to give the server a head start
33 sleep 10
34}
35
36client_test() {
37 local test_name="$1"
38 local mac=$2
39 local cmdline="$3"
a29f15a5
DD
40 local fstype=$4
41 local fsopt=$5
42 local found opts nbdinfo
43
44 [[ $fstype ]] || fstype=ext3
75e8f476 45 [[ $fsopt ]] || fsopt="ro"
9ecbe2e4
DD
46
47 echo "CLIENT TEST START: $test_name"
48
49 # Clear out the flags for each test
0be1785a 50 if ! dd if=/dev/zero of=$TESTDIR/flag.img bs=1M count=1; then
83691c41
HH
51 echo "Unable to make client sda image" 1>&2
52 return 1
9ecbe2e4 53 fi
83691c41 54
0be1785a 55 $testdir/run-qemu \
4358ace4 56 -drive format=raw,index=0,media=disk,file=$TESTDIR/flag.img \
bb278147 57 -m 512M -smp 2 \
83691c41
HH
58 -nographic \
59 -net nic,macaddr=$mac,model=e1000 \
60 -net socket,connect=127.0.0.1:12340 \
781f1971 61 -append "rd.shell=0 $cmdline $DEBUGFAIL rd.auto rd.info rd.retry=10 ro console=ttyS0,115200n81 selinux=0 " \
83691c41 62 -initrd $TESTDIR/initramfs.testing
9ecbe2e4 63
021b2fdd 64 if [[ $? -ne 0 ]] || ! grep -F -m 1 -q nbd-OK $TESTDIR/flag.img; then
83691c41
HH
65 echo "CLIENT TEST END: $test_name [FAILED - BAD EXIT]"
66 return 1
9ecbe2e4
DD
67 fi
68
a29f15a5 69 # nbdinfo=( fstype fsoptions )
0be1785a 70 nbdinfo=($(awk '{print $2, $3; exit}' $TESTDIR/flag.img))
a29f15a5
DD
71
72 if [[ "${nbdinfo[0]}" != "$fstype" ]]; then
83691c41
HH
73 echo "CLIENT TEST END: $test_name [FAILED - WRONG FS TYPE] \"${nbdinfo[0]}\" != \"$fstype\""
74 return 1
a29f15a5
DD
75 fi
76
77 opts=${nbdinfo[1]},
78 while [[ $opts ]]; do
83691c41
HH
79 if [[ ${opts%%,*} = $fsopt ]]; then
80 found=1
81 break
82 fi
83 opts=${opts#*,}
a29f15a5
DD
84 done
85
86 if [[ ! $found ]]; then
83691c41
HH
87 echo "CLIENT TEST END: $test_name [FAILED - BAD FS OPTS] \"${nbdinfo[1]}\" != \"$fsopt\""
88 return 1
a29f15a5
DD
89 fi
90
9ecbe2e4
DD
91 echo "CLIENT TEST END: $test_name [OK]"
92}
93
94test_run() {
ebcfda6c 95 modinfo nbd &>/dev/null || { echo "Kernel does not support nbd"; exit 1; }
9ecbe2e4 96 if ! run_server; then
83691c41
HH
97 echo "Failed to start server" 1>&2
98 return 1
9ecbe2e4 99 fi
57aa1e91
HH
100 client_run
101 kill_server
97add1b3
HH
102}
103
104client_run() {
a29f15a5
DD
105 # The default is ext3,errors=continue so use that to determine
106 # if our options were parsed and used
b070c1d3
HH
107 client_test "NBD root=nbd:IP:port" 52:54:00:12:34:00 \
108 "root=nbd:192.168.50.1:raw rd.luks=0" || return 1
109
83691c41 110 client_test "NBD root=nbd:IP:port::fsopts" 52:54:00:12:34:00 \
b070c1d3 111 "root=nbd:192.168.50.1:raw::errors=panic rd.luks=0" \
83691c41
HH
112 ext3 errors=panic || return 1
113
bcf94bba 114 client_test "NBD root=nbd:IP:port:fstype" 52:54:00:12:34:00 \
b070c1d3 115 "root=nbd:192.168.50.1:raw:ext2 rd.luks=0" ext2 || return 1
a29f15a5 116
bcf94bba 117 client_test "NBD root=nbd:IP:port:fstype:fsopts" 52:54:00:12:34:00 \
b070c1d3 118 "root=nbd:192.168.50.1:raw:ext2:errors=panic rd.luks=0" \
83691c41 119 ext2 errors=panic || return 1
a29f15a5 120
bcf94bba 121 client_test "NBD Bridge root=nbd:IP:port:fstype:fsopts" 52:54:00:12:34:00 \
b070c1d3 122 "root=nbd:192.168.50.1:raw:ext2:errors=panic bridge rd.luks=0" \
83691c41 123 ext2 errors=panic || return 1
beb097d9 124
50e7ff76
PS
125 # There doesn't seem to be a good way to validate the NBD options, so
126 # just check that we don't screw up the other options
a29f15a5 127
bcf94bba 128 client_test "NBD root=nbd:IP:port:::NBD opts" 52:54:00:12:34:00 \
b070c1d3 129 "root=nbd:192.168.50.1:raw:::bs=2048 rd.luks=0" || return 1
a29f15a5 130
bcf94bba 131 client_test "NBD root=nbd:IP:port:fstype::NBD opts" 52:54:00:12:34:00 \
b070c1d3 132 "root=nbd:192.168.50.1:raw:ext2::bs=2048 rd.luks=0" ext2 || return 1
a29f15a5 133
bcf94bba 134 client_test "NBD root=nbd:IP:port:fstype:fsopts:NBD opts" \
83691c41 135 52:54:00:12:34:00 \
b070c1d3 136 "root=nbd:192.168.50.1:raw:ext2:errors=panic:bs=2048 rd.luks=0" \
83691c41 137 ext2 errors=panic || return 1
a29f15a5 138
a29f15a5
DD
139 # DHCP root-path parsing
140
9ecbe2e4 141 client_test "NBD root=dhcp DHCP root-path nbd:srv:port" 52:54:00:12:34:01 \
83691c41 142 "root=dhcp rd.luks=0" || return 1
a29f15a5 143
beb097d9 144 client_test "NBD Bridge root=dhcp DHCP root-path nbd:srv:port" 52:54:00:12:34:01 \
83691c41 145 "root=dhcp bridge rd.luks=0" || return 1
beb097d9 146
a29f15a5 147 client_test "NBD root=dhcp DHCP root-path nbd:srv:port:fstype" \
83691c41 148 52:54:00:12:34:02 "root=dhcp rd.luks=0" ext2 || return 1
a29f15a5
DD
149
150 client_test "NBD root=dhcp DHCP root-path nbd:srv:port::fsopts" \
83691c41 151 52:54:00:12:34:03 "root=dhcp rd.luks=0" ext3 errors=panic || return 1
a29f15a5
DD
152
153 client_test "NBD root=dhcp DHCP root-path nbd:srv:port:fstype:fsopts" \
83691c41 154 52:54:00:12:34:04 "root=dhcp rd.luks=0" ext2 errors=panic || return 1
aec48753
DD
155
156 # netroot handling
157
158 client_test "NBD netroot=nbd:IP:port" 52:54:00:12:34:00 \
b070c1d3 159 "netroot=nbd:192.168.50.1:raw rd.luks=0" || return 1
aec48753
DD
160
161 client_test "NBD netroot=dhcp DHCP root-path nbd:srv:port:fstype:fsopts" \
83691c41 162 52:54:00:12:34:04 "netroot=dhcp rd.luks=0" ext2 errors=panic || return 1
8bd5873f
DD
163
164 # Encrypted root handling via LVM/LUKS over NBD
165
83691c41
HH
166 . $TESTDIR/luks.uuid
167
bb278147 168 client_test "NBD root=LABEL=dracut netroot=nbd:IP:port" \
83691c41 169 52:54:00:12:34:00 \
b070c1d3 170 "root=LABEL=dracut rd.luks.uuid=$ID_FS_UUID rd.lv.vg=dracut netroot=nbd:192.168.50.1:encrypted" || return 1
8bd5873f
DD
171
172 # XXX This should be ext2,errors=panic but that doesn't currently
173 # XXX work when you have a real root= line in addition to netroot=
174 # XXX How we should work here needs clarification
bb278147 175 client_test "NBD root=LABEL=dracut netroot=dhcp (w/ fstype and opts)" \
83691c41 176 52:54:00:12:34:05 \
bb278147 177 "root=LABEL=dracut rd.luks.uuid=$ID_FS_UUID rd.lv.vg=dracut netroot=dhcp" || return 1
9ca74ffe
HH
178
179 if [[ -s server.pid ]]; then
83691c41 180 sudo kill -TERM $(cat $TESTDIR/server.pid)
32bd2fbb 181 rm -f -- $TESTDIR/server.pid
9ca74ffe
HH
182 fi
183
8bd5873f
DD
184}
185
186make_encrypted_root() {
187 # Create the blank file to use as a root filesystem
0be1785a
HH
188 dd if=/dev/null of=$TESTDIR/encrypted.ext2 bs=1M seek=20
189 dd if=/dev/null of=$TESTDIR/flag.img bs=1M seek=1
8bd5873f
DD
190
191 kernel=$KVERSION
192 # Create what will eventually be our root filesystem onto an overlay
193 (
83691c41 194 export initdir=$TESTDIR/overlay/source
777f2db0 195 . $basedir/dracut-init.sh
28f0b27f 196 mkdir -p "$initdir"
06853123
HH
197 (
198 cd "$initdir"; mkdir -p dev sys proc etc var/run tmp
199 mkdir -p root usr/bin usr/lib usr/lib64 usr/sbin
200 for i in bin sbin lib lib64; do
201 ln -sfnr usr/$i $i
202 done
203 )
af119460 204 inst_multiple sh df free ls shutdown poweroff stty cat ps ln ip \
83691c41 205 mount dmesg mkdir cp ping
96d22bd7 206 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
83691c41
HH
207 [ -f ${_terminfodir}/l/linux ] && break
208 done
af119460 209 inst_multiple -o ${_terminfodir}/l/linux
83691c41 210 inst ./client-init.sh /sbin/init
021b2fdd 211 inst_simple /etc/os-release
af119460 212 find_binary plymouth >/dev/null && inst_multiple plymouth
83691c41
HH
213 cp -a /etc/ld.so.conf* $initdir/etc
214 sudo ldconfig -r "$initdir"
8bd5873f
DD
215 )
216
217 # second, install the files needed to make the root filesystem
218 (
83691c41 219 export initdir=$TESTDIR/overlay
777f2db0 220 . $basedir/dracut-init.sh
06853123
HH
221 (
222 cd "$initdir"; mkdir -p dev sys proc etc var/run tmp
223 mkdir -p root usr/bin usr/lib usr/lib64 usr/sbin
224 for i in bin sbin lib lib64; do
225 ln -sfnr usr/$i $i
226 done
227 )
af119460 228 inst_multiple mke2fs poweroff cp umount tune2fs
4e882b80 229 inst_hook shutdown-emergency 000 ./hard-off.sh
781f1971 230 inst_hook emergency 000 ./hard-off.sh
83691c41 231 inst_hook initqueue 01 ./create-root.sh
356333b3 232 inst_hook initqueue/finished 01 ./finished-false.sh
83691c41 233 inst_simple ./99-idesymlinks.rules /etc/udev/rules.d/99-idesymlinks.rules
8bd5873f
DD
234 )
235
236 # create an initramfs that will create the target root filesystem.
237 # We do it this way so that we do not risk trashing the host mdraid
238 # devices, volume groups, encrypted partitions, etc.
552ecca6 239 $basedir/dracut.sh -l -i $TESTDIR/overlay / \
eda73c0a 240 -m "dash crypt lvm mdraid udev-rules base rootfs-block fs-lib kernel-modules" \
83691c41 241 -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod" \
e3e1f406 242 --no-hostonly-cmdline -N \
83691c41 243 -f $TESTDIR/initramfs.makeroot $KVERSION || return 1
32bd2fbb 244 rm -rf -- $TESTDIR/overlay
8bd5873f
DD
245
246 # Invoke KVM and/or QEMU to actually create the target filesystem.
0be1785a 247 $testdir/run-qemu \
4358ace4
HH
248 -drive format=raw,index=0,media=disk,file=$TESTDIR/flag.img \
249 -drive format=raw,index=1,media=disk,file=$TESTDIR/encrypted.ext2 \
bb278147 250 -m 256M -smp 2\
83691c41 251 -nographic -net none \
bb278147 252 -append "root=/dev/fakeroot rw quiet console=ttyS0,115200n81 selinux=0" \
83691c41 253 -initrd $TESTDIR/initramfs.makeroot || return 1
021b2fdd
HH
254 grep -F -m 1 -q dracut-root-block-created $TESTDIR/flag.img || return 1
255 grep -F -a -m 1 ID_FS_UUID $TESTDIR/flag.img > $TESTDIR/luks.uuid
9ecbe2e4
DD
256}
257
258make_client_root() {
0be1785a
HH
259 dd if=/dev/null of=$TESTDIR/nbd.ext2 bs=1M seek=30
260 mke2fs -F -j $TESTDIR/nbd.ext2
261 mkdir $TESTDIR/mnt
262 sudo mount -o loop $TESTDIR/nbd.ext2 $TESTDIR/mnt
9ecbe2e4
DD
263
264 kernel=$KVERSION
265 (
83691c41 266 export initdir=$TESTDIR/mnt
777f2db0 267 . $basedir/dracut-init.sh
28f0b27f 268 mkdir -p "$initdir"
06853123
HH
269 (
270 cd "$initdir"; mkdir -p dev sys proc etc var/run tmp
271 mkdir -p root usr/bin usr/lib usr/lib64 usr/sbin
272 for i in bin sbin lib lib64; do
273 ln -sfnr usr/$i $i
274 done
275 )
af119460 276 inst_multiple sh ls shutdown poweroff stty cat ps ln ip \
83691c41 277 dmesg mkdir cp ping
96d22bd7 278 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
83691c41
HH
279 [ -f ${_terminfodir}/l/linux ] && break
280 done
af119460 281 inst_multiple -o ${_terminfodir}/l/linux
83691c41 282 inst ./client-init.sh /sbin/init
021b2fdd 283 inst_simple /etc/os-release
83691c41
HH
284 inst /etc/nsswitch.conf /etc/nsswitch.conf
285 inst /etc/passwd /etc/passwd
286 inst /etc/group /etc/group
287 for i in /usr/lib*/libnss_files* /lib*/libnss_files*;do
28f0b27f 288 [ -e "$i" ] || continue
83691c41
HH
289 inst $i
290 done
291 cp -a /etc/ld.so.conf* $initdir/etc
292 sudo ldconfig -r "$initdir"
9ecbe2e4
DD
293 )
294
0be1785a 295 sudo umount $TESTDIR/mnt
32bd2fbb 296 rm -fr -- $TESTDIR/mnt
9ecbe2e4
DD
297}
298
299make_server_root() {
0be1785a
HH
300 dd if=/dev/null of=$TESTDIR/server.ext2 bs=1M seek=30
301 mke2fs -F $TESTDIR/server.ext2
302 mkdir $TESTDIR/mnt
303 sudo mount -o loop $TESTDIR/server.ext2 $TESTDIR/mnt
9ecbe2e4
DD
304
305 kernel=$KVERSION
306 (
83691c41 307 export initdir=$TESTDIR/mnt
777f2db0 308 . $basedir/dracut-init.sh
28f0b27f 309 mkdir -p "$initdir"
83691c41
HH
310 (
311 cd "$initdir";
b070c1d3 312 mkdir -p dev sys proc etc var/run var/lib/dhcpd tmp etc/nbd-server
83691c41 313 )
b070c1d3
HH
314 cat > "$initdir/etc/nbd-server/config" <<EOF
315[generic]
316[raw]
317exportname = /dev/sdb
318port = 2000
319[encrypted]
320exportname = /dev/sdc
321port = 2001
322EOF
af119460 323 inst_multiple sh ls shutdown poweroff stty cat ps ln ip \
83691c41 324 dmesg mkdir cp ping grep \
b070c1d3 325 sleep nbd-server chmod modprobe vi
96d22bd7 326 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
83691c41
HH
327 [ -f ${_terminfodir}/l/linux ] && break
328 done
af119460 329 inst_multiple -o ${_terminfodir}/l/linux
83691c41 330 instmods af_packet
af119460 331 type -P dhcpd >/dev/null && inst_multiple dhcpd
83691c41
HH
332 [ -x /usr/sbin/dhcpd3 ] && inst /usr/sbin/dhcpd3 /usr/sbin/dhcpd
333 inst ./server-init.sh /sbin/init
021b2fdd 334 inst_simple /etc/os-release
83691c41
HH
335 inst ./hosts /etc/hosts
336 inst ./dhcpd.conf /etc/dhcpd.conf
337 inst /etc/nsswitch.conf /etc/nsswitch.conf
338 inst /etc/passwd /etc/passwd
339 inst /etc/group /etc/group
340 for i in /usr/lib*/libnss_files* /lib*/libnss_files*;do
28f0b27f 341 [ -e "$i" ] || continue
83691c41
HH
342 inst $i
343 done
9ecbe2e4 344
83691c41
HH
345 cp -a /etc/ld.so.conf* $initdir/etc
346 sudo ldconfig -r "$initdir"
9ecbe2e4
DD
347 )
348
0be1785a 349 sudo umount $TESTDIR/mnt
32bd2fbb 350 rm -fr -- $TESTDIR/mnt
9ecbe2e4
DD
351}
352
353test_setup() {
ebcfda6c
HH
354
355 modinfo nbd &>/dev/null || { echo "Kernel does not support nbd"; exit 1; }
356
8bd5873f 357 make_encrypted_root || return 1
9ecbe2e4
DD
358 make_client_root || return 1
359 make_server_root || return 1
360
361 # Make the test image
362 (
83691c41 363 export initdir=$TESTDIR/overlay
777f2db0 364 . $basedir/dracut-init.sh
af119460 365 inst_multiple poweroff shutdown
4e882b80 366 inst_hook shutdown-emergency 000 ./hard-off.sh
83691c41 367 inst_simple ./99-idesymlinks.rules /etc/udev/rules.d/99-idesymlinks.rules
3f7e5358 368 inst ./cryptroot-ask.sh /sbin/cryptroot-ask
83691c41
HH
369
370# inst ./debug-shell.service /lib/systemd/system/debug-shell.service
371# mkdir -p "${initdir}/lib/systemd/system/sysinit.target.wants"
372# ln -fs ../debug-shell.service "${initdir}/lib/systemd/system/sysinit.target.wants/debug-shell.service"
373
3f7e5358
HH
374 . $TESTDIR/luks.uuid
375 mkdir -p $initdir/etc
83691c41 376 echo "luks-$ID_FS_UUID /dev/nbd0 /etc/key" > $initdir/etc/crypttab
3f7e5358 377 echo -n test > $initdir/etc/key
9ecbe2e4
DD
378 )
379
552ecca6 380 sudo $basedir/dracut.sh -l -i $TESTDIR/overlay / \
eda73c0a 381 -m "dash udev-rules rootfs-block fs-lib base debug kernel-modules" \
83691c41 382 -d "af_packet piix ide-gd_mod ata_piix ext2 ext3 sd_mod e1000" \
e3e1f406 383 --no-hostonly-cmdline -N \
83691c41 384 -f $TESTDIR/initramfs.server $KVERSION || return 1
9ecbe2e4 385
552ecca6 386 sudo $basedir/dracut.sh -l -i $TESTDIR/overlay / \
83691c41
HH
387 -o "plymouth" \
388 -a "debug watchdog" \
56fb5c4d 389 -d "af_packet piix ide-gd_mod ata_piix ext2 ext3 sd_mod e1000 i6300esb ib700wdt" \
e3e1f406 390 --no-hostonly-cmdline -N \
83691c41 391 -f $TESTDIR/initramfs.testing $KVERSION || return 1
9ecbe2e4
DD
392}
393
97add1b3 394kill_server() {
0be1785a 395 if [[ -s $TESTDIR/server.pid ]]; then
83691c41 396 sudo kill -TERM $(cat $TESTDIR/server.pid)
32bd2fbb 397 rm -f -- $TESTDIR/server.pid
9ecbe2e4 398 fi
97add1b3
HH
399}
400
401test_cleanup() {
402 kill_server
9ecbe2e4
DD
403}
404
405. $testdir/test-functions