After a while, I finally came to understand how bind mounts are indistinguishable from "regular" mounts in Linux (other than by being mounted with --bind). If /dev/sda1 is mounted on /mnt and /mnt/variable_data is then bind mounted on /var, the only difference between the two is that the root of the filesystem on /dev/sda1 is mounted at /mnt while the path /variable_data on that same filesystem is mounted at /var (in the virtual/root filesystem).

My question is two fold:

  1. Assuming that I have not run mount /dev/sda1 /mnt (meaning that there is no /mnt/variable_data), how can I mount the /variable_data inside sda1 at /var?
  2. How can the above be accomplished with fstab?

The simplest, and possibly only way, is to mount /dev/sda1 first. You can mount the device read only if you are concerned about accidental changes, but bind mount permissions can be tricky to get set. It is also possibly to over mount the directory on top of the device. In other words, in general, you can mount /dev/sda1 to /var and then mount /var/variable_data to /var.

A potential issue with this is that the mounting process is not atomic and there is a chance something could get written to /var in the brief moment that /dev/sda1 is mounted there. Even if /dev/sda1 is mounted read only, this could cause problems depending on how write failures are handled. As /var is often a separate partition mounted in fstab, this should not be a problem.

  • Could there be some stackable filesystem or something of the sort that achieves this. Like, you'd specify /dev/sda1 as the source, subtreefs as the type, and the subtree's path as an option? – Melab Jun 15 '17 at 23:05
  • @Melab I am not aware of a technical limitation why there couldn't be a stackable filesystem, but I am not sure what benefits it provides over a bind mount. – StrongBad Jun 15 '17 at 23:17

Judging from the man page of mount(2), I don't think you can do that directly. The system call doesn't take a path that would be used to consider a subtree of the mounted filesystem. There's just the source device, target path, filesystem type, mount flags (the common options like ro and nosuid), and an opaque data field that is interpreted by the filesystem itself (the filesystem-specific options).

It seems you need to mount the device to a temporary path, then bindmount the subtree you want, and finally unmount the first filesystem. Apart from the unmounting, you might able to do that from fstab. Something like this worked on Ubuntu (with systemd):

# fstab
/dev/somedevice      /tmpmnt      ext4      auto,rw
/tmpmnt/var          /var         none      auto,bind
  • The subtree would most likely be part of the options. – Melab Jun 15 '17 at 23:03
  • 1
    @Melab, mm, I suppose the path would have to go to the VFS layer, and the options field that takes a free-form/string argument is interpreted by the filesystem code. There's a mention in the man page of the mount(8) utility, that e.g. NFS and others want a binary argument, so it doesn't seem even possible to piggyback anything on the FS-specific options. (And there's no mention of the possibility in the manual, anyway.) – ilkkachu Jun 16 '17 at 8:14

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.