feat: Make boot path absolute (#59)

This commit is contained in:
Kroese 2024-05-06 00:55:17 +02:00 committed by GitHub
parent ce11aa8f38
commit 134cfc00a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 27 additions and 23 deletions

View file

@ -93,7 +93,7 @@ docker run -it --rm --name qemu -e "BOOT=http://example.com/image.iso" -p 8006:8
```yaml ```yaml
volumes: volumes:
- /home/user/example.iso:/storage/boot.iso - /home/user/example.iso:/boot.iso
``` ```
Replace the example path `/home/user/example.iso` with the filename of the desired ISO file. Replace the example path `/home/user/example.iso` with the filename of the desired ISO file.

View file

@ -10,8 +10,7 @@ set -Eeuo pipefail
: "${DISK_DISCARD:="on"}" # Controls whether unmap (TRIM) commands are passed to the host. : "${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 : "${DISK_ROTATION:="1"}" # Rotation rate, set to 1 for SSD storage and increase for HDD
BOOT="$STORAGE/$BASE" [ ! -f "$BOOT" ] || [ ! -s "$BOOT" ] && BOOT="/dev/null"
[ ! -f "$BOOT" ] && BOOT="/dev/null"
DISK_OPTS="-object iothread,id=io2" DISK_OPTS="-object iothread,id=io2"
DISK_OPTS="$DISK_OPTS -drive id=cdrom0,media=cdrom,if=none,format=raw,readonly=on,file=$BOOT" DISK_OPTS="$DISK_OPTS -drive id=cdrom0,media=cdrom,if=none,format=raw,readonly=on,file=$BOOT"
@ -19,7 +18,7 @@ DISK_OPTS="$DISK_OPTS -device virtio-scsi-pci,id=scsi0,iothread=io2,addr=0x5"
DISK_OPTS="$DISK_OPTS -device scsi-cd,bus=scsi0.0,drive=cdrom0,bootindex=$BOOT_INDEX" DISK_OPTS="$DISK_OPTS -device scsi-cd,bus=scsi0.0,drive=cdrom0,bootindex=$BOOT_INDEX"
DRIVERS="$STORAGE/drivers.iso" DRIVERS="$STORAGE/drivers.iso"
[ ! -f "$DRIVERS" ] && DRIVERS="/run/drivers.iso" [ ! -f "$DRIVERS" ] || [ ! -s "$DRIVERS" ] && DRIVERS="/run/drivers.iso"
if [ -f "$DRIVERS" ]; then if [ -f "$DRIVERS" ]; then
DISK_OPTS="$DISK_OPTS -drive id=cdrom1,media=cdrom,if=none,format=raw,readonly=on,file=$DRIVERS -device usb-storage,drive=cdrom1" DISK_OPTS="$DISK_OPTS -drive id=cdrom1,media=cdrom,if=none,format=raw,readonly=on,file=$DRIVERS -device usb-storage,drive=cdrom1"

View file

@ -3,36 +3,41 @@ set -Eeuo pipefail
# Check if running with interactive TTY or redirected to docker log # Check if running with interactive TTY or redirected to docker log
if [ -t 1 ]; then if [ -t 1 ]; then
PROGRESS="--progress=bar:noscroll" progress="--progress=bar:noscroll"
else else
PROGRESS="--progress=dot:giga" progress="--progress=dot:giga"
fi fi
BASE=$(find "$STORAGE" -maxdepth 1 -type f -iname boot.iso -printf "%f\n" | head -n 1) file="/boot.iso" && [ -f "$file" ] && [ -s "$file" ] && BOOT="$file" && return 0
[ -z "$BASE" ] && BASE=$(find "$STORAGE" -maxdepth 1 -type f -iname boot.img -printf "%f\n" | head -n 1) file="/boot.img" && [ -f "$file" ] && [ -s "$file" ] && BOOT="$file" && return 0
[ -f "$STORAGE/$BASE" ] && return 0 file=$(find "$STORAGE" -maxdepth 1 -type f -iname boot.iso -printf "%f\n" | head -n 1)
[ -z "$file" ] && file=$(find "$STORAGE" -maxdepth 1 -type f -iname boot.img -printf "%f\n" | head -n 1)
[ -n "$file" ] && file="$STORAGE/$file"
[ -f "$file" ] && [ -s "$file" ] && BOOT="$file" && return 0
if [ -z "$BOOT" ]; then if [ -z "$BOOT" ]; then
error "No boot disk specified, set BOOT= to the URL of an ISO file." && exit 64 error "No boot disk specified, set BOOT= to the URL of an ISO file." && exit 64
fi fi
BASE=$(basename "$BOOT") base=$(basename "$BOOT")
[ -f "$STORAGE/$BASE" ] && return 0 [ -n "$base" ] && file="$STORAGE/$base"
[ -f "$file" ] && [ -s "$file" ] && BOOT="$file" && return 0
BASE=$(basename "${BOOT%%\?*}") base=$(basename "${BOOT%%\?*}")
: "${BASE//+/ }"; printf -v BASE '%b' "${_//%/\\x}" : "${base//+/ }"; printf -v base '%b' "${_//%/\\x}"
BASE=$(echo "$BASE" | sed -e 's/[^A-Za-z0-9._-]/_/g') base=$(echo "$base" | sed -e 's/[^A-Za-z0-9._-]/_/g')
[ -f "$STORAGE/$BASE" ] && return 0 [ -n "$base" ] && file="$STORAGE/$base"
[ -f "$file" ] && [ -s "$file" ] && BOOT="$file" && return 0
TMP="$STORAGE/${BASE%.*}.tmp" TMP="$STORAGE/${base%.*}.tmp"
rm -f "$TMP" rm -f "$TMP"
MSG="Downloading $BASE..." msg="Downloading $base..."
info "$MSG" && html "$MSG" info "$msg" && html "$msg"
/run/progress.sh "$TMP" "" "Downloading $BASE ([P])..." & /run/progress.sh "$TMP" "" "Downloading $base ([P])..." &
{ wget "$BOOT" -O "$TMP" -q --timeout=10 --show-progress "$PROGRESS"; rc=$?; } || : { wget "$BOOT" -O "$TMP" -q --timeout=10 --show-progress "$progress"; rc=$?; } || :
fKill "progress.sh" fKill "progress.sh"
@ -41,12 +46,12 @@ fKill "progress.sh"
html "Download finished successfully..." html "Download finished successfully..."
SIZE=$(stat -c%s "$TMP") size=$(stat -c%s "$TMP")
if ((SIZE<100000)); then if ((size<100000)); then
error "Invalid ISO file: Size is smaller than 100 KB" && exit 62 error "Invalid ISO file: Size is smaller than 100 KB" && exit 62
fi fi
mv -f "$TMP" "$STORAGE/$BASE" mv -f "$TMP" "$file"
return 0 return 0