How to shut down or savetate VMs before the host is turned off by the UPS shutdown signal?

Discussions related to using VirtualBox on Linux hosts.
Post Reply
Cube3
Posts: 1
Joined: 30. Jul 2019, 06:31

How to shut down or savetate VMs before the host is turned off by the UPS shutdown signal?

Post by Cube3 »

Hello everyone! Sorry for my English, I used a translation tool

I am running multiple VMs (Virtualbox 5 and 6) on Debian 8 and **I need the VMs to shut down or savetate before the host is turned off by the UPS shutdown signal** (Or Poweroff buton or shutdown command).

I have created a script that starts the VMs as a service

/etc/systemd/system/vncservice.service

Code: Select all

    [Unit]
    Description=VBox Virtual Machine %i Service
    Requires=systemd-modules-load.service
    After=systemd-modules-load.service
    
    [Service]
    User=usuario
    Group=vboxusers
    ExecStart=/home/scripts/script.start.sh
    ExecStop=/home/scripts/script.stop.sh
    RemainAfterExit=true
    KillMode=none
    
    [Install]
    WantedBy=multi-user.target
Another that starts them

/home/scripts/script.start.sh

Code: Select all

    #!/bin/bash
    echo "Starting VMs...."
    export DISPLAY=:1
    /usr/bin/vboxmanage startvm VM1 --type gui
    /usr/bin/vboxmanage startvm VM2 --type gui
And another that turns them off

/home/scripts/script.stop.sh

Code: Select all

    #!/bin/sh -
    export DISPLAY=:1
    echo "Apagando VMs...."
    /usr/bin/VBoxManage controlvm VM1 acpipowerbutton
    /usr/bin/VBoxManage controlvm VM2 acpipowerbutton
The script works on host power on, but it doesn't turn them off in time, the host shuts down before the VMs, I don't know how to delay the host shutdown

Any suggestions or another way to do this?
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: How to shut down or savetate VMs before the host is turned off by the UPS shutdown signal?

Post by socratis »

Cube3 wrote:it doesn't turn them off in time, the host shuts down before the VMs, I don't know how to delay the host shutdown
I don't see this as a VirtualBox problem, but as a host-shutdown-procedure problem. I'll say that I'm not Debian-anything master, I'm going to simply pose a couple of points to hopefully steer you in the right direction.

I assume that the UPS is receiving a power-down trigger and issues a shutdown command on the host. I'd start from there to see if you could modify the shutdown scripts/commands and add a delay of sorts, or a 'ps' control on the VBoxSVC, a conditional that checks for and waits until specific processes have ended.

I can think of a gazillion programs that have to terminate properly to avoid a corruption during shutdown, so there's got to be an option out there. For example a quick search for "debian UPS shutdown delay" returns (among other links) the wiki for apcupsd. I think that should be a very good starting point for you to work with.
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
Post Reply