2

I setup a rule in udev to run a script in background to automount the USB drive. It managed to run the script fine, but the mounting is failing for some reason.

RULE:

ACTION=="add", KERNEL=="sd[a-z]*", RUN+="/etc/mntUsbChk.sh &"

In this script:

#!/bin/sh

sleep 2

mkdir /mnt/usb

foundUsb=false

if [ -e /dev/sda1 ]; then
 mount /dev/sda1 /mnt/usb
 $foundUsb=true
fi

if [ -e /dev/sdb1 ]; then
  mount /dev/sdb1 /mnt/usb
  $foundUsb=true
fi

if [ -e /dev/sdc1 ]; then
  mount /dev/sdc1 /mnt/usb
  $foundUsb=true
fi

if [ $foundUsb -eq false ]; then
 exit
fi

echo "USB MOUNTED"

[ 1610.868626] FAT-fs (sdb1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.

EDIT: More details: The script was being executed for sure because the /mnt/usb folder was created after I plugged in the USB.

And also, if I manually type mount /dev/sda1 /mnt/usb after I plug in USB before I enable the rule, it was able to mount it just fine. I just don't get it why it cannot mount in the rules udev.

slm
  • 369,824
GeneCode
  • 157

3 Answers3

2

This script will fail is /mnt/usb already exists. Not sure if this is the source of the problem, but it is for sure an error of the script.

mount /mnt/usb

Use instead:

mount -p /mnt/usb

From man mount:

-p, --parents

no error if existing, make parent directories as needed

Tim
  • 1,012
1

You have no guarantee the USB drive will be sda1,sdb1,sdc1 on add. I think some better logic may be needed there, but completely unrelated to the question.

For the question, see the existing link below: Why does Linux mark FAT as 'dirty' simply due to mounting it?

My understanding is just pulling a usb from a Windows machine w/o ejecting doesn't clear the dirty bit on the drive. Linux assumes the worst when the dirty bit is set. I'm assuming here you can automatically run fsck to clear the dirty bit on this error message if present, then proceed to mount again. Although I'm not sure if a call to umount or fsck would clear the dirty bit.

I hope this heads in the right direction.

  • Helps if I add the link reference: https://unix.stackexchange.com/questions/230181/why-does-linux-mark-fat-as-dirty-simply-due-to-mounting-it – MalformedPacket Aug 13 '18 at 05:35
1

Although it is not very well documented, you are not supposed to run mount from a Udev rule.

Please see my answer to a similar question for alternatives.

Metamorphic
  • 1,179