Skip to content

Instantly share code, notes, and snippets.

@Lahorde
Last active August 31, 2018 07:49
Show Gist options
  • Save Lahorde/f54bc23e715e4f6df32f533fa40e9016 to your computer and use it in GitHub Desktop.
Save Lahorde/f54bc23e715e4f6df32f533fa40e9016 to your computer and use it in GitHub Desktop.
Handle efficiently your nfs, samba shares even when it is offline.

Mounting samba shares with this method, https://wiki.archlinux.org/index.php/fstab#Automount_with_systemd gave me some freeze when accessing samba shares when it has been disconnected. Using systemd timeouts does not help : https://bbs.archlinux.org/viewtopic.php?id=225973 .

As an alternative, using https://wiki.archlinux.org/index.php/NetworkManager#Network_services_with_NetworkManager_dispatcher gives some good results : no freeze when shares are offline.

For Samba configuration refer : https://www.tecmint.com/setup-samba-file-sharing-for-linux-windows-clients/

In order to be able to write on shared drives, either configure samba with create mask=0770 Force create mode=0770 or add it to fstab

//a4h-rpi-dev-remi.local/rpi_projects /mnt/partages/a4h/rpi-dev_projects cifs _netdev,noauto,credentials=/etc/samba/credentials/a4h/rpi-dev_projects,file_mode=0777,dir_mode=0777 0 0

//a4h-rpi-dev-remi.local/rpi_projects /mnt/partages/a4h/rpi-dev_projects cifs _netdev,noauto,credentials=/etc/samba/credentials/a4h/rpi-dev_projects,file_mode=0777,dir_mode=0777 0 0
0 0
#!/bin/sh
echo "Executing wifi network shares (Network dispatcher script)"
# mounts defined in /etc/fstab
home_mounts=("/mnt/partages/home/partage" "/mnt/partages/home/openhab_configuration")
work_mounts=("/mnt/a4h-rpi-dev_projects")
function net_drive_mount
{
nb_attempts=10
for mount_id in "${@}"
do
while [ $nb_attempts -gt 0 ]
do
if mount $mount_id
then
echo "$mount_id successfully mounted"
break
else
echo "attempt $nb_attempts : could not mount $mount_id"
fi
sleep 0.2
nb_attempts=$[$nb_attempts -1]
done
done
}
function net_drive_umount
{
for mount_id in "${@}"
do
echo $mount_id
if mount | grep "$mount_id"
then
# umounting can take some time when drive is offline
# but as soon umount is called, umount seems to be
# effective : access to previously mounted drives
# are not freezed during some sec
echo "umounting $mount_id in a background process"
umount -l "$mount_id" &
fi
done
}
if [ "$2" == 'down' ]; then
# try to umount all shares
net_drive_umount "${work_mounts[@]}"
net_drive_umount "${home_mounts[@]}"
elif [ "$2" == 'up' ]; then
conn_name=$(iwgetid wlo1 -r)
if [ -n "$conn_name" ] ; then
if [ "$conn_name" == "A4H_creativity_lab" ] ; then
echo "at work : mount creativity lab network drives"
net_drive_mount "${work_mounts[@]}"
elif [ "$conn_name" == "Bbox-DCE7FB" ] ; then
echo "at home : mount network drives"
net_drive_mount "${home_mounts[@]}"
fi
else
echo "could not get wireless connection name - may be connected over wired network"
fi
else
echo "nothing to do for event $2"
fi
echo "End of wifi network shares (Network dispatcher script)"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment