This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

remove usb drive without unmounting

Our application can store data on a USB flash drive.

If the user removes the flash drive without running "umount" on the mount first, we always get the "do you want to scan and fix" error message in Windows when the drive is inserted in the PC.

I modified the mount script "/etc/udev/scripts/mount.sh" to add the "-o sync" option to make the accesses synchronous, but this did not make any difference.

Is there something else I need to set or do to fully flush all writes so the user can remove the USB drive at will without getting this error?

Thanks,

  Peter Steinberg

  • Dear Peter Steinberg,
    User may remove the pendrive at any time regardless of the USB transfer (copy,delete etc.,) ??
    Or they may remove once the USB transfer is get completed ??

    We have "sync" command in Linux to flush the write/delete datas from mass storage devices (mounted devices).

    In your application, you can use this command after USB transfer is get completed then you can remove the PENDRIVE at anytime without doing umount.

    Please let me know if any clarifications required.
    Also explain your scenario to understand the requirement better.
  • Yes, the user can remove the flash drive at any time.

    I tried using the sync command from the console after writing to the flash drive, but still get the "Do you want to scan and fix..." message when I insert the drive into a Windows PC.

  • The file system on the drive contains a "dirty" flag that indicates whether it was cleanly unmounted, i.e., it is set on mounting, and cleared on unmounting.

    Even when all data is synced, this flag remains set. You would have to unmount the drive to avoid the message.
  • Correct.

    At least on Windows, there is a policy you can set "Quick removal" where the OS disables write caching on the device so the flag is quickly cleared.

    I'm looking to see if there is something similar that can be set in Linux.
  • I've had the same problem. Never did find a solution. I tried both sync, dir sync and no atime. Besides still getting the "fix" messages, sync results in much slower write speeds.
    If you are the only writer the to USB drive, you could programmatically mount-write-sync-umount on each file. Assumes you tolerate the slight delay in mount. That should reduce the "fix" message. There is always a chance that the USB driver is removes before you can sync-umount.
  • There is no backdoor to the "dirty" flag.

    If you really want to disable it completely (so that unplugging in the middle of your writes is not detected), and are OK with having a custom kernel, remove the code from the fat_set_state() function in fs/fat/inode.c.