Hibernate host OS with running VirtualBox

From PCLinuxOSHelp Knowledge Base
Jump to: navigation, search

For this page, we assume that the host OS is PCLinuxOS and the guest OS is any OS running on VirtualBox. When there is a virtual machine is running hibernating the host os, either manually or automatically because of low battery in laptops, may cause the hibernation process to hang. To avoid this situation, we have to make sure all the running virtual machines are saved before hibernating the host OS.

Here are the steps to achieve this.

Setup

1. Create a script with below lines of code to save running virtual machines under all logged in users. Let us call it saveRunningVBoxes.sh . THIS SCRIPT WILL NOT LET THE HIBERNATION PROCEED FURTHER UNTIL ALL THE VIRTUAL BOXES ARE SAVED. IF THE MACHINE STAYS IN LOCK SCREEN LONGER THAN EXPECTED, PLEASE SAVE THE RUNNING VIRTUAL BOXES MANUALLY WHICH WILL ALLOW THIS SCRIPT TO PROCEED FURTHER.

#!/bin/bash
#
# output from this script are saved in /var/log/vm-suspend.log

echo `date` executing $0 $* >> /var/log/vm-suspend.log

export EVENT=$1

# This file will execute with following parameters and events
# hibernate hibernate - when the system hibernates
# thaw hibernate - when the system comes back from hibernation
# suspend suspend - when the system suspends to RAM or sleeps
# resume suspend - when the system comes back from suspension

# below condition checks if the execution is because of hibernation initiation
if [ "$EVENT" == "hibernate" ]
then
  echo hibernating VMs... >> /var/log/vm-suspend.log

# below 2 lines make sure all the virtual box instances are saved before proceeding.
# if this script fails to save all the running virtual boxes, the system will stay in the lock screen and we can manually save the virtual boxes and the script will automatically continue further
  export vboxcount=`ps -ef | grep -i virtualbox | grep -v grep | wc -l`
  while [ $vboxcount -gt 0 ]
  do
     echo found running VMs... >> /var/log/vm-suspend.log

# loops through all the logged in users
     for usr in `users`
     do

# loops through all running virtual box instances and saves the state
        for vm in `su - $usr -c "VBoxManage list runningvms" | cut -d " " -f 2`; do echo saving $usr $vm >> /var/log/vm-suspend.log; su - $usr -c "VBoxManage controlvm $vm savestate";echo saved $usr $vm >> /var/log/vm-suspend.log; done
     done
     export vboxcount=`ps -ef | grep -i virtualbox | grep startvm | wc -l`
  done
fi
echo `date` executed $0 $* >> /var/log/vm-suspend.log

2. As root user save the script saveRunningVBoxes.sh under directory /etc/pm/sleep.d/ and set execute permission by executing below command as root

chmod +x /etc/pm/sleep.d/saveRunningVBoxes.sh

Verification

Now lets test the script.

3. Run the script as root without any virtual box running and check the log file /var/log/vm-suspend.log

4. Run the scirpt as root with virtual box running and check the log file /var/log/vm-suspend.log

5. Hibernate the machine with virtual box running and check the log file /var/log/vm-suspend.log after recovering from hibernation

6. Suspend/sleep the machine with virtual box running and check the log file /var/log/vm-suspend.log after recovering from suspension