this post was submitted on 28 Jan 2025
23 points (100.0% liked)

Linux

49393 readers
1748 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

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

top 14 comments
sorted by: hot top controversial new old
[–] mvirts@lemmy.world 4 points 1 day ago

Only way I know of is to unmount the binded folder. I don't think there is a way to have the filesystem show files from both at the same time.

[–] whaleross@lemmy.world 6 points 2 days ago (1 children)

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.

[–] linuxPIPEpower@discuss.tchncs.de 2 points 2 days ago (1 children)

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 1 points 1 day ago* (last edited 1 day 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.)

[–] Zucca@sopuli.xyz 6 points 2 days ago (1 children)

You need to

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

before you

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

[–] linuxPIPEpower@discuss.tchncs.de 2 points 2 days ago (1 children)

The results are the same no matter which order I do the mounts in.

[–] Zucca@sopuli.xyz 3 points 2 days ago

So... are you saying the contents of /home/user/folderApple-original really changes after you bindmount something over /home/user/folderApple? This seems odd. Do you have submounts under the original directory? Try with --rbind?

[–] Badabinski@kbin.earth 2 points 1 day ago

Sounds like you may want to use a union filesystem like overlayfs. I'm not sure if the specific behavior of overlayfs will work for you, but it's worth investigating.

Thank you for putting your use-case in your post, since otherwise I think this might be an XY problem.

EDIT: There's also mergefs and unionfs. I don't know what the features and drawbacks are for these three union filesystems. mergefs seems like it might be the most configurable, but it's also FUSE. unionfs and overlayfs are both in-kernel, so they'll perform better (which may not matter for your use-case). overlayfs is the one I'm most familiar with of those two, since it's used by most container runtimes.

[–] thingsiplay@beehaw.org 1 points 2 days ago* (last edited 2 days ago) (2 children)

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

[–] ryannathans@aussie.zone 5 points 2 days ago (1 children)

Well that's bullshit, I can mount to directories with contents

[–] thingsiplay@beehaw.org 3 points 2 days ago (1 children)

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.

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)