2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 # vi: ts=4 sw=4 tw=0 et:
7 # * MD (mdadm) -> dm-crypt -> LVM
8 # * iSCSI -> dm-crypt -> LVM
11 TEST_DESCRIPTION
="systemd-udev storage tests"
14 # Save only journals of failing test cases by default (to conserve space)
15 TEST_SAVE_JOURNAL
="${TEST_SAVE_JOURNAL:-fail}"
16 QEMU_TIMEOUT
="${QEMU_TIMEOUT:-600}"
18 # shellcheck source=test/test-functions
19 .
"${TEST_BASE_DIR:?}/test-functions"
21 USER_QEMU_OPTIONS
="${QEMU_OPTIONS:-}"
22 USER_KERNEL_APPEND
="${KERNEL_APPEND:-}"
24 if ! get_bool
"$QEMU_KVM"; then
25 echo "This test requires KVM, skipping..."
29 _host_has_feature
() {(
34 modprobe
-nv btrfs
&& command -v mkfs.btrfs
&& command -v btrfs ||
return $?
37 # Client/initiator (Open-iSCSI)
38 command -v iscsiadm
&& command -v iscsid ||
return $?
40 command -v tgtadm
&& command -v tgtd ||
return $?
43 command -v lvm ||
return $?
46 command -v multipath
&& command -v multipathd ||
return $?
49 echo >&2 "ERROR: Unknown feature '$1'"
50 # Make this a hard error to distinguish an invalid feature from
56 test_append_files
() {(
58 # An associative array of requested (but optional) features and their
59 # respective "handlers" from test/test-functions
61 # Note: we install cryptsetup unconditionally, hence it's not explicitly
67 [multipath
]=install_multipath
70 instmods
"=block" "=md" "=nvme" "=scsi"
72 image_install lsblk swapoff swapon
wc wipefs
74 # Install the optional features if the host has the respective tooling
75 for feature
in "${!features[@]}"; do
76 if _host_has_feature
"$feature"; then
77 "${features[$feature]}"
81 generate_module_dependencies
84 dd if=/dev
/zero of
="${TESTDIR:?}/disk$i.img" bs
=1M count
=1
85 echo "device$i" >"${TESTDIR:?}/disk$i.img"
91 # Clean up certain "problematic" files which may be left over by failing tests
92 : >"${initdir:?}/etc/fstab"
93 : >"${initdir:?}/etc/crypttab"
97 local test_id
="${1:?}"
99 if run_qemu
"$test_id"; then
100 check_result_qemu ||
{ echo "QEMU test failed"; return 1; }
107 local test_id
="${1:?}"
115 if get_bool
"${TEST_NO_QEMU:=}" ||
! find_qemu_bin
; then
116 dwarn
"can't run QEMU, skipping"
120 # Execute each currently defined function starting with "testcase_"
121 for testcase
in "${TESTCASES[@]}"; do
123 echo "------ $testcase: BEGIN ------"
124 # Note for my future frustrated self: `fun && xxx` (as well as ||, if, while,
125 # until, etc.) _DISABLES_ the `set -e` behavior in _ALL_ nested function
126 # calls made from `fun()`, i.e. the function _CONTINUES_ even when a called
127 # command returned non-zero EC. That may unexpectedly hide failing commands
128 # if not handled properly. See: bash(1) man page, `set -e` section.
130 # So, be careful when adding clean up snippets in the testcase_*() functions -
131 # if the `test_run_one()` function isn't the last command, you have propagate
132 # the exit code correctly (e.g. `test_run_one() || return $?`, see below).
134 "$testcase" "$test_id" || ec
=$?
137 passed
+=("$testcase")
141 skipped
+=("$testcase")
145 failed
+=("$testcase")
148 echo "------ $testcase: END ($state) ------"
151 echo "Passed tests: ${#passed[@]}"
152 printf " * %s\n" "${passed[@]}"
153 echo "Skipped tests: ${#skipped[@]}"
154 printf " * %s\n" "${skipped[@]}"
155 echo "Failed tests: ${#failed[@]}"
156 printf " * %s\n" "${failed[@]}"
158 [[ ${#failed[@]} -eq 0 ]] ||
return 1
163 testcase_megasas2_basic
() {
164 if ! "${QEMU_BIN:?}" -device help |
grep 'name "megasas-gen2"'; then
165 echo "megasas-gen2 device driver is not available, skipping test..."
170 "-device megasas-gen2,id=scsi0"
171 "-device megasas-gen2,id=scsi1"
172 "-device megasas-gen2,id=scsi2"
173 "-device megasas-gen2,id=scsi3"
176 for i
in {0.
.127}; do
177 # Add 128 drives, 32 per bus
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"
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:?}"
189 testcase_nvme_basic
() {
190 if ! "${QEMU_BIN:?}" -device help |
grep 'name "nvme"'; then
191 echo "nvme device driver is not available, skipping test..."
197 "-device nvme,drive=nvme$i,serial=deadbeef$i,num_queues=8"
198 "-drive format=raw,cache=unsafe,file=${TESTDIR:?}/disk$i.img,if=none,id=nvme$i"
202 KERNEL_APPEND
="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
203 QEMU_OPTIONS
="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
204 test_run_one
"${1:?}"
207 # Test for issue https://github.com/systemd/systemd/issues/20212
208 testcase_virtio_scsi_identically_named_partitions
() {
209 if ! "${QEMU_BIN:?}" -device help |
grep 'name "virtio-scsi-pci"'; then
210 echo "virtio-scsi-pci device driver is not available, skipping test..."
214 # Create 16 disks, with 8 partitions per disk (all identically named)
215 # and attach them to a virtio-scsi controller
216 local qemu_opts
=("-device virtio-scsi-pci,id=scsi0,num_queues=4")
217 local diskpath
="${TESTDIR:?}/namedpart0.img"
218 local lodev qemu_timeout
220 dd if=/dev
/zero of
="$diskpath" bs
=1M count
=18
221 lodev
="$(losetup --show -f -P "$diskpath")"
222 sfdisk
"${lodev:?}" <<EOF
225 name="Hello world", size=2M
226 name="Hello world", size=2M
227 name="Hello world", size=2M
228 name="Hello world", size=2M
229 name="Hello world", size=2M
230 name="Hello world", size=2M
231 name="Hello world", size=2M
232 name="Hello world", size=2M
237 diskpath
="${TESTDIR:?}/namedpart$i.img"
238 if [[ $i -gt 0 ]]; then
239 cp -uv "${TESTDIR:?}/namedpart0.img" "$diskpath"
243 "-device scsi-hd,drive=drive$i,bus=scsi0.0,channel=0,scsi-id=0,lun=$i"
244 "-drive format=raw,cache=unsafe,file=$diskpath,if=none,id=drive$i"
248 # Bump the timeout when collecting test coverage, since the test is a bit
249 # slower in that case
250 is_built_with_coverage
&& qemu_timeout
=120 || qemu_timeout
=60
252 KERNEL_APPEND
="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
253 # Limit the number of VCPUs and set a timeout to make sure we trigger the issue
254 QEMU_OPTIONS
="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
255 QEMU_SMP
=1 QEMU_TIMEOUT
=$qemu_timeout test_run_one
"${1:?}" ||
return $?
257 rm -f "${TESTDIR:?}"/namedpart
*.img
260 testcase_multipath_basic_failover
() {
261 if ! _host_has_feature
"multipath"; then
262 echo "Missing multipath tools, skipping the test..."
266 local qemu_opts
=("-device virtio-scsi-pci,id=scsi")
267 local partdisk
="${TESTDIR:?}/multipathpartitioned.img"
268 local image lodev nback ndisk wwn
270 dd if=/dev
/zero of
="$partdisk" bs
=1M count
=16
271 lodev
="$(losetup --show -f -P "$partdisk")"
272 sfdisk
"${lodev:?}" <<EOF
275 name="first_partition", size=5M
276 uuid="deadbeef-dead-dead-beef-000000000000", name="failover_part", size=5M
279 mkfs.ext4
-U "deadbeef-dead-dead-beef-111111111111" -L "failover_vol" "${lodev}p2"
282 # Add 64 multipath devices, each backed by 4 paths
283 for ndisk
in {0.
.63}; do
284 wwn
="0xDEADDEADBEEF$(printf "%.4d
" "$ndisk")"
285 # Use a partitioned disk for the first device to test failover
286 [[ $ndisk -eq 0 ]] && image
="$partdisk" || image
="${TESTDIR:?}/disk$ndisk.img"
288 for nback
in {0.
.3}; do
290 "-device scsi-hd,drive=drive${ndisk}x${nback},serial=MPIO$ndisk,wwn=$wwn"
291 "-drive format=raw,cache=unsafe,file=$image,file.locking=off,if=none,id=drive${ndisk}x${nback}"
296 KERNEL_APPEND
="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
297 QEMU_OPTIONS
="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
298 test_run_one
"${1:?}" ||
return $?
303 # Test case for issue https://github.com/systemd/systemd/issues/19946
304 testcase_simultaneous_events
() {
305 local qemu_opts
=("-device virtio-scsi-pci,id=scsi")
306 local partdisk
="${TESTDIR:?}/simultaneousevents.img"
308 dd if=/dev
/zero of
="$partdisk" bs
=1M count
=110
310 "-device scsi-hd,drive=drive1,serial=deadbeeftest"
311 "-drive format=raw,cache=unsafe,file=$partdisk,if=none,id=drive1"
314 KERNEL_APPEND
="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
315 QEMU_OPTIONS
="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
316 test_run_one
"${1:?}" ||
return $?
321 testcase_lvm_basic
() {
322 if ! _host_has_feature
"lvm"; then
323 echo "Missing lvm tools, skipping the test..."
327 local qemu_opts
=("-device ahci,id=ahci0")
330 # Attach 4 SATA disks to the VM (and set their model and serial fields
331 # to something predictable, so we can refer to them later)
333 diskpath
="${TESTDIR:?}/lvmbasic${i}.img"
334 dd if=/dev
/zero of
="$diskpath" bs
=1M count
=32
336 "-device ide-hd,bus=ahci0.$i,drive=drive$i,model=foobar,serial=deadbeeflvm$i"
337 "-drive format=raw,cache=unsafe,file=$diskpath,if=none,id=drive$i"
341 KERNEL_APPEND
="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
342 QEMU_OPTIONS
="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
343 test_run_one
"${1:?}" ||
return $?
345 rm -f "${TESTDIR:?}"/lvmbasic
*.img
348 testcase_btrfs_basic
() {
349 if ! _host_has_feature
"btrfs"; then
350 echo "Missing btrfs tools/modules, skipping the test..."
354 local qemu_opts
=("-device ahci,id=ahci0")
355 local diskpath i size
358 diskpath
="${TESTDIR:?}/btrfsbasic${i}.img"
359 # Make the first disk larger for multi-partition tests
360 [[ $i -eq 0 ]] && size
=350 || size
=128
362 dd if=/dev
/zero of
="$diskpath" bs
=1M count
="$size"
364 "-device ide-hd,bus=ahci0.$i,drive=drive$i,model=foobar,serial=deadbeefbtrfs$i"
365 "-drive format=raw,cache=unsafe,file=$diskpath,if=none,id=drive$i"
369 KERNEL_APPEND
="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
370 QEMU_OPTIONS
="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
371 test_run_one
"${1:?}" ||
return $?
373 rm -f "${TESTDIR:?}"/btrfsbasic
*.img
376 testcase_iscsi_lvm
() {
377 if ! _host_has_feature
"iscsi" ||
! _host_has_feature
"lvm"; then
378 echo "Missing iSCSI client/server tools (Open-iSCSI/TGT) or LVM utilities, skipping the test..."
382 local qemu_opts
=("-device ahci,id=ahci0")
383 local diskpath i size
386 diskpath
="${TESTDIR:?}/iscsibasic${i}.img"
387 # Make the first disk larger for multi-partition tests
388 [[ $i -eq 0 ]] && size
=150 || size
=64
389 # Make the first disk larger for multi-partition tests
391 dd if=/dev
/zero of
="$diskpath" bs
=1M count
="$size"
393 "-device ide-hd,bus=ahci0.$i,drive=drive$i,model=foobar,serial=deadbeefiscsi$i"
394 "-drive format=raw,cache=unsafe,file=$diskpath,if=none,id=drive$i"
398 KERNEL_APPEND
="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
399 QEMU_OPTIONS
="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
400 test_run_one
"${1:?}" ||
return $?
402 rm -f "${TESTDIR:?}"/iscsibasic
*.img
405 testcase_long_sysfs_path
() {
407 local testdisk
="${TESTDIR:?}/longsysfspath.img"
409 "-drive if=none,id=drive0,format=raw,cache=unsafe,file=$testdisk"
410 "-device pci-bridge,id=pci_bridge0,bus=pci.0,chassis_nr=64"
413 dd if=/dev
/zero of
="$testdisk" bs
=1M count
=64
414 lodev
="$(losetup --show -f -P "$testdisk")"
415 sfdisk
"${lodev:?}" <<EOF
418 name="test_swap", size=32M
419 uuid="deadbeef-dead-dead-beef-000000000000", name="test_part", size=5M
422 mkswap
-U "deadbeef-dead-dead-beef-111111111111" -L "swap_vol" "${lodev}p1"
423 mkfs.ext4
-U "deadbeef-dead-dead-beef-222222222222" -L "data_vol" "${lodev}p2"
426 # Create 25 additional PCI bridges, each one connected to the previous one
427 # (basically a really long extension cable), and attach a virtio drive to
428 # the last one. This should force udev into attempting to create a device
429 # unit with a _really_ long name.
430 for brid
in {1.
.25}; do
431 qemu_opts
+=("-device pci-bridge,id=pci_bridge$brid,bus=pci_bridge$((brid-1)),chassis_nr=$((64+brid))")
434 qemu_opts
+=("-device virtio-blk-pci,drive=drive0,scsi=off,bus=pci_bridge$brid")
436 KERNEL_APPEND
="systemd.setenv=TEST_FUNCTION_NAME=${FUNCNAME[0]} ${USER_KERNEL_APPEND:-}"
437 QEMU_OPTIONS
="${qemu_opts[*]} ${USER_QEMU_OPTIONS:-}"
438 test_run_one
"${1:?}" ||
return $?
440 rm -f "${testdisk:?}"
443 # Allow overriding which tests should be run from the "outside", useful for manual
444 # testing (make -C test/... TESTCASES="testcase1 testcase2")
445 if [[ -v "TESTCASES" && -n "$TESTCASES" ]]; then
446 read -ra TESTCASES
<<< "$TESTCASES"
448 # This must run after all functions were defined, otherwise `declare -F` won't
450 mapfile
-t TESTCASES
< <(declare -F |
awk '$3 ~ /^testcase_/ {print $3;}')