Comment 24 for bug 350936

Revision history for this message
tomcrus (tomcrus) wrote :

As I'm currently setting up an intranet mail-server for my company (based on lucid / ebox) in some kvm-guests and there for have exactly the same problem. Reading all previous posts - and because I need kvm-guest shutdown as well - I will use something like this:

Add following lines in /etc/init.d/sendsigs just before the sync line:

  # avoid every running kvm (started thru libvirt-bin) from being killed
  for pidfile in /var/run/libvirt/qemu/*.pid; do
    OMITPIDS="${OMITPIDS:+$OMITPIDS }-o $(cat $pidfile)"
  done

I think this should prevent kvm-guests from being killed on shutdown.
Then in /etc/init/libvirt-bin.conf I would try to add following lines:

  kill timeout 360

  pre-stop script
    # only shutdown all guests if stopping of task is because of
    # runlevels 0 (halt) or 6 (reboot)
    if [ "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "6" ]; then
      TIMEOUT=300
      VIRSH_CONNECT="qemu:///system"

      function list_running_vms(){
        virsh --connect $VIRSH_CONNECT list | grep running | awk '{ print $2 }'
      }

      # send shutdown to each running vm (they must handle acpi-power-button!)
      list_running_vms | while read vm; do
        virsh --connect $VIRSH_CONNECT shutdown $vm
      done

      # wait until timeout has reached or no running are vms left
      END_TIME=$(date -d "$TIMEOUT seconds" +%s)
      while [ $END_TIME -lt $(date +%s) ]; do
        test -z "$(list_running_vms)" && break
        sleep 1
      done

      # if any vms left running destroy them now
      if [ -n "$(list_running_vms)" ]; then
        list_running_vms | while read vm; do
          virsh --connect $VIRSH_CONNECT destroy $vm
        done
      fi
      sleep 3
    fi
  end script

I don't know yet if this works, but I will test as soon as possible. Maybe if somebody else is in the mood to do so please report if it succeeded...