Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I tried to union two directories together and failed. Here is a transcript of what I tried, and based on tutorials and information I've seen from googling around, I'm pretty sure this should work. I guess aufs2 is generally preferred and there's also mddhfs but I can't find any documentation on the latter and the former is not in the repositories (also, I don't want to install build-essentials etc because I actually want to install them in the union, not in the main file system).

Anyway, I set up the following test scenario

user@host:~$ cd Desktop
user@host:~/Desktop$ mkdir test
user@host:~/Desktop$ cd test
user@host:~/Desktop/test$ mkdir test1
user@host:~/Desktop/test$ mkdir test2
user@host:~/Desktop/test$ mkdir test3
user@host:~/Desktop/test$ echo "this is file A" > test1/fileA.txt
user@host:~/Desktop/test$ echo "this is file B" > test2/fileB.txt

Then I merge the test1 and test2 directories as such

unionfs-fuse /home/user/Desktop/test/test1=RW:/home/user/Desktop/test/test2=RO /home/user/Desktop/test/test3

And I get the expected result

user@host:~/Desktop/test$ unionfs-fuse /home/user/Desktop/test/test1=RW:/home/user/Desktop/test/test2=RO /home/user/Desktop/test/test3
user@host:~/Desktop/test$ ls -l test3
-rw-r--r-- 1 user user 14 2010-06-25 11:34 fileA.txt
-rw-r--r-- 1 user user 14 2010-06-25 11:34 fileB.txt

I then unmount the new directory

user@host:~/Desktop/test$ sudo umount test3

And then try this

user@host:~/Desktop/test$ unionfs-fuse /home/user/Desktop/test/test1=RW:/home/user/Desktop/test/test2=RO /home/user/Desktop/test/test1
fuse: mountpoint not empty
fuse: if you are sure this is safe, use the 'nonempty' mount option

Which I was definately not expecting. So I figured "what the heck, lets try it"

user@host:~/Desktop/test$ unionfs-fuse /home/user/Desktop/test/test1=RW:/home/user/Desktop/test/test2=RO /home/user/Desktop/test/test1 -o nonempty
user@host:~/Desktop/test$ ls -l test1

The first command executes, but the second freezes... Any one care to explain why? I thought unionfs allowed merging of two directories into one of the two as the mount point. At least, that's what a number of the unionfs tutorials have indicated. Is there a way to do what I want? Some other options?

share|improve this question
    
I've had unionfs-fuse forkbomb on me, so I'm not sure it's very mature. There are other options: mhddfs (fuse-based, in debian), aufs (out of tree, but built with ubuntu kernels). Hopefully Val Aurora will get union mounts done right and in-kernel. – Tobu Jun 25 '10 at 21:44

Here is a use case that was succcessfully tested on a (K)Ubuntu system (Linux step3 3.13.0-63-generic #103-Ubuntu SMP x86_64 x86_64 x86_64 GNU/Linux).

All noted commands were executed with root rights. Please acknowledge that the following is just a sort of proof-of-concept, adjust the arguments to the commands to your individual needs.

For a standard (K)Ubuntu installation please ensure that the package "unionfs-fuse" is installed - it may be installed using the command line "sudo apt-get install unionfs-fuse".

For example the following instructions describe how to overlay an existing, non empty (user home) directory "with itself" so that all changes are stored persistent in a directory (case 1.) or discarded anyway (case 2.).

The "trick" is to first "mount bind" the directory to overlay to another one, then use an union file system to combine the unchangeable original with the "changes" directory.

Files created or modified in the "original" directory will be available only in the "changes" directory. Files created in the "relocated" directory will be persistent available in the "original" directory, but not in the "changes" directory.

  1. Make (non empty) dir (e.g. /home/user1) unwritable so that all changes and all newly created files will stay persistent (in /tmp/home_user1_changes)

1.1 Create auxiliary directories

1.1.1 mkdir /tmp/home_user1_changes

1.1.2 mkdir /tmp/home_user1_relocated

1.2 Use the mount bind option to mount the directory to the "overlay unwritable" dir (/tmp/home_user1_relocated)

1.2.1 mount --bind /home/user1 /tmp/home_user1_relocated

1.3 Use unionfs file system to combine the relocated original contents with the contents of the "changes" dir

1.3.1 unionfs-fuse -o cow -o allow_other -o use_ino -o nonempty /tmp/home_user1_changes=RW:/tmp/home_user1_relocated=RO /home/user1

[now all changes to /home/user1 are saved in /tmp/home_user1_changes, but not in "overlaid" /home/user1]

1.4 Rollback

1.4.1 umount /home/user1

1.4.2 umount /tmp/home_user1_relocated

1.4.3 optionally inspect files in /tmp/home_user1_changes

1.4.4 optionally rm -fR /tmp/home_user1_changes, rmdir /tmp/home_user1_relocated

[/home/user1 in state as before 1., modifications since then reside in /tmp/home_user1_changes]

  1. Make (non empty) dir (e.g. /home/user1) unwritable so that all changes and all newly created files persist only temporarily (in /tmp/home_user1_changes)

2.1 Create auxiliary directories

2.1.1 mkdir /tmp/home_user1_changes

2.1.2 mkdir /tmp/home_user1_relocated

2.2 Use the mount bind option to mount the directory to the "overlay unwritable" dir (/tmp/home_user1_relocated)

2.2.1 mount --bind /home/user1 /tmp/home_user1_relocated

2.3 Force the /tmp/home_user1_changes dir to be temporary

2.3.1 mount -t tmpfs -o size=20% none /tmp/home_user1_changes

2.4 Use unionfs file system to combine the relocated original contents with the contents of the "changes" dir

2.4.1 unionfs-fuse -o cow -o allow_other -o use_ino -o nonempty /tmp/home_user1_changes=RW:/tmp/home_user1_relocated=RO /home/user1

[now all changes to /home/user1 are saved in /tmp/home_user1_changes, but not in "overlaid" /home/user1]

2.5 Rollback

2.5.1 umount /home/user1

2.5.2 umount /tmp/home_user1_relocated

2.5.3.1 optionally inspect files in /tmp/home_user1_changes

2.5.3.2 umount /tmp/home_user1_changes [will make changes disappear]

2.5.4 optionally rm -fR /tmp/home_user1_changes, rmdir /tmp/home_user1_relocated

[/home/user1 is now in state as before 1., modifications are all gone]

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.