unionfs 與 aufs

更新日期: 23/1/12



unionfs 與 aufs  為的都是要把多個目錄"虛擬"成一個目錄(overlays)


  1. 方便管理檔案(相關的檔案分佈於不同的地方, 如 sda1, sdb1)
  2. 想修改 readonly 的地方(如 squashfs)

而且我們可以指定某些目錄(Branch) 是 Readonly 的, 而把修改過的檔案放到另一個目錄



# 把 Dir1, Dir2 合併成一個目錄

Dir1(RW) ----------->   Dir
Dir2(RO) ----------->
Branch                 virtual filesystem


當有一個檔案同時存在於 Dir1 及 Dir2 時, 那 "top_branch" 會被先調用(Dir1)

另外當 Dir2 是 readonly, 而 Dir1 是read-write 時, 當我們修改 Dir2 上面的檔案時, 那會有同名的檔案在 Dir1 上出現 !!
(啟用 -o cow)

而 aufs(AnotherUnionFS)則是監於早期的 unionfs 不足, 因而另外建立的一套功能一切的系統

不過, 我個人比較喜歡 unionfs, 因為它有 fuse 版, 可以在 VPS 內使用



apt-get install fuse

apt-get install unionfs-fuse


unionfs-fuse [-o option1 -o option2 ... -o optionN ]
                    top_branch:lower_branch:...:lowest_branch        <-- 注意次序 !!


-o cow Enable copy-on-write

Default 每個 branch 都是 ro 的, 如果連 top_branch 都是 ro, 那會有很多錯 file 出現 !!


       1) Another issue is that presently there is no support for read-only branches
       when copy-on-write is disabled, thus, -ocow is NOT specified! Support for
       that might be added in later releases.





├── dir1
│   └── file1
├── dir2
│   ├── file2
│   ├── file3
│   └── folder
│       └── file4
└── mountpoint

4 directories, 4 files


unionfs-fuse -o cow dir1=rw:dir2=ro mountpoint


mount 後:

├── dir1
│   └── file1
├── dir2
│   ├── file2
│   ├── file3
│   └── folder
│       └── file4
└── mountpoint
    ├── file1
    ├── file2
    ├── file3
    └── folder
        └── file4

5 directories, 8 files



.unionfs 目錄

當 ro 的目錄有改動過後, 在 top_branch 的 rw 目錄會有這樣一個目錄 ".unionfs/"

它的作用是記錄下 readonly 而又被刪除了的檔案

比如當刪除了file3 後, 在 .unionfs/ 內會見到以下其中一樣


-rw------- 1 root root 0 Dec 13 13:30 file3_HIDDEN~                  <-- 刪除


drwx------ 2 root root 4096 Dec 13 13:35 folder_HIDDEN~              <-- 刪除


aufs - another unionfs

apt-get install aufs-tools




mount -t aufs -o br=/home/rw_folder=rw:/home/ro_folder=ro none /home/aufs-root/

br – specifies a branch (directory on a system), separated by colon ":"

By default, if no permissions are specified, first branch will be mounted as writable, and the remaining branches will be mounted as readonly.

mount -t aufs -o br=/home/rw_folder=rw:/home/ro_folder=ro -o udba=reval none /home/aufs-root/

# udba: User’s Direct Branch Access
# udba=none - may show incorrect data (create / delete without through the AuFS)
# udba=reval - AuFS will re-lookup the branches and update it
# udba=notify – AuFS will register for inotify for all the directories in the branches.

mount -t aufs -o br=/tmp/dir1=rw:/home/lakshmanan=rw -o udba=reval -o create=rr  none  /home/aufs-root/

# Round Robin Policy for Creating files


Aufs3 supports linux-3.0 and later.

# Modifies  the  permission  flags  of  the  branch

       # auplink /your/aufs/root flush
       # mount -o remount,mod:/your/writable/branch=ro /your/aufs/root

       # auplink /your/aufs/root flush
       # mount -o remount,noplink /your/aufs/root

# Removes   a   branch.
       # auplink /your/aufs/root flush
       # mount -o remount,del:/your/aufs/branch /your/aufs/root
# add:index:BRANCH
# index begins with 0
# append   <-- add:(last index + 1):BRANCH
# prepend  <-- add:0:BRANCH

       # auplink /your/aufs/root flush
       # mount -o remount,append:/your/aufs/branch /your/aufs/root


usage: auplink aufs_mount_point list|cpup|flush

'list' shows the pseudo-linked inode numbers and filenames.
'cpup' copies-up all pseudo-link to the writeble branch.
'flush' calls 'cpup', and then 'mount -o remount,clean_plink=inum'

# Removes  all  pseudo-links  in  memory.