2023-04-03 02:43:28 +07:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -eu
|
|
|
|
|
2023-04-23 06:47:27 +07:00
|
|
|
# Docker environment variables
|
2023-04-20 01:56:09 +07:00
|
|
|
|
2023-04-27 02:53:07 +07:00
|
|
|
: ${DISK_IO:='native'} # I/O Mode, can be set to 'native', 'threads' or 'io_turing'
|
|
|
|
: ${DISK_ROTATION:='1'} # Rotation rate, set to 1 for SSD storage and increase for HDD
|
2023-04-20 02:01:52 +07:00
|
|
|
: ${DISK_CACHE:='none'} # Caching mode, can be set to 'writeback' for better performance
|
2023-04-20 01:56:09 +07:00
|
|
|
|
2023-04-16 21:18:09 +07:00
|
|
|
BOOT="$STORAGE/$BASE.boot.img"
|
|
|
|
SYSTEM="$STORAGE/$BASE.system.img"
|
2023-04-15 07:20:36 +07:00
|
|
|
|
|
|
|
[ ! -f "$BOOT" ] && echo "ERROR: Virtual DSM boot-image does not exist ($BOOT)" && exit 81
|
2023-04-14 22:39:21 +07:00
|
|
|
[ ! -f "$SYSTEM" ] && echo "ERROR: Virtual DSM system-image does not exist ($SYSTEM)" && exit 82
|
2023-04-03 02:43:28 +07:00
|
|
|
|
2023-04-16 21:18:09 +07:00
|
|
|
DATA="${STORAGE}/data.img"
|
2023-04-15 22:30:37 +07:00
|
|
|
|
2023-04-16 21:18:09 +07:00
|
|
|
if [[ ! -f "${DATA}" ]] && [[ -f "$STORAGE/data$DISK_SIZE.img" ]]; then
|
2023-04-16 00:41:54 +07:00
|
|
|
# Fallback for legacy installs
|
2023-04-16 21:18:09 +07:00
|
|
|
DATA="$STORAGE/data$DISK_SIZE.img"
|
2023-04-16 00:41:54 +07:00
|
|
|
fi
|
2023-04-15 22:30:37 +07:00
|
|
|
|
2023-04-16 00:41:54 +07:00
|
|
|
DISK_SIZE=$(echo "${DISK_SIZE}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
|
2023-04-15 22:53:14 +07:00
|
|
|
DATA_SIZE=$(numfmt --from=iec "${DISK_SIZE}")
|
2023-04-10 20:27:09 +07:00
|
|
|
|
2023-04-15 22:53:14 +07:00
|
|
|
if (( DATA_SIZE < 6442450944 )); then
|
2023-04-15 22:30:37 +07:00
|
|
|
echo "ERROR: Please increase DISK_SIZE to at least 6 GB." && exit 83
|
|
|
|
fi
|
2023-04-03 02:43:28 +07:00
|
|
|
|
2023-04-15 22:30:37 +07:00
|
|
|
if [ -f "${DATA}" ]; then
|
2023-04-15 07:20:36 +07:00
|
|
|
|
2023-04-15 22:30:37 +07:00
|
|
|
OLD_SIZE=$(stat -c%s "${DATA}")
|
|
|
|
|
2023-04-15 22:53:14 +07:00
|
|
|
if [ "$DATA_SIZE" -gt "$OLD_SIZE" ]; then
|
|
|
|
|
2023-04-15 23:00:07 +07:00
|
|
|
echo "INFO: Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.."
|
2023-04-15 22:30:37 +07:00
|
|
|
|
2023-04-19 09:06:04 +07:00
|
|
|
if [ "$ALLOCATE" = "N" ]; then
|
2023-04-18 23:05:52 +07:00
|
|
|
|
2023-04-22 22:10:40 +07:00
|
|
|
# Resize file by changing its length
|
2023-04-18 23:05:52 +07:00
|
|
|
truncate -s "${DATA_SIZE}" "${DATA}";
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
REQ=$((DATA_SIZE-OLD_SIZE))
|
|
|
|
|
|
|
|
# Check free diskspace
|
|
|
|
SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)
|
|
|
|
|
|
|
|
if (( REQ > SPACE )); then
|
2023-04-18 23:36:54 +07:00
|
|
|
echo "ERROR: Not enough free space to resize virtual disk to ${DISK_SIZE}."
|
2023-04-20 01:22:13 +07:00
|
|
|
echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 84
|
2023-04-18 23:05:52 +07:00
|
|
|
fi
|
|
|
|
|
2023-04-22 22:10:40 +07:00
|
|
|
# Resize file by allocating more space
|
2023-04-19 11:54:40 +07:00
|
|
|
if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then
|
|
|
|
echo "ERROR: Could not allocate a file for the virtual disk." && exit 85
|
|
|
|
fi
|
|
|
|
|
2023-04-19 09:06:04 +07:00
|
|
|
if [ "$ALLOCATE" = "Z" ]; then
|
2023-04-19 08:55:32 +07:00
|
|
|
|
|
|
|
GB=$(( (REQ + 1073741823)/1073741824 ))
|
|
|
|
|
2023-04-19 12:09:08 +07:00
|
|
|
echo "INFO: Preallocating ${GB} GB of diskspace, please wait..."
|
2023-04-19 10:21:36 +07:00
|
|
|
dd if=/dev/urandom of="${DATA}" seek="${OLD_SIZE}" count="${REQ}" bs=1M iflag=count_bytes oflag=seek_bytes status=none
|
2023-04-18 23:05:52 +07:00
|
|
|
|
2023-04-19 08:55:32 +07:00
|
|
|
fi
|
2023-04-15 07:15:18 +07:00
|
|
|
fi
|
2023-04-15 22:53:14 +07:00
|
|
|
fi
|
2023-04-15 22:30:37 +07:00
|
|
|
|
2023-04-15 22:53:14 +07:00
|
|
|
if [ "$DATA_SIZE" -lt "$OLD_SIZE" ]; then
|
2023-04-15 22:30:37 +07:00
|
|
|
|
2023-04-15 22:53:14 +07:00
|
|
|
echo "INFO: Shrinking existing disks is not supported yet!"
|
|
|
|
echo "INFO: Creating backup of old drive in storage folder..."
|
2023-04-15 22:30:37 +07:00
|
|
|
|
2023-04-15 22:53:14 +07:00
|
|
|
mv -f "${DATA}" "${DATA}.bak"
|
2023-04-15 22:30:37 +07:00
|
|
|
|
|
|
|
fi
|
2023-04-03 02:43:28 +07:00
|
|
|
fi
|
|
|
|
|
2023-04-15 22:30:37 +07:00
|
|
|
if [ ! -f "${DATA}" ]; then
|
|
|
|
|
2023-04-19 08:19:52 +07:00
|
|
|
if [ "$ALLOCATE" = "N" ]; then
|
2023-04-18 22:59:19 +07:00
|
|
|
|
2023-04-22 22:10:40 +07:00
|
|
|
# Create an empty file
|
2023-04-18 22:59:19 +07:00
|
|
|
truncate -s "${DATA_SIZE}" "${DATA}"
|
|
|
|
|
|
|
|
else
|
|
|
|
|
2023-04-18 23:05:52 +07:00
|
|
|
# Check free diskspace
|
|
|
|
SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)
|
|
|
|
|
|
|
|
if (( DATA_SIZE > SPACE )); then
|
2023-04-18 23:22:11 +07:00
|
|
|
echo "ERROR: Not enough free space to create a virtual disk of ${DISK_SIZE}."
|
2023-04-20 01:22:13 +07:00
|
|
|
echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 86
|
2023-04-18 23:05:52 +07:00
|
|
|
fi
|
|
|
|
|
2023-04-22 22:10:40 +07:00
|
|
|
# Create an empty file
|
2023-04-19 11:54:40 +07:00
|
|
|
if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then
|
|
|
|
rm -f "${DATA}"
|
|
|
|
echo "ERROR: Could not allocate a file for the virtual disk." && exit 87
|
|
|
|
fi
|
2023-04-19 08:19:52 +07:00
|
|
|
|
2023-04-19 11:54:40 +07:00
|
|
|
if [ "$ALLOCATE" = "Z" ]; then
|
2023-04-19 08:19:52 +07:00
|
|
|
|
2023-04-19 12:09:08 +07:00
|
|
|
echo "INFO: Preallocating ${DISK_SIZE} of diskspace, please wait..."
|
2023-04-19 10:21:36 +07:00
|
|
|
dd if=/dev/urandom of="${DATA}" count="${DATA_SIZE}" bs=1M iflag=count_bytes status=none
|
2023-04-19 08:19:52 +07:00
|
|
|
|
|
|
|
fi
|
2023-04-15 22:30:37 +07:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if file exists
|
|
|
|
if [ ! -f "${DATA}" ]; then
|
2023-04-19 08:19:52 +07:00
|
|
|
echo "ERROR: Virtual disk does not exist ($DATA)" && exit 88
|
|
|
|
fi
|
|
|
|
|
2023-04-15 22:30:37 +07:00
|
|
|
# Format as BTRFS filesystem
|
|
|
|
mkfs.btrfs -q -L data -d single -m dup "${DATA}" > /dev/null
|
|
|
|
|
|
|
|
fi
|
2023-04-10 20:27:09 +07:00
|
|
|
|
2023-04-19 08:55:32 +07:00
|
|
|
# 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
|
|
|
|
|
2023-04-17 08:30:34 +07:00
|
|
|
AGENT="${STORAGE}/${BASE}.agent"
|
2023-04-18 22:59:19 +07:00
|
|
|
[ -f "$AGENT" ] && AGENT_VERSION=$(cat "${AGENT}") || AGENT_VERSION=1
|
2023-04-17 08:30:34 +07:00
|
|
|
|
2023-04-18 02:10:38 +07:00
|
|
|
if ((AGENT_VERSION < 5)); then
|
2023-04-18 22:59:19 +07:00
|
|
|
echo "INFO: The installed VirtualDSM Agent v${AGENT_VERSION} is an outdated version, please upgrade it."
|
2023-04-17 08:30:34 +07:00
|
|
|
fi
|
|
|
|
|
2023-04-19 21:13:48 +07:00
|
|
|
DISK_OPTS="\
|
2023-04-03 18:30:19 +07:00
|
|
|
-device virtio-scsi-pci,id=hw-synoboot,bus=pcie.0,addr=0xa \
|
2023-04-20 01:56:09 +07:00
|
|
|
-drive file=${BOOT},if=none,id=drive-synoboot,format=raw,cache=${DISK_CACHE},aio=${DISK_IO},discard=on,detect-zeroes=on \
|
|
|
|
-device scsi-hd,bus=hw-synoboot.0,channel=0,scsi-id=0,lun=0,drive=drive-synoboot,id=synoboot0,rotation_rate=${DISK_ROTATION},bootindex=1 \
|
2023-04-03 18:30:19 +07:00
|
|
|
-device virtio-scsi-pci,id=hw-synosys,bus=pcie.0,addr=0xb \
|
2023-04-20 01:56:09 +07:00
|
|
|
-drive file=${SYSTEM},if=none,id=drive-synosys,format=raw,cache=${DISK_CACHE},aio=${DISK_IO},discard=on,detect-zeroes=on \
|
|
|
|
-device scsi-hd,bus=hw-synosys.0,channel=0,scsi-id=0,lun=0,drive=drive-synosys,id=synosys0,rotation_rate=${DISK_ROTATION},bootindex=2 \
|
2023-04-03 18:30:19 +07:00
|
|
|
-device virtio-scsi-pci,id=hw-userdata,bus=pcie.0,addr=0xc \
|
2023-04-20 01:56:09 +07:00
|
|
|
-drive file=${DATA},if=none,id=drive-userdata,format=raw,cache=${DISK_CACHE},aio=${DISK_IO},discard=on,detect-zeroes=on \
|
|
|
|
-device scsi-hd,bus=hw-userdata.0,channel=0,scsi-id=0,lun=0,drive=drive-userdata,id=userdata0,rotation_rate=${DISK_ROTATION},bootindex=3"
|