Move all hooks to "$hookdir"
[dracut.git] / test / TEST-40-NBD / test.sh
blob3b1bf0e749dbd3812eff8ba7b2c64d35dd1368da
1 #!/bin/bash
2 TEST_DESCRIPTION="root filesystem on NBD"
4 KVERSION=${KVERSION-$(uname -r)}
6 # Uncomment this to debug failures
7 #DEBUGFAIL="rd.shell"
8 #SERIAL="udp:127.0.0.1:9999"
9 SERIAL="null"
11 run_server() {
12 # Start server first
13 echo "NBD TEST SETUP: Starting DHCP/NBD server"
15 $testdir/run-qemu -hda server.ext2 -hdb nbd.ext2 -hdc encrypted.ext2 \
16 -m 256M -nographic \
17 -net nic,macaddr=52:54:00:12:34:56,model=e1000 \
18 -net socket,listen=127.0.0.1:12345 \
19 -serial $SERIAL \
20 -kernel /boot/vmlinuz-$KVERSION \
21 -append "root=/dev/sda rw quiet console=ttyS0,115200n81 selinux=0" \
22 -initrd initramfs.server -pidfile server.pid -daemonize || return 1
23 sudo chmod 644 server.pid || return 1
25 # Cleanup the terminal if we have one
26 tty -s && stty sane
28 echo Sleeping 10 seconds to give the server a head start
29 sleep 10
32 client_test() {
33 local test_name="$1"
34 local mac=$2
35 local cmdline="$3"
36 local fstype=$4
37 local fsopt=$5
38 local found opts nbdinfo
40 [[ $fstype ]] || fstype=ext3
41 [[ $fsopt ]] || fsopt="errors=continue"
43 echo "CLIENT TEST START: $test_name"
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
51 $testdir/run-qemu -hda flag.img -m 256M -nographic \
52 -net nic,macaddr=$mac,model=e1000 \
53 -net socket,connect=127.0.0.1:12345 \
54 -kernel /boot/vmlinuz-$KVERSION \
55 -append "$cmdline $DEBUGFAIL rd.debug rd.info ro quiet console=ttyS0,115200n81 selinux=0" \
56 -initrd initramfs.testing
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
63 # nbdinfo=( fstype fsoptions )
64 nbdinfo=($(awk '{print $2, $3; exit}' flag.img))
66 if [[ "${nbdinfo[0]}" != "$fstype" ]]; then
67 echo "CLIENT TEST END: $test_name [FAILED - WRONG FS TYPE]"
68 return 1
71 opts=${nbdinfo[1]},
72 while [[ $opts ]]; do
73 if [[ ${opts%%,*} = $fsopt ]]; then
74 found=1
75 break
77 opts=${opts#*,}
78 done
80 if [[ ! $found ]]; then
81 echo "CLIENT TEST END: $test_name [FAILED - BAD FS OPTS]"
82 return 1
85 echo "CLIENT TEST END: $test_name [OK]"
88 test_run() {
89 modinfo nbd &>/dev/null || { echo "Kernel does not support nbd"; exit 1; }
90 if ! run_server; then
91 echo "Failed to start server" 1>&2
92 return 1
94 client_run || { kill_server; return 1; }
97 client_run() {
99 # The default is ext3,errors=continue so use that to determine
100 # if our options were parsed and used
102 client_test "NBD root=nbd:IP:port" 52:54:00:12:34:00 \
103 "root=nbd:192.168.50.1:2000" || return 1
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
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
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
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
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
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
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
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
134 # DHCP root-path parsing
136 client_test "NBD root=dhcp DHCP root-path nbd:srv:port" 52:54:00:12:34:01 \
137 "root=dhcp" || return 1
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
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
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
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
151 # netroot handling
153 client_test "NBD netroot=nbd:IP:port" 52:54:00:12:34:00 \
154 "netroot=nbd:192.168.50.1:2000" || return 1
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
159 # Encrypted root handling via LVM/LUKS over NBD
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
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
172 if [[ -s server.pid ]]; then
173 sudo kill -TERM $(cat server.pid)
174 rm -f server.pid
179 make_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
184 kernel=$KVERSION
185 # Create what will eventually be our root filesystem onto an overlay
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 )
196 # second, install the files needed to make the root filesystem
198 initdir=overlay
199 . $basedir/dracut-functions
200 dracut_install mke2fs poweroff cp umount
201 inst_hook initqueue 01 ./create-root.sh
202 inst_simple ./99-idesymlinks.rules /etc/udev/rules.d/99-idesymlinks.rules
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 / \
209 -m "dash crypt lvm mdraid udev-rules base rootfs-block kernel-modules" \
210 -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod" \
211 -f initramfs.makeroot $KVERSION || return 1
212 rm -rf overlay
214 # Invoke KVM and/or QEMU to actually create the target filesystem.
215 $testdir/run-qemu -hda flag.img -hdb encrypted.ext2 -m 256M \
216 -nographic -net none \
217 -kernel "/boot/vmlinuz-$kernel" \
218 -append "root=/dev/dracut/root rw quiet console=ttyS0,115200n81 selinux=0" \
219 -initrd initramfs.makeroot || return 1
220 grep -m 1 -q dracut-root-block-created flag.img || return 1
223 make_client_root() {
224 dd if=/dev/zero of=nbd.ext2 bs=1M count=30
225 mke2fs -F -j nbd.ext2
226 mkdir mnt
227 sudo mount -o loop nbd.ext2 mnt
229 kernel=$KVERSION
231 initdir=mnt
232 . $basedir/dracut-functions
233 dracut_install sh ls shutdown poweroff stty cat ps ln ip \
234 /lib/terminfo/l/linux dmesg mkdir cp ping
235 inst ./client-init /sbin/init
237 cd "$initdir";
238 mkdir -p dev sys proc etc var/run tmp
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
247 cp -a /etc/ld.so.conf* $initdir/etc
248 sudo ldconfig -r "$initdir"
251 sudo umount mnt
252 rm -fr mnt
255 make_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
261 kernel=$KVERSION
263 initdir=mnt
264 . $basedir/dracut-functions
265 dracut_install sh ls shutdown poweroff stty cat ps ln ip \
266 /lib/terminfo/l/linux dmesg mkdir cp ping grep \
267 sleep nbd-server chmod
268 type -P dhcpd >/dev/null && dracut_install dhcpd
269 [ -x /usr/sbin/dhcpd3 ] && inst /usr/sbin/dhcpd3 /usr/sbin/dhcpd
270 inst ./server-init /sbin/init
271 inst ./hosts /etc/hosts
272 inst ./dhcpd.conf /etc/dhcpd.conf
274 cd "$initdir";
275 mkdir -p dev sys proc etc var/run var/lib/dhcpd tmp
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
284 cp -a /etc/ld.so.conf* $initdir/etc
285 sudo ldconfig -r "$initdir"
288 sudo umount mnt
289 rm -fr mnt
292 test_setup() {
294 modinfo nbd &>/dev/null || { echo "Kernel does not support nbd"; exit 1; }
296 make_encrypted_root || return 1
297 make_client_root || return 1
298 make_server_root || return 1
300 # Make the test image
302 initdir=overlay
303 . $basedir/dracut-functions
304 dracut_install poweroff shutdown
305 inst_hook emergency 000 ./hard-off.sh
306 inst_simple ./99-idesymlinks.rules /etc/udev/rules.d/99-idesymlinks.rules
307 inst ./cryptroot-ask /sbin/cryptroot-ask
310 sudo $basedir/dracut -l -i overlay / \
311 -m "dash udev-rules rootfs-block base debug kernel-modules" \
312 -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod e1000" \
313 -f initramfs.server $KVERSION || return 1
315 sudo $basedir/dracut -l -i overlay / \
316 -o "plymouth" \
317 -a "debug" \
318 -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod e1000" \
319 -f initramfs.testing $KVERSION || return 1
322 kill_server() {
323 if [[ -s server.pid ]]; then
324 sudo kill -TERM $(cat server.pid)
325 rm -f server.pid
329 test_cleanup() {
330 kill_server
331 rm -fr overlay mnt
332 rm -f flag.img server.ext2 nbd.ext2 encrypted.ext2
333 rm -f initramfs.server initramfs.testing initramfs.makeroot
336 . $testdir/test-functions