How to Auto-start Libvirt/KVM Domains on Host Boot: Persistent Configuration Methods


2 views

Libvirt provides a built-in mechanism for auto-starting virtual machines when the host system boots, which is more elegant than using rc.local or cron jobs. The system uses symbolic links in the autostart directory to manage this behavior.

To enable autostart for a specific domain (VM), use the following virsh command:

virsh autostart <domain-name>

For example, to make a domain named "ubuntu-server" start automatically:

virsh autostart ubuntu-server

This command creates a symbolic link from /etc/libvirt/qemu/autostart/ to the domain's XML configuration file in /etc/libvirt/qemu/. For systemd-based systems (like Ubuntu 16.04+), libvirt registers these domains with the libvirtd.service which handles the startup sequence.

To disable autostart for a domain:

virsh autostart --disable <domain-name>

If you prefer manual control, you can create your own systemd service unit. Create a file at /etc/systemd/system/vm-startup.service:

[Unit]
Description=Start libvirt VMs
After=libvirtd.service
Requires=libvirtd.service

[Service]
Type=oneshot
ExecStart=/usr/bin/virsh start ubuntu-server
ExecStart=/usr/bin/virsh start centos-vm
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Then enable it with:

systemctl enable vm-startup.service

If domains aren't starting automatically:

  • Check if libvirtd is configured to start on boot: systemctl is-enabled libvirtd
  • Verify the symlink exists: ls -l /etc/libvirt/qemu/autostart/
  • Examine logs with: journalctl -u libvirtd

Consider these additional points:

  • Set proper boot order for VMs that depend on each other
  • Configure resource limits if multiple VMs start simultaneously
  • Test startup sequence after major host system updates

Libvirt provides a built-in autostart feature that's more elegant than using rc.local. The system manages domain startup through symlinks in /etc/libvirt/qemu/autostart/ which point to your actual XML configurations in /etc/libvirt/qemu/.

To configure a domain to start automatically:

virsh autostart <domain-name>
# Example:
virsh autostart ubuntu-server

This creates a symbolic link automatically. To verify:

ls -l /etc/libvirt/qemu/autostart/

If you need to remove a domain from autostart:

virsh autostart --disable <domain-name>

For custom startup order (using libvirt 5.6.0+):

virsh edit <domain-name>
# Add or modify in the <metadata> section:
<metadata>
  <libvirt:autostart xmlns:libvirt="http://libvirt.org/schemas/domain/1.0" order="5"/>
</metadata>

On modern Ubuntu systems, libvirt uses systemd. The service responsible is:

systemctl status libvirt-guests.service

Configuration file location:

/etc/default/libvirt-guests

Key parameters you might want to adjust:

# Default is 'suspend', can change to 'start'
ON_BOOT=start

# Parallel startup (number of concurrent guests)
PARALLEL_START=4

# Timeout per guest
START_DELAY=30

If domains aren't starting automatically:

  1. Verify the libvirt-guests service is enabled:
    systemctl is-enabled libvirt-guests
  2. Check for errors in the journal:
    journalctl -u libvirt-guests
  3. Ensure proper permissions exist for the domain XML files

For maximum control, create custom systemd services:

# /etc/systemd/system/vm@.service
[Unit]
Description=Start KVM domain %i
After=network.target libvirtd.service

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

[Install]
WantedBy=multi-user.target

Then enable for specific domains:

systemctl enable vm@ubuntu-server.service