Skip to content

Instantly share code, notes, and snippets.

@caiocampoos
Last active June 16, 2024 18:59
Show Gist options
  • Save caiocampoos/96ecf3dc3034c96a57e4ac735346a412 to your computer and use it in GitHub Desktop.
Save caiocampoos/96ecf3dc3034c96a57e4ac735346a412 to your computer and use it in GitHub Desktop.
script to create a vm template in proxmox with cloud init
#!/bin/sh
# install tools
apt update -y && apt install nano wget curl libguestfs-tools -y
# remove old image
rm -rfv ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img
# remove old template container - WILL DESTROY COMPLETELY
qm destroy 999 --destroy-unreferenced-disks 1 --purge 1
# download new image
wget http://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img
# add agent to image
virt-customize -a ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img --install qemu-guest-agent
# set timezone
virt-customize -a ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img --timezone Europe/London
# set password auth to yes
virt-customize -a ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img --run-command 'sed -i s/^PasswordAuthentication.*/PasswordAuthentication\ yes/ /etc/ssh/sshd_config'
# allow root login with ssh-key only
virt-customize -a ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img --run-command 'sed -i s/^#PermitRootLogin.*/PermitRootLogin\ prohibit-password/ /etc/ssh/sshd_config'
# increase image by 5.82gb (original is 2.2GB, not sure why it needs an 20mb extra tho?)
qemu-img resize ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img +5G
qemu-img resize ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img +820M
# create VM
qm create 999 --name "ubuntu-2204-template" --memory 4096 --cores 2 --net0 virtio,bridge=vmbr0,firewall=1 --bios ovmf --agent enabled=1 --efidisk0 local-zfs:999,efitype=4m --ostype l26 --serial0 socket --vga serial0 --machine q35 --scsi1 local-zfs:cloudinit --scsihw virtio-scsi-pci
# import image to VM
qm importdisk 999 ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img local-zfs
# add disk to VM
qm set 9000 --scsi0 local-zfs:vm-9000-disk-1
# set bootdisk to image
qm set 9000 --boot c --bootdisk scsi0
# convert to template
qm template 999
# remove new template
rm -rfv ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment