]> git.ipfire.org Git - thirdparty/dracut.git/blob - test/TEST-40-NBD/test.sh
netroot: Introduce detailed cmdline parsers
[thirdparty/dracut.git] / test / TEST-40-NBD / test.sh
1 #!/bin/bash
2 TEST_DESCRIPTION="root filesystem on NBD"
3
4 KVERSION=${KVERSION-$(uname -r)}
5
6 # Uncomment this to debug failures
7 #DEBUGFAIL="rdinitdebug rdnetdebug rdbreak"
8
9 run_server() {
10 # Start server first
11 echo "NBD TEST SETUP: Starting DHCP/NBD server"
12
13 $testdir/run-qemu -hda server.ext2 -hdb nbd.ext2 -hdc encrypted.ext2 \
14 -m 512M -nographic \
15 -net nic,macaddr=52:54:00:12:34:56,model=e1000 \
16 -net socket,mcast=230.0.0.1:1234 \
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"
34 local fstype=$4
35 local fsopt=$5
36 local found opts nbdinfo
37
38 [[ $fstype ]] || fstype=ext3
39 [[ $fsopt ]] || fsopt="errors=continue"
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
49 $testdir/run-qemu -hda flag.img -m 512M -nographic \
50 -net nic,macaddr=$mac,model=e1000 \
51 -net socket,mcast=230.0.0.1:1234 \
52 -kernel /boot/vmlinuz-$KVERSION \
53 -append "$cmdline $DEBUGFAIL ro quiet console=ttyS0,115200n81" \
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
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
71 if [[ ${opts%%,*} = $fsopt ]]; then
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
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
92 # The default is ext3,errors=continue so use that to determine
93 # if our options were parsed and used
94
95 client_test "NBD root=nbd:IP:port" 52:54:00:12:34:00 \
96 "root=nbd:192.168.50.1:2000" || return 1
97
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
100
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
104
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
108
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
111
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
114
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
117
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
122
123 # Check legacy parsing
124
125 client_test "NBD root=nbd nbdroot=srv:port" 52:54:00:12:34:00 \
126 "root=nbd nbdroot=192.168.50.1:2000" || return 1
127
128 # This test must fail: root=dhcp ignores nbdroot
129 client_test "NBD root=dhcp nbdroot=srv:port" 52:54:00:12:34:00 \
130 "root=dhcp nbdroot=192.168.50.1:2000" && return 1
131
132 client_test "NBD root=nbd nbdroot=srv,port" 52:54:00:12:34:00 \
133 "root=nbd nbdroot=192.168.50.1,2000" || return 1
134
135 # This test must fail: root=dhcp ignores nbdroot
136 client_test "NBD root=dhcp nbdroot=srv,port" 52:54:00:12:34:00 \
137 "root=dhcp nbdroot=192.168.50.1,2000" && return 1
138
139 # DHCP root-path parsing
140
141 client_test "NBD root=dhcp DHCP root-path nbd:srv:port" 52:54:00:12:34:01 \
142 "root=dhcp" || return 1
143
144 client_test "NBD root=dhcp DHCP root-path nbd:srv:port:fstype" \
145 52:54:00:12:34:02 "root=dhcp" ext2 || return 1
146
147 client_test "NBD root=dhcp DHCP root-path nbd:srv:port::fsopts" \
148 52:54:00:12:34:03 "root=dhcp" ext3 errors=panic || return 1
149
150 client_test "NBD root=dhcp DHCP root-path nbd:srv:port:fstype:fsopts" \
151 52:54:00:12:34:04 "root=dhcp" ext2 errors=panic || return 1
152
153 # netroot handling
154
155 client_test "NBD netroot=nbd:IP:port" 52:54:00:12:34:00 \
156 "netroot=nbd:192.168.50.1:2000" || return 1
157
158 client_test "NBD netroot=dhcp DHCP root-path nbd:srv:port:fstype:fsopts" \
159 52:54:00:12:34:04 "netroot=dhcp" ext2 errors=panic || return 1
160
161 # Encrypted root handling via LVM/LUKS over NBD
162
163 client_test "NBD root=/dev/dracut/root netroot=nbd:IP:port" \
164 52:54:00:12:34:00 \
165 "root=/dev/dracut/root netroot=nbd:192.168.50.1:2001" || return 1
166
167 # XXX This should be ext2,errors=panic but that doesn't currently
168 # XXX work when you have a real root= line in addition to netroot=
169 # XXX How we should work here needs clarification
170 client_test "NBD root=/dev/dracut/root netroot=dhcp (w/ fstype and opts)" \
171 52:54:00:12:34:05 \
172 "root=/dev/dracut/root netroot=dhcp" || return 1
173 }
174
175 make_encrypted_root() {
176 # Create the blank file to use as a root filesystem
177 dd if=/dev/zero of=encrypted.ext2 bs=1M count=20
178 dd if=/dev/zero of=flag.img bs=1M count=1
179
180 kernel=$KVERSION
181 # Create what will eventually be our root filesystem onto an overlay
182 (
183 initdir=overlay/source
184 . $basedir/dracut-functions
185 dracut_install sh df free ls shutdown poweroff stty cat ps ln ip \
186 /lib/terminfo/l/linux mount dmesg mkdir cp ping
187 inst ./client-init /sbin/init
188 find_binary plymouth >/dev/null && dracut_install plymouth
189 (cd "$initdir"; mkdir -p dev sys proc etc var/run tmp )
190 )
191
192 # second, install the files needed to make the root filesystem
193 (
194 initdir=overlay
195 . $basedir/dracut-functions
196 dracut_install mke2fs poweroff cp umount
197 inst_simple ./create-root.sh /pre-mount/01create-root.sh
198 )
199
200 # create an initramfs that will create the target root filesystem.
201 # We do it this way so that we do not risk trashing the host mdraid
202 # devices, volume groups, encrypted partitions, etc.
203 $basedir/dracut -l -i overlay / \
204 -m "dash crypt lvm mdraid udev-rules base rootfs-block" \
205 -d "ata_piix ext2 sd_mod" \
206 -f initramfs.makeroot $KVERSION || return 1
207 rm -rf overlay
208
209 # Invoke KVM and/or QEMU to actually create the target filesystem.
210 $testdir/run-qemu -hda flag.img -hdb encrypted.ext2 -m 512M \
211 -nographic -net none \
212 -kernel "/boot/vmlinuz-$kernel" \
213 -append "root=/dev/dracut/root rw quiet console=ttyS0,115200n81" \
214 -initrd initramfs.makeroot || return 1
215 grep -m 1 -q dracut-root-block-created flag.img || return 1
216 }
217
218 make_client_root() {
219 dd if=/dev/zero of=nbd.ext2 bs=1M count=30
220 mke2fs -F -j nbd.ext2
221 mkdir mnt
222 sudo mount -o loop nbd.ext2 mnt
223
224 kernel=$KVERSION
225 (
226 initdir=mnt
227 . $basedir/dracut-functions
228 dracut_install sh ls shutdown poweroff stty cat ps ln ip \
229 /lib/terminfo/l/linux dmesg mkdir cp ping
230 inst ./client-init /sbin/init
231 (
232 cd "$initdir";
233 mkdir -p dev sys proc etc var/run tmp
234 )
235 inst /etc/nsswitch.conf /etc/nsswitch.conf
236 inst /etc/passwd /etc/passwd
237 inst /etc/group /etc/group
238 for i in /lib*/libnss_files**;do
239 inst_library $i
240 done
241
242 ldconfig -n -r "$initdir" /lib* /usr/lib*
243 )
244
245 sudo umount mnt
246 rm -fr mnt
247 }
248
249 make_server_root() {
250 dd if=/dev/zero of=server.ext2 bs=1M count=30
251 mke2fs -F server.ext2
252 mkdir mnt
253 sudo mount -o loop server.ext2 mnt
254
255 kernel=$KVERSION
256 (
257 initdir=mnt
258 . $basedir/dracut-functions
259 dracut_install sh ls shutdown poweroff stty cat ps ln ip \
260 /lib/terminfo/l/linux dmesg mkdir cp ping grep \
261 sleep nbd-server chmod
262 which dhcpd >/dev/null 2>&1 && dracut_install dhcpd
263 [ -x /usr/sbin/dhcpd3 ] && inst /usr/sbin/dhcpd3 /usr/sbin/dhcpd
264 inst ./server-init /sbin/init
265 inst ./hosts /etc/hosts
266 inst ./dhcpd.conf /etc/dhcpd.conf
267 (
268 cd "$initdir";
269 mkdir -p dev sys proc etc var/run var/lib/dhcpd tmp
270 )
271 inst /etc/nsswitch.conf /etc/nsswitch.conf
272 inst /etc/passwd /etc/passwd
273 inst /etc/group /etc/group
274 for i in /lib*/libnss_files**;do
275 inst_library $i
276 done
277
278 ldconfig -n -r "$initdir" /lib* /usr/lib*
279 )
280
281 sudo umount mnt
282 rm -fr mnt
283 }
284
285 test_setup() {
286 make_encrypted_root || return 1
287 make_client_root || return 1
288 make_server_root || return 1
289
290 # Make the test image
291 (
292 initdir=overlay
293 . $basedir/dracut-functions
294 dracut_install poweroff shutdown
295 inst_simple ./hard-off.sh /emergency/01hard-off.sh
296 inst ./cryptroot-ask /sbin/cryptroot-ask
297 )
298
299 sudo $basedir/dracut -l -i overlay / \
300 -m "dash udev-rules rootfs-block base debug" \
301 -d "ata_piix ext2 sd_mod e1000" \
302 -f initramfs.server $KVERSION || return 1
303
304 sudo $basedir/dracut -l -i overlay / \
305 -m "dash crypt lvm mdraid udev-rules base rootfs-block nbd debug" \
306 -d "ata_piix ext2 ext3 sd_mod e1000" \
307 -f initramfs.testing $KVERSION || return 1
308 }
309
310 test_cleanup() {
311 if [[ -s server.pid ]]; then
312 sudo kill -TERM $(cat server.pid)
313 rm -f server.pid
314 fi
315 rm -fr overlay mnt
316 rm -f flag.img server.ext2 nbd.ext2 encrypted.ext2
317 rm -f initramfs.server initramfs.testing initramfs.makeroot
318 }
319
320 . $testdir/test-functions