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