LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Search this Thread
Old 08-18-2010, 06:28 AM   #1
Slax-Dude
Member
 
Registered: Mar 2006
Location: Valadares, Portugal
Distribution: Slackware
Posts: 254

Rep: Reputation: 72
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.

Last edited by Slax-Dude; 08-18-2010 at 06:45 AM. Reason: corrected script
 
Old 08-18-2010, 08:18 AM   #2
ponce
Senior Member
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 2,447

Rep: Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875
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?
 
1 members found this post helpful.
Old 08-18-2010, 08:27 AM   #3
Slax-Dude
Member
 
Registered: Mar 2006
Location: Valadares, Portugal
Distribution: Slackware
Posts: 254

Original Poster
Rep: Reputation: 72
Quote:
Originally Posted by ponce View Post
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.
 
Old 08-18-2010, 08:36 AM   #4
ponce
Senior Member
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 2,447

Rep: Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875
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)

Last edited by ponce; 08-18-2010 at 08:40 AM.
 
1 members found this post helpful.
Old 08-18-2010, 09:09 AM   #5
Slax-Dude
Member
 
Registered: Mar 2006
Location: Valadares, Portugal
Distribution: Slackware
Posts: 254

Original Poster
Rep: Reputation: 72
Quote:
Originally Posted by ponce View Post
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
 
Old 08-18-2010, 09:15 AM   #6
Slax-Dude
Member
 
Registered: Mar 2006
Location: Valadares, Portugal
Distribution: Slackware
Posts: 254

Original Poster
Rep: Reputation: 72
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
 
Old 08-18-2010, 09:24 AM   #7
ponce
Senior Member
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 2,447

Rep: Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875
tnx a lot, I think I'll use it on my desktop
 
  


Reply

Tags
init, slackware


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Diffrence /etc/init.d and /etc/init.d/rc?.d scripts veeruhs Linux - Server 3 07-29-2010 04:42 AM
Init Scripts are broken problem while mounting the hard disk partitions using ntfsmou nikunjchaudhary Linux - Desktop 1 07-26-2010 05:29 AM
[SOLVED] init scripts, variables in INIT INFO section catkin Linux - General 1 08-18-2009 02:02 PM
Problem installing debian etch /scripts/init-premount guillermo Linux - General 2 01-19-2008 11:00 AM
Problem with init scripts when shutting down atlesn Linux - General 3 03-13-2004 05:10 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration