Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make boot path absolute #489

Merged
merged 5 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ docker run -it --rm --name qemu -e "BOOT=http://example.com/image.iso" -p 8006:8

```yaml
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.
Expand Down
5 changes: 2 additions & 3 deletions src/disk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ set -Eeuo pipefail
: "${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

BOOT="$STORAGE/$BASE"
[ ! -f "$BOOT" ] && BOOT="/dev/null"
[ ! -f "$BOOT" ] || [ ! -s "$BOOT" ] && BOOT="/dev/null"

DISK_OPTS="-object iothread,id=io2"
DISK_OPTS="$DISK_OPTS -drive id=cdrom0,media=cdrom,if=none,format=raw,readonly=on,file=$BOOT"
Expand All @@ -24,7 +23,7 @@ else
fi

DRIVERS="$STORAGE/drivers.iso"
[ ! -f "$DRIVERS" ] && DRIVERS="/run/drivers.iso"
[ ! -f "$DRIVERS" ] || [ ! -s "$DRIVERS" ] && DRIVERS="/run/drivers.iso"

if [ -f "$DRIVERS" ] && [[ "${MACHINE,,}" != "pc-q35-2"* ]]; then
DISK_OPTS="$DISK_OPTS -drive id=cdrom1,media=cdrom,if=none,format=raw,readonly=on,file=$DRIVERS"
Expand Down
43 changes: 24 additions & 19 deletions src/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,41 @@ set -Eeuo pipefail

# Check if running with interactive TTY or redirected to docker log
if [ -t 1 ]; then
PROGRESS="--progress=bar:noscroll"
progress="--progress=bar:noscroll"
else
PROGRESS="--progress=dot:giga"
progress="--progress=dot:giga"
fi

BASE=$(find "$STORAGE" -maxdepth 1 -type f -iname boot.iso -printf "%f\n" | head -n 1)
[ -z "$BASE" ] && BASE=$(find "$STORAGE" -maxdepth 1 -type f -iname boot.img -printf "%f\n" | head -n 1)
file="/boot.iso" && [ -f "$file" ] && [ -s "$file" ] && BOOT="$file" && return 0
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
error "No boot disk specified, set BOOT= to the URL of an ISO file." && exit 64
fi

BASE=$(basename "$BOOT")
[ -f "$STORAGE/$BASE" ] && return 0
base=$(basename "$BOOT")
[ -n "$base" ] && file="$STORAGE/$base"
[ -f "$file" ] && [ -s "$file" ] && BOOT="$file" && return 0

BASE=$(basename "${BOOT%%\?*}")
: "${BASE//+/ }"; printf -v BASE '%b' "${_//%/\\x}"
BASE=$(echo "$BASE" | sed -e 's/[^A-Za-z0-9._-]/_/g')
[ -f "$STORAGE/$BASE" ] && return 0
base=$(basename "${BOOT%%\?*}")
: "${base//+/ }"; printf -v base '%b' "${_//%/\\x}"
base=$(echo "$base" | sed -e 's/[^A-Za-z0-9._-]/_/g')
[ -n "$base" ] && file="$STORAGE/$base"
[ -f "$file" ] && [ -s "$file" ] && BOOT="$file" && return 0

TMP="$STORAGE/${BASE%.*}.tmp"
TMP="$STORAGE/${base%.*}.tmp"
rm -f "$TMP"

MSG="Downloading $BASE..."
info "$MSG" && html "$MSG"
msg="Downloading $base..."
info "$msg" && html "$msg"

/run/progress.sh "$TMP" "" "Downloading $BASE ([P])..." &
{ wget "$BOOT" -O "$TMP" -q --timeout=10 --show-progress "$PROGRESS"; rc=$?; } || :
/run/progress.sh "$TMP" "" "Downloading $base ([P])..." &
{ wget "$BOOT" -O "$TMP" -q --timeout=10 --show-progress "$progress"; rc=$?; } || :

fKill "progress.sh"

Expand All @@ -41,12 +46,12 @@ fKill "progress.sh"

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
fi

mv -f "$TMP" "$STORAGE/$BASE"
mv -f "$TMP" "$file"

return 0
Loading