Lately I’ve been playing with virtual machines. Although virt-manager is great, it’s not so great for the family members who just want to use the computer. Instead of dual booting, I chose seamless virtualization.
Software
Although I love FreeBSD, I am more experienced with virtualization under Fedora. I’m sure a similar technique would with with bhyve, replacing virt-viewer with your fullscreen VNC client. But for this article, we’re placing the Fedora on our heads.
If you’ve installed Fedora 34 Workstation, you already have kvm and qemu thanks to gnome-boxes. These are very user friendly but not as scriptable.
[user@localhost]$ sudo dnf install libvirt-client virt-viewer
VM Installation
This step is largely user specific, but it’s the same as normal hardware. If you are installing Windows, you’ll want the kvm guest additions. You can use gnome-boxes, virt-manager, or virsh. # Scripting VM Startup ## Writing a startup script After our VM is installed, we need to figure out what it’s name is. Use the following command to find your VM’s name. Mine is ‘win10’.
[user@localhost]$ virsh list --all Id Name State ------------------------------ 8 freebsd13.0 running - kali shut off - openbsd6.7 shut off - solaris10 shut off - win10 shut off
Now we need to write a small shell script. This shell script will be used to start our VM and launch virt-viewer. I like to put my custom scripts and programs in /opt. Name it something specific to the VM, like ‘win10xsession.sh’.
#!/bin/bash # starts our VM virsh start win10 # launches the viewer fullscreen # in kiosk mode that exits when the vm shuts down virt-viewer -f -k --kiosk-quit on-disconnect --attach win10
Now we need to chmod it
[user@localhost]$ sudo chmod a+x /opt/win10xsession.sh
Writing an xsession .desktop file
Display managers like GDM, SDDM, LightDM, &c are helpful because they allow us to easily switch between desktops and window managers. They know what desktops are installed by scanning for .desktop files in<share>/xsessions/
This is extremely helpful for us because we can write a custom .desktop file. It’s easiest to copy a pre-existing .desktop file and modify it.
[user@localhost]$ cd /usr/share/xsessions # we must preserve the SELinux context # or GDM won't see it [user@localhost]$ sudo cp --preserve=context gnome.desktop win10.desktop
And now we can modify our file. Mine looks something like this, but you really should look at the file before nuking it.
[Desktop Entry] Name=Windows10 Comment=Windows10 Exec=/opt/win10xsession.sh TryExec=/opt/win10xsession.sh Type=Application X-LightDM-DesktopName=i3 DesktopNames=i3
Now we can restart gdm and log in to our VM. Beware, restarting gdm will kill your current xsession and send you back to the login screen.
[user@localhost]$ sudo systemctl restart gdm.serice
Conclusion
Now you should be able to select your VM from the gear icon or session button on the display manager. If you cannot see your desktop entry, double check your .desktop file and make sure the syntax is correct and that your script is executable. If your desktop session refuses to start, you probably have an error in your script and need to revise it.
I think that virtualization might be a better option than dual booting, especially when Windows doesn’t like to play nicely with other operating systems.
I don’t know if I have an immediate use for this, but modifying xsession files so that the display manager dumps the user into an unexpected operating system makes for a good prank.