Safely unplug mounted USB device without first unmounting?
Linux - Embedded & Single-board computerThis forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Safely unplug mounted USB device without first unmounting?
I've started working with Linux two months ago so I'm pretty new to it.
Google didn't help me much so I'm asking here.
I'm working on a project running embedded Linux. USB devices are supported and the user can plug and unplug whenever he/she wants.
When the device is plugged in it is auto mounted, but the device could be unplugged at any time, which could lead to device malfunction. I'm not sure if it is possible to protect from damage if device is disconnected during read/write, but I suppose there is a way at least to let the user disconnect the device safely when it is not in use (there is no way provided for the user to select some "Safely remove device" option).
What I observe is:
1. Plug USB device (mass storage device mainly) - Working properly
2. Unplug without unmounting or ejecting
3. Plug it again - "FAT: Filesystem error... Invalid access to FAT" messages are displayed
4. I found that fsck.msdos should be able to repair it, so I plugged it to a Linux PC and run it - went OK
5. I repeat steps 1-3 and the same results
6. Now even fsck.msdos cannot help because it says it has repaired it but my embedded Linux still displays errors
7. I've tried with a couple of different USB mass storage devices with the same results
I have not tried yet if I umount it before unplug, but I suppose it will work fine then (I make many assumptions...)
You can use automount tool to automaticaly mount just before access by user and unmount device after specified, short time (if user remove device before this time it still can corrupt data).
Second option is add action to udev (if you have installed) and force unmounting device after udev detect disconnecting of device (but written data still may be corrupted). Also FAT is not very resistant to errors, there are better filesystems.
Maybe you can bypass cache, then your user will known when coping has really ended, but I believe pendrives has also own caching. Or inform user when automatic unmount is done by some LED if you have free.
Well, yeah, that would be (relatively) safe. As long as you're only reading from the drive, there's no danger of messing it up.
But it's pretty much impossible to do what you want otherwise. Any drive that gets suddenly removed in the middle of a write operation is subject to filesystem corruption. There's no way to prevent it that I know of. And because most systems do transfers in asynchronous mode, the write operation may still be in progress and half of the data still sitting in a buffer somewhere, even though the display says it's finished.
You might try using the sync mount option, which will keep it from doing asynchronous transfers as in the above, and the system won't report the operation as finished until it's actually finished. The sync command likewise forces an immediate flushing of the buffers, after which the drive should be safe to remove. Although even then, just yanking it out wouldn't automatically inform the system that the drive is gone.
It might also be a good idea to use a filesystem on the drive that's less easily corrupted than fat, as mentioned before.
Again, though, the most important thing really is to educate the users as to just how important it is to remove the drive properly.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.