Check Samba Mount at Boot in Linux

This is technically a continuation of my post Here, but I felt this deserved its own post. If you are like me, and have Samba mounts that HAVE to be in place and ready right after boot, you want a way to make sure the drive is mounted before the system fully starts up. Well, I made a nice little script that will check a samba mount at boot time.

So here is the script, which you can download from Here: http://servernetworktech.com/uploads/scripts/mountsmb

#!/bin/bash

# SMBMBT: Check Samba Mount at Boot
# By Chris Blake
# http://servernetworktech.com
# Version 1.1

# Define the location that needs to be checked for mount
DRIVE="/mnt/NFS-SMB"

# Define the name of the interface that is used for the samba mount (ex eth0)
INTERFACE="vmbr1"

#Code starts here, no need to edit after this

if [ "$IFACE" != $INTERFACE ]; then
        exit 0
fi

logger "SMBMNT: Starting SAMBA Mount Check Script..."
echo "SMBMNT: Starting SAMBA Mount Check Script..."
LOOPCHECK=0
COUNT=0
while [ $LOOPCHECK != 1 ]; do
        if grep -swq $DRIVE /proc/mounts;
        then
            logger "SMBMNT: $DRIVE is already mounted. Finishing..."
            echo "SMBMNT: $DRIVE is already mounted. Finishing..."
            let LOOPCHECK=1
        else
        if [ $COUNT == 10 ];
        then
            logger "SMBMNT: Failed to mount 10 times... System will now reboot!"
            echo "SMBMNT: Failed to mount 10 times... System will now reboot!"
            reboot
            exit 0
        fi
        logger "SMBMNT: $DRIVE is not mounted, attempting to mount..."
        echo "SMBMNT: $DRIVE is not mounted, attempting to mount..."
        mount $DRIVE &>/dev/null
        logger "SMBMNT: $DRIVE should be mounted, pausing 5 seconds to try again..."
        echo "SMBMNT: $DRIVE should be mounted, pausing 5 seconds to try again..."
        sleep 5
        let COUNT=COUNT+1
        fi
done

logger "SMBMNT: Script Finished"
echo "SMBMNT: Script Finished"
exit 0

What this script does is it takes a defined mount point, checks it, and if it is not mounted, it will attempt to mount it. If it fails to mount, it will retry up to 10 times before restarting the system to try the process again.

In this script, there are two variables you need to modify.

  • DRIVE = This is the location of the mount. In the script, it is defined as “/mnt/NFS-SMB”
  • INTERFACE The interface that is used for the samba mount. For me, it is “vmbr1” as I am using this with proxmox.

After you modify those variables, place this script in /etc/network/if-up.d/ and then make it executable (chmod +x mountsmb).

And BAM, at startup this script will run, and will log to /var/syslog its process, as well as output during boot. Hope this script helps.

Note: This is done on a Debian based OS, and may not work with redhat based distros.

Leave a Reply

Your email address will not be published. Required fields are marked *