libvirt: how can I make a domain start when the host starts?

Solution 1:

Looking at the output of "help":

virsh # help autostart
  NAME
    autostart - autostart a domain

  SYNOPSIS
    autostart <domain> [--disable]

  DESCRIPTION
    Configure a domain to be automatically started at boot.

  OPTIONS
    [--domain] <string>  domain name, id or uuid
    --disable        disable autostarting

This requires that you defined the domain from a file (i.e., that the domain is persistent rather than transient).

Solution 2:

If you get the response:

    virsh autostart domainname
    "cannot set autostart for transient domain"

then

    virsh shutdown domainname
    virsh define xmlfile
    virsh start domainname
    virsh autostart domainname

In my experience on Ubuntu 12.04, this only happens if a virsh undefine has been run. Domains created with virsh create usually allow setting the autostart immediately after creation.