Skip to content

Instantly share code, notes, and snippets.

@si458
Last active September 19, 2024 19:17
Show Gist options
  • Save si458/98aa940837784e9ef9bff9e24a7a8bfd to your computer and use it in GitHub Desktop.
Save si458/98aa940837784e9ef9bff9e24a7a8bfd to your computer and use it in GitHub Desktop.
virt-customize ubuntu22
#!/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 9000 --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 9000 --name "ubuntu-2204-template" --memory 4096 --cores 2 --net0 virtio,bridge=vmbr0,firewall=1 --bios ovmf --agent enabled=1 --efidisk0 local-zfs:9000,efitype=4m --ostype l26 --serial0 socket --vga serial0 --machine q35 --scsi1 local-zfs:cloudinit --scsihw virtio-scsi-pci
# import image to VM
qm importdisk 9000 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 9000
# remove new template
rm -rfv ubuntu-22.04-server-cloudimg-amd64-disk-kvm.img
@caiocampoos
Copy link

now that is a thing of beauty.
Thanks

@maxfield-allison
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment