0

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 
Naveed
  • 15
  • 6
  • This site is for programming questions. Try SuperUser instead. – Sneftel Mar 09 '15 at 10:14
  • I am sorry but i want to use it in my bash script with the code that i have written if there is a way to do it with c++ i will be happy to accept that as well – Naveed Mar 09 '15 at 10:15

1 Answers1

1

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.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • You are putting three of them in the fourth folder i am trying to put a, b, c in a. without changing the user permissions. a is initially accesible but after mount its not accessible anymore – Naveed Mar 09 '15 at 10:38
  • And I've just added an example of mounting them all over `c` in this example case. Is there anything special about `a`? Is it an external drive? is it some other kind of file system? – Anya Shenanigans Mar 09 '15 at 10:47
  • Yes you are right it works with aufs thanks for the help. However it was not working with unionfs. – Naveed Mar 09 '15 at 10:54
  • That is supposed to be supported in unionfs 0.26; which isn't currently on my system (I've only got unionfs 0.24). The suggested workaround is to mount onto a separate directory and then bind-mount that back over the required folder. – Anya Shenanigans Mar 09 '15 at 11:20