]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/TEST-64-UDEV-STORAGE/test.sh
test-64-udev-storage: partition disk inside testcase - testcase_multipath_basic_failover
[thirdparty/systemd.git] / test / TEST-64-UDEV-STORAGE / test.sh
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 # vi: ts=4 sw=4 tw=0 et:
4 #
5 # TODO:
6 # * SW raid (mdadm)
7 # * MD (mdadm) -> dm-crypt -> LVM
8 # * iSCSI -> dm-crypt -> LVM
9 set -e
10
11 TEST_DESCRIPTION="systemd-udev storage tests"
12 TEST_NO_NSPAWN=1
13 # Save only journals of failing test cases by default (to conserve space)
14 TEST_SAVE_JOURNAL="${TEST_SAVE_JOURNAL:-fail}"
15
16 # shellcheck source=test/test-functions
17 . "${TEST_BASE_DIR:?}/test-functions"
18
19 USER_QEMU_OPTIONS="${QEMU_OPTIONS:-}"
20 USER_KERNEL_APPEND="${KERNEL_APPEND:-}"
21
22 _host_has_feature() {(
23 set -e
24
25 case "${1:?}" in
26 btrfs)
27 host_has_btrfs
28 ;;
29 iscsi)
30 # Client/initiator (Open-iSCSI)
31 command -v iscsiadm && command -v iscsid || return $?
32 # Server/target (TGT)
33 command -v tgtadm && command -v tgtd || return $?
34 ;;
35 lvm)
36 command -v lvm || return $?
37 ;;
38 mdadm)
39 host_has_mdadm
40 ;;
41 multipath)
42 command -v multipath && command -v multipathd || return $?
43 ;;
44 *)
45 echo >&2 "ERROR: Unknown feature '$1'"
46 # Make this a hard error to distinguish an invalid feature from
47 # a missing feature
48 exit 1
49 esac
50 )}
51
52 test_append_files() {(
53 local feature
54 # An associative array of requested (but optional) features and their
55 # respective "handlers" from test/test-functions
56 #
57 # Note: we install cryptsetup unconditionally, hence it's not explicitly
58 # checked for here
59 local -A features=(
60 [btrfs]=install_btrfs
61 [iscsi]=install_iscsi
62 [lvm]=install_lvm
63 [mdadm]=install_mdadm
64 [multipath]=install_multipath
65 )
66
67 instmods "=block" "=md" "=nvme" "=scsi"
68 install_dmevent
69 image_install lsblk swapoff swapon wc wipefs
70
71 # Install the optional features if the host has the respective tooling
72 for feature in "${!features[@]}"; do
73 if _host_has_feature "$feature"; then
74 "${features[$feature]}"
75 fi
76 done
77
78 generate_module_dependencies
79
80 for i in {0..127}; do
81 dd if=/dev/zero of="${TESTDIR:?}/disk$i.img" bs=1M count=1
82 echo "device$i" >"${TESTDIR:?}/disk$i.img"
83 done
84 )}
85
86 _image_cleanup() {
87 mount_initdir
88 # Clean up certain "problematic" files which may be left over by failing tests
89 : >"${initdir:?}/etc/fstab"
90 : >"${initdir:?}/etc/crypttab"
91 # Clear previous assignment
92 QEMU_OPTIONS_ARRAY=()
93 }
94
95 test_run_one() {
96 local test_id="${1:?}"
97
98 if run_qemu "$test_id"; then
99 check_result_qemu || { echo "qemu test failed"; return 1; }
100 fi
101
102 return 0
103 }
104
105 test_run() {
106 local test_id="${1:?}"
107 local passed=()
108 local failed=()
109 local skipped=()
110 local ec state
111
112 mount_initdir
113
114 if get_bool "${TEST_NO_QEMU:=}" || ! find_qemu_bin; then
115 dwarn "can't run qemu, skipping"
116 return 0
117 fi
118
119 # Execute each currently defined function starting with "testcase_"
120 for testcase in "${TESTCASES[@]}"; do
121 _image_cleanup
122 echo "------ $testcase: BEGIN ------"
123 # Note for my future frustrated self: `fun && xxx` (as well as ||, if, while,
124 # until, etc.) _DISABLES_ the `set -e` behavior in _ALL_ nested function
125 # calls made from `fun()`, i.e. the function _CONTINUES_ even when a called
126 # command returned non-zero EC. That may unexpectedly hide failing commands
127 # if not handled properly. See: bash(1) man page, `set -e` section.
128 #
129 # So, be careful when adding clean up snippets in the testcase_*() functions -
130 # if the `test_run_one()` function isn't the last command, you have propagate
131 # the exit code correctly (e.g. `test_run_one() || return $?`, see below).
132 ec=0
133 "$testcase" "$test_id" || ec=$?
134 case $ec in
135 0)
136 passed+=("$testcase")
137 state="PASS"
138 ;;
139 77)
140 skipped+=("$testcase")
141 state="SKIP"
142 ;;
143 *)
144 failed+=("$testcase")
145 state="FAIL"
146 esac
147 echo "------ $testcase: END ($state) ------"
148 done
149
150 echo "Passed tests: ${#passed[@]}"
151 printf " * %s\n" "${passed[@]}"
152 echo "Skipped tests: ${#skipped[@]}"
153 printf " * %s\n" "${skipped[@]}"
154 echo "Failed tests: ${#failed[@]}"
155 printf " * %s\n" "${failed[@]}"
156
157 [[ ${#failed[@]} -eq 0 ]] || return 1
158
159 return 0
160 }
161
162 testcase_megasas2_basic() {
163 if ! "${QEMU_BIN:?}" -device help | grep 'name "megasas-gen2"'; then
164 echo "megasas-gen2 device driver is not available, skipping test..."
165 return 77
166 fi
167
168 local i
169 local qemu_opts=(
170 "-device megasas-gen2,id=scsi0"
171 "-device megasas-gen2,id=scsi1"
172 "-device megasas-gen2,id=scsi2"
173 "-device megasas-gen2,id=scsi3"
174 )
175
176 for i in {0..127}; do
177 # Add 128 drives, 32 per bus
178 qemu_opts+=(
179 "-device scsi-hd,drive=drive$i,bus=scsi$((i / 32)).0,channel=0,scsi-id=$((i % 32)),lun=0"
180 "-drive format=raw,cache=unsafe,file=${TESTDIR:?}/disk$i.img,if=none,id=drive$i"
181 )
182 done
183
184 KERNEL_APPEND="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
185 QEMU_OPTIONS="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
186 test_run_one "${1:?}"
187 }
188
189 testcase_nvme_basic() {
190 if ! "${QEMU_BIN:?}" -device help | grep 'name "nvme"'; then
191 echo "nvme device driver is not available, skipping test..."
192 return 77
193 fi
194
195 local i
196 local qemu_opts=()
197
198 for (( i = 0; i < 5; i++ )); do
199 qemu_opts+=(
200 "-device" "nvme,drive=nvme$i,serial=deadbeef$i,num_queues=8"
201 "-drive" "format=raw,cache=unsafe,file=${TESTDIR:?}/disk$i.img,if=none,id=nvme$i"
202 )
203 done
204 for (( i = 5; i < 10; i++ )); do
205 qemu_opts+=(
206 "-device" "nvme,drive=nvme$i,serial= deadbeef $i ,num_queues=8"
207 "-drive" "format=raw,cache=unsafe,file=${TESTDIR:?}/disk$i.img,if=none,id=nvme$i"
208 )
209 done
210 for (( i = 10; i < 15; i++ )); do
211 qemu_opts+=(
212 "-device" "nvme,drive=nvme$i,serial= dead/beef/$i ,num_queues=8"
213 "-drive" "format=raw,cache=unsafe,file=${TESTDIR:?}/disk$i.img,if=none,id=nvme$i"
214 )
215 done
216 for (( i = 15; i < 20; i++ )); do
217 qemu_opts+=(
218 "-device" "nvme,drive=nvme$i,serial=dead/../../beef/$i,num_queues=8"
219 "-drive" "format=raw,cache=unsafe,file=${TESTDIR:?}/disk$i.img,if=none,id=nvme$i"
220 )
221 done
222
223 KERNEL_APPEND="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
224 QEMU_OPTIONS="${USER_QEMU_OPTIONS}"
225 QEMU_OPTIONS_ARRAY=("${qemu_opts[@]}")
226 test_run_one "${1:?}"
227 }
228
229 # Testcase for:
230 # * https://github.com/systemd/systemd/pull/24748
231 # * https://github.com/systemd/systemd/pull/24766
232 # * https://github.com/systemd/systemd/pull/24946
233 # Docs: https://qemu.readthedocs.io/en/latest/system/devices/nvme.html#nvm-subsystems
234 testcase_nvme_subsystem() {
235 if ! "${QEMU_BIN:?}" -device help | grep 'name "nvme-subsys"'; then
236 echo "nvme-subsystem device driver is not available, skipping test..."
237 return 77
238 fi
239
240 local i
241 local qemu_opts=(
242 # Create an NVM Subsystem Device
243 "-device nvme-subsys,id=nvme-subsys-64,nqn=subsys64"
244 # Attach two NVM controllers to it
245 "-device nvme,subsys=nvme-subsys-64,serial=deadbeef"
246 "-device nvme,subsys=nvme-subsys-64,serial=deadbeef"
247 # And create two shared namespaces attached to both controllers
248 "-device nvme-ns,drive=nvme0,nsid=16,shared=on"
249 "-drive format=raw,cache=unsafe,file=${TESTDIR:?}/disk0.img,if=none,id=nvme0"
250 "-device nvme-ns,drive=nvme1,nsid=17,shared=on"
251 "-drive format=raw,cache=unsafe,file=${TESTDIR:?}/disk1.img,if=none,id=nvme1"
252 )
253
254 KERNEL_APPEND="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
255 QEMU_OPTIONS="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
256 test_run_one "${1:?}"
257 }
258
259 # Test for issue https://github.com/systemd/systemd/issues/20212
260 testcase_virtio_scsi_identically_named_partitions() {
261
262 if ! "${QEMU_BIN:?}" -device help | grep 'name "virtio-scsi-pci"'; then
263 echo "virtio-scsi-pci device driver is not available, skipping test..."
264 return 77
265 fi
266
267 # Create 16 disks, with 8 partitions per disk (all identically named)
268 # and attach them to a virtio-scsi controller
269 local qemu_opts=("-device virtio-scsi-pci,id=scsi0,num_queues=4")
270 local diskpath="${TESTDIR:?}/namedpart0.img"
271 local i num_disk qemu_timeout
272
273 if get_bool "${IS_BUILT_WITH_ASAN:=}" || ! get_bool "$QEMU_KVM"; then
274 num_disk=4
275 else
276 num_disk=16
277 fi
278
279 dd if=/dev/zero of="$diskpath" bs=1M count=18
280
281 for ((i = 0; i < num_disk; i++)); do
282 diskpath="${TESTDIR:?}/namedpart$i.img"
283 if [[ $i -gt 0 ]]; then
284 cp -uv "${TESTDIR:?}/namedpart0.img" "$diskpath"
285 fi
286
287 qemu_opts+=(
288 "-device scsi-hd,drive=drive$i,bus=scsi0.0,channel=0,scsi-id=0,lun=$i"
289 "-drive format=raw,cache=unsafe,file=$diskpath,if=none,id=drive$i"
290 )
291 done
292
293 # Bump the timeout when collecting test coverage, since the test is a bit
294 # slower in that case
295 if get_bool "${IS_BUILT_WITH_ASAN:=}" || ! get_bool "$QEMU_KVM"; then
296 qemu_timeout=240
297 elif is_built_with_coverage; then
298 qemu_timeout=120
299 else
300 qemu_timeout=60
301 fi
302
303 KERNEL_APPEND="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
304 # Limit the number of VCPUs and set a timeout to make sure we trigger the issue
305 QEMU_OPTIONS="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
306 QEMU_SMP=1 QEMU_TIMEOUT=$qemu_timeout test_run_one "${1:?}" || return $?
307
308 rm -f "${TESTDIR:?}"/namedpart*.img
309 }
310
311 testcase_multipath_basic_failover() {
312 if ! _host_has_feature "multipath"; then
313 echo "Missing multipath tools, skipping the test..."
314 return 77
315 fi
316
317 local qemu_opts=("-device virtio-scsi-pci,id=scsi")
318 local partdisk="${TESTDIR:?}/multipathpartitioned.img"
319 local image nback ndisk wwn
320
321 dd if=/dev/zero of="$partdisk" bs=1M count=16
322
323 # Add 16 multipath devices, each backed by 4 paths
324 for ndisk in {0..15}; do
325 wwn="0xDEADDEADBEEF$(printf "%.4d" "$ndisk")"
326 # Use a partitioned disk for the first device to test failover
327 [[ $ndisk -eq 0 ]] && image="$partdisk" || image="${TESTDIR:?}/disk$ndisk.img"
328
329 for nback in {0..3}; do
330 qemu_opts+=(
331 "-device scsi-hd,drive=drive${ndisk}x${nback},serial=MPIO$ndisk,wwn=$wwn"
332 "-drive format=raw,cache=unsafe,file=$image,file.locking=off,if=none,id=drive${ndisk}x${nback}"
333 )
334 done
335 done
336
337 KERNEL_APPEND="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
338 QEMU_OPTIONS="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
339 test_run_one "${1:?}" || return $?
340
341 rm -f "$partdisk"
342 }
343
344 # Test case for issue https://github.com/systemd/systemd/issues/19946
345 testcase_simultaneous_events() {
346 local qemu_opts=("-device virtio-scsi-pci,id=scsi")
347 local diskpath i
348
349 for i in {0..9}; do
350 diskpath="${TESTDIR:?}/simultaneousevents${i}.img"
351
352 dd if=/dev/zero of="$diskpath" bs=1M count=128
353 qemu_opts+=(
354 "-device scsi-hd,drive=drive$i,serial=deadbeeftest$i"
355 "-drive format=raw,cache=unsafe,file=$diskpath,if=none,id=drive$i"
356 )
357 done
358
359 KERNEL_APPEND="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
360 QEMU_OPTIONS="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
361 test_run_one "${1:?}" || return $?
362
363 rm -f "$diskpath"
364 }
365
366 testcase_lvm_basic() {
367 if ! _host_has_feature "lvm"; then
368 echo "Missing lvm tools, skipping the test..."
369 return 77
370 fi
371
372 local qemu_opts=("-device ahci,id=ahci0")
373 local diskpath i
374
375 # Attach 4 SATA disks to the VM (and set their model and serial fields
376 # to something predictable, so we can refer to them later)
377 for i in {0..3}; do
378 diskpath="${TESTDIR:?}/lvmbasic${i}.img"
379 dd if=/dev/zero of="$diskpath" bs=1M count=32
380 qemu_opts+=(
381 "-device ide-hd,bus=ahci0.$i,drive=drive$i,model=foobar,serial=deadbeeflvm$i"
382 "-drive format=raw,cache=unsafe,file=$diskpath,if=none,id=drive$i"
383 )
384 done
385
386 KERNEL_APPEND="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
387 QEMU_OPTIONS="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
388 test_run_one "${1:?}" || return $?
389
390 rm -f "${TESTDIR:?}"/lvmbasic*.img
391 }
392
393 testcase_btrfs_basic() {
394 if ! _host_has_feature "btrfs"; then
395 echo "Missing btrfs tools/modules, skipping the test..."
396 return 77
397 fi
398
399 local qemu_opts=("-device ahci,id=ahci0")
400 local diskpath i size
401
402 for i in {0..3}; do
403 diskpath="${TESTDIR:?}/btrfsbasic${i}.img"
404 # Make the first disk larger for multi-partition tests
405 [[ $i -eq 0 ]] && size=350 || size=128
406
407 dd if=/dev/zero of="$diskpath" bs=1M count="$size"
408 qemu_opts+=(
409 "-device ide-hd,bus=ahci0.$i,drive=drive$i,model=foobar,serial=deadbeefbtrfs$i"
410 "-drive format=raw,cache=unsafe,file=$diskpath,if=none,id=drive$i"
411 )
412 done
413
414 KERNEL_APPEND="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
415 QEMU_OPTIONS="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
416 test_run_one "${1:?}" || return $?
417
418 rm -f "${TESTDIR:?}"/btrfsbasic*.img
419 }
420
421 testcase_iscsi_lvm() {
422 if ! _host_has_feature "iscsi" || ! _host_has_feature "lvm"; then
423 echo "Missing iSCSI client/server tools (Open-iSCSI/TGT) or LVM utilities, skipping the test..."
424 return 77
425 fi
426
427 local qemu_opts=("-device ahci,id=ahci0")
428 local diskpath i size
429
430 for i in {0..3}; do
431 diskpath="${TESTDIR:?}/iscsibasic${i}.img"
432 # Make the first disk larger for multi-partition tests
433 [[ $i -eq 0 ]] && size=150 || size=64
434 # Make the first disk larger for multi-partition tests
435
436 dd if=/dev/zero of="$diskpath" bs=1M count="$size"
437 qemu_opts+=(
438 "-device ide-hd,bus=ahci0.$i,drive=drive$i,model=foobar,serial=deadbeefiscsi$i"
439 "-drive format=raw,cache=unsafe,file=$diskpath,if=none,id=drive$i"
440 )
441 done
442
443 KERNEL_APPEND="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
444 QEMU_OPTIONS="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
445 test_run_one "${1:?}" || return $?
446
447 rm -f "${TESTDIR:?}"/iscsibasic*.img
448 }
449
450 testcase_long_sysfs_path() {
451 local brid
452 local testdisk="${TESTDIR:?}/longsysfspath.img"
453 local qemu_opts=(
454 "-drive if=none,id=drive0,format=raw,cache=unsafe,file=$testdisk"
455 "-device pci-bridge,id=pci_bridge0,chassis_nr=64"
456 )
457
458 dd if=/dev/zero of="$testdisk" bs=1M count=64
459 # Create 25 additional PCI bridges, each one connected to the previous one
460 # (basically a really long extension cable), and attach a virtio drive to
461 # the last one. This should force udev into attempting to create a device
462 # unit with a _really_ long name.
463 for brid in {1..25}; do
464 qemu_opts+=("-device pci-bridge,id=pci_bridge$brid,bus=pci_bridge$((brid-1)),chassis_nr=$((64+brid))")
465 done
466
467 qemu_opts+=("-device virtio-blk-pci,drive=drive0,scsi=off,bus=pci_bridge$brid")
468
469 KERNEL_APPEND="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
470 QEMU_OPTIONS="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
471 test_run_one "${1:?}" || return $?
472
473 rm -f "${testdisk:?}"
474 }
475
476 testcase_mdadm_basic() {
477 if ! _host_has_feature "mdadm"; then
478 echo "Missing mdadm tools/modules, skipping the test..."
479 return 77
480 fi
481
482 local qemu_opts=("-device ahci,id=ahci0")
483 local diskpath i size
484
485 for i in {0..4}; do
486 diskpath="${TESTDIR:?}/mdadmbasic${i}.img"
487
488 dd if=/dev/zero of="$diskpath" bs=1M count=64
489 qemu_opts+=(
490 "-device ide-hd,bus=ahci0.$i,drive=drive$i,model=foobar,serial=deadbeefmdadm$i"
491 "-drive format=raw,cache=unsafe,file=$diskpath,if=none,id=drive$i"
492 )
493 done
494
495 KERNEL_APPEND="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
496 QEMU_OPTIONS="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
497 test_run_one "${1:?}" || return $?
498
499 rm -f "${TESTDIR:?}"/mdadmbasic*.img
500 }
501
502 testcase_mdadm_lvm() {
503 if ! _host_has_feature "mdadm" || ! _host_has_feature "lvm"; then
504 echo "Missing mdadm tools/modules or LVM tools, skipping the test..."
505 return 77
506 fi
507
508 local qemu_opts=("-device ahci,id=ahci0")
509 local diskpath i size
510
511 for i in {0..4}; do
512 diskpath="${TESTDIR:?}/mdadmlvm${i}.img"
513
514 dd if=/dev/zero of="$diskpath" bs=1M count=64
515 qemu_opts+=(
516 "-device ide-hd,bus=ahci0.$i,drive=drive$i,model=foobar,serial=deadbeefmdadmlvm$i"
517 "-drive format=raw,cache=unsafe,file=$diskpath,if=none,id=drive$i"
518 )
519 done
520
521 KERNEL_APPEND="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
522 QEMU_OPTIONS="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
523 test_run_one "${1:?}" || return $?
524
525 rm -f "${TESTDIR:?}"/mdadmlvm*.img
526 }
527 # Allow overriding which tests should be run from the "outside", useful for manual
528 # testing (make -C test/... TESTCASES="testcase1 testcase2")
529 if [[ -v "TESTCASES" && -n "$TESTCASES" ]]; then
530 read -ra TESTCASES <<< "$TESTCASES"
531 else
532 # This must run after all functions were defined, otherwise `declare -F` won't
533 # see them
534 mapfile -t TESTCASES < <(declare -F | awk '$3 ~ /^testcase_/ {print $3;}')
535 fi
536
537 do_test "$@"