HOWTO: VirtualBox Folder Sharing (OS X Host, FreeBSD Guest)

Folder Sharing Between OS X and FreeBSD on VirtualBox

Objective: Share files between my OS X host and FreeBSD VirtualBox guest. Just for kicks, I wanted to synchronize this folder online, so I added it to my Dropbox as well.

Potential Applications:
  • Backups
  • Transferring config files
  • Quickly sharing files to Dropbox (since Dropbox is not officially supported on FreeBSD)

Note: User @kpa has pointed out that this method should work fine as long as you only have one user account that is common between the OS X host and the FreeBSD client. Having more users would require you to find a way to sync those user accounts, perhaps with LDAP.

Basic information about my setup:
Code:
MacBook Pro 8,2 (Early 2011)
Mac OS 10.9.1 "Mavericks"
VirtualBox 4.3.6
FreeBSD 9.2-RELEASE
OS X username: lion
FreeBSD username: lion
I've provided my sources at the end of this document. They're cited throughout the text with red numbers in brackets like this [source].

First Steps

Folder sharing is currently not supported by the VirtualBox-OSE guest additions, so, with the help of some users here on the forums, I used NFS to get around that limitation. Here's how you can do the same.

First, I created a folder called "BSDfiles" and saved it in my DropBox folder. The path was then /Users/lion/Dropbox/BSDfiles. This is the folder we want to share.

For networking, I used the NAT option for my FreeBSD guest in VirtualBox settings. The reason for this is that the IP for the VirtualBox host is always 10.0.2.2 and the guest is always 10.0.2.15 when they access each other [1], making them easier for me to remember rather than running ifconfig whenever something changes. Also (if I understand this correctly), using NAT instead of Bridged Network will allow me to change Wi-Fi networks without any consequences to the shared folders because all connections remain within the Mac.

Setting up the NFS Server on OS X

On OS X, edit the file /etc/exports and nfsd will start automatically [2]. The file did not already exist on my machine, so I just created it:
# vim /etc/exports
In this file, the syntax is basically:
Code:
<shared folder> <options> <client IP>
For my setup, that translated to the following:
Code:
/Users/lion/Dropbox/BSDfiles -mapall=501:20 -alldirs 127.0.0.1
  • The -mapall=501:20 option means any users in the guest (FreeBSD) that change files in the shared folder will be treated as your normal user on OS X [2]. 501 refers to the UID and 20 refers to the GUID. To find out your user information on OS X, run the id command in your terminal.
  • The -alldirs option allows users in the guest to mount any directory within the shared folder [2]. I added this option just in case I decide to add folders within BSDfiles and mount them directly, so -alldirs might not be necessary.
  • Using 127.0.0.1 instead of the VirtualBox guest IP (10.0.2.15) shares only to the loopback and gives access to all of your VMs with NAT networking [3].

There is one more file to edit on OS X. In /etc/nfs.conf, add the following line:
Code:
nfs.server.mount.require_resv_port = 0
Then let NFS know you've made a change by running
# nfsd update
Otherwise you won't be able to connect. See link [3] below for more information.

Setting up the NFS Client on FreeBSD

FreeBSD is pretty great for this because it's got all you need built in. Here are a few lines to add to your /etc/rc.conf:
Code:
nfs_client_enable="YES"
  • Code:
    nfs_client_enable="YES"
    will, as you might expect, enable the NFS client [4]
  • The next three lines are there because word on the ArchWiki is that NFS will cause some delays in your system's time [5]. I noticed that my clock was off as soon as I started using NFS, but using NTP fixed it. The
    Code:
    ntpd_sync_on_start="YES"
    line will do exactly that, taking over functionality from ntpdate [6]. Using NTP means you don't need VirtualBox's time syncing feature [7]
    [*]UPDATE: There's no need to use NTP since VirtualBox timesync works just fine even when you're using NFS. Just add the line above.

Then, from your FreeBSD guest, start the nfs_client service with this command [4]:
# service nfsclient restart
I'm not sure why it says restart in the Handbook, so if that doesn't work just try # service nfsclient start.

Decide which folder you'll use as a mount point (/mnt/share in this case), and run the following command to mount your shared folder. Remember to edit the path name for the location of your own shared folder.
# mount 10.0.2.2:/Users/lion/Dropbox/BSDfiles /mnt/share

To see if it worked, put a file (I just used a .txt) in the shared folder from OS X. Then ls from FreeBSD to see if it's there:
# ls /mnt/share
If your text file is there, it worked! Permissions should be alright since we used the option -mapall=501:20 in /etc/exports earlier. Files in your shared folder show up as owned by "staff" in FreeBSD, and editing them will be equivalent to editing them as your normal user on OS X.

Since I like to access my shared folder as a normal user, I logged into my normal user on FreeBSD and made the following symlink for convenience:
% ln -s /mnt/share /usr/home/lion/share

Test it a few times to make sure you can create files from FreeBSD and edit them from OS X and vice versa.

Now, rather than running that long command every time we boot up FreeBSD, we can edit the /etc/fstab file to have our shared folder mount at boot [4]. Add the following line:
Code:
10.0.2.2:/Users/lion/Dropbox/BSDfiles /mnt/share nfs rw 0 0
  • Edit the path name as necessary.

Reboot your FreeBSD guest, and your shared folder should be mounted!

Sources
[1] https://forums.virtualbox.org/viewtopic.php?f=1&t=36592
[2] https://developer.apple.com/library/mac ... rts.5.html
[3] http://www.brianwcook.com/2013/02/servi ... albox.html
[4] http://www.freebsd.org/doc/en_US.ISO885 ... k-nfs.html
[5] https://wiki.archlinux.org/index.php/NFS
[6] viewtopic.php?&t=16295
[7] https://wiki.freebsd.org/VirtualBox

Please let me know if there's anything in this guide that's incorrect or that might cause problems in the future.

Thanks again to the forum users who helped me, the maintainers of FreeBSD documentation, and those who wrote the other articles I cited for providing the information I needed at the right time. I hope this howto helps someone else when they need it!
 
Last edited by a moderator:
Re: HOWTO: VirtualBox Folder Sharing (OS X Host, FreeBSD Gue

Thank you very much for that brilliantly written and structured tutorial. Sadly there is no best of list for tutorials by users at the moment ...
Previously I used fuse for my communication between Guest OS and HOST, but you have to set up and install that in Mavericks now (as it seems; it certainly works not by default). So I thought about nfs. Thank you very much again ....
 
Re: HOWTO: VirtualBox Folder Sharing (OS X Host, FreeBSD Gue

Thank you for the tutorial! I was looking for a solution like this for quite a bit. Why did I not think about NFS? And.. bingo! Works like a charm!
 
I have a MacOSX, installed Virtualbox and the guest OS is FreeBSD. I want to connect to the guest OS through the host OS. Please let me know what to do. I don't want to make the network bridged. I want to keep the network settings as NAT only.
 
Back
Top