Allow disk resizing

Allow disk resizing
This commit is contained in:
Kroese 2023-04-15 17:53:14 +02:00 committed by GitHub
parent ac023cc6ec
commit a8a81d1232

38
disk.sh
View File

@ -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,16 +22,26 @@ 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
echo "Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.."
if [ "$NEW_SIZE" -gt "$OLD_SIZE" ]; then
echo "Resizing data disk from $OLD_SIZE to $NEW_SIZE bytes.."
fallocate -l "${NEW_SIZE}" "${DATA}"
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
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..."
@ -39,7 +49,7 @@ if [ -f "${DATA}" ]; then
mv -f "${DATA}" "${DATA}.bak"
fi
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