I have set up an init-script to control the state of a VirtualBox VM:
#!/bin/sh
#chkconfig: 35 99 5
#description: vTiger virtual machine
### BEGIN INIT INFO
# Provides: vtigervm
# Required-Start: $local_fs
# Requider-Stop: $stop_fs
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Manage vTiger virtual machine
# Description: Utility to start and stop vTiger virtual machine on VirtualBox
### END INIT INFO
start()
{
echo -n "Starting vTiger"
echo
su myuser -c '/usr/bin/VBoxManage startvm "vTiger" --type headless'
echo "Started virtual machine" >> /var/log/messages
}
stop()
{
echo -n "Shutting vTiger down..."
echo
su myuser-c '/usr/bin/VBoxManage controlvm "vTiger" acpipowerbutton'
while [ ! -z "`su - juhani -c '/usr/bin/VBoxManage list runningvms | grep vTiger'`" ]; do
echo -n "."
sleep 1
done
echo "Done."
echo "Stopped virtual machine" >> /var/log/messages
}
status()
{
echo -n "Running VMs: "
su myuser -c '/usr/bin/VBoxManage list runningvms'
echo
if [ -z "`su - juhani -c '/usr/bin/VBoxManage list runningvms | grep vTiger'`" ]; then
RETVAL=3
else
RETVAL=0
fi
echo "Queried virtual machine status" >> /var/log/messages
}
echo "Called virtual machine management script with: $1" >> /var/log/messages
case "$1" in
start)
start
;;
stop)
stop
;;
restart|try-restart|condrestart|reload)
stop
start
;;
status)
status
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit $RETVAL
The system is CentOS 6.5. If I manually run service [start|stop|status] vtigervm
, it works as I expect it to. If the VM is running, $?
after the service vtigervm status
returns 0, and 3 if it is stopped. I installed it with chkconfig
and it created among a few others rc5.d/S99vtigervm
and rc0.d/K05vtigervm
.
The problem
When I start the system it starts the "service", but on shutdown it does not even run the scripts.
grep "virtual machine" /var/log/messages
shows:
*[machine starting]*
Called virtual machine management script with: start
Started virtual machine
*[shutdown -h now]*
*[machine stopped]*
What I expect:
*[machine starting]*
Called virtual machine management script with: start
Started virtual machine
*[shutdown -h now]*
Called virtual machine management script with: status
Queried virtual machine status
Called virtual machine management script with: stop
Stopped virtual machine
*[machine stopped]*
File permissions & etc:
# ls -lah /etc/rc0.d/
lrwxrwxrwx. 1 root root 13 7.2. 17:49 /etc/rc0.d/K05atd -> ../initd.d/atd
lrwxrwxrwx. 1 root root 18 9.2. 00:06 /etc/rc0.d/K05vtigervm -> ../initd.d/vtigervm