I have 2 directories which both have stuff in them:

  • /home/user/folderApple

  • /mnt/drive/folderBanana

I want to mount folderBanana onto folderApple like this:

sudo mount --bind "/mnt/drive/folderBanana" "/home/user/folderApple"

But I still want to be able to access the contents of folderApple while this is activated. From what I am reading, binding the original directory to a new location should make it available, like this:

mkdir "/home/user/folderApple-original"
sudo mount --bind  "/home/user/folderApple" "/home/user/folderApple-original"

But this just binds /mnt/drive/folderBanana to /home/user/folderApple-original as well. I tried reversing the order and result is the same.

How do I tell mount to look for the underlying directory?

I am happy to use symlinks or something else if it’ll reliably get the job done, I am not wedded to this mechanism.

(The purpose of all this is that when an external drive is connected, I can have the storage conveniently available, but when it is not connected, the system will fallback to internal storage. But then I will want to move files between the fallback and external locations when both are available. So I need to see both locations at once.)

  • thingsiplay@beehaw.org
    link
    fedilink
    arrow-up
    4
    arrow-down
    3
    ·
    edit-2
    2 days ago

    You can only mount into an empty directory. (Edit: Ok that is not really correct. You can mount into directories with content, but then the content will no longer be accessible from that point.) You cannot mix two directories with mount.

    Edit: You could have two points using one Mountpoint at a time maybe. With a script you unmount the fallback, and if you connect the new drive you mount it with the script. And if you are done, use your unmount script to reverse it. Just an idea.

    Some more Edit: In short you create an empty directory that is the Mountpoint, lets say “/home/user/Apple”. Now your real local files are at “/home/user/folderApple”. You mount folderApple to Apple. This is your fallback. Then if you connect the other drive, with your script you unmount that and mount your “/mnt/drive/Banana” to “/home/user/Apple”, which is empty again after the unmount. And reverse it if you want to unplug.

      • thingsiplay@beehaw.org
        link
        fedilink
        arrow-up
        3
        ·
        2 days ago

        Alright, I didn’t know you can mount “over” a directory. But my point was, you cannot mix them, you do not mount into the directly. It just replaces it. Which also would make this directory no longer accessible, but he wants both accessible at the same time.

    • linuxPIPEpowerOP
      link
      fedilink
      arrow-up
      3
      ·
      2 days ago

      Ideally I’d like to avoid a script because my experience is they aren’t very durable. I make mistakes and they are difficult to troubleshoot. So I am trying to just use the tools that are already available in the system.

      But maybe there is something in the idea of using a second mount, like if

      • /home/user/folderApple is always empty
      • /home/user/folderApple-original mounts ontop of /home/user/folderApple at boot
      • then /mnt/drive/folderBanana also mounts ontop of /home/user/folderApple when/if it becomes available (later in the order)