2023-04-02 21:43:28 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -eu
|
|
|
|
|
2023-04-16 16:18:09 +02:00
|
|
|
BOOT="$STORAGE/$BASE.boot.img"
|
|
|
|
SYSTEM="$STORAGE/$BASE.system.img"
|
2023-04-15 02:20:36 +02:00
|
|
|
|
|
|
|
[ ! -f "$BOOT" ] && echo "ERROR: Virtual DSM boot-image does not exist ($BOOT)" && exit 81
|
2023-04-14 17:39:21 +02:00
|
|
|
[ ! -f "$SYSTEM" ] && echo "ERROR: Virtual DSM system-image does not exist ($SYSTEM)" && exit 82
|
2023-04-02 21:43:28 +02:00
|
|
|
|
2023-04-16 16:18:09 +02:00
|
|
|
DATA="${STORAGE}/data.img"
|
2023-04-15 17:30:37 +02:00
|
|
|
|
2023-04-16 16:18:09 +02:00
|
|
|
if [[ ! -f "${DATA}" ]] && [[ -f "$STORAGE/data$DISK_SIZE.img" ]]; then
|
2023-04-15 19:41:54 +02:00
|
|
|
# Fallback for legacy installs
|
2023-04-16 16:18:09 +02:00
|
|
|
DATA="$STORAGE/data$DISK_SIZE.img"
|
2023-04-15 19:41:54 +02:00
|
|
|
fi
|
2023-04-15 17:30:37 +02:00
|
|
|
|
2023-04-15 19:41:54 +02:00
|
|
|
DISK_SIZE=$(echo "${DISK_SIZE}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
|
2023-04-15 17:53:14 +02:00
|
|
|
DATA_SIZE=$(numfmt --from=iec "${DISK_SIZE}")
|
2023-04-10 15:27:09 +02:00
|
|
|
|
2023-04-15 17:53:14 +02:00
|
|
|
if (( DATA_SIZE < 6442450944 )); then
|
2023-04-15 17:30:37 +02:00
|
|
|
echo "ERROR: Please increase DISK_SIZE to at least 6 GB." && exit 83
|
|
|
|
fi
|
2023-04-02 21:43:28 +02:00
|
|
|
|
2023-04-15 17:30:37 +02:00
|
|
|
if [ -f "${DATA}" ]; then
|
2023-04-15 02:20:36 +02:00
|
|
|
|
2023-04-15 17:30:37 +02:00
|
|
|
OLD_SIZE=$(stat -c%s "${DATA}")
|
|
|
|
|
2023-04-15 17:53:14 +02:00
|
|
|
if [ "$DATA_SIZE" -gt "$OLD_SIZE" ]; then
|
|
|
|
|
2023-04-15 18:00:07 +02:00
|
|
|
echo "INFO: Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.."
|
2023-04-15 17:53:14 +02:00
|
|
|
|
|
|
|
REQ=$((DATA_SIZE-OLD_SIZE))
|
|
|
|
|
|
|
|
# Check free diskspace
|
2023-04-16 16:18:09 +02:00
|
|
|
SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)
|
2023-04-15 17:53:14 +02:00
|
|
|
|
|
|
|
if (( REQ > SPACE )); then
|
|
|
|
echo "ERROR: Not enough free space to resize virtual disk." && exit 84
|
|
|
|
fi
|
2023-04-15 17:30:37 +02:00
|
|
|
|
2023-04-15 17:53:14 +02:00
|
|
|
if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then
|
|
|
|
echo "ERROR: Could not allocate file for virtual disk." && exit 85
|
2023-04-15 02:15:18 +02:00
|
|
|
fi
|
2023-04-15 17:53:14 +02:00
|
|
|
|
|
|
|
fi
|
2023-04-15 17:30:37 +02:00
|
|
|
|
2023-04-15 17:53:14 +02:00
|
|
|
if [ "$DATA_SIZE" -lt "$OLD_SIZE" ]; then
|
2023-04-15 17:30:37 +02:00
|
|
|
|
2023-04-15 17:53:14 +02:00
|
|
|
echo "INFO: Shrinking existing disks is not supported yet!"
|
|
|
|
echo "INFO: Creating backup of old drive in storage folder..."
|
2023-04-15 17:30:37 +02:00
|
|
|
|
2023-04-15 17:53:14 +02:00
|
|
|
mv -f "${DATA}" "${DATA}.bak"
|
2023-04-15 17:30:37 +02:00
|
|
|
|
|
|
|
fi
|
2023-04-15 17:53:14 +02:00
|
|
|
|
2023-04-02 21:43:28 +02:00
|
|
|
fi
|
|
|
|
|
2023-04-15 17:30:37 +02:00
|
|
|
if [ ! -f "${DATA}" ]; then
|
|
|
|
|
|
|
|
# Check free diskspace
|
2023-04-16 16:18:09 +02:00
|
|
|
SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)
|
2023-04-15 17:30:37 +02:00
|
|
|
|
2023-04-15 17:53:14 +02:00
|
|
|
if (( DATA_SIZE > SPACE )); then
|
|
|
|
echo "ERROR: Not enough free space to create virtual disk." && exit 86
|
2023-04-15 17:30:37 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Create an empty file
|
2023-04-15 17:53:14 +02:00
|
|
|
if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then
|
2023-04-15 17:30:37 +02:00
|
|
|
rm -f "${DATA}"
|
2023-04-15 17:53:14 +02:00
|
|
|
echo "ERROR: Could not allocate file for virtual disk." && exit 87
|
2023-04-15 17:30:37 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if file exists
|
|
|
|
if [ ! -f "${DATA}" ]; then
|
2023-04-15 17:53:14 +02:00
|
|
|
echo "ERROR: Virtual DSM data disk does not exist ($DATA)" && exit 88
|
2023-04-15 17:30:37 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Format as BTRFS filesystem
|
|
|
|
mkfs.btrfs -q -L data -d single -m dup "${DATA}" > /dev/null
|
|
|
|
|
|
|
|
fi
|
2023-04-10 15:27:09 +02:00
|
|
|
|
2023-04-17 03:30:34 +02:00
|
|
|
AGENT="${STORAGE}/${BASE}.agent"
|
|
|
|
[ ! -f "$AGENT" ] && echo "1" > "$AGENT"
|
|
|
|
AGENT_VERSION=$(cat "${AGENT}")
|
|
|
|
|
|
|
|
if ((AGENT_VERSION < 3)); then
|
|
|
|
echo "INFO: The installed Guest Agent is outdated, please consider updating it or do a clean install."
|
|
|
|
fi
|
|
|
|
|
2023-04-02 21:43:28 +02:00
|
|
|
KVM_DISK_OPTS="\
|
2023-04-03 13:30:19 +02:00
|
|
|
-device virtio-scsi-pci,id=hw-synoboot,bus=pcie.0,addr=0xa \
|
2023-04-14 17:39:21 +02:00
|
|
|
-drive file=${BOOT},if=none,id=drive-synoboot,format=raw,cache=none,aio=native,discard=on,detect-zeroes=on \
|
2023-04-08 00:15:17 +02:00
|
|
|
-device scsi-hd,bus=hw-synoboot.0,channel=0,scsi-id=0,lun=0,drive=drive-synoboot,id=synoboot0,rotation_rate=1,bootindex=1 \
|
2023-04-03 13:30:19 +02:00
|
|
|
-device virtio-scsi-pci,id=hw-synosys,bus=pcie.0,addr=0xb \
|
2023-04-14 17:39:21 +02:00
|
|
|
-drive file=${SYSTEM},if=none,id=drive-synosys,format=raw,cache=none,aio=native,discard=on,detect-zeroes=on \
|
2023-04-08 00:15:17 +02:00
|
|
|
-device scsi-hd,bus=hw-synosys.0,channel=0,scsi-id=0,lun=0,drive=drive-synosys,id=synosys0,rotation_rate=1,bootindex=2 \
|
2023-04-03 13:30:19 +02:00
|
|
|
-device virtio-scsi-pci,id=hw-userdata,bus=pcie.0,addr=0xc \
|
2023-04-14 17:39:21 +02:00
|
|
|
-drive file=${DATA},if=none,id=drive-userdata,format=raw,cache=none,aio=native,discard=on,detect-zeroes=on \
|
2023-04-08 00:15:17 +02:00
|
|
|
-device scsi-hd,bus=hw-userdata.0,channel=0,scsi-id=0,lun=0,drive=drive-userdata,id=userdata0,rotation_rate=1,bootindex=3"
|