]>
Commit | Line | Data |
---|---|---|
9ecbe2e4 DD |
1 | #!/bin/bash |
2 | TEST_DESCRIPTION="root filesystem on NBD" | |
3 | ||
4 | KVERSION=${KVERSION-$(uname -r)} | |
5 | ||
6 | # Uncomment this to debug failures | |
5db73403 | 7 | #DEBUGFAIL="rdinitdebug rdnetdebug" |
9ecbe2e4 DD |
8 | |
9 | run_server() { | |
10 | # Start server first | |
b2c5f5dc | 11 | echo "NBD TEST SETUP: Starting DHCP/NBD server" |
9ecbe2e4 | 12 | |
8bd5873f | 13 | $testdir/run-qemu -hda server.ext2 -hdb nbd.ext2 -hdc encrypted.ext2 \ |
5831685c | 14 | -m 256M -nographic \ |
9ecbe2e4 | 15 | -net nic,macaddr=52:54:00:12:34:56,model=e1000 \ |
ada0bc3e | 16 | -net socket,mcast=230.0.0.1:1236 \ |
9ecbe2e4 DD |
17 | -serial udp:127.0.0.1:9999 \ |
18 | -kernel /boot/vmlinuz-$KVERSION \ | |
19 | -append "root=/dev/sda rw quiet console=ttyS0,115200n81" \ | |
20 | -initrd initramfs.server -pidfile server.pid -daemonize || return 1 | |
21 | sudo chmod 644 server.pid || return 1 | |
22 | ||
23 | # Cleanup the terminal if we have one | |
24 | tty -s && stty sane | |
25 | ||
26 | echo Sleeping 10 seconds to give the server a head start | |
27 | sleep 10 | |
28 | } | |
29 | ||
30 | client_test() { | |
31 | local test_name="$1" | |
32 | local mac=$2 | |
33 | local cmdline="$3" | |
a29f15a5 DD |
34 | local fstype=$4 |
35 | local fsopt=$5 | |
36 | local found opts nbdinfo | |
37 | ||
38 | [[ $fstype ]] || fstype=ext3 | |
39 | [[ $fsopt ]] || fsopt="errors=continue" | |
9ecbe2e4 DD |
40 | |
41 | echo "CLIENT TEST START: $test_name" | |
42 | ||
43 | # Clear out the flags for each test | |
44 | if ! dd if=/dev/zero of=flag.img bs=1M count=1; then | |
45 | echo "Unable to make client sda image" 1>&2 | |
46 | return 1 | |
47 | fi | |
48 | ||
5831685c | 49 | $testdir/run-qemu -hda flag.img -m 256M -nographic \ |
9ecbe2e4 | 50 | -net nic,macaddr=$mac,model=e1000 \ |
ada0bc3e | 51 | -net socket,mcast=230.0.0.1:1236 \ |
9ecbe2e4 | 52 | -kernel /boot/vmlinuz-$KVERSION \ |
a3a3448d | 53 | -append "$cmdline $DEBUGFAIL rdshell ro quiet console=ttyS0,115200n81" \ |
9ecbe2e4 DD |
54 | -initrd initramfs.testing |
55 | ||
56 | if [[ $? -ne 0 ]] || ! grep -m 1 -q nbd-OK flag.img; then | |
57 | echo "CLIENT TEST END: $test_name [FAILED - BAD EXIT]" | |
58 | return 1 | |
59 | fi | |
60 | ||
a29f15a5 DD |
61 | # nbdinfo=( fstype fsoptions ) |
62 | nbdinfo=($(awk '{print $2, $3; exit}' flag.img)) | |
63 | ||
64 | if [[ "${nbdinfo[0]}" != "$fstype" ]]; then | |
65 | echo "CLIENT TEST END: $test_name [FAILED - WRONG FS TYPE]" | |
66 | return 1 | |
67 | fi | |
68 | ||
69 | opts=${nbdinfo[1]}, | |
70 | while [[ $opts ]]; do | |
9f786a9d | 71 | if [[ ${opts%%,*} = $fsopt ]]; then |
a29f15a5 DD |
72 | found=1 |
73 | break | |
74 | fi | |
75 | opts=${opts#*,} | |
76 | done | |
77 | ||
78 | if [[ ! $found ]]; then | |
79 | echo "CLIENT TEST END: $test_name [FAILED - BAD FS OPTS]" | |
80 | return 1 | |
81 | fi | |
82 | ||
9ecbe2e4 DD |
83 | echo "CLIENT TEST END: $test_name [OK]" |
84 | } | |
85 | ||
86 | test_run() { | |
87 | if ! run_server; then | |
88 | echo "Failed to start server" 1>&2 | |
89 | return 1 | |
90 | fi | |
91 | ||
a29f15a5 DD |
92 | # The default is ext3,errors=continue so use that to determine |
93 | # if our options were parsed and used | |
94 | ||
50e7ff76 PS |
95 | client_test "NBD root=nbd:IP:port" 52:54:00:12:34:00 \ |
96 | "root=nbd:192.168.50.1:2000" || return 1 | |
9ecbe2e4 | 97 | |
50e7ff76 PS |
98 | client_test "NBD root=nbd:IP:port:fstype" 52:54:00:12:34:00 \ |
99 | "root=nbd:192.168.50.1:2000:ext2" ext2 || return 1 | |
a29f15a5 | 100 | |
50e7ff76 PS |
101 | client_test "NBD root=nbd:IP:port::fsopts" 52:54:00:12:34:00 \ |
102 | "root=nbd:192.168.50.1:2000::errors=panic" \ | |
103 | ext3 errors=panic || return 1 | |
a29f15a5 | 104 | |
50e7ff76 PS |
105 | client_test "NBD root=nbd:IP:port:fstype:fsopts" 52:54:00:12:34:00 \ |
106 | "root=nbd:192.168.50.1:2000:ext2:errors=panic" \ | |
107 | ext2 errors=panic || return 1 | |
a29f15a5 | 108 | |
50e7ff76 PS |
109 | # There doesn't seem to be a good way to validate the NBD options, so |
110 | # just check that we don't screw up the other options | |
a29f15a5 | 111 | |
50e7ff76 PS |
112 | client_test "NBD root=nbd:IP:port:::NBD opts" 52:54:00:12:34:00 \ |
113 | "root=nbd:192.168.50.1:2000:::bs=2048" || return 1 | |
a29f15a5 | 114 | |
50e7ff76 PS |
115 | client_test "NBD root=nbd:IP:port:fstype::NBD opts" 52:54:00:12:34:00 \ |
116 | "root=nbd:192.168.50.1:2000:ext2::bs=2048" ext2 || return 1 | |
a29f15a5 | 117 | |
50e7ff76 PS |
118 | client_test "NBD root=nbd:IP:port:fstype:fsopts:NBD opts" \ |
119 | 52:54:00:12:34:00 \ | |
120 | "root=nbd:192.168.50.1:2000:ext2:errors=panic:bs=2048" \ | |
121 | ext2 errors=panic || return 1 | |
a29f15a5 | 122 | |
a29f15a5 DD |
123 | # DHCP root-path parsing |
124 | ||
9ecbe2e4 DD |
125 | client_test "NBD root=dhcp DHCP root-path nbd:srv:port" 52:54:00:12:34:01 \ |
126 | "root=dhcp" || return 1 | |
a29f15a5 DD |
127 | |
128 | client_test "NBD root=dhcp DHCP root-path nbd:srv:port:fstype" \ | |
129 | 52:54:00:12:34:02 "root=dhcp" ext2 || return 1 | |
130 | ||
131 | client_test "NBD root=dhcp DHCP root-path nbd:srv:port::fsopts" \ | |
132 | 52:54:00:12:34:03 "root=dhcp" ext3 errors=panic || return 1 | |
133 | ||
134 | client_test "NBD root=dhcp DHCP root-path nbd:srv:port:fstype:fsopts" \ | |
135 | 52:54:00:12:34:04 "root=dhcp" ext2 errors=panic || return 1 | |
aec48753 DD |
136 | |
137 | # netroot handling | |
138 | ||
139 | client_test "NBD netroot=nbd:IP:port" 52:54:00:12:34:00 \ | |
140 | "netroot=nbd:192.168.50.1:2000" || return 1 | |
141 | ||
142 | client_test "NBD netroot=dhcp DHCP root-path nbd:srv:port:fstype:fsopts" \ | |
143 | 52:54:00:12:34:04 "netroot=dhcp" ext2 errors=panic || return 1 | |
8bd5873f DD |
144 | |
145 | # Encrypted root handling via LVM/LUKS over NBD | |
146 | ||
147 | client_test "NBD root=/dev/dracut/root netroot=nbd:IP:port" \ | |
148 | 52:54:00:12:34:00 \ | |
149 | "root=/dev/dracut/root netroot=nbd:192.168.50.1:2001" || return 1 | |
150 | ||
151 | # XXX This should be ext2,errors=panic but that doesn't currently | |
152 | # XXX work when you have a real root= line in addition to netroot= | |
153 | # XXX How we should work here needs clarification | |
154 | client_test "NBD root=/dev/dracut/root netroot=dhcp (w/ fstype and opts)" \ | |
155 | 52:54:00:12:34:05 \ | |
156 | "root=/dev/dracut/root netroot=dhcp" || return 1 | |
9ca74ffe HH |
157 | |
158 | if [[ -s server.pid ]]; then | |
159 | sudo kill -TERM $(cat server.pid) | |
160 | rm -f server.pid | |
161 | fi | |
162 | ||
8bd5873f DD |
163 | } |
164 | ||
165 | make_encrypted_root() { | |
166 | # Create the blank file to use as a root filesystem | |
167 | dd if=/dev/zero of=encrypted.ext2 bs=1M count=20 | |
168 | dd if=/dev/zero of=flag.img bs=1M count=1 | |
169 | ||
170 | kernel=$KVERSION | |
171 | # Create what will eventually be our root filesystem onto an overlay | |
172 | ( | |
173 | initdir=overlay/source | |
174 | . $basedir/dracut-functions | |
175 | dracut_install sh df free ls shutdown poweroff stty cat ps ln ip \ | |
176 | /lib/terminfo/l/linux mount dmesg mkdir cp ping | |
177 | inst ./client-init /sbin/init | |
178 | find_binary plymouth >/dev/null && dracut_install plymouth | |
179 | (cd "$initdir"; mkdir -p dev sys proc etc var/run tmp ) | |
180 | ) | |
181 | ||
182 | # second, install the files needed to make the root filesystem | |
183 | ( | |
184 | initdir=overlay | |
185 | . $basedir/dracut-functions | |
186 | dracut_install mke2fs poweroff cp umount | |
58dbb43e | 187 | inst_simple ./create-root.sh /initqueue/01create-root.sh |
8bd5873f DD |
188 | ) |
189 | ||
190 | # create an initramfs that will create the target root filesystem. | |
191 | # We do it this way so that we do not risk trashing the host mdraid | |
192 | # devices, volume groups, encrypted partitions, etc. | |
193 | $basedir/dracut -l -i overlay / \ | |
e2dbd86f | 194 | -m "dash crypt lvm mdraid udev-rules base rootfs-block kernel-modules" \ |
8bd5873f DD |
195 | -d "ata_piix ext2 sd_mod" \ |
196 | -f initramfs.makeroot $KVERSION || return 1 | |
197 | rm -rf overlay | |
198 | ||
199 | # Invoke KVM and/or QEMU to actually create the target filesystem. | |
5831685c | 200 | $testdir/run-qemu -hda flag.img -hdb encrypted.ext2 -m 256M \ |
8bd5873f DD |
201 | -nographic -net none \ |
202 | -kernel "/boot/vmlinuz-$kernel" \ | |
203 | -append "root=/dev/dracut/root rw quiet console=ttyS0,115200n81" \ | |
204 | -initrd initramfs.makeroot || return 1 | |
205 | grep -m 1 -q dracut-root-block-created flag.img || return 1 | |
9ecbe2e4 DD |
206 | } |
207 | ||
208 | make_client_root() { | |
209 | dd if=/dev/zero of=nbd.ext2 bs=1M count=30 | |
a29f15a5 | 210 | mke2fs -F -j nbd.ext2 |
9ecbe2e4 DD |
211 | mkdir mnt |
212 | sudo mount -o loop nbd.ext2 mnt | |
213 | ||
214 | kernel=$KVERSION | |
215 | ( | |
216 | initdir=mnt | |
217 | . $basedir/dracut-functions | |
218 | dracut_install sh ls shutdown poweroff stty cat ps ln ip \ | |
a29f15a5 | 219 | /lib/terminfo/l/linux dmesg mkdir cp ping |
9ecbe2e4 DD |
220 | inst ./client-init /sbin/init |
221 | ( | |
222 | cd "$initdir"; | |
223 | mkdir -p dev sys proc etc var/run tmp | |
224 | ) | |
225 | inst /etc/nsswitch.conf /etc/nsswitch.conf | |
226 | inst /etc/passwd /etc/passwd | |
227 | inst /etc/group /etc/group | |
228 | for i in /lib*/libnss_files**;do | |
229 | inst_library $i | |
230 | done | |
231 | ||
232 | ldconfig -n -r "$initdir" /lib* /usr/lib* | |
233 | ) | |
234 | ||
235 | sudo umount mnt | |
236 | rm -fr mnt | |
237 | } | |
238 | ||
239 | make_server_root() { | |
240 | dd if=/dev/zero of=server.ext2 bs=1M count=30 | |
241 | mke2fs -F server.ext2 | |
242 | mkdir mnt | |
243 | sudo mount -o loop server.ext2 mnt | |
244 | ||
245 | kernel=$KVERSION | |
246 | ( | |
247 | initdir=mnt | |
248 | . $basedir/dracut-functions | |
249 | dracut_install sh ls shutdown poweroff stty cat ps ln ip \ | |
45630db1 PS |
250 | /lib/terminfo/l/linux dmesg mkdir cp ping grep \ |
251 | sleep nbd-server chmod | |
252 | which dhcpd >/dev/null 2>&1 && dracut_install dhcpd | |
253 | [ -x /usr/sbin/dhcpd3 ] && inst /usr/sbin/dhcpd3 /usr/sbin/dhcpd | |
9ecbe2e4 DD |
254 | inst ./server-init /sbin/init |
255 | inst ./hosts /etc/hosts | |
256 | inst ./dhcpd.conf /etc/dhcpd.conf | |
257 | ( | |
258 | cd "$initdir"; | |
259 | mkdir -p dev sys proc etc var/run var/lib/dhcpd tmp | |
260 | ) | |
261 | inst /etc/nsswitch.conf /etc/nsswitch.conf | |
262 | inst /etc/passwd /etc/passwd | |
263 | inst /etc/group /etc/group | |
264 | for i in /lib*/libnss_files**;do | |
265 | inst_library $i | |
266 | done | |
267 | ||
268 | ldconfig -n -r "$initdir" /lib* /usr/lib* | |
269 | ) | |
270 | ||
271 | sudo umount mnt | |
272 | rm -fr mnt | |
273 | } | |
274 | ||
275 | test_setup() { | |
8bd5873f | 276 | make_encrypted_root || return 1 |
9ecbe2e4 DD |
277 | make_client_root || return 1 |
278 | make_server_root || return 1 | |
279 | ||
280 | # Make the test image | |
281 | ( | |
282 | initdir=overlay | |
283 | . $basedir/dracut-functions | |
284 | dracut_install poweroff shutdown | |
285 | inst_simple ./hard-off.sh /emergency/01hard-off.sh | |
8bd5873f | 286 | inst ./cryptroot-ask /sbin/cryptroot-ask |
9ecbe2e4 DD |
287 | ) |
288 | ||
289 | sudo $basedir/dracut -l -i overlay / \ | |
e2dbd86f | 290 | -m "dash udev-rules rootfs-block base debug kernel-modules" \ |
9ecbe2e4 DD |
291 | -d "ata_piix ext2 sd_mod e1000" \ |
292 | -f initramfs.server $KVERSION || return 1 | |
293 | ||
294 | sudo $basedir/dracut -l -i overlay / \ | |
5db73403 HH |
295 | -o "plymouth" \ |
296 | -a "debug" \ | |
a29f15a5 | 297 | -d "ata_piix ext2 ext3 sd_mod e1000" \ |
9ecbe2e4 DD |
298 | -f initramfs.testing $KVERSION || return 1 |
299 | } | |
300 | ||
301 | test_cleanup() { | |
302 | if [[ -s server.pid ]]; then | |
303 | sudo kill -TERM $(cat server.pid) | |
304 | rm -f server.pid | |
305 | fi | |
306 | rm -fr overlay mnt | |
8bd5873f DD |
307 | rm -f flag.img server.ext2 nbd.ext2 encrypted.ext2 |
308 | rm -f initramfs.server initramfs.testing initramfs.makeroot | |
9ecbe2e4 DD |
309 | } |
310 | ||
311 | . $testdir/test-functions |