[BPI-R4] BPI-R4 with Read-only Root (SD)

Same as RaspberryPi, A read-only root filesystem can prevent sdcard corruption when the BPI suddenly loses power.

Read-only Root has many solutions, we use overlayRoot solution. The overlayRoot solution is the easiest solution to set up.

We use frank-w BPI-R4 Debian version.

Setup step:

  1. Modify BPI-BOOT/uEnv.txt in SD
    Add boot parameter as follows: root=/dev/mmcblk0p6 init=/sbin/overlayRoot.sh rootfstype=ext4 rootwait

  1. Download overlayRoot.sh and put into BPI-BOOT/sbin/overlayRoot.sh

image

We use fitu996’s overlayRoot.sh

  1. Modify fstab and chmod overlayRoot.sh /etc/fstab
    Backup fstab as fstab.rw Setup /boot parameter as ro

chmod 755 [BPI-ROOT]/sbin/overlayRoot.sh

image

  1. Insert SD to boot or reboot
    Boot from SD.

Normal Debian filesystem:

image

Read-Only Root Debian filesystem:

image

We can see some thing different.
First, / mount from /dev/root to overlayfs-root.
Second, /boot mount params as ro (read-only).

Now, we can touch file to /, mkdir folder to /.
When we reboot, all changes will disappear(all changes in ram).

1 Like

How to modify data in Root?

We has 2 methods:

  1. We can remount / to rw, and modify files, create folder.
  2. We can mark root parameter in uEnv.txt.

The first method cannot install deb packages because package information is stored in /var, but it can modify/add system files and directory.

The second method can modify system and install deb packages, but it requires two reboots (the first time to apply the read-write system, the second time back to read-only system).

First method:

  1. Mount /dev/mmcblk0p6 to /mnt and remount it to rw
    mount /dev/mmcblk0p6 /mnt mount -o remount,rw /mnt

image

  1. Create directory
    mkdir /mnt/A
    ls /mnt/

  1. Umount /mnt

image

  1. Reboot and check /

Second method:

  1. Remount rw /boot

image

  1. Modify uEnv.txt

  1. Check filesystem and reboot

image

  1. After reboot, check filesystem

image

When we finish install packages, upgrade system.
We modify uEnv.txt and reboot back to read-only Root.

1 Like

Thanks for the instructions.

There are more ways to prevent corruption:

Use emmc instead of sd card. It is more resillient to wear.

Use f2fs or btrfs instead of ext4.

Or when really must use ext4 mount with options noatime and nodiratime. Format with correct alignment and setup a blocksize that fits the card.

Check sd card when in the the bananapi board:

bc -l <<<"$(cat /sys/block/mmcblk0/device/preferred_erase_size) /1024 /1024"
SD_BLOCK_SIZE_KB=16                   # in kilo bytes
SD_ERASE_SIZE_MB=4                   # in Mega bytes

Make sure the roots partition starts exactly in a multiple of SD_ERASE_SIZE_MB

stride=$(( $SD_BLOCK_SIZE_KB / $SD_BLOCK_SIZE_KB ))
stripe=$(( ($SD_ERASE_SIZE_MB * 1024) / $SD_BLOCK_SIZE_KB ))
mkfs.ext4 -v -b $(( $SD_BLOCK_SIZE_KB * 1024 ))  -L ROOTFS_LABEL \
                    -E stride=$stride,stripe-width=$stripe /dev/mmcblk0pX

So really, it is easier to use f2fs or btrfs, you don’t neef to worry about all this and it is practically all automatic.

Thanks.
You are right, f2fs is optimized for Flash reading, writing, and data correctness. :smile: