Difference between revisions of "How to use overlayfs on Linux"

From FriendlyELEC WiKi
Jump to: navigation, search
(updated by API)
(updated by API)
 
Line 24: Line 24:
 
tmpfs          793M  116K  793M  1% /run/user/1000
 
tmpfs          793M  116K  793M  1% /run/user/1000
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
==Partition layout when using OverlayFS==
==Partition Settings With 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. <br />
If OverlayFS is enabled in your system there will be three partitions:rootfs, data and boot, and these partitions correspond to a rootfs.img, a userdata.img and a boot.img separately:
+
Enter the following command to view the partition layout:
 +
<syntaxhighlight lang="bash">
 +
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
 +
</syntaxhighlight>
 +
The output is shown as follows:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
pi@NanoPi-R6C:/etc$ sudo apt install parted
 
pi@NanoPi-R6C:/etc$ sudo apt install parted
Line 47: Line 52:
 
  9      4173MB  31.9GB  27.7GB  ext4        userdata
 
  9      4173MB  31.9GB  27.7GB  ext4        userdata
 
</syntaxhighlight>
 
</syntaxhighlight>
The system data will be in the rootfs partition and the data written to the root directory will be in the data partition.
+
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.<br />
 
+
== Common Operations ==
==Restore Factory Settings With OverlayFS==
+
'''Notes:'''<br/>
===Method 1: Operate in Userdata Partition===
+
* These operations will erase user data, so be sure to backup your data in advance.<br>
factory reset:
+
* The OS image needs to be updated to version 2023/03/14 or later, or only the boot.img needs to be updated.<br>
 +
* 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.<br>
 +
===View the current partition layout===
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
mount /dev/mmcblk0p3 /media/ && cd /media/
+
sudo apt update
mv root/ deleteme.root
+
sudo apt install parted
mv work/ deleteme.work
+
export DEV=/dev/mmcblkX # Need to change to real device
reboot
+
sudo parted -s ${DEV} unit MiB print
 
</syntaxhighlight>
 
</syntaxhighlight>
cleanup after reboot:
+
===Disable OverlayFS feature===
 +
Create a file named ".init_wipedata" in the root directory with the content "overlayfs=disable" and then reboot:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
mount /dev/mmcblk0p3 /media/ && rm -rf /media/deleteme.*
+
sudo passwd root  # Create a password for the root user, if not done before
 +
su - root -c 'echo "overlayfs=disable" > /.init_wipedata'
 +
sudo reboot
 
</syntaxhighlight>
 
</syntaxhighlight>
===Method 2: Restore by Setting Commandline Parameters===
+
=== Resize the userdata partition and create an additional partition===
===S5P4418 & S5P6818 platform===
+
* View current partition layout
Add "wipedata=yes" to the kernel's commandline parameters. For S5P4418 and S5P6818 based boards you can do it after system boots;
+
====恢复出厂设置====
+
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
sudo fw_setenv bootargs "`sed 's/ wipedata=yes//g;s/ wipedata=no//g' /proc/cmdline` wipedata=yes"
+
sudo apt update
sudo reboot
+
sudo apt install parted fdisk
 +
export DEV=/dev/mmcblkX # Need to change to real device
 +
sudo parted -s ${DEV} unit MiB print
 
</syntaxhighlight>
 
</syntaxhighlight>
====Keep Data in Data Partition on System Reboot====
+
* 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:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
sudo fw_setenv bootargs "`sed 's/ wipedata=yes//g;s/ wipedata=no//g' /proc/cmdline` wipedata=no"
+
su - root -c 'echo "overlayfs=enable userdata=8096" > /.init_wipedata'
 
sudo reboot
 
sudo reboot
 
</syntaxhighlight>
 
</syntaxhighlight>
For other SoC based boards you need to make changes in the kernel commandline by following the methods supported by their BSPs or by entering uboot to proceed.
+
After reboot, you can see that the userdata partition has been resized to 8GB:
====RK3399 platform====
+
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
echo wipedata=yes > /var/.init_wipedata; sync; reboot
+
export DEV=/dev/mmcblkX  # Need to change to real device
 +
sudo parted -s ${DEV} unit MiB print
 
</syntaxhighlight>
 
</syntaxhighlight>
The next time you boot to the ramdisk stage, the data partition will be formatted, and the /var/.init_wipedata file will also be deleted.
+
* Create new partition and format it
====H3/H5 platform====
+
=====change boot.cmd=====
+
Enter the boot directory and modify file boot.cmd, change the following:
+
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
setenv bootargs "console=${debug_port} earlyprintk
+
(echo n; echo ""; echo ""; echo ""; echo w) | sudo fdisk ${DEV}
root=/dev/mmcblk0p2 rootfstype=ext4 rw rootwait fsck.repair=${fsck.repair}
+
NUM=$(sudo parted ${DEV} print | awk 'NF > 1 {p = $1} END {print p}')  # Get the last partition index
panic=10 fbcon=${fbcon} ${hdmi_res} ${overlayfs} ${pmdown}"
+
sudo mkfs.ext4 ${DEV}p${NUM}
 
</syntaxhighlight>
 
</syntaxhighlight>
to:
+
* Mount the new partition to the specified directory
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
setenv bootargs "console=${debug_port} earlyprintk
+
sudo mkdir -p /oem
root=/dev/mmcblk0p2 rootfstype=ext4 rw rootwait fsck.repair=${fsck.repair}
+
sudo blkid ${DEV}p${NUM}
panic=10 fbcon=${fbcon} ${hdmi_res} ${overlayfs} ${pmdown} wipedata=yes"
+
# 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
 +
</syntaxhighlight>
 +
===Restore factory settings===
 +
<syntaxhighlight lang="bash">
 +
su - root -c 'echo "overlayfs=enable" > /.init_wipedata'
 +
sudo reboot
 
</syntaxhighlight>
 
</syntaxhighlight>
Added wipedata=yes.
+
The next time boot into the ramdisk stage, the userdata partition is formatted and the /var/.init_wipedata file is cleared.
=====regenerate boot.scr=====
+
===Debug===
Enter the boot directory,execute the following command:
+
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:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$ mkimage -C none -A arm -T script -d boot.cmd boot.scr
+
dmesg | grep initfs
 
</syntaxhighlight>
 
</syntaxhighlight>
==Disable OverlayFS==
 
If you have an installation TF card, use the fdisk command to delete the data partition.
 

Latest revision as of 09:33, 20 March 2023

查看中文

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