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