docker-core-tech-aufs

AUFS

Introduction

- advanced multi layered unification filesystem

Aufs is a stackable unification filesystem such as Union fs which unifies several directories and provides a merged single directory.

  • overlay2 is the preferred storage driver, for all currently supported Linux distributions, and requires no extra configuration.
  • aufs was the preferred storage driver for Docker 18.06 and older, when running on Ubuntu 14.04 on kernel 3.13 which had no support for overlay2

AUFS is not merged into linux main branch, only ubuntu/debian support it!!!

Core Concept

Here is an example to merge two dirs into a single one, let’s explain it in details.

1
# mount -t aufs -o br=/tmp/dir1:/tmp/dir2 none /tmp/uniondir

This is the basic usage for aufs, more options refer to
each dir is called branch, the dir has order, the first one call top branch(br0), the later called bottom branch(brX)

Order is important as

  • if same file(with path) exists in different branches, the top branch wins
  • Only top branch has RW attribute, others has RO if not set explicitly

AUFS create/modify/delete

check aufs is supported or not

1
2
# grep aufs /proc/filesystems
nodev aufs

then mount two dirs into single one

1
2
3
4
5
6
7
8
9
10
11
# mount -t aufs -o br=/tmp/dir1:/tmp/dir2 none /tmp/udir
here is the output after mount
# tree .
.
|-- dir1
| -- file1
|-- dir2
| -- file2
-- udir
|-- file1
-- file2

Create file

1
2
3
4
5
6
7
8
9
10
11
12
# touch udir/file3
# tree .
.
|-- dir1
| |-- file1
| -- file3
|-- dir2
| -- file2
-- udir
|-- file1
|-- file2
-- file3

As you can see new file is created at RW branch

modify file

modify file from RW branch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# echo 'f1' >>udir/file1
# cat dir1/file1
f1
# cat udir/file1
f1
# tree .
.
|-- dir1
| |-- file1
| -- file3
|-- dir2
| -- file2
-- udir
|-- file1
|-- file2
-- file3

file is updated both in aufs and it’s source

modify file from RO branch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# echo 'f2' >>udir/file2
# cat udir/file2
f2
# cat dir2/file2 (unchanged original file2 is empty)
# tree .
.
|-- dir1
| |-- file1
| |-- file2 ---------->copied file2
| -- file3
|-- dir2
| -- file2
-- udir
|-- file1
|-- file2
-- file3

file is coped to RW branch, the original one unchanged

Delete file

delete file from RW
File is deleted from disk as well

delete file from RO branch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# tree -a .
.
|-- dir1
| |-- file1
| |-- .wh..wh.aufs
| |-- .wh..wh.orph
| -- .wh..wh.plnk
|-- dir2
| -- file2
-- udir
|-- file1
-- file2
# rm udir/file2 ---->remove file2 from aufs
# tree -a .
.
|-- dir1
| |-- file1
| |-- .wh.file2 -------------->whiteout file is created
| |-- .wh..wh.aufs
| |-- .wh..wh.orph
| -- .wh..wh.plnk
|-- dir2
| -- file2
-- udir
-- file1 ---------------->can not see file2 from udir


# rm dir1/.wh.file2 -------->remove whiteout file for file2
# tree -a .
.
|-- dir1
| |-- file1
| |-- .wh..wh.aufs
| |-- .wh..wh.orph
| -- .wh..wh.plnk
|-- dir2
| -- file2
-- udir
|-- file1
-- file2 ------>file2 is back after remove whiteout file

Note: actually file is hided, so that you can't see it from udir
as a whiteout file is created in the RW branch to tell aufs does not show the file which is deleted

check mount point and info

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/tmp/udir must exist before mount
# mount -t aufs -o br=/tmp/dir1:/tmp/dir2 none /tmp/udir
# mount | grep aufs
none on /tmp/udir type aufs (rw,relatime,si=226e11c4af0fb920) ---> si is the id for thi mount

use this si to check the mount info for it.
# ls -l /sys/fs/aufs/si_226e11c4af0fb920/
total 0
-r--r--r-- 1 root root 4096 Oct 14 11:05 br0
-r--r--r-- 1 root root 4096 Oct 14 11:05 br1
-r--r--r-- 1 root root 4096 Oct 14 11:05 brid0
-r--r--r-- 1 root root 4096 Oct 14 11:05 brid1
-r--r--r-- 1 root root 4096 Oct 14 11:05 xi_path

# cat /sys/fs/aufs/si_226e11c4af0fb920/br0
/tmp/dir1=rw --->source dir

# cat /sys/fs/aufs/si_226e11c4af0fb920/br1
/tmp/dir2=ro --->source dir