It’s a strange quirk of the way Linux mounts drives, that it is entirely possible to write to the wrong drive. So with that in mind we’re going to look at how to prevent writes to an unmounted drive.
MOUNTING BASICS (very)
As I’m sure you’re aware if you’re reading this post, mounting a drive or remote storage under Linux is very simple.
- Create a mountpoint (local folder) often under /mnt.
- Mount (attach) the remote storage to the mountpoint.
- Write the files to the mountpoint.
This works well until the remote storage becomes unmounted. This can happen for a number of reasons, but as soon as it does – any subsequent writes to the remote storage will in fact be written to the local folder.
Whilst this can be seen as inconvenient, it can in fact be disastrous.
WHY IT MATTERS
Let’s consider the critical process of backing up files.
Your remote storage is used for backups. Every night your backup process writes to /mnt/backupstore which maybe a NAS, a server in another building or a Cloud based service.
You check the backup logs daily and do test restores regularly.
Then one day the drive becomes unmounted.
Your backup application continues to backup to /mnt/backupstore. But instead of writing to remote storage, it’s now writing to the local folder on your server.
You continue to check the backup logs daily and do test restores regularly. Everything works as expected.
Then one day your disk dies, your raid set becomes corrupted or some tealeaf just nicks your server.
You then kick off a restore from your remote storage and wonder why your backups are two years out of date. Unless you’re lucky enough to run out of disk space on your local storage you may never find out what’s happened until you need that data.
So as you can see, learning how to prevent writes to an unmounted drive can be a life saver.
SUCH A SIMPLE FIX
As you’ve probably gleaned from the section header, the fix is oh so simple and is just one command.
With the remote storage unmounted, use the following:
> sudo chattr +i [/mnt/mountpoint]
where /mnt/mountpoint is your local folder.
The +i sets the folder as immutable. This means no renaming, no writing, and no symbolic link creation – nada – nothing – nowt.
When you mount your remote storage to the mountpoint, the remote storage is writable as usual. However, as soon as the remote storage becomes unmounted, no writes are possible – which should start generating a whole heap of errors.
Such a simple fix, which should be part of any mount creation instructions.