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.)

  • whaleross@lemmy.world
    link
    fedilink
    arrow-up
    6
    ·
    2 days ago

    The easy solution would be to have a third common mount point for the two that is switched if the external drive is connected or not.

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

      In another subthread I came up with the below, is this what you mean? I haven’t tried it yet.

      • /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)
      • whaleross@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        2 days ago

        Basically, yeah. Bind the “local” path on boot and then have systemd triggers for when USB mounts and unmounts to swap them automatically.

        (Personally I wouldn’t do it like this though because it will become trouble with any open files or shell or whatever in a path that is replaced by a different mount.)