Close

Pause all virtual machines before sleep

A project log for Silly software wishlist

Motivation to do some software projects by writing them down.

lion-mclionheadlion mclionhead 11/14/2021 at 00:430 Comments

Sleep mode in Linux works about 90% of the time, but virtualbox is the 1 program which crashes every time.

Suspend with a virtual machine running, resume with the virtual machine running, & your VDI image is gone for good.

 The leading idea is putting all the virtual machines in pause before suspending the host.  There's no way to pause all the virtual machines with a wildcard, but 1 VM at a time can be paused with:

VBoxManage controlvm 'The Grid' pause

In theory, this has to go in /etc/pm/sleep.d/10_virtualbox

#! /bin/sh


case "${1}" in
        hibernate|suspend)
            VBoxManage controlvm 'The Grid' pause
                ;;
        resume|thaw)
            VBoxManage controlvm 'The Grid' resume
                ;;
esac

All those files are run before a sleep or wake.  In reality, there's no consistency in the naming of the hibernate operation, so they have a bunch of random names in the switch statement.  The Virtualbox screen greys out a nanosecond before sleep to show it's working.  A sleep statement would make it more obvious.  


Helas, if the virtual machine isn't running, the script fails & the hibernate command aborts.  Another problem is new lines have to be added for every single virtual machine.

There's

VBoxManage list runningvms

to show all the running & paused virtual machines.

VBoxManage list vms

to show all the virtual machines. 

#!/usr/bin/python

# pause the virtual machines

import os
import sys
import subprocess

command = sys.argv[1]

text = subprocess.check_output(['VBoxManage', 'list', 'runningvms'])
lines = text.split('\n')
for i in range(len(lines)):
    words = lines[i].split(" {")
    if len(words[0]) > 0:
# strip the "
        name = ""
        for c in words[0]:
            if c != '\"':
                name += c
        print("'" + command + "'ing " + name)
        subprocess.call(['VBoxManage', 'controlvm', name, command])


It needs an increasingly sophisticated python script to handle all the corner cases.

Discussions