- Kubernetes CoreDNS - Mon, Dec 4 2023
- Update container images with Copa - Mon, Nov 27 2023
- Deploying stateful applications with Kubernetes StatefulSets - Wed, Nov 1 2023
Prepare the VM
To create a template for a Windows VM, you can run Sysprep and then directly skip to the next step. If you already have an Ubuntu VM to convert into a template, you don't need to follow all the steps mentioned in this section. Instead, you just need to remove machine-specific settings, such as machine ID, SSH host keys, and IP addresses, with the following commands:
# truncate the machine-id file sudo truncate -s 0 /etc/machine-id # remove ssh host keys sudo rm -f /etc/ssh/ssh_host_* # remove ip addresses; change enp0s3 to your own interface name sudo ip addr flush dev enp0s3
It is worth noting that you should not delete the /etc/machine-id file but truncate it (remove its content) only, as shown in the first command above. Deleting this file could cause unexpected errors in your VM. Shut down your VM after running these commands, and go directly to the next section.
For demonstration purposes, I will set up a new VM with an Ubuntu 22.04 cloud image. Cloud images have the cloud-init package installed, so I will use it to avoid the hassle of installing an operating system manually. For those who don't know, cloud-init is a cloud instance initialization package, which provides a way of storing various settings, such as hostname, locale, ssh keys, and other user data, in a configuration file. When your VM boots for the first time, cloud-init automatically applies these settings to configure the VM for you the way you like.
- I will first create a VM in Proxmox with slightly unusual settings, as shown in the screenshots below:
You can see in the screenshots that I did not choose any ISO media and removed the default disk so the VM doesn't have any disk at the start.
- We will now download the cloud image on our Proxmox server. The Proxmox GUI, unfortunately, doesn't have any option to download and move the image to the right place, so we need to do that on the CLI. Switch to the Proxmox server terminal and run the following command:
wget https://cloud-images.ubuntu.com/minimal/releases/jammy/release/ubuntu-22.04-minimal-cloudimg-amd64.img -O /tmp/ubuntu-22.04.img
This command downloads the minimal cloud image with Ubuntu 22.04 into the /tmp directory on the Proxmox host.
- Now, we need to import this image to the Proxmox storage. If you're using block-based storage (e.g., lvm or ceph), the disk will be imported as raw. If you're using directory-based storage (e.g., local or NFS), it is a good idea to import the image with the qcow2 format to utilize the live snapshot feature. To view all the storage, use the pvesm status command, as shown below:
You can see in the screenshot that I have both types of storage available in Proxmox. The local storage is the directory type, and the lvm-datastore is the lvmthin (block) type.
- To import the cloud image into the lvm-datastore, use the following command:
qm importdisk 999 /tmp/ubuntu-22.04.img lvm-datastore
Make sure you replace 999 with the VM ID of your own VM.
The success message at the end shows that the image was imported as an unused disk in the lvm-datastore, so we need to manually attach it later in the VM settings.
- To view the imported disk through CLI, you can run pvesm list lvm-datastore, as shown in the screenshot below:
The format column shows that the disk format is raw, which was expected since we imported it to block storage. To import the image to a directory type storage (e.g., local), you need to run the following command instead:
qm importdisk 999 /tmp/ubuntu-22.04.img local --format qcow2
The --format qcow2 option ensures that the image is imported in the qcow2 format. That is all we need to do in the Proxmox terminal. Let's now switch back to the GUI to make further changes to the VM.
- In the Proxmox GUI, select the VM, click the Hardware tab, and then double-click Unused Disk.
- Now choose the disk image that you imported earlier. If you're using an SSD, you can also enable SSD emulation under the Advanced options. Finally, click the Add button. The disk is attached to the VM.
- Add a Serial Port and a CloudInit drive to the VM by clicking the Add button on the Hardware tab. See the following screenshot for reference:
We are adding a serial port because it is used by most cloud-init images for display. Without this, you will not see a screen when the VM is started. The CloudInit Drive lets you define various settings, such as username, password, IP config, SSH keys, etc.
- After adding the serial port, double-click the Display option, set Serial terminal 0 as the graphics card, and then click OK.
If you still don't see any display when the VM boots, you might need to revert this setting to Default.
- The disk we imported and attached is not automatically added to the boot order. So, click the Options tab, double-click the Boot Order option, then add the disk to the boot order, move it to the top, and click OK.
You may also want to enable the Start at boot option to make sure the VMs cloned from this template are automatically started when the Proxmox server is started.
- Finally, click the Cloud-Init tab, configure a username, password, DNS server, SSH public key, and IP address, and click the Regenerate Image button when you're done. Setting a username and password is essential here; otherwise, you won't be able to log in when a VM is cloned from this template.
That's it. Your VM is now ready to be converted into a template.
Create a VM template
To create a template, right-click your VM and select the Convert to template option, as shown in the screenshot below:
You will see a confirmation prompt. Note that you cannot undo this process, so before confirming this operation, make sure you have followed all the steps.
Click Yes to proceed, and the VM will be converted to a template within a few seconds. You will notice the VM icon change a little bit to indicate that it is now a VM template instead of a regular VM.
Deploy a VM from template
Now that we have our first VM template ready, let's quickly create a VM using it. To do so, right-click the template and select the Clone option.
You will now see a bunch of options for the new VM that you are deploying from the template, as shown in the image below:
Select the required options, click Clone, and you are done. The most important option that I want to point out is Mode. When cloning a VM template, you get two choices: Full clone and Linked clone. A full clone, as the name suggests, is a fully independent copy of the VM template, which uses more storage than a linked clone. A linked clone requires access to the original VM template as it uses all its unchanged files. This saves a lot of disk space. Just keep in mind that a linked clone VM cannot be started without the original template.
Once your new VM is created, you can start it and log in with the username/password you defined in the CloudInit section (or with your previously defined credentials if you used an existing VM). The following screenshot shows that a new machine ID is automatically generated for our new VM, and thanks to cloud-init, my SSH public key is also copied to the .ssh\authorized_keys file.
Subscribe to 4sysops newsletter!
You can now configure a static IP (or use an auto-assigned IP) to connect the new VM over SSH using public key authentication.