Lo faccio in una modalità molto simile a quella proposta sopra ma questi script creano il necessario e montano o smontano la cartella condivisa con il seguente 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 "[email protected]"; 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
Salva come /opt/.scripts/VBoxShared.sh
.
Assicurati che questo possa essere eseguito. Sul tipo di shell:
sudo chmod a+x /opt/.scripts/VBoxShared.sh
Ora aggiungiamo una riga che esegue questo script su rc.local
:
sudo nano /etc/rc.local
e aggiungiamo questa riga prima dell'ultima riga ( exit 0
):
. /opt/.scripts/VBoxShared.sh --mount <SharedFolder1> [<SharedFolder2> <SharedFolder3> ...]
Salva ( Ctrl O ) e chiudilo ( Ctrl X )
A questo punto, montiamo automaticamente tutte le cartelle condivise elencate su <SharedFolder>
all'avvio.
Per smontarlo, abbiamo solo bisogno di tipo:
sudo nano /etc/rc6.d/K99-vboxsf-umount.sh
#!/bin/bash
. /opt/.scripts/VBoxShared --umount <SharedFolder1> [<SharedFolder2> <SharedFolder3> ...]
exit 0
Salva ( Ctrl O ) e chiudi ( Ctrl X )
sudo chmod a+x /etc/rc6.d/K99-vboxsf-auto.sh
E questo è tutto!