Ubuntu Server 18.04 – Simplifying virtual machine creation with cloning

Installing MySQL On CentOS 8

Now that we have a KVM server, and we can spin up an army of virtual machines to do our bidding, we can try and find clever ways of automating some of the workload of setting up a new VM. Every time we go to create a new VM, we need to go through the entire installation process again. We’ll select an ISO file, navigate through the various screens to install the operating system, and then the VM is ready for use.

Most prominent virtualization solutions feature something called a Template. Essentially, we can create a virtual machine once and get it completely configured. Then, we can convert it to a template and use it as a base for all future VMs that will use that same operating system. This saves a tremendous amount of time. You’ll probably recall the many screens you had to navigate through to install Ubuntu Server in our first chapter. Imagine not having to go through that process again (or at least not nearly as often).

Unfortunately, as great as QEMU/KVM is, it doesn’t have a template feature. This glaring hole in its feature set is a sizable setback, but thankfully we Linux administrators are very clever, and we can easily work around this to roll our own solution and create a system that is essentially the same thing as templates. Take the following screenshot, for example:

Selecting a bridge for a newly created VM

That screenshot is taken from an actual QEMU/KVM server that I maintain. It has four VMs, dev, nagios, pi-hole, and ubuntu-18.04-template. The latter is not a template at all; it’s just a virtual machine. There’s nothing really different between it and the others, aside from the fact that it isn’t running. What it is, though, is a clever workaround (if I do say so myself). If I want to create a new virtual machine, I simply right-click on it, then click Clone. The following window will appear:

Cloning a VM

When I click Clone in this window, after giving the new VM a name, I’ve made a copy of it to serve as my new virtual machine. It will use the original as a base, which I’ve already configured. Since Ubuntu Server was installed on the template” I don’t need to do all that work again.

However, there are a few tasks to do after cloning the template. For one, it won’t have all of the security updates that have been released since it was created, so updating all of the packages in the clone is a good first step so we can make sure it’s current. But there’s another aspect that you may not immediately think of, and that is OpenSSH. When installed, OpenSSH (assuming you had it installed on the original template) will generate a Host Key. On the clone, the OpenSSH host key will be the same as the one in the template. This isn’t good, because that will confuse the OpenSSH client into thinking that it is connecting to the same machine but at a different IP address. Resetting the OpenSSH host key should be done before we put the new server into production. To do that, connect to the cloned machine and run the following two commands:

sudo rm /etc/ssh/ssh_host_*
sudo dpkg-reconfigure openssh-server

Basically, all you’re doing is deleting all the original host keys and regenerating them. While this solution isn’t glamorous, it does work. Now, the cloned virtual machine has its own set of host keys. Now you can put this server into production. So long as you maintain your base VM, you can spin up as many virtual machines as you need and be able to do so with minimal configuration steps.

Comments are closed.