300-setup-shared-folder
#!/bin/bash
#
# Shared folder setup with autommout
#
if [ "$UID" -eq 0 ]
  then echo "This script should NOT be run as root."
  exit 1
fi
#
# Source config file
if [ ! -f ~/.vbconfig ]; then
    echo "Config File ~/.vbconfig not found!"
    echo "Run: set-folders to create .vbconfig"
    exit 1
fi
. ~/.vbconfig
#
if [ $# -ne 1 ]; then
  echo "Usage for $0 : VM Name required as argument."
  exit 1
fi
#
VB_VM_NAME="$1"
#
# Update Share Name below
# Designate different share names for each or a set of VMs
VB_VM_SHARE_NAME="share1"
VB_VM_GUESTUSER=`whoami`
# The specific share folder can ride under the common parent folder
VB_VM_HOST_PATH=$VB_SHR_LOC/$VB_VM_SHARE_NAME
mkdir -p $VB_VM_HOST_PATH
#
VB_VM_PATH=${VB_VMS_LOC}/${VB_VM_NAME}
VB_VM_CONF_FILE=${VB_VM_PATH}/${VB_VM_NAME}.vbox
#
echo "Guest Folder Creation ..."
echo "Review key parameters before proceeding ..."
echo "VM Name        : $VB_VM_NAME"
echo "VM Share Name  : $VB_VM_SHARE_NAME"
echo "Host Path      : $VB_VM_HOST_PATH"
echo "Guest user     : $VB_VM_GUESTUSER"
echo
echo "Review parameters for adding Guest Folder"
echo "Note: Shared folders requires Guest Additions"
echo
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
echo
#
VM_POWERED_ON=`VBoxManage list runningvms|grep -c '^"'${VB_VM_NAME}'"'`
if [ $VM_POWERED_ON -eq 1 ]; then
  echo "VM $VB_VM_NAME is up. Please power-down VM to continue"
  echo "Exiting ..."
  exit 1
fi
#
echo
echo "Include user $VB_VM_GUESTUSER to VirtualBox SF Group (On guest)"
echo "This will facilitate user $VB_VM_GUESTUSER access the Shared Folder"
echo "Alternatively only root (on guest) will be able to access the Shared Folder"
echo "Run the following command on Guest (as root)"
echo "usermod -a -G vboxsf $VB_VM_GUESTUSER"
echo
echo "About to mount shared folders ..."
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
echo
#
echo "Mounting Shared Folders"
VBoxManage sharedfolder add ${VB_VM_NAME} \
 --name ${VB_VM_SHARE_NAME} --hostpath ${VB_VM_HOST_PATH} --automount
echo "
Shared folder on Guest appears as: /media/sf_${VB_VM_SHARE_NAME}
"
echo "Powering VM on ..."
VBoxManage startvm ${VB_VM_NAME} --type headless
echo
echo "All done. Enjoy shared folders."
#
exit