Linux – Unmounting Detachable devices (eSATA,USB storage) in Linux

busyboxembedded-linuxlinuxlinux-kernel

A detachable device like eSATA, USB drive can be abruptly removed (by simply pulling the plug).

If there are open file handles on a partition, then this partitions won't unmount, i.e. the Linux umount command will fail, even AFTER the drive is physically detached.

If the unmount fails, then on reattachment of the device, the mount will fail as well. So you have to find out which processes are using the drive and kill them or close all the handles. If you cannot do either, you will have to reboot the box to get your drive mounted. And I can definitely not kill the process using it.

I see no "force unmount" option, there is a -f option, but it is only for NFS.

This sounds very strange,doesn't Linux accommodate for this scenario where a user simply yanks a drive? Does anyone know how to handle this scenario gracefully in Linux?

Is there any way to find out what file handles are open on a particular partition/device or selectively flush and close all file handles only for a particular device?

Note: The lsof command is not available in the embedded Linux that I am using (busybox).


"fuser" is not available in my embedded linux.

I tried the lazy umount -l. However, it does not seem to consistently work. Say for e.g.
I keep a file handle open (with "tail -f" on some file on the device). Then I detach a drive and then I do "umount -l" and it unmounts. And then I reattach the drive and try to mount it again on the same mount point, while the tail is still running. It does not consistently work. Sometimes it succeeds and sometimes it does not. This makes me uncomfortable using the lazy option what if it leaves the file system in inconsistent state. And also am not sure if this lazy option was meant to be used for such scenarios.

I cannot kill the process which has file handles open.


It seems if I have mounted the device on say /mnt/abc and if I disconnect the drive and then reconnect, Linux seems to re-attach the device's file system to the same mount point "/mnt/abc", without us doing any unmount or mount. And then the same old open file handles seems to start working after reattachment (atleast for file read operation). This is my observation. I am not sure if this is the expected behavior.. However, this also does not seem to be working consistently.

I had an open file handle for reading ("tail -f") which I left open, then I detached and reattached and then modified the file being tailed and I saw the "tail -f" output getting updated with the changes.
However, if I try to modify a file after the device was gone( it gives error as expected) and then I reattach, the device's file system does not get properly re-attached to the same mount point. In case of a file write (while the device was not there) it does not seem to be working.

Is there any standard/consistent behavior that Linux follows when a drive is removed abruptly without closing all handles and properly unmounting all partitions ?

Best Answer

You can write a bash script to scan all the file descriptors listed in /proc (assume you have that) and list/kill the processes.

/proc/$m/fd/$n is the n-th file descriptor for PID m presented as a symbolic link. busybox does have readlink support, so you should be able to automatic it.

Edit: Just to say this is essentially re-implementing lsof, but it's actually fairly simple.

Related Question