11

Sometimes I unplug my USB drive only to find out files were not written to it.

I suppose the only way to ensure files are written to it is to right click on the USB drive on the desktop and then select "un-mount", but sometimes I forget.

What is the best way to ensure files are written to the USB drive instantly? This way I can remove the USB drive as soon as I notice the LED light on the USB drive stopped blinking.

OS: CentOS 6

  • Disable the write cache. Maybe hdparm -W0 /dev/sdX (where sdX is your USB device) can do this, but I'm not really sure. Also I don't know how one can disable the write cache for USB devices by default. – Martin von Wittich Sep 14 '13 at 19:55
  • 1
    @MartinvonWittich - the other one is a post by a known troll, I'm attempting to save my answer and Gilles' answers and also a decent question. – slm Sep 14 '13 at 20:16
16

This is Gilles' answer, saving it here so it doesn't get lost.

If you use the sync mount option on the removable drive, all writes are written to the disk immediately, so you won't lose data from not-yet-written files. It's a bad idea, but it does what you're asking, kind of.

Note that sync does not guarantee that you won't lose data. Unmounting a removable drive also ensures that no application has a file open. If you don't unmount before unplugging, you won't notice if you have unsaved data until it's too late. Unmounting while a file is open also increases the chance of corruption, both at the filesystem level (the OS may have queued some operations until the file is closed) and at the application level (e.g. if the application puts a lock file, it won't be removed).

Furthermore, sync is bad for the lifetime of the device. Without the sync option, the kernel reorders writes and writes them in batches. With the sync option, the kernel writes every sector in the order requested by the applications. On cheap flash media that doesn't reallocate sectors (meaning pretty much any older USB stick, I don't know if it's still true of recent ones), the repeated writes to the file allocation table on (V)FAT or to the journal on a typical modern filesystem can kill the stick pretty fast.

Therefore I do not recommend using the sync mount option.

On FAT filesystems, you can use the flush mount option. This is intermediate between async (the default) and sync: with the flush option, the kernel flushes all writes as soon as the drive becomes idle, but it does not preserve the order of writes (so e.g. all writes to the FAT are merged).

  • 1
    @Gilles - didn't want your answer to get lost, so I copied it here, feel free to copy/paste it onto this Q&A, and I can delete this when you're done. – slm Sep 14 '13 at 21:17
  • @gilles Should I use flush with NTFS drives too? – becko Nov 22 '16 at 14:17
12

Patient: "Doctor, It hurts when I do this."
Doctor: "Well, don't do that."
maybe the Marx Brothers, but they probably stole it from other vaudevillians if so

Both answers are fine, but I do question the question. If you can't remember to unmount a thumbdrive properly before removing it, perhaps you should remember harder.

In general, the problem has no good solution. The original Macintosh (c. 1984) had a 90mm floppy drive with no proper eject button. You either unmounted the disk or it wouldn't come out. They did provide an override for when the software refused to eject the disk, but you needed a special tool (unbent paper-clip) to actuate it. The paper clip formed a Don Normanesque "forcing function" as it was hard to be unaware that you were jamming a paper clip into a 1mm hole.

Because of the mechanics of a USB connector, it is hard to devise a similar forcing function for thumbdrives. Thus the imprecation: don't remove without unmounting. Also, it is unwise to ride in an automobile without a safety belt and just about every forcing function ever attempted failed to achieve the desired end.

  • I liked Gilles answer and didn't want to let it go to the ether so I saved the Q for really no other reason. – slm Sep 14 '13 at 21:55
  • I concur. I also like my answer because it made me think about the design of thumbdrives and how the intentionally cheap USB connector wound up with a completely unanticipated consequence ("What's a thumbdrive?" would have been a utterly valid question in 1995). – msw Sep 14 '13 at 21:58
  • 1
    Just trying to make lemons into lemonade 8-) – slm Sep 14 '13 at 21:59
1

Well typically mass storage devices such as USB thumb drives are mounted using a command, mount. They need to be explicitly unmounted using the compliment to this command, umount (that's not a typo). There are tools that can detect events at the hardware level, such as, a USB device was just plugged in. These "services" will then do the mounting automatically for you. This is what's happening when you use the GNOME desktop and you'll typically see USB drives showing up as mounted in the file browser Nautilus.

It's slightly trickier to know when the device is no longer being written to. You'd have to write your own handler for this making use of Udev and DBUS messages to know when the device was done being written to each time, and then keep it explicitly in the "un-mounted" state.

I do not see any other way to do this without much deeper integration's into the various subsystems in Linux, and to my knowledge that just doesn't exist as of now.

MAC OSX

My guess would be that on OSX, they are performing sync commands and leaving the device "mounted", which can be considered a dangerous thing to do, but at a convenience for the operator.

  • Can you please elaborate on why using sync is dangerous? And in what way is sync specific to MAC OS X? – Joseph R. Sep 14 '13 at 19:09
  • 1
    @JosephR. - I did not mean that sync was specific to OSX, only that they are most likely performing one as a means of protecting the data on the disk, prior to it being removed. Running a sync is OK but it's much better to explicitly unmount. – slm Sep 14 '13 at 19:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.