]> git.ipfire.org Git - thirdparty/dracut.git/blob - test/TEST-40-NBD/test.sh
testsuite: fixups
[thirdparty/dracut.git] / test / TEST-40-NBD / test.sh
1 #!/bin/bash
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
4 TEST_DESCRIPTION="root filesystem on NBD"
5
6 KVERSION=${KVERSION-$(uname -r)}
7
8 # Uncomment this to debug failures
9 #DEBUGFAIL="rd.shell rd.retry=10 rd.break"
10 #SERIAL="udp:127.0.0.1:9999"
11 SERIAL="null"
12
13 run_server() {
14 # Start server first
15 echo "NBD TEST SETUP: Starting DHCP/NBD server"
16
17 $testdir/run-qemu \
18 -hda $TESTDIR/server.ext2 \
19 -hdb $TESTDIR/nbd.ext2 \
20 -hdc $TESTDIR/encrypted.ext2 \
21 -m 256M -nographic \
22 -net nic,macaddr=52:54:00:12:34:56,model=e1000 \
23 -net socket,listen=127.0.0.1:12340 \
24 -serial $SERIAL \
25 -kernel /boot/vmlinuz-$KVERSION \
26 -append "root=/dev/sda rootfstype=ext2 rw quiet console=ttyS0,115200n81 selinux=0" \
27 -initrd $TESTDIR/initramfs.server -pidfile $TESTDIR/server.pid -daemonize || return 1
28 sudo chmod 644 $TESTDIR/server.pid || return 1
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
37 client_test() {
38 local test_name="$1"
39 local mac=$2
40 local cmdline="$3"
41 local fstype=$4
42 local fsopt=$5
43 local found opts nbdinfo
44
45 [[ $fstype ]] || fstype=ext3
46 [[ $fsopt ]] || fsopt="ro"
47
48 echo "CLIENT TEST START: $test_name"
49
50 # Clear out the flags for each test
51 if ! dd if=/dev/zero of=$TESTDIR/flag.img bs=1M count=1; then
52 echo "Unable to make client sda image" 1>&2
53 return 1
54 fi
55
56 $testdir/run-qemu \
57 -hda $TESTDIR/flag.img \
58 -m 256M -nographic \
59 -net nic,macaddr=$mac,model=e1000 \
60 -net socket,connect=127.0.0.1:12340 \
61 -kernel /boot/vmlinuz-$KVERSION \
62 -append "$cmdline $DEBUGFAIL rd.debug rd.info rd.retry=10 ro quiet console=ttyS0,115200n81 selinux=0" \
63 -initrd $TESTDIR/initramfs.testing
64
65 if [[ $? -ne 0 ]] || ! grep -m 1 -q nbd-OK $TESTDIR/flag.img; then
66 echo "CLIENT TEST END: $test_name [FAILED - BAD EXIT]"
67 return 1
68 fi
69
70 # nbdinfo=( fstype fsoptions )
71 nbdinfo=($(awk '{print $2, $3; exit}' $TESTDIR/flag.img))
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
80 if [[ ${opts%%,*} = $fsopt ]]; then
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
92 echo "CLIENT TEST END: $test_name [OK]"
93 }
94
95 test_run() {
96 modinfo nbd &>/dev/null || { echo "Kernel does not support nbd"; exit 1; }
97 if ! run_server; then
98 echo "Failed to start server" 1>&2
99 return 1
100 fi
101 client_run || { kill_server; return 1; }
102 }
103
104 client_run() {
105
106 # The default is ext3,errors=continue so use that to determine
107 # if our options were parsed and used
108 client_test "NBD root=nbd:IP:port" 52:54:00:12:34:00 \
109 "root=nbd:192.168.50.1:2000" || return 1
110
111 client_test "NBD root=nbd:IP:port:fstype" 52:54:00:12:34:00 \
112 "root=nbd:192.168.50.1:2000:ext2" ext2 || return 1
113
114 client_test "NBD root=nbd:IP:port::fsopts" 52:54:00:12:34:00 \
115 "root=nbd:192.168.50.1:2000::errors=panic" \
116 ext3 errors=panic || return 1
117
118 client_test "NBD root=nbd:IP:port:fstype:fsopts" 52:54:00:12:34:00 \
119 "root=nbd:192.168.50.1:2000:ext2:errors=panic" \
120 ext2 errors=panic || return 1
121
122 client_test "NBD Bridge root=nbd:IP:port:fstype:fsopts" 52:54:00:12:34:00 \
123 "root=nbd:192.168.50.1:2000:ext2:errors=panic bridge" \
124 ext2 errors=panic || return 1
125
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
128
129 client_test "NBD root=nbd:IP:port:::NBD opts" 52:54:00:12:34:00 \
130 "root=nbd:192.168.50.1:2000:::bs=2048" || return 1
131
132 client_test "NBD root=nbd:IP:port:fstype::NBD opts" 52:54:00:12:34:00 \
133 "root=nbd:192.168.50.1:2000:ext2::bs=2048" ext2 || return 1
134
135 client_test "NBD root=nbd:IP:port:fstype:fsopts:NBD opts" \
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
139
140 # DHCP root-path parsing
141
142 client_test "NBD root=dhcp DHCP root-path nbd:srv:port" 52:54:00:12:34:01 \
143 "root=dhcp" || return 1
144
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
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
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
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
177
178 if [[ -s server.pid ]]; then
179 sudo kill -TERM $(cat $TESTDIR/server.pid)
180 rm -f $TESTDIR/server.pid
181 fi
182
183 }
184
185 make_encrypted_root() {
186 # Create the blank file to use as a root filesystem
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
189
190 kernel=$KVERSION
191 # Create what will eventually be our root filesystem onto an overlay
192 (
193 initdir=$TESTDIR/overlay/source
194 . $basedir/dracut-functions.sh
195 mkdir -p "$initdir"
196 (cd "$initdir"; mkdir -p dev sys proc etc var/run tmp )
197 dracut_install sh df free ls shutdown poweroff stty cat ps ln ip \
198 mount dmesg mkdir cp ping
199 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
200 [ -f ${_terminfodir}/l/linux ] && break
201 done
202 dracut_install -o ${_terminfodir}/l/linux
203 inst ./client-init.sh /sbin/init
204 find_binary plymouth >/dev/null && dracut_install plymouth
205 cp -a /etc/ld.so.conf* $initdir/etc
206 sudo ldconfig -r "$initdir"
207 )
208
209 # second, install the files needed to make the root filesystem
210 (
211 initdir=$TESTDIR/overlay
212 . $basedir/dracut-functions.sh
213 dracut_install mke2fs poweroff cp umount tune2fs
214 inst_hook initqueue 01 ./create-root.sh
215 inst_simple ./99-idesymlinks.rules /etc/udev/rules.d/99-idesymlinks.rules
216 )
217
218 # create an initramfs that will create the target root filesystem.
219 # We do it this way so that we do not risk trashing the host mdraid
220 # devices, volume groups, encrypted partitions, etc.
221 $basedir/dracut.sh -l -i $TESTDIR/overlay / \
222 -m "dash crypt lvm mdraid udev-rules base rootfs-block kernel-modules" \
223 -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod" \
224 -f $TESTDIR/initramfs.makeroot $KVERSION || return 1
225 rm -rf $TESTDIR/overlay
226
227 # Invoke KVM and/or QEMU to actually create the target filesystem.
228 $testdir/run-qemu \
229 -hda $TESTDIR/flag.img \
230 -hdb $TESTDIR/encrypted.ext2 \
231 -m 256M \
232 -nographic -net none \
233 -kernel "/boot/vmlinuz-$kernel" \
234 -append "root=/dev/dracut/root rw quiet console=ttyS0,115200n81 selinux=0" \
235 -initrd $TESTDIR/initramfs.makeroot || return 1
236 grep -m 1 -q dracut-root-block-created $TESTDIR/flag.img || return 1
237 }
238
239 make_client_root() {
240 dd if=/dev/null of=$TESTDIR/nbd.ext2 bs=1M seek=30
241 mke2fs -F -j $TESTDIR/nbd.ext2
242 mkdir $TESTDIR/mnt
243 sudo mount -o loop $TESTDIR/nbd.ext2 $TESTDIR/mnt
244
245 kernel=$KVERSION
246 (
247 initdir=$TESTDIR/mnt
248 . $basedir/dracut-functions.sh
249 mkdir -p "$initdir"
250 (cd "$initdir"; mkdir -p dev sys proc etc var/run tmp )
251 dracut_install sh ls shutdown poweroff stty cat ps ln ip \
252 dmesg mkdir cp ping
253 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
254 [ -f ${_terminfodir}/l/linux ] && break
255 done
256 dracut_install -o ${_terminfodir}/l/linux
257 inst ./client-init.sh /sbin/init
258 inst /etc/nsswitch.conf /etc/nsswitch.conf
259 inst /etc/passwd /etc/passwd
260 inst /etc/group /etc/group
261 for i in /usr/lib*/libnss_files* /lib*/libnss_files*;do
262 [ -e "$i" ] || continue
263 inst $i
264 done
265 cp -a /etc/ld.so.conf* $initdir/etc
266 sudo ldconfig -r "$initdir"
267 )
268
269 sudo umount $TESTDIR/mnt
270 rm -fr $TESTDIR/mnt
271 }
272
273 make_server_root() {
274 dd if=/dev/null of=$TESTDIR/server.ext2 bs=1M seek=30
275 mke2fs -F $TESTDIR/server.ext2
276 mkdir $TESTDIR/mnt
277 sudo mount -o loop $TESTDIR/server.ext2 $TESTDIR/mnt
278
279 kernel=$KVERSION
280 (
281 initdir=$TESTDIR/mnt
282 . $basedir/dracut-functions.sh
283 mkdir -p "$initdir"
284 (
285 cd "$initdir";
286 mkdir -p dev sys proc etc var/run var/lib/dhcpd tmp
287 )
288 dracut_install sh ls shutdown poweroff stty cat ps ln ip \
289 dmesg mkdir cp ping grep \
290 sleep nbd-server chmod
291 for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
292 [ -f ${_terminfodir}/l/linux ] && break
293 done
294 dracut_install -o ${_terminfodir}/l/linux
295 type -P dhcpd >/dev/null && dracut_install dhcpd
296 [ -x /usr/sbin/dhcpd3 ] && inst /usr/sbin/dhcpd3 /usr/sbin/dhcpd
297 inst ./server-init.sh /sbin/init
298 inst ./hosts /etc/hosts
299 inst ./dhcpd.conf /etc/dhcpd.conf
300 inst /etc/nsswitch.conf /etc/nsswitch.conf
301 inst /etc/passwd /etc/passwd
302 inst /etc/group /etc/group
303 for i in /usr/lib*/libnss_files* /lib*/libnss_files*;do
304 [ -e "$i" ] || continue
305 inst $i
306 done
307
308 cp -a /etc/ld.so.conf* $initdir/etc
309 sudo ldconfig -r "$initdir"
310 )
311
312 sudo umount $TESTDIR/mnt
313 rm -fr $TESTDIR/mnt
314 }
315
316 test_setup() {
317
318 modinfo nbd &>/dev/null || { echo "Kernel does not support nbd"; exit 1; }
319
320 make_encrypted_root || return 1
321 make_client_root || return 1
322 make_server_root || return 1
323
324 # Make the test image
325 (
326 initdir=$TESTDIR/overlay
327 . $basedir/dracut-functions.sh
328 dracut_install poweroff shutdown
329 inst_hook emergency 000 ./hard-off.sh
330 inst_simple ./99-idesymlinks.rules /etc/udev/rules.d/99-idesymlinks.rules
331 inst ./cryptroot-ask.sh /sbin/cryptroot-ask
332 )
333
334 sudo $basedir/dracut.sh -l -i $TESTDIR/overlay / \
335 -m "dash udev-rules rootfs-block base debug kernel-modules" \
336 -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod e1000" \
337 -f $TESTDIR/initramfs.server $KVERSION || return 1
338
339 sudo $basedir/dracut.sh -l -i $TESTDIR/overlay / \
340 -o "plymouth" \
341 -a "debug" \
342 -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod e1000" \
343 -f $TESTDIR/initramfs.testing $KVERSION || return 1
344 }
345
346 kill_server() {
347 if [[ -s $TESTDIR/server.pid ]]; then
348 sudo kill -TERM $(cat $TESTDIR/server.pid)
349 rm -f $TESTDIR/server.pid
350 fi
351 }
352
353 test_cleanup() {
354 kill_server
355 }
356
357 . $testdir/test-functions