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.

7
  • 2
    See my answer here.
    – jasonwryan
    Aug 13, 2018 at 4:34
  • 1
    What distro are you on?
    – Tim
    Aug 14, 2018 at 10:12
  • Does you system run only in ram? (busybox tag). Is the filesystem persistent? If it is not persistent, does the script need to run more then once in the lifetime of the boot session?
    – Tim
    Aug 14, 2018 at 10:29
  • @Tim, busybox. Filesystem is persistent.
    – GeneCode
    Aug 14, 2018 at 23:28
  • If it's plain BusyBox, how is it you have udev? BusyBox uses mdev.
    – Tim
    Aug 15, 2018 at 6:11

3 Answers 3

2
+25

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

1
  • Good to know about the -p flag. But I always delete the /mnt/usb first before i plug in my usb.
    – GeneCode
    Aug 14, 2018 at 23:28
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.

1
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .