Skip to content

Instantly share code, notes, and snippets.

@leafonthewind
Created July 19, 2024 20:41
Show Gist options
  • Save leafonthewind/de7803221a227784964395de8c4af42a to your computer and use it in GitHub Desktop.
Save leafonthewind/de7803221a227784964395de8c4af42a to your computer and use it in GitHub Desktop.
Proxmox Cloud Init Debian

Get the cloud-init image.

wget https://cdimage.debian.org/cdimage/cloud/bookworm/latest/debian-12-generic-amd64.qcow2

The downloaded image does not include quemu-guest-agent which I want to have in order for Proxmox to show details about the VM.

SSH into the Proxmox node and run:

apt update
apt install -y libguestfs-tools

This needs to only be done once for the life of the pve host.

Then for the cloud image run:

virt-customize --install qemu-guest-agent -a debian-12-generic-amd64.qcow2

This will install libguestfs-tools and install quemu-guest-agent into the image we downloaded earlier. The same process can be used to add other packages into the image.

Create the template:

qm create 9001 --name debian12-template --memory 1024 --net0 virtio,bridge=vmbr0

This will create a new VM with ID 9001, the name debian12-template, 1024Mb of RAM and a network bridge set to vmbr0 which is the usually the default one created by Proxmox.

qm importdisk 9001 debian-12-generic-amd64.qcow2 local-zfs

Next we import the image we downloaded earlier as a disk for the VM with ID 9001 that we just created. local-zfs is the name of the storage in Proxmox where the disk should be stored.

qm set 9001 --scsihw virtio-scsi-pci --scsi0 local-zfs:vm-9001-disk-0
qm set 9001 --ide2 local-zfs:cloudinit

With the first command, we set the disk for the VM to be the disk we just imported in the previous step vm-9001-disk-0 is the name of the disk that was generated after the import.

The second command creates the cloud-init CD-ROM drive which activates the cloud-init options for the VM.

qm set 9001 --boot c --bootdisk scsi0
qm set 9001 --serial0 socket --vga serial0
qm set 9001 --ipconfig0 ip=dhcp

First we set the boot order and specify the first boot disk to be used to be scsi0 which is the disk with the cloud image, then we configure a serial console to use as display otherwise we won't see anything in the "Console" view in Proxmox.

The last command will set the networking stack config to use DHCP in order for the VM to obtain an IP address.

qm set 9001 --agent enabled=1

Enables guest agent so Proxmox UI can give more info about the VM when running.

qm resize 9001 scsi0 15G
qm template 9001

These are the final steps, resize the disk, otherwise it will be the size of the cloud image we downloaded, in here I make it a 15GB disk so it has enough storage to install some packages, then we tell Proxmox to make VM 9001 a template.

Finally, cleanup:

rm debian-12-generic-amd64.qcow2
# This works for me using Proxmox v7.* and telmate/proxmox v2.9.14
resource "proxmox_vm_qemu" "sample-server" {
target_node = "pve"
name = "server-host-name"
desc = "Description of this server."
os_type = "cloud-init"
full_clone = true
clone = "debian12-template"
memory = 2048
sockets = 1
cores = 2
ssh_user = "matt"
ciuser = "matt"
ipconfig0 = "ip=10.0.0.100/24,gw=10.0.0.1"
nameserver = "10.0.0.1 8.8.8.8"
automatic_reboot = true
onboot = true
disk {
storage = "vmdata"
type = "scsi"
size = "15G"
discard = "on"
}
network {
bridge = "vmbr0"
model = "virtio"
mtu = 0
queues = 0
rate = 0
}
# sshkeys set using variables. the variable contains the text of the key.
sshkeys = <<EOF
ssh-ed25519 ...
EOF
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment