virtual-dsm/run/disk.sh

240 lines
8.5 KiB
Bash
Raw Normal View History

2023-04-02 21:43:28 +02:00
#!/usr/bin/env bash
2023-05-03 18:59:25 +02:00
set -Eeuo pipefail
2023-04-02 21:43:28 +02:00
2023-04-23 01:47:27 +02:00
# Docker environment variables
2023-04-19 20:56:09 +02:00
2023-05-11 19:37:17 +02:00
: ${DISK_IO:='native'} # I/O Mode, can be set to 'native', 'threads' or 'io_turing'
2023-04-19 21:01:52 +02:00
: ${DISK_CACHE:='none'} # Caching mode, can be set to 'writeback' for better performance
2023-04-29 00:48:52 +02:00
: ${DISK_DISCARD:='on'} # Controls whether unmap (TRIM) commands are passed to the host.
: ${DISK_ROTATION:='1'} # Rotation rate, set to 1 for SSD storage and increase for HDD
2023-04-19 20:56:09 +02:00
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
2023-05-11 03:49:40 +02:00
[ ! -f "$BOOT" ] && error "Virtual DSM boot-image does not exist ($BOOT)" && exit 81
[ ! -f "$SYSTEM" ] && 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
MIN_SIZE=6442450944
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')
DATA_SIZE=$(numfmt --from=iec "${DISK_SIZE}")
2023-04-10 15:27:09 +02:00
if (( DATA_SIZE < MIN_SIZE )); then
2023-05-11 03:49:40 +02:00
error "Please increase DISK_SIZE to at least 6 GB." && exit 83
2023-04-15 17:30:37 +02:00
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}")
if [ "$DATA_SIZE" -gt "$OLD_SIZE" ]; then
2023-05-11 03:49:40 +02:00
info "Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.."
2023-04-15 17:30:37 +02:00
2023-05-06 13:26:46 +02:00
if [[ "${ALLOCATE}" == [Nn]* ]]; then
2023-04-18 18:05:52 +02:00
2023-04-22 17:10:40 +02:00
# Resize file by changing its length
2023-07-03 11:38:11 +02:00
if ! truncate -s "${DATA_SIZE}" "${DATA}"; then
error "Could not resize the file for the virtual disk." && exit 85
fi
2023-04-18 18:05:52 +02:00
else
REQ=$((DATA_SIZE-OLD_SIZE))
2023-07-04 18:46:16 +02:00
# Check free diskspace
2023-04-18 18:05:52 +02:00
SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)
if (( REQ > SPACE )); then
2023-05-11 03:49:40 +02:00
error "Not enough free space to resize virtual disk to ${DISK_SIZE}."
error "Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 84
2023-04-18 18:05:52 +02:00
fi
2023-04-22 17:10:40 +02:00
# Resize file by allocating more space
2023-04-19 06:54:40 +02:00
if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then
2023-07-03 11:38:11 +02:00
if ! truncate -s "${DATA_SIZE}" "${DATA}"; then
error "Could not resize the file for the virtual disk." && exit 85
fi
2023-04-19 06:54:40 +02:00
fi
2023-05-06 13:26:46 +02:00
if [[ "${ALLOCATE}" == [Zz]* ]]; then
GB=$(( (REQ + 1073741823)/1073741824 ))
2023-05-11 03:49:40 +02:00
info "Preallocating ${GB} GB of diskspace, please wait..."
2023-04-19 05:21:36 +02: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 18:05:52 +02:00
fi
2023-04-15 02:15:18 +02:00
fi
fi
2023-04-15 17:30:37 +02:00
if [ "$DATA_SIZE" -lt "$OLD_SIZE" ]; then
2023-04-15 17:30:37 +02:00
2023-05-11 03:49:40 +02:00
info "Shrinking existing disks is not supported yet!"
info "Creating backup of old drive in storage folder..."
2023-04-15 17:30:37 +02:00
mv -f "${DATA}" "${DATA}.bak"
2023-04-15 17:30:37 +02:00
fi
2023-04-02 21:43:28 +02:00
fi
2023-04-15 17:30:37 +02:00
if [ ! -f "${DATA}" ]; then
2023-05-06 13:26:46 +02:00
if [[ "${ALLOCATE}" == [Nn]* ]]; then
2023-04-18 17:59:19 +02:00
2023-04-22 17:10:40 +02:00
# Create an empty file
2023-07-03 11:38:11 +02:00
if ! truncate -s "${DATA_SIZE}" "${DATA}"; then
rm -f "${DATA}"
error "Could not create a file for the virtual disk." && exit 87
fi
2023-04-18 17:59:19 +02:00
else
2023-04-18 18:05:52 +02:00
# Check free diskspace
SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)
if (( DATA_SIZE > SPACE )); then
2023-05-11 03:49:40 +02:00
error "Not enough free space to create a virtual disk of ${DISK_SIZE}."
error "Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 86
2023-04-18 18:05:52 +02:00
fi
2023-04-22 17:10:40 +02:00
# Create an empty file
2023-04-19 06:54:40 +02:00
if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then
2023-07-03 11:38:11 +02:00
if ! truncate -s "${DATA_SIZE}" "${DATA}"; then
rm -f "${DATA}"
error "Could not create a file for the virtual disk." && exit 87
fi
2023-04-19 06:54:40 +02:00
fi
2023-05-06 13:26:46 +02:00
if [[ "${ALLOCATE}" == [Zz]* ]]; then
2023-05-11 03:49:40 +02:00
info "Preallocating ${DISK_SIZE} of diskspace, please wait..."
2023-04-19 05:21:36 +02:00
dd if=/dev/urandom of="${DATA}" count="${DATA_SIZE}" bs=1M iflag=count_bytes status=none
fi
2023-04-15 17:30:37 +02:00
fi
# Check if file exists
if [ ! -f "${DATA}" ]; then
2023-05-11 03:49:40 +02:00
error "Virtual disk does not exist ($DATA)" && exit 88
fi
2023-04-15 17:30:37 +02:00
fi
2023-04-10 15:27:09 +02:00
# Check the filesize
SIZE=$(stat -c%s "${DATA}")
if [[ SIZE -ne DATA_SIZE ]]; then
2023-05-11 03:49:40 +02:00
error "Virtual disk has the wrong size: ${SIZE}" && exit 89
fi
2023-04-19 16:13:48 +02:00
DISK_OPTS="\
2023-04-03 13:30:19 +02:00
-device virtio-scsi-pci,id=hw-synoboot,bus=pcie.0,addr=0xa \
2023-04-29 00:48:52 +02:00
-drive file=${BOOT},if=none,id=drive-synoboot,format=raw,cache=${DISK_CACHE},aio=${DISK_IO},discard=${DISK_DISCARD},detect-zeroes=on \
2023-04-19 20:56:09 +02:00
-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 13:30:19 +02:00
-device virtio-scsi-pci,id=hw-synosys,bus=pcie.0,addr=0xb \
2023-04-29 00:48:52 +02:00
-drive file=${SYSTEM},if=none,id=drive-synosys,format=raw,cache=${DISK_CACHE},aio=${DISK_IO},discard=${DISK_DISCARD},detect-zeroes=on \
2023-04-19 20:56:09 +02:00
-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 13:30:19 +02:00
-device virtio-scsi-pci,id=hw-userdata,bus=pcie.0,addr=0xc \
2023-04-29 00:48:52 +02:00
-drive file=${DATA},if=none,id=drive-userdata,format=raw,cache=${DISK_CACHE},aio=${DISK_IO},discard=${DISK_DISCARD},detect-zeroes=on \
2023-04-19 20:56:09 +02:00
-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"
2023-10-09 14:10:31 +02:00
: ${DISK2_SIZE:=''}
EXTRA_SIZE=DISK2_SIZE
2023-10-10 06:39:59 +02:00
EXTRA_DISK="/storage2/data.img"
2023-10-09 14:10:31 +02:00
if [ -d "$(dirname "${EXTRA_DISK}")" ]; then
if [ ! -f "${EXTRA_DISK}" ]; then
[ -z "$EXTRA_SIZE" ] && EXTRA_SIZE="16G"
if ! truncate -s "${EXTRA_SIZE}" "${EXTRA_DISK}"; then
error "Could not create the file for the second disk." && exit 53
fi
fi
2023-10-09 18:30:13 +02:00
if [ -n "$EXTRA_SIZE" ]; then
CUR_SIZE=$(stat -c%s "${EXTRA_DISK}")
DATA_SIZE=$(numfmt --from=iec "${EXTRA_SIZE}")
if [ "$DATA_SIZE" -gt "$CUR_SIZE" ]; then
truncate -s "${EXTRA_SIZE}" "${EXTRA_DISK}"
fi
fi
2023-10-09 14:10:31 +02:00
DISK_OPTS="${DISK_OPTS} \
2023-10-09 18:30:13 +02:00
-device virtio-scsi-pci,id=hw-userdata2,bus=pcie.0,addr=0xd \
2023-10-10 06:39:59 +02:00
-drive file=${EXTRA_DISK},if=none,id=drive-userdata2,format=raw,cache=${DISK_CACHE},aio=${DISK_IO},discard=${DISK_DISCARD},detect-zeroes=on \
2023-10-09 18:30:13 +02:00
-device scsi-hd,bus=hw-userdata2.0,channel=0,scsi-id=0,lun=0,drive=drive-userdata2,id=userdata2,rotation_rate=${DISK_ROTATION},bootindex=4"
2023-10-09 14:10:31 +02:00
fi
: ${DISK3_SIZE:=''}
EXTRA_SIZE=DISK3_SIZE
2023-10-10 06:39:59 +02:00
EXTRA_DISK="/storage3/data.img"
2023-10-09 14:10:31 +02:00
if [ -d "$(dirname "${EXTRA_DISK}")" ]; then
if [ ! -f "${EXTRA_DISK}" ]; then
[ -z "$EXTRA_SIZE" ] && EXTRA_SIZE="16G"
if ! truncate -s "${EXTRA_SIZE}" "${EXTRA_DISK}"; then
error "Could not create the file for the third disk." && exit 54
fi
fi
if [ -n "$EXTRA_SIZE" ]; then
CUR_SIZE=$(stat -c%s "${EXTRA_DISK}")
DATA_SIZE=$(numfmt --from=iec "${EXTRA_SIZE}")
if [ "$DATA_SIZE" -gt "$CUR_SIZE" ]; then
truncate -s "${EXTRA_SIZE}" "${EXTRA_DISK}"
fi
fi
2023-10-09 18:30:13 +02:00
2023-10-09 14:10:31 +02:00
DISK_OPTS="${DISK_OPTS} \
2023-10-09 18:30:13 +02:00
-device virtio-scsi-pci,id=hw-userdata3,bus=pcie.0,addr=0xe \
2023-10-10 06:39:59 +02:00
-drive file=${EXTRA_DISK},if=none,id=drive-userdata3,format=raw,cache=${DISK_CACHE},aio=${DISK_IO},discard=${DISK_DISCARD},detect-zeroes=on \
2023-10-09 18:30:13 +02:00
-device scsi-hd,bus=hw-userdata3.0,channel=0,scsi-id=0,lun=0,drive=drive-userdata3,id=userdata3,rotation_rate=${DISK_ROTATION},bootindex=5"
2023-10-09 14:10:31 +02:00
fi
2023-10-10 05:26:37 +02:00
2023-10-10 06:29:03 +02:00
: ${DEVICE:=''} # Docker variable to passthrough a block device, like /dev/vdc1.
2023-10-31 16:53:44 +01:00
: ${DEVICE2:=''}
: ${DEVICE3:=''}
2023-10-10 06:29:03 +02:00
if [ -n "${DEVICE}" ]; then
2023-10-10 05:26:37 +02:00
2023-10-10 06:29:03 +02:00
[ ! -b "${DEVICE}" ] && error "Device ${DEVICE} cannot be found! Please add it to the 'devices' section of your compose file." && exit 55
2023-10-10 05:26:37 +02:00
DISK_OPTS="${DISK_OPTS} \
-device virtio-scsi-pci,id=hw-userdata4,bus=pcie.0,addr=0xf \
-drive file=${DEVICE},if=none,id=drive-userdata4,format=raw,cache=${DISK_CACHE},aio=${DISK_IO},discard=${DISK_DISCARD},detect-zeroes=on \
-device scsi-hd,bus=hw-userdata4.0,channel=0,scsi-id=0,lun=0,drive=drive-userdata4,id=userdata4,rotation_rate=${DISK_ROTATION},bootindex=6"
fi
2023-10-31 16:53:44 +01:00
if [ -n "${DEVICE2}" ]; then
[ ! -b "${DEVICE2}" ] && error "Device ${DEVICE2} cannot be found! Please add it to the 'devices' section of your compose file." && exit 56
DISK_OPTS="${DISK_OPTS} \
-device virtio-scsi-pci,id=hw-userdata5,bus=pcie.0,addr=0x5 \
-drive file=${DEVICE2},if=none,id=drive-userdata5,format=raw,cache=${DISK_CACHE},aio=${DISK_IO},discard=${DISK_DISCARD},detect-zeroes=on \
-device scsi-hd,bus=hw-userdata5.0,channel=0,scsi-id=0,lun=0,drive=drive-userdata5,id=userdata5,rotation_rate=${DISK_ROTATION},bootindex=7"
fi
if [ -n "${DEVICE3}" ]; then
[ ! -b "${DEVICE3}" ] && error "Device ${DEVICE3} cannot be found! Please add it to the 'devices' section of your compose file." && exit 57
DISK_OPTS="${DISK_OPTS} \
-device virtio-scsi-pci,id=hw-userdata6,bus=pcie.0,addr=0x6 \
-drive file=${DEVICE3},if=none,id=drive-userdata6,format=raw,cache=${DISK_CACHE},aio=${DISK_IO},discard=${DISK_DISCARD},detect-zeroes=on \
-device scsi-hd,bus=hw-userdata6.0,channel=0,scsi-id=0,lun=0,drive=drive-userdata6,id=userdata6,rotation_rate=${DISK_ROTATION},bootindex=8"
fi