I have a Raspberry Pi as my NAS machine with a USB external hard drive connected. When I remove the drive or power down the machine, I don't want to SSH to the computer (non-technical people will be using it).

How should I configure the machine so there is the least issues? FSCKing a 2TB drive takes a few hours so I'd like to avoid that if possible. Can I add options fstab to lessen the chance of this?

  • fsck should not take any amount of hours unless the drive actually needs to be repaired. Try umounting it cleanly, then run fsck; notice it will return almost immediately. – goldilocks Feb 16 '13 at 20:29

How about wiring a shutdown button and/or an unmount button to the GPIO? Then you would have some script that waits for a button-press and runs the appropriate action, perhaps lighting a LED when it is safe to unplug the drive.

Another idea would be to unmount the disk after some time without activity. Routinely removing the drive without unmounting it seems like a bad idea, regardless of the fstab settings. However, the ext3 or ext4 file systems are a lot safer than ext2. Because of journaling, they should remain in a consistent state even if the drive is suddenly unplugged. Also, fsck on an ext3 is fast. However, you may still lose data if a write is in progress while the drive is removed. The added safety also comes at a price: file systems with journaling are typically slower than ones without.

ADDITION: There are mount options that help prevent data loss. (Super User answer, unix SE question) The most extreme is sync, which will write changes to the disk as fast as possible. It should then be safe to remove the drive, once there is no write activity. Note that sync is bad for performance and causes unnecessary wear on flash memory. For FAT file systems, there is another less drastic option, flush.

From the patch introducing flush:

Fat is commonly used on removable media. Mounting with -o flush tells the FS to write things to disk as quickly as possible. It is like -o sync, but much faster (and not as safe).

The dirsync option might be a good idea together with flush, to ensure that newly created directories are synced properly.

| improve this answer | |
  • Removing the drive without unmounting is definitely a bad idea and if you intend to rely on the system for any serious purpose do not regard that as a real option, because it isn't ;) – goldilocks Feb 11 '13 at 12:06
  • What is the easiest way to unmount on no activity and remount when it is wanted? My DD-WRT NAS would not ever have to fsck the drive and they pulled the plug on it many times with the drive still attached. – Paul Tarjan Feb 12 '13 at 8:17
  • I don't know how to detect inactivity. But I found some info on the sync and flush mount options, which I added to the answer. – Frepa Feb 12 '13 at 12:12

When I remove the drive or power down the machine

These are two different issues.

When you "power down" the machine, you must instruct it to do so, and it will cleanly unmount all filesystems as part of the shutdown process...unless by "power down" you mean you just pull the plug, which is a very bad habit and will cause you problems sooner or later.

What options should I have in my fstab to help prevent data corruption and forced fscks?

If you mean, "I want to just unplug the drive (or the pi) without properly unmounting a filesystem, but I don't want to risk data corruption or have to run fsck," then you are out of luck. Either you properly unmount the filesystem, or you don't. If you don't want data corruption, then you do the former. If you don't care, then do the later, but in that case, the system will notice and will run fsck when you remount.

Maybe you need to clarify in more detail what it is you want to do? There is no option whereby you just pull the usb cable out without doing anything else, but there are ways to communicate other than ssh, or ways to automate sending commands with ssh. For example, create this script and save it on the pi into the home directory of whatever user account you ssh into as "test.sh":

echo "hello world"

Make it executable (chmod +x test.sh).

Now from somewhere else, try:

ssh me@ '~/test.sh'

Notice what happens, and that you don't log into a shell. So you can alias that command or put it another short script and instead of the 'test.sh', you execute umount or whatever you want.

If you want to cleanly shutdown (and dismount) without another computer, one option is to use udev settings regarding some device that you can unplug from the pi, see here.

But if you want a way to cleanly unmount a device by pulling its plug -- not possible. By analogy, this is like saying, "I want the gas to stop flowing when I pull the pump handle out, but I don't want to have to release the trigger". I suppose that could be possible, if we redesigned gas pump nozzles and the hole on the side of your car. USB ports are in the same situation. The system does recognize the device is removed, but at that point it is too late.

| improve this answer | |
  • SSHing is way above the level of my housemates. My DD-WRT NAS would not ever have to fsck the drive and they pulled the plug on it many times with the drive still attached. Same filesystem (ext3). – Paul Tarjan Feb 12 '13 at 8:20
  • In reality if no one is making use of the NAS when the plug gets pulled, you are unlikely to end up with corruption. Having fsck run is not a bad thing, and (if the drive is really okay anyway) shouldn't take very long. – goldilocks Feb 12 '13 at 12:48

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.