Skip to content

Setup NFS Shares on VirtualBox

Harsh Patel edited this page Sep 29, 2020 · 3 revisions

Setup NFS Shares on VirtualBox

It is extremely useful to share a local code folder with a development container that has all the tools you need to work with that code. However, the shared folders feature of hypervisors like VMware, VirtualBox, etc. dramatically slows the reading/writing of files between the two environments. A really great alternative is to avoid the shared folders feature completely and instead share the local code directory on your laptop with development docker container via the unix NFS file sharing protocol. It turns out that this can be up to 5x faster than regular hypervisor shared folders. Below are the instructions on how to implement this between MacOS and VirtualBox. This can also be used as a guide for implementation between other environments.

On host MacOS:

  1. Check to see if you already have an /etc/exports file. If the file does not already exist then create it and give it the following ownership and permissions:

    -rw-r--r--  1 root  wheel  151 Feb 25  2015 /etc/exports
    
  2. Add the following line to your /etc/exports file:

    /Users/greg/Documents/code -alldirs -mapall=greg 127.0.0.1
    

    The /Users/greg/Documents/code is the directory that will be shared with the linux VM.

    The greg is the username that will be assigned as the owner of all new files created by the linux VM.

    The 127.0.0.1 IP address above assumes that you are using NAT networking for the VM. The host MacOS will only allow connections from this localhost IP.

  3. Next check to see if you already have an etc/nfs.conf file. If the file does not exist then create it and give it the following ownership and permissions:

    -rw-r--r--  1 root  wheel  43 Aug 22  2015 /etc/nfs.conf
    
  4. Add the following line to your etc/nfs.conf file:

    nfs.server.mount.require_resv_port = 0
    
  5. After changing either the /etc/exports or /etc/nfs.conf files, you must restart the nfs daemon with:

    $ sudo nfsd restart
    

On linux in VM:

  1. Verify that the nfs client is installed in the linux VM:

    $ cat /proc/filesystems
    

    If nfs is installed on the linux VM then you should see something like the following 3 lines (along with a lot of others) in the output:

    nodev    nfs
    nodev    nfs4
    nodev    nfsd
    

    If you don't see any nfs related filesystems then install the nfs-common package with the following:

    $ sudo apt-get install nfs-common
    

    Or, alternatively, you could install this package with a gui package manager like 'Synaptic Package manager' or others.

  2. Create a mount point directory where the shared directory on the host MacOS will appear in the linux VM:

    $ mkdir /home/rails/Documents/code
    

    The code directory should be empty. After mounting the shared directory, the contents of the directory on the host MacOS should appear within this directory in the linux VM.

    Note that you can choose where to put this mount point wherever you like.

  3. Mount the shared directory:

    $ sudo mount -t nfs -v -o vers=3,nolock,udp 10.0.2.2:/Users/greg/Documents/code /home/rails/Documents/code
    

    The 10.0.2.2 is the IP address of the host MacOS as seen from the linux VM. VirtualBox usually uses this IP.

    The /Users/greg/Documents/code is the directory being shared from the host MacOS and that appears in the etc/exports file.

    The /home/rails/Documents/code is the mount point in the linux VM.

  4. Test that the mount was successful by navigating out of and then to that directory then listing the files:

    $ cd ~
    $ cd /home/rails/Documents/code
    $ ls -la
    
  5. Make the nfs share automatically mount on startup of the linux VM by adding the following line to /etc/fstab:

    10.0.2.2:/Users/greg/Documents/code    /home/rails/Documents/code   nfs auto,noatime,nolock,bg,nfsvers=3,intr,tcp,actimeo=1800 0 0
    

    The 10.0.2.2:/Users/greg/Documents/code is the same IP and share directory on the host MacOS that we used in the mount command above.

    The /home/rails/Documents/code is the mount point that we created in the linux VM.

    This automatic mount can be tested by restarting the linux VM and then navigating to the mount point and listing the files.

Troubleshooting:

To verify that the nfs server is running on the host MacOS and that the linux VM is using the correct IP to access it, try the following:

$ sudo showmount -e 10.0.2.2

If everything is setup correctly then it should respond with the list of mounts being shared in the /etc/exports file on the host MacOS.


Try checking the status of the nfs mounts in the linux VM using:

$ nfsstat -m

If you get an access denied by server error while mounting

mount.nfs: access denied by server while mounting 192.168.64.1

make sure that /etc/exports ip address matches this

/Users/harshp/Code -alldirs -mapall=harshp 192.168.64.8

Clone this wiki locally