Cloud-init enabled Rocky-Linux 8 template for Proxmox to facilitate automatic instance deploy by Terraform

Cloud-init enabled Rocky-Linux 8 template for Proxmox to facilitate automatic instance deploy by Terraform

Create Proxmox Template (Rocky 8)

Prepare Proxmox node for Template creation process

Update/Upgrade all packages. And install libguestfs-tools package

sudo apt update
sudo apt upgrade -y

sudo apt install libguestfs-tools -y

Add current user to kvm group to be able to use the virt-edit command

sudo usermod -aG kvm <username>

Logout and login again and add an environment variable EDITOR which is used by virt-edit

export EDITOR=nano
printenv | grep EDITOR

Download and Prepare Rocky8 cloud image

Download Rocky8 cloud image

https://download.rockylinux.org/pub/rocky/8/images/x86_64/Rocky-8-GenericCloud-Base.latest.x86_64.qcow2 -O rocky8.qcow2

Customize cloud.cfg file for cloud-init enabling SSH PasswordAuthentication by adding a line

virt-edit -a rocky8.qcow2 /etc/cloud/cloud.cfg

ssh_pwauth: 1

Customize sshd_conf file for ssh configuration

virt-edit -a rocky8.qcow2 /etc/ssh/sshd_config

After nano opens up the sshd_config file, do the following changes, save the file and exit

  • Comment HostKey(s)
  • Comment SyslogFacility AUTHPRIV
  • Comment AuthorizedKeysFile
  • UnComment PubkeyAuthentication yes
  • Comment GSSAPIAuthentication yes
  • Comment GSSAPICleanupCredentials no
  • Comment AcceptEnv(s)
  • Add AcceptEnv LC_*

Customize SELINUX configuration file

virt-edit -a rocky8.qcow2 /etc/selinux/config

After nano opens up the config file, change SELINUX value to permissive, save the file and exit

SELINUX=permissive

Since Rocky-8's cloud-init adds /etc/resolv.conf files instead of replacing it. It need to be edited from the cloud image, before template creation

virt-edit -a rocky8.qcow2 /etc/resolv.conf

After nano opens up the resolv.conf file, change nameserver value to 172.16.4.1, save the file and exit

nameserver 172.16.4.1

Prepare Cloud image before starting to create a new VM from it

Setup Time-Zone, Run update for all packages, Install additional packages

virt-customize -a rocky8.qcow2 --timezone "Asia/Dhaka"
virt-customize -a rocky8.qcow2 --update
virt-customize -a rocky8.qcow2 --install qemu-guest-agent,nano,wget
virt-customize -a rocky8.qcow2 --run-command 'sudo systemctl enable qemu-guest-agent'

Set password for root

read -sp "Password for root: " pwd && virt-customize -a rocky8.qcow2 --root-password password:$pwd

Create Proxmox Template from prepared cloud image

Create a VM with minimum configuration

sudo qm create 8073 --name "rocky8-CI-Template" --memory 1024 --cores 1 --net0 virtio,bridge=vmbr0
sudo qm importdisk 8073 rocky8.qcow2 pve-rbd

Additional settings for the new VM

sudo qm set 8073 --scsihw virtio-scsi-pci --scsi0 pve-rbd:vm-8073-disk-0
sudo qm set 8073 --boot c --bootdisk scsi0
sudo qm set 8073 --ide2 pve-rbd:cloudinit
sudo qm set 8073 --agent 1
sudo qm set 8073 --sshkey .ssh/myKey.pub

Set cloud-init settings for the VM

sudo qm set 8073 --ciuser rocky8 --citype nocloud --nameserver 172.16.4.1 --ipconfig0 ip=dhcp --searchdomain cs.net
read -sp 'Password for centos: ' pwd && sudo qm set 8073 --cipassword $pwd

Convert the new VM to template

sudo qm template 8073