68

On my headless NAS I have sdf1 (a flash-card) mounted as / while /home is mounted from lv00 (an LVM volume backed by a software RAID). To be able to access the machine when the RAID fails, I have a copy of my ssh public key, etc. in /home/foo/.ssh on the file-system from sdf1.
To update the files that are hidden by the mounted /home I normally remount lv00 in /mnt/home, do what I have to do, and then move lv00 back in place.
Is there a way to achieve this without unmounting /home?

3 Answers 3

90
mkdir /mnt/root
mount --bind / /mnt/root
ls /mnt/root/home/foo/.ssh

As long as you use --bind (as opposed to --rbind), you get a clone of the mount without the stuff mounted on top of it.

5
  • Perfect! I was toying with the idea of mount --bind, but only got some convoluted ideas that were sure to break when I needed it. This does exactly what I need and can even go in as a permanent mount. Thanks
    – Janus
    Commented Nov 30, 2010 at 5:23
  • 1
    it's a dup of unix.stackexchange.com/questions/49345/…
    – poige
    Commented Apr 5, 2013 at 18:42
  • 2
    Is it worth editing this answer to include the fact that you have to bind-mount a level above the directory you have covered with a mount, as is covered in the other answer? This answer is otherwise better, because it is shorter and hence easier to read quickly! Commented Mar 5, 2019 at 16:03
  • I finally found what is eating up my disk space. What a life saver.
    – Dyin
    Commented Jan 8, 2022 at 5:46
  • For a little more clarity, you can't mount the target directly, but you can mount its parent, e.g. mkdir /home/home; mount --bind /home /home/home, then if you're user "foo" you can run ls /home/home/foo/.ssh
    – Adam Katz
    Commented Jan 24, 2023 at 3:56
8

I've tried to achieve something similar, but ephemient's answer didn't explain the semantics of the method. It failed for me and so I asked virtually the same question earlier here on unix.SE. After a comment I figured it out on my own and answered it. This is an edited version of my answer to fit into this context here. I removed my other question (and answer) in favor of this one.

Here's what I was trying to do:

Example case

Mounts:

/dev/sda1  on /     type ext4 (rw)
/dev/sdb1  on /data type ext4 (rw)
/data/home on /home type none (rw,bind)

After mounting / I have a folder /home/joe for user joe. Once the other location gets mounted (/data) I have the full set of home folders available, so I am bind-mounting them into place (/data/home on /home). There is a folder /data/home/joe, so as long as the mounting of /dev/sdb1 succeeds, he'll get the contents of /data/home/joe, otherwise he'll fall back to /home/joe (on /dev/sda1!).

When it succeeds, how can I access the original contents of /home/joe (on /dev/sda1!) instead of those bind-mounted into place from /data/home/joe?

Solution

Based on a comment by Patrick's comment on my question and the solution by ephemient (accepted answer here), I came up with the following.

It is apparently possible to mount --bind (or mount -o bind) the parent folder (this is the crucial part) of a bind-mount elsewhere and thereby access the original contents. So for my example case, instead of trying to:

mount --bind /home/joe /home/joe/underneath
# or ...
mount --bind /home /home/joe/underneath

(i.e. mount the already bind-mounted locations elsewhere) I had to:

test -d /.ROOT || mkdir /.ROOT
mount --bind / /.ROOT
mount --bind /.ROOT/home/joe /home/joe/underneath

So this is what Patrick meant in his comment

Unless you're remounting over / (root), that answer should work just fine.

As long as you have a parent folder to the bind-mounted location available, it'll work, albeit with one indirection as shown above. If you bind-mounted something over / you're out of luck, as there is no parent folder for /.

3
  • Mounting the parent folder was the key part that I was missing. Thanks.
    – aidan
    Commented Mar 6, 2018 at 1:11
  • Ditto with the parent folder. Intuitively, I thought you should bind mount the original mount point, but instead you mount the bind mounted directory.
    – Manngo
    Commented May 29, 2020 at 8:47
  • You can just mount the device that originally was mounted as / if you have a later bind-mount on /. It's safe: unix.stackexchange.com/a/276399/5589
    – pepoluan
    Commented Mar 25, 2022 at 2:09
1

You can move the mount to a new location without unmounting it, using mount --move:

$ mount --move /home /mnt/home
do stuff with the local /home
$ mount --move /mnt/home /home
2
  • 1
    Thanks Michael. This has the same disadvantage of making /home inaccessible to other users.
    – Janus
    Commented Nov 30, 2010 at 5:18
  • @Janus You can unshare the mount namespace, then.
    – Melab
    Commented Feb 10, 2021 at 4:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .