Jump to content

Detecting USB FAT write is finished


teledyn
 Share

Recommended Posts

I have a Blackberry phone that uses a memory card formatted as an 1980's vintage MSDOS FAT drive; because RIM is also staunchly anti-Linux :wall: I must remove the memory card from the phone and use a USB adaptor to transfer files to my computer. Unfortunately, USB FAT writes are quite slow, and when I remove the card from the USB reader before the writes are finished, the FAT is trashed, all data is lost and I have to reformat the card before I can use it.

 

in pre-2007 Mandriva, before the /media/disk convention, I think there was an fstab mount option to mark MSDOS partitions synchronous and block any processes accessing the drive until the write operation was finished. I need something like that now so I can tell when the writes are finished and it is safe to pull the card from the reader.

 

Is there any way to do this with a USB-mounted drive?

 

Is there any way to detect if an MSDOS partition is still 'busy' and should not be disconnected?

Link to comment
Share on other sites

Any non-read-only removable device can safely be removed only after it has been unmounted.

 

And before people say "yet that's not the way it is with Windows", I'll say: yes it is. Once I removed the floppy too soon on Windows; it made no noise anymore so I though all was completed; it obviously was not: the drive spinned up again the moment I removed the floppy and it was trashed.

 

Besides, now in Windows there's this "remove the media securely" thing (translated from French), which is just Windows way to say "unmount media before removing".

 

So in short: unmount your media and you'll be OK.

Supermount and such things in linux were made because they are convenient (especially with read-only media such as CDs) and Windows-like, but if you use it, you have to accept the risk alongside the convenience. In Windows it is the same, except you have no choice.

 

Yves.

Link to comment
Share on other sites

I must remove the memory card from the phone and use a USB adaptor to transfer files to my computer. Unfortunately, USB FAT writes are quite slow, and when I remove the card from the USB reader before the writes are finished, the FAT is trashed, all data is lost and I have to reformat the card before I can use it.

With Mandriva 2007, if I right-click on a usb-storage-icon, it asked if I would like to 'Safely Remove', and it will not do so until all writing has stopped.

Link to comment
Share on other sites

With Mandriva 2007, if I right-click on a usb-storage-icon, it asked if I would like to 'Safely Remove', and it will not do so until all writing has stopped.

Excellent! Thanks -- good to know. So it is possible to detect, so now my question can be better phrased as How can I do the same 'busy' test on the command line? -- I don't have the system resources to run Nautilus all the time, and while I do get a notification when a blank CD is inserted, I don't get any notifications or icons about USB devices. All my programs for accessing my camera and my phone-card are all shell-scripts.

 

So is it possible to do the same 'busy' test from a shell script?

Edited by teledyn
Link to comment
Share on other sites

umount /device/name or umount /mount/point

Should give you an error if it is busy.

Yes, and maybe this is changed, but in 2007.0 when I did this, the device would no longer be auto-mounted when a card was re-inserted, and I would be stuck with doing manual mount/umount until the next reboot.

 

But clearly whatever program it is that is managing the above mentioned USB-drive icon must have some sort of benign test it can do, a /proc pseudofile it can check or something passive that can deliver the status answer without imparing the system.

 

what program manages that icon? there may be a clue in the sources (which is what open source is all about ;)

 

on a related question, whenever I insert a blank CD I get a dialog that says "blank CD inserted: ignore, burn data, burn music" or something like that, only, no matter which option I choose, it does nothing. clearly I'm missing some essential bit of software or config but beyond that, when I insert a CD I already know what I wanted to do with it as I have been inserting CDs into Linux machines since InfoMagic introduced the box-set ;) Can I turn this auto-alert off?

Link to comment
Share on other sites

the device would no longer be auto-mounted when a card was re-inserted, and I would be stuck with doing manual mount/umount until the next reboot.

How are you mounting your memory card with your script?

 

Here's an example of mine:

cd /mnt/removable
ls

I have:

flash-p/ music/ seatools_cli.tar*

then do

cp -R music  /home/greg/

it will not return to a prompt until the writing is completed.

 

If you want to do this with a shell script, you will have to edit it accordingly.

Link to comment
Share on other sites

You can try running:

 

$ sync

 

before removing the media. That will flush the filesystem buffers in ram to disk. From info sync:

 

`sync' writes any data buffered in memory out to disk. This can

include (but is not limited to) modified superblocks, modified inodes,

and delayed reads and writes. This must be implemented by the kernel;

The `sync' program does nothing but exercise the `sync' system call.

 

The kernel keeps data in memory to avoid doing (relatively slow) disk

reads and writes. This improves performance, but if the computer

crashes, data may be lost or the file system corrupted as a result.

The `sync' command ensures everything in memory is written to disk.

 

Any arguments are ignored, except for a lone `--help' or `--version'

(*note Common options::).

 

An exit status of zero indicates success, and a nonzero value

indicates failure.

 

IIRC when you unmount a filesystem, the sync command is automatically run before the dismount.

Link to comment
Share on other sites

You can try running:

 

$ sync

 

before removing the media. That will flush the filesystem buffers in ram to disk.

Yes of course! I should have thought of that ;)

Just tried it now, loading a whack of mp3s across, and it appears to have worked; I yanked the card the instant sync was done (and it paused several seconds) but the card appears to be un-trashed.

 

plus the status code result will be a useful bit of knowledge to have at the end.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...