Improve the script logic
Key improvements made: Enhanced find_fedora_iso function: Takes dish_name as parameter to understand which version to select Parses dish names to identify "rawhide" or numeric versions like "43", "44", "45" Matches ISO files containing both "Fedora-Everything" and the version information Falls back gracefully if no exact match is found Backward compatibility: Maintained the original find_fedora_iso_old function for fallback behavior If no matching ISO is found based on dish name, falls back to existing behavior Smart version matching: For virtual-desktop_43 → looks for ISO containing "43" For virtual-desktop_rawhide → looks for ISO containing "Rawhide" Handles edge cases with proper fallbacks Improved robustness: Proper error handling for missing directories Ensures matched files actually exist and are files (not directories) Handles multiple version patterns
This commit is contained in:
@@ -4,8 +4,59 @@
|
||||
DEFAULT_MEMORY=4096
|
||||
DEFAULT_DISK_SIZE=10
|
||||
|
||||
# Function to find Fedora ISO
|
||||
# Function to find Fedora ISO based on dish name
|
||||
find_fedora_iso() {
|
||||
local iso_dir="/var/lib/libvirt/isos"
|
||||
local dish_name="$1"
|
||||
local fedora_iso=""
|
||||
|
||||
# Check if directory exists
|
||||
if [ -d "$iso_dir" ]; then
|
||||
# Parse dish name to extract version or "rawhide"
|
||||
local version=""
|
||||
if [[ "$dish_name" == *"rawhide"* ]]; then
|
||||
version="Rawhide"
|
||||
elif [[ "$dish_name" == *"_43"* ]]; then
|
||||
version="43"
|
||||
elif [[ "$dish_name" == *"_44"* ]]; then
|
||||
version="44"
|
||||
elif [[ "$dish_name" == *"_45"* ]]; then
|
||||
version="45"
|
||||
fi
|
||||
|
||||
# If we found a version, try to match with that version in ISO name
|
||||
if [ -n "$version" ]; then
|
||||
if [ "$version" = "Rawhide" ]; then
|
||||
# For rawhide, look for ISO with "Rawhide" in the name
|
||||
fedora_iso=$(find "$iso_dir" -maxdepth 1 -name "Fedora-Everything*.iso" -type f | grep -i "rawhide" | head -n 1)
|
||||
else
|
||||
# For regular versions, look for ISO with that version number in the name
|
||||
fedora_iso=$(find "$iso_dir" -maxdepth 1 -name "Fedora-Everything*.iso" -type f | grep -i "$version" | head -n 1)
|
||||
fi
|
||||
|
||||
# If no specific version match found, fallback to any Fedora-Everything ISO
|
||||
if [ -z "$fedora_iso" ] || [ ! -f "$fedora_iso" ]; then
|
||||
fedora_iso=$(find "$iso_dir" -maxdepth 1 -name "Fedora-Everything*.iso" -type f | head -n 1)
|
||||
fi
|
||||
else
|
||||
# No version match found, fallback to any Fedora-Everything ISO
|
||||
fedora_iso=$(find "$iso_dir" -maxdepth 1 -name "Fedora-Everything*.iso" -type f | head -n 1)
|
||||
fi
|
||||
|
||||
# If found, return the full path
|
||||
if [ -n "$fedora_iso" ] && [ -f "$fedora_iso" ]; then
|
||||
echo "$fedora_iso"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# Return empty if no ISO found
|
||||
echo ""
|
||||
return 1
|
||||
}
|
||||
|
||||
# Function to find Fedora ISO (backward compatibility)
|
||||
find_fedora_iso_old() {
|
||||
local iso_dir="/var/lib/libvirt/isos"
|
||||
local fedora_iso=""
|
||||
|
||||
@@ -120,14 +171,21 @@ vm_name="${dish_name[$((choice - 1))]}"
|
||||
# Output the selected filename
|
||||
echo "You selected: $vm_name"
|
||||
|
||||
# Find Fedora ISO or use default location
|
||||
fedora_iso=$(find_fedora_iso)
|
||||
# Find Fedora ISO based on the dish name
|
||||
fedora_iso=$(find_fedora_iso "$vm_name")
|
||||
if [ -n "$fedora_iso" ]; then
|
||||
location_param="$fedora_iso"
|
||||
echo "Using local ISO: $fedora_iso"
|
||||
else
|
||||
location_param="https://download.fedoraproject.org/pub/fedora/linux/releases/43/Everything/x86_64/os/"
|
||||
echo "Using default online repository"
|
||||
# Fallback to original behavior if no specific ISO found
|
||||
fedora_iso=$(find_fedora_iso_old)
|
||||
if [ -n "$fedora_iso" ]; then
|
||||
location_param="$fedora_iso"
|
||||
echo "Using local ISO: $fedora_iso"
|
||||
else
|
||||
location_param="https://download.fedoraproject.org/pub/fedora/linux/releases/43/Everything/x86_64/os/"
|
||||
echo "Using default online repository"
|
||||
fi
|
||||
fi
|
||||
|
||||
# virt-install command with user-defined VM name
|
||||
@@ -165,4 +223,4 @@ virt-install \
|
||||
--initrd-inject ./dishes/"$vm_name".cfg \
|
||||
--extra-args "inst.ks=file:/$vm_name.cfg"
|
||||
|
||||
echo "virt-install command executed with VM name: $vm_name"
|
||||
echo "virt-install command executed with VM name: $vm_name"
|
||||
|
||||
Reference in New Issue
Block a user