-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplatectl.sh
More file actions
executable file
·1173 lines (997 loc) · 32.9 KB
/
templatectl.sh
File metadata and controls
executable file
·1173 lines (997 loc) · 32.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
#
# templatectl.sh - Creates Proxmox VE templates from cloud-init images.
# Repository: https://git.mukhtabar.de/proxmox/proxstack
# Maintainers: Proless
set -euo pipefail
# ==============================================================================
# GLOBAL VARIABLES & CONFIGURATION
# ==============================================================================
# Supported distro families. RHEL-compatible distros are normalized to "rhel".
declare -a SUPPORTED_DISTROS=("debian" "ubuntu" "fedora" "rhel")
# Storage configuration
declare -A DISK_STORAGE_CONFIG=()
declare -A SNIPPETS_STORAGE_CONFIG=()
# Distro information extracted from image
declare -A DISTRO_INFO=()
# Keyboard configuration
declare -A KEYBOARD_CONFIG=(
[layout]="" # Keyboard layout
[variant]="" # Keyboard variant
)
# Disk configuration
declare -A DISK_CONFIG=(
[size]="" # Disk size for the VM (e.g., 32G)
[bus]="scsi" # Disk bus/controller type: scsi (default), virtio, sata, ide
[format]="qcow2" # Disk format: qcow2 (default), raw, or vmdk
[flags]="discard=on" # Default disk flags
[scsihw]="virtio-scsi-single" # SCSI controller model (default: virtio-scsi-single)
[storage]="local-lvm" # The Proxmox storage where the VM disk will be allocated (default: local-lvm)
)
# Network configuration
declare -A NET_CONFIG=(
[bridge]="vmbr0" # The Proxmox network bridge for the VM (default: vmbr0)
[vlan]="" # Optional VLAN tag for net0 (1-4094)
)
# SSH configuration
declare -A SSH_CONFIG=(
[keys]="" # Path to file with public SSH keys
)
# DNS configuration
declare -A DNS_CONFIG=(
[servers]="" # DNS servers
[domains]="" # Domain search domains
)
# Snippets configuration
declare -A SNIPPETS_CONFIG=(
[storage]="" # Storage where snippets are stored (default: same as DISK_CONFIG[storage])
)
declare -a MERGED_ARGS=() # Final merged arguments built from config defaults + CLI overrides
# Settings
ID="" # ID for the template
URL="" # Cloud Image URL
NAME="" # Name for the template
IMAGE_FILE="" # Local path to the downloaded image file
USER="" # Cloud-init user
PASSWORD="" # Cloud-init password
UPGRADE="0" # Cloud-init package upgrade behavior: 1=enable, 0=disable
MEMORY="2048" # Memory in MB
CORES="4" # Number of CPU cores
CPU="x86-64-v2-AES" # CPU type
DISPLAY="std" # Display type
TIMEZONE="" # Timezone
LOCALE="" # Locale
# Advanced options
PACKAGES="" # Space-separated list of packages to install inside the VM template
PATCHES="" # Space-separated list of patches to apply (none by default)
REBOOT="false" # Reboot VM after cloud-init completes
ONBOOT="0" # Start VM automatically on Proxmox host boot
# Internal variables
VENDOR_ONLY="false" # Write vendor-data file and exit before VM creation
VERBOSE_MODE="false" # Enable verbose mode for debugging
# ==============================================================================
# CLOUD-INIT VENDOR DATA
# ==============================================================================
ci_create_base_config() {
local vendor_data_file="${1}"
# Create base vendor-data file with update settings
yq -y -n \
" .package_update = true
| .package_reboot_if_required = true
| .packages = []
| .write_files = []
| .runcmd = []
" >"${vendor_data_file}"
}
ci_add_qemu_guest_agent() {
local vendor_data_file="${1}"
# Add qemu-guest-agent package
yq -i -y ".packages += [\"qemu-guest-agent\"]" "${vendor_data_file}"
# Add distro-specific commands to enable and start qemu-guest-agent
case "${DISTRO_INFO[family]}" in
debian | ubuntu | fedora | rhel)
yq -i -y ".runcmd += [\"systemctl enable qemu-guest-agent\"]" "${vendor_data_file}"
yq -i -y ".runcmd += [\"systemctl start qemu-guest-agent\"]" "${vendor_data_file}"
;;
esac
}
ci_add_extra_packages() {
local vendor_data_file="${1}"
# Append extra packages if specified
if [[ -n "${PACKAGES}" ]]; then
IFS=' ' read -ra pkg_array <<<"${PACKAGES}"
for pkg in "${pkg_array[@]}"; do
yq -i -y ".packages += [\"${pkg}\"]" "${vendor_data_file}"
done
fi
}
ci_add_localization() {
local vendor_data_file="${1}"
# Add locale configuration
[[ -n "${LOCALE}" ]] && yq -i -y ".locale = \"${LOCALE}\"" "${vendor_data_file}"
# Add timezone configuration
[[ -n "${TIMEZONE}" ]] && yq -i -y ".timezone = \"${TIMEZONE}\"" "${vendor_data_file}"
# Add keyboard configuration
if [[ -n "${KEYBOARD_CONFIG[layout]}" ]]; then
yq -i -y ".keyboard.layout = \"${KEYBOARD_CONFIG[layout]}\"" "${vendor_data_file}"
[[ -n "${KEYBOARD_CONFIG[variant]}" ]] && yq -i -y ".keyboard.variant = \"${KEYBOARD_CONFIG[variant]}\"" "${vendor_data_file}"
fi
}
ci_add_reboot() {
local vendor_data_file="${1}"
[[ "${REBOOT}" != "true" ]] && return 0
yq -i -y '.power_state = {"mode":"reboot","message":"Rebooting after cloud-init completion","timeout":30,"condition":true}' "${vendor_data_file}"
}
ci_build_vendor_data() {
local vendor_data_file="${1}"
echo "Building cloud-init vendor-data..."
ci_create_base_config "${vendor_data_file}"
ci_add_qemu_guest_agent "${vendor_data_file}"
ci_add_extra_packages "${vendor_data_file}"
ci_add_localization "${vendor_data_file}"
ci_add_reboot "${vendor_data_file}"
}
# ==============================================================================
# TEMPLATE
# ==============================================================================
prepare_disk() {
local image_file="${1}"
# Resize disk if size specified
if [[ -n "${DISK_CONFIG[size]}" ]]; then
echo "Resizing disk to ${DISK_CONFIG[size]}..."
quiet_run qemu-img resize "${image_file}" "${DISK_CONFIG[size]}"
fi
}
create_vm() {
local image_file="${1}"
local net0_config="virtio,macaddr=00:00:00:00:00:00,bridge=${NET_CONFIG[bridge]}"
[[ -n "${NET_CONFIG[vlan]}" ]] && net0_config+=",tag=${NET_CONFIG[vlan]}"
echo "Creating VM ${ID}..."
quiet_run qm create "${ID}" --name "${NAME}" \
--memory "${MEMORY}" \
--cpu "${CPU}" \
--cores "${CORES}" \
--net0 "${net0_config}" \
--agent enabled=1 \
--ostype l26 \
--vga "${DISPLAY}" \
--serial0 socket
echo "Importing disk..."
quiet_run qm importdisk "${ID}" "${image_file}" "${DISK_STORAGE_CONFIG[name]}" --format "${DISK_CONFIG[format]}"
}
configure_vm() {
echo "Configuring VM storage and cloud-init..."
# Build disk path based on storage type
local disk_path
if [[ "${DISK_STORAGE_CONFIG[type]}" =~ ^(lvmthin|zfspool|rbd)$ ]]; then
# Block storage types use simple format: storage:vm-ID-disk-N
disk_path="${DISK_STORAGE_CONFIG[name]}:vm-${ID}-disk-0"
else
# Directory-based storage types use: storage:ID/vm-ID-disk-N.format
disk_path="${DISK_STORAGE_CONFIG[name]}:${ID}/vm-${ID}-disk-0.${DISK_CONFIG[format]}"
fi
# Build qm set command with conditional cloud-init parameters
local disk_device
local cloudinit_device="ide0"
case "${DISK_CONFIG[bus]}" in
scsi)
disk_device="scsi0"
;;
virtio)
disk_device="virtio0"
;;
sata)
disk_device="sata0"
;;
ide)
disk_device="ide0"
cloudinit_device="ide2"
;;
*)
die "Unsupported disk bus '${DISK_CONFIG[bus]}'. Supported values: scsi, virtio, sata, ide"
;;
esac
local qm_cmd=(qm set "${ID}"
--ciupgrade "${UPGRADE}"
--cicustom "vendor=${SNIPPETS_STORAGE_CONFIG[name]}:snippets/ci-vendor-data-${ID}.yml"
--ipconfig0 "ip=dhcp"
--onboot "${ONBOOT}"
)
# scsihw only applies to scsi bus.
[[ "${DISK_CONFIG[bus]}" == "scsi" ]] && qm_cmd+=(--scsihw "${DISK_CONFIG[scsihw]}")
qm_cmd+=("--${disk_device}" "${disk_path},${DISK_CONFIG[flags]// /,}")
qm_cmd+=("--${cloudinit_device}" "${DISK_STORAGE_CONFIG[name]}:cloudinit")
qm_cmd+=(--boot "order=${disk_device}")
# Add DNS servers if specified
[[ -n "${DNS_CONFIG[servers]}" ]] && qm_cmd+=(--nameserver "${DNS_CONFIG[servers]}")
# Add search domain if specified
[[ -n "${DNS_CONFIG[domains]}" ]] && qm_cmd+=(--searchdomain "${DNS_CONFIG[domains]}")
# Add cloud-init user settings if user is specified
if [[ -n "${USER}" ]]; then
qm_cmd+=(--ciuser "${USER}")
[[ -n "${PASSWORD}" ]] && qm_cmd+=(--cipassword "${PASSWORD}")
[[ -n "${SSH_CONFIG[keys]}" ]] && qm_cmd+=(--sshkeys "${SSH_CONFIG[keys]}")
fi
quiet_run "${qm_cmd[@]}"
}
create_template() {
echo "Creating template ${NAME} (ID: ${ID})..."
local tmp_yaml
local image_copy
local vendor_data_file
tmp_yaml=$(mktemp)
image_copy="${NAME}.${IMAGE_FILE##*.}"
vendor_data_file="${SNIPPETS_STORAGE_CONFIG[snippets_dir]}/ci-vendor-data-${ID}.yml"
# Create a copy of the image
cp "${IMAGE_FILE}" "${image_copy}"
# Build the complete vendor-data
ci_build_vendor_data "${tmp_yaml}"
# Apply patches
apply_patches "${tmp_yaml}" "${image_copy}"
# Prepend header and create the final ci config file
{
echo "#cloud-config"
cat "${tmp_yaml}"
} >"${vendor_data_file}"
if [[ "${VENDOR_ONLY}" == "true" ]]; then
rm -f "${tmp_yaml}" "${image_copy}"
realpath "${vendor_data_file}"
return 0
fi
# Prepare the disk
prepare_disk "${image_copy}"
# Create VM
create_vm "${image_copy}"
# Configure VM
configure_vm
# Convert to template
echo "Converting VM ${ID} to a template..."
quiet_run qm template "${ID}"
# Clean up temporary files
rm -f "${tmp_yaml}"
rm -f "${image_copy}"
echo "Template ${NAME} created successfully"
}
# ==============================================================================
# ARGUMENT
# ==============================================================================
require_arg_file() {
if [[ ! -f "${1}" || ! -s "${1}" ]]; then
die "File not found or empty: ${2} (${1})"
fi
}
require_arg_string() {
if [[ -z "${1}" ]]; then
die "Missing required argument: ${2}"
fi
}
require_arg_number() {
if ! [[ "${1}" =~ ^[0-9]+$ && "${1}" -gt 0 ]]; then
die "Argument '${2}' must be a positive number (got '${1}')"
fi
}
require_arg_vlan() {
if ! [[ "${1}" =~ ^[0-9]+$ ]] || [[ "${1}" -lt 1 ]] || [[ "${1}" -gt 4094 ]]; then
die "Argument '${2}' must be a VLAN ID between 1 and 4094 (got '${1}')"
fi
}
require_arg_disk_bus() {
case "${1}" in
scsi | virtio | sata | ide)
return 0
;;
*)
die "Argument '${2}' must be one of: scsi, virtio, sata, ide (got '${1}')"
;;
esac
}
parse_storage_config() {
local storage="${1}"
local -n storage_config="${2}"
local cfg="/etc/pve/storage.cfg"
if [[ ! -f "${cfg}" ]]; then
echo "Error: Storage configuration file not found at ${cfg}" >&2
return 1
fi
# Initialize variables
local in_section=0
local storage_type=""
local path=""
local content=""
local content_dirs=""
# Parse storage.cfg to find the storage section and extract information
while IFS= read -r line; do
# Check if this is our storage header
if [[ "${line}" =~ ^(dir|nfs|cifs|cephfs|lvmthin|zfspool|rbd):[[:space:]]+${storage}$ ]]; then
in_section=1
storage_type="${BASH_REMATCH[1]}"
continue
fi
# Check if we're entering a new storage section
if [[ "${line}" =~ ^[a-z]+: ]]; then
if [[ "${in_section}" -eq 1 ]]; then
break
fi
fi
# Extract properties if in our section
if [[ "${in_section}" -eq 1 ]]; then
if [[ "${line}" =~ ^[[:space:]]+path[[:space:]]+(.+)$ ]]; then
path="${BASH_REMATCH[1]}"
elif [[ "${line}" =~ ^[[:space:]]+content[[:space:]]+(.+)$ ]]; then
content="${BASH_REMATCH[1]}"
elif [[ "${line}" =~ ^[[:space:]]+content-dirs[[:space:]]+(.+)$ ]]; then
content_dirs="${BASH_REMATCH[1]}"
fi
fi
done <"${cfg}"
# Validate results
if [[ "${in_section}" -eq 0 ]]; then
echo "Error: Storage '${storage}' not found or is not supported." >&2
return 1
fi
# For network storage types, default to /mnt/pve/<storage> if path is empty
if [[ "${storage_type}" =~ ^(nfs|cifs|cephfs)$ ]] && [[ -z "${path}" ]]; then
path="/mnt/pve/${storage}"
fi
# Check if storage supports images
local supports_images="false"
local image_formats=""
if [[ "${content}" == *"images"* ]]; then
supports_images="true"
# Set supported image formats based on storage type
case "${storage_type}" in
dir)
image_formats="raw,qcow2,vmdk,subvol"
;;
nfs | cifs)
image_formats="raw,qcow2,vmdk"
;;
lvmthin)
image_formats="raw"
;;
zfspool)
image_formats="raw,subvol"
;;
rbd)
image_formats="raw"
;;
esac
fi
# Check if storage supports snippets
local supports_snippets="false"
local snippets_dir=""
if [[ "${content}" == *"snippets"* && -n "${path}" ]]; then
supports_snippets="true"
# Determine snippets directory
local relative_dir="snippets"
# Check if content-dirs has a custom snippets path
if [[ -n "${content_dirs}" ]] && [[ "${content_dirs}" =~ snippets=([^,]+) ]]; then
relative_dir="${BASH_REMATCH[1]}"
fi
# Construct full path
snippets_dir="${path}/${relative_dir}"
mkdir -p "${snippets_dir}"
fi
# Store configuration in associative array
storage_config["name"]="${storage}"
storage_config["type"]="${storage_type}"
storage_config["path"]="${path}"
storage_config["content"]="${content}"
storage_config["content_dirs"]="${content_dirs}"
storage_config["supports_images"]="${supports_images}"
storage_config["image_formats"]="${image_formats}"
storage_config["supports_snippets"]="${supports_snippets}"
# shellcheck disable=SC2034
storage_config["snippets_dir"]="${snippets_dir}"
}
parse_arguments() {
while [[ "$#" -gt 0 ]]; do
case "${1}" in
--url)
URL="${2}"
shift 2
;;
--id)
ID="${2}"
shift 2
;;
--name)
NAME="${2}"
shift 2
;;
--user)
USER="${2}"
shift 2
;;
--password)
PASSWORD="${2}"
shift 2
;;
--upgrade)
UPGRADE="1"
shift
;;
--memory)
MEMORY="${2}"
shift 2
;;
--cores)
CORES="${2}"
shift 2
;;
--cpu)
CPU="${2}"
shift 2
;;
--disk-scsihw)
DISK_CONFIG[scsihw]="${2}"
shift 2
;;
--net-bridge)
NET_CONFIG[bridge]="${2}"
shift 2
;;
--net-vlan)
NET_CONFIG[vlan]="${2}"
shift 2
;;
--disk-size)
DISK_CONFIG[size]="${2}"
shift 2
;;
--disk-bus)
DISK_CONFIG[bus]="${2}"
shift 2
;;
--disk-storage)
DISK_CONFIG[storage]="${2}"
SNIPPETS_CONFIG[storage]="${SNIPPETS_CONFIG[storage]:-${2}}"
shift 2
;;
--disk-format)
DISK_CONFIG[format]="${2}"
shift 2
;;
--disk-flags)
DISK_CONFIG[flags]="${2}"
shift 2
;;
--display)
DISPLAY="${2}"
shift 2
;;
--timezone)
TIMEZONE="${2}"
shift 2
;;
--keyboard-layout)
KEYBOARD_CONFIG[layout]="${2}"
shift 2
;;
--keyboard-variant)
KEYBOARD_CONFIG[variant]="${2}"
shift 2
;;
--locale)
LOCALE="${2}"
shift 2
;;
--ssh-keys)
SSH_CONFIG[keys]="${2}"
shift 2
;;
--dns-servers)
DNS_CONFIG[servers]="${2}"
shift 2
;;
--dns-domains)
DNS_CONFIG[domains]="${2}"
shift 2
;;
--snippets-storage)
SNIPPETS_CONFIG[storage]="${2}"
shift 2
;;
--packages)
PACKAGES="${2}"
shift 2
;;
--patches)
PATCHES+=" ${2}"
shift 2
;;
--reboot)
REBOOT="true"
shift
;;
--onboot)
ONBOOT="1"
shift
;;
--vendor-only)
VENDOR_ONLY="true"
shift
;;
-v | --verbose)
VERBOSE_MODE="true"
shift
;;
-h | --help)
usage
exit 0
;;
*)
die "Unknown option: ${1}"
;;
esac
done
# Parse storage configuration
parse_storage_config "${DISK_CONFIG[storage]}" DISK_STORAGE_CONFIG
if [[ "${SNIPPETS_CONFIG[storage]}" == "${DISK_CONFIG[storage]}" ]]; then
# Copy storage config
for key in "${!DISK_STORAGE_CONFIG[@]}"; do
SNIPPETS_STORAGE_CONFIG["${key}"]="${DISK_STORAGE_CONFIG[${key}]}"
done
else
parse_storage_config "${SNIPPETS_CONFIG[storage]}" SNIPPETS_STORAGE_CONFIG
fi
}
validate_args() {
# Validate required parameters
require_arg_number "${ID}" "id (--id)"
if [[ "${VENDOR_ONLY}" != "true" ]] && qm status "${ID}" &>/dev/null; then
die "ID ${ID} already exists. Please choose a different ID."
fi
require_arg_string "${URL}" "url (--url)"
require_arg_string "${NAME}" "name (--name)"
require_arg_string "${DISK_CONFIG[storage]}" "disk storage (--disk-storage)"
require_arg_string "${DISK_CONFIG[format]}" "disk format (--disk-format)"
require_arg_string "${NET_CONFIG[bridge]}" "network bridge (--net-bridge)"
require_arg_number "${MEMORY}" "memory (--memory)"
require_arg_number "${CORES}" "cores (--cores)"
# Validate optional parameters
if [[ -n "${USER}" ]]; then
if [[ -z "${PASSWORD}" && -z "${SSH_CONFIG[keys]}" ]]; then
die "You must provide at least one of --password or --ssh-keys when --user is specified"
fi
# If SSH keys provided, check file existence
[[ -n "${SSH_CONFIG[keys]}" ]] && require_arg_file "${SSH_CONFIG[keys]}" "ssh keys (--ssh-keys)"
else
echo "Warning: No cloud-init user provided"
fi
[[ -n "${NET_CONFIG[vlan]}" ]] && require_arg_vlan "${NET_CONFIG[vlan]}" "vlan (--net-vlan)"
[[ -n "${DISK_CONFIG[bus]}" ]] && require_arg_disk_bus "${DISK_CONFIG[bus]}" "disk bus (--disk-bus)"
}
validate_storage() {
# Validate disk storage supports images
if [[ "${DISK_STORAGE_CONFIG[supports_images]}" != "true" ]]; then
die "Storage '${DISK_STORAGE_CONFIG[name]}' does not support VM disk images. Supported content: ${DISK_STORAGE_CONFIG[content]}"
fi
# Validate disk format is supported by the storage type
local supported_formats="${DISK_STORAGE_CONFIG[image_formats]}"
if [[ ! ",${supported_formats}," == *",${DISK_CONFIG[format]},"* ]]; then
die "Disk format '${DISK_CONFIG[format]}' is not supported by storage '${DISK_STORAGE_CONFIG[name]}' (type: ${DISK_STORAGE_CONFIG[type]}). Supported formats: ${supported_formats}"
fi
# Validate snippets storage supports snippets
if [[ "${SNIPPETS_STORAGE_CONFIG[supports_snippets]}" != "true" ]]; then
die "Storage '${SNIPPETS_STORAGE_CONFIG[name]}' does not support snippets. Supported content: ${SNIPPETS_STORAGE_CONFIG[content]}"
fi
# Verify actual directories are writable (Proxmox-specific)
local snippets_dir="${SNIPPETS_STORAGE_CONFIG[snippets_dir]}"
if [[ -n "${snippets_dir}" ]] && [[ ! -w "${snippets_dir}" ]]; then
die "Snippets directory not writable: ${snippets_dir}"
fi
}
validate_distro() {
echo "Detecting distro from image..."
# Run virt-inspector once and capture full XML output
local inspector_xml
inspector_xml=$(virt-inspector --no-applications -a "${IMAGE_FILE}" 2>/dev/null)
# Helper: extract a single XML tag value; returns empty string if not found
_extract_xml_field() {
local tag="${1}"
local value
value=$(echo "${inspector_xml}" | grep "<${tag}>" | head -1 | sed -E "s/.*<${tag}>([^<]+)<\/${tag}>.*/\1/")
echo "${value}"
}
# Detect the distro
DISTRO_INFO[name]=$(_extract_xml_field "distro")
if [[ -z "${DISTRO_INFO[name]}" ]]; then
die "Failed to detect distro from image"
fi
DISTRO_INFO[family]=$(normalize_distro "${DISTRO_INFO[name]}") || die "Unsupported distro '${DISTRO_INFO[name]}'. Supported distro families: ${SUPPORTED_DISTROS[*]}"
# Populate DISTRO_INFO array; fields default to empty if not present
DISTRO_INFO[arch]=$(_extract_xml_field "arch")
DISTRO_INFO[osinfo]=$(_extract_xml_field "osinfo")
DISTRO_INFO[product_name]=$(_extract_xml_field "product_name")
DISTRO_INFO[package_format]=$(_extract_xml_field "package_format")
DISTRO_INFO[package_management]=$(_extract_xml_field "package_management")
echo "Detected distro: ${DISTRO_INFO[product_name]} (family: ${DISTRO_INFO[family]})"
[[ -n "${DISTRO_INFO[arch]}" ]] && echo " arch: ${DISTRO_INFO[arch]}"
[[ -n "${DISTRO_INFO[osinfo]}" ]] && echo " osinfo: ${DISTRO_INFO[osinfo]}"
[[ -n "${DISTRO_INFO[product_name]}" ]] && echo " product_name: ${DISTRO_INFO[product_name]}"
[[ -n "${DISTRO_INFO[package_format]}" ]] && echo " package_format: ${DISTRO_INFO[package_format]}"
[[ -n "${DISTRO_INFO[package_management]}" ]] && echo " package_management: ${DISTRO_INFO[package_management]}"
}
# ==============================================================================
# UTILITY
# ==============================================================================
quiet_run() {
if [[ "$VERBOSE_MODE" == "true" ]]; then
"$@"
else
"$@" >/dev/null 2>&1 || die "Command failed: $*"
fi
}
quiet_run_ext() {
local cmd="$*"
local fname="__tmp_wrap_${$}_${RANDOM}"
# Create a dynamic wrapper function that contains the entire command
eval "
$fname() {
set -e
$cmd
}
"
# Run through quiet_run so quiet/verbose works
quiet_run "$fname"
# Clean up
unset -f "$fname"
}
die() {
echo "$*" >&2
exit 1
}
usage() {
echo "Usage: $0 --url <url> --id <id> --name <name> [OPTIONS]"
echo " $0 --config <file|name> [OPTIONS]"
echo ""
echo "Creates a Proxmox VE template for a given Linux cloud image."
echo ""
echo "Required options:"
echo " --url <url> URL to the cloud image to use for the template"
echo " --id <id> ID for the template"
echo " --name <name> Name for the template"
echo ""
echo "Options:"
echo " --user <user> Set the cloud-init user"
echo " --password <password> Set the cloud-init password"
echo " --upgrade Enable cloud-init package upgrade (default: disabled)"
echo " --net-bridge <bridge> Network bridge for VM (default: vmbr0)"
echo " --net-vlan <id> VLAN tag for VM network interface (1-4094)"
echo " --memory <mb> Memory in MB (default: 2048)"
echo " --cores <num> Number of CPU cores (default: 4)"
echo " --cpu <type> CPU type for VM (default: x86-64-v2-AES)"
echo " --disk-scsihw <type> SCSI controller model for scsi bus (default: virtio-scsi-single)"
echo " --timezone <timezone> Timezone (e.g., America/New_York, Europe/London)"
echo " --keyboard-layout <layout> Keyboard layout (e.g., us, uk, de)"
echo " --keyboard-variant <variant> Keyboard variant (e.g., intl)"
echo " --locale <locale> Locale (e.g., en_US.UTF-8, de_DE.UTF-8)"
echo " --ssh-keys <file> Path to file with public SSH keys (one per line, OpenSSH format)"
echo " --disk-size <size> Disk size (e.g., 32G, 50G, 6144M)"
echo " --disk-bus <type> Disk bus/controller type: scsi (default), virtio, sata, ide"
echo " --disk-storage <storage> Proxmox storage for VM disk (default: local-lvm)"
echo " --disk-format <format> Disk format: ex. qcow2 (default)"
echo " --disk-flags <flags> Space-separated Disk flags (default: discard=on)"
echo " --display <type> Set the display/vga type (default: std)"
echo " --packages <packages> Space-separated list of packages to install in the template using cloud-init"
echo " --dns-servers <servers> Space-separated DNS servers (e.g., '10.10.10.10 9.9.9.9')"
echo " --dns-domains <domains> Space-separated domain names (e.g., 'example.com internal.local')"
echo " --snippets-storage <storage> Proxmox storage for cloud-init snippets (default: same as --disk-storage)"
echo " --patches <patches> Space-separated list of patch names to apply (default: none)"
echo " --reboot Reboot the VM after cloud-init has completed"
echo " --onboot Start VM automatically when Proxmox host boots (default: disabled)"
echo " --vendor-only Write the final vendor-data file, print its absolute path, and exit before VM creation"
echo " --config <file|name> YAML config file path, or a template name resolved from templates/<name>.{yaml,yml}; CLI flags override config values"
echo " -v, --verbose Enable verbose mode"
echo " -h, --help Display this help message"
echo ""
echo "Supported distro families: ${SUPPORTED_DISTROS[*]}"
echo "RHEL-compatible images such as Rocky, CentOS, AlmaLinux, and RHEL are normalized to rhel"
}
load_patches() {
local script_dir
local patch_dir
local patch_file
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
patch_dir="${script_dir}/patches"
if [[ ! -d "${patch_dir}" ]]; then
die "Patch directory not found: ${patch_dir}"
fi
shopt -s nullglob
for patch_file in "${patch_dir}"/*.sh; do
# shellcheck disable=SC1090
source "${patch_file}"
done
shopt -u nullglob
}
build_args_from_config() {
local config_file="${1}"
local -n _out_args="${2}"
_resolve_config_path() {
local raw_path="${1}"
local expanded_path
case "${raw_path}" in
"~")
expanded_path="${HOME}"
;;
\~/*)
expanded_path="${HOME}/${raw_path:2}"
;;
/*)
expanded_path="${raw_path}"
;;
*)
expanded_path="${PWD}/${raw_path}"
;;
esac
realpath -m "${expanded_path}"
}
# Helper: read a yq path and append CLI argument based on type (string, bool, or list)
_cfg_read() {
local yq_path="${1}" flag="${2}" type="${3}" output exit_code read_type read_value resolved_value
echo "Reading config key '${yq_path}' for flag '${flag}'..."
# Helper to safely execute yq and capture exit code
_safe_yq() {
local yq_expr="${1}"
set +e
output=$(yq -r "${yq_expr}" "${config_file}" 2>&1)
exit_code=$?
set -e
}
# Read key once: emit either __MISSING__ or "<type>\t<value>".
# For arrays, value is space-joined to match CLI list argument format.
_safe_yq "if (${yq_path} | type) == \"null\" then \"__MISSING__\" else ((${yq_path} | type) + \"\\t\" + (if (${yq_path} | type) == \"array\" then (${yq_path} | join(\" \") ) elif (${yq_path} | type) == \"boolean\" then (if ${yq_path} then \"true\" else \"false\" end) else (${yq_path} | tostring) end)) end"
if [[ ${exit_code} -ne 0 ]]; then
die "Failed to parse config at '${yq_path}': $output"
fi
if [[ "${output}" == "__MISSING__" ]]; then
echo " Key '${yq_path}' not found, skipping"
return 0
fi
# Split "type<TAB>value".
read_type="${output%%$'\t'*}"
if [[ "${output}" == *$'\t'* ]]; then
read_value="${output#*$'\t'}"
else
read_value=""
fi
# Skip if value is empty.
if [[ -z "${read_value}" ]]; then
echo " Key '${yq_path}' has empty value, skipping"
return 0
fi
case "${type}" in
string)
if [[ "${read_type}" != "string" ]]; then
die "Invalid config key '${yq_path}': expected string but got '${read_type}'"
fi
echo " Setting ${flag}=${read_value}"
_out_args+=("${flag}" "${read_value}")
;;
path)
if [[ "${read_type}" != "string" ]]; then
die "Invalid config key '${yq_path}': expected string path but got '${read_type}'"
fi
resolved_value="$(_resolve_config_path "${read_value}")"
echo " Setting ${flag}=${resolved_value}"
_out_args+=("${flag}" "${resolved_value}")
;;
number)
if [[ "${read_type}" != "number" ]]; then
die "Invalid config key '${yq_path}': expected number but got '${read_type}'"
fi
echo " Setting ${flag}=${read_value}"
_out_args+=("${flag}" "${read_value}")
;;
bool)
if [[ "${read_type}" != "boolean" ]]; then
die "Invalid config key '${yq_path}': expected boolean (true/false) but got '${read_type}'"
fi
if [[ "${read_value}" == "true" ]]; then
echo " Setting ${flag}"
_out_args+=("${flag}")
elif [[ "${read_value}" == "false" ]]; then
echo " Key '${yq_path}' is false, skipping"
else
die "Invalid config key '${yq_path}': expected boolean value true/false but got '${read_value}'"
fi
;;
list)
if [[ "${read_type}" != "array" ]]; then
die "Invalid config key '${yq_path}': expected YAML list (array) but got '${read_type}'"
fi
echo " Setting ${flag}=${read_value}"
_out_args+=("${flag}" "${read_value}")
;;
esac
}
# Required options:
_cfg_read '.url' "--url" string
_cfg_read '.id' "--id" number
_cfg_read '.name' "--name" string
# top-level VM hardware:
_cfg_read '.memory' "--memory" number
_cfg_read '.cores' "--cores" number
_cfg_read '.cpu' "--cpu" string
_cfg_read '.display' "--display" string
# disk:
_cfg_read '.disk.storage' "--disk-storage" string
_cfg_read '.disk.size' "--disk-size" string
_cfg_read '.disk.bus' "--disk-bus" string
_cfg_read '.disk.format' "--disk-format" string
_cfg_read '.disk.scsihw' "--disk-scsihw" string
_cfg_read '.disk.flags' "--disk-flags" list
# snippets:
_cfg_read '.snippets.storage' "--snippets-storage" string
# cloud-init:
_cfg_read '.user' "--user" string
_cfg_read '.password' "--password" string
_cfg_read '.upgrade' "--upgrade" bool
_cfg_read '.reboot' "--reboot" bool
_cfg_read '.onboot' "--onboot" bool
# packages:
_cfg_read '.packages' "--packages" list
# localization-related keys:
_cfg_read '.timezone' "--timezone" string
_cfg_read '.keyboard.layout' "--keyboard-layout" string
_cfg_read '.keyboard.variant' "--keyboard-variant" string
_cfg_read '.locale' "--locale" string
# network:
_cfg_read '.net.bridge' "--net-bridge" string
_cfg_read '.net.vlan' "--net-vlan" number
# dns:
_cfg_read '.dns.servers' "--dns-servers" list
_cfg_read '.dns.domains' "--dns-domains" list
# ssh:
_cfg_read '.ssh.keys' "--ssh-keys" path
# Top-level misc:
_cfg_read '.patches' "--patches" list
}
normalize_distro() {
case "${1}" in
debian | ubuntu | fedora)