0

In Linux, how to do quick remove of USB device, like Windows, without file system impact or corruption?

I know the following:

  1. Lazy unmount will remove the mount reference, on USB device quick remove in Linux, but that does not protect file system, ext4, NTFS, vFat, whatever the USB device file system is, still may have pending writes cached.

  2. Mount with dirsync, sync, flush, none of these seem to work interface to no. 1 above. Have actually tested this, and you get warnings about running fsck, or chkdsk.

  3. Turning off write cache of device, would seem to make sense to avoid no. 1 and no. 2 above, right? Is that likely?

Is there a consistent method to disable the write cache before mount is done? Prefer that this be done via a UDEV add rule, i.e. call script, whatever.

Windows feature as noted above... Really does imply it is just a write cache/sync removal configuration.

enter image description here

  • Does `umount -f` still produce a corruption for you? Does it not succeed? – root Dec 17 '22 at 00:31
  • Doing a forced umount? I think I tested that, and it still happened. The issue is that the file system on the drive, for example, vfat, knows it was not a clean unmount, independent of what the parent OS knows or believes. If you just pull a USB device (on Linux) and have UDEV rule that does a lazy umount (-l) the parent OS is cool with that. – Jibun no Kage Dec 18 '22 at 01:14
  • But when you plug the device in again, the parent OS, states the file system on the device maybe corrupted. Windows avoids this some how, even though NTFS needs a clean unmount WIndows does not report NTFS corrupted or vfat or even exfat the way Linux does. So there is some driver difference. I thought disable of the write cache might force flush,sync after EVERY write to the device so there would be no open/active resources on the USB device. But have not figured out how to do this in Linux as yet, to fully test the idea. – Jibun no Kage Dec 18 '22 at 01:14
  • `unmount -f` kills all the (killable) processes using the device, if it succeeded then the filesystem wouldn't be "unclean". Are you removing the device even after unmount failure? Please edit your question to specify exactly what sequence of operations you're doing and what is the output from each step. – root Dec 18 '22 at 02:57
  • I thought I was clear, this is an attempt to mimic the Windows OS behavior, where not explicit umount or eject is done. Windows 11 and previous has a quick removal feature that does not mark the partition dirty and does corrupt the file system. Linux by default does not do this. – Jibun no Kage Dec 20 '22 at 02:55
  • But to answer your question, a mount is done, some write activity is done, say add a file, then device removed physically. UDEV rule for add, does the mount, UDEV rule for remove (right now) does a lazy umount to clean up the mount list. But this is incomplete because some filesystems became dirty or corrupted at times. Dirty is consistent, but corruption seems to be inconsistent. – Jibun no Kage Dec 20 '22 at 02:55
  • I wasn't aware that "quick remove" is a name of a Windows feature. It looks like it's just the `sync` mount option. In any case lazy unmount is NOT what you want. So if you mount with `sync,dirsync,flush` and unmount with `umount -f` and NOT with `-l` - does unmount succeed, and do you get corruption? – root Dec 20 '22 at 05:45
  • I will give that a few tests and see what results. I think I may have tried sync,dirsync,flush before, but I had not tried same with umount -f. As for quick remove... it is under volume properties for USB devices. I updated the question with picture showing it. – Jibun no Kage Dec 22 '22 at 16:58

0 Answers0