From c068a7beddfead936bfc946f4c36a3a75f856030 Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 19 Apr 2023 03:19:52 +0200 Subject: [PATCH] Add support for full allocation (writing zeroes) --- run/disk.sh | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/run/disk.sh b/run/disk.sh index a8960a0..56d6926 100644 --- a/run/disk.sh +++ b/run/disk.sh @@ -66,7 +66,7 @@ if [ ! -f "${DATA}" ]; then # Create an empty file - if [ "$ALLOCATE" != "Y" ]; then + if [ "$ALLOCATE" = "N" ]; then truncate -s "${DATA_SIZE}" "${DATA}" @@ -80,16 +80,35 @@ if [ ! -f "${DATA}" ]; then echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATION=N." && exit 86 fi - if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then - rm -f "${DATA}" - echo "ERROR: Could not allocate a file for the virtual disk." && exit 87 - fi + if [ "$ALLOCATE" = "F" ]; then + MB=$(( (DATA_SIZE + 1048575)/1048576 )) + + echo "INFO: Writing ${MB} MB of zeroes, please wait.." + dd if=/dev/zero of="${DATA}" count="${MB}" bs=1M + truncate -s "${DATA_SIZE}" "${DATA}" + + else + + if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then + rm -f "${DATA}" + echo "ERROR: Could not allocate a file for the virtual disk." && exit 87 + fi + + fi fi # Check if file exists if [ ! -f "${DATA}" ]; then - echo "ERROR: Virtual DSM data disk does not exist ($DATA)" && exit 88 + echo "ERROR: Virtual disk does not exist ($DATA)" && exit 88 + fi + + # Check the filesize + SIZE=$(stat -c%s "${DATA}") + + if [[ SIZE -ne DATA_SIZE ]]; then + rm -f "${DATA}" + echo "ERROR: Virtual disk has the wrong size: ${SIZE}" && exit 89 fi # Format as BTRFS filesystem