running VBox on OS X Server

Discussions related to using VirtualBox on Mac OS X hosts.

Re: running VBox on OS X Server

Postby chip » 27. Apr 2009, 07:46

borgo1971 wrote:But... it's a windows tool. I want to shutdown Windows Server VM from Mac (we haven't any real PC in our company, only the VM running on our Mac Server)

If you're prepared to spend time installing Cygwin on your windows server, you'll be able to set up OpenSSH and SSH into your windows box from anything that has an SSH client (like an OS X machine). Once you're in, you can execute VBscripts with cscript, run .bat files or use the shutdown command to power down the system gracefully.
chip
 
Posts: 10
Joined: 6. Apr 2009, 06:32
Location: Ontario
Primary OS: Ubuntu other
VBox Version: PUEL
Guest OSses: Windows

Re: running VBox on OS X Server

Postby zeroG » 1. May 2009, 15:26

SlaunchaMan wrote:Have you tried using a LaunchDaemon to start the VBoxHeadless process? Here's an example:

That's stored as /Library/LaunchDaemons/edu.umich.sites.vm.plist and it runs the VM "Windows Vista Ultimate (32-Bit)" at startup as user slauncha. I haven't tested this on Mac OS X 10.5 Server, but it ought to work.


been struggling with this off and on for a week or so so i figured i'd post here. this seems to be the closest to my situation. i'm trying to get an ubuntu VM to startup on on mac at boot and cleanly save state on shutdown. the VM's going to run an personal web and email server (i.e., production). after a bit of searching (coming from a linux background, i was unfamiliar with init in leopard), i decided to try to make launchd work. using your example, i can start the VM but when launchctl unload is run, the machine is aborted. is that how you run your production boxes?

i found a couple of other links that talk about firing up a wrapper script with launchd that will then fire up vboxheadless and trap SIGTERM and, similarly, i can get that configuration to start but can't seem to find the right combination (sudo'ing, backgrounding the vboxheadless process, etc.) to catch the signal and issue a command to savestate. the basic idea looks like:

Code: Select all   Expand viewCollapse view
#!/bin/bash -v

# issue trap
trap shutdown SIGKILL SIGTERM SIGHUP SIGINT

function shutdown()
{
        echo "Shutting down VirtualBox"
        /usr/bin/VBoxManage controlvm tain savestate
        exit 0
}

echo "Starting VirtualBox"
# start vbox and wait for signal
/usr/bin/VBoxHeadless -s tain -vrdp=off


as i mentioned, i've tried different variations with this script: launching sub-shells to start/stop vboxheadless and "waiting" on that process, etc. but can't seem to get it right. hoping someone here will be able to get me pointed in the right direction.
zeroG
 
Posts: 7
Joined: 1. May 2009, 14:56
Primary OS: Mac OS X Leopard
VBox Version: PUEL
Guest OSses: Ubuntu

Re: running VBox on OS X Server

Postby SlaunchaMan » 1. May 2009, 16:35

zeroG wrote:is that how you run your production boxes?

No - we don't yet have production boxes. But when we do, we'll likely do something like what that script is doing. We'll just start the script with launchd. With clever use of the KeepAlive property, it'll be possible to prevent the VM from restarting when we don't want it to.
SlaunchaMan
 
Posts: 65
Joined: 6. Apr 2009, 22:28
Location: Detroit, MI
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Ubuntu 18.04

Re: running VBox on OS X Server

Postby zeroG » 1. May 2009, 16:56

SlaunchaMan wrote:
zeroG wrote:is that how you run your production boxes?

No - we don't yet have production boxes. But when we do, we'll likely do something like what that script is doing. We'll just start the script with launchd. With clever use of the KeepAlive property, it'll be possible to prevent the VM from restarting when we don't want it to.


ah. ok, that makes sense. thanks for the reply.

if you don't mind, i'd like to follow up a bit to help me conceptualize what the script needs to be doing (from launchd's perspective). launchd sends a SIGTERM to whatever process is spawned from the ProgramArguments spec, right? does it care if the script spawns additional processes or forks? should i be running vboxheadless in a subshell? does it matter? i'm kinda feeling my way around here - i'm a long-time linux admin and developer but not really a daemon expert. i did find 2 examples of how this can be done but neither one worked for me out of the box (one actually had some egregious errors in it).
zeroG
 
Posts: 7
Joined: 1. May 2009, 14:56
Primary OS: Mac OS X Leopard
VBox Version: PUEL
Guest OSses: Ubuntu

Re: running VBox on OS X Server

Postby SlaunchaMan » 1. May 2009, 19:25

zeroG wrote:launchd sends a SIGTERM to whatever process is spawned from the ProgramArguments spec, right?

I have yet to test this, but what I'll likely end up doing is trapping SIGTERM with a graceful shutdown of the VM. From the launchd.plist manpage, a daemon should catch SIGTERM at least.
zeroG wrote:does it care if the script spawns additional processes or forks? should i be running vboxheadless in a subshell? does it matter?

This does matter. Launchd doesn't want you to fork. That said, it's OK if you spawn other processes as long as your original process stays open. If, for instance, you were to call VBoxHeadless and run it in the background, your script would finish and launchd would restard it (if that's what it's designed to do). Here's a relevant portion of the manpage:
EXPECTATIONS
Daemons or agents managed by launchd are expected to behave certain ways.

A daemon or agent launched by launchd MUST NOT do the following in the process directly launched by launchd:

o Call daemon(3).
o Do the moral equivalent of daemon(3) by calling fork(2) and have the parent process exit(3) or _exit(2).

A daemon or agent launched by launchd SHOULD NOT do the following as a part of their startup initialization:

o Setup the user ID or group ID.
o Setup the working directory.
o chroot(2)
o setsid(2)
o Close "stray" file descriptors.
o Change stdio(3) to /dev/null.
o Setup resource limits with setrusage(2).
o Setup priority with setpriority(2).
o Ignore the SIGTERM signal.

A daemon or agent launched by launchd SHOULD:

o Launch on demand given criteria specified in the XML property list. More information can be found later in this man page.
o Catch the SIGTERM signal.

So how does this impact running VirtualBox? Here's a quick draft of a script to use:

Code: Select all   Expand viewCollapse view
#!/bin/bash

trap shutdown SIGTERM

function shutdown()
{
        /usr/bin/VBoxManage controlvm ${VMNAME} savestate
        exit 0
}

/usr/bin/VBoxHeadless --startvm ${VMNAME}


I haven't had time to test it but I'll probably do that this afternoon. I'll be sure to post my entire script, since I'm going to be doing much, much more. Our script will handle creation of VMs as well, so that we can push it out to all of our Macs and have them build Windows instead of having to push out a 30GB image. Instead, we'll push out a disk image that will boot them up and install Windows from a network share. So, our script will be smart enough to create a VM if it isn't there, create a hard drive if need be, and finally start the machine. It will stay open while VBoxHeadless runs, and stop the VM if it gets a SIGTERM.
SlaunchaMan
 
Posts: 65
Joined: 6. Apr 2009, 22:28
Location: Detroit, MI
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Ubuntu 18.04

Re: running VBox on OS X Server

Postby zeroG » 1. May 2009, 22:52

that's what i've tried but the script is not handling the SIGTERM correctly. i have the exact same script. when i start the service with launchctl, the vm comes up. when i try to unload it, launchctl hangs for a bit, the returns and the vm is aborted. here's what system.log says.

Code: Select all   Expand viewCollapse view
May  1 16:49:50 mini com.apple.launchd[1] (net.me[11452]): Exit timeout elapsed (20 seconds). Killing.
May  1 16:49:50 mini com.apple.launchd[1] (net.me[11452]): Stray process with PGID equal to this dead job: PID 11453 PPID 1 VBoxHeadless
May  1 16:49:50 mini com.apple.launchd[1] (net.me[11452]): Exited: Killed
May  1 16:49:51 mini kernel[0]: VBoxNetFlt: was detached from 'en1' (0)
zeroG
 
Posts: 7
Joined: 1. May 2009, 14:56
Primary OS: Mac OS X Leopard
VBox Version: PUEL
Guest OSses: Ubuntu

Re: running VBox on OS X Server

Postby SlaunchaMan » 2. May 2009, 05:53

I'm running into the same problem. I tried adding the ExitTimeOut key and setting it to 600 (five minutes) and it still didn't work. I guess I'll have to try more on Monday. Everything else in my script works.
SlaunchaMan
 
Posts: 65
Joined: 6. Apr 2009, 22:28
Location: Detroit, MI
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Ubuntu 18.04

Re: running VBox on OS X Server

Postby zeroG » 2. May 2009, 16:28

SlaunchaMan wrote:I'm running into the same problem. I tried adding the ExitTimeOut key and setting it to 600 (five minutes) and it still didn't work. I guess I'll have to try more on Monday. Everything else in my script works.


some progress. found this on bash:

If Bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes. When Bash is waiting for an asynchronous command via the wait builtin, the reception of a signal for which a trap has been set will cause the wait builtin to return immediately with an exit status greater than 128, immediately after which the trap is executed.


which explains what we were seeing. so i tried this:

Code: Select all   Expand viewCollapse view
    #!/bin/bash

    trap shutdown SIGTERM

    function shutdown()
    {
            /usr/bin/VBoxManage controlvm ${VMNAME} savestate
            exit 0
    }

    /usr/bin/VBoxHeadless --startvm ${VMNAME} &
    wait $!



and it appears to be behaving as expected.

i guess the only remaining question is whether savestate is the most appropriate option for controlvm. can't imagine it's harmful but a clean shutdown would also be nice. i've got ubuntu running in the vm. i wonder if there's any option that could passed in to ask ubuntu to shut itself down (soft power?)?
zeroG
 
Posts: 7
Joined: 1. May 2009, 14:56
Primary OS: Mac OS X Leopard
VBox Version: PUEL
Guest OSses: Ubuntu

Re: running VBox on OS X Server

Postby baf » 2. May 2009, 16:53

Try running the guest from the gui.
Then do ACPI shutdown either via host-U or menu machine->acpi shutdown or just by trying to close the window.
What happens does it shutdown nicely or give you a dialog ?
if a dialog it will have to be reconfigured in your window environment gnome? kde?

If nothing happens is ACPI enabled in VB? is acpid running in the guest?
Some say: "You learn as long as you live".
My way: "You live as long as you learn".
baf
Volunteer
 
Posts: 829
Joined: 27. Sep 2008, 06:18
Location: Luleå or Skellefteå, Sweden
Primary OS: Mac OS X Leopard
VBox Version: PUEL
Guest OSses: linux,xp,win7

Re: running VBox on OS X Server

Postby zeroG » 2. May 2009, 17:03

thanks, baf. meanwhile, i think i spoke too soon. while launchctl is happy testing the daemon from the command line, when i restarted, i found this in my system.log and no VM running:

Code: Select all   Expand viewCollapse view
May  2 10:34:01 mini tain[50]: VBoxHeadless: Error -1908 in suplibOsInit!
May  2 10:34:01 mini tain[50]: VBoxHeadless: Kernel driver not installed
May  2 10:34:01 mini tain[50]: VBoxHeadless: Tip! Make sure the kernel module is loaded. It may also help to reinstall VirtualBox.
May  2 10:34:01 mini com.apple.launchd[1] (tain[50]): Stray process with PGID equal to this dead job: PID 80 PPID 1 VBoxHeadless
May  2 10:34:01 mini com.apple.launchd[1] (tain[50]): Exited with exit code: 1


not quite sure where to go on this. googling that particular error leads right back to the .cpp source file. have to run but will look into it (and try your suggestion for clean shutdown) later this afternoon.
zeroG
 
Posts: 7
Joined: 1. May 2009, 14:56
Primary OS: Mac OS X Leopard
VBox Version: PUEL
Guest OSses: Ubuntu

Re: running VBox on OS X Server

Postby SlaunchaMan » 2. May 2009, 17:37

You might need to make sure that /Library/StartupItems/VirtualBox starts before your script. Try adding this line at the beginning of your script:

Code: Select all   Expand viewCollapse view
/Library/StartupItems/VirtualBox/VirtualBox start


And thanks for the tip on using wait; I've added that to my script and it seems to work pretty well. I'll post it now (when reading, know that Sites is the group I work for):

Code: Select all   Expand viewCollapse view
#!/bin/bash

##
# launch_vbox: Start a Sites Windows VM, creating it if necessary.
##

# Declarations

PATH=/bin:/usr/bin:/usr/sbin
VMName="Sites Windows"
VMOSType="WindowsVista"
HDDName="Windows"
HomeDir="$(dscl . -read /Users/$(whoami) NFSHomeDirectory | awk '{ print $2 }')"
VBoxFolder="${HomeDir}/Library/VirtualBox"
max_ram=2048
max_vram=64

# VM Settings
acpi=on
ioapic=on
pae=on
hwvirtex=on
nestedpaging=on
vtxvpid=on
monitorcount=1
accelerate3d=on
boot1=disk
boot2=dvd
boot3=none
boot4=none
dvdpassthrough=off
nic1=bridged
nictype1=82540EM
bridgeadapter1="en0: Ethernet"
audio=coreaudio
audiocontroller=ac97
clipboard=disabled
vrdp=on
usb=off

# HDD Settings
hdd_size=102400

# DVD Settings
dvd_folder="${VBoxFolder}/DVD Images"
dvd_filename=LiteTouchPE_x86.iso

stop_vm()
{
   echo "Received SIGTERM, saving VM state..."
   VBoxManage controlvm "${VMName}" savestate
   exit 0
}

create_vm()
{
   VBoxManage createvm --name     "${VMName}"   \
                       --ostype   "${VMOSType}" \
                       --register

   VBoxManage modifyvm "${VMName}" --memory          $(get_vm_memory)    \
                                   --vram            $(get_vm_vram)      \
                                   --acpi            ${acpi}             \
                                   --ioapic          ${ioapic}           \
                                   --pae             ${pae}              \
                                   --hwvirtex        ${hwvirtex}         \
                                   --nestedpaging    ${nestedpaging}     \
                                   --vtxvpid         ${vtxvpid}          \
                                   --monitorcount    ${monitorcount}     \
                                   --accelerate3d    ${accelerate3d}     \
                                   --boot1           ${boot1}            \
                                   --boot2           ${boot2}            \
                                   --boot3           ${boot3}            \
                                   --boot4           ${boot4}            \
                                   --hda             $(get_vm_hdd)       \
                                   --dvd             $(get_vm_dvd)       \
                                   --dvdpassthrough  ${dvdpassthrough}   \
                                   --nic1            ${nic1}             \
                                   --nictype1        ${nictype1}         \
                                   --bridgeadapter1  "${bridgeadapter1}" \
                                   --macaddress1     $(get_mac_addr)     \
                                   --audio           ${audio}            \
                                   --audiocontroller ${audiocontroller}  \
                                   --clipboard       ${clipboard}        \
                                   --vrdp            ${vrdp}             \
                                   --usb             ${usb}
}

get_vm_memory()
{
   # Look at the local machine's RAM and determine how much to give the VM
   tmpfile=/tmp/.system_profiler.$$
   system_profiler SPHardwareDataType > ${tmpfile} 2>&1

   local_mac_ram=$(($(grep -m 1 -i memory ${tmpfile} | \
                      awk '{ print $2 }') \
                         * 1024))

   if [ ${local_mac_ram} -gt $(($((${max_ram} * 2)) - 1)) ]; then
      echo ${max_ram}
   else
      echo $((local_mac_ram / 2))
   fi

   rm -f ${tmpfile}
}

get_vm_vram()
{
   # Look at the local machine's video memory and determine how much to
   # give the VM.
        tmpfile=/tmp/.system_profiler.$$
        system_profiler SPDisplaysDataType > ${tmpfile} 2>&1

        local_mac_vram=$(grep -m 1 -i vram ${tmpfile} | \
                    awk '{ print $3 }')

        if [ ${local_mac_vram} -gt $(($((${max_vram} * 2)) - 1)) ]; then
                echo ${max_vram}
        else
                echo $((local_mac_vram / 2))
        fi

   rm -f ${tmpfile}
}

get_mac_addr()
{
   # Will replace with better version that gets the right address so that
   # VMs get the right DHCP settings.
   echo 080027F122E0
}

get_vm_hdd()
{
   # If there's no hard disk, create one.
   if [ "$(get_hdd_uuid)" == "" ]; then
      # No hard disk. Create and register.
      VBoxManage createhd --filename "${HDDName}.vdi" \
                          --size     "${hdd_size}"    \
                          --format   VDI              \
                          --remember > /dev/null 2>&1
   fi

   echo "$(get_hdd_uuid)"
}

get_hdd_uuid()
{
   hdd_uuid="$(VBoxManage list hdds | grep -B 2 "${HDDName}" | \
               grep UUID | awk '{ print $2 }')"

   echo "${hdd_uuid}"
}

get_vm_dvd()
{
   # Make sure that the DVD is recognized by VirtualBox.
   if [ "$(get_dvd_uuid)" == "" ]; then
      # Load DVD into VirtualBox.
      VBoxManage openmedium dvd "${dvd_folder}/${dvd_filename}" \
           > /dev/null 2>&1
   fi

   echo $(get_dvd_uuid)
}

get_dvd_uuid()
{
   dvd_uuid="$(VBoxManage list dvds | grep -B 1 "${dvd_filename}" | \
               grep UUID | awk '{ print $2 }')"

   echo "${dvd_uuid}"
}

if [ "$(VBoxManage list vms | grep "${VMName}")" == "" ]; then
   # Need to create VM.
   create_vm
fi

# Trap stopping signal from launchd.
trap stop_vm SIGTERM

VBoxHeadless --startvm "${VMName}" &
wait $!


And here's the launchd plist:

Code: Select all   Expand viewCollapse view
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>Label</key>
   <string>edu.umich.sites.vm</string>
   <key>ProgramArguments</key>
   <array>
      <string>/private/etc/hooks/vbox/launch_vbox</string>
   </array>
   <key>RunAtLoad</key>
   <true/>
   <key>KeepAlive</key>
   <dict>
      <key>PathState</key>
      <dict>
         <key>/tmp/.DoNotRestartVM</key>
         <false/>
      </dict>
   </dict>
   <key>UserName</key>
   <string>vbox</string>
   <key>StandardOutPath</key>
   <string>/var/log/sites_vbox.log</string>
   <key>StandardErrorPath</key>
   <string>/var/log/sites_vbox.log</string>
   <key>ExitTimeOut</key>
   <integer>600</integer>
</dict>
</plist>


A few of things to note:
  • The get_vm_memory() and get_vm_vram() functions return either half of the machine's RAM or VRAM or the max_ram and max_vram values, whichever is lower.
  • The DVD ISO is something our Windows team gave me; when booted to, it loads Windows off of their servers and joins the machine to Active Directory.
  • I still need to write the get_mac_addr() function, which in its final state will get the correct MAC address for DHCP from some source. We'll be deploying this to several hundred Macs and each one is going to have a Windows VM in the background, but since they'll be in Active Directory they need to have their own IPs.
  • For security purposes, we're starting it as a new user I created, vbox. It doesn't matter who you run the script as, since it will automatically determine your home directory, but I'd advise against starting it as root (or even an admin user) just in case there are any VirtualBox exploits lurking around out there.
SlaunchaMan
 
Posts: 65
Joined: 6. Apr 2009, 22:28
Location: Detroit, MI
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Ubuntu 18.04

Re: running VBox on OS X Server

Postby zeroG » 2. May 2009, 20:52

thanks for the tip on starting up VirtualBox. i was hoping there'd be a HopefullyStartLast option but there wasn't. tried throwing a sleep 20 in there before calling VBoxHeadless but that didn't work. finally just added that line you mentioned and the VM started. got a bunch of errors from the daemon script though and figured it was because my launchd process is not running as root. so i added NOPASSWD to the admin group in the sudoers file and am sudo'ing to start Virtualbox. seems to be working. i am seeing this in the log right after the call to Virtualbox start:

Code: Select all   Expand viewCollapse view
May  2 14:17:00 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/VBoxDD2GC.gc => /Applications/VirtualBox.app/Contents/MacOS/VBoxDD2GC.gc-x86
May  2 14:17:01 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/VBoxDD2R0.r0 => /Applications/VirtualBox.app/Contents/MacOS/VBoxDD2R0.r0-x86
May  2 14:17:01 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/VBoxDDGC.gc => /Applications/VirtualBox.app/Contents/MacOS/VBoxDDGC.gc-x86
May  2 14:17:01 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/VBoxDDR0.r0 => /Applications/VirtualBox.app/Contents/MacOS/VBoxDDR0.r0-x86
May  2 14:17:01 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/VBoxHeadless => /Applications/VirtualBox.app/Contents/MacOS/VBoxHeadless-x86
May  2 14:17:01 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/VBoxManage => /Applications/VirtualBox.app/Contents/MacOS/VBoxManage-x86
May  2 14:17:01 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/VBoxNetAdpCtl => /Applications/VirtualBox.app/Contents/MacOS/VBoxNetAdpCtl-x86
May  2 14:17:01 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/VBoxNetDHCP => /Applications/VirtualBox.app/Contents/MacOS/VBoxNetDHCP-x86
May  2 14:17:01 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/VBoxSVC => /Applications/VirtualBox.app/Contents/MacOS/VBoxSVC-x86
May  2 14:17:01 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/VBoxXPCOMIPCD => /Applications/VirtualBox.app/Contents/MacOS/VBoxXPCOMIPCD-x86
May  2 14:17:01 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/VMMGC.gc => /Applications/VirtualBox.app/Contents/MacOS/VMMGC.gc-x86
May  2 14:17:01 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/VMMR0.r0 => /Applications/VirtualBox.app/Contents/MacOS/VMMR0.r0-x86
May  2 14:17:01 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/VirtualBox => /Applications/VirtualBox.app/Contents/MacOS/VirtualBox-x86
May  2 14:17:01 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/VirtualBoxVM => /Applications/VirtualBox.app/Contents/MacOS/VirtualBoxVM-x86
May  2 14:17:01 mini tain[50]: /Applications/VirtualBox.app/Contents/MacOS/vboxwebsrv => /Applications/VirtualBox.app/Contents/MacOS/vboxwebsrv-x86
May  2 14:17:01 mini tain[50]: -v Error: VBoxDrv.kext is already loaded
May  2 14:17:01 mini tain[50]: -v Error: VBoxUSB.kext is already loaded
May  2 14:17:01 mini tain[50]: -v Error: VBoxNetFlt.kext is already loaded
May  2 14:17:01 mini tain[50]: -v Error: VBoxNetAdp.kext is already loaded
May  2 14:17:01 mini tain[50]: -f VirtualBox


not sure if these errors are cosmetic. guessing they are. otherwise, seems ok.
zeroG
 
Posts: 7
Joined: 1. May 2009, 14:56
Primary OS: Mac OS X Leopard
VBox Version: PUEL
Guest OSses: Ubuntu

Re: running VBox on OS X Server

Postby borgo1971 » 4. May 2009, 21:46

OK, I started the thread, and I'll give you my solution. 8)
This is the launchd .plist that starts our VM:
Code: Select all   Expand viewCollapse view
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Disabled</key>
        <false/>
        <key>KeepAlive</key>
        <dict>
                <key>SuccessfulExit</key>
                <false/>
        </dict>
        <key>Label</key>
        <string>com.virtualbox.start-headless.vmname</string>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/bin/VBoxHeadless</string>
                <string>-s</string>
                <string>vmname</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>ServiceDescription</key>
        <string>VirtualBox Virtualization Engine</string>
        <key>StandardErrorPath</key>
        <string>/var/log/VBox.log</string>
        <key>UserName</key>
        <string>serveradmin</string>
</dict>
</plist>

This starts the VM and let it be shut downed gracefully.
To shutdown the VM, we have two ways: the first, shutting down window from inside the VM with a remote desktop connection (even as TS admin or VRDP), or launching this script manually as serveradmin before shutting down the Mac Server (this saves VM state):
Code: Select all   Expand viewCollapse view
#!/bin/bash
clear
vboxmanage controlvm vmname savestate
sleep 10
launchctl unload /Library/LaunchDaemons/com.virtualbox.start-headless.vmname.plist
exit

I lost a lot of time looking how to execute this script automatically before shutdown, but seems there's no way to start a script on Mac OS X shutdown. Truly, that's not serious problem because in any case (and only If I'm not in the office or I can't connect to the server) we need to log-in as administrator to gracefully shutdown the server, so I made a sticky in serveradmin account to remember to launch the vm-shutdown-script. This can be an unperfected way, but it works for us :mrgreen:
borgo1971
 
Posts: 24
Joined: 11. Feb 2009, 18:32
Location: Italy
Primary OS: Mac OS X Leopard
VBox Version: PUEL
Guest OSses: Windows Server 2003

Re: running VBox on OS X Server

Postby SlaunchaMan » 4. May 2009, 22:45

borgo1971 wrote:there's no way to start a script on Mac OS X shutdown.

Re-read the portion of the launchd.plist man page that talks about this. Launchd sends the SIGTERM signal to processes on shutdown. To run something on shutdown, catch that signal.
SlaunchaMan
 
Posts: 65
Joined: 6. Apr 2009, 22:28
Location: Detroit, MI
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Ubuntu 18.04

Re: running VBox on OS X Server

Postby zeroG » 19. Mar 2010, 22:59

coming back to this after a while. i wiped my leopard install for snow leopard and reinstalled the vb and vm. using the same scripts, i have the vm starting up just fine. however, i have a maddening problem - *sometimes* (haven't found any pattern here), although showvminfo says it's running, the vm is unreachable. running latest vb for snow leopard and ubuntu 64-bit server version with the paravirtualized network interface for the vm. i don't see anything in the logs other than the startup cpuid dumps and state changes.

anyone seen anything like this?
zeroG
 
Posts: 7
Joined: 1. May 2009, 14:56
Primary OS: Mac OS X Leopard
VBox Version: PUEL
Guest OSses: Ubuntu

PreviousNext

Return to VirtualBox on Mac OS X Hosts

Who is online

Users browsing this forum: No registered users and 19 guests