Script to check and mount drives

Want to share your OpenWrt / Gargoyle knowledge? Implemented a new feature? Let us know here.

Moderator: Moderators

Post Reply
mugre1975
Posts: 2
Joined: Fri May 01, 2015 4:05 pm

Script to check and mount drives

Post by mugre1975 »

Hello

Since I'm using fstab to mount my drives (I have a USB hub with 3 external drives mounted on it), I found that sometimes during boot Gargoyle doesn't stick to the fstab mounts and performs a temporary mount at /tmp/usb_mount
To prevent that, and ensure that the drives get mounted at the mount points I've set in fstab (which are the ones I've specified for the samba server), I've written the following script to:
1) Check if the drive exists
2) Check if the drive is mounted
3) Check is the drive is mounted at the desired mount point

In case 2) or 3) tells me otherwise, the drive is mounted at the desired mount point, please have in mind that, in this script the first drive has an NTFS filesystem while the other two are ext4


The script works with a parameter that lets it know which drive has to check (last two letters of the sdxx devices), and it has my mount points hardcoded as an example (mp variable)

Code: Select all

#!/bin/sh
if [ -z "$1" ]; then
	echo "You must provide a parameter"
else
	#Complete path for the drive
	dv='/dev/sd'$1
	#Define complete path for mount point and filesystem type
	if [ "$1" == "a1" ]; then
		mp='/tmp/usb_mount/sda1'
		ft=''
	else
		mp='/mnt/'
		ft='-t ext4'
		if [ "$1" == "b1" ]; then
			mp=$mp'lacie'
		elif [ "$1" == "c1" ]; then
			mp=$mp'wesdig'
		fi
	fi
	#Check if drive exists
	if [ -e "$dv" ]; then
		#Check if drive is mounted
		if grep -q "$dv" /etc/mtab; then
			#Check if mount point is OK
			if mount | grep $mp > /dev/null; then
				echo "Drive mounted OK"
			else
				#Mount drive in desired mount point
				umount $dv
				mount $ft $dv $mp
			fi
		else
			#Mount drive in desired mount point
			mount $ft $dv $mp
		fi
	else
		echo "Drive does not exist"
	fi
fi
Plus, I'll also give you a script that checks whether a drive exists and is mounted, and unmounts it, in case you find it useful:

Code: Select all

#!/bin/sh
if [ -z "$1" ]; then
	echo "You must provide a parameter"
else
	#Complete path for the drive
	dv='/dev/sd'$1
	#Check if drive exists
	if [ -e "$dv" ]; then
		#Check if drive is mounted
		if grep -q "$dv" /etc/mtab; then
			#Unmount drive
			umount $dv
		else
			echo "Drive is already unmounted"
		fi
	else
		echo "Drive does not exist"
	fi
fi

ispyisail
Moderator
Posts: 5212
Joined: Mon Apr 06, 2009 3:15 am
Location: New Zealand

Re: Script to check and mount drives

Post by ispyisail »

+1

Post Reply