How to use overlayfs on Linux

From FriendlyELEC WiKi
Revision as of 09:33, 20 March 2023 by Tzs (Talk | contribs) (updated by API)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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:
1) you can easily restore a system's factory settings by formatting the data partition;
2) you can still boot your system since the rootfs is read-only even when the data partition cannot be correctly mounted due to unexpected shutdown.

2 FriendlyELEC's Systems That Support OverlayFS

2.1 Hardware Systems

H3, H5, S5P4418, S5P6818, RK3399, RK3328, RK3568, RK3588 based boards

2.2 OS Systems

All Linux-based systems

2.3 Scope of Application

This document is only applicable to products using Rockchip platform. If you are using other platforms, please click on this link: How to use overlayfs on S5Pxxxx,H3,H5 platform

3 How to Check Whether OverlayFS Is Working

Run the df command. If the "/" partition is mounted as "overlay" it means OverlayFS is working;

pi@NanoPi-R6C:/etc$ df -h
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           792M  2.2M  790M   1% /run
overlay          25G   13G   11G  53% /
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           793M  116K  793M   1% /run/user/1000

4 Partition layout when using OverlayFS

The user data will be composed of the rootfs partition and the userdata partition, corresponding to the image files rootfs.img and userdata.img.
Enter the following command to view the partition layout:

View eMMC partition layout: sudo parted -s /dev/mmcblk2 unit MiB print
View SD card partition layout: sudo parted -s /dev/mmcblk0 unit MiB print

The output is shown as follows:

pi@NanoPi-R6C:/etc$ sudo apt install parted
pi@NanoPi-R6C:/etc$ sudo parted /dev/mmcblk0 print
Model: SD SR32G (sd/mmc)
Disk /dev/mmcblk0: 31.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
 
Number  Start   End     Size    File system  Name      Flags
 1      8389kB  12.6MB  4194kB               uboot
 2      12.6MB  16.8MB  4194kB               misc
 3      16.8MB  21.0MB  4194kB               dtbo
 4      21.0MB  37.7MB  16.8MB               resource
 5      37.7MB  79.7MB  41.9MB               kernel
 6      79.7MB  113MB   33.6MB               boot
 7      113MB   147MB   33.6MB               recovery
 8      147MB   4173MB  4027MB  ext4         rootfs
 9      4173MB  31.9GB  27.7GB  ext4         userdata

the rootfs partition is mounted as read-only, while userdata is mounted as read-write. The rootfs partition stores fixed system data, and any subsequent writes to the root directory will be written to the userdata partition. Therefore, formatting userdata is equivalent to restoring factory settings.

5 Common Operations

Notes:

  • These operations will erase user data, so be sure to backup your data in advance.
  • The OS image needs to be updated to version 2023/03/14 or later, or only the boot.img needs to be updated.
  • The device nodes /dev/mmcblkX that appear in the commands are fictitious nodes, and need to be changed to the real devices. The device node for eMMC is /dev/mmcblk2, and the device node for TF card is /dev/mmcblk0.

5.1 View the current partition layout

sudo apt update
sudo apt install parted
export DEV=/dev/mmcblkX # Need to change to real device
sudo parted -s ${DEV} unit MiB print

5.2 Disable OverlayFS feature

Create a file named ".init_wipedata" in the root directory with the content "overlayfs=disable" and then reboot:

sudo passwd root  # Create a password for the root user, if not done before
su - root -c 'echo "overlayfs=disable" > /.init_wipedata'
sudo reboot

5.3 Resize the userdata partition and create an additional partition

  • View current partition layout
sudo apt update
sudo apt install parted fdisk
export DEV=/dev/mmcblkX # Need to change to real device
sudo parted -s ${DEV} unit MiB print
  • Resize the userdata partition

Here is an example of resizing to 8G, create a file named ".init_wipedata" in the root directory, with the content "overlayfs=enable userdata=8096", and then Reboot, where userdata= is followed by the new userdata partition size in MB, the command is as follows:

su - root -c 'echo "overlayfs=enable userdata=8096" > /.init_wipedata'
sudo reboot

After reboot, you can see that the userdata partition has been resized to 8GB:

export DEV=/dev/mmcblkX  # Need to change to real device
sudo parted -s ${DEV} unit MiB print
  • Create new partition and format it
(echo n; echo ""; echo ""; echo ""; echo w) | sudo fdisk ${DEV}
NUM=$(sudo parted ${DEV} print | awk 'NF > 1 {p = $1} END {print p}')  # Get the last partition index
sudo mkfs.ext4 ${DEV}p${NUM}
  • Mount the new partition to the specified directory
sudo mkdir -p /oem
sudo blkid ${DEV}p${NUM}
# Note down the UUID
# Configure automatic mounting
sudo vi /etc/fstab
# Add the following to the end of the file (where the UUID needs to be replaced with the real one)
UUID=bbb06fe1-df52-4c7c-b2eb-926b14605fe4 /oem ext4 suid,dev,exec,auto,nouser,async,noatime,nofail 0 0
# Enter the following command to mount the partition
sudo mount /oem

5.4 Restore factory settings

su - root -c 'echo "overlayfs=enable" > /.init_wipedata'
sudo reboot

The next time boot into the ramdisk stage, the userdata partition is formatted and the /var/.init_wipedata file is cleared.

5.5 Debug

If you find that the partition has not changed, there may be an error, check if an error message is printed with the following command:

dmesg | grep initfs