Fedora SystemD Clean Shutdown

Discussions related to using VirtualBox on Linux hosts.
Post Reply
wraeth
Posts: 1
Joined: 4. Apr 2013, 07:41
Primary OS: Fedora other
VBox Version: OSE Fedora
Guest OSses: Windows XP Pro, Windows Server 2008 R2, Sabayon Linux, Fedora Linux

Fedora SystemD Clean Shutdown

Post by wraeth »

Greetings;

I have been trying to get my virtual machines to run as a service to allow things like clean shutdowns during a reboot, however while I have been able to create the systemd unit file easily enough, VirtualBox Manager still reports the machine as 'Aborted' after a reboot.

I have searched the forums and found other instances where people have reported this, but nothing much was done with it, so I figured I'd post it as a dedicated topic to see if anyone was able to help.

My systemd unit file looks like this:

Code: Select all

[Unit]
Description=*vmname* Virtual Machine
After=network.target

[Service]
Type=simple
User=*vm_owner_name*
StandardOutput=journal
TimeoutSec=60
ExecStart=/usr/bin/VBoxHeadless --startvm *vmname* --vrde on
ExecStop=/usr/bin/VBoxManage controlvm *vmname* savestate

[Install]
WantedBy=multi-user.target
Looking in /var/log/messages after a reboot doesn't appear to be executing the ExecStop command during the system shutdown, though calling 'systemctl stop vm.service' works perfectly.

Does anyone have any suggestions?

Cheers;
wraeth
virtualhuman
Posts: 53
Joined: 22. Jan 2013, 22:11

Re: Fedora SystemD Clean Shutdown

Post by virtualhuman »

I know this is an old post from 2013, but I have the same problem and there are tons of posts from people asking the same thing on other forums.

Apparently, when VBoxManage executes with the parameter savestate, it returns immediately, even though the VM is still running and its in the process of saving its state.

This is a problem for systemd, because systemd expects the ExecStop VBoxManage command to do its own waiting (which VBoxManage does not do). Thus, systemd sees the live process of the VM and kills them. This results in a broken saved state, which makes the VM impossible to boot the next time, which in turn is only fixed by using the discardstate command.

The only solution I've found, is by using the following systemd script, which adds a 15 second delay after the savestate command (by using ExecStop sleep) and by forcing systemd to ignore live processes via the RemainAfterExit parameter. I also disable process killing via KillMode. Here is my script:

Code: Select all

[Unit]
Description=VBox 'myVM' Service
Requires=systemd-modules-load.service
After=systemd-modules-load.service vboxdrv.service

[Service]
Type=simple
Restart=no
User=myUser
Group=myUser
ExecStart=/usr/bin/VBoxHeadless -startvm 'myVM'
ExecStop=/usr/bin/VBoxManage controlvm 'myVM' savestate
ExecStop=/usr/bin/sleep 15
TimeoutSec=5min
KillMode=none
RemainAfterExit=true

[Install]
WantedBy=multi-user.target

I hope this is of some help to others with the same problem. Also, if you are starting VMs as root then you may remove the User and Group parameters, they are useful for starting headless VMs as a different user than root.
Post Reply