What's new
  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Script for USB HDD mount management

algorab

Occasional Visitor
Hello,

This post is addressed to those who have a problem with keeping the USB HDD stably mounted, as I do.

My problem is that quite randomly the router loses the mount of the HDD and even worse, the device files /dev/sd* dissapear, which means that the OS is no longer seeing the device attached to the USB port.

In order to fix this problem, I have developed the following solution:

1. In /jffs/configs create a file called fstab, with the following content:

UUID=7b6c3baa-55f6-4dd0-ac08-3958317b68f4 /tmp/mnt/<Your Mount Folder #1> ext3 rw,nodev,noatime,sync,data=journal 0 0
UUID=cbc2771e-588e-498d-b64a-1a1a3ec5e699 /tmp/mnt/<Your Mount Folder #2> ext3 rw,nodev,noatime,sync,data=journal 0 0

As you can see, my HDD is partitioned in 2, and it is formatted with Linux ext3 file-system, with journaling active. Journaling is very important because in case of an unexpected umount, the file-system may get in a corrupted state, which may lead to data loss. With journaling active, the probability of seeing a inconsistent file-system is very much reduced.

Also, I am making reference to devices by their UUID because this will always stay fixed and it is unique, whereas the device file or the mount point may vary in time.

To get the UUID of the devices, use the command : blkid

This file is copied by the OS on boot to /etc/fstab.

2. In /jffs/scripts I created a script called usb_mon.sh, with the following content:

#!/bin/sh

# Is the user ROOT?
if [[ "$USER" != "admin" ]] ; then
echo "This must be run as admin!"
exit 1
fi

if [ -f /tmp/fstab.txt ]
then
rm /tmp/fstab.txt
fi
if [ -f /tmp/mtab.txt ]
then
rm /tmp/mtab.txt
fi
if [ -f /tmp/blkid.txt ]
then
rm /tmp/blkid.txt
fi

# Part 1 : verify if the HDD is still mounted

echo "Verifying the mount status of the USB drives ..."

grep ^UUID /etc/fstab | awk '{ print $1 }' > /tmp/fstab.txt
mount | grep ^\/dev\/sd | awk '{ print $1 }' > /tmp/mtab.txt
blkid | grep LABEL | awk '{print $1 $3}' > /tmp/blkid.txt
awk '{gsub(":", " ");print}' /tmp/blkid.txt > /tmp/blkid2.txt
rm /tmp/blkid.txt
awk '{gsub("\"", "");print}' /tmp/blkid2.txt > /tmp/blkid.txt
rm /tmp/blkid2.txt

grep -Foq -f /tmp/mtab.txt /tmp/blkid.txt
dev=$?
grep -Foq -f /tmp/fstab.txt /tmp/blkid.txt
uuid=$?
if [[ "$dev" -eq 0 && "$uuid" -eq 0 ]]
then
echo "The drives are mounted properly!"
echo "Exiting ..."
exit 0
fi

rm /tmp/fstab.txt
rm /tmp/mtab.txt
rm /tmp/blkid.txt

# Part 2 : force USB unbind and bind

echo "Starting to reset the USB devices!"

for xhci in /sys/bus/pci/drivers/?hci_hcd ; do
if ! cd $xhci ; then
echo Weird error. Failed to change directory to $xhci
exit 1
fi

echo Resetting devices from $xhci...

for i in ????:??:??.? ; do
echo -n "$i" > unbind
echo -n "$i" > bind
done
done

echo "USB devices reset done!"

# Part 3 : Once the HDD is correctly detected by the router, mount the partitions and verify once again that they are mounted correctly

echo "Mounting back the devices..."

mkdir -p /tmp/mnt/<Your Mount Folder #1>
mkdir -p /tmp/mnt/<Your Mount Folder #2>

sleep 10

mount /tmp/mnt/<Your Mount Folder #1>
mount /tmp/mnt/<Your Mount Folder #2>

echo "Verifying that the drives are mounted back properly..."

sleep 30

grep ^UUID /etc/fstab | awk '{ print $1 }' > /tmp/fstab.txt
mount | grep ^\/dev\/sd | awk '{ print $1 }' > /tmp/mtab.txt
blkid | grep LABEL | awk '{print $1 $3}' > /tmp/blkid.txt
awk '{gsub(":", " ");print}' /tmp/blkid.txt > /tmp/blkid2.txt
rm /tmp/blkid.txt
awk '{gsub("\"", "");print}' /tmp/blkid2.txt > /tmp/blkid.txt
rm /tmp/blkid2.txt

grep -Foq -f /tmp/mtab.txt /tmp/blkid.txt
dev=$?
grep -Foq -f /tmp/fstab.txt /tmp/blkid.txt
uuid=$?
if [[ "$dev" -ne 0 || "$uuid" -ne 0 ]]
then
echo "Error remounting the USB devices ..."
echo "Exiting ..."
exit 1
fi

echo "The drives are mounted properly!"

rm /tmp/fstab.txt
rm /tmp/mtab.txt
rm /tmp/blkid.txt

# the end

echo "Exiting ..."
exit 0

3. With the command cru, the script can be scheduled to run automatically at a specified time interval, and remount the drives in case the OS lost them since the last check.

NOTE : On boot, the router mounts the drives on its own, ignoring the statements from the /etc/fstab file. It is possible that at the moment when the mount is executed, the fstab file is not yet copied over from /jffs/configs/fstab. And the mount is executed without journaling.

To remount with journaling, in the post-mount script the drives can be unmounted and remounted based on the configuration from the fstab file.
 
Last edited:

Similar threads

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Back
Top