Using VMs and learning your tools

Posted on Dec 3, 2023

For as much as I’ve been talking about Distrobox lately, I’ve actually been spending a lot of time working with virtual machines. I’ve learned so much about using KVM, QEMU, and virt-manager in the last few months I figured I would drop my notes.

I like to do this occasionally so others can see how I’m using tools that are available. I know that when I see how someone else uses something it gets my wheels turning on how I could use their techniques to build something different or solve one of my problems. Yes, its nice to see a guide on how to do something. But often what is missing for me is context as to why they are doing it. I don’t need “what if” scenarios. What are you actually using it for?

Being proficient in your tools

As a construction contractor I think about the phrase, “You are only as good as your tools” all the time. That phrase doesn’t just mean that you should buy quality tools, which is often how it is taken. It also means you are only as good as how well you know how to use those tools. This means to be good at a job you don’t need to know what the tool can do, you also need to know in the abstract how to use to accomplish jobs and projects. You have to be able to know the tool inside and out and then use that information in conjunction with your imagination.

Constantly researching new tools and methods is a form of procrastination. I have chosen to use KVM, QEMU, and virt-manager on a Debian host to create and use virtual machines. So, I’ve been going through all the options for those tools so I can leverage them to my interests as best as possible.

Below are notes on what I’ve learned recently and how I’ve been using those features. I know for people who have been in IT this is probably old news. But, it is new to me and I’m happy with how it has been going for someone who doesn’t do this professionally.

I’m very interested to find out how others use virtual machines. Please send me your tips and tricks!

Migrate to new machine

Lately I have been setting up VM’s on my desktop and going through all of the configuration locally, then migrating to my homelab after testing is complete.

Depending on the service, sometimes I’m just testing a Docker container and I want to keep my host clean. This service may not stick around and just doing some testing. If I like it, then I will run the container directly on the host for my LAN.

If the service will be public facing, I like to leave that in the VM for extra protection. For example, I like to run the Hugo snap inside a VM so it is sandboxed inside a container, abstracted away from the host with multiple layers.

In this instance, this is how to move that VM to a new machine. I also create backups of my VM’s in case I need to rebuild my server and using these steps I can recreate the containers very quickly. Last, I’ll pull containers from my homelab to my desktop and make edits, updates, etc. while the primary VM is still running. Then, if I like the updates/changes, I will replace the VM on the host with the new qcow storage.

How to migrate a VM

  1. Copy the VM storage from path/to/qcow/files on (former) host to the same directory on destination host. I normally have these somewhere in /mnt.
  2. On the (former) source host, run virsh dumpxml VMNAME domxml.xml. Then copy this xml file to the (new) destination.
  3. On the (new) destination run virsh define domxml.xml

If any of the information on the new destination is different than the original setup, be sure to edit it before running. I do this in virt-manager and edit the xml.

Clone to bootable image

I’ve recently started creating new laptops/desktops as VM’s and then writing the file to a HDD and stuffing it into the PC after I’m done configuring. It makes it a lot faster to setup because I can use a static image of Debian 12 with all the base apps and tools installed. Then, depending on the use case, I can layer on as necessary.

To create the image, change to the directory where the qcow storage file is located. Then create the image with:

sudo qemu-img convert <name_of>.qcow2 -O raw <filename>.img

Once this is done writing we can create a bootable disk using standard dd. The gist I’m getting is that this will work, but the machine that it goes into needs to be set to “legacy” as the image will not boot with EFI.

dd if=/name/of/image/.img of=/dev/sdX status=progress && sync

After testing I can confirm, the standard way of using the img file will not boot on EFI systems. For most of my homelab this doesn’t matter, I can just tweak the BIOS settings.

Remotely managing virtual machines

Something that never dawned on me is using virt-manager to manage remote machines. This is fucking rad and makes my life so much simpler.

The goal is to ssh into a box and then see all the machines in virt-manager. Right now this is important because when I want to edit a machine settings, create a new, or just turn on a dormant setup, I need to either do it at the laptop or use x2go to view the full desktop. Using x2go isn’t bad, but I don’t want to start the display manager for no reason.

Instead of mirroring the server desktop and then launching virt-manager on that machine, I can access it through virt-manager on another device using the ssh credentials. Even better it will use entries in the ssh config file, so I can just call the hostname. You can do this in the terminal or in the virt-manager GUI.

From the terminal, this is the command:

virt-manager -c 'qemu+ssh://<ssh_config_hostname>/system'

In the command you can see that I am passing access to qemu and also want to connect via ssh. Instead of putting the full path for the ssh connection, I can just use the access names in my config file.

To do it manually, the command includes the port and the name of the ssh key. First you have to move to the directory with the keyfile, and then use a command like this:

virt-manager -c 'qemu+ssh://<user>@192.168.1.xxx:PORT/system?keyfile=key_file'

I set up the above command using an alias in fish so its easier to use later.

- - - - -

Did you like this post? Give it an upvote by clicking on the arrows below! Sending me an upvote is like you and I giving each other a high five.

🙏 😎

Thank you for reading! If you would like to comment on this post you can start a conversation on the Fediverse. Message me on Mastodon at @cinimodev@masto.ctms.me. Or, you may email me at blog.discourse904@8alias.com. This is an intentionally masked email address that will be forwarded to the correct inbox.

If you enjoy the random stuff I write here, post to Mastodon, or watch on YouTube, and are feeling generous, I am open to tips of Ko-fi.