✨ (cloud-init.sh): dynamically generate VMID and IP based on the last octet of the host machine's IP address
🐛 (cloud-init.sh): fix IP address assignment to use CIDR notation 🐛 (cloud-init.sh): add validation for IP last octet to ensure it is between 1 and 254
This commit is contained in:
parent
5db6405b19
commit
0520839925
@ -1,6 +1,21 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -x
|
set -x
|
||||||
|
|
||||||
|
# Get IP address of the local machine and extract the last octet
|
||||||
|
LOCAL_IP=$(ip -4 addr show vmbr0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -1)
|
||||||
|
if [ -z "$LOCAL_IP" ]; then
|
||||||
|
echo "Error: Could not detect IP address on vmbr0 interface"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
IP_LAST_OCTET=$(echo "$LOCAL_IP" | cut -d. -f4)
|
||||||
|
|
||||||
|
# Validate IP last octet
|
||||||
|
if ! [[ $IP_LAST_OCTET =~ ^[0-9]+$ ]] || [ $IP_LAST_OCTET -lt 1 ] || [ $IP_LAST_OCTET -gt 254 ]; then
|
||||||
|
echo "Error: Detected IP last octet ($IP_LAST_OCTET) is invalid. Must be between 1 and 254"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# Define the Debian version (e.g., 11, 12, 13)
|
# Define the Debian version (e.g., 11, 12, 13)
|
||||||
DEBIAN_VERSION="12"
|
DEBIAN_VERSION="12"
|
||||||
|
|
||||||
@ -65,8 +80,9 @@ SSH_KEY_PATH="id_rsa.pub"
|
|||||||
virt-customize -v -x --install qemu-guest-agent -a $IMG_NAME --root-password password:coolpass --ssh-inject root:file:$SSH_KEY_PATH |& tee qemu.log
|
virt-customize -v -x --install qemu-guest-agent -a $IMG_NAME --root-password password:coolpass --ssh-inject root:file:$SSH_KEY_PATH |& tee qemu.log
|
||||||
|
|
||||||
# Define VM parameters
|
# Define VM parameters
|
||||||
|
IP_ADDRESS="10.10.0.${IP_LAST_OCTET}"
|
||||||
|
VMID="90${IP_LAST_OCTET}" # VMID will be 9050 for IP 10.10.0.50
|
||||||
TEMPL_NAME="debian-${DEBIAN_VERSION}-generic-$(date +%Y%m%d)"
|
TEMPL_NAME="debian-${DEBIAN_VERSION}-generic-$(date +%Y%m%d)"
|
||||||
VMID="9004"
|
|
||||||
MEM="2048"
|
MEM="2048"
|
||||||
DISK_SIZE="64G"
|
DISK_SIZE="64G"
|
||||||
DISK_STOR="local-lvm"
|
DISK_STOR="local-lvm"
|
||||||
@ -80,7 +96,7 @@ qm set $VMID --scsihw virtio-scsi-pci --scsi0 $DISK_STOR:vm-$VMID-disk-0
|
|||||||
qm set $VMID --ide2 $DISK_STOR:cloudinit
|
qm set $VMID --ide2 $DISK_STOR:cloudinit
|
||||||
qm set $VMID --boot c --bootdisk scsi0
|
qm set $VMID --boot c --bootdisk scsi0
|
||||||
qm set $VMID --serial0 socket --vga serial0
|
qm set $VMID --serial0 socket --vga serial0
|
||||||
qm set $VMID --ipconfig0 ip=dhcp,gw=10.10.0.1
|
qm set $VMID --ipconfig0 ip=${IP_ADDRESS}/24,gw=10.10.0.1
|
||||||
qm resize $VMID scsi0 $DISK_SIZE
|
qm resize $VMID scsi0 $DISK_SIZE
|
||||||
qm template $VMID
|
qm template $VMID
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user