From a8a81d1232b82009f6f87dbe4caf85c9d9d2f589 Mon Sep 17 00:00:00 2001 From: Kroese Date: Sat, 15 Apr 2023 17:53:14 +0200 Subject: [PATCH] Allow disk resizing Allow disk resizing --- disk.sh | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/disk.sh b/disk.sh index be1897a..1083a53 100644 --- a/disk.sh +++ b/disk.sh @@ -12,9 +12,9 @@ DISK_SIZE=$(echo "${DISK_SIZE}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g') [ -f "$IMG/data$DISK_SIZE.img" ] && mv -f "$IMG/data$DISK_SIZE.img" "${DATA}" -NEW_SIZE=$(numfmt --from=iec "${DISK_SIZE}") +DATA_SIZE=$(numfmt --from=iec "${DISK_SIZE}") -if (( NEW_SIZE < 6442450944 )); then +if (( DATA_SIZE < 6442450944 )); then echo "ERROR: Please increase DISK_SIZE to at least 6 GB." && exit 83 fi @@ -22,24 +22,34 @@ if [ -f "${DATA}" ]; then OLD_SIZE=$(stat -c%s "${DATA}") - # Resize disk to new size if needed + if [ "$DATA_SIZE" -gt "$OLD_SIZE" ]; then - if [ "$NEW_SIZE" -ne "$OLD_SIZE" ]; then - - if [ "$NEW_SIZE" -gt "$OLD_SIZE" ]; then - echo "Resizing data disk from $OLD_SIZE to $NEW_SIZE bytes.." - fallocate -l "${NEW_SIZE}" "${DATA}" + echo "Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.." + + REQ=$((DATA_SIZE-OLD_SIZE)) + + # Check free diskspace + SPACE=$(df --output=avail -B 1 "${IMG}" | tail -n 1) + + if (( REQ > SPACE )); then + echo "ERROR: Not enough free space to resize virtual disk." && exit 84 fi - if [ "$NEW_SIZE" -lt "$OLD_SIZE" ]; then - - echo "INFO: Shrinking existing disks is not supported yet!" - echo "INFO: Creating backup of old drive in storage folder..." - - mv -f "${DATA}" "${DATA}.bak" - + if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then + echo "ERROR: Could not allocate file for virtual disk." && exit 85 fi + fi + + if [ "$DATA_SIZE" -lt "$OLD_SIZE" ]; then + + echo "INFO: Shrinking existing disks is not supported yet!" + echo "INFO: Creating backup of old drive in storage folder..." + + mv -f "${DATA}" "${DATA}.bak" + + fi + fi if [ ! -f "${DATA}" ]; then @@ -47,19 +57,19 @@ if [ ! -f "${DATA}" ]; then # Check free diskspace SPACE=$(df --output=avail -B 1 "${IMG}" | tail -n 1) - if (( NEW_SIZE > SPACE )); then - echo "ERROR: Not enough free space to create virtual disk." && exit 84 + if (( DATA_SIZE > SPACE )); then + echo "ERROR: Not enough free space to create virtual disk." && exit 86 fi # Create an empty file - if ! fallocate -l "${NEW_SIZE}" "${DATA}"; then + if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then rm -f "${DATA}" - echo "ERROR: Could not allocate file for virtual disk." && exit 85 + echo "ERROR: Could not allocate file for virtual disk." && exit 87 fi # Check if file exists if [ ! -f "${DATA}" ]; then - echo "ERROR: Virtual DSM data disk does not exist ($DATA)" && exit 86 + echo "ERROR: Virtual DSM data disk does not exist ($DATA)" && exit 88 fi # Format as BTRFS filesystem