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