VirtualBox: Sharing OSX host folder on your ubuntu guest

I finally resolved the last bit of annoyance of Virtualbox Related to sharing folders. So here are the steps

$ # Create a holder on your host osx
$ mkdir -p /websites/topsecretproject/trunk
$ # turn off the VM if its on or skip this step
$ VBoxManage controlvm 'ubuntu-x11' poweroff
$ # Share this folder via VBoxManage, ensure that your VM is turned off
$ VBoxManage sharefolder add <vmname> --name <name> --hostpath <path of the directory on host>
$ # Bootup on the virtual box as headless unless you need to use its gui as well
$ # Ignore this step and boot normally if you want to use the X Server as well on the guest, i dont
$ nohup VBoxHeadless -s 'ubuntu-x11' -v off &
$ # You would want to mount the share as owned by the user on the guest
$ # so you need to get information about the user first
rp@x11-vm:~/code$ id rp
uid=1000(rp) gid=1000(rp) groups=1000(rp),4(adm),20(dialout),24(cdrom),46(plugdev),105(lpadmin),119(admin),122(sambashare)

The last step is required to know the group id and user id to mount the share as so that the guest user would have the right set of permissions. Now open up the /etc/fstab and create the following entry. You would obviously need to change the share name and path based on your settings. Mine looks like this.

# mount the share on the host machine
code            /home/rp/code    vboxsf   defaults,uid=1000,gid=1000 0 0

Save the file and exit and then remount all the files with the following command

rp@x11-vm:~/$ mount -a
rp@x11-vm:~/code$ ls -l
total 4
-rw-r--r-- 1 rp rp 20 2010-07-14 11:05 test.php

This will remount all the shares (be careful if you have any other shares currently in use and now when you do an ls in the shared folder you will see all the files are owned by the user in guest and you would the required permissions automatically set.

One of the reasons why I had to go this route was that sshfs didn’t work very well and macfusion failed to mount (with or without keys) and I couldn’t get it work. Would be great to hear if any of you have had similar problems and what were the approaches that you took to fix it.

About rp

Architect for large, highly scalable LAMP applications and Technical Manager with special focus on metrics based continuous improvement of teams and products. Rajat has close to a decade of experience of a very wide range of skills related to infrastructure, middleware, app servers all the way to front-end technologies and software development methodologies including agile, iterative waterfall, waterfall as well as ah-hoc startup using the right approach in the right context to reduce time to market.