3

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

  • 1
    Probably the best you can do is to mount with the sync option. However, it's always safer to unmount a file system. – Marco Sep 10 '13 at 22:44
  • 2
    This is just something you have to remember to do. I've never seen anything available that can do what you're asking. – slm Sep 10 '13 at 22:44
  • @slm, if it was mac os x, you simply wait for the usb drive to stop blinking if it has a led light on it for example. ( all my usb drives have lights ). and then i'd remove it. why can't it be like that in centos linux as well ? – user46875 Sep 10 '13 at 22:48
  • @user46875 No, you should unmount in OS X before unplugging, too. – endolith Feb 2 '16 at 2:12
  • this question is on-topic: Using or administering a *nix desktop or server – ctrl-alt-delor Sep 17 '16 at 10:08
0

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.

11

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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