Copying Virtual Machines
- Last Updated: 08/10/2018
Intro: Understanding Virtual Machines¶
Virtual machines are essentially machines simulated on a physical device. It's standard for a virtual machine to consist of two pieces, an image (.img) file and a configuration (.xml) file. The .img file is its storage - the VM sees it as a hard drive. The .xml file associated with the virtual machine is its configuration file, which defines aspects of the machine such as its storage capacity, allocated CPU cores and RAM, interfaces, and so on. Different programs handle the files differently but this base concept will hold true across many OSes and applications.
For our labs we use Virsh, a command line tool for managing virtual machines in Linux. When setting up a VM, we first configure our .xml file to give Virsh a path to the .img file along with allocating its resources (disk space, CPU, RAM, etc). After that, we run "virsh define filename.xml" to define a new virtual machine based on the specified .xml file's configuration. When we run this command, Virsh stores a copy of the .xml, which it then uses every time we start the VM. From then on, we access the VM based on the name specified in the .xml file, . We can then start that VM using "virsh start vmname".
It's worth noting that if you change filename.xml after a Virsh VM definition the changes will have no effect, as Virsh copies the xml to associate it with the VM when the define command is run. Instead, if you wish to edit a VM's configuration, you should . You can shut down a VM using "virsh shutdown vmname" (or "virsh destroy vmname" to 'unplug' it). You can then do "virsh edit vmname" which will open Virsh's stored copy of the configuration file for editing.
Copying Virtual Machines¶
Because virtual machines are stored in files, copying them is fairly straightforward. We have a tool to make our job even easier: virt-clone. If we have a defined and shut down VM we want to copy, we can simply use the following command:
virt-clone -o oldvm -n newvm -f newvm.img
In this case, oldvm is the vm we wish to copy and newvm is the name of the copy. We specify the vm we wish to copy with -o, the name of the new one with -n, and the name and path of the new one's .img file with -f. That's it! Virsh will automatically define the new VM based on the same config as the old one, but with the updated name. If you wish to make any changes, you can simply "virsh edit newvm" before starting it.
If we want to use a copy of an existing image with a new or different .xml, the image we want to copy isn't defined in virsh, or our system doesn't have virt-clone, we can also do this manually without much more effort. First, . Next, we can simply copy the image file in terminal using the linux cp (copy) command:
cp oldvm.img newvm.img
This may take some time as image files can be very large. Once the image is copied, we can either edit an existing xml file for configuration or copy one (just like above). Be sure to change at minimum the name of the VM (at the top) and the filepath (the image file will be different). Once you've done that, simply use Virsh to define the new VM from the .xml file you created.
If you're wondering how long the copying process will take, you can check! Linux command line systems usually support multiple consoles. You can switch to the first one with alt-f1, the second with alt-f2, and so on. Different machines will have a different maximum number. If you are copying a file in /data for example, you can switch to a second console, go to /data, and run "ls -l" to list the files and their filesizes in that directory. You can compare the size of the file you are copying from and the one you are copying to: when they're the same, it's almost done copying. If you don't want to have to enter the command repeatedly, you can use the "watch" command to automatically run it for you. Ctrl-C to cancel.
watch ls -lh