Allow disk resizing

This commit is contained in:
Kroese 2023-04-15 17:30:37 +02:00
parent 2bebca6c58
commit ac023cc6ec

65
disk.sh
View File

@ -7,52 +7,65 @@ SYSTEM="$IMG/$BASE.system.img"
[ ! -f "$BOOT" ] && echo "ERROR: Virtual DSM boot-image does not exist ($BOOT)" && exit 81
[ ! -f "$SYSTEM" ] && echo "ERROR: Virtual DSM system-image does not exist ($SYSTEM)" && exit 82
DATA="${IMG}/data.img"
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="$IMG/data$DISK_SIZE.img"
if (( NEW_SIZE < 6442450944 )); then
echo "ERROR: Please increase DISK_SIZE to at least 6 GB." && exit 83
fi
if [ ! -f "$DATA" ]; then
if [ -f "${DATA}" ]; then
OLD_SIZE=$(stat -c%s "${DATA}")
# Resize disk to new size if needed
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}"
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"
fi
fi
fi
if [ ! -f "${DATA}" ]; then
# Check free diskspace
SPACE=$(df --output=avail -B 1 "$IMG" | tail -n 1)
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 87
echo "ERROR: Not enough free space to create virtual disk." && exit 84
fi
# Create an empty file
if ! fallocate -l "${NEW_SIZE}" "${DATA}"; then
rm -f "${DATA}"
echo "ERROR: Could not allocate file for virtual disk." && exit 88
echo "ERROR: Could not allocate file for virtual disk." && exit 85
fi
# Check if file exists
if [ ! -f "$DATA" ]; then
echo "ERROR: Virtual DSM data-image does not exist ($DATA)" && exit 83
if [ ! -f "${DATA}" ]; then
echo "ERROR: Virtual DSM data disk does not exist ($DATA)" && exit 86
fi
# Format as BTRFS filesystem
mkfs.btrfs -q -L data -d single -m dup "${DATA}" > /dev/null
fi
# Resizing requires mounting a loop device which in turn requires
# the container to be privileged, so we must disable it for now.
#
# OLD_SIZE=$(stat -c%s "${DATA}")
#
# if [ "$NEW_SIZE" -ne "$OLD_SIZE" ]; then
# echo "Resizing data disk from $OLD_SIZE to $NEW_SIZE bytes"
#
# if [ "$NEW_SIZE" -gt "$OLD_SIZE" ]; then
# fallocate -l "${NEW_SIZE}" "${DATA}"
# btrfs filesystem resize "${NEW_SIZE}" "${DATA}"
# fi
#
# if [ "$NEW_SIZE" -lt "$OLD_SIZE" ]; then
# btrfs filesystem resize "${NEW_SIZE}" "${DATA}"
# fallocate -l "${NEW_SIZE}" "${DATA}"
# fi
# fi
fi
KVM_DISK_OPTS="\
-device virtio-scsi-pci,id=hw-synoboot,bus=pcie.0,addr=0xa \