diff --git a/run/disk.sh b/run/disk.sh index 8a0a146..129b620 100644 --- a/run/disk.sh +++ b/run/disk.sh @@ -1,6 +1,12 @@ #!/usr/bin/env bash 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" [ ! -f "$BOOT" ] && echo "ERROR: Boot image does not exist ($BOOT)" && exit 81 @@ -15,20 +21,36 @@ if [ -f "${DATA}" ]; then if [ "$DATA_SIZE" -gt "$OLD_SIZE" ]; then echo "INFO: Resizing data disk from $OLD_SIZE to $DATA_SIZE bytes.." - - REQ=$((DATA_SIZE-OLD_SIZE)) - - # Check free diskspace - SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1) - - if (( REQ > SPACE )); then - echo "ERROR: Not enough free space to resize virtual disk." && exit 84 - fi - if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then - echo "ERROR: Could not allocate file for virtual disk." && exit 85 + if [ "$ALLOCATE" = "N" ]; then + + 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 + 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 + + if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then + echo "ERROR: Could not allocate a file for the data disk." && exit 85 + 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 if [ "$DATA_SIZE" -lt "$OLD_SIZE" ]; then @@ -39,35 +61,57 @@ if [ -f "${DATA}" ]; then mv -f "${DATA}" "${DATA}.bak" fi - fi if [ ! -f "${DATA}" ]; then - # Check free diskspace - SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1) - - if (( DATA_SIZE > SPACE )); then - echo "ERROR: Not enough free space to create virtual disk." && exit 86 - fi - # Create an empty file - if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then - rm -f "${DATA}" - echo "ERROR: Could not allocate file for virtual disk." && exit 87 + + if [ "$ALLOCATE" = "N" ]; then + + truncate -s "${DATA_SIZE}" "${DATA}" + + else + + # Check free diskspace + SPACE=$(df --output=avail -B 1 "${STORAGE}" | tail -n 1) + + if (( DATA_SIZE > SPACE )); then + 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 + + if ! fallocate -l "${DATA_SIZE}" "${DATA}"; then + rm -f "${DATA}" + 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 # Check if file exists 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 -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} \ -device virtio-scsi-pci,id=scsi0 \ -device scsi-cd,bus=scsi0.0,drive=cdrom0 \ -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 \ - -device scsi-hd,bus=hw-userdata.0,channel=0,scsi-id=0,lun=0,drive=drive-userdata,id=userdata0,rotation_rate=1,bootindex=1" + -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=1"