How to Autostart KVM/QEMU VMs Created with virt-manager Using Systemd or Init Scripts


2 views

When working with KVM/QEMU virtual machines through virt-manager, it's important to understand that virt-manager is just a GUI frontend for libvirt - the actual virtualization management layer. The disconnect you're experiencing occurs because manually launching QEMU/KVM processes bypasses libvirt's control.

Instead of directly executing the QEMU command, you should use libvirt's management tools:

# Set VM to autostart
virsh autostart BORON
# Manually start the VM
virsh start BORON

This creates a symlink in /etc/libvirt/qemu/autostart/ pointing to your VM's XML configuration.

For systems using systemd, create a service unit:

[Unit]
Description=BORON Virtual Machine
Requires=libvirtd.service
After=network.target libvirtd.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/virsh start BORON
ExecStop=/usr/bin/virsh shutdown BORON

[Install]
WantedBy=multi-user.target

Then enable it with:

sudo systemctl daemon-reload
sudo systemctl enable boron-vm.service

For older systems using SysV init:

#!/bin/bash
# /etc/init.d/boron-vm

case "$1" in
  start)
    /usr/bin/virsh start BORON
    ;;
  stop)
    /usr/bin/virsh shutdown BORON
    ;;
  *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac

exit 0

Make it executable and set to start at boot:

sudo chmod +x /etc/init.d/boron-vm
sudo update-rc.d boron-vm defaults

When using the libvirt approach, the VM will properly appear in virt-manager because:

  • libvirt maintains complete VM state information
  • All management interfaces (monitor, console) are properly registered
  • The XML configuration remains the single source of truth

If you encounter problems:

# Check libvirt logs
journalctl -u libvirtd
# Verify VM definition
virsh dumpxml BORON > boron.xml
# Check autostart configuration
ls -l /etc/libvirt/qemu/autostart/

When manually launching VMs through virt-manager, the process automatically handles all libvirt integration and management console visibility. However, when attempting to start the same VM through direct QEMU/KVM commands or scripts, we lose this crucial integration with libvirt's management ecosystem.

Instead of replicating the QEMU command line (which creates "rogue" VMs invisible to libvirt), the correct method is to use libvirt's native autostart functionality:

# Enable autostart for a specific VM
virsh autostart BORON

This creates a symlink in /etc/libvirt/qemu/autostart/ pointing to your VM's XML configuration. The libvirt daemon (libvirtd) will then automatically start these VMs during host boot.

For systems requiring custom init scripts, use virsh commands instead of raw QEMU:

#!/bin/bash
# /etc/init.d/vm_BORON

case "$1" in
  start)
    virsh start BORON
    ;;
  stop)
    virsh shutdown BORON
    ;;
  *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac

If your VM still doesn't appear in virt-manager after starting:

  • Verify libvirtd is running: systemctl status libvirtd
  • Check VM state: virsh list --all
  • Examine logs: journalctl -u libvirtd

For complex scenarios requiring dependencies between VMs or host services, create a systemd service unit:

[Unit]
Description=BORON Virtual Machine
After=network.target libvirtd.service
Requires=libvirtd.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/virsh start BORON
ExecStop=/usr/bin/virsh shutdown BORON
TimeoutSec=180

[Install]
WantedBy=multi-user.target