2

I'm working on a embedded linux system based on busybox. Sometimes a USB device which is physically removed from the post is still displayed in mount, df or /proc/mounts. If I reattach the device, it is detected as for example /dev/sdb1 while the /dev/sda1 device is still mounted.

I know that open file handles on removed devices can cause this but is there any other condition which can trigger this bahavior?

5
  • I guess you didn't correctly umount the file system before removing the stick, did you? Commented May 7, 2014 at 16:19
  • Is your system running udev? Unplugging a USB device should trigger a udev event — does this happen? Commented May 7, 2014 at 22:54
  • @Gilles Yes, a udev event is triggered according to udevmonitor. The problem is that the script which is triggered can not do anything (It tries to unmount the dev file and removes the mount point).
    – Noir
    Commented May 8, 2014 at 12:52
  • @AndreasWiese Sometimes it happens that someone pulls the stick without unmounting it. But I have to handle that scenario too because theres a risk that applications will try to write on it.
    – Noir
    Commented May 8, 2014 at 13:00
  • @Noir: You cannot really handle this scenario well. You could for example define udev-rules that automatically unmounts filesystems when the underlaying blockdevice vanishes, but that won't protect you from any data loss. Best way to go is have your users eject their block devices properly. Hit them! :) Commented May 8, 2014 at 13:25

1 Answer 1

1

You could write a udev rule like

ACTION=="remove", KERNEL=="sd[b-z][0-9]", SUBSYSTEM=="block", RUN+="umount-removed.sh"

Which will trigger the unplug event and then umount the file system from that script

MNT_POINT=`df | grep "$DEVNAME" | awk '{print $6}'`

#If still mounted
if [ ! -z "$MNT_POINT" ];then
    logger -i -t usbrm -p daemon.notice "The device $DEVNAME is still mounted"
    #Umount fs
    umount $DEVNAME

    if [ $? -eq 0 ];then
        logger -i -t usbrm -p daemon.notice "$DEVNAME successfully unmounted from $MNT_POINT"
    else
        logger -i -t usbrm -p daemon.err "Impossible to umount $DEVNAME from $MNT_POINT. Aborting..."
    fi
fi

You can see the log in /var/daemon.log.

Edit : Forgot to precise that udev use a specific name space so you have to configure it to share mount point with userspace.

To do it locate the file systemd-udevd.service on your system and duplicate it to /etc/systemd/system/systemd-udevd.service and replace content to MountFlags=shared

3
  • Are you sure this works? My recollection of trying to trigger on a removal is that because the device was now removed the subsystem no longer knew what device it had been. So I only got a general "USB device removed" message. But that was a couple of years ago... Commented Aug 18, 2017 at 14:39
  • Pretty sure it works because I'm using it ;) It may depends on kernel version, I'm running it on 3.6 and 4.9 kernels and there is no problem. udev and systemd may also need extra configuration to work
    – Arkaik
    Commented Aug 18, 2017 at 14:44
  • Oh ok. Maybe I just couldn't find the right parameters - or it's a feature added since I last looked at this. Thanks. Commented Aug 18, 2017 at 15:07

You must log in to answer this question.

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