OpenWrt Forum Archive

Topic: [Q] How to check if a partition is mounted

The content of this topic has been archived on 24 Mar 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I need to write a simple scripts file that will check if the partition on a USB memory stick has been mounted and then unmount the partition if it is already mounted. Currently, I have an indea to achieve this with the following scripts:

[ -d "/usb/lost+found" ] && umount /usb

I believe this will only work if the partition is formatted with a Un*x/Linux filesystem type. Is there a better way to achieve this regardless what filesystem type?

Try this:

if grep -qE '^[^[:space:]]+ /usb ' /proc/mounts; then
  umount /usb
fi
jow wrote:

Try this:

if grep -qE '^[^[:space:]]+ /usb ' /proc/mounts; then
  umount /usb
fi

Thanks for your quick response. FYI, I had to remove the empty space between the "/usb" and "'" to make your scripts to work.

The space was intentional, consider the following example in /proc/mounts:

/dev/sda1 /usb ext3 rw,errors=continue,data=ordered 0 0
/dev/sda2 /usb2 ext3 rw,errors=continue,data=ordered 0 0

Without the additional space, the grep would match the second entry too which could lead to false positives.

Regards,
JoW

You are right. Thanks for this pointer. However, if I include the space, the scripts won't work. For the moment, I have to remove such a space to make it work.

The discussion might have continued from here.