Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Default values for template
- MEMORY="2048"
- DISK_SIZE="10G"
- STORAGE="local-lvm"
- NET_IFACE="vmbr0"
- # Image data: a list of options with URLs and parameters
- declare -A IMAGE_DATA=(
- ["Debian 12 Cloud"]="https://cdimage.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2|debian-12-nocloud-amd64.qcow2|9100|bookworm-cloud"
- ["Ubuntu Server Cloud 24.04 LTS"]="https://cloud-images.ubuntu.com/minimal/releases/noble/release/ubuntu-24.04-minimal-cloudimg-amd64.img|ubuntu-24.04-minimal-cloudimg-amd64.img|9101|noble-cloud"
- ["Rocky Linux 9 Cloud"]="https://dl.rockylinux.org/pub/rocky/9/images/x86_64/Rocky-9-GenericCloud-Base.latest.x86_64.qcow2|Rocky-9-GenericCloud-Base.latest.x86_64.qcow2|9102|rocky-9.5-cloud"
- ["Oracle Linux 9 Cloud"]="https://yum.oracle.com/templates/OracleLinux/OL9/u5/x86_64/OL9U5_x86_64-kvm-b253.qcow2|OL9U5_x86_64-kvm-b253.qcow2|9103|ol-9-cloud"
- ["Alma Linux 9 Cloud"]="https://repo.almalinux.org/almalinux/9.4/cloud/x86_64/images/AlmaLinux-9-GenericCloud-latest.x86_64.qcow2|AlmaLinux-9-GenericCloud-latest.x86_64.qcow2|9104|alma-9-cloud"
- ["Ubuntu Server Cloud 20.04 LTS"]="https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img|focal-server-cloudimg-amd64.img|9105|focal-cloud"
- ["CentOS Stream 9"]="https://cloud.centos.org/centos/9-stream/x86_64/images/CentOS-Stream-GenericCloud-9-latest.x86_64.qcow2|CentOS-Stream-GenericCloud-9-latest.x86_64.qcow2|9106|centos-Stream-9"
- ["Fedora Server 40"]="https://download.fedoraproject.org/pub/fedora/linux/releases/40/Cloud/x86_64/images/Fedora-Cloud-Base-Generic.x86_64-40-1.14.qcow2|Fedora-Cloud-Base-Generic.x86_64-40-1.14.qcow2|9107|fedora-server-40"
- ["Arch Linux 2024.08.01"]="https://geo.mirror.pkgbuild.com/images/latest/Arch-Linux-x86_64-cloudimg.qcow2|Arch-Linux-x86_64-cloudimg.qcow2|9108|arch-linux-2024.08.01"
- )
- # Prepare choices for dialog
- choices=()
- for template in "${!IMAGE_DATA[@]}"; do
- choices+=("$template" "")
- done
- # Use whiptail to present a single-choice menu
- selected_template=$(whiptail --menu \
- "Select the cloud image template to use" \
- 20 60 10 \
- "${choices[@]}" \
- 2>&1 >/dev/tty)
- # If the user exits without selecting anything
- if [ -z "$selected_template" ]; then
- echo "No template selected. Exiting."
- exit 1
- fi
- # Check if IMAGE_DATA contains the template
- if [[ -z "${IMAGE_DATA[$selected_template]}" ]]; then
- echo "Error: Template '$selected_template' not found in IMAGE_DATA"
- exit 1
- fi
- # Parse the template data
- IFS="|" read -r IMAGE DST ID_VM VM_NAME <<< "${IMAGE_DATA[$selected_template]}"
- # Check if ID_VM is valid (numeric and non-empty)
- if [[ ! "$ID_VM" =~ ^[0-9]+$ ]]; then
- echo "Error: Invalid VM ID ($ID_VM) for template '$selected_template'. Exiting."
- exit 1
- fi
- clear
- # Log selected option
- echo "You selected: $selected_template"
- echo "Downloading cloud image..."
- # Download the image
- wget -O "$DST" "$IMAGE"
- echo "Creating template for: $selected_template"
- # Create VM and set parameters
- qm create "$ID_VM" --name "$VM_NAME" --memory "$MEMORY" --net0 virtio,bridge="$NET_IFACE" --scsihw virtio-scsi-pci
- qm set "$ID_VM" --agent enabled=1,fstrim_cloned_disks=1 --cpu cputype=x86-64-v2
- qm set "$ID_VM" --scsi0 "$STORAGE":0,import-from="$PWD/$DST"
- qm set "$ID_VM" --ide2 "$STORAGE":cloudinit --boot order=scsi0 --serial0 socket
- qm disk resize "$ID_VM" scsi0 "$DISK_SIZE"
- echo "Adding cloud-init configuration for: $selected_template"
- # Cloud-init (user and vendor scripts)
- qm set "$ID_VM" --cicustom "vendor=local:snippets/vendor-data.yaml"
- qm set "$ID_VM" --ciuser ansible --cipassword changeme --ipconfig0 ip=dhcp
- qm set "$ID_VM" --sshkeys <(echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIE5m+soZgnZ+l4p7gdsVcIqZw7tmoHTpV1rYgL8NkoUw DJ-ed25519-ansible")
- qm cloudinit update "$ID_VM"
- # Convert VM to template
- qm template "$ID_VM"
- rm -Rf "$DST"
- printf "Done!\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement