Skip to content

Commit 62ec22d

Browse files
committed
Use the core image fetcher, and grow Ubuntu's root filesystem
Nine scripts kept images in a cache and each decided for itself whether a cached file was still usable. Four asked only whether it was non-empty -- docker-vm, netbird-server, truenas-vm, waydroid -- so an interrupted download stayed in the cache and every later run built a VM from half an image. Three compared a minimum size, two ran xz -t through a helper copied verbatim into both. None of them used vm_fetch_image, which has had the size and space checks all along. They now call it with --cache, and --min-bytes or --verify-xz where those checks were. The size floors are kept, not dropped. 172 lines of per-script download logic go with it. The four Ubuntu scripts import a cloud image and only ever ran qm resize, which grows the block device and nothing inside it, so the guest kept its original root on a larger disk. debian-vm already calls vm_expand_image for exactly this; they now do the same, in the same guarded form. Left alone on purpose: the ISO installers partition the disk themselves, and the appliance images (haos, umbrel, openwrt, opnsense, mikrotik, unifi) have fixed layouts where expanding the largest partition is not obviously right. Needs the core change first -- --cache and --verify-xz do not exist on main yet, and vm_fetch_image rejects unknown options.
1 parent 95aa04a commit 62ec22d

13 files changed

Lines changed: 33 additions & 172 deletions

‎vm/blissos-vm.sh‎

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -127,31 +127,8 @@ msg_ok "BlissOS ${CL}${BL}${BLISS_VERSION}${CL} ${GN}(build ${BLISS_BUILD})"
127127
# decides whether this is an ISO, not curl's exit code. Learned from cachyos.
128128
MIN_ISO_BYTES=$((1024 * 1024 * 1024))
129129

130-
iso_size() { stat -c%s "$1" 2>/dev/null || echo 0; }
131-
132-
if [[ -f "$CACHE_FILE" ]] && (($(iso_size "$CACHE_FILE") >= MIN_ISO_BYTES)); then
133-
msg_ok "Using cached ISO ${CL}${BL}${FILENAME}${CL}"
134-
else
135-
[[ -f "$CACHE_FILE" ]] && rm -f "$CACHE_FILE"
136-
137-
msg_info "Downloading BlissOS (approximately 2 GB, this may take a while)"
138-
if ! curl -fSL --retry 3 --retry-delay 5 -o "$CACHE_FILE" "$URL"; then
139-
rm -f "$CACHE_FILE"
140-
msg_error "Failed to download the BlissOS image"
141-
exit 1
142-
fi
143-
144-
DOWNLOADED_BYTES=$(iso_size "$CACHE_FILE")
145-
if ((DOWNLOADED_BYTES < MIN_ISO_BYTES)); then
146-
rm -f "$CACHE_FILE"
147-
msg_error "Downloaded ${DOWNLOADED_BYTES} bytes, which is not an ISO"
148-
msg_error "A SourceForge mirror most likely served an error page. Try again in a moment."
149-
exit 1
150-
fi
151-
152-
echo -en "\e[1A\e[0K"
153-
msg_ok "Downloaded ${CL}${BL}${FILENAME}${CL}"
154-
fi
130+
msg_info "Downloading BlissOS (approximately 2 GB, this may take a while)"
131+
vm_fetch_image "$URL" "$CACHE_FILE" --cache --min-bytes "$MIN_ISO_BYTES" || exit 115
155132

156133
msg_info "Creating a BlissOS VM"
157134

‎vm/cachyos-vm.sh‎

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -130,31 +130,8 @@ msg_ok "${CL}${BL}CachyOS Desktop ISO (Release: ${CACHYOS_VERSION})${CL}"
130130
# decides whether this is an ISO, not curl's exit code.
131131
MIN_ISO_BYTES=$((500 * 1024 * 1024))
132132

133-
iso_size() { stat -c%s "$1" 2>/dev/null || echo 0; }
134-
135-
if [[ -f "$CACHE_FILE" ]] && (($(iso_size "$CACHE_FILE") >= MIN_ISO_BYTES)); then
136-
msg_ok "Using cached ISO ${CL}${BL}${FILENAME}${CL}"
137-
else
138-
[[ -f "$CACHE_FILE" ]] && rm -f "$CACHE_FILE"
139-
140-
msg_info "Downloading CachyOS ISO (approximately 3.1 GB, this may take a while)"
141-
if ! curl -fSL --retry 3 --retry-delay 5 -o "$CACHE_FILE" "$URL"; then
142-
rm -f "$CACHE_FILE"
143-
msg_error "Failed to download CachyOS ISO"
144-
exit 1
145-
fi
146-
147-
DOWNLOADED_BYTES=$(iso_size "$CACHE_FILE")
148-
if ((DOWNLOADED_BYTES < MIN_ISO_BYTES)); then
149-
rm -f "$CACHE_FILE"
150-
msg_error "Downloaded ${DOWNLOADED_BYTES} bytes, which is not an ISO"
151-
msg_error "A SourceForge mirror most likely served an error page. Try again in a moment."
152-
exit 1
153-
fi
154-
155-
echo -en "\e[1A\e[0K"
156-
msg_ok "Downloaded ${CL}${BL}${FILENAME}${CL}"
157-
fi
133+
msg_info "Downloading CachyOS ISO (approximately 3.1 GB, this may take a while)"
134+
vm_fetch_image "$URL" "$CACHE_FILE" --cache --min-bytes "$MIN_ISO_BYTES" || exit 115
158135

159136
# ==============================================================================
160137
# VM CREATION

‎vm/docker-vm.sh‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,7 @@ CACHE_FILE="$CACHE_DIR/$(basename "$URL")"
229229
mkdir -p "$CACHE_DIR"
230230
msg_ok "${CL}${BL}${URL}${CL}"
231231

232-
if [[ ! -s "$CACHE_FILE" ]]; then
233-
curl -f#SL -o "$CACHE_FILE" "$URL"
234-
echo -en "\e[1A\e[0K"
235-
msg_ok "Downloaded ${CL}${BL}$(basename "$CACHE_FILE")${CL}"
236-
else
237-
msg_ok "Using cached image ${CL}${BL}$(basename "$CACHE_FILE")${CL}"
238-
fi
232+
vm_fetch_image "$URL" "$CACHE_FILE" --cache || exit 115
239233

240234
# ==============================================================================
241235
# STORAGE TYPE DETECTION

‎vm/haos-vm.sh‎

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -57,40 +57,6 @@ function ensure_pv() {
5757
fi
5858
}
5959

60-
# Download an .xz file and validate it
61-
# Args: $1=url $2=cache_file
62-
function download_and_validate_xz() {
63-
local url="$1"
64-
local file="$2"
65-
66-
# If file exists, check validity
67-
if [[ -s "$file" ]]; then
68-
if xz -t "$file" &>/dev/null; then
69-
msg_ok "Using cached image $(basename "$file")"
70-
return 0
71-
else
72-
msg_error "Cached file $(basename "$file") is corrupted. Deleting and retrying download..."
73-
rm -f "$file"
74-
fi
75-
fi
76-
77-
# Download fresh file
78-
msg_info "Downloading image: $(basename "$file")"
79-
if ! curl -fSL -o "$file" "$url"; then
80-
msg_error "Download failed: $url"
81-
rm -f "$file"
82-
exit 115
83-
fi
84-
85-
# Validate again
86-
if ! xz -t "$file" &>/dev/null; then
87-
msg_error "Downloaded file $(basename "$file") is corrupted. Please try again later."
88-
rm -f "$file"
89-
exit 115
90-
fi
91-
msg_ok "Downloaded and validated $(basename "$file")"
92-
}
93-
9460
# Extract .xz with pv
9561
# Args: $1=cache_file $2=target_img
9662
function extract_xz_with_pv() {
@@ -425,7 +391,7 @@ FILE_IMG="/var/lib/vz/template/tmp/${CACHE_FILE##*/%.xz}" # .qcow2
425391
mkdir -p "$CACHE_DIR" "$(dirname "$FILE_IMG")"
426392
msg_ok "${CL}${BL}${URL}${CL}"
427393

428-
download_and_validate_xz "$URL" "$CACHE_FILE"
394+
vm_fetch_image "$URL" "$CACHE_FILE" --cache --verify-xz || exit 115
429395

430396
msg_info "Creating Home Assistant OS VM shell"
431397
qm create $VMID${MACHINE} -bios ovmf -agent 1 -tablet 0 -localtime 1 ${CPU_TYPE} \

‎vm/netbird-server.sh‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,7 @@ CACHE_FILE="$CACHE_DIR/$(basename "$URL")"
270270
mkdir -p "$CACHE_DIR"
271271
msg_ok "${CL}${BL}${URL}${CL}"
272272

273-
if [[ ! -s "$CACHE_FILE" ]]; then
274-
curl -f#SL -o "$CACHE_FILE" "$URL"
275-
echo -en "\e[1A\e[0K"
276-
msg_ok "Downloaded ${CL}${BL}$(basename "$CACHE_FILE")${CL}"
277-
else
278-
msg_ok "Using cached image ${CL}${BL}$(basename "$CACHE_FILE")${CL}"
279-
fi
273+
vm_fetch_image "$URL" "$CACHE_FILE" --cache || exit 115
280274

281275
# ==============================================================================
282276
# STORAGE TYPE DETECTION

‎vm/truenas-vm.sh‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,8 @@ ISO_NAME=$(basename "$FULL_URL")
403403
CACHE_DIR="/var/lib/vz/template/iso"
404404
CACHE_FILE="$CACHE_DIR/$ISO_NAME"
405405

406-
if [[ ! -s "$CACHE_FILE" ]]; then
407-
msg_info "Retrieving the ISO for the TrueNAS Disk Image"
408-
curl -f#SL -o "$CACHE_FILE" "$FULL_URL"
409-
msg_ok "Downloaded ${CL}${BL}$(basename "$CACHE_FILE")${CL}"
410-
else
411-
msg_ok "Using cached image ${CL}${BL}$(basename "$CACHE_FILE")${CL}"
412-
fi
406+
msg_info "Retrieving the ISO for the TrueNAS Disk Image"
407+
vm_fetch_image "$FULL_URL" "$CACHE_FILE" --cache || exit 115
413408

414409
set -o pipefail
415410
msg_info "Creating TrueNAS VM shell"

‎vm/ubuntu2204-vm.sh‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ echo -en "\e[1A\e[0K"
101101
FILE=$(basename $URL)
102102
msg_ok "Downloaded ${CL}${BL}${FILE}${CL}"
103103

104+
if [ "${CLOUD_INIT:-no}" != "yes" ]; then
105+
msg_info "Expanding the root filesystem to ${DISK_SIZE}"
106+
vm_expand_image "$FILE" "$DISK_SIZE" || true
107+
fi
108+
104109
STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
105110
case $STORAGE_TYPE in
106111
nfs | dir | cifs)

‎vm/ubuntu2404-vm.sh‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ echo -en "\e[1A\e[0K"
101101
FILE=$(basename $URL)
102102
msg_ok "Downloaded ${CL}${BL}${FILE}${CL}"
103103

104+
if [ "${CLOUD_INIT:-no}" != "yes" ]; then
105+
msg_info "Expanding the root filesystem to ${DISK_SIZE}"
106+
vm_expand_image "$FILE" "$DISK_SIZE" || true
107+
fi
108+
104109
STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
105110
case $STORAGE_TYPE in
106111
nfs | dir | cifs)

‎vm/ubuntu2410-vm.sh‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ echo -en "\e[1A\e[0K"
220220
FILE=$(basename $URL)
221221
msg_ok "Downloaded ${CL}${BL}${FILE}${CL}"
222222

223+
if [ "${CLOUD_INIT:-no}" != "yes" ]; then
224+
msg_info "Expanding the root filesystem to ${DISK_SIZE}"
225+
vm_expand_image "$FILE" "$DISK_SIZE" || true
226+
fi
227+
223228
STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
224229
case $STORAGE_TYPE in
225230
nfs | dir | cifs)

‎vm/ubuntu2504-vm.sh‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ echo -en "\e[1A\e[0K"
100100
FILE=$(basename $URL)
101101
msg_ok "Downloaded ${CL}${BL}${FILE}${CL}"
102102

103+
if [ "${CLOUD_INIT:-no}" != "yes" ]; then
104+
msg_info "Expanding the root filesystem to ${DISK_SIZE}"
105+
vm_expand_image "$FILE" "$DISK_SIZE" || true
106+
fi
107+
103108
STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
104109
case $STORAGE_TYPE in
105110
nfs | dir | cifs)

0 commit comments

Comments
 (0)