While running a number of virtual machines in LXC you might need gracefully shutdown each virtual machine while host reboot. Here is a script (file
/usr/local/sbin/lxc-shutdown):
#!/bin/sh
name=$1
timeout=15
if lxc-info -n $name | grep -qs "STOPPED"
then
echo $name not running...
exit 0
fi
ssh $name halt &
#if [ -e /usr/bin/lxc-halt ]; then
# /usr/bin/lxc-halt -n $name
#else
# ssh $name halt &
#fi
while [ $timeout -gt 0 ]
do
timeout=$(($timeout-1));sleep 1
if lxc-info -n $name | grep -qs "STOPPED"
then
exit 0
fi
done
lxc-stop -n $name
lxc-wait -n $name -s 'STOPPED'
This approach requires root to have password-less ssh login (see more
here). So now that you have a script that let you halt gracefully virtual machine, let make few changes to
/etc/init.d/lxc (somewhere around line 56):
# ...
stop)
log_daemon_msg "Stopping $DESC"
#action_all "lxc-stop -n"
# Uncomment below if you need to halt containers
# in reverse order
CONTAINERS=`echo $CONTAINERS | tac -s ' '`
action_all "lxc-halt"
;;
# ...
Use the following two commands to override lxc-shutdown for lxc v0.8+
update-alternatives --install /usr/bin/lxc-shutdown \
lxc-shutdown /usr/local/sbin/lxc-shutdown 1
update-alternatives --set lxc-shutdown \
/usr/local/sbin/lxc-shutdown