tests: don't use "dd" to allocate empty files

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.
This commit is contained in:
Lennart Poettering
2026-02-06 18:19:33 +01:00
parent 5f815d6fa1
commit dd2676c1ab
9 changed files with 12 additions and 12 deletions
+1 -1
View File
@@ -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
+3 -3
View File
@@ -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
+1 -1
View File
@@ -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.
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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]
+1 -1
View File
@@ -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
+2 -2
View File
@@ -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}"
+1 -1
View File
@@ -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"