Virtual machine template is an awesome feature provided by Proxmox. This template can be used to quickly set up a pre-configured virtual machine, minimizing the time required for installation and setup. This is very handy when you want to set up a virtual machine that is similar to the other ones in your environment or to maintain consistency in your environment.
Virtual machine templates can be created in multiple ways, of which the most common ones are to create a template from an existing machine with all the necessary configurations or use a pre-build image such as cloud-init and apply necessary configurations to those images. In this blog, we shall be using cloud-init images to create our VM template, which I shall be following along in my other upcoming blogs to create multiple virtual machines for different use cases.
What is Cloud-init?
Cloud-init is a set of scripts and utilities that enable automatic configuration and customization of virtual machines when they are launched in various cloud environments such as Amazon Web Services TM (AWS) and Microsoft Azure. The benefits of using Cloud-init include:
- Automation: Easy and quick automated instance setup with minimal human intervention.
- Flexibility: Has a range of customization options to customize your images as per your need.
- Consistency: Helps ensure that each instance is correctly configured and is having consistent configurations across the instances.
Cloud-init is compatible with major Linux distros such as Ubuntu, Alpine, CentOS, Fedora, FreeBSD, OpenBSD, and many more. The complete list of compatible distros with Cloud-init can be found at https://cloudinit.readthedocs.io/en/latest/reference/distros.html
In this blog, I shall walk you through setting up a Ubuntu server using a Ubuntu Cloud image along with basic cloud-init configuration to create a template that can then be used later to set up multiple other Ubuntu servers.
Creating a Virtual Machine
To create a new virtual machine, login into your Proxmox web console, and then click on the “Create VM” button on the top right corner of the UI as shown in the figure below.
Add the VM details and select the OS details as shown in the below images. Since we shall be using Ubuntu Cloud image, we will not install anything via CD / DVD drive. Also, the ID in the first screen is given as higher as I want to have my templates listed at the end of the list in the UI. VM IDs are given starting at 100 till 999999999. I usually use a number in the 900s. Additionally, follow the steps as shown in the figures below. In the system configuration, ensure Qemu Agent is checked. Remove any storage disk that gets populated by default. Set up at least 2 cores in the CPU and a minimum of 1 GB (1024 MB) of RAM in the configuration. Rest everything can be left at default unless you want to customize this for your specific needs. Also, when a new VM is created, these parameters can be changed for those VMs. So, these can be kept at a bare minimum or the most used ones and then adjusted for individual ones.
Now verify all the configurations and ensure that ‘Start after created’ is NOT selected, and then click on Finish and give Proxmox a few seconds to set up our VM.
Adding a VGA port for display for your VM
VGA port is usually necessary to provide your display over the web UI or via different systems such as X11 or VNC. To add the VGA display port, type the following command once your VM is created and turned off.
root@pve:~# qm set 900 --serial0 socket --vga serial0
In the command above, we are using the ‘qm’ command to set the ‘serial0’ port as a VGA port on the VM with ID ‘900’. Ensure to cross-check your VM ID when running this command.
Downloading Ubuntu Cloud Image
An Ubuntu cloud image is a specialized version of the Ubuntu operating system designed for use in cloud computing environments. Cloud images are optimized to be lightweight, efficient, and easily deployable on virtualization platforms and cloud infrastructure, such as Amazon Web Services TM (AWS) and Microsoft Azure. Ubuntu Cloud image comes with pre-installed Ubuntu OS and minimizes the time to run the installation scripts for is very efficient in provisioning servers in an unattended fashion. Additionally, Ubuntu cloud images are fully cloud-init compatible, so you can automate the provision and configuration of the image.
To download a cloud image, you can browse Ubuntu Minimal 22.04 LTS (Jammy Jellyfish) release [20230814] site. We shall be using the .img file to create our disk for the VM. You can also download other versions of Ubuntu at Ubuntu Minimal Cloud Images (RELEASED). For this blog, I am using Ubuntu Jammy Jellyfish (22.04 LTS) Cloud image. Download the cloud image by running the following commands.
root@pve:~# wget https://cloud-images.ubuntu.com/minimal/releases/jammy/release/ubuntu-22.04-minimal-cloudimg-amd64.img
root@pve:~# wget https://cloud-images.ubuntu.com/minimal/releases/jammy/release/SHA256SUMS
root@pve:~# sha256sum ubuntu-22.04-minimal-cloudimg-amd64.img -c SHA256SUMS
Adding the disk to the VM
Once verified, convert the ‘img’ to ‘qcow2’ format. Then resize the qcow2 image to 64GB or whatever size you want to configure for your server’s OS disk. But ensure to keep it at least 20 GB which is the minimum size required by Ubuntu 22.04 server. Beware that the size selected shall be the size of the disk created for any VMs created using this template. In order to increase/decrease the size of the disk on the VM, it has to be done before adding any new data to the VM. Also, decreasing the size of the disk may result in some data loss for the data that might have been written on the sectors present after the reduced size.
To perform the above actions and add the disk to your VM, run the below commands. Ensure to verify the VM ID when running the ‘qm importdisk’ command. For my use case, it is ‘900’.
root@pve:~# mv ubuntu-22.04-minimal-cloudimg-amd64.img ubuntu-22.04.qcow2
root@pve:~# qemu-img resize ubuntu-22.04.qcow2 64G
root@pve:~# qm importdisk 900 ubuntu-22.04.qcow2 nvme
Once the disk is added, navigate to the ‘Hardware’ options on your VM and see if the disk is added. It should the disk as ‘Unused Disk 0’. Double-click on the disk and add it as a SCSI hard disk. Verify the hard disk size. It should match the size that you resized the image to. If you are using Proxmox on SSD like mine, select ‘Advance’ and check ‘Discard’ and ‘SSD Emulation’ as shown in the below figure. Once done, click on ‘Add’.
Adding Cloud-init configuration
The cloud-init configuration will ensure that all the virtual machines created using this template will have the same configurations across and are updated to the latest available patches when they are provisioned. The machines will have the same users across with same passwords and you can also add the SSH public key to have your system authorized for login. Generating a public-private key pair for SSH is beyond the scope of this blog, and this is with the assumption that you have already generated a key pair and the public key needs to be used in the configuration.
To add the cloud-init configuration, navigate to your VM and select ‘Hardware’ from the left-hand side menu. Then click on the ‘Add’ button on the top bar as shown in the figure below and then select the ‘Cloud-init drive’ option. This should then add a cloud-init drive and a cloud-init menu on the left-hand side below the ‘Hardware’ menu.
Once the drive is added, you can add the cloud-init configurations as shown in the below images. Use can use any username or keep it at default to use the default ‘os’ based usernames. You can choose a password for your user. Additionally, you can add any specific DNS configurations, if you have one in your network. You can also add the SSH public keys of the authorized systems to connect to your server remotely via SSH. If you choose to provide specific IP addresses to the server, then you can provide so else you can set it to be at DHCP to automatically fetch the IP addresses from the DHCP system or your home router.
Once all the configurations are done, select ‘Regenerate Image’ on the top bar as shown in the figure below to ensure your configurations are registered with Cloud-init and shall be applied when the system is set up. Anytime should you choose to update the configuration, ensure to always regenerate the image, else the configurations will not reflect when setting up the server using the template.
Creating a template from the VM
Finally, we are at the last stage and now the VM needs to be converted to a template. Ensure that you do not boot the virtual machine. Select the virtual machine from the left-hand side menu and right-click and select the ‘Convert to Template’ option from the action list as shown in the figure below.
Once done, allow Proxmox to convert your VM into the template which may take a while. Once done, you shall see the icon for your VM is changed as shown in the below image. Now, your template is ready to be used.
Now you can use your VM to deploy servers with uniform configurations across your environment/home lab. To do so, you can simply right-click on your template and select ‘Clone’. See you in the next blog on cloning the servers from templates and what are the different methods of cloning available in Proxmox.