Trinix Quick Tip!
Using ‘virt-clone’ & ‘virt-sysprep’ to Clone a KVM/QEMU _ubuntu Based Virtual Machine

Using ‘virt-clone’ & ‘virt-sysprep’ to Clone a KVM/QEMU _ubuntu Based Virtual Machine

Introduction & Prerequisites

This short guide will show you how to clone an existing virtual machine and run virt-sysprep to prepare the VM so it doesn’t conflict with the network settings of the original guest image.

Before we begin, the main prerequisite is to have a ‘relatively’ clean install of _ubuntu. By ‘relatively’, I mean in the sense that you haven’t done really anything more than sudo apt update && sudo apt upgrade on the guest machine that you want to make a clone of.

References & Additional Web Resources

Below are various web resources I used during the creation of this guide and also when I initially started researching Virtualization with KVM & QEMU.

The Procedure

In my case, I originally made 2 clones from a VM that I had a fresh install of Xubuntu 18.04 with no updates installed. I didn’t even do an initial login. The problem that I ran into was the time it took to run the update on each cloned machine took too long. So I recommend doing this on the Virtual Machine you will use as a source for the clones to save you some time.

One you have updated your guest machine, shut it down. Then open the console and navigate to the path where your virtual machine images are stored

Once you are within that directory, do a ls -lah to get a list of the VM images. Find the image of the machine you want to clone and modify the command below by inserting the file name where it says ‘source-vm-name’ and chose a cloned-image-name and cloned-disk-iamge-filename.qcow2 for your new cloned virtual machine.

/path/to/vm-imagesvirt-clone --original source-vm-name --name cloned-image-name \
--file /path/to/your/vm/cloned-disk-image-filename.qcow2

Once the Virtual Machine is cloned, if you already have virt-manager running, you will see the newly created machine appear in the list of VMs. We can now run the virt-sysprep tool to prepare our freshly minted clone.

To prepare our VM, the virt-sysprep tool offers a lot of options. I found this page: How to reset a KVM Clone Virtual Machines with virt-sysprep on Linux that goes over a couple of the virt-sysprep command options along with providing a table of what looks like the complete set of command line options (keep in mind, that this article was written in Oct. 2018 and as time goes on, options may be added or removed in this software so when you read this the options list may not contain all the options available.)

For my clone, I just really needed to change the host name and make sure my user account stayed. You may need more or less options when preparing your machine, so it’s best to go over the available set to see if there is anything that stands out that you would like to explicitly set.

I ran into a little trouble when trying to set the HOSTNAME on the cloned VMs. Basically that’s the only setting I needed to set for my clones, but I had to go an unconventional route to get virt-sysprep to change the host name in the /etc/hostname & also in the /etc/hostname file.

The command I tried is below. Setting the –hostname option had no effect. You may or may not have more luck than me. The location of those 2 files is pretty standard within Luinux so I’m not quite sure what the issue is because virt-sysprep did not throw any errors.

The command that didn’t work for me…use at your own risk:

/path/to/your/vm/imagesvirt-sysprep -d XubuntuTestVM02 --hostname 'xubuntu-testvm02' --keep-user-accounts brandon

The command I created basically over-writes the hostname file by using the –write option.

For the /etc/hosts I had to-do something a little different because there should really be a bare minimum of 2 lines in this file. The first one sets the localhost to 127.0.0.1 and the second line usually sets the current machines hostname to 127.0.1.1.

No I couldn’t just append to the hosts file because it would still have the original hostname from the virtual machine we cloned in the file. The –write option will wipe the slate clean for us, but then we need to follow that up with the –append option for the second line.

Now someone will probably ‘enlighten’ me to a fact that I probably could of just done this in a single –write options with out the extra append argument and to be honest, I didn’t even try it. I did it this way because it was explicit and frustration was getting the better of me after the –hostname option not doing what it promised.

With all that said, here is my ‘hack’ to set the hostname in both the /etc/hostname and the /etc/hosts files. This may be sloppy but it did what I needed it todo. As always, I’m open to suggestions, so if you have a ‘better’ way, please let me know. Just don’t be a troll about it. We’re all just trying to learn.

/path/to/your/vm/imagesvirt-sysprep virt-sysprep -v -d "Xubuntu-cloned-VM01"\
--write "/etc/hostname:xubuntu-clonedVM01" \
--write "/etc/hosts:127.0.0.1" \
--append-line "/etc/hosts:127.0.1.1 xubuntu-clonedVM01"


Closing Thoughts

Well that’s about it for this one. I did create a little helper script for the virt-sysprep helper script. All it basically does is take in the original virtual machine name to clone and the name of the new VM. From this information it creates a hostname from the name of the new virtual machine and calls both the virt-clone and the virt-sysprep utilities

I’m currently editing a video walk-through of the process that will be uploaded to our YouTube channel within the next few day. Once that’s up, I’ll post a link to the screen-cast walk-through video here.

Thanks for stopping by & leave a comment if you have any questions, thoughts or recommendations. Keep it civil as no one is perfect & knows everything. We’re all human after all.

Leave a Reply

Your email address will not be published. Required fields are marked *