4

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.

4

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)

  • Thanks! I don't currently have a host system to test this on, but it looks exactly like what I was after. And simpler than the other solutions. – LaVache May 15 '18 at 10:53
1
#! /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.

1

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.

0

I use systemctl restart libvirtd.service. Works without a flaw in CentOS 7 (host).

0

On Debian Stretch:

systemctl restart libvirtd

didn't do the job. Libvirt actually restarted its service but all qemu processes and the OS's inside kept running.

This worked:

systemctl stop libvirt-guests   # stops VMs
systemctl restart libvirtd      # starts VMs agains

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.