Getting those pesky VM's to autostart with gnome Boxes

In my day to day I have a need for use of several virtual machines.

Previously I had a full scale lab with dedicated virtualization hosts, which was handy, but had it’s own issues, which I won’t get into here.

Recently I moved and setting up a full scale lab just wasn’t in the cards any more, mostly due to space.

Given that I have a relatively overbuilt PC that I use for my day to day workstation, so why not use that as a Type II hypervisor?

Easy right? Kind of. Fedora ships with a nice GUI front end for virtual machines by way of Gnome Boxes. Unfortunately there are a couple of annoyances that I have with the app:

  • It doesn’t allow you to appropriately name the machine upon creation
  • It doesn’t have an option to make the VMs start upon boot

Admittedly the first item is just me being cranky, although it is worth noting that when you rename the machine its not actually renaming the machine, its just changing the label.

The second one is much more important, I don’t want to have the first thing that I do be to launch my myriad of virtual machines. Sometimes I don’t think about them until I need them, and waiting for infrastructure to start can be awkward, especially if I need to give an impromptu demo.

Also, I run pihole for all my DNS traffic, so if that VM isn’t started then there is no Internet at all- which is problematic for pretty obvious reasons.

So how do we address this?

First, it is important to know that Boxes is really just a front end from qemu + kvm, so you have a lot of functionality for those utilities.

Enter virsh.

Virsh is a command line utility that was constructed for managing virtual machines, including ones based on qemu (as well as Xen, LXC, VMware, VirtualBox and others).

So, let’s figure out how to fix our two headaches using virsh.

Show me my machines!

Let’s take a look at the machines I currently have running by way of boxes:

[bmarlow@ripper ~]$ virsh list
 Id   Name           State
------------------------------
 1    RHEL-8-Prod    running
 2    pihole         running
 3    Satellite      running
 4    Tower          running
 5    RHEL-8-Dev     running
 6    centos7.0-mi   running

Interesting, but this is missing my non-running machines, lets take a look at those:

[bmarlow@ripper ~]$ virsh list --all
 Id   Name           State
-------------------------------
 1    RHEL-8-Prod    running
 2    pihole         running
 3    Satellite      running
 4    Tower          running
 5    RHEL-8-Dev     running
 6    centos7.0-mi   running
 -    rhel8.4-4      shut off
 -    rhel8.4-5      shut off

Okay, now we see the shut off machines, but what is with those names- they don’t match my names in the Boxes GUI:

gnome Boxes

The reason for this is that virsh looks at the xml definition of the virtual machine and reads the name field which is automatically created by Boxes when the virtual machine is created (there is no way in boxes to change this). However Boxes reads (and gives the ability to update) the title field.

We can see this by looking at the XML configuration of the virtual machine:

bmarlow@ripper ~]$ virsh dumpxml rhel8.4-4
<domain type='kvm'>
  <name>rhel8.4-4</name>
  <uuid>296b0cba-2d45-4b44-81e1-0fa643b6f743</uuid>
  <title>My-RHEL-VM</title>

...truncated for brevity...

Now that we understand where it is stored, how do we change it?

Enter more virsh commands

Rename my Virtual Machine

[bmarlow@ripper ~]$ virsh domrename rhel8.4-4 My-RHEL-VM
Domain successfully renamed

Now we can start it with the new name!

[bmarlow@ripper ~]$ virsh start My-RHEL-VM
Domain 'My-RHEL-VM' started

And we can see it using the new name in the list of VMs:

[bmarlow@ripper ~]$ virsh list
 Id   Name          State
-----------------------------
 1    RHEL-8-Prod   running
 2    pihole        running
 3    Satellite     running
 4    Tower         running
 7    My-RHEL-VM    running

Cool. Now, let’s fix the autostart.

Configuring Autostart

This one is really simple!

[bmarlow@ripper ~]$ virsh autostart My-RHEL-VM
Domain 'My-RHEL-VM' marked as autostarted

Boom, now it will start automatically when the libvirtd does.

Speaking of that…

Configuring libvirtd for to automatically start

Again, a pretty easy setup. Mind you I’m using Fedora, so milage will vary on other platforms, but for my setup it’s as easy as:

[bmarlow@ripper ~]$ sudo systemctl enable libvirtd
[bmarlow@ripper ~]$ 

We can verify this by looking at the status of the service:

[bmarlow@ripper ~]$ systemctl status libvirtd
● libvirtd.service - Virtualization daemon
     Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-08-15 16:07:58 PDT; 22h ago
TriggeredBy: ● libvirtd-admin.socket
             ● libvirtd-ro.socket
             ● libvirtd.socket
       Docs: man:libvirtd(8)
             https://libvirt.org
   Main PID: 1807 (libvirtd)
      Tasks: 21 (limit: 32768)
     Memory: 3.3M
        CPU: 2.754s
     CGroup: /system.slice/libvirtd.service
             ├─1807 /usr/sbin/libvirtd --timeout 120
             ├─1942 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
             └─1943 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper

...truncated for brevity...

Specifically on second line we see that the service is loaded and enabled.

And now that you’ve fixed all these issues, you can get back to work! :-)

Tagged #gnome, #boxes, #libvirt, #kvm, #lab, #fedora, #linux

2021

Back to top ↑

2020

Self Service with Satellite

less than 1 minute read

Recently I had a customer who was running into some legacy infrastructure challenges with VM consistency, meeting (internal) customer expectations, and gener...

Winning with Soft Tokens

2 minute read

Well, I’d call that a sabbatical, but I think that would mean I was relaxing. Time to revive this thing (again).

Back to top ↑

2018

Back to top ↑

2017

Using Powershell and Posh-SSH to GSD.

5 minute read

I’m a guy who thinks you should use the right tool for the job. For instance, if you’re in a Windows environment, and you need to script something, installi...

iDrac, RACADM, sshpass, and BASH

8 minute read

If it were up to me, I suppose that the only thing that I’d really be responsible for would be core networking infrastructure (and consumption of craft brews...

Dynamic DNS and you

3 minute read

Okay, so I know the popular thing with network engineers is to remember the IP of EVERYTHING.  I’m pretty good at it too.  But having a lab at home and needi...

Back to top ↑