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