libvirt: command to start up all guest virtual machines which have auto-start enabled

22,243

Solution 1

Like @jason-harris solution. But simpler and start only marked for autostart.

for i in $(virsh list --name --autostart); do virsh start $i; done

UPD: I tested it on libvirt 3.2.0 (CentOS 7.4.1708)

Solution 2

I don't believe there is a simple command to do that (but it would be great!).

I do this fairly often and it is usually just a quick script / one line command using a for loop and some awk.

Keep in mind that if your hypervisor is connected to other hypervisors, you might want to sever that link temporarily so you don't inadvertently interfere with other VMs on other hypervisors. I haven't seen many people actually utilize this though, but I wanted to point that out.

For instance, you could type the following to achieve the result you want, assuming you want to turn everything back on. This assumes you are root:

for i in `virsh list --all|awk '{print $2}'|grep -v Name`; do virsh start $i; done

The command breaks down like this:

virsh list --all

Shows all of the virtual machines, whether they are on or not.

awk '{print $2}'|grep -v Name

We are taking out the unneeded columns and only printing the column with the virtual machine names. Then we take out the header row with grep since it isn't actually a virtual machine.

virsh start $i

Turns on the virtual machine.

And this is all wrapped around a general for loop.

Solution 3

#! /bin/bash

xmlfiles=( $(find /etc/libvirt/qemu/autostart/ -name '*.xml') )

for f in "${xmlfiles[@]}" ; do
  domain=$(xml2 < $f | awk -F= '$1 == "/domain/name" {print $2}')

  # only start domain if it's not already running
  if ! virsh list | grep -q " ${domain} .*running" ; then
    virsh start "$domain"
  #else
    # optionally reboot domain otherwise
    #virsh reboot "$domain"
  fi

done

This script requires the xml2 utility (in package xml2 in debian and probably ubuntu too).

It uses xml2 to extract the domain name(s) from all XML files in libvirt's qemu autostart directory, and runs virsh start on them if they're not already running.

Save it somewhere in your PATH, as something like virsh-autostart.sh (or whatever makes sense to you) and make it executable with chmod.

Share:
22,243

Related videos on Youtube

LaVache
Author by

LaVache

Updated on September 18, 2022

Comments

  • LaVache
    LaVache over 1 year

    I'm using Debian Jessie as a virtual machine host using libvirt/qemu/kvm.

    I've set some of the guest virtual machines to automatically start when the host OS boots up, this is working fine.

    For maintenance purposes, I'm running "service libvirt-guests stop" to shut all the guests down (but not the host).

    Once I've done my maintenance, I want to easily boot all the guests up again (without rebooting the host).

    Is there a single command that will start all the guest VMs up again? I'm interested in knowing about both:

    1. a command to start all the autostart-marked guests up again

    2. a command to start all the guests up again that were running before I ran "service libvirt-guests stop"

    Rebooting the host OS would achieve #1, but I don't want to reboot the host.

    I tried, "service libvirt-guests start" but it doesn't seem to do it.

  • mgutt
    mgutt about 2 years
    Does not work if domain name contains whitespaces.