Skip to content

Instantly share code, notes, and snippets.

@paulojeronimo
Last active February 16, 2021 16:54
Show Gist options
  • Save paulojeronimo/87c054b60ab8ecb05eb8f25fa9b861bf to your computer and use it in GitHub Desktop.
Save paulojeronimo/87c054b60ab8ecb05eb8f25fa9b861bf to your computer and use it in GitHub Desktop.
Scripts to mount my VM shared folders (VMware Fusion or VirtualBox) #bash
#!/usr/bin/env bash
set -eou pipefail
cd `dirname "$(readlink -m "$0")"`
vm=${1:-vmware}
action=${2:-mount}
script=./$vm-$action
source ./vm-mount.exit_codes
[ -f "$script" ] && {
error=/tmp/`basename "$script"`.error
$script 2> $error
case "$?" in
0|$ALREADY_MOUNTED|$ALREADY_NOT_MOUNTED):;;
*)
echo "An error ocurred when \"$script\" was invoked:"
cat $error
exit $?
;;
esac
} || {
[ "$vm" = "unknown" ] || echo "Script \"$script\" not found!" >&2
}
#!/usr/bin/env bash
set -eou pipefail
BASE_DIR=`dirname "$(readlink -m "$0")"`
SHARED_FOLDER=${SHARED_FOLDER:-pj}
MOUNT_DIR=${MOUNT_DIR:-/mnt/$SHARED_FOLDER}
UID_=${UID_:-`id -u`}
GID_=${GID_:-`id -g`}
cd "$BASE_DIR"
source ./vm-mount.exit_codes
script_name=`basename $0`
vm_type=${script_name%-*}
action=${script_name#*-}
mount=true
! [ $action = umount ] || mount=false
$mount && {
sudo mkdir -p "$MOUNT_DIR"
! mount | grep -q "$MOUNT_DIR" || {
echo "Directory \"$MOUNT_DIR\" already mounted!" >&2
exit $ALREADY_MOUNTED
}
[ ! "$(ls -A "$MOUNT_DIR")" ] && {
case $vm_type in
vmware) sudo vmhgfs-fuse .host:/$SHARED_FOLDER "$MOUNT_DIR" -o allow_other -o uid=$UID_ -o gid=$GID_;;
virtualbox) sudo mount -t vboxsf -o uid=$UID_,gid=$GID_ $SHARED_FOLDER "$MOUNT_DIR";;
*) echo "Shared folder \"$SHARED_FOLDER\" can not be mounted on a VM of type \"$vm_type\"!"; exit 1
esac
} || {
echo "Directory \"$MOUNT_DIR\" was't mounted by \"$script_name\" because it is not empty!" >&2
exit $IS_NOT_EMPTY
}
} || {
sudo umount "$MOUNT_DIR"
remove=-r
}
s=create-my-links; ! command -v $s >&- || $s -o "$MOUNT_DIR" ${remove:-}
#!/usr/bin/env bash
readonly ALREADY_MOUNTED=20
readonly IS_NOT_EMPTY=21
readonly ALREADY_NOT_MOUNTED=32
#!/usr/bin/env bash
set -eou pipefail
case "$(sudo dmidecode -s system-product-name)" in
VMware*) echo vmware ;;
VirtualBox) echo virtualbox ;;
*) echo unknown ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment