Autostarting Vms on Host Reboot
Autostarting VMs on Host reboot
Suyash Singh
Posted by Suyash Singh
on October 7, 2022
Photo by Pietro on Unsplash

Linux users have a lot options when it comes to spinning up a VM quickly with a hypervisor or emulator, for instance Gnome Boxes offers a minimal UI and a terrific experience of spinning up the latest versions of distros, Oracle’s Virtualbox comes with a lot of upfront configurations if that’s what needed at the time. And then, there’s the tech behind Gnome Boxes: QEMU, KVM and libvirt which can do all sorts of hypervisor magical stuff: GPU passthrough for instance.

Let’s see how we can autostart the VM when the linux host machine starts up.

Install QEMU

To run VMs you’d need a hypervisor, ensure you have QEMU & KVM installed

$ sudo pacman -S qemu-base
# https://wiki.archlinux.org/title/KVM#Checking_support_for_KVM check if you have KVM support available for your machine
# KVM requires that the virtual machine host's processor has virtualization support (named VT-x for Intel processors and AMD-V for AMD processors). 
# You can check whether your processor supports hardware virtualization with the following command:
$ LC_ALL=C lscpu | grep Virtualization```

Install libvirt

To get access to tons of VM management API grab the libvirt package

$ sudo pacman -S libvirt

Install an OS in the VM

Using either the virt-manager (GUI app) or using the virsh (virtual shell) CLI spin up a VM using a distro’s ISO Autostart vm using virsh Use virsh to autostart a VM

$ virsh autostart $VM_NAME

Reboot

Reboot your machine and rejoice to see your autostarted VM

But why would you even want to ever autostart a VM?

Well, a few scenarios top of my head are:

Let’s say that you self host your websites in a VM (saves a lot of money) and also that you live in an area with uncertain power supply — You’d want to automate the init processes if your host machine goes for shutdown or reboot

You can run your private build servers for CI/CD like Jetbrains TeamCity — at the time of this writing they are offering it for free with a 100 build configurations limit and unlimited build time. Alternatively, you can use Github Actions to automate these things for free, but there’s more freedom & control when you run your own private CI server.

These points alone make for a strong case when you have a shared family computer at home and you need to self host a lot personal projects (family members can now freely restart the machine if they need to and you get to work on your patience a bit, haha 😅)

Further Reading