Skip to content

Instantly share code, notes, and snippets.

@arevindh
Created April 14, 2024 16:11
Show Gist options
  • Save arevindh/1d1211f3f3847eaf715343193dc7baf8 to your computer and use it in GitHub Desktop.
Save arevindh/1d1211f3f3847eaf715343193dc7baf8 to your computer and use it in GitHub Desktop.
Ubuntu cloudinit image generator proxmox
#!/bin/bash
##############################################################################
# things to double-check:
# 1. user directory
# 2. your SSH key location
# 3. which bridge you assign with the create line (currently set to vmbr100)
# 4. which storage is being utilized (script uses local-lvm)
##############################################################################
DISK_IMAGE="jammy-server-cloudimg-amd64.img"
IMAGE_URL="https://cloud-images.ubuntu.com/jammy/current/$DISK_IMAGE"
# Function to check if a file was modified in the last 24 hours or doesn't exist
should_download_image() {
local file="$1"
# If file doesn't exist, return true (i.e., should download)
[[ ! -f "$file" ]] && return 0
local current_time=$(date +%s)
local file_mod_time=$(stat --format="%Y" "$file")
local difference=$(( current_time - file_mod_time ))
# If older than 24 hours, return true
(( difference >= 86400 ))
}
# Download the disk image if it doesn't exist or if it was modified more than 24 hours ago
if should_download_image "$DISK_IMAGE"; then
rm -f "$DISK_IMAGE"
wget -q "$IMAGE_URL"
fi
if qm list | grep -qw "9022"; then
qm destroy 9022
fi
qm create 9022 --name "ubuntu-2204-cloudinit" --memory 2048 --cores 2 --net0 virtio,bridge=vmbr100
qm importdisk 9022 "$DISK_IMAGE" local-lvm
qm set 9022 --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-9022-disk-0
qm set 9022 --boot c --bootdisk scsi0
qm set 9022 --ide2 local-lvm:cloudinit
qm set 9022 --serial0 socket --vga serial0
qm set 9022 --agent enabled=1
qm set 9022 --sshkey /root/id_rsa.pub
qm set 9022 --ipconfig0 ip=dhcp
qm set 9022 --ciuser sid
qm disk resize 9022 scsi0 3G
qm template 9022
echo "Next up, clone VM, then expand the disk"
echo "You also still need to copy ssh keys to the newly cloned VM"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment