Skip to content

Instantly share code, notes, and snippets.

@RogerSik
Created February 24, 2023 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RogerSik/f02e9e5cb8366ab30dfc681feeb9e45e to your computer and use it in GitHub Desktop.
Save RogerSik/f02e9e5cb8366ab30dfc681feeb9e45e to your computer and use it in GitHub Desktop.
ubuntu-proxmox-template-creation.sh
#!/bin/bash
VM_ID=200
RELEASE=jammy
PROJECT_PATH=/opt/homelab/ubuntu-template/
# cleanup before start
rm -f $PROJECT_PATH/jammy-server-cloudimg-amd64.img
qm destroy $VM_ID
# image download
wget https://cloud-images.ubuntu.com/$RELEASE/current/$RELEASE-server-cloudimg-amd64.img
apt install libguestfs-tools -y
# copy file so that we can preserve "last modified date" for template name
cp $RELEASE-server-cloudimg-amd64.img $RELEASE-server-cloudimg-amd64-modified.img
# Install qemu-guest-agent on Ubuntu image
virt-customize -a $PROJECT_PATH/$RELEASE-server-cloudimg-amd64-modified.img --install qemu-guest-agent
# Create Proxmox VM image from Ubuntu Cloud Image
qm create $VM_ID --memory 1024 --balloon 0 --cores 2 --net0 virtio,bridge=vmbr0 --scsihw virtio-scsi-pci
qm set $VM_ID --scsi0 local-lvm:0,import-from=$PROJECT_PATH/$RELEASE-server-cloudimg-amd64-modified.img
qm set $VM_ID --agent enabled=1,fstrim_cloned_disks=1
VM_NAME="ubuntu-$RELEASE-$(date -r $PROJECT_PATH/$RELEASE-server-cloudimg-amd64.img "+%Y-%m-%d")"
qm set $VM_ID --name $VM_NAME
qm resize $VM_ID scsi0 +3G
# Create Cloud-Init Disk and configure boot.
qm set $VM_ID --ide2 local-lvm:cloudinit
qm set $VM_ID --boot order=scsi0
# Configure cloud init
qm set $VM_ID --onboot 1
qm set $VM_ID --ciuser "devops"
qm set $VM_ID --sshkeys /opt/homelab/ubuntu-template/ssh-keys.pub
qm set $VM_ID --ipconfig0 "ip=dhcp,ip6=dhcp"
qm template $VM_ID
# Cleanup after the work
rm -f $PROJECT_PATH/jammy-server-cloudimg-amd64.img
rm -f $PROJECT_PATH/jammy-server-cloudimg-amd64-modified.img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment