Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to mount /Users with nfs instead of vboxsf in boot2docker
#!/bin/bash
#
# This script will mount /Users in the boot2docker VM using NFS (instead of the
# default vboxsf). It's probably not a good idea to run it while there are
# Docker containers running in boot2docker.
#
# Usage: sudo ./boot2docker-use-nfs.sh
#
if [ "$USER" != "root" ]
then
echo "This script must be run with sudo: sudo ${0}"
exit -1
fi
# Run command as non root http://stackoverflow.com/a/10220200/96855
B2D_IP=$(sudo -u ${SUDO_USER} boot2docker ip &> /dev/null)
if [ "$?" != "0" ]
then
sudo -u ${SUDO_USER} boot2docker up
$(sudo -u ${SUDO_USER} boot2docker shellinit)
B2D_IP=$(sudo -u ${SUDO_USER} boot2docker ip &> /dev/null)
#echo "You need to start boot2docker first: boot2docker up && \$(boot2docker shellinit) "
#exit -1
fi
OSX_IP=$(ifconfig en0 | grep --word-regexp inet | awk '{print $2}')
MAP_USER=${SUDO_USER}
MAP_GROUP=$(sudo -u ${SUDO_USER} id -n -g)
RESTART_NFSD=0
EXPORTS_LINE="/Users -mapall=${MAP_USER}:${MAP_GROUP} ${OSX_IP}"
grep "$EXPORTS_LINE" /etc/exports > /dev/null
if [ "$?" != "0" ]
then
RESTART_NFSD=1
# Backup exports file
$(cp -n /etc/exports /etc/exports.bak) && \
echo "Backed up /etc/exports to /etc/exports.bak"
# Delete previously generated line if it exists
grep -v '^/Users ' /etc/exports > /etc/exports
# We are using the OS X IP because the b2d VM is behind NAT
echo "$EXPORTS_LINE" >> /etc/exports
fi
NFSD_LINE="nfs.server.mount.require_resv_port = 0"
grep "$NFSD_LINE" /etc/nfs.conf > /dev/null
if [ "$?" != "0" ]
then
RESTART_NFSD=1
# Backup /etc/nfs.conf file
$(cp -n /etc/nfs.conf /etc/nfs.conf.bak) && \
echo "Backed up /etc/nfs.conf to /etc/nfs.conf.bak"
echo "nfs.server.mount.require_resv_port = 0" >> /etc/nfs.conf
fi
if [ "$RESTART_NFSD" == "1" ]
then
echo "Restarting nfsd"
nfsd update 2> /dev/null || (nfsd start && sleep 5)
fi
sudo -u ${SUDO_USER} boot2docker ssh << EOF
mount | grep nfs > /dev/null && \
echo "/Users already mounted with NFS" && \
exit
echo "Unmounting /Users"
sudo umount /Users 2> /dev/null
echo "Starting nfs-client"
sudo /usr/local/etc/init.d/nfs-client start 2> /dev/null
echo "Mounting /Users"
sudo mount $OSX_IP:/Users /Users -o rw,async,noatime,rsize=32768,wsize=32768,proto=tcp,nfsvers=3
echo "Mounted /Users:"
ls -al /Users
exit
EOF
@neilbartley

This comment has been minimized.

Copy link

commented Apr 16, 2015

Thanks very much for this. I made a minor update (see my forked version) which was mentioned my kpsmith in a different Gist.

This just resolves the following error:

mount: RPC: Authentication error; why = Client credential too weak
@brikis98

This comment has been minimized.

Copy link

commented May 7, 2015

@neilbartley: I hit the same error and your forked script fixed it for me. Thanks for that.

Unfortunately, even with NFS, mounted drives are still unusably slow with Docker and Boot2Docker. I'm still looking for a solution, so if anyone has any ideas, let me know :)

@olalonde

This comment has been minimized.

Copy link
Owner Author

commented May 23, 2015

Should be fixed in latest revision.

@notDavid

This comment has been minimized.

Copy link

commented Jul 9, 2015

In case it's useful to anyone else, i use this to determine the correct local ip instead of always using en0 (on line #29) :

OSX_IP=$(ifconfig -m `route get 8.8.8.8 | awk '{if ($1 ~ /interface:/){print $2}}'` | awk 'sub(/inet /,""){print $1}')
@olalonde

This comment has been minimized.

Copy link
Owner Author

commented Jul 15, 2015

I just updated the script to make it faster (e.g. won't wait 10s if nfsd already running or /Users already mounted with nfs).

@david-martin

This comment has been minimized.

Copy link

commented Jul 17, 2015

Thanks @DavidStaron. OSX_IP wasn't been set, but your suggestion works great

@benjamin-smith

This comment has been minimized.

Copy link

commented Jul 18, 2015

+1 @DavidStaron

@olalonde

This comment has been minimized.

Copy link
Owner Author

commented Aug 13, 2015

I ported this script to docker-machine in case anyone wants to switch: https://gist.github.com/olalonde/74de9d053448b977f77e

@KeremTubluk

This comment has been minimized.

Copy link

commented Oct 5, 2015

Great script! Here is my take on it; it uses a little know fact that if you mount a volume on a preexisting folder with subfolders the new mount takes precedence. So the boot2docker guest still have access to required volumes under /Users.

The changes persist between reboots of the boot2docker guest as long as VirtualBox keeps the ip (have never changed for me, but a possibility).

I have also successfully tested this while using folders other then /Users/~/. The script shares the current folder with the boot2docker guest so if you need multiple folders just run it in a parent folder.

https://gist.github.com/KeremTubluk/cf6a5fffa39aca3431b0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.