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, which is not necessarily the same as the .xml file’s name. 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 first ensure it is shut down. 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.

Cloning Virtual Machines

Note: Anything in this
fonts
or
fonts
means that you’ll be typing in the terminal or what you’ll be seeing in the terminal.
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, make sure the image to copy is shutdown (if applicable). 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 file sizes 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