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