I have an Ubuntu server where I'm automounting an external hard drive each boot.

To do this, I've created an empty folder on the root partition, and the drive gets mounted "inside" this folder.

However, what if I perform a backup to this path when the drive isn't properly mounted? The backup would instead fill up my root partition!

I can ensure that the drive is mounted each time by performing:

sudo mount -a

... before each backup.

However, what are the best practices to ensure that data is never written to the empty mount-folder (except when the external hard drive is truly mounted)?

Can this be solved without scripting? Say with permissions for example? What are the best practices?


2 Answers 2


I go a step further and always set the attributes of my mountpoint directories to immutable using chattr.

This is accomplished with chattr +i /mountpoint (with the mount unmounted).

This would error-out on new write activity and also protects the mount point in other situations.

But I suppose you could use the mountpoint command, too ;)

  • 5
    This is a better solution; it requires no scripting to ensure nothing ever gets written to the root partition. To me, this seems like a best practice that should be done for all folders that will be exclusively used for mounting.
    – LonnieBest
    Jan 27, 2014 at 22:33
  • 1
    @LonnieBest Thank you. I strive to make sure any removable or NFS/CIFS or major data partitions are mounted this way.
    – ewwhite
    Jan 27, 2014 at 22:35
  • 1
    That is even better - I like it!
    – fukawi2
    Jan 28, 2014 at 2:22
  • 3
    How can you ensure that files will not be written to the local file system if you are a non-root user? You see, chattr +i requires root permissions, and even if you do it using root, thereafter a non-root user cannot mount to this protected mountpoint. So how does non-root user achieve this?
    – LonnieBest
    Jun 27, 2016 at 18:18
  • 1
    Presumably using chattr is a great idea iff there's no need to add/move files in the root of the mountpoint?
    – mwfearnley
    Apr 5, 2018 at 15:25

To expand on the comment about using mountpoint, this is roughly what I put into scripts when I need to check these kind of things:

if ! mountpoint -q "$DEST" ; then
    echo "Destination is not mounted; attempting to mount"
    mount $DEST
    if ! mountpoint -q "$DEST" ; then
        echo "Unable to mount $DEST; Aborting"
        exit 1
    echo "Mounted $DEST; Continuing backup"

This assumes that $DEST exists in /etc/fstab; it doesn't matter if it is an auto or noauto mountpoint.

As per the mount man page:

If only directory or device is given, for example:

mount /dir

then mount looks for a mountpoint and if not found then for a device in the /etc/fstab file. It's possible to use --tar‐get or --source options to avoid ambivalent interpretation of the given argument. For example

mount --target /mountpoint
  • Is $DEST the path-location to the (potentially empty) mount-folder? How does the command "mount $DEST" know what to mount there? Could you edit this post to include an example value for the variable $DEST?
    – LonnieBest
    Jan 27, 2014 at 22:24
  • 1
    @LonnieBest Done.
    – fukawi2
    Jan 28, 2014 at 2:21
  • Thanks man. I wonder how the mount command knows which device is associated with /mnt/backup folder. Does it cross-reference with the fstab?
    – LonnieBest
    Jan 28, 2014 at 3:59
  • 1
    @LonnieBest Yes, if you only provide 1 argument to mount it will consult /etc/fstab to determine the rest of the information. I'll update the answer with an excerpt from the man page.
    – fukawi2
    Jan 28, 2014 at 4:02

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