Last active
August 29, 2015 14:26
-
-
Save rtfpessoa/95ec7effc6c2cafbad48 to your computer and use it in GitHub Desktop.
boot2docker init script for OS X
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# boot2docker init OS X | |
# | |
FORCE=${1-true} | |
USERS="/Users" | |
VAR="/var/folders" | |
TMP="/tmp" | |
DIRS=("$USERS" "$VAR" "$TMP"); | |
USERID="2004" | |
GROUPID="50" | |
VM="boot2docker-vm" | |
DOCKER_MACHINE="boot2docker" | |
VBOXMANAGE="vboxmanage" | |
BLUE='\033[0;34m' | |
GREEN='\033[0;32m' | |
NC='\033[0m' | |
unset DYLD_LIBRARY_PATH | |
unset LD_LIBRARY_PATH | |
if [ ! -f $DOCKER_MACHINE ] || [ ! -f $VBOXMANAGE ]; then | |
echo "Either VirtualBox or boot2docker are not installed. Please install them and try again." | |
exit 1 | |
fi | |
# If no boot2docker vm is found, inititialize a new instance | |
if [ $($VBOXMANAGE list vms | grep -ci $VM) -ne 1 ]; then | |
echo "No boot2docker VM found, initializing a new instance." | |
boot2docker init | |
fi | |
# Start the vm if it is powered off and set shellinit | |
if [ $($VBOXMANAGE list runningvms | grep -ci $VM) -ne 1 ]; then | |
echo "The boot2docker VM is not running, starting now." | |
if [ $(boot2docker start | grep -ci "Started.") -eq 1 ]; then | |
$(boot2docker shellinit) | |
else | |
echo "Error starting boot2docker VM" | |
exit 1 | |
fi | |
fi | |
echo "Setting environment variables for machine $VM..." | |
eval $(boot2docker shellinit) | |
function umount { | |
DIR=$1 | |
WORKDIR=$2 | |
echo "$DIR mount deteted, unmounting now." | |
$DOCKER_MACHINE ssh "sudo umount $DIR" | |
$VBOXMANAGE sharedfolder remove $VM --name "$WORKDIR" --transient | |
} | |
function mount { | |
DIR=$1 | |
WORKDIR=$2 | |
$DOCKER_MACHINE ssh "sudo mkdir -p $DIR" | |
$VBOXMANAGE sharedfolder add $VM --name "$WORKDIR" --hostpath "$DIR" --transient | |
$DOCKER_MACHINE ssh "sudo mount -t vboxsf -o uid=$USERID,gid=$GROUPID $WORKDIR $DIR" | |
$VBOXMANAGE setextradata $VM "VBoxInternal2/SharedFoldersEnableSymlinksCreate/$WORKDIR" 1 | |
echo "Mount $DIR created." | |
} | |
for DIR in "${DIRS[@]}"; do | |
WORKDIR=$(basename "$DIR") | |
MOUNT_EXISTS="$($DOCKER_MACHINE ssh "mount | grep vboxsf | grep $DIR" 2>/dev/null)" | |
if [ "$FORCE" == "true" ] && [ -n "$MOUNT_EXISTS" ]; then | |
umount $DIR $WORKDIR | |
mount $DIR $WORKDIR | |
elif [ -z "$MOUNT_EXISTS" ]; then | |
mount $DIR $WORKDIR | |
else | |
echo "$DIR already mounted" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment