How to use overlayfs on Linux

From FriendlyELEC WiKi
Revision as of 12:10, 18 September 2019 by Yftan (Talk | contribs) (What Is OverlayFS)

Jump to: navigation, search

查看中文

1 What Is OverlayFS

OverlayFS is a union mount filesystem implementation for Linux. It allows a virtual merge of two partitions, while keeping their actual contents separate. One partition is the rootfs partition and the other is the data partition. It has the following advantages

Overlayfs是Linux下的一种堆叠文件系统,通俗地讲,根文件系统虽然在逻辑仍然是一个分区,但物理上被拆分成了两个分区来存储,其中,一个分区只读存放固化的系统数据(rootfs分区),另一个分区存储写入的数据(data分区),优点如下:
1) 方便恢复出厂设置,格式化data分区即可;
2) 避免重复掉电或异常掉电导致data分区挂载异常无法进入系统,由于rootfs是只读的,此时仍可以进入系统,方便维护升级;


Union file systems are a creative solution to allow a virtual merge of multiple folders, while keeping their actual contents separate. The Overlay file system (OverlayFS) is one example of these, though it is more of a mounting mechanism than a file system.

Brought into the Linux kernel mainline with version 3.18, OverlayFS allows you to overlay the contents (both files and directories) of one directory onto another. The source directories can be on different volumes and can even be different file systems, which creates an interesting mechanism for allowing temporary modification of read-only files and folders.

The simplest case (image below) involves two directories, each containing files and folders. We can think of them as “upper” and “lower,” with the rest of Linux and applications positioned above that. The “lower” directory is read-only. File access through the OverlayFS retrieves data from the “upper” directory first, and then defaults to the “lower” directory if a file doesn’t exist.


The need and specification of a kernel mode Linux union mount filesystem was identified in late 2009.[1] The initial RFC patchset of OverlayFS was submitted by Miklos Szeredi in 2010.[2] By 2011, OpenWrt had already adopted it for their use.[3]

It was merged into the Linux kernel mainline in 2014, in kernel version 3.18.[4][5] It was improved in version 4.0, bringing improvements necessary for e.g. the overlay2 storage driver in Docker.[6]

While most Live CD linux distributions used Aufs as of November 2016, Slackware used overlayfs for its live CD

The main mechanics of OverlayFS relate to the merging of directory access when both filesystems present a directory for the same name. Otherwise, OverlayFS presents the object, if any, yielded by one or the other, with the "upper" filesystem taking precedence. Unlike some other overlay filesystems, the directory subtrees being merged by OverlayFS do not necessarily have to be from distinct filesystems.[8]

OverlayFS supports whiteouts and opaque directories in the upper filesystem to allow file and directory deletion.[8]

OverlayFS does not support renaming files without performing a full copy-up of the file, however renaming directories in an upper filesystem has limited support.

OverlayFS does not support merging changes from an upper filesystem to a lower filesystem.

2 哪些平台与系统支持OverlayFS

2.1 支持的硬件平台

H3, H5, S5P4418, S5P6818, RK3399系列开发板

2.2 支持的软件平台

FriendlyCore, FriendlyDesktop, Lubuntu

3 如何鉴别系统是否工作在OverlayFS

执行df命令,如果/分区挂载类型为 overlay,表示OverlayFS正在工作:

pi@NanoPi-M1-Plus:~$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              474848       0    474848   0% /dev
tmpfs             102304    3564     98740   4% /run
overlay         28925547 1315493  26112465   5% /
tmpfs             511512       0    511512   0% /dev/shm
tmpfs               5120       4      5116   1% /run/lock
tmpfs             511512       0    511512   0% /sys/fs/cgroup
/dev/mmcblk0p1     40862   11852     29010  30% /boot
tmpfs             102304       0    102304   0% /run/user/1000

4 使用OverlayFS时的分区布局

会有三个分区,分别为 rootfs分区、data分区与boot分区,对应映像文件rootfs.img, userdata.img 和 boot.img:

root@NanoPi-M1-Plus:~# lsblk /dev/mmcblk0
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
mmcblk0     179:0    0 29.7G  0 disk
|-mmcblk0p2 179:2    0  1.2G  0 part
|-mmcblk0p3 179:3    0 28.5G  0 part
`-mmcblk0p1 179:1    0   40M  0 part /boot

其中,rootfs分区存放固化的系统数据,所有对根目录的数据写入,都会写入到 data分区。

5 使用OverlayFS实现恢复出厂设置功能

5.1 方法1:操作userdata分区

factory reset:

mount /dev/mmcblk0p3 /media/ && cd /media/
mv root/ deleteme.root
mv work/ deleteme.work
reboot

cleanup after reboot:

mount /dev/mmcblk0p3 /media/ && rm -rf /media/deleteme.*

5.2 方法2:命令行参数

在内核命令行参数中加入 wipedata=yes,S5P4418与S5P6818平台可以在系统运行时进行修改:

5.2.1 设置下次开机时抹掉data分区的数据(即恢复出厂设置)

sudo fw_setenv bootargs "`cat /proc/cmdline` wipedata=yes"
sudo reboot

5.2.2 开机时不再抹掉data分区

sudo fw_setenv bootargs "`cat /proc/cmdline` wipedata=no"
sudo reboot

其他平台需要根据BSP提供的方法进行kernel command line修改,也可以开机进入uboot命令行进行设置。

6 临时禁用OverlayFS

可以做完TF启动卡之后,用 fdisk 操作TF卡,删除 data 分区。