Files
advanced-libvirt-terraform-…/download_images.sh
2025-10-26 12:02:16 +01:00

129 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
# Function to get latest Fedora Rawhide image URL using a more reliable method
get_fedora_latest_rawhide_url() {
local base_url="https://dl.fedoraproject.org/pub/fedora/linux/development/rawhide/Cloud/x86_64/images/"
# Method 1: Try fetching the latest link from the directory
local temp_dir
temp_dir=$(mktemp -d)
# Download the HTML directory listing
if curl -s -o "$temp_dir/listing.html" "$base_url"; then
# Look for lines with qcow2 files that match our pattern
local latest_file
latest_file=$(grep -i "Fedora-Cloud-Base-Generic-Rawhide.*\.qcow2" "$temp_dir/listing.html" | \
sort -r | head -1 | sed -E 's/.*href="([^"]*)".*/\1/')
if [[ -n "$latest_file" ]]; then
echo "${base_url}${latest_file}"
else
# If we can't find a specific file, try to find any valid Fedora image
local any_file
any_file=$(grep -i "Fedora-Cloud-Base-Generic.*\.qcow2" "$temp_dir/listing.html" | \
head -1 | sed -E 's/.*href="([^"]*)".*/\1/')
if [[ -n "$any_file" ]]; then
echo "${base_url}${any_file}"
else
# Return empty string if we can't find any valid file
echo ""
fi
fi
else
# If network fails, return empty string to skip Fedora download
echo ""
fi
# Cleanup
rm -rf "$temp_dir"
}
# Image URLs with dynamic Fedora URL handling
IMAGES=(
"https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.raw"
"https://download.fedoraproject.org/pub/fedora/linux/releases/42/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-42-1.1.x86_64.qcow2"
"https://download.opensuse.org/tumbleweed/appliances/openSUSE-Tumbleweed-Minimal-VM.x86_64-Cloud.qcow2"
"https://dl.rockylinux.org/pub/rocky/10/images/x86_64/Rocky-10-GenericCloud-Base.latest.x86_64.qcow2"
"https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
"https://cloud.centos.org/centos/10-stream/x86_64/images/CentOS-Stream-GenericCloud-x86_64-10-latest.x86_64.qcow2"
)
# Add Fedora image if we can get a valid URL
FEDORA_URL=$(get_fedora_latest_rawhide_url)
if [[ -n "$FEDORA_URL" ]]; then
IMAGES+=("$FEDORA_URL")
fi
# Target directory
TARGET_DIR="/var/lib/libvirt/images"
# Main script execution
main() {
# Check if we have write permissions to the target directory
if [[ ! -w "$TARGET_DIR" ]]; then
# Check if we're already running as root
if [[ $EUID -ne 0 ]]; then
echo "This script requires write access to $TARGET_DIR"
echo "Re-executing with sudo..."
exec sudo "$0" "$@"
else
echo "Error: Cannot write to $TARGET_DIR even with sudo privileges."
exit 1
fi
fi
# Download all images
echo "Starting download of all images..."
echo ""
local success_count=0
local failure_count=0
for url in "${IMAGES[@]}"; do
# Skip empty URLs
if [[ -z "$url" ]]; then
continue
fi
local filename
filename=$(basename "$url")
local filepath="$TARGET_DIR/$filename"
if [[ -f "$filepath" ]]; then
echo "Image $filename already exists, skipping..."
((success_count++))
continue
fi
echo "Downloading $filename..."
# Use wget with progress and retry options
if ! wget -P "$TARGET_DIR" --progress=bar:force:noscroll -c "$url"; then
echo "Failed to download $filename"
((failure_count++))
else
echo "Download completed: $filename"
((success_count++))
fi
done
# Summary
echo ""
echo "Download summary:"
echo "Successful downloads: $success_count"
echo "Failed downloads: $failure_count"
if [[ $failure_count -gt 0 ]]; then
echo "Some downloads failed. Check above messages for details."
exit 1
else
echo "All images downloaded successfully!"
fi
}
# Run main function if script is executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi