This commit is contained in:
Kroese 2023-04-20 18:33:03 +02:00 committed by GitHub
parent 2897ef6a02
commit ede16d8863

View file

@ -1,6 +1,12 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -eu set -eu
# Docker environment variabeles
: ${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
: ${DISK_CACHE:='none'} # Caching mode, can be set to 'writeback' for better performance
BOOT="$STORAGE/boot.img" BOOT="$STORAGE/boot.img"
[ ! -f "$BOOT" ] && echo "ERROR: Boot image does not exist ($BOOT)" && exit 81 [ ! -f "$BOOT" ] && echo "ERROR: Boot image does not exist ($BOOT)" && exit 81
@ -16,19 +22,35 @@ if [ -f "${DATA}" ]; then
echo "INFO: Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.." echo "INFO: Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.."
if [ "$ALLOCATE" = "N" ]; then
truncate -s "${DATA_SIZE}" "${DATA}";
else
REQ=$((DATA_SIZE-OLD_SIZE)) REQ=$((DATA_SIZE-OLD_SIZE))
# Check free diskspace # Check free diskspace
SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1) SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)
if (( REQ > SPACE )); then if (( REQ > SPACE )); then
echo "ERROR: Not enough free space to resize virtual disk." && exit 84 echo "ERROR: Not enough free space to resize data disk to ${DISK_SIZE}."
echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 84
fi fi
if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then
echo "ERROR: Could not allocate file for virtual disk." && exit 85 echo "ERROR: Could not allocate a file for the data disk." && exit 85
fi fi
if [ "$ALLOCATE" = "Z" ]; then
GB=$(( (REQ + 1073741823)/1073741824 ))
echo "INFO: Preallocating ${GB} GB of diskspace, please wait..."
dd if=/dev/urandom of="${DATA}" seek="${OLD_SIZE}" count="${REQ}" bs=1M iflag=count_bytes oflag=seek_bytes status=none
fi
fi
fi fi
if [ "$DATA_SIZE" -lt "$OLD_SIZE" ]; then if [ "$DATA_SIZE" -lt "$OLD_SIZE" ]; then
@ -39,35 +61,57 @@ if [ -f "${DATA}" ]; then
mv -f "${DATA}" "${DATA}.bak" mv -f "${DATA}" "${DATA}.bak"
fi fi
fi fi
if [ ! -f "${DATA}" ]; then if [ ! -f "${DATA}" ]; then
# Create an empty file
if [ "$ALLOCATE" = "N" ]; then
truncate -s "${DATA_SIZE}" "${DATA}"
else
# Check free diskspace # Check free diskspace
SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1) SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1)
if (( DATA_SIZE > SPACE )); then if (( DATA_SIZE > SPACE )); then
echo "ERROR: Not enough free space to create virtual disk." && exit 86 echo "ERROR: Not enough free space to create a data disk of ${DISK_SIZE}."
echo "ERROR: Specify a smaller size or disable preallocation with ALLOCATE=N." && exit 86
fi fi
# Create an empty file
if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then
rm -f "${DATA}" rm -f "${DATA}"
echo "ERROR: Could not allocate file for virtual disk." && exit 87 echo "ERROR: Could not allocate a file for the data disk." && exit 87
fi
if [ "$ALLOCATE" = "Z" ]; then
echo "INFO: Preallocating ${DISK_SIZE} of diskspace, please wait..."
dd if=/dev/urandom of="${DATA}" count="${DATA_SIZE}" bs=1M iflag=count_bytes status=none
fi
fi fi
# Check if file exists # Check if file exists
if [ ! -f "${DATA}" ]; then if [ ! -f "${DATA}" ]; then
echo "ERROR: Data image does not exist ($DATA)" && exit 88 echo "ERROR: Data disk does not exist ($DATA)" && exit 88
fi fi
fi fi
KVM_DISK_OPTS="\ # Check the filesize
SIZE=$(stat -c%s "${DATA}")
if [[ SIZE -ne DATA_SIZE ]]; then
echo "ERROR: Data disk has the wrong size: ${SIZE}" && exit 89
fi
DISK_OPTS="\
-drive id=cdrom0,if=none,format=raw,readonly=on,file=${BOOT} \ -drive id=cdrom0,if=none,format=raw,readonly=on,file=${BOOT} \
-device virtio-scsi-pci,id=scsi0 \ -device virtio-scsi-pci,id=scsi0 \
-device scsi-cd,bus=scsi0.0,drive=cdrom0 \ -device scsi-cd,bus=scsi0.0,drive=cdrom0 \
-device virtio-scsi-pci,id=hw-userdata,bus=pcie.0,addr=0xa \ -device virtio-scsi-pci,id=hw-userdata,bus=pcie.0,addr=0xa \
-drive file=${DATA},if=none,id=drive-userdata,format=raw,cache=none,aio=native,discard=on,detect-zeroes=on \ -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=1,bootindex=1" -device scsi-hd,bus=hw-userdata.0,channel=0,scsi-id=0,lun=0,drive=drive-userdata,id=userdata0,rotation_rate=${DISK_ROTATION},bootindex=1"