linuxmountunmountunionfs

how to mount additively on linux


I have been trying to mount directories in linux additively and fail to do so. I have three directrories a, b and c. a contains file x, b contains file y and c contains file z. Now when i mount "a" and "b" into c and then open c there are only x and y present in "c". when i mount a, b and c into c the directory c is empty and ls says permission denied. i cannot also unmount c even with sudo. What i want to do is to mount a b and c in one of them and be able to see x y and z all together. is it possible or is there a solution to this problem please let me know.

naveed@ubuntu:~$ mkdir /tmp/test1 /tmp/test2 /tmp/test3
naveed@ubuntu:~$ touch /tmp/test{1,2,3}/{a,b,c}
naveed@ubuntu:~$ rm /tmp/test1/{b,c}
naveed@ubuntu:~$ rm /tmp/test2/{a,c}
naveed@ubuntu:~$ rm /tmp/test3/{a,b}
naveed@ubuntu:~$ ls -al /tmp/test*/
/tmp/test1/:
total 16
drwxrwxr-x  2 naveed naveed  4096 Mar  9 15:00 .
drwxrwxrwt 11 root   root   12288 Mar  9 15:00 ..
-rw-rw-r--  1 naveed naveed     0 Mar  9 15:00 a

/tmp/test2/:
total 16
drwxrwxr-x  2 naveed naveed  4096 Mar  9 15:00 .
drwxrwxrwt 11 root   root   12288 Mar  9 15:00 ..
-rw-rw-r--  1 naveed naveed     0 Mar  9 15:00 b

/tmp/test3/:
total 16
drwxrwxr-x  2 naveed naveed  4096 Mar  9 15:00 .
drwxrwxrwt 11 root   root   12288 Mar  9 15:00 ..
-rw-rw-r--  1 naveed naveed     0 Mar  9 15:00 c
naveed@ubuntu:~$ sudo unionfs-fuse -o nonempty /tmp/test1=RO:/tmp/test2=RO:/tmp/test3=RO /tmp/test1/
naveed@ubuntu:~$ ls -al /tmp/test1 
ls: cannot access /tmp/test1: Permission denied
naveed@ubuntu:~$ sudo ls -al /tmp/test1 
#nothing shows up here 

Solution

  • This is really a SuperUser question, so I've flagged it as such.

    If you're creating a unioned file system of multiple sources, then all the source locations need to be accessible by the user that's trying to access the directory.

    Take for example:

    $ mkdir a; touch a/a
    $ mkdir b; touch b/b
    $ mkdir c; touch c/c
    $ mkdir join
    $ ls join
    drwxr-xr-x 4 petesh petesh 4096 Mar  9 10:25 a/
    drwxr-xr-x 2 petesh petesh 4096 Mar  9 10:19 b/
    drwxr-xr-x 2 petesh petesh 4096 Mar  9 10:20 c/
    drwxr-xr-x 4 petesh petesh 4096 Mar  9 10:25 join/
    
    $ sudo mount -t aufs -o br:(pwd)/a:(pwd)/b:(pwd)/c none (pwd)/join
    $  ls join
    a  b  c
    

    i.e. we can see all the content.

    change the permissions on one of the folders - e.g.

    $ chmod u-rwx a
    

    and now when we ls the join folder:

    $ ls join
    ls: cannot open directory join: Permission denied
    

    i.e. all folders need to be accessible in order for the joined directory to be accessible.

    Make sure that the permissions are correct for accessing all the directories - the union file system enforces the permissions of the underlying directories, you can't bypass the OS's protection using this.

    Next, if we mount them all into one folder:

    $ sudo mount -t aufs -o br:(pwd)/a:(pwd)/b:(pwd)/c none (pwd)/c
    $ ls c
    a  b  c
    

    i.e there's no issue mounting them all over one directory, making the mounted directory look different to the underlying directory.