Skip to content

Instantly share code, notes, and snippets.

@cernoel
Created September 23, 2024 13:50
Show Gist options
  • Save cernoel/a9b31e42eef0bb6189aa5d792aa26047 to your computer and use it in GitHub Desktop.
Save cernoel/a9b31e42eef0bb6189aa5d792aa26047 to your computer and use it in GitHub Desktop.
This is just a starter cloud-init template generator to begin with for proxmox.
#!/bin/bash
# get new image if file does not exist
if [[ ! -f debian-12-genericcloud-amd64.qcow2 ]]; then
#rm debian-12-genericcloud-amd64.qcow2
wget https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2
#sha check
fi;
# create vendor-cloudinit-template
cat <<EOT > /var/lib/vz/snippets/debian12-cloudinit-vendor.yaml
#cloud-config
package_update: true
package_upgrade: true
disable_root: true
manage_etc_hosts: true
timezone: Europe/Vienna
package_reboot_if_required: true
# reboots after final setup
autoinstall:
shutdown: reboot
runcmd:
- apt update -y && apt install -y qemu-guest-agent apt-transport-https ca-certificates curl gnupg-agent software-properties-common sudo vim
- systemctl start qemu-guest-agent
- curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
- add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
- apt update -y && apt install -y docker-ce docker-ce-cli containerd.io
- systemctl enable docker && systemctl enable docker
- fallocate -l 1G /swapfile
- chmod 600 /swapfile
- mkswap /swapfile
- swapon /swapfile
- echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
- (echo "reboot in 30 sec ..." && sleep 30 && reboot) &
EOT
# Set the VM ID to operate on
VMID=9000
# Choose a name for the VM
TEMPLATE_NAME=debian-12-cloud-init
# Choose the disk image to import
DISKIMAGE=debian-12-genericcloud-amd64.qcow2
# Select Host disk
HOST_DISK=nvmedisk1
# disk bus/deviceid
DISK_BUS_DEVICE=scsi0
qm destroy $VMID
# Create the VM
qm create $VMID --name $TEMPLATE_NAME --net0 virtio,bridge=vmbr100
# Set the OSType to Linux Kernel 6.x
qm set $VMID --ostype l26
# Import the disk
qm importdisk $VMID $DISKIMAGE $HOST_DISK
# Attach disk to scsi bus
qm set $VMID --scsihw virtio-scsi-single --$DISK_BUS_DEVICE file=$HOST_DISK:vm-$VMID-disk-0,aio=threads,iothread=1
# Set scsi disk as boot device
qm set $VMID --boot c --bootdisk $DISK_BUS_DEVICE
# resize disk a bit, otherwise setup fails
qm resize $VMID $DISK_BUS_DEVICE +3G
# Create and attach cloudinit drive
qm set $VMID --ide2 $HOST_DISK:cloudinit
# Set serial console, which is needed by OpenStack/Proxmox
qm set $VMID --serial0 socket --vga serial0
# Enable Qemu Guest Agent
qm set $VMID --agent enabled=1 # optional but recommened
# set 1GB of RAM
qm set $VMID --memory 1024
#set one core
qm set $VMID --cores 1
# set interface dhcp
qm set $VMID --ipconfig0 ip=dhcp
# set sshuser
qm set $VMID --ciuser nerox
# set local root ssh_key as key for login / can be file with multiple keys
qm set $VMID --sshkeys $(pwd)/ssh_pubkeys
# add custom vendor cloud init config to image (this only fires at first boot, dont use user)
qm set $VMID --cicustom "vendor=local:snippets/debian12-cloudinit-vendor.yaml"
# convert to template
qm template $VMID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment