Create template to create VM's within couple of clicks

So first thing first, why we need templates? Well, there are couple of reasons:

  1. for speed, you can create new VM's within couple of clicks
  2. for consistency, you can be sure that all your VM's will have same settings
  3. for automation, you can automate creation of new VM's with Terraform

Ok, now when we know why we need templates let's create one, but first we need ISO image. You can download whatever you want, but I will use Ubuntu 20.04 LTS as it's the one I'm using the most. You can download it from Ubuntu-cloud-images Go to terminal (you can use Gui shell for this) and download it with wget:

wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img

Next, lest create new VM. In shell run:

qm create 7777 --name ubuntu-cloud-focal --memory 2048 --net0 virtio,bridge=vmbr0

Quick explanation of what we just did:

  1. qm - command to manage VM's more info
  2. create 7777 - create new VM with ID 7777, high ID is for easier identification
  3. --name ubuntu-cloud-20.04 - name of our VM
  4. --memory 2048 - amount of RAM in MB (it can be modified later for each VM)
  5. --net0 virtio,bridge=vmbr0 - network interface, we will use virtio and bridge vmbr0

Next step is to import our ISO image to our VM. To do that run:

qm importdisk 7777 focal-server-cloudimg-amd64.img local-lvm

Quick explanation of what we just did: qm importdisk 7777 - import disk to VM with ID 7777 focal-server-cloudimg-amd64.img - path to our ISO image

Next step is to attach our ISO image to our VM. To do that run:

qm set 7777 --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-7777-disk-0

Quick explanation: qm set 7777 - set options for VM with ID 7777 --scsihw virtio-scsi-pci - set SCSI controller to virtio-scsi-pci --scsi0 local-lvm:vm-7777-disk-0 - attach disk to SCSI controller

Next, let's configure cloud-init. To do that run:

qm set 7777 --ide2 local-lvm:cloudinit

Quick explanation: qm set 7777 - set options for VM with ID 7777 --ide2 local-lvm:cloudinit - attach cloud-init to IDE controller

Next, let's configure boot order. To do that run:

qm set 7777 --boot c --bootdisk scsi0

Quick explanation: qm set 7777 - set options for VM with ID 7777 --boot c - boot from disk --bootdisk scsi0 - set boot disk to scsi0

Next, let's configure serial console. To do that run:

qm set 7777 --serial0 socket --vga serial0

Quick explanation: qm set 7777 - set options for VM with ID 7777 --serial0 socket - set serial0 to socket --vga serial0 - set vga to serial0

Great, let's go back to our GUI and configure Cloud-Init tab. First, let's set User and Password. Go to Cloud-Init tab and set User and Password. Remember that this will be your default user and password for your VM. Next, let's set SSH Public Key. You can generate new key pair with ssh-keygen command. After you generate your key pair you can copy your public key to SSH Public Key field.

Last thing is to set IP Config. You can set it to Static or DHCP. I will set it to DHCP as I will use DHCP server to assign IP address to my VM's.

Now, let's convert our VM to template. To do that click right mouse button on your VM and click Convert to template. And that's it, now you can create new VM's based on your template.

How to create new VM based on template?

To create new VM based on template click right mouse button on your template and click Clone. Set here a name, ID and select Full Clone. If you'll use Linked Clone you will save some space, but you will not be able to delete your template. And that's it, now you can start your VM and connect to it via SSH. Cheers!