]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: don't use "dd" to allocate empty files
authorLennart Poettering <lennart@amutable.com>
Fri, 6 Feb 2026 10:16:48 +0000 (11:16 +0100)
committerLennart Poettering <lennart@amutable.com>
Fri, 6 Feb 2026 17:19:33 +0000 (18:19 +0100)
Let's use truncate -s … to create empty files (if they can be sparse)

Let's use fallocate -l … to create empty non-sparse files.

This should reduce the disk footprint of our tests a bit, given that in
most cases we won't use the allocate disk space in full, not even
remotely.

test/units/TEST-13-NSPAWN.machined.sh
test/units/TEST-13-NSPAWN.nspawn.sh
test/units/TEST-17-UDEV.sanity-check.sh
test/units/TEST-50-DISSECT.sh
test/units/TEST-55-OOMD.sh
test/units/TEST-58-REPART.sh
test/units/TEST-64-UDEV-STORAGE.sh
test/units/TEST-67-INTEGRITY.sh
test/units/TEST-87-AUX-UTILS-VM.mount.sh

index dbf126f1fcf3e0c8f4601e05296ea1a1e797a97a..e48a2776abc403d5605c31b28009839dc2f659aa 100755 (executable)
@@ -190,7 +190,7 @@ test ! -d /var/lib/machines/.hidden1
 
 # Prepare a simple raw container
 mkdir -p /tmp/mnt
-dd if=/dev/zero of=/var/tmp/container.raw bs=1M count=256
+truncate -s 256M /var/tmp/container.raw
 mkfs.ext4 /var/tmp/container.raw
 mount -o loop /var/tmp/container.raw /tmp/mnt
 cp -r /var/lib/machines/container1/* /tmp/mnt
index ae5dfdce383fdad85ec5fca32027d945b7b03b4c..d17c08c02ab86272c3196a17946dd29503223d3a 100755 (executable)
@@ -77,7 +77,7 @@ testcase_sanity() {
     create_dummy_container "$template"
     # Create a simple image from the just created container template
     image="$(mktemp /var/lib/machines/TEST-13-NSPAWN.image-XXX.img)"
-    dd if=/dev/zero of="$image" bs=1M count=256
+    truncate -s 256M "$image"
     mkfs.ext4 "$image"
     mkdir -p /mnt
     mount -o loop "$image" /mnt
@@ -810,7 +810,7 @@ testcase_rootidmap() {
     root="$(mktemp -d /var/lib/machines/TEST-13-NSPAWN.rootidmap-path.XXX)"
     # Create ext4 image, as ext4 supports idmapped-mounts.
     mkdir -p /tmp/rootidmap/bind
-    dd if=/dev/zero of=/tmp/rootidmap/ext4.img bs=4k count=2048
+    truncate -s $((4096*2048)) /tmp/rootidmap/ext4.img
     mkfs.ext4 /tmp/rootidmap/ext4.img
     mount /tmp/rootidmap/ext4.img /tmp/rootidmap/bind
     trap "rootidmap_cleanup /tmp/rootidmap/" RETURN
@@ -854,7 +854,7 @@ testcase_owneridmap() {
     root="$(mktemp -d /var/lib/machines/TEST-13-NSPAWN.owneridmap-path.XXX)"
     # Create ext4 image, as ext4 supports idmapped-mounts.
     mkdir -p /tmp/owneridmap/bind
-    dd if=/dev/zero of=/tmp/owneridmap/ext4.img bs=4k count=2048
+    truncate -s $((4096*2048)) /tmp/owneridmap/ext4.img
     mkfs.ext4 /tmp/owneridmap/ext4.img
     mount /tmp/owneridmap/ext4.img /tmp/owneridmap/bind
     trap "owneridmap_cleanup /tmp/owneridmap/" RETURN
index 5f1a93e78f5a6a92eb0ffa8e883d3bd063108042..6246638fc941e49f12e2479fd70d763de98ea4b2 100755 (executable)
@@ -25,7 +25,7 @@ netdev=hoge
 ip link add $netdev type dummy
 
 blk="$(mktemp)"
-dd if=/dev/zero of="$blk" bs=1M count=1
+truncate -s 1M "$blk"
 loopdev="$(losetup --show -f "$blk")"
 
 # Wait for devices created in the above being processed.
index 972272d3a479d1c08af4f619133b849fff0cde3d..14ff8ad250b332cf5f0a7ea83787ad1550c45918 100755 (executable)
@@ -153,7 +153,7 @@ root_size="$(du --apparent-size -k "$MINIMAL_IMAGE.raw" | cut -f1)"
 verity_size="$(du --apparent-size -k "$MINIMAL_IMAGE.verity" | cut -f1)"
 signature_size=4
 # 4MB seems to be the minimum size blkid will accept, below that probing fails
-dd if=/dev/zero of="$MINIMAL_IMAGE.gpt" bs=512 count=$((8192+root_size*2+verity_size*2+signature_size*2))
+truncate -s $(((8192+root_size*2+verity_size*2+signature_size*2)*512)) "$MINIMAL_IMAGE.gpt"
 # sfdisk seems unhappy if the size overflows into the next unit, eg: 1580KiB will be interpreted as 1MiB
 # so do some basic rounding up if the minimal image is more than 1 MB
 if [[ "$root_size" -ge 1024 ]]; then
index 19197309041436464714ec6c745eb9e0b831a5f7..56a32926a04b98b41d3c68da20d47f5415231ebf 100755 (executable)
@@ -26,7 +26,7 @@ if systemd-detect-virt --vm --quiet; then
     if [[ "$(findmnt -n -o FSTYPE /)" == btrfs ]]; then
         btrfs filesystem mkswapfile -s 64M /swapfile
     else
-        dd if=/dev/zero of=/swapfile bs=1M count=64
+        fallocate -l 64M /swapfile
         chmod 0600 /swapfile
         mkswap /swapfile
     fi
index 4ac4f2ee4b6fda0f74c50d0f570ef679054ad60a..f25cf42c4b3ee2afc83d57070e5469c9ecde285f 100755 (executable)
@@ -1708,7 +1708,7 @@ testcase_btrfs_compression() {
     # Must not be in tmpfs due to exclusions. It also must be large and
     # compressible so that the compression check succeeds later.
     src=/etc/test-source-file
-    dd if=/dev/zero of="$src" bs=1M count=1 2>/dev/null
+    fallocate -l 1M "$src"
 
     tee "$defs/btrfs-compressed.conf" <<EOF
 [Partition]
index f54da9276852fa128c9d3d579e4403cc5baf02d4..f7bc59ff44a1b094182147f927809a43c54a2ea9 100755 (executable)
@@ -1013,7 +1013,7 @@ testcase_iscsi_lvm() {
     udevadm wait --settle --timeout=30 "${devices[0]}"
     mount "${devices[0]}" "$mpoint"
     for i in {1..4}; do
-        dd if=/dev/zero of="$mpoint/lun$i.img" bs=1M count=32
+        truncate -s 32M "$mpoint/lun$i.img"
     done
     # Initialize a new iSCSI target <$target_name> consisting of 4 LUNs, each
     # backed by a file
index 0919f2024870375547dfa3d953a7fe751a66afd0..667fa5634340154a4c4ff7a2e4b94876dcade738 100755 (executable)
@@ -48,8 +48,8 @@ EOF
 udevadm control --reload
 
 TMP_DIR="$(mktemp -d -t -p / integrity.tmp.XXXXXX)"
-dd if=/dev/zero of="${TMP_DIR}/image" bs=1048576 count=64
-dd if=/dev/zero of="${TMP_DIR}/data" bs=1048576 count=64
+truncate -s 64M "${TMP_DIR}/image"
+truncate -s 64M "${TMP_DIR}/data"
 LOOP="$(losetup --show -f "${TMP_DIR}/image")"
 udevadm wait --timeout=30 --settle "${LOOP}"
 
index 92fa2eb9dce3f8b6fff13568156f29d69694503f..5eba8e6bbc8ac5c7df22c5aeb7dd87ed74ff4822 100755 (executable)
@@ -43,7 +43,7 @@ test -e "$WORK_DIR/upper/foo"
 systemd-umount "$WORK_DIR/overlay"
 
 # Set up a simple block device for further tests
-dd if=/dev/zero of="$WORK_DIR/simple.img" bs=1M count=16
+truncate -s 16M "$WORK_DIR/simple.img"
 mkfs.ext4 -L sd-mount-test "$WORK_DIR/simple.img"
 LOOP="$(losetup --show --find "$WORK_DIR/simple.img")"
 udevadm wait --timeout=60 --settle "$LOOP"