From 5f212b4041bd86d323ecc8611a1acc850315f039 Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 19 Apr 2023 02:55:06 +0200 Subject: [PATCH 1/9] Add full allocation mode (write zeroes) --- run/install.sh | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/run/install.sh b/run/install.sh index b50a911..88ef32b 100644 --- a/run/install.sh +++ b/run/install.sh @@ -106,7 +106,7 @@ unzip -q -o "$BOOT".zip -d $TMP echo "Install: Creating partition table..." SYSTEM="$TMP/sys.img" -SYSTEM_SIZE="4954537983" +SYSTEM_SIZE=4954537983 # Check free diskspace SPACE=$(df --output=avail -B 1 "$TMP" | tail -n 1) @@ -115,9 +115,20 @@ if (( SYSTEM_SIZE > SPACE )); then echo "ERROR: Not enough free space to create a 4 GB system disk." && exit 87 fi -if ! fallocate -l "${SYSTEM_SIZE}" "${SYSTEM}"; then - rm -f "${SYSTEM}" - echo "ERROR: Could not allocate a file for the system disk." && exit 88 +if [ "$ALLOCATE" != "F" ]; then + + if ! fallocate -l "${SYSTEM_SIZE}" "${SYSTEM}"; then + rm -f "${SYSTEM}" + echo "ERROR: Could not allocate a file for the system disk." && exit 88 + fi + +else + + MB=(( (SYSTEM_SIZE + 1048575)/1048576 )) + + dd if=/dev/zero of="${SYSTEM}" count="${MB}" bs=1M + truncate -s "${SYSTEM_SIZE}" "${SYSTEM}" + fi PART="$TMP/partition.fdisk" From 970502ee66e267c383b1599d6781e45cc9eff8c1 Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 19 Apr 2023 03:05:34 +0200 Subject: [PATCH 2/9] Check system size --- run/install.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/run/install.sh b/run/install.sh index 88ef32b..b19208b 100644 --- a/run/install.sh +++ b/run/install.sh @@ -124,13 +124,20 @@ if [ "$ALLOCATE" != "F" ]; then else - MB=(( (SYSTEM_SIZE + 1048575)/1048576 )) + MB=$(( (SYSTEM_SIZE + 1048575)/1048576 )) + echo "INFO: Writing ${MB} MB of zeroes.." dd if=/dev/zero of="${SYSTEM}" count="${MB}" bs=1M truncate -s "${SYSTEM_SIZE}" "${SYSTEM}" fi +SIZE=$(stat -c%s "${SYSTEM}") + +if [[ SIZE -ne SYSTEM_SIZE ]]; then + echo "ERROR: System disk has the wrong size." && exit 89 +fi + PART="$TMP/partition.fdisk" { echo "label: dos" From c068a7beddfead936bfc946f4c36a3a75f856030 Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 19 Apr 2023 03:19:52 +0200 Subject: [PATCH 3/9] 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 From 9b9d8285feb173c490f7b25ac1302f07b213dc9e Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 19 Apr 2023 03:26:29 +0200 Subject: [PATCH 4/9] Support full preallocation (writing zeroes) --- run/install.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/run/install.sh b/run/install.sh index b19208b..5d3a3d6 100644 --- a/run/install.sh +++ b/run/install.sh @@ -125,17 +125,24 @@ if [ "$ALLOCATE" != "F" ]; then else MB=$(( (SYSTEM_SIZE + 1048575)/1048576 )) + echo "INFO: Writing ${MB} MB of zeroes, please wait.." - echo "INFO: Writing ${MB} MB of zeroes.." dd if=/dev/zero of="${SYSTEM}" count="${MB}" bs=1M truncate -s "${SYSTEM_SIZE}" "${SYSTEM}" fi +# Check if file exists +if [ ! -f "${SYSTEM}" ]; then + echo "ERROR: System disk does not exist ($SYSTEM)" && exit 89 +fi + +# Check the filesize SIZE=$(stat -c%s "${SYSTEM}") if [[ SIZE -ne SYSTEM_SIZE ]]; then - echo "ERROR: System disk has the wrong size." && exit 89 + rm -f "${SYSTEM}" + echo "ERROR: System disk has the wrong size: ${SIZE}" && exit 90 fi PART="$TMP/partition.fdisk" From e65ab41b7c234641d29de663a348d49cccb62213 Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 19 Apr 2023 03:55:32 +0200 Subject: [PATCH 5/9] Add full preallocation (writing zeroes) --- run/disk.sh | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/run/disk.sh b/run/disk.sh index 56d6926..8257eee 100644 --- a/run/disk.sh +++ b/run/disk.sh @@ -45,10 +45,20 @@ if [ -f "${DATA}" ]; then echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATION=N." && exit 84 fi - if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then - echo "ERROR: Could not allocate a file for the virtual disk." && exit 85 - fi + if [ "$ALLOCATE" = "F" ]; then + GB=$(( (REQ + 1073741823)/1073741824 )) + echo "INFO: Writing ${GB} GB of zeroes, please wait.." + + dd if=/dev/zero of="${DATA}" seek="{OLD_SIZE}" count="${REQ}" bs=1M iflag=count_bytes oflag=seek_bytes + + else + + if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then + echo "ERROR: Could not allocate a file for the virtual disk." && exit 85 + fi + + fi fi fi @@ -82,11 +92,9 @@ if [ ! -f "${DATA}" ]; then if [ "$ALLOCATE" = "F" ]; then - MB=$(( (DATA_SIZE + 1048575)/1048576 )) + echo "INFO: Writing ${DISK_SIZE} of zeroes, please wait.." - echo "INFO: Writing ${MB} MB of zeroes, please wait.." - dd if=/dev/zero of="${DATA}" count="${MB}" bs=1M - truncate -s "${DATA_SIZE}" "${DATA}" + dd if=/dev/zero of="${DATA}" count="${DATA_SIZE}" bs=1M iflag=count_bytes else @@ -103,19 +111,18 @@ if [ ! -f "${DATA}" ]; then 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 mkfs.btrfs -q -L data -d single -m dup "${DATA}" > /dev/null fi +# Check the filesize +SIZE=$(stat -c%s "${DATA}") + +if [[ SIZE -ne DATA_SIZE ]]; then + echo "ERROR: Virtual disk has the wrong size: ${SIZE}" && exit 89 +fi + AGENT="${STORAGE}/${BASE}.agent" [ -f "$AGENT" ] && AGENT_VERSION=$(cat "${AGENT}") || AGENT_VERSION=1 From 0f99873f604619404c6d76512d997ec123d639dc Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 19 Apr 2023 03:58:21 +0200 Subject: [PATCH 6/9] Add full preallocation (writing zeroes) --- run/install.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/run/install.sh b/run/install.sh index 5d3a3d6..5be640b 100644 --- a/run/install.sh +++ b/run/install.sh @@ -124,11 +124,10 @@ if [ "$ALLOCATE" != "F" ]; then else - MB=$(( (SYSTEM_SIZE + 1048575)/1048576 )) - echo "INFO: Writing ${MB} MB of zeroes, please wait.." + GB=$(( (SYSTEM_SIZE + 1073741823)/1073741824 )) + echo "INFO: Writing ${GB} GB of zeroes, please wait.." - dd if=/dev/zero of="${SYSTEM}" count="${MB}" bs=1M - truncate -s "${SYSTEM_SIZE}" "${SYSTEM}" + dd if=/dev/zero of="${SYSTEM}" count="${SYSTEM_SIZE}" bs=1M iflag=count_bytes fi From e641f20493dc9b0c08cb07ac0c46f43d37526f26 Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 19 Apr 2023 04:02:02 +0200 Subject: [PATCH 7/9] Add full preallocation (writing zeroes) --- run/disk.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run/disk.sh b/run/disk.sh index 8257eee..b4b385d 100644 --- a/run/disk.sh +++ b/run/disk.sh @@ -50,7 +50,7 @@ if [ -f "${DATA}" ]; then GB=$(( (REQ + 1073741823)/1073741824 )) echo "INFO: Writing ${GB} GB of zeroes, please wait.." - dd if=/dev/zero of="${DATA}" seek="{OLD_SIZE}" count="${REQ}" bs=1M iflag=count_bytes oflag=seek_bytes + dd if=/dev/zero of="${DATA}" seek="${OLD_SIZE}" count="${REQ}" bs=1M iflag=count_bytes oflag=seek_bytes else From f044f5966ebcb4be21ca4d600f8ab41a120df60f Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 19 Apr 2023 04:06:04 +0200 Subject: [PATCH 8/9] Write zeroes --- run/disk.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/run/disk.sh b/run/disk.sh index b4b385d..86b9c08 100644 --- a/run/disk.sh +++ b/run/disk.sh @@ -29,7 +29,7 @@ if [ -f "${DATA}" ]; then echo "INFO: Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.." - if [ "$ALLOCATE" != "Y" ]; then + if [ "$ALLOCATE" = "N" ]; then truncate -s "${DATA_SIZE}" "${DATA}"; @@ -45,7 +45,7 @@ if [ -f "${DATA}" ]; then echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATION=N." && exit 84 fi - if [ "$ALLOCATE" = "F" ]; then + if [ "$ALLOCATE" = "Z" ]; then GB=$(( (REQ + 1073741823)/1073741824 )) echo "INFO: Writing ${GB} GB of zeroes, please wait.." @@ -90,7 +90,7 @@ if [ ! -f "${DATA}" ]; then echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATION=N." && exit 86 fi - if [ "$ALLOCATE" = "F" ]; then + if [ "$ALLOCATE" = "Z" ]; then echo "INFO: Writing ${DISK_SIZE} of zeroes, please wait.." From 0d32e03958ca496014a12fc62d8ff5d8ebc57d88 Mon Sep 17 00:00:00 2001 From: Kroese Date: Wed, 19 Apr 2023 04:08:47 +0200 Subject: [PATCH 9/9] Write zeroes --- run/install.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/run/install.sh b/run/install.sh index 5be640b..2e98307 100644 --- a/run/install.sh +++ b/run/install.sh @@ -115,7 +115,9 @@ if (( SYSTEM_SIZE > SPACE )); then echo "ERROR: Not enough free space to create a 4 GB system disk." && exit 87 fi -if [ "$ALLOCATE" != "F" ]; then +if [ "$ALLOCATE" != "Z" ]; then + + # Cannot use truncate here because of swap partition if ! fallocate -l "${SYSTEM_SIZE}" "${SYSTEM}"; then rm -f "${SYSTEM}"