]>
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 \ |
8eb16b08 | 26 | -append "root=/dev/sda 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 | |
55 | ||
0be1785a HH |
56 | $testdir/run-qemu \ |
57 | -hda $TESTDIR/flag.img \ | |
58 | -m 256M -nographic \ | |
9ecbe2e4 | 59 | -net nic,macaddr=$mac,model=e1000 \ |
0be1785a | 60 | -net socket,connect=127.0.0.1:12340 \ |
9ecbe2e4 | 61 | -kernel /boot/vmlinuz-$KVERSION \ |
28f0b27f | 62 | -append "$cmdline $DEBUGFAIL rd.debug rd.info rd.retry=10 ro quiet console=ttyS0,115200n81 selinux=0" \ |
0be1785a | 63 | -initrd $TESTDIR/initramfs.testing |
9ecbe2e4 | 64 | |
0be1785a | 65 | if [[ $? -ne 0 ]] || ! grep -m 1 -q nbd-OK $TESTDIR/flag.img; then |
9ecbe2e4 DD |
66 | echo "CLIENT TEST END: $test_name [FAILED - BAD EXIT]" |
67 | return 1 | |
68 | fi | |
69 | ||
a29f15a5 | 70 | # nbdinfo=( fstype fsoptions ) |
0be1785a | 71 | nbdinfo=($(awk '{print $2, $3; exit}' $TESTDIR/flag.img)) |
a29f15a5 DD |
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 | |
9f786a9d | 80 | if [[ ${opts%%,*} = $fsopt ]]; then |
a29f15a5 DD |
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 | ||
9ecbe2e4 DD |
92 | echo "CLIENT TEST END: $test_name [OK]" |
93 | } | |
94 | ||
95 | test_run() { | |
ebcfda6c | 96 | modinfo nbd &>/dev/null || { echo "Kernel does not support nbd"; exit 1; } |
9ecbe2e4 DD |
97 | if ! run_server; then |
98 | echo "Failed to start server" 1>&2 | |
99 | return 1 | |
100 | fi | |
97add1b3 HH |
101 | client_run || { kill_server; return 1; } |
102 | } | |
103 | ||
104 | client_run() { | |
9ecbe2e4 | 105 | |
a29f15a5 DD |
106 | # The default is ext3,errors=continue so use that to determine |
107 | # if our options were parsed and used | |
bcf94bba | 108 | client_test "NBD root=nbd:IP:port" 52:54:00:12:34:00 \ |
50e7ff76 | 109 | "root=nbd:192.168.50.1:2000" || return 1 |
9ecbe2e4 | 110 | |
bcf94bba | 111 | client_test "NBD root=nbd:IP:port:fstype" 52:54:00:12:34:00 \ |
50e7ff76 | 112 | "root=nbd:192.168.50.1:2000:ext2" ext2 || return 1 |
a29f15a5 | 113 | |
bcf94bba | 114 | client_test "NBD root=nbd:IP:port::fsopts" 52:54:00:12:34:00 \ |
50e7ff76 PS |
115 | "root=nbd:192.168.50.1:2000::errors=panic" \ |
116 | ext3 errors=panic || return 1 | |
a29f15a5 | 117 | |
bcf94bba | 118 | client_test "NBD root=nbd:IP:port:fstype:fsopts" 52:54:00:12:34:00 \ |
50e7ff76 PS |
119 | "root=nbd:192.168.50.1:2000:ext2:errors=panic" \ |
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 \ |
beb097d9 WT |
123 | "root=nbd:192.168.50.1:2000:ext2:errors=panic bridge" \ |
124 | ext2 errors=panic || return 1 | |
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 \ |
50e7ff76 | 130 | "root=nbd:192.168.50.1:2000:::bs=2048" || return 1 |
a29f15a5 | 131 | |
bcf94bba | 132 | client_test "NBD root=nbd:IP:port:fstype::NBD opts" 52:54:00:12:34:00 \ |
50e7ff76 | 133 | "root=nbd:192.168.50.1:2000:ext2::bs=2048" ext2 || return 1 |
a29f15a5 | 134 | |
bcf94bba | 135 | client_test "NBD root=nbd:IP:port:fstype:fsopts:NBD opts" \ |
50e7ff76 PS |
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 | |
a29f15a5 | 139 | |
a29f15a5 DD |
140 | # DHCP root-path parsing |
141 | ||
9ecbe2e4 DD |
142 | client_test "NBD root=dhcp DHCP root-path nbd:srv:port" 52:54:00:12:34:01 \ |
143 | "root=dhcp" || return 1 | |
a29f15a5 | 144 | |
beb097d9 WT |
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 | ||
a29f15a5 DD |
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 | |
aec48753 DD |
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 | |
8bd5873f DD |
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 | |
9ca74ffe HH |
177 | |
178 | if [[ -s server.pid ]]; then | |
0be1785a HH |
179 | sudo kill -TERM $(cat $TESTDIR/server.pid) |
180 | rm -f $TESTDIR/server.pid | |
9ca74ffe HH |
181 | fi |
182 | ||
8bd5873f DD |
183 | } |
184 | ||
185 | make_encrypted_root() { | |
186 | # Create the blank file to use as a root filesystem | |
0be1785a HH |
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 | |
8bd5873f DD |
189 | |
190 | kernel=$KVERSION | |
191 | # Create what will eventually be our root filesystem onto an overlay | |
192 | ( | |
0be1785a | 193 | initdir=$TESTDIR/overlay/source |
28f0b27f HH |
194 | mkdir -p "$initdir" |
195 | (cd "$initdir"; mkdir -p dev sys proc etc var/run tmp ) | |
552ecca6 | 196 | . $basedir/dracut-functions.sh |
8bd5873f | 197 | dracut_install sh df free ls shutdown poweroff stty cat ps ln ip \ |
96d22bd7 HH |
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 | |
552ecca6 | 203 | inst ./client-init.sh /sbin/init |
8bd5873f | 204 | find_binary plymouth >/dev/null && dracut_install plymouth |
28f0b27f HH |
205 | cp -a /etc/ld.so.conf* $initdir/etc |
206 | sudo ldconfig -r "$initdir" | |
8bd5873f DD |
207 | ) |
208 | ||
209 | # second, install the files needed to make the root filesystem | |
210 | ( | |
0be1785a | 211 | initdir=$TESTDIR/overlay |
552ecca6 | 212 | . $basedir/dracut-functions.sh |
75e8f476 | 213 | dracut_install mke2fs poweroff cp umount tune2fs |
0b53ca70 | 214 | inst_hook initqueue 01 ./create-root.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 | |
9ecbe2e4 DD |
237 | } |
238 | ||
239 | make_client_root() { | |
0be1785a HH |
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 | |
9ecbe2e4 DD |
244 | |
245 | kernel=$KVERSION | |
246 | ( | |
0be1785a | 247 | initdir=$TESTDIR/mnt |
552ecca6 | 248 | . $basedir/dracut-functions.sh |
28f0b27f HH |
249 | mkdir -p "$initdir" |
250 | (cd "$initdir"; mkdir -p dev sys proc etc var/run tmp ) | |
9ecbe2e4 | 251 | dracut_install sh ls shutdown poweroff stty cat ps ln ip \ |
96d22bd7 HH |
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 | |
552ecca6 | 257 | inst ./client-init.sh /sbin/init |
9ecbe2e4 DD |
258 | inst /etc/nsswitch.conf /etc/nsswitch.conf |
259 | inst /etc/passwd /etc/passwd | |
260 | inst /etc/group /etc/group | |
28f0b27f HH |
261 | for i in /usr/lib*/libnss_files* /lib*/libnss_files*;do |
262 | [ -e "$i" ] || continue | |
263 | inst $i | |
9ecbe2e4 | 264 | done |
8a080127 PS |
265 | cp -a /etc/ld.so.conf* $initdir/etc |
266 | sudo ldconfig -r "$initdir" | |
9ecbe2e4 DD |
267 | ) |
268 | ||
0be1785a HH |
269 | sudo umount $TESTDIR/mnt |
270 | rm -fr $TESTDIR/mnt | |
9ecbe2e4 DD |
271 | } |
272 | ||
273 | make_server_root() { | |
0be1785a HH |
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 | |
9ecbe2e4 DD |
278 | |
279 | kernel=$KVERSION | |
280 | ( | |
0be1785a | 281 | initdir=$TESTDIR/mnt |
552ecca6 | 282 | . $basedir/dracut-functions.sh |
28f0b27f HH |
283 | mkdir -p "$initdir" |
284 | ( | |
285 | cd "$initdir"; | |
286 | mkdir -p dev sys proc etc var/run var/lib/dhcpd tmp | |
287 | ) | |
9ecbe2e4 | 288 | dracut_install sh ls shutdown poweroff stty cat ps ln ip \ |
96d22bd7 | 289 | dmesg mkdir cp ping grep \ |
45630db1 | 290 | sleep nbd-server chmod |
96d22bd7 HH |
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 | |
f3af7bd6 | 295 | type -P dhcpd >/dev/null && dracut_install dhcpd |
45630db1 | 296 | [ -x /usr/sbin/dhcpd3 ] && inst /usr/sbin/dhcpd3 /usr/sbin/dhcpd |
552ecca6 | 297 | inst ./server-init.sh /sbin/init |
9ecbe2e4 DD |
298 | inst ./hosts /etc/hosts |
299 | inst ./dhcpd.conf /etc/dhcpd.conf | |
9ecbe2e4 DD |
300 | inst /etc/nsswitch.conf /etc/nsswitch.conf |
301 | inst /etc/passwd /etc/passwd | |
302 | inst /etc/group /etc/group | |
28f0b27f HH |
303 | for i in /usr/lib*/libnss_files* /lib*/libnss_files*;do |
304 | [ -e "$i" ] || continue | |
305 | inst $i | |
9ecbe2e4 DD |
306 | done |
307 | ||
8a080127 PS |
308 | cp -a /etc/ld.so.conf* $initdir/etc |
309 | sudo ldconfig -r "$initdir" | |
9ecbe2e4 DD |
310 | ) |
311 | ||
0be1785a HH |
312 | sudo umount $TESTDIR/mnt |
313 | rm -fr $TESTDIR/mnt | |
9ecbe2e4 DD |
314 | } |
315 | ||
316 | test_setup() { | |
ebcfda6c HH |
317 | |
318 | modinfo nbd &>/dev/null || { echo "Kernel does not support nbd"; exit 1; } | |
319 | ||
8bd5873f | 320 | make_encrypted_root || return 1 |
9ecbe2e4 DD |
321 | make_client_root || return 1 |
322 | make_server_root || return 1 | |
323 | ||
324 | # Make the test image | |
325 | ( | |
0be1785a | 326 | initdir=$TESTDIR/overlay |
552ecca6 | 327 | . $basedir/dracut-functions.sh |
9ecbe2e4 | 328 | dracut_install poweroff shutdown |
0b53ca70 | 329 | inst_hook emergency 000 ./hard-off.sh |
778d2ba2 | 330 | inst_simple ./99-idesymlinks.rules /etc/udev/rules.d/99-idesymlinks.rules |
552ecca6 | 331 | inst ./cryptroot-ask.sh /sbin/cryptroot-ask |
9ecbe2e4 DD |
332 | ) |
333 | ||
552ecca6 | 334 | sudo $basedir/dracut.sh -l -i $TESTDIR/overlay / \ |
e2dbd86f | 335 | -m "dash udev-rules rootfs-block base debug kernel-modules" \ |
778d2ba2 | 336 | -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod e1000" \ |
0be1785a | 337 | -f $TESTDIR/initramfs.server $KVERSION || return 1 |
9ecbe2e4 | 338 | |
552ecca6 | 339 | sudo $basedir/dracut.sh -l -i $TESTDIR/overlay / \ |
5db73403 HH |
340 | -o "plymouth" \ |
341 | -a "debug" \ | |
778d2ba2 | 342 | -d "piix ide-gd_mod ata_piix ext2 ext3 sd_mod e1000" \ |
0be1785a | 343 | -f $TESTDIR/initramfs.testing $KVERSION || return 1 |
9ecbe2e4 DD |
344 | } |
345 | ||
97add1b3 | 346 | kill_server() { |
0be1785a HH |
347 | if [[ -s $TESTDIR/server.pid ]]; then |
348 | sudo kill -TERM $(cat $TESTDIR/server.pid) | |
349 | rm -f $TESTDIR/server.pid | |
9ecbe2e4 | 350 | fi |
97add1b3 HH |
351 | } |
352 | ||
353 | test_cleanup() { | |
354 | kill_server | |
9ecbe2e4 DD |
355 | } |
356 | ||
357 | . $testdir/test-functions |