LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   problem with libvirt and init scripts (http://www.linuxquestions.org/questions/slackware-14/problem-with-libvirt-and-init-scripts-826972/)

Slax-Dude 08-18-2010 06:28 AM

problem with libvirt and init scripts
 
I installed libvirt from slackbuilds.org and it has been working great except for one thing: it does not work once we switch to run level 6 or 0.

I'll explain further :)
I would like to shutdown all running VMs before shutting down the host.
To accomplish this, I made a script named /etc/rc.d/rc.vm_shutdown and tried to call it from /etc/rc.d/rc.local_shutdown, but it doesn't work.

My script is very simple, and works as intended if called directly:
Code:

#!/bin/bash                                           

# number of seconds to wait for VMs to shutdown, before killing them
MAX_TIMEOUT=30                                                     

RUNNING_VM_LIST=$(virsh list|grep running|cut -d " " -f 4-4)
               
for RUNNING_VM in $RUNNING_VM_LIST; do                     
  virsh shutdown $RUNNING_VM
done

TIMEOUT_COUNTER=0
while true; do
  RUNNING_VM_LIST=$(virsh list|grep running|cut -d " " -f 4-4)
  if [ -z "$RUNNING_VM_LIST" ]; then
    exit 0
  else
    if [ $TIMEOUT_COUNTER -lt $MAX_TIMEOUT ]; then
      sleep 1
      TIMEOUT_COUNTER=$[$TIMEOUT_COUNTER+1]
    else
      for RUNNING_VM in $RUNNING_VM_LIST; do
        virsh destroy RUNNING_VM
      done
    fi
  fi
done

As you can see, I use "virsh list" to check for running VMs... the problem is that "virsh list" reports an empty list when called from within rc.local_shutdown :(

Can anyone explain why this happens and how to solve it?

As a workaround, I edited inittab so it calls my script BEFORE rc.6, but I would rather avoid editing system files if I can.

ponce 08-18-2010 08:18 AM

just a wild guess: could it be because PATH isn't set when executing rc.d's scripts so you have to specify the full PATH to virsh?

Slax-Dude 08-18-2010 08:27 AM

Quote:

Originally Posted by ponce (Post 4070221)
just a wild guess: could it be because PATH isn't set when executing rc.d's scripts so you have to specify the full PATH to virsh?

Hi ponce,

I don't think that's it.
The script runs fine, it just reports an empty VM list and exits.
PATH is set on rc.6, which calls rc.local_shutdown, which calls my script.

ponce 08-18-2010 08:36 AM

yes, but in the path set in rc.6 there isn't /usr/sbin: instead of the simple virsh you can try launching like
Code:

/usr/sbin/virsh -c qemu:///system
(it should connect to qemu:///system by default but who knows)

Slax-Dude 08-18-2010 09:09 AM

Quote:

Originally Posted by ponce (Post 4070238)
yes, but in the path set in rc.6 there isn't /usr/sbin: instead of the simple virsh you can try launching like
Code:

/usr/sbin/virsh -c qemu:///system
(it should connect to qemu:///system by default but who knows)

OMG!
I've been lurking in IRC channels for the past week and rebooted my VM server more times than I can remember... and you nailed it in 5 mins...

Thank you very much, ponce :)

Slax-Dude 08-18-2010 09:15 AM

As it may be useful to someone, here is my (now working) script:
Code:

#!/bin/bash                                           

# number of seconds to wait for VMs to shutdown, before killing them
MAX_TIMEOUT=30                                                     

RUNNING_VM_LIST=$(/usr/sbin/virsh -c qemu:///system list|grep running|cut -d " " -f 4-4)
               
for RUNNING_VM in $RUNNING_VM_LIST; do                     
  /usr/sbin/virsh -c qemu:///system shutdown $RUNNING_VM
done

TIMEOUT_COUNTER=0
while true; do
  RUNNING_VM_LIST=$(/usr/sbin/virsh -c qemu:///system list|grep running|cut -d " " -f 4-4)
  if [ -z "$RUNNING_VM_LIST" ]; then
    exit 0
  else
    if [ $TIMEOUT_COUNTER -lt $MAX_TIMEOUT ]; then
      sleep 1
      TIMEOUT_COUNTER=$[$TIMEOUT_COUNTER+1]
    else
      for RUNNING_VM in $RUNNING_VM_LIST; do
        /usr/sbin/virsh -c qemu:///system destroy RUNNING_VM
      done
    fi
  fi
done


ponce 08-18-2010 09:24 AM

tnx a lot, I think I'll use it on my desktop :D


All times are GMT -5. The time now is 01:29 PM.