Skip to content

Instantly share code, notes, and snippets.

@KeremTubluk
Last active October 2, 2017 14:01
Show Gist options
  • Save KeremTubluk/cf6a5fffa39aca3431b0 to your computer and use it in GitHub Desktop.
Save KeremTubluk/cf6a5fffa39aca3431b0 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# This script will create a fstab entry for the current folder in the boot2docker guest
# and mount the volume over exististing one if it exists. You can also use folders
# other then /User.
#
# Usage: ./nfs-share.sh
#
DIR=`printf "%q\n" "$(pwd)"`
MACHINE=`echo $DOCKER_HOST | perl -pe 's/^tcp:\/\/(\d{1,3}(\.\d{1,3}){3})\:2376$/$1/'`
BASEIP=`echo $MACHINE | cut -d"." -f1-3`
cat > bootlocal.sh <<EOF
#!/bin/sh
sudo /usr/local/etc/init.d/nfs-client start
sudo install -d -o docker -g staff -m 775 ${DIR}
sudo echo "$BASEIP.1:$DIR $DIR nfs rw,async,noatime,rsize=32768,wsize=32768,proto=tcp,nfsvers=3 0 0" | sudo tee -a /etc/fstab
sudo mount $DIR
EOF
docker-machine scp ./bootlocal.sh $DOCKER_MACHINE_NAME:/tmp/bootlocal.sh
rm bootlocal.sh
ssh docker@$MACHINE -i $DOCKER_CERT_PATH/id_rsa "sudo /bin/chmod +x /tmp/bootlocal.sh"
ssh docker@$MACHINE -i $DOCKER_CERT_PATH/id_rsa "sudo /bin/mv /tmp/bootlocal.sh /mnt/sda1/var/lib/boot2docker/bootlocal.sh"
docker-machine stop $DOCKER_MACHINE_NAME
HASH=`md5 -q -s "$MACHINE$DIR"`
if ! grep -q $HASH /etc/exports; then
GID=$(id -g)
echo "Appending to /etc/exports:"
cat > exports.tmp <<EOF
# DOCKER-BEGIN $HASH
"$DIR" $MACHINE -alldirs -mapall=$UID:$GID
# DOCKER-END $HASH
EOF
cat exports.tmp | sudo tee -a /etc/exports
rm exports.tmp
echo "Appending to /etc/nfs.conf:"
if ! sudo grep -q 'nfs.server.mount.require_resv_port' /etc/nfs.conf; then
echo "# Added by nfs-share.sh" | sudo tee -a /etc/nfs.conf
echo "nfs.server.mount.require_resv_port = 0" | sudo tee -a /etc/nfs.conf
fi
sudo nfsd restart
fi
docker-machine start $DOCKER_MACHINE_NAME
@david-resnick
Copy link

Thanks, helpful script!

I'm not sure how this will be for others, but the IP address my VM needed to access my mac was not .1 but the IP address for my bridge100 network interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment