Headless Mode

Discussions related to using VirtualBox on Mac OS X hosts.

Headless Mode

Postby ajbrehm » 5. Nov 2009, 16:52

VMware Fusion 2 had a (officially unsupported) mode which allowed me to change the View of a running VM from "Fullscreen", "Window", or "Modality" (seamless mode) to "Headless", which basically disconnected the VM from the VMware Fusion viewer program and let it run in the background undisturbed. Then I could log out and back in and reconnect to my VMs by restarting VMware Fusion and doubleclicking on the running VMs in the list.

I hear VirtualBox has a Headless Mode, but can I switch to and from it without restarting the VM?

Also, which account does a VirtualBox VM run under? VMware VMs run as root and only the viewer runs under my user name. How does VirtualBox handle this?

Lastly, is there a war to configure VirtualBox to start up certain VMs, in the background/headlessly, when the host OS is booting up?

VMware Fusion 3 doesn't support Headless Mode anymore and hence requires me to rethink how I work with VMs. I have heard good things about VirtualBox and now that I am still waiting for my Windows 7 disc to arrive would be a good moment to switch to VirtualBox.

Any ideas/answers/comments/tips?
ajbrehm
 
Posts: 72
Joined: 5. Nov 2009, 16:46
Location: Dublin, Ireland
Primary OS: Mac OS X Leopard
VBox Version: OSE other
Guest OSses: Windows, Linux

Re: Headless Mode

Postby ae3265 » 8. Nov 2009, 06:53

A few tips on headless mode for mac.

1. Use the Display settings to set up a remote console. You need RDC to connect to these, MS's Remote Desktop Client or CoRD. Unfortunately, at this time, VirtualBox does not allow you to flip back and forth.

2. Getting VM's starting from boot with a launctl script is a bit of a pain. I found the easiest thing to do was to generate a self-signed code-signing certificate and key and codesign /usr/bin/VBoxHeadless. Between that and running the startup as root, I found I managed to get things going without the app being griped at about being an untrusted application trying to access the window manager. That may not be the best way, and there are some apple developer pages out there that talk about doing some other things. But I wasn't able to get those to work.

The second issue looks like it pretty much exists for other applications as well, you probably saw my posts on the VMware forums.

If I can remember everything I did, I'll try to post some kind of instructions. The tricky part is generating the certificate and key properly...as usual dealing with openssl is kind of a pain.
ae3265
 
Posts: 16
Joined: 28. Oct 2009, 17:48
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Solaris Windows

Re: Headless Mode

Postby ae3265 » 5. Dec 2009, 07:32

Okay, so here is my procedure for running headless VM's on boot without a user login for VirtualBox on Mac, Snow Leopard. Should be similar for Leopard.

1. Create a code signing certificate.

Launch Keychain Access, Select the Keychain Access menu, Certificate Assistant, Create a Certificate
Fill in a name, change the Certificate Type to "Codesigning", select the checkbox next to "Let me override defaults"
I up the validity period to 3650 days...up to you.
Click though the dialog until you get the point where it asks you what keychain to add it to. Add it to the "System" keychain. Find the certificate in Keychain access and set the trust to always allow for code signing.

2. Create startup scripts. Several ways to do this, here's what I did (as root "sudo su -":
create /etc/vbtab - fill in the names of the VM's you want to start, one per line
create /usr/local/sbin/start_vbox:

#!/bin/ksh
for BOX in `cat /etc/vbtab`
do
/usr/bin/VBoxHeadless -startvm ${BOX} &
done

create /usr/local/sbin/stop_vbox:

#!/bin/ksh
for BOX in `cat /etc/vbtab`
do
/usr/bin/VBoxManage controlvm ${BOX} acpipowerbutton
done

chmod those to 755

Make sure you have /var/root/Library/
I symlink /var/root/Library/VirtualBox back to my user home so whatever I create in the GUI I can startup with this...

ln -s /Users/ae3265/Library/VirtualBox /var/root/Library/VirtualBox

add this line to /etc/hostconfig:

VBSERVER=-YES-

create /Library/StartupItems/vbserver

in that directory, create:
StartupParameters.plist

{
Description = "vbserver";
Provides = ("vbserver");
Requires = ("VirtualBox");
OrderPreference = "Late";
Messages =
{
start = "Starting VM's";
stop = "Stopping VM's";
};
}

and:
vbserver

#!/bin/sh

##
# vbserver
##

. /etc/rc.common

StartService ()
{
if [ "${VBSERVER:=-NO-}" = "-YES-" ]
then
ConsoleMessage "Booting Virtual Servers"
/usr/local/sbin/start_vbox
fi
}

StopService ()
{
ConsoleMessage "Stopping Virtual Servers"
/usr/local/sbin/stop_vbox
}

RestartService ()
{
StopService
StartService
}

RunService "$1"

chmod 755 vbserver

3. Code sign /usr/bin/VBoxHeadless and /usr/bin/VBoxManage

cd /usr/bin
codesign -s "Name of Cert you used in step 1" VBoxHeadless
codesign -s "Name of Cert you used in step 1" VBoxManage

reboot to test.

Your VM's should launch on boot. And you can start/stop them with the /usr/local/sbin scripts as desired. If, for some reason, you don't want VM's to start on boot, change the VBSERVER line in /etc/hostconfig to:
VBSERVER=-NO-

And that should prevent the VM's from starting.

It would probably be more elegant to use LaunchAgents/LaunchDaemons, but this is a quick & dirty and I don't have to fiddle with launchctl to stop/start vm's. Also, they keep saying that /etc/hostconfig is going away some day. But I'll deal with that when the time comes.

The code signing thing allows VBoxHeadless and VBoxManage to run with out a login, if you skip this, then the login window manager will not let them run.
ae3265
 
Posts: 16
Joined: 28. Oct 2009, 17:48
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Solaris Windows

Re: Headless Mode

Postby ae3265 » 6. Dec 2009, 02:34

Okay, so for some reason, VBoxHeadless does not like to start as a regular user with something like "su - User" etc...might work with other variants of su, but this also works.

However, daemontools from macports (of djbdns fame) helps take care of that, specifically, setuidsetgid.

So, install daemontools from macports (or fink if you prefer, or from source, whatever floats your boat.)

In /Library/StartupItems/vbserver/vbserver from my scripts above add /opt/local/sbin/setuidgid User in front of the start/stop vm commands:

/opt/local/sbin/setuidgid User /usr/local/sbin/start_vbox

/opt/local/sbin/setuidgid User /usr/local/sbin/stop_vbox

Just make sure /etc/vbtab is readable by your user, chmod it 644, or modify paths accordingly.
ae3265
 
Posts: 16
Joined: 28. Oct 2009, 17:48
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Solaris Windows

Re: Headless Mode

Postby ae3265 » 7. Dec 2009, 10:36

Okay, so the final annoyance was getting things to shut down clean...and I think I have that under control.

So, basically, we're going to trash the stuff with /etc/hostconfig and /Library/Startup items and go with a combination of daemondo from macports and launchd. Oh, and this approach has the benefit of being able to run as a user account, so there's a little better integration with running headless and the GUI.

Here's what you need:

1. /etc/vbtab as in my previous post
2. /usr/local/sbin/start_vbox

#!/bin/ksh
for BOX in `cat /etc/vbtab`
do
/usr/bin/VBoxHeadless -startvm ${BOX} &
done

3. /usr/local/sbin/stop_vbox

#!/bin/ksh
trap "" SIGKILL SIGTERM SIGHUP SIGINT
RUNVM=`/usr/bin/VBoxManage list -l runningvms|grep Name|grep -v Storage|awk '{print $2}'`
for BOX in ${RUNVM}
do
/usr/bin/VBoxManage controlvm ${BOX} savestate
done

4. macports - install the correct package from http://www.macports.org/

5. /Library/LaunchDaemons/vbserver

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
<plist version='1.0'>
<dict>
<key>Label</key><string>vbserver</string>
<key>ProgramArguments</key>
<array>
<string>/opt/local/bin/daemondo</string>
<string>--label=vbserver</string>
<string>--start-cmd</string>
<string>/usr/local/sbin/start_vbox</string>
<string>;</string>
<string>--stop-cmd</string>
<string>/usr/local/sbin/stop_vbox</string>
<string>;</string>
<string>--pid=none</string>
</array>
<key>ExitTimeOut</key>
<integer>600</integer>
<key>Debug</key><false/>
<key>Disabled</key><true/>
<key>OnDemand</key><false/>
<key>RunAtLoad</key><true/>
<key>UserName</key>
<string>User</string>
</dict>
</plist>

Edit scripts to your taste if you want different paths...

launchctl load -w /Library/LaunchDaemons/vbserver

Will kick everything off.

launchctl unload /Library/LaunchDaemons/vbserver

Will turn it off. And you can start/stop your VMs outside of this without breaking anything.

I used savestate as it seems to process faster than acpipowerbutton. It's hard to tell from watching the consoles of the VM's if everything is getting enough time from launchd to completely shutdown, so this "should" be safer...YMMV...and wouldn't it be nice if Apple trashed the mess that is launchd and used Solaris SMF instead...jeesh!

BTW, this also works with Fusion, which for some reason is ignoring the powerType.powerOff = "soft" setting when launchd closes things down. Adjust your commands accordingly.

It would also be nice if VirtualBox had an option to catch SIGTERM from launchd and process the command of your choice (poweroff, suspend, acpipoweroff, etc). This would greatly simplify dealing with headless VM's on OS X.
ae3265
 
Posts: 16
Joined: 28. Oct 2009, 17:48
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Solaris Windows


Return to VirtualBox on Mac OS X Hosts

Who is online

Users browsing this forum: No registered users and 23 guests