]> 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)
committerLuca Boccassi <luca.boccassi@gmail.com>
Fri, 27 Feb 2026 21:57:04 +0000 (21:57 +0000)
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.

(cherry picked from commit dd2676c1abf26f47f9374c1dcc3757f47992c6fa)
(cherry picked from commit d29a7cced52ad5ae09bf902b71c209f595f8dcd3)
(cherry picked from commit f9a5b2e7900d6e9df72a2098cd597fc49a78b0ea)

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

index b3c04544ae85dc8b5ab0b71019b686d4f40d101c..ee2fdb132329d0b4ed33da1ec8b787f1f9127c74 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 b98d505e54f411b4fcff528465654e7e2eca43e8..841a75b8dd63b6cab175a202ac5cc0147172c8c5 100755 (executable)
@@ -86,7 +86,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
@@ -637,7 +637,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
@@ -681,7 +681,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 a770a68f27e62f5cfde67daaa2af890ba3370788..e7bee4695933fe878990f12124ef280f0b0466e6 100755 (executable)
@@ -25,7 +25,7 @@ netdev=dummy17.10
 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 0efea0fc1808b54a1c8e1779dc9e1e1efed83c98..bc4e32b9cf8b61fa6bba62f7bca76158ffe93f94 100755 (executable)
@@ -134,7 +134,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 3c9b4391a963e7d58872b1608c91ebc44098ed77..b2a071b95834d3d8bb097318bd060360e66084c7 100755 (executable)
@@ -28,7 +28,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 a0db70b292d90acf5ddbb1dbcb52325398a1c752..cd2941834cfe5f50780cf9cbe7e4ecfc14ae7853 100755 (executable)
@@ -1007,7 +1007,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 c015703666f4a2befbe7e3e89444582f26e00180..f83f5bb22d00c2f79060bb0128fae41a67b4ae64 100755 (executable)
@@ -46,8 +46,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 701defc3c6eb22cedba4bf9212b120fa20af2ee4..7ce08b4483d72f2f864932d2a134851ec462bd5b 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"