LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Password
Linux - Embedded & Single-board computer This 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


Reply
  Search this Thread
Old 04-25-2012, 03:11 AM   #1
helleye
LQ Newbie
 
Registered: Apr 2012
Posts: 2

Rep: Reputation: Disabled
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...)
 
Old 04-25-2012, 04:09 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
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.

Last edited by eSelix; 04-25-2012 at 04:14 AM.
 
Old 04-25-2012, 05:12 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
The "flush" mount option may reduce the chance of damage by flushing the cache earlier.
 
Old 04-30-2012, 10:36 AM   #4
helleye
LQ Newbie
 
Registered: Apr 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you for your comments.
I was told that we need to mount manually with readonly and that would also solve my problem.
 
Old 05-01-2012, 01:24 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can root unplug/plug USB device with only command root ? frenchn00b Linux - General 2 10-16-2007 12:49 AM
Is it possible to disable power supply to an USB storage device when unmounting? costavi Linux - Hardware 2 11-14-2006 03:23 AM
USB mobile disk safely unplug serafimg Linux - Newbie 6 03-21-2006 03:32 PM
how can I safely unplug an usb device? smau Linux - Hardware 5 03-14-2006 06:30 AM
Unmounting usb-device with konqueror matrem Linux - Software 1 04-13-2005 02:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer

All times are GMT -5. The time now is 06:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration