Persistent-on VM?

Here you can provide suggestions on how to improve the product, website, etc.
Post Reply
grepa
Posts: 2
Joined: 27. Feb 2017, 10:50

Persistent-on VM?

Post by grepa »

Hello,

If I want a guest machine to stay persistently on, is there a neater way of doing this than using a batch file which runs every few minutes?
The idea is that if a user mistakenly shuts a VM down while in a remote session, it should power on again swiftly thereafter.

No built-in functionality for 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: Persistent-on VM?

Post by socratis »

Not that I know of, on both counts; 1) the external script's necessity and 2) the feature not being there. Not that I remember a feature like that being asked for, to tell you the truth.

I expect our resident Windows script guru to chime in anytime with their own suggestions, anytime now... ;)

I hope you don't mind if I move this to the "Suggestions" forum from the "Windows hosts" one, since this suggestion is not / should not be restricted to Windows hosts.
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.
grepa
Posts: 2
Joined: 27. Feb 2017, 10:50

Re: Persistent-on VM?

Post by grepa »

Yeah, go on and move it, it would be a great feature to have! Thanks
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Persistent-on VM?

Post by scottgus1 »

One of the first things you might do is take away the ability to shut down the guest OS from within the guest. For Windows there's Group Policy Editor for Pro & higher (and placing the registry changes that Group Policy editor makes, in Home & lower) to remove Shut Down, Restart, Log Off, etc. from the Start menu. Google for how to prevent running Shutdown.exe, or for preventing access to the Run... box, Task Manager, and the command window. Also look for Kiosk Mode, which restricts use of the OS to the programs you want, like a library might do for a PC-based card catalog. Linux guests may have something similar.

A scripted running-tester might involve sampling the output of "Vboxmanage.exe showvminfo <vmname>", looking for "State: powered off" or "State: saved". If either is found, the guest has been turned off, and the script can attempt to start it again. This script will only look for Virtualbox-recognizable turn-offs.

For in-the-guest-OS failures, a heartbeat indicator script would either:

No Guest Additions: run a repeating script in the guest OS that makes a text file on a shared folder on the host, with another repeating script on the host that deletes said text file and waits for it to come back. If it doesn't come back after a while, assume the guest has failed and restart it.

With Guest Additions, do the same as the text file scripts except, instead of using a text file in a host shared folder, use "Vboxmanage.exe guestproperty get/set" in the host script and "Vboxcontrol.exe guestproperty get/set" in the guest. "Guestproperty get/set" uses a Guest Additions text passing function to alter and monitor text strings in the guest's .vbox file. See the manual, section 8.33
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: Persistent-on VM?

Post by socratis »

scottgus1 wrote:No Guest Additions: run a repeating script in the guest OS that makes a text file on a shared folder on the host
Unfortunately, you can't do that. Shared folders requires GAs. What you could do is to check for the VM's log file and if it's open by another process or not.
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.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Persistent-on VM?

Post by scottgus1 »

socratis wrote:Shared folders requires GAs
Sorry, should have been clearer. I was thinking of a real shared folder, not a GAs shared folder. If a full network is established between host and guest the text-file heartbeat could be run without GAs.

Checking for the log file being locked is also an interesting possibility, I'll have to look into that. I have run into situations though where the guest OS has crashed or the guest was told to reboot or shut down but Virtualbox missed the memo and didn't reset the guest environment. Monitoring running Virtualbox processes won't catch this situation, thus the possible need for an in-the-guest hearbeat process.

Of course a missed heartbeat has to have a sufficient time before the host script resets the guest, to allow for not mistaking a "skipped beat" as a total "heart attack".
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: Persistent-on VM?

Post by socratis »

I was thinking of the following scenario, the least common denominator: LiveCD, no VDI allocated, no GAs, no network for connectivity. The only consistent thing that I could come up with that would always be available to check, would be the VBox.log. That's it. Check for the log, either print the end of it and compare it with a known, or, check to see if another process has the file already open.
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.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Persistent-on VM?

Post by scottgus1 »

Ah, yes, such a guest would have no way to communicate, true.

I think there's often long periods of time when nothing gets written to the log: one guest I'm running now went from one entry at 37 seconds, to the next at 4 hours 20 mins 27 sec, then the next was 23 hours etc. Might not be able to get a reliable heartbeat by monitoring changes in the log.

But trying to save on top of an open vbox.log does cause an error, which would be catchable. Interesting idea!
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: Persistent-on VM?

Post by socratis »

Linux, OSX: "lsof" (read the manual first, too many options).
Win: MS's Openfiles.
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.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Persistent-on VM?

Post by scottgus1 »

Boy, I'm learning all sorts of new stuff today, thanks, Socratis!
Never heard of "openfiles" before. I tried it, seems to run OK on Windows 7, but needs admin privileges on 10. I have to reboot to turn on local monitoring to see how it works.

I did try command-line-copying a previous text file over a vbox.log on a running guest with the command prompt, and the copy completed without error despite the guest running, so no such testing in batch files, apparently. But VBscript does error out when trying to open the running guest's log file for writing:

Code: Select all

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")

On Error Resume Next
'try to open log file to write to it:
Set test = fso.OpenTextFile("K:\Win7vms\Win7vmB\Logs\VBox.log",ForWriting,True)
errornum = Err.Number
if errornum <> 0 Then 'error occured
	'error trying to overwrite active log, guest is running
	MsgBox "Guest Running"
Else
	'no error, log can be overwritten, guest has stopped
	MsgBox "Guest Stopped"
End If
test.Close
grepa2
Posts: 1
Joined: 16. Jan 2020, 12:37

Re: Persistent-on VM?

Post by grepa2 »

Three years later the feature is still not there, but I found a simple hack that's appliciable, without any complicated need to query the running process etc.

VboxHeadless.exe will relinquish control to a command shell if the running machine exits. Therefore you can do like this:

In my case I have a VM registrered in the VirtualBox system called Ubuntu Server.

Create a batch file (file ending .bat) on, for instance, your desktop, containing the following (indeed change install location of Virtualbox and name of VM as appliciable):

Code: Select all

:startvm
timeout 60
"C:\Program Files\Oracle\VirtualBox\VboxHeadless.exe" --startvm "Ubuntu Server"
goto startvm
(note: the timeout - delay 60 seconds - is just to "slow things down" a bit and not restart it immediately on exit - prevents what I call "racy loop condition")

Now if your system or server is set to auto-logon, you might just put that batch file in your startup folder and you should be done. However, much neater is to register the batch file in task scheduler, which will in practice make the machine an invisible service you cannot close by mistake, etc.

So, open task scheduler and create a basic task. Select to have the task run when the computer starts. Point to the batch file you created above. In the final step, choose to "open the properties dialog for this task when I click finish".

Here, choose to run the task whether user is logged on or not. Change user to the one that you would normally run VMs under and whose desktop is keeping the batch file you created. Finally, under the "settings" and "conditions" tabs, ensure everything is unchecked to prevent Windows from interfering with the process. Click OK and enter the credentials for the user.

Done, you are now hopefully running a VM as a kind of persistent service!
Post Reply