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