
所以我尝试将两个目录合并在一起,但失败了。这是我尝试的记录,根据我在谷歌上搜索到的教程和信息,我很确定这应该可行。我猜 aufs2 通常是首选,还有 mddhfs,但我找不到关于后者的任何文档,而前者不在存储库中(此外,我不想安装 build-essentials 等,因为我实际上想将它们安装在联合中,而不是主文件系统中)。
无论如何,我设置了以下测试场景
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
然后我合并 test1 和 test2 目录
unionfs-fuse /home/user/Desktop/test/test1=RW:/home/user/Desktop/test/test2=RO /home/user/Desktop/test/test3
我得到了预期的结果
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
然后我卸载新目录
user@host:~/Desktop/test$ sudo umount test3
然后尝试一下
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
这绝对是我没想到的。所以我想“管他呢,试试看吧”
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
第一个命令执行了,但第二个命令冻结了...有人能解释一下为什么吗?我以为 unionfs 允许将两个目录合并为一个作为挂载点。至少,许多 unionfs 教程都指出了这一点。有办法做我想做的事吗?还有其他选择吗?
答案1
这是一个在 (K)Ubuntu 系统 (Linux step3 3.13.0-63-generic #103-Ubuntu SMP x86_64 x86_64 x86_64 GNU/Linux) 上成功测试的用例。
所有提到的命令都是以 root 权限执行的。请注意,以下内容只是一种概念验证,请根据您的个人需求调整命令的参数。
对于标准 (K)Ubuntu 安装,请确保安装了包“unionfs-fuse” - 可以使用命令行“sudo apt-get install unionfs-fuse”进行安装。
例如,以下说明描述了如何将现有的非空(用户主)目录“与其自身”覆盖,以便所有更改都持久存储在目录中(情况 1)或无论如何都被丢弃(情况 2)。
“技巧”是首先“挂载绑定”目录以覆盖另一个目录,然后使用联合文件系统将不可更改的原始目录与“更改”目录结合起来。
在“original”目录中创建或修改的文件将仅在“changes”目录中可用。在“relocated”目录中创建的文件将永久存在于“original”目录中,但不存在于“changes”目录中。
- 使(非空)目录(例如 /home/user1)不可写,以便所有更改和所有新创建的文件将保持持久(在 /tmp/home_user1_changes 中)
1.1 创建辅助目录
1.1.1 mkdir /tmp/home_user1_changes
1.1.2 mkdir /tmp/home_user1_relocated
1.2 使用 mount bind 选项将目录挂载到“覆盖不可写”目录 (/tmp/home_user1_relocated)
1.2.1 mount --bind /home/user1 /tmp/home_user1_relocated
1.3 使用 unionfs 文件系统将重定位的原始内容与“changes”目录的内容合并
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
[现在对 /home/user1 的所有更改都保存在 /tmp/home_user1_changes 中,但没有保存在“覆盖”的 /home/user1 中]
1.4 回滚
1.4.1 卸载 /home/user1
1.4.2 卸载 /tmp/home_user1_relocated
1.4.3 选择性地检查 /tmp/home_user1_changes 中的文件
1.4.4 可选 rm -fR /tmp/home_user1_changes、rmdir /tmp/home_user1_relocated
[/home/user1 处于 1. 之前的状态,此后的修改位于 /tmp/home_user1_changes]
- 使(非空)目录(例如 /home/user1)不可写,以便所有更改和所有新创建的文件仅暂时保留(在 /tmp/home_user1_changes 中)
2.1 创建辅助目录
2.1.1 mkdir /tmp/home_user1_changes
2.1.2 mkdir /tmp/home_user1_relocated
2.2 使用 mount bind 选项将目录挂载到“覆盖不可写”目录 (/tmp/home_user1_relocated)
2.2.1 mount --bind /home/user1 /tmp/home_user1_relocated
2.3 强制将 /tmp/home_user1_changes 目录设为临时目录
2.3.1 mount -t tmpfs -o size=20% none /tmp/home_user1_changes
2.4 使用 unionfs 文件系统将重定位的原始内容与“changes”目录的内容合并
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
[现在对 /home/user1 的所有更改都保存在 /tmp/home_user1_changes 中,但没有保存在“覆盖”的 /home/user1 中]
2.5 回滚
2.5.1 卸载/home/user1
2.5.2 卸载 /tmp/home_user1_relocated
2.5.3.1 选择性地检查 /tmp/home_user1_changes 中的文件
2.5.3.2 umount /tmp/home_user1_changes [将使更改消失]
2.5.4 可选 rm -fR /tmp/home_user1_changes、rmdir /tmp/home_user1_relocated
[/home/user1 现在处于与 1 之前相同的状态,修改全部消失]