I do it of a very similar mode at was proposed above but these script create the required and mount or unmount the shared folder with the following script:
#!/bin/bash
#
# Mount automatically even shared folder on startup and unmount it at shutdown.
#
# VirtualBox (c) 2015 by Oracle Systems Inc.
#
####
# Check user privileges.
if [[ $EUID -ne 0 ]]; then
echo -e "This script must run at ROOT user!" \
"\nPlease, use 'sudo', 'visudo' or any other to run it."
exit 1
fi
# Check paramas from caller.
if [[ $# -eq 0 ]]; then
echo -e "Auto-Mount selected shared folder of VirtualBox machine." \
"\nUsage:" \
"\n VBoxShared <drive_one> <drive_two> <...>"
exit 2
fi
declare EVENT= # This set the ACTION: -m OR -u
declare -a DRIVES=()
# Processing each param:
for arg in "$@"; do
case "$arg" in
"-m"|"--mount")
if [[ -z ${EVENT} ]]; then
EVENT=-m
else
exit 318 # parameters at conflict!
fi
;;
"-u"|"--umount")
if [[ -z ${EVENT} ]]; then
EVENT=-u
else
exit 318 # parameters at conflict!
fi
;;
*)
DRIVES=("${DRIVES[@]}" "${arg}")
;;
esac
done
unset arg
[[ -z ${EVENT} ]] && exit 1 # ERROR: No se ha establecido la acción a realizar.
[[ "${#DRIVES[@]}" -gt 0 ]] || exit 1 # ERROR: No se han indicado las unidades a manejar.
# Process each shared folder stored on '${DRIVES}' array
for drive in "${DRIVES[@]}"; do
DEST="/media/sf_${drive}"
case "${EVENT}" in
"-m")
[[ -d ${DEST} ]] || (mkdir ${DEST} && chown root:vboxsf ${DEST} && chmod 770 ${DEST})
mount -t vboxsf ${drive} ${DEST}
;;
"-u")
if [[ `df --output=target | grep "${DEST}"` > /dev/null ]]; then
umount -f ${DEST}
rm -rf "${DEST}"
fi
;;
esac
unset DEST
done
unset drive
unset EVENT
unset DRIVES
exit 0
Save it as /opt/.scripts/VBoxShared.sh
.
Make sure that this can be runned. On shell type:
sudo chmod a+x /opt/.scripts/VBoxShared.sh
Now, we add a line that run this script on rc.local
:
sudo nano /etc/rc.local
and we add these line before last line (exit 0
):
. /opt/.scripts/VBoxShared.sh --mount <SharedFolder1> [<SharedFolder2> <SharedFolder3> ...]
Save (CtrlO) and close it (CtrlX)
At this point, we mount automatically all shared folder listed on <SharedFolder>
at startup.
For unmount it, we only need type:
sudo nano /etc/rc6.d/K99-vboxsf-umount.sh
#!/bin/bash
. /opt/.scripts/VBoxShared --umount <SharedFolder1> [<SharedFolder2> <SharedFolder3> ...]
exit 0
Save (CtrlO) and close (CtrlX)
sudo chmod a+x /etc/rc6.d/K99-vboxsf-auto.sh
And that's all!
noauto
in the fstab options and then to later mount typically in a startup script (such as .profile), Option 2) the main issue being that vboxsf isn't loaded prior to fstab running, appendvboxsf
to the file/etc/modules
, asking the kernal to load the module prior to fstab being run. Maybe this will help someone else. – garromark May 20 '14 at 22:11vboxsf
kernel module to/etc/modules
. In/etc/fstab
file, i provided withuid=$UID,gid=$(id -g)
as<options>
– Wasif Hossain Jul 04 '16 at 05:31sudo mount -t vboxsf -o uid=$UID,gid=$(id -g) windows_share ~/shared/mount_point
and putting the following in /etc/fstab works:windows_share /home/dev/shared/mount_point vboxsf rw 0 0
where dev is my user :) – MediaVince Dec 31 '16 at 16:03comment=systemd.automount
as in this answer pointed out above. I am using VirtualBox 5.2.2 and Kubuntu 17.10. Since this seems to work for others, when there's a lot of red herrings here, would you mind integrating that piece of information into your highly-ranked answer? – HostileFork says dont trust SE Dec 15 '17 at 21:29