unionfs 與 aufs

更新日期: 2020-07-28

介紹

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        <-- 注意次序 !!
                    mount_point

Option

-o cow Enable copy-on-write

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

KNOWN ISSUES

       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.

Example

mount

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

4 directories, 4 files

CMD:

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

 

aufs = advanced multi layered unification filesystem

AUFS uses the Copy-on-Write (CoW) strategy to maximize storage efficiency and minimize overhead.

aufs 是依賴 kernel module 的, 很多時由 dkms 建立

lsmod | grep aufs

aufs                  274432  0

ls /lib/modules/`uname -r`/updates/dkms/

aufs.ko

Note

Not all directories in the filesystem can be used as branch es of aufs.

Currently, aufs do not support btrfs aufs eCryptfs

Install

apt-get install aufs-tools

Default Config

  • /etc/default/aufs

bin

  • /usr/bin/aubusy
  • /usr/bin/auchk
  • /usr/bin/aubrsync

sbin

  • /sbin/auibusy
  • /sbin/auplink
  • /sbin/mount.aufs
  • /sbin/umount.aufs

Usage

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)

":"         colon = separated by

By default, if no permissions are specified,

    first branch will be mounted as writable, and

    the remaining branches will be mounted as readonly.

"none"    aufs doesn't need devices

Policy

Read Policy: udba

# udba: User’s Direct Branch Access

  • none - aufs runs fastest, but may incorrect data (create / delete without through the AuFS)
  • reval - AuFS will re-lookup the branches and update it (Default)
  • notify – AuFS will register for inotify for all the directories in the branches

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

Write Policy: create

Round Robin Policy for Creating files

mount -t aufs -o br=/home/data1=rw:/home/data2=rw \
    -o udba=reval -o create=rr  none  /home/aufs-root/

Remark

Aufs3 supports linux-3.0 and later.

auplink

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'

# 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

clean_plink

# Removes all pseudo-links in memory.

HomePage

 


Other

 

 

 

 

Creative Commons license icon Creative Commons license icon