Allowing a VirtualBox VM to be run by ALL Users

Allowing a VirtualBox VM to be run by ALL Users

2014, Jan 16    

A few months ago we were tasked with allowing a user to use a Windows program on a Mac until the Macs can be recycled for PCs. Of course the first response was to buy PCs however this could not be done immediately. The next best thing was to use VirtualBox as it is a free Virtual Machine application for OS X. The challenge however was allowing the Virtual Machine to be run by any user on the Mac as by default, the virtual machine can only be run by the first user to run it as it sets ownership to that user for the virtual machine. Luckily, we deploy VirtualBox with Munki so I was able to create a deployable version that can be used by all users.

The first thing to do is create a preinstall script which will create the directories where the Virtual Machine will be stored in a shared directory.  I used /Users/Shared/. I create the configuration I needed I installed VirtualBox on my machine and configured it the way I wanted for a shared user environment. Then I used echo to create an XML file in the directory I created for the shared configuration. Listed below is my preinstall script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
/bin/test -d /Users/Shared/VirtualBox || mkdir /Users/Shared/VirtualBox/
/bin/test -d /Users/Shared/VirtualBox || mkdir /Users/Shared/VirtualBox/VirtualBox\ VMs
/bin/test -e /Users/Shared/VirtualBox/VirtualBox.xml || /bin/echo '<?xml version='1.0'?>
<!--
** DO NOT EDIT THIS FILE.
** If you make changes to this file while any VirtualBox related application
** is running, your changes will be overwritten later, without taking effect.
** Use VBoxManage or the VirtualBox Manager GUI to make changes.
-->
<VirtualBox xmlns='http://www.innotek.de/VirtualBox-settings' version='1.12-macosx'>
<Global>
<ExtraData>
<ExtraDataItem name='GUI/LastWindowPosition' value='895,325,770,550'/>
<ExtraDataItem name='GUI/SplitterSizes' value='153,610'/>
<ExtraDataItem name='GUI/UpdateCheckCount' value='2'/>
<ExtraDataItem name='GUI/UpdateDate' value='never'/>
</ExtraData>
<MachineRegistry/>
<MediaRegistry>
<HardDisks/>
<DVDImages/>
<FloppyImages/>
</MediaRegistry>
<NetserviceRegistry>
<DHCPServers>
<DHCPServer networkName='HostInterfaceNetworking-vboxnet0' IPAddress='192.168.56.100' networkMask='255.255.255.0' lowerIP='192.168.56.101' upperIP='192.168.56.254' enabled='1'/>
</DHCPServers>
</NetserviceRegistry>
<SystemProperties defaultMachineFolder='/Users/Shared/VirtualBox/VirtualBox VMs' defaultHardDiskFormat='VDI' VRDEAuthLibrary='VBoxAuth' webServiceAuthLibrary='VBoxAuth' LogHistoryCount='3'/>
<USBDeviceFilters/>
</Global>
</VirtualBox>' > /Users/Shared/VirtualBox/VirtualBox.xml

You will need to change < and > to < and > so that they can be used by echo within an XML file efficiently. Now we move on to the post install script. This script will set the permissions on this directory so the default VirtualBox behavior cannot occur which sets the owner of the Virtual Machine. Also with the upcoming release of OS X 10.10 our launchd.conf environment variable will not work. I have gone ahead and created a very simple launch agent that will use launchctl to set the variable each time a user logs in. This will ensure it works every time and is compatible with previous and future versions of OS X. Listed here is that install script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/sh</p>
/bin/chmod -R +a 'everyone allow delete,chown,list,search,add_file,add_subdirectory,delete_child,file_inherit,directory_inherit' /Users/Shared/VirtualBox
/bin/test -e /Library/LaunchAgents/edu.psu.educ.virtualboxhome.plist || /bin/echo '<?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>RunAtLoad</key>
<true/>
<key>Label</key>
<string>edu.psu.educ.virtualboxhome>/string>
<key>ProgramArguments</key>
<array>
<string>/bin/launchctl</string>
<string>setenv</string>
<string>VBOX_USER_HOME</string>
<string>/Users/Shared/VirtualBox</string>
</array>
</dict>
</plist>' > /Library/LaunchAgents/edu.psu.educ.virtualboxhome.plist
# You may not need this line if you never had a launchd.conf
/bin/test -e /etc/launchd.conf && /bin/rm -f /etc/launchd.conf

Once completed, You can install a virtual machine which can then be used by all users. The last line of the post install script is used to remove the old method if you ever used it. If not you can omit that line. If anyone has any additional questions about this please let me know.