Creating a VM Template with cloud-init in Proxmox
Greetings!

In this note, we will import a previously prepared OS image in qcow2 format into Proxmox as a VM template using QEMU console commands.

All examples from the article were performed in the Proxmox 9 hypervisor environment (Debian 13).

1️⃣ Connect to the Proxmox server via SSH:

BASH
ssh root@proxmox.home.lan
Click to expand and view more

2️⃣ Resize the disk (specify a suitable value for your case):

BASH
qemu-img resize ./debian-13-generic-amd64.qcow2 20G
Click to expand and view more

3️⃣ Create a new virtual machine with any name, for example debian13-k8s-template, an ID, for example 7777, and basic settings (adjust them for your environment):

BASH
qm create 7777 --name "debian13-k8s-template" \
    --memory 2048 --cores 2 --net0 virtio,bridge=vmbr0
Click to expand and view more

4️⃣ Import the debian-13-generic-amd64.qcow2 disk image into the storage storage (replace it with your own) and attach it to the VM with ID 7777:

BASH
qm importdisk 7777 ./debian-13-generic-amd64.qcow2 storage --format qcow2
Click to expand and view more

5️⃣ Set the SCSI controller and add the imported disk as scsi0:

BASH
qm set 7777 --scsihw virtio-scsi-single \
    --scsi0 storage:7777/vm-7777-disk-0.qcow2
Click to expand and view more

6️⃣ Set the boot order, specifying scsi0 as the first boot disk:

BASH
qm set 7777 --boot order=scsi0
Click to expand and view more

7️⃣ Add a cloud-init disk to the virtual machine as ide0 (for automatic configuration on first boot):

BASH
qm set 7777 --ide0 storage:cloudinit
Click to expand and view more

8️⃣ Configure the user, password, and SSH for cloud-init:

BASH
# User creation
qm set 7777 --ciuser ivan
qm set 7777 --cipassword $(openssl passwd -6)

# IP configuration
qm set 7777 --ipconfig0 ip=dhcp
# or static
qm set 7777 --ipconfig0 ip=192.168.1.100/24,gw=192.168.1.1

# Adding an SSH key
qm set 7777 --sshkeys ~/.ssh/id_ed25519.pub
Click to expand and view more

9️⃣ Configure the serial port for the console and the video adapter linked to it:

BASH
qm set 7777 --serial0 socket --vga serial0
Click to expand and view more

🔟 Enable QEMU Guest Agent for interaction between the host and the guest system:

BASH
qm set 7777 --agent enabled=1
Click to expand and view more

1️⃣1️⃣ Convert the virtual machine into a template (for cloning and quick deployment):

BASH
qm template 7777
Click to expand and view more

1️⃣2️⃣ Rescan the virtual machine configuration (may be useful after making changes):

BASH
qm rescan --vmid 7777
Click to expand and view more

1️⃣3️⃣ View the full configuration of our virtual machine template:

BASH
qm config 7777
Click to expand and view more

1️⃣4️⃣ Check the list of files in the VM directory (storage storage), in my case this is: /mnt/storage/images/7777/:

BASH
ls -l /mnt/storage/images/7777/
Click to expand and view more

We can see the image itself and the cloud-init disk👌.

1️⃣5️⃣ Now calmly clone your template into a new virtual machine in Proxmox:

1️⃣6️⃣ After startup, check access to the new VM console by password:

1️⃣7️⃣ Check SSH key connection:

Everything works!

In the next article, we will automate infrastructure deployment in Proxmox using Terraform/OpenTofu. Do not miss it👨‍💻.

Thank you for reading! Good luck😉.

Copyright Notice

Author: Иван Чёрный

Link: https://r4ven.me/en/virtualization/sozdanie-shablona-vm-s-cloud-init-v-proxmox/

License: CC BY-NC-SA 4.0

Использование материалов блога разрешается при условии: указания авторства/источника, некоммерческого использования и сохранения лицензии.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut