Improve function: create_template
This commit is contained in:
parent
fb0193e0f0
commit
7b9323b143
@ -1,4 +1,4 @@
|
||||
# Default temporary Internet settings.
|
||||
# Default temporary Internet settings ###############################################
|
||||
|
||||
set -g VM_TEMP_NET_BRIDGE vmbr1
|
||||
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_GATEWAY "192.0.2.1"
|
||||
|
||||
# Default template settings.
|
||||
# Default template settings #########################################################
|
||||
|
||||
set -g VM_TEMPLATE_BRIDGE vmbr0
|
||||
set -g VM_TEMPLATE_BRIDGES vmbr0 vmbr1 vmbr2
|
||||
set -g VM_TEMPLATE_BRIDGE vmbr0
|
||||
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_STORAGE netapp-lvm
|
||||
set -g VM_TEMPLATE_IMAGE /root/os_images/debian-13-generic-amd64.qcow2
|
||||
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
|
||||
@ -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
|
||||
echo "Usage: create_template <vmid> <name> [bridge] [image] [storage]"
|
||||
return 0
|
||||
end
|
||||
|
||||
# Arguments
|
||||
set -l vmid $argv[1]
|
||||
set -l name $argv[2]
|
||||
set -l bridge $argv[3]
|
||||
set -l image $argv[4]
|
||||
set -l storage $argv[5]
|
||||
|
||||
echo "Proxmox Debian Template Creator"
|
||||
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
|
||||
|
||||
# Defaults from config
|
||||
test -n "$bridge"; or set bridge $VM_TEMPLATE_BRIDGE
|
||||
test -n "$image"; or set image $VM_TEMPLATE_IMAGE
|
||||
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"
|
||||
echo "Error: VM_TEMPLATE_BRIDGE is not configured."
|
||||
echo "Error: No bridge configured."
|
||||
return 1
|
||||
end
|
||||
|
||||
if test -z "$image"
|
||||
echo "Error: VM_TEMPLATE_IMAGE is not configured."
|
||||
echo "Error: No image configured."
|
||||
return 1
|
||||
end
|
||||
|
||||
if test -z "$storage"
|
||||
echo "Error: VM_TEMPLATE_STORAGE is not configured."
|
||||
echo "Error: No storage configured."
|
||||
return 1
|
||||
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]"
|
||||
set -l options (string join "/" $VM_TEMPLATE_BRIDGES)
|
||||
|
||||
read -P "Bridge [$options]: " tmp
|
||||
|
||||
if test -n "$tmp"
|
||||
set bridge $tmp
|
||||
if not set -q $var
|
||||
echo "Error: $var is not configured."
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
# Summary
|
||||
echo
|
||||
echo "Template ID: $vmid"
|
||||
echo "Name: $name"
|
||||
echo "Bridge: $bridge"
|
||||
echo "Image: $image"
|
||||
echo "Storage: $storage"
|
||||
echo "Configuration"
|
||||
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."
|
||||
return 1
|
||||
return 0
|
||||
end
|
||||
|
||||
qm create $vmid \
|
||||
qm create "$vmid" \
|
||||
--name "$name" \
|
||||
--ostype l26 \
|
||||
--machine q35 \
|
||||
--cpu host \
|
||||
--cores 2 \
|
||||
--memory 2048 \
|
||||
--agent enabled=1 \
|
||||
--scsihw virtio-scsi-single \
|
||||
--net0 "virtio,bridge=$bridge"
|
||||
--ostype "$VM_TEMPLATE_OSTYPE" \
|
||||
--machine "$VM_TEMPLATE_MACHINE" \
|
||||
--cpu "$VM_TEMPLATE_CPU" \
|
||||
--cores "$VM_TEMPLATE_CORES" \
|
||||
--memory "$VM_TEMPLATE_MEMORY" \
|
||||
--agent "$VM_TEMPLATE_AGENT" \
|
||||
--scsihw "$VM_TEMPLATE_SCSIHW" \
|
||||
--net0 "$VM_TEMPLATE_NET_MODEL,bridge=$bridge"
|
||||
or return
|
||||
|
||||
qm set $vmid \
|
||||
--scsi0 "$storage:0,import-from=$image,discard=on,ssd=1,iothread=1" \
|
||||
qm set "$vmid" \
|
||||
--scsi0 "$storage:0,import-from=$image,$VM_TEMPLATE_IMPORT_OPTS" \
|
||||
--ide2 "$storage:cloudinit" \
|
||||
--boot order=scsi0 \
|
||||
--serial0 socket \
|
||||
--vga serial0 \
|
||||
--ipconfig0 ip=dhcp
|
||||
--boot "$VM_TEMPLATE_BOOT" \
|
||||
--serial0 "$VM_TEMPLATE_SERIAL" \
|
||||
--vga "$VM_TEMPLATE_VGA" \
|
||||
--ipconfig0 "$VM_TEMPLATE_IPCONFIG"
|
||||
or return
|
||||
|
||||
qm template $vmid
|
||||
qm template "$vmid"
|
||||
or return
|
||||
|
||||
echo
|
||||
echo "Template '$name' ($vmid) created successfully."
|
||||
|
||||
echo "[OK] Template '$name' ($vmid) created."
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user