Improve function: create_template

This commit is contained in:
Candifloss 2026-07-23 22:11:30 +05:30
parent fb0193e0f0
commit 7b9323b143
2 changed files with 105 additions and 71 deletions

View File

@ -1,4 +1,4 @@
# Default temporary Internet settings. # Default temporary Internet settings ###############################################
set -g VM_TEMP_NET_BRIDGE vmbr1 set -g VM_TEMP_NET_BRIDGE vmbr1
set -g VM_TEMP_NET_MODEL virtio set -g VM_TEMP_NET_MODEL virtio
@ -6,10 +6,29 @@ set -g VM_TEMP_NET_MODEL virtio
set -g VM_TEMP_NET_IP "192.0.2.100/24" set -g VM_TEMP_NET_IP "192.0.2.100/24"
set -g VM_TEMP_NET_GATEWAY "192.0.2.1" set -g VM_TEMP_NET_GATEWAY "192.0.2.1"
# Default template settings. # Default template settings #########################################################
set -g VM_TEMPLATE_BRIDGE vmbr0 set -g VM_TEMPLATE_BRIDGE vmbr0
set -g VM_TEMPLATE_BRIDGES vmbr0 vmbr1 vmbr2 set -g VM_TEMPLATE_BRIDGES vmbr0 vmbr1 vmbr2
set -g VM_TEMPLATE_IMAGE "/root/os_images/debian-13-generic-amd64.qcow2" set -g VM_TEMPLATE_IMAGE /root/os_images/debian-13-generic-amd64.qcow2
set -g VM_TEMPLATE_STORAGE netapp-lvm set -g VM_TEMPLATE_STORAGE netapp-lvm
set -g VM_TEMPLATE_MACHINE q35
set -g VM_TEMPLATE_CPU host
set -g VM_TEMPLATE_OSTYPE l26
set -g VM_TEMPLATE_CORES 2
set -g VM_TEMPLATE_MEMORY 2048
set -g VM_TEMPLATE_NET_MODEL virtio
set -g VM_TEMPLATE_SCSIHW virtio-scsi-single
set -g VM_TEMPLATE_AGENT enabled=1
set -g VM_TEMPLATE_BOOT order=scsi0
set -g VM_TEMPLATE_SERIAL socket
set -g VM_TEMPLATE_VGA serial0
set -g VM_TEMPLATE_IPCONFIG ip=dhcp
set -g VM_TEMPLATE_IMPORT_OPTS discard=on,ssd=1,iothread=1

View File

@ -1,113 +1,128 @@
function create_template --description "Create a Debian cloud-init template" function create_template --description "Create a Proxmox cloud-init template"
# Help
if contains -- $argv[1] -h --help if contains -- $argv[1] -h --help
echo "Usage: create_template <vmid> <name> [bridge] [image] [storage]" echo "Usage: create_template <vmid> <name> [bridge] [image] [storage]"
return 0 return 0
end end
# Arguments
set -l vmid $argv[1] set -l vmid $argv[1]
set -l name $argv[2] set -l name $argv[2]
set -l bridge $argv[3] set -l bridge $argv[3]
set -l image $argv[4] set -l image $argv[4]
set -l storage $argv[5] set -l storage $argv[5]
echo "Proxmox Debian Template Creator" # Defaults from config
echo
# Required arguments
if test -z "$vmid"
read -P "Template ID: " vmid
if test -z "$vmid"
echo "Error: Template ID is required."
return 1
end
end
if test -z "$name"
echo "Example: debian13tmpl"
read -P "Template Name: " name
if test -z "$name"
echo "Error: Template name is required."
return 1
end
end
# Optional arguments
test -n "$bridge"; or set bridge $VM_TEMPLATE_BRIDGE test -n "$bridge"; or set bridge $VM_TEMPLATE_BRIDGE
test -n "$image"; or set image $VM_TEMPLATE_IMAGE test -n "$image"; or set image $VM_TEMPLATE_IMAGE
test -n "$storage"; or set storage $VM_TEMPLATE_STORAGE test -n "$storage"; or set storage $VM_TEMPLATE_STORAGE
# Prompt for required values
test -n "$vmid"; or read -P "Template ID: " vmid
test -n "$name"; or read -P "Template name [e.g. debian13tmpl]: " name
if test -z "$vmid"
echo "Error: Template ID is required."
return 1
end
if test -z "$name"
echo "Error: Template name is required."
return 1
end
# Validate provided values
if test -z "$bridge" if test -z "$bridge"
echo "Error: VM_TEMPLATE_BRIDGE is not configured." echo "Error: No bridge configured."
return 1 return 1
end end
if test -z "$image" if test -z "$image"
echo "Error: VM_TEMPLATE_IMAGE is not configured." echo "Error: No image configured."
return 1 return 1
end end
if test -z "$storage" if test -z "$storage"
echo "Error: VM_TEMPLATE_STORAGE is not configured." echo "Error: No storage configured."
return 1 return 1
end end
# Allow bridge override interactively # Validate template configuration
for var in \
VM_TEMPLATE_MACHINE \
VM_TEMPLATE_CPU \
VM_TEMPLATE_CORES \
VM_TEMPLATE_MEMORY \
VM_TEMPLATE_NET_MODEL \
VM_TEMPLATE_SCSIHW \
VM_TEMPLATE_OSTYPE \
VM_TEMPLATE_AGENT \
VM_TEMPLATE_BOOT \
VM_TEMPLATE_SERIAL \
VM_TEMPLATE_VGA \
VM_TEMPLATE_IPCONFIG \
VM_TEMPLATE_IMPORT_OPTS
if test -z "$argv[3]" if not set -q $var
set -l options (string join "/" $VM_TEMPLATE_BRIDGES) echo "Error: $var is not configured."
return 1
read -P "Bridge [$options]: " tmp
if test -n "$tmp"
set bridge $tmp
end end
end end
# Summary
echo echo
echo "Template ID: $vmid" echo "Configuration"
echo "Name: $name"
echo "Bridge: $bridge"
echo "Image: $image"
echo "Storage: $storage"
echo echo
read -P "Continue? [y/N]: " confirm printf " %-12s %s\n" "VMID" "$vmid"
printf " %-12s %s\n" "Name" "$name"
printf " %-12s %s\n" "Bridge" "$bridge"
printf " %-12s %s\n" "Image" "$image"
printf " %-12s %s\n" "Storage" "$storage"
if test (string lower -- "$confirm") != y echo
printf " %-12s %s\n" "Machine" "$VM_TEMPLATE_MACHINE"
printf " %-12s %s\n" "CPU" "$VM_TEMPLATE_CPU"
printf " %-12s %s\n" "Cores" "$VM_TEMPLATE_CORES"
printf " %-12s %s MiB\n" "Memory" "$VM_TEMPLATE_MEMORY"
printf " %-12s %s\n" "NIC Model" "$VM_TEMPLATE_NET_MODEL"
printf " %-12s %s\n" "SCSI HW" "$VM_TEMPLATE_SCSIHW"
printf " %-12s %s\n" "OS Type" "$VM_TEMPLATE_OSTYPE"
echo
read -P "Create template? [y/N] " confirm
if not string match -iqr '^y(es)?$' "$confirm"
echo "Aborted." echo "Aborted."
return 1 return 0
end end
qm create $vmid \ qm create "$vmid" \
--name "$name" \ --name "$name" \
--ostype l26 \ --ostype "$VM_TEMPLATE_OSTYPE" \
--machine q35 \ --machine "$VM_TEMPLATE_MACHINE" \
--cpu host \ --cpu "$VM_TEMPLATE_CPU" \
--cores 2 \ --cores "$VM_TEMPLATE_CORES" \
--memory 2048 \ --memory "$VM_TEMPLATE_MEMORY" \
--agent enabled=1 \ --agent "$VM_TEMPLATE_AGENT" \
--scsihw virtio-scsi-single \ --scsihw "$VM_TEMPLATE_SCSIHW" \
--net0 "virtio,bridge=$bridge" --net0 "$VM_TEMPLATE_NET_MODEL,bridge=$bridge"
or return or return
qm set $vmid \ qm set "$vmid" \
--scsi0 "$storage:0,import-from=$image,discard=on,ssd=1,iothread=1" \ --scsi0 "$storage:0,import-from=$image,$VM_TEMPLATE_IMPORT_OPTS" \
--ide2 "$storage:cloudinit" \ --ide2 "$storage:cloudinit" \
--boot order=scsi0 \ --boot "$VM_TEMPLATE_BOOT" \
--serial0 socket \ --serial0 "$VM_TEMPLATE_SERIAL" \
--vga serial0 \ --vga "$VM_TEMPLATE_VGA" \
--ipconfig0 ip=dhcp --ipconfig0 "$VM_TEMPLATE_IPCONFIG"
or return or return
qm template $vmid qm template "$vmid"
or return or return
echo echo
echo "Template '$name' ($vmid) created successfully." echo "[OK] Template '$name' ($vmid) created."
end end