Skip to content

Commit

Permalink
Update bashvm-create-vm.sh
Browse files Browse the repository at this point in the history
- Fix Debian 12.5 not downloading
- Added extra ISO options to download
- Convert disk_question to lowercase
  • Loading branch information
babywhale321 authored Jul 1, 2024
1 parent 67249a0 commit 7e025bb
Showing 1 changed file with 56 additions and 14 deletions.
70 changes: 56 additions & 14 deletions bashvm-create-vm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,84 @@ generate_mac_address() {
printf '52:54:%02x:%02x:%02x:%02x\n' $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256))
}

# Prompt user for VM details
read -ep "Enter the name for the new / existing virtual machine: " new_vm_name
read -ep "Enter the amount of memory in MB (e.g., 1024): " new_memory
read -ep "Enter the number of virtual CPUs (e.g., 2): " new_vcpus

read -ep "Would you like to download or use a debian 12 iso? (y/n): " iso_question
if [ "$iso_question" == y ];then

read -ep "Enter the storage pool name [default]: " pool_name
pool_image_download() {
read -ep "Enter the storage pool name to download / use [default]: " pool_name

if [ -z "$pool_name" ]; then
pool_name="default"
fi
pool_path=$(virsh pool-dumpxml "$pool_name" | grep '<path>' | cut -d'>' -f2 | cut -d'<' -f1)
iso_img=$(ls "$pool_path"/ | grep "debian-12.5.0-amd64-netinst.iso")
iso_path="$pool_path/$iso_img"
# Check to see if the iso file is there
if [ -f "$iso_path" ]; then
# ISO is already present, Dont download
echo "File debian-12.5.0-amd64-netinst.iso already there. Canceling re-download."
echo "File "$iso_img" already there. Canceling re-download."
else
# ISO is not present, Download
cd "$pool_path"
wget https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-12.5.0-amd64-netinst.iso
iso_img="debian-12.5.0-amd64-netinst.iso"
wget "$iso_download"
iso_path="$pool_path/$iso_img"
fi
}


# Prompt user for VM details
read -ep "Enter the name for the new / existing virtual machine: " new_vm_name
read -ep "Enter the amount of memory in MB (e.g., 1024): " new_memory
read -ep "Enter the number of virtual CPUs (e.g., 2): " new_vcpus

echo ""
echo "1. debian-12.5 2. ubuntu-22.04 3. AlmaLinux-9.4"
echo "4. openmediavault_7.0 5. TrueNas-13.0 "
echo ""

echo "Enter the iso you would like to use"
read -ep "You can safely say no if you have your own or not using an iso: " iso_question


if [ "$iso_question" == 1 ];then
iso_img="debian-12.5.0-amd64-netinst.iso"
iso_download="https://cdimage.debian.org/mirror/cdimage/archive/12.5.0/amd64/iso-cd/debian-12.5.0-amd64-netinst.iso"
pool_image_download

elif [ "$iso_question" == 2 ];then
iso_img="ubuntu-22.04.4-live-server-amd64.iso"
iso_download="https://releases.ubuntu.com/jammy/ubuntu-22.04.4-live-server-amd64.iso"
pool_image_download

elif [ "$iso_question" == 3 ];then
iso_img="AlmaLinux-9.4-x86_64-minimal.iso"
iso_download="https://repo.almalinux.org/almalinux/9.4/isos/x86_64/AlmaLinux-9.4-x86_64-minimal.iso"
pool_image_download

elif [ "$iso_question" == 4 ];then
iso_img="openmediavault_7.0-32-amd64.iso"
iso_download="https://sourceforge.net/projects/openmediavault/files/iso/7.0-32/openmediavault_7.0-32-amd64.iso"
pool_image_download

elif [ "$iso_question" == 5 ];then
iso_img="TrueNAS-13.0-U6.1.iso"
iso_download="https://download-core.sys.truenas.net/13.0/STABLE/U6.1/x64/TrueNAS-13.0-U6.1.iso"
pool_image_download

else
# full iso path needed
echo "Enter the full path to the ISO file (e.g., /var/lib/libvirt/images/debian-12.5.0-amd64-netinst.iso)"
echo "Note: If you dont want to add an ISO then you can just ignore this option and press enter"
read -ep ": " iso_path
fi



# DISK CREATION #


read -ep "Would you like to create a new volume? (y/n): " disk_question
if [ "$disk_question" == y ];then

# Convert to lowercase
lowercase_input=$(echo "$disk_question" | tr '[:upper:]' '[:lower:]')
if [[ "$lowercase_input" == y || "$lowercase_input" == yes ]];then

# disk name, capacity and pool
read -ep "Enter the name of the new storage volume (e.g., new-vm): " volume_name
read -ep "Enter the size of the volume (e.g., 10GB): " volume_capacity
Expand Down

0 comments on commit 7e025bb

Please sign in to comment.