Page 1 of 1

[Solved] Starting VirtualBox from batch failing

Posted: 15. May 2018, 20:52
by MarkFoley
I have a backup job that shuts down the VM, does a backup of all VirtualBox files, then restarts the VM. Unfortunately, the restart does not work. The job is run from cron as root and is:

Code: Select all

# su -l mfoley --command "/usr/bin/VBoxManage startvm WIN7VM"
VBoxManage: error: The virtual machine 'WIN7VM' has terminated unexpectedly during startup because of signal 6
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component MachineWrap, interface IMachine
Waiting for VM "WIN7VM" to power on...
If I run this command as root from the command line, it works. What's going on?

Re: Starting VirtualBox from batch failing

Posted: 15. May 2018, 23:20
by socratis
"signal 6" is a libc memory allocation error AFAIK.

But I have a more "basic" question. When you run that command (why as root?) someone is logged in, there is a window manager responsible for actually drawing the VM. When you run that cron job, where's that VM going to be displayed? Is anyone logged in?

How about if you try either of the following commands:
  • su -l mfoley --command "VBoxManage startvm WIN7VM --type headless"
    su -l mfoley --command "VBoxHeadless --startvm WIN7VM"
Also, from ch. 2.3.5 Starting VirtualBox on Linux:
All VirtualBox applications (VirtualBox, VBoxSDL, VBoxManage and VBoxHeadless) require the VirtualBox directory to be in the library path:
  • LD_LIBRARY_PATH=. ./VBoxManage showvminfo "Windows XP"

Re: Starting VirtualBox from batch failing

Posted: 16. May 2018, 02:40
by MarkFoley
socratis wrote:"signal 6" is a libc memory allocation error AFAIK.

But I have a more "basic" question. When you run that command (why as root?) someone is logged in, there is a window manager responsible for actually drawing the VM. When you run that cron job, where's that VM going to be displayed? Is anyone logged in?
"Why root", because root is doing a system-wide backup, which includes backing up the VM. It first shuts down the VM via 'su mfoley --command "/usr/bin/VBoxManage controlvm WIN7VM savestate"'. Note that the script actually runs through the list of running VMs (in case there is more than one -- there isn't really, but just in case) shutting them down in the save state and logging which ones were shut down for later. After the backup is complted, the root cron job attempts to restart the VMs based on the log of ones shut down.

Yes, there is a user logged in on Linux. I am using KDE and if the user running the VM logs out it shuts down the VM, in which case the root cron job won't find a VM to shutdown and hence won't attempt to start it. I am expecting the VM to be displayed on that logged in user's screen.

When, as that logged in normal user, I 'su' to root and run the startvm, it fires up just fine. Since I'm doing that as root, why is it any different that root doing that in a cron script? This particular restart VM error is consistent with 3 VMs on 3 different Linux workstations.
How about if you try either of the following commands:
  • su -l mfoley --command "VBoxManage startvm WIN7VM --type headless"
    su -l mfoley --command "VBoxHeadless --startvm WIN7VM"
Well, I don't want it to be headless. The idea is to backup production workstations and have the user none the wiser the next morning, especially if lots of windows with unsaved files are left open at end of day, which is typical!
Also, from ch. 2.3.5 Starting VirtualBox on Linux:
All VirtualBox applications (VirtualBox, VBoxSDL, VBoxManage and VBoxHeadless) require the VirtualBox directory to be in the library path:
  • LD_LIBRARY_PATH=. ./VBoxManage showvminfo "Windows XP"
OK, this gives me a clue! I was wondering if something needed to be set in the environment. Unfortunately, LD_LIBRARY_PATH is not set even with an interactive logon, but I'll try listing the ENV in both interactive and cron modes and see if there is some difference. (to be honest, I'm not sure what that env set command does. When I run 'LD_LIBRARY_PATH=. VBoxManage showvminfo WIN7VM' LD_LIBRARY_PATH is not set to anything)

Re: Starting VirtualBox from batch failing

Posted: 16. May 2018, 11:51
by Martin
MarkFoley wrote:When, as that logged in normal user, I 'su' to root and run the startvm, it fires up just fine. Since I'm doing that as root, why is it any different that root doing that in a cron script?
If I remember correctly the runtime environment of cron doesn't have input/output access (and no way to access the gui).

Re: Starting VirtualBox from batch failing

Posted: 17. May 2018, 05:57
by MarkFoley
I just tried this again using the PATH setting of the login user in the cron script. It didn't make any difference, same error.
Martin wrote:If I remember correctly the runtime environment of cron doesn't have input/output access (and no way to access the gui).
To test that idea, I logged into the host from a remote computer and su'd to root. I.e. I did not su from the local user to whom the VM belongs and who does have a KDE GUI session. The result was the same error, as if run from the cron job.

So, after the local user, with a KDE/GUI session, opens a terminal window, su's to root and runs 'su -l userId --command "/usr/bin/VBoxManage startvm vmname" it will work, but not if run from root with no GUI session. This seems to back your theory.

Is there therefore no way for a root cron job to start a VM on behalf of a user who DOES have a GUI session?

Re: Starting VirtualBox from batch failing

Posted: 17. May 2018, 08:07
by socratis
MarkFoley wrote:Is there therefore no way for a root cron job to start a VM on behalf of a user who DOES have a GUI session?
It would be best if that question was asked in a Linux related forum, you'll have better chances. Not that no one will know here, but the expertise of this forum are mostly VirtualBox based...

Re: Starting VirtualBox from batch failing

Posted: 17. May 2018, 09:38
by andyp73
MarkFoley wrote:Is there therefore no way for a root cron job to start a VM on behalf of a user who DOES have a GUI session?
Thinking aloud...

Can you not set up two per user cron jobs one to suspend the VM and the other to restart it? That then gives you a time window in which your root job can do the backup. You might also need to use .bash_login and .bash_logout to create lock files which you can check for the presence (or lack) of in order to synchronise things.

-Andy.

Re: Starting VirtualBox from batch failing

Posted: 18. May 2018, 05:59
by MarkFoley
andyp73 wrote:Thinking aloud...

Can you not set up two per user cron jobs one to suspend the VM and the other to restart it? That then gives you a time window in which your root job can do the backup. You might also need to use .bash_login and .bash_logout to create lock files which you can check for the presence (or lack) of in order to synchronise things.
I did try that. I created a cron job for the same KDE/GUI user (not root) who was logged into the desktop the whole time. It failed with the same error:

Code: Select all

VBoxManage: error: The virtual machine 'WIN7VM' has terminated unexpectedly during startup because of signal 6
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component MachineWrap, interface IMachine
I will do as socratis suggested and post this question on Linuxquestions.org. In the meantime, if anyone on this forum knows what might cause an "NS_FAILURE component MachineWrap interface IMachine". A signal 6 is
A process dying with this signal is usually due to it calling the abort(3) function. That generally indicates that the process itself has found that some essential pre-requisite for correct function is not available and voluntarily killing itself, ...
These are errors generated by VBoxManage.

Re: Starting VirtualBox from batch failing

Posted: 18. May 2018, 08:27
by Martin
Unfortunately "code NS_ERROR_FAILURE (0x80004005)" is just a generic "something has gone wrong" error message.

Re: Starting VirtualBox from batch failing

Posted: 20. May 2018, 20:57
by MarkFoley
Found the answer:

Code: Select all

DISPLAY=:0 backupVM.sh
where backupVM.sh is the backup script containing the 'VBoxManage startvm'. I've also tried this in the crontab:

Code: Select all

30 1 1,15 * * /bin/bash -c "DISPLAY=:0 $HOME/backupVM >> $HOME/backupVM.log 2>&1"
and I've tried it as both the VM owner/user and as root. It works both ways. Now I can back up users' VMs after hours and they'll be non-the-wiser when they come in the next day!

Re: Starting VirtualBox from batch failing

Posted: 21. May 2018, 00:03
by socratis
The DISPLAY! Makes sense!
Glad you got it going and thanks for the feedback!
Marking as [Solved].