Advertisement
jracing

PVE-MOP_Dialog

Dec 26th, 2024 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.86 KB | Software | 0 0
  1. #!/bin/bash
  2.  
  3. # Default values for template
  4. MEMORY="2048"
  5. DISK_SIZE="10G"
  6. STORAGE="local-lvm"
  7. NET_IFACE="vmbr0"
  8.  
  9.  
  10. # Image data: a list of options with URLs and parameters
  11. declare -A IMAGE_DATA=(
  12.   ["Debian 12 Cloud"]="https://cdimage.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2|debian-12-nocloud-amd64.qcow2|9100|bookworm-cloud"
  13.   ["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"
  14.   ["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"
  15.   ["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"
  16.   ["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"
  17.   ["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"
  18.   ["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"
  19.   ["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"
  20.   ["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"
  21. )
  22.  
  23. # Prepare choices for dialog
  24. choices=()
  25. for template in "${!IMAGE_DATA[@]}"; do
  26.   choices+=("$template" "")
  27. done
  28.  
  29. # Use whiptail to present a single-choice menu
  30. selected_template=$(whiptail --menu \
  31.   "Select the cloud image template to use" \
  32.   20 60 10 \
  33.   "${choices[@]}" \
  34.   2>&1 >/dev/tty)
  35.  
  36. # If the user exits without selecting anything
  37. if [ -z "$selected_template" ]; then
  38.   echo "No template selected. Exiting."
  39.   exit 1
  40. fi
  41.  
  42. # Check if IMAGE_DATA contains the template
  43. if [[ -z "${IMAGE_DATA[$selected_template]}" ]]; then
  44.   echo "Error: Template '$selected_template' not found in IMAGE_DATA"
  45.   exit 1
  46. fi
  47.  
  48. # Parse the template data
  49. IFS="|" read -r IMAGE DST ID_VM VM_NAME <<< "${IMAGE_DATA[$selected_template]}"
  50.  
  51. # Check if ID_VM is valid (numeric and non-empty)
  52. if [[ ! "$ID_VM" =~ ^[0-9]+$ ]]; then
  53.   echo "Error: Invalid VM ID ($ID_VM) for template '$selected_template'. Exiting."
  54.   exit 1
  55. fi
  56.  
  57. clear
  58. # Log selected option
  59. echo "You selected: $selected_template"
  60. echo "Downloading cloud image..."
  61.  
  62.  
  63. # Download the image
  64. wget -O "$DST" "$IMAGE"
  65.  
  66. echo "Creating template for: $selected_template"
  67. # Create VM and set parameters
  68. qm create "$ID_VM" --name "$VM_NAME" --memory "$MEMORY" --net0 virtio,bridge="$NET_IFACE" --scsihw virtio-scsi-pci
  69. qm set "$ID_VM" --agent enabled=1,fstrim_cloned_disks=1 --cpu cputype=x86-64-v2
  70. qm set "$ID_VM" --scsi0 "$STORAGE":0,import-from="$PWD/$DST"
  71. qm set "$ID_VM" --ide2 "$STORAGE":cloudinit --boot order=scsi0 --serial0 socket
  72. qm disk resize "$ID_VM" scsi0 "$DISK_SIZE"
  73.  
  74. echo "Adding cloud-init configuration for: $selected_template"
  75. # Cloud-init (user and vendor scripts)
  76. qm set "$ID_VM" --cicustom "vendor=local:snippets/vendor-data.yaml"
  77. qm set "$ID_VM" --ciuser ansible --cipassword changeme --ipconfig0 ip=dhcp
  78. qm set "$ID_VM" --sshkeys <(echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIE5m+soZgnZ+l4p7gdsVcIqZw7tmoHTpV1rYgL8NkoUw DJ-ed25519-ansible")
  79.  
  80. qm cloudinit update "$ID_VM"
  81.  
  82. # Convert VM to template
  83. qm template "$ID_VM"
  84. rm -Rf "$DST"
  85. printf "Done!\n"
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement