KVM: Start a Virtual Machine / Guest At Boot Time

Posted on in Categories , , , last updated May 6, 2017

There are various ways to start virtual machines at at boot time. This means you don’t have to type virsh start vmName command. This can be done by marking a vm as autostart. To configure a domain to be automatically started at boot. It will create an softlink at /etc/libvirt/qemu/autostart/. So if your VM name is debianlenny1, your config file name should be /etc/libvirt/qemu/debianlenny1.xml and the softlink should be created at /etc/libvirt/qemu/autostart//debianlenny1.xml

KVM: Autostart a Domain / VM Command

Type the following command:

virsh autostart vmName
virsh autostart debianlenny1

Sample outputs:

Domain debianlenny1 marked as autostarted

Also, make sure /etc/init.d/libvirtd service is stared on boot:
chkconfig libvirtd on
OR
systemctl enable libvirtd

How do I disable autostarting a domain/VMs

The syntax is:
virsh autostart VMNameHere --disable
To disable auto starting freebsd vm, enter:
virsh autostart freebsd --disable
Sample outputs:

Domain freebsd unmarked as autostarted
This entry is 11 of 14 in the CentOS / Redhat (RHEL) KVM Virtulization series. Keep reading the rest of the series:
  1. CentOS / Redhat: Install KVM Virtualization Software
  2. CentOS / Redhat: KVM Bridged Network Configuration
  3. KVM virt-manager: Install CentOS As Guest Operating System
  4. KVM virt-install: Install FreeBSD / CentOS As Guest Operating System
  5. KVM: Install CentOS / RHEL Using Kickstart File (Automated Installation)
  6. Troubleshooting KVM Virtualization Problem With Log Files
  7. KVM Virsh: Redirect FreeBSD Console To A Serial Port
  8. KVM: Starting / Stopping Guest Operating Systems With virsh Command
  9. Linux KVM: Disable virbr0 NAT Interface
  10. FreeBSD / OpeBSD Running in KVM Does Not Accept FTP Traffic
  11. KVM: Start a Virtual Machine / Guest At Boot Time
  12. KVM virt-install: Install OpenBSD As Guest Operating System
  13. Linux KVM: OpenBSD Guest Hangs At Starting tty Flags
  14. KVM Virtualization: Start VNC Remote Access For Guest Operating Systems

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

6 comment

  1. Also note that to disable autostarting use the –disable flag:

    virsh # autostart footest –disable
    Domain footest unmarked as autostarted

  2. hi
    i see that you posted some time ago so i don’t know if you still monitor this but i am hoping someone can help me out.
    i am capable of doing what you said above and the 6 virtual machines (VMs) all boot when the (bare metal) server boots up; however, they are in a cluster and for whatever reason – in order for the VMs to boot up consistently and not hang – i have to boot the 1st VM 1st and after it starts the quorum (somewhere around “starting crond”) i can start the remaining VMs and all VMs consistently boot up. also, when shutting the VMs down, i have to shutdown all of the VMs except the 1st VM – in order for the VMs to shutdown consistently and not hang / give corosync dlm lock errors – and after all the other VMs have shutdown i can shutdown the 1st VM and this works consistently.
    SO…
    i cannot simply tell all VMs to automatically boot up when the server boots up…i have to create a service to tell the VMs to boot up / shutdown in the order i specified with a “sleep” in between. i write this service no problem but i am getting errors and none of the VMs are starting up. i made sure to put my service at the very end “S99service_name” in the /etc/rc.d/rc3.d file. i am wondering if it is being killed by the libvirtd or fence-virt services even though they’re set to “S98”??
    at what point in time / what service is responsible for performing the “virsh start VM” command? i’m guessing wherever that is i should put my service.
    thanks
    scott

  3. Scott – have you tried putting the startup commands in /etc/rc.d/rc.local with sleep in between them. That is executed when everything else has started, it’s where I would start.

  4. hi john
    thanks for responding.
    i have since gotten someone involved and it is resolved. there were a number of problems. 1) we were using rhel 6 which requires the use of a certain format at the beginning of the service files 2) i forget which of the services (it could have been libvirtd or fence-virt) but it was attempting to start the VMs as well. i think this is what prevented the service files that i created from working.
    scott

Comments are closed.