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