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
|
|
|
|
2024-01-14 16:01:15 +01:00
|
|
|
: "${DISK_IO:="native"}" # I/O Mode, can be set to 'native', 'threads' or 'io_turing'
|
|
|
|
: "${DISK_FMT:="raw"}" # Disk file format, 'raw' by default for best performance
|
|
|
|
: "${DISK_FLAGS:=""}" # Specifies the options for use with the qcow2 disk format
|
|
|
|
: "${DISK_CACHE:="none"}" # Caching mode, can be set to 'writeback' for better performance
|
|
|
|
: "${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
|
|
|
|
2024-04-16 15:35:35 +02:00
|
|
|
[ ! -s "$BOOT" ] && error "Virtual DSM boot-image does not exist ($BOOT)" && exit 81
|
|
|
|
[ ! -s "$SYSTEM" ] && error "Virtual DSM system-image does not exist ($SYSTEM)" && exit 82
|
2023-04-02 21:43:28 +02:00
|
|
|
|
2023-11-16 20:36:30 +01:00
|
|
|
DISK_OPTS="\
|
2023-12-29 17:34:23 +01:00
|
|
|
-object iothread,id=io2 \
|
|
|
|
-device virtio-scsi-pci,id=hw-synoboot,iothread=io2,bus=pcie.0,addr=0xa \
|
2023-12-09 21:19:08 +01:00
|
|
|
-drive file=$BOOT,if=none,id=drive-synoboot,format=raw,cache=$DISK_CACHE,aio=$DISK_IO,discard=$DISK_DISCARD,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-12-29 17:34:23 +01:00
|
|
|
-device virtio-scsi-pci,id=hw-synosys,iothread=io2,bus=pcie.0,addr=0xb \
|
2023-12-09 21:19:08 +01:00
|
|
|
-drive file=$SYSTEM,if=none,id=drive-synosys,format=raw,cache=$DISK_CACHE,aio=$DISK_IO,discard=$DISK_DISCARD,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-11-16 20:36:30 +01:00
|
|
|
|
2023-12-09 02:55:39 +01:00
|
|
|
fmt2ext() {
|
|
|
|
local DISK_FMT=$1
|
|
|
|
|
|
|
|
case "${DISK_FMT,,}" in
|
|
|
|
qcow2)
|
|
|
|
echo "qcow2"
|
|
|
|
;;
|
|
|
|
raw)
|
|
|
|
echo "img"
|
|
|
|
;;
|
|
|
|
*)
|
2023-12-16 01:30:34 +01:00
|
|
|
error "Unrecognized disk format: $DISK_FMT" && exit 78
|
2023-12-09 02:55:39 +01:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
ext2fmt() {
|
|
|
|
local DISK_EXT=$1
|
|
|
|
|
|
|
|
case "${DISK_EXT,,}" in
|
|
|
|
qcow2)
|
|
|
|
echo "qcow2"
|
|
|
|
;;
|
|
|
|
img)
|
|
|
|
echo "raw"
|
|
|
|
;;
|
|
|
|
*)
|
2023-12-16 01:30:34 +01:00
|
|
|
error "Unrecognized file extension: .$DISK_EXT" && exit 78
|
2023-12-09 02:55:39 +01:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
getSize() {
|
|
|
|
local DISK_FILE=$1
|
2023-12-10 00:26:13 +01:00
|
|
|
local DISK_EXT DISK_FMT
|
2023-12-09 02:55:39 +01:00
|
|
|
|
2024-01-20 19:59:44 +01:00
|
|
|
DISK_EXT=$(echo "${DISK_FILE//*./}" | sed 's/^.*\.//')
|
|
|
|
DISK_FMT=$(ext2fmt "$DISK_EXT")
|
2023-12-09 02:55:39 +01:00
|
|
|
|
|
|
|
case "${DISK_FMT,,}" in
|
|
|
|
raw)
|
2023-12-09 15:15:25 +01:00
|
|
|
stat -c%s "$DISK_FILE"
|
2023-12-09 02:55:39 +01:00
|
|
|
;;
|
|
|
|
qcow2)
|
2023-12-09 15:15:25 +01:00
|
|
|
qemu-img info "$DISK_FILE" -f "$DISK_FMT" | grep '^virtual size: ' | sed 's/.*(\(.*\) bytes)/\1/'
|
2023-12-09 02:55:39 +01:00
|
|
|
;;
|
|
|
|
*)
|
2023-12-16 01:30:34 +01:00
|
|
|
error "Unrecognized disk format: $DISK_FMT" && exit 78
|
2023-12-09 02:55:39 +01:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2023-12-24 02:41:44 +01:00
|
|
|
isCow() {
|
|
|
|
local FS=$1
|
|
|
|
|
2024-02-09 23:53:18 +01:00
|
|
|
if [[ "${FS,,}" == "btrfs" ]]; then
|
2023-12-24 02:41:44 +01:00
|
|
|
return 0
|
|
|
|
fi
|
2024-01-13 18:16:17 +01:00
|
|
|
|
2023-12-24 02:41:44 +01:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2024-04-26 06:04:48 +02:00
|
|
|
supportsDirect() {
|
|
|
|
local FS=$1
|
|
|
|
|
|
|
|
if [[ "${FS,,}" == "ecryptfs" ]] || [[ "${FS,,}" == "tmpfs" ]]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-12-18 16:44:26 +01:00
|
|
|
createDisk() {
|
|
|
|
local DISK_FILE=$1
|
|
|
|
local DISK_SPACE=$2
|
|
|
|
local DISK_DESC=$3
|
|
|
|
local DISK_FMT=$4
|
2023-12-24 02:41:44 +01:00
|
|
|
local FS=$5
|
|
|
|
local DATA_SIZE DIR SPACE FA
|
2023-12-18 16:44:26 +01:00
|
|
|
|
2023-12-19 08:54:08 +01:00
|
|
|
DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE")
|
|
|
|
|
2023-12-24 02:41:44 +01:00
|
|
|
rm -f "$DISK_FILE"
|
|
|
|
|
2023-12-18 16:44:26 +01:00
|
|
|
if [[ "$ALLOCATE" != [Nn]* ]]; then
|
|
|
|
|
|
|
|
# Check free diskspace
|
|
|
|
DIR=$(dirname "$DISK_FILE")
|
|
|
|
SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
|
|
|
|
|
|
|
|
if (( DATA_SIZE > SPACE )); then
|
2023-12-19 08:54:08 +01:00
|
|
|
local SPACE_GB=$(( (SPACE + 1073741823)/1073741824 ))
|
2023-12-18 16:44:26 +01:00
|
|
|
error "Not enough free space to create a $DISK_DESC of $DISK_SPACE in $DIR, it has only $SPACE_GB GB available..."
|
|
|
|
error "Please specify a smaller ${DISK_DESC^^}_SIZE or disable preallocation by setting ALLOCATE=N." && exit 76
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2024-01-23 00:15:11 +01:00
|
|
|
html "Creating a $DISK_DESC image..."
|
2024-01-25 17:02:50 +01:00
|
|
|
info "Creating a $DISK_SPACE $DISK_TYPE $DISK_DESC image in $DISK_FMT format..."
|
2024-01-20 19:59:44 +01:00
|
|
|
|
2023-12-20 03:34:26 +01:00
|
|
|
local FAIL="Could not create a $DISK_TYPE $DISK_FMT $DISK_DESC image of $DISK_SPACE ($DISK_FILE)"
|
2023-12-18 16:44:26 +01:00
|
|
|
|
|
|
|
case "${DISK_FMT,,}" in
|
|
|
|
raw)
|
|
|
|
|
2023-12-24 02:41:44 +01:00
|
|
|
if isCow "$FS"; then
|
|
|
|
if ! touch "$DISK_FILE"; then
|
|
|
|
error "$FAIL" && exit 77
|
|
|
|
fi
|
|
|
|
{ chattr +C "$DISK_FILE"; } || :
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
2024-01-13 18:16:17 +01:00
|
|
|
|
2023-12-18 16:44:26 +01:00
|
|
|
# Create an empty file
|
2023-12-19 08:54:08 +01:00
|
|
|
if ! truncate -s "$DATA_SIZE" "$DISK_FILE"; then
|
2023-12-18 16:44:26 +01:00
|
|
|
rm -f "$DISK_FILE"
|
|
|
|
error "$FAIL" && exit 77
|
|
|
|
fi
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
# Create an empty file
|
2023-12-19 08:54:08 +01:00
|
|
|
if ! fallocate -l "$DATA_SIZE" "$DISK_FILE"; then
|
|
|
|
if ! truncate -s "$DATA_SIZE" "$DISK_FILE"; then
|
2023-12-18 16:44:26 +01:00
|
|
|
rm -f "$DISK_FILE"
|
|
|
|
error "$FAIL" && exit 77
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
qcow2)
|
2023-12-24 02:41:44 +01:00
|
|
|
|
|
|
|
local DISK_PARAM="$DISK_ALLOC"
|
|
|
|
isCow "$FS" && DISK_PARAM="$DISK_PARAM,nocow=on"
|
|
|
|
[ -n "$DISK_FLAGS" ] && DISK_PARAM="$DISK_PARAM,$DISK_FLAGS"
|
|
|
|
|
|
|
|
if ! qemu-img create -f "$DISK_FMT" -o "$DISK_PARAM" -- "$DISK_FILE" "$DATA_SIZE" ; then
|
2023-12-18 16:44:26 +01:00
|
|
|
rm -f "$DISK_FILE"
|
|
|
|
error "$FAIL" && exit 70
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2023-12-24 14:27:29 +01:00
|
|
|
if isCow "$FS"; then
|
|
|
|
FA=$(lsattr "$DISK_FILE")
|
|
|
|
if [[ "$FA" != *"C"* ]]; then
|
|
|
|
error "Failed to disable COW for $DISK_DESC image $DISK_FILE on ${FS^^} filesystem (returned $FA)"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-12-18 16:44:26 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-12-09 02:55:39 +01:00
|
|
|
resizeDisk() {
|
|
|
|
local DISK_FILE=$1
|
2023-12-18 16:44:26 +01:00
|
|
|
local DISK_SPACE=$2
|
|
|
|
local DISK_DESC=$3
|
|
|
|
local DISK_FMT=$4
|
2023-12-24 02:41:44 +01:00
|
|
|
local FS=$5
|
2023-12-18 16:44:26 +01:00
|
|
|
local CUR_SIZE DATA_SIZE DIR SPACE
|
|
|
|
|
|
|
|
CUR_SIZE=$(getSize "$DISK_FILE")
|
|
|
|
DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE")
|
|
|
|
local REQ=$((DATA_SIZE-CUR_SIZE))
|
|
|
|
(( REQ < 1 )) && error "Shrinking disks is not supported yet, please increase ${DISK_DESC^^}_SIZE." && exit 71
|
2023-12-19 03:59:19 +01:00
|
|
|
|
2023-12-18 16:44:26 +01:00
|
|
|
if [[ "$ALLOCATE" != [Nn]* ]]; then
|
|
|
|
|
|
|
|
# Check free diskspace
|
|
|
|
DIR=$(dirname "$DISK_FILE")
|
|
|
|
SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
|
|
|
|
|
|
|
|
if (( REQ > SPACE )); then
|
2023-12-19 08:54:08 +01:00
|
|
|
local SPACE_GB=$(( (SPACE + 1073741823)/1073741824 ))
|
2023-12-18 16:44:26 +01:00
|
|
|
error "Not enough free space to resize $DISK_DESC to $DISK_SPACE in $DIR, it has only $SPACE_GB GB available.."
|
|
|
|
error "Please specify a smaller ${DISK_DESC^^}_SIZE or disable preallocation by setting ALLOCATE=N." && exit 74
|
|
|
|
fi
|
|
|
|
fi
|
2023-04-15 17:30:37 +02:00
|
|
|
|
2023-12-18 12:18:28 +01:00
|
|
|
local GB=$(( (CUR_SIZE + 1073741823)/1073741824 ))
|
2024-01-20 19:59:44 +01:00
|
|
|
MSG="Resizing $DISK_DESC from ${GB}G to $DISK_SPACE..."
|
|
|
|
info "$MSG" && html "$MSG"
|
|
|
|
|
2023-12-20 03:34:26 +01:00
|
|
|
local FAIL="Could not resize the $DISK_TYPE $DISK_FMT $DISK_DESC image from ${GB}G to $DISK_SPACE ($DISK_FILE)"
|
2023-12-09 15:15:25 +01:00
|
|
|
|
2023-12-09 02:55:39 +01:00
|
|
|
case "${DISK_FMT,,}" in
|
|
|
|
raw)
|
2023-12-24 02:41:44 +01:00
|
|
|
|
2023-12-09 15:15:25 +01:00
|
|
|
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
2023-04-18 18:05:52 +02:00
|
|
|
|
2023-11-16 20:36:30 +01:00
|
|
|
# Resize file by changing its length
|
2023-12-19 08:54:08 +01:00
|
|
|
if ! truncate -s "$DATA_SIZE" "$DISK_FILE"; then
|
2023-12-16 01:30:34 +01:00
|
|
|
error "$FAIL" && exit 75
|
2023-11-16 20:36:30 +01:00
|
|
|
fi
|
2023-04-18 18:05:52 +02:00
|
|
|
|
2023-11-16 20:36:30 +01:00
|
|
|
else
|
2023-04-18 18:05:52 +02:00
|
|
|
|
2023-11-16 20:36:30 +01:00
|
|
|
# Resize file by allocating more space
|
2023-12-19 08:54:08 +01:00
|
|
|
if ! fallocate -l "$DATA_SIZE" "$DISK_FILE"; then
|
|
|
|
if ! truncate -s "$DATA_SIZE" "$DISK_FILE"; then
|
2023-12-16 01:30:34 +01:00
|
|
|
error "$FAIL" && exit 75
|
2023-11-16 20:36:30 +01:00
|
|
|
fi
|
|
|
|
fi
|
2023-04-18 18:05:52 +02:00
|
|
|
|
2023-04-19 03:55:32 +02:00
|
|
|
fi
|
2023-12-09 02:55:39 +01:00
|
|
|
;;
|
|
|
|
qcow2)
|
2023-12-24 02:41:44 +01:00
|
|
|
|
2023-12-19 08:54:08 +01:00
|
|
|
if ! qemu-img resize -f "$DISK_FMT" "--$DISK_ALLOC" "$DISK_FILE" "$DATA_SIZE" ; then
|
2023-12-16 01:30:34 +01:00
|
|
|
error "$FAIL" && exit 72
|
2023-12-09 02:55:39 +01:00
|
|
|
fi
|
2023-12-24 02:41:44 +01:00
|
|
|
|
2023-12-09 02:55:39 +01:00
|
|
|
;;
|
|
|
|
esac
|
2023-12-18 16:44:26 +01:00
|
|
|
|
|
|
|
return 0
|
2023-12-09 02:55:39 +01:00
|
|
|
}
|
2023-04-15 17:30:37 +02:00
|
|
|
|
2023-12-09 02:55:39 +01:00
|
|
|
convertDisk() {
|
|
|
|
local SOURCE_FILE=$1
|
|
|
|
local SOURCE_FMT=$2
|
|
|
|
local DST_FILE=$3
|
|
|
|
local DST_FMT=$4
|
2023-12-18 16:44:26 +01:00
|
|
|
local DISK_BASE=$5
|
|
|
|
local DISK_DESC=$6
|
2023-12-24 02:41:44 +01:00
|
|
|
local FS=$7
|
2023-12-18 16:44:26 +01:00
|
|
|
|
|
|
|
[ -f "$DST_FILE" ] && error "Conversion failed, destination file $DST_FILE already exists?" && exit 79
|
|
|
|
[ ! -f "$SOURCE_FILE" ] && error "Conversion failed, source file $SOURCE_FILE does not exists?" && exit 79
|
|
|
|
|
2023-12-24 02:41:44 +01:00
|
|
|
local TMP_FILE="$DISK_BASE.tmp"
|
|
|
|
rm -f "$TMP_FILE"
|
|
|
|
|
2023-12-18 16:44:26 +01:00
|
|
|
if [[ "$ALLOCATE" != [Nn]* ]]; then
|
|
|
|
|
2023-12-24 02:41:44 +01:00
|
|
|
local DIR CUR_SIZE SPACE
|
|
|
|
|
2023-12-18 16:44:26 +01:00
|
|
|
# Check free diskspace
|
|
|
|
DIR=$(dirname "$TMP_FILE")
|
|
|
|
CUR_SIZE=$(getSize "$SOURCE_FILE")
|
|
|
|
SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
|
|
|
|
|
|
|
|
if (( CUR_SIZE > SPACE )); then
|
2023-12-19 08:54:08 +01:00
|
|
|
local SPACE_GB=$(( (SPACE + 1073741823)/1073741824 ))
|
2023-12-18 16:44:26 +01:00
|
|
|
error "Not enough free space to convert $DISK_DESC to $DST_FMT in $DIR, it has only $SPACE_GB GB available..."
|
|
|
|
error "Please free up some disk space or disable preallocation by setting ALLOCATE=N." && exit 76
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2024-01-23 00:15:11 +01:00
|
|
|
html "Converting $DISK_DESC to $DST_FMT..."
|
|
|
|
info "Converting $DISK_DESC to $DST_FMT, please wait until completed..."
|
2023-04-02 21:43:28 +02:00
|
|
|
|
2023-12-24 02:41:44 +01:00
|
|
|
local CONV_FLAGS="-p"
|
|
|
|
local DISK_PARAM="$DISK_ALLOC"
|
|
|
|
isCow "$FS" && DISK_PARAM="$DISK_PARAM,nocow=on"
|
|
|
|
|
2023-12-19 08:54:08 +01:00
|
|
|
if [[ "$DST_FMT" != "raw" ]]; then
|
2023-12-18 12:18:28 +01:00
|
|
|
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
|
|
|
CONV_FLAGS="$CONV_FLAGS -c"
|
|
|
|
fi
|
2023-12-24 02:41:44 +01:00
|
|
|
[ -n "$DISK_FLAGS" ] && DISK_PARAM="$DISK_PARAM,$DISK_FLAGS"
|
2023-12-19 08:54:08 +01:00
|
|
|
fi
|
2023-04-15 17:30:37 +02:00
|
|
|
|
2023-12-18 16:44:26 +01:00
|
|
|
# shellcheck disable=SC2086
|
2023-12-24 02:41:44 +01:00
|
|
|
if ! qemu-img convert -f "$SOURCE_FMT" $CONV_FLAGS -o "$DISK_PARAM" -O "$DST_FMT" -- "$SOURCE_FILE" "$TMP_FILE"; then
|
2023-12-18 16:44:26 +01:00
|
|
|
rm -f "$TMP_FILE"
|
2023-12-20 03:34:26 +01:00
|
|
|
error "Failed to convert $DISK_TYPE $DISK_DESC image to $DST_FMT format in $DIR, is there enough space available?" && exit 79
|
2023-12-18 16:44:26 +01:00
|
|
|
fi
|
2023-12-09 02:55:39 +01:00
|
|
|
|
2023-12-19 08:54:08 +01:00
|
|
|
if [[ "$DST_FMT" == "raw" ]]; then
|
|
|
|
if [[ "$ALLOCATE" != [Nn]* ]]; then
|
2023-12-24 02:41:44 +01:00
|
|
|
# Work around qemu-img bug
|
2023-12-19 08:54:08 +01:00
|
|
|
CUR_SIZE=$(stat -c%s "$TMP_FILE")
|
|
|
|
if ! fallocate -l "$CUR_SIZE" "$TMP_FILE"; then
|
2023-12-24 02:41:44 +01:00
|
|
|
error "Failed to allocate $CUR_SIZE bytes for $DISK_DESC image $TMP_FILE"
|
2023-12-19 08:54:08 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-12-18 16:44:26 +01:00
|
|
|
rm -f "$SOURCE_FILE"
|
|
|
|
mv "$TMP_FILE" "$DST_FILE"
|
2023-12-09 02:55:39 +01:00
|
|
|
|
2023-12-24 02:41:44 +01:00
|
|
|
if isCow "$FS"; then
|
|
|
|
FA=$(lsattr "$DST_FILE")
|
|
|
|
if [[ "$FA" != *"C"* ]]; then
|
|
|
|
error "Failed to disable COW for $DISK_DESC image $DST_FILE on ${FS^^} filesystem (returned $FA)"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2024-01-23 00:15:11 +01:00
|
|
|
html "Conversion of $DISK_DESC completed..."
|
|
|
|
info "Conversion of $DISK_DESC to $DST_FMT completed succesfully!"
|
2023-12-09 02:55:39 +01:00
|
|
|
|
2023-12-18 16:44:26 +01:00
|
|
|
return 0
|
2023-12-09 02:55:39 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 06:15:33 +01:00
|
|
|
checkFS () {
|
2023-12-24 02:41:44 +01:00
|
|
|
local FS=$1
|
|
|
|
local DISK_FILE=$2
|
|
|
|
local DISK_DESC=$3
|
|
|
|
local DIR FA
|
2023-12-09 02:55:39 +01:00
|
|
|
|
2023-12-09 15:15:25 +01:00
|
|
|
DIR=$(dirname "$DISK_FILE")
|
|
|
|
[ ! -d "$DIR" ] && return 0
|
2023-12-09 02:55:39 +01:00
|
|
|
|
2023-12-24 02:41:44 +01:00
|
|
|
if [[ "${FS,,}" == "overlay"* ]]; then
|
2023-12-09 15:15:25 +01:00
|
|
|
info "Warning: the filesystem of $DIR is OverlayFS, this usually means it was binded to an invalid path!"
|
|
|
|
fi
|
2023-12-22 06:15:33 +01:00
|
|
|
|
2024-01-05 13:24:54 +01:00
|
|
|
if [[ "${FS,,}" == "fuse"* ]]; then
|
|
|
|
info "Warning: the filesystem of $DIR is FUSE, this extra layer will negatively affect performance!"
|
|
|
|
fi
|
|
|
|
|
2024-04-26 06:04:48 +02:00
|
|
|
if ! supportsDirect "$FS"; then
|
|
|
|
info "Warning: the filesystem of $DIR is $FS, which does not support O_DIRECT mode, adjusting settings..."
|
|
|
|
fi
|
|
|
|
|
2023-12-24 02:41:44 +01:00
|
|
|
if isCow "$FS"; then
|
|
|
|
if [ -f "$DISK_FILE" ]; then
|
|
|
|
FA=$(lsattr "$DISK_FILE")
|
2023-12-23 20:01:27 +01:00
|
|
|
if [[ "$FA" != *"C"* ]]; then
|
2023-12-24 14:27:29 +01:00
|
|
|
info "Warning: COW (copy on write) is not disabled for $DISK_DESC image file $DISK_FILE, this is recommended on ${FS^^} filesystems!"
|
2023-12-24 02:41:44 +01:00
|
|
|
fi
|
2023-12-19 05:46:49 +01:00
|
|
|
fi
|
|
|
|
fi
|
2023-12-09 15:15:25 +01:00
|
|
|
|
2023-12-22 06:15:33 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-02-01 11:05:33 +01:00
|
|
|
createDevice () {
|
|
|
|
|
|
|
|
local DISK_ID=$1
|
|
|
|
local DISK_FILE=$2
|
|
|
|
local DISK_INDEX=$3
|
|
|
|
local DISK_ADDRESS=$4
|
|
|
|
local DISK_FMT=$5
|
2024-04-26 06:04:48 +02:00
|
|
|
local DISK_IO=$6
|
|
|
|
local DISK_CACHE=$7
|
2024-02-01 11:05:33 +01:00
|
|
|
|
2024-04-26 06:04:48 +02:00
|
|
|
local result="-drive file=$DISK_FILE,if=none,id=drive-$DISK_ID,format=$DISK_FMT,cache=$DISK_CACHE,aio=$DISK_IO,discard=$DISK_DISCARD,detect-zeroes=on"
|
|
|
|
|
|
|
|
result="$result \
|
2024-02-01 11:05:33 +01:00
|
|
|
-device virtio-scsi-pci,id=hw-$DISK_ID,iothread=io2,bus=pcie.0,addr=$DISK_ADDRESS \
|
|
|
|
-device scsi-hd,bus=hw-$DISK_ID.0,channel=0,scsi-id=0,lun=0,drive=drive-$DISK_ID,id=$DISK_ID,rotation_rate=$DISK_ROTATION,bootindex=$DISK_INDEX"
|
|
|
|
|
2024-04-26 06:04:48 +02:00
|
|
|
echo "$result"
|
2024-02-01 11:05:33 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-12-22 06:15:33 +01:00
|
|
|
addDisk () {
|
2024-04-26 06:04:48 +02:00
|
|
|
local DISK_BASE=$1
|
|
|
|
local DISK_EXT=$2
|
|
|
|
local DISK_DESC=$3
|
|
|
|
local DISK_SPACE=$4
|
|
|
|
local DISK_INDEX=$5
|
|
|
|
local DISK_ADDRESS=$6
|
|
|
|
local DISK_FMT=$7
|
|
|
|
local DISK_IO=$8
|
|
|
|
local DISK_CACHE=$9
|
|
|
|
local DISK_ID="userdata$DISK_INDEX"
|
2023-12-22 06:15:33 +01:00
|
|
|
local DISK_FILE="$DISK_BASE.$DISK_EXT"
|
2024-02-01 11:05:33 +01:00
|
|
|
local DIR DATA_SIZE FS PREV_FMT PREV_EXT CUR_SIZE OPTS
|
2023-12-22 06:15:33 +01:00
|
|
|
|
|
|
|
DIR=$(dirname "$DISK_FILE")
|
|
|
|
[ ! -d "$DIR" ] && return 0
|
|
|
|
|
2023-12-09 15:15:25 +01:00
|
|
|
[ -z "$DISK_SPACE" ] && DISK_SPACE="16G"
|
2023-12-22 06:15:33 +01:00
|
|
|
DISK_SPACE=$(echo "${DISK_SPACE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
|
2023-12-09 15:15:25 +01:00
|
|
|
DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE")
|
|
|
|
|
|
|
|
if (( DATA_SIZE < 6442450944 )); then
|
2023-12-19 08:54:08 +01:00
|
|
|
if (( DATA_SIZE < 1 )); then
|
|
|
|
error "Invalid value for ${DISK_DESC^^}_SIZE: $DISK_SPACE" && exit 73
|
|
|
|
else
|
|
|
|
error "Please increase ${DISK_DESC^^}_SIZE to at least 6 GB." && exit 73
|
|
|
|
fi
|
2023-12-09 02:55:39 +01:00
|
|
|
fi
|
|
|
|
|
2023-12-24 02:41:44 +01:00
|
|
|
FS=$(stat -f -c %T "$DIR")
|
|
|
|
checkFS "$FS" "$DISK_FILE" "$DISK_DESC" || exit $?
|
2023-12-22 06:15:33 +01:00
|
|
|
|
2024-04-26 06:04:48 +02:00
|
|
|
if ! supportsDirect "$FS"; then
|
|
|
|
DISK_IO="threads"
|
|
|
|
DISK_CACHE="writeback"
|
|
|
|
fi
|
|
|
|
|
2024-04-16 15:35:35 +02:00
|
|
|
if ! [ -s "$DISK_FILE" ] ; then
|
2023-12-09 02:55:39 +01:00
|
|
|
|
|
|
|
if [[ "${DISK_FMT,,}" != "raw" ]]; then
|
|
|
|
PREV_FMT="raw"
|
|
|
|
else
|
|
|
|
PREV_FMT="qcow2"
|
|
|
|
fi
|
2024-01-20 19:59:44 +01:00
|
|
|
PREV_EXT=$(fmt2ext "$PREV_FMT")
|
2023-12-09 15:15:25 +01:00
|
|
|
|
2024-04-16 15:35:35 +02:00
|
|
|
if [ -s "$DISK_BASE.$PREV_EXT" ] ; then
|
2023-12-24 02:41:44 +01:00
|
|
|
convertDisk "$DISK_BASE.$PREV_EXT" "$PREV_FMT" "$DISK_FILE" "$DISK_FMT" "$DISK_BASE" "$DISK_DESC" "$FS" || exit $?
|
2023-12-09 15:15:25 +01:00
|
|
|
fi
|
2023-12-09 02:55:39 +01:00
|
|
|
fi
|
|
|
|
|
2024-04-16 15:35:35 +02:00
|
|
|
if [ -s "$DISK_FILE" ]; then
|
2023-12-09 02:55:39 +01:00
|
|
|
|
2023-12-09 15:15:25 +01:00
|
|
|
CUR_SIZE=$(getSize "$DISK_FILE")
|
2023-12-09 02:55:39 +01:00
|
|
|
|
2023-12-18 16:44:26 +01:00
|
|
|
if (( DATA_SIZE > CUR_SIZE )); then
|
2023-12-24 02:41:44 +01:00
|
|
|
resizeDisk "$DISK_FILE" "$DISK_SPACE" "$DISK_DESC" "$DISK_FMT" "$FS" || exit $?
|
2023-04-18 18:05:52 +02:00
|
|
|
fi
|
2023-12-09 02:55:39 +01:00
|
|
|
|
|
|
|
else
|
|
|
|
|
2023-12-24 02:41:44 +01:00
|
|
|
createDisk "$DISK_FILE" "$DISK_SPACE" "$DISK_DESC" "$DISK_FMT" "$FS" || exit $?
|
2023-12-09 02:55:39 +01:00
|
|
|
|
2023-04-15 17:30:37 +02:00
|
|
|
fi
|
|
|
|
|
2024-04-26 06:04:48 +02:00
|
|
|
OPTS=$(createDevice "$DISK_ID" "$DISK_FILE" "$DISK_INDEX" "$DISK_ADDRESS" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE")
|
2024-02-01 11:05:33 +01:00
|
|
|
DISK_OPTS="$DISK_OPTS $OPTS"
|
2023-04-19 03:19:52 +02:00
|
|
|
|
2023-11-16 20:36:30 +01:00
|
|
|
return 0
|
|
|
|
}
|
2023-04-10 15:27:09 +02:00
|
|
|
|
2024-01-12 18:01:20 +01:00
|
|
|
addDevice () {
|
|
|
|
|
2024-04-26 06:04:48 +02:00
|
|
|
local DISK_DEV=$1
|
|
|
|
local DISK_DESC=$2
|
|
|
|
local DISK_INDEX=$3
|
|
|
|
local DISK_ADDRESS=$4
|
|
|
|
local DISK_ID="userdata$DISK_INDEX"
|
2024-01-12 18:01:20 +01:00
|
|
|
|
|
|
|
[ -z "$DISK_DEV" ] && return 0
|
|
|
|
[ ! -b "$DISK_DEV" ] && error "Device $DISK_DEV cannot be found! Please add it to the 'devices' section of your compose file." && exit 55
|
|
|
|
|
2024-02-01 11:05:33 +01:00
|
|
|
local OPTS
|
2024-04-26 06:04:48 +02:00
|
|
|
OPTS=$(createDevice "$DISK_ID" "$DISK_DEV" "$DISK_INDEX" "$DISK_ADDRESS" "raw" "$DISK_IO" "$DISK_CACHE")
|
2024-02-01 11:05:33 +01:00
|
|
|
DISK_OPTS="$DISK_OPTS $OPTS"
|
2024-01-12 18:01:20 +01:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-01-20 19:59:44 +01:00
|
|
|
html "Initializing disks..."
|
|
|
|
|
|
|
|
DISK_EXT=$(fmt2ext "$DISK_FMT")
|
2023-04-19 03:55:32 +02:00
|
|
|
|
2024-01-12 23:50:04 +01:00
|
|
|
if [ -z "$ALLOCATE" ]; then
|
|
|
|
if [[ "${DISK_FMT,,}" == "raw" ]]; then
|
|
|
|
ALLOCATE="Y"
|
|
|
|
else
|
|
|
|
ALLOCATE="N"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-12-18 12:18:28 +01:00
|
|
|
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
2023-12-20 03:34:26 +01:00
|
|
|
DISK_TYPE="growable"
|
2023-12-18 12:18:28 +01:00
|
|
|
DISK_ALLOC="preallocation=off"
|
|
|
|
else
|
2023-12-20 03:34:26 +01:00
|
|
|
DISK_TYPE="preallocated"
|
2023-12-18 12:18:28 +01:00
|
|
|
DISK_ALLOC="preallocation=falloc"
|
|
|
|
fi
|
|
|
|
|
2023-12-09 21:19:08 +01:00
|
|
|
DISK1_FILE="$STORAGE/data"
|
|
|
|
if [[ ! -f "$DISK1_FILE.img" ]] && [[ -f "$STORAGE/data${DISK_SIZE}.img" ]]; then
|
2023-11-16 20:36:30 +01:00
|
|
|
# Fallback for legacy installs
|
2023-12-09 21:19:08 +01:00
|
|
|
mv "$STORAGE/data${DISK_SIZE}.img" "$DISK1_FILE.img"
|
2023-04-19 03:55:32 +02:00
|
|
|
fi
|
|
|
|
|
2023-12-09 02:55:39 +01:00
|
|
|
DISK2_FILE="/storage2/data2"
|
2023-12-09 21:19:08 +01:00
|
|
|
if [ ! -f "$DISK2_FILE.img" ]; then
|
2023-11-16 20:36:30 +01:00
|
|
|
# Fallback for legacy installs
|
|
|
|
FALLBACK="/storage2/data.img"
|
2023-12-09 21:19:08 +01:00
|
|
|
if [[ -f "$DISK1_FILE.img" ]] && [[ -f "$FALLBACK" ]]; then
|
2023-12-09 15:15:25 +01:00
|
|
|
SIZE1=$(stat -c%s "$FALLBACK")
|
2023-12-09 21:19:08 +01:00
|
|
|
SIZE2=$(stat -c%s "$DISK1_FILE.img")
|
2023-11-16 20:36:30 +01:00
|
|
|
if [[ SIZE1 -ne SIZE2 ]]; then
|
2023-12-09 21:19:08 +01:00
|
|
|
mv "$FALLBACK" "$DISK2_FILE.img"
|
2023-11-11 16:50:08 +01:00
|
|
|
fi
|
|
|
|
fi
|
2023-10-09 14:10:31 +02:00
|
|
|
fi
|
|
|
|
|
2023-12-09 02:55:39 +01:00
|
|
|
DISK3_FILE="/storage3/data3"
|
2023-12-09 21:19:08 +01:00
|
|
|
if [ ! -f "$DISK3_FILE.img" ]; then
|
2023-11-16 20:36:30 +01:00
|
|
|
# Fallback for legacy installs
|
|
|
|
FALLBACK="/storage3/data.img"
|
2023-12-09 21:19:08 +01:00
|
|
|
if [[ -f "$DISK1_FILE.img" ]] && [[ -f "$FALLBACK" ]]; then
|
2023-12-09 15:15:25 +01:00
|
|
|
SIZE1=$(stat -c%s "$FALLBACK")
|
2023-12-09 21:19:08 +01:00
|
|
|
SIZE2=$(stat -c%s "$DISK1_FILE.img")
|
2023-11-16 20:36:30 +01:00
|
|
|
if [[ SIZE1 -ne SIZE2 ]]; then
|
2023-12-09 21:19:08 +01:00
|
|
|
mv "$FALLBACK" "$DISK3_FILE.img"
|
2023-11-11 16:50:08 +01:00
|
|
|
fi
|
|
|
|
fi
|
2023-10-09 14:10:31 +02:00
|
|
|
fi
|
2023-10-10 05:26:37 +02:00
|
|
|
|
2023-12-09 02:55:39 +01:00
|
|
|
DISK4_FILE="/storage4/data4"
|
2023-10-10 06:29:03 +02:00
|
|
|
|
2024-01-14 16:01:15 +01:00
|
|
|
: "${DISK2_SIZE:=""}"
|
|
|
|
: "${DISK3_SIZE:=""}"
|
|
|
|
: "${DISK4_SIZE:=""}"
|
2023-10-10 05:26:37 +02:00
|
|
|
|
2024-01-14 16:01:15 +01:00
|
|
|
: "${DEVICE:=""}" # Docker variables to passthrough a block device, like /dev/vdc1.
|
|
|
|
: "${DEVICE2:=""}"
|
|
|
|
: "${DEVICE3:=""}"
|
|
|
|
: "${DEVICE4:=""}"
|
2023-10-31 16:53:44 +01:00
|
|
|
|
2024-01-12 18:01:20 +01:00
|
|
|
if [ -n "$DEVICE" ]; then
|
2024-04-26 06:04:48 +02:00
|
|
|
addDevice "$DEVICE" "device" "3" "0xc" || exit $?
|
2024-01-12 18:01:20 +01:00
|
|
|
else
|
2024-04-26 06:04:48 +02:00
|
|
|
addDisk "$DISK1_FILE" "$DISK_EXT" "disk" "$DISK_SIZE" "3" "0xc" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE" || exit $?
|
2024-01-12 18:01:20 +01:00
|
|
|
fi
|
2023-10-31 16:53:44 +01:00
|
|
|
|
2024-01-12 18:01:20 +01:00
|
|
|
if [ -n "$DEVICE2" ]; then
|
2024-04-26 06:04:48 +02:00
|
|
|
addDevice "$DEVICE2" "device2" "4" "0xd" || exit $?
|
2024-01-12 18:01:20 +01:00
|
|
|
else
|
2024-04-26 06:04:48 +02:00
|
|
|
addDisk "$DISK2_FILE" "$DISK_EXT" "disk2" "$DISK2_SIZE" "4" "0xd" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE" || exit $?
|
2024-01-12 18:01:20 +01:00
|
|
|
fi
|
2023-10-31 16:53:44 +01:00
|
|
|
|
2024-01-12 18:01:20 +01:00
|
|
|
if [ -n "$DEVICE3" ]; then
|
2024-04-26 06:04:48 +02:00
|
|
|
addDevice "$DEVICE3" "device3" "5" "0xe" || exit $?
|
2024-01-12 18:01:20 +01:00
|
|
|
else
|
2024-04-26 06:04:48 +02:00
|
|
|
addDisk "$DISK3_FILE" "$DISK_EXT" "disk3" "$DISK3_SIZE" "5" "0xe" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE" || exit $?
|
2024-01-12 18:01:20 +01:00
|
|
|
fi
|
2023-10-31 16:53:44 +01:00
|
|
|
|
2024-01-12 18:01:20 +01:00
|
|
|
if [ -n "$DEVICE4" ]; then
|
2024-04-26 06:04:48 +02:00
|
|
|
addDevice "$DEVICE4" "device4" "6" "0xf" || exit $?
|
2024-01-12 18:01:20 +01:00
|
|
|
else
|
2024-04-26 06:04:48 +02:00
|
|
|
addDisk "$DISK4_FILE" "$DISK_EXT" "disk4" "$DISK4_SIZE" "6" "0xf" "$DISK_FMT" "$DISK_IO" "$DISK_CACHE" || exit $?
|
2024-01-12 18:01:20 +01:00
|
|
|
fi
|
2023-11-15 19:58:51 +01:00
|
|
|
|
2024-01-20 19:59:44 +01:00
|
|
|
html "Initialized disks successfully..."
|
2023-11-15 19:58:51 +01:00
|
|
|
return 0
|