Read-only Root-FS for most Linux distributions using OverlayFS. Just like the package overlayroot in Ubuntu but it will work on almost all distributions.
Forked from: https://wiki.psuter.ch/doku.php?id=solve_raspbian_sd_card_corruption_issues_with_read-only_mounted_root_partition
- Move this script to /sbin/overlayRoot.sh or other location you want.
chmod +x /sbin/overlayRoot.sh- Change your boot parameter. Add/Modify
init=/sbin/inittoinit=/sbin/overlayRoot.sh. - Reboot and enjoy.
In general, the script will mount a OverlayFS over the original root file system using tmpfs as upper layer.
- Get all useful rootfs information from
/etc/fstab. - Mount a tmpfs at
/mnt/overlay, create/mnt/overlay/upper,/mnt/overlay/work,/mnt/overlay/newroot. - Bind mount rootfs to
/mnt/lower. - Mount OverlayFS using
/mnt/loweras lower,/mnt/overlay/upperand/mnt/overlay/workas upper and work directory,/mnt/overlay/newrootas merged destination. pivot_rootto/mnt/overlay/newrootand put old root to/mnt.- Move mount point
/mnt/mnt/lower(the original/mnt/lower) to/lower,/mnt/mnt/overlay(the original/mnt/overlay) to/overlay. - Move all other useful virtual filesystems like procfs or devfs to new root.
- Unmount
/mnt(the original root file system). - Exec
/sbin/initand continue the init process.
The upstream project is only compatible with Raspbian and has not been updated for years. Thus I add some patches to make it work on almost all Linux distributions.
- Systemd
- OpenRC
- Raspbian
- Armbian
- ArchLinux
- Do all actions in Installation part with root permissions.
- To temporary disable overlayRoot.sh, add
noOverlayRootto your kernel parameter. - Double-check your
/etc/fstab, this script will get rootfs mount point information from it. only UUID, PARTUUID, LABLE and raw device is supported but use PARTUUID remain not recommended. - If you use this script and stuck at the emergency shell or something similar, change your boot parameter back.
- Some distribution is extremely Non-POSIX during init (such as Nix OS). Do not use this script.
- Failed on some distributions whose init executable is not
/sbin/init.
FIX: Simply replace all
/sbin/initin this script to the value of your distribution use (for example:sed -i 's|/sbin/init|/init|g' overlayRoot.sh).
- Failed on some distributions doesn't have
/etc/fstabor doesn't have it during the very early stage of init.
FIX: Manually add rootfs mount information in your
/etc/fstab.
- Failed on some kernel that not support OverlayFS.
FIX: Use another kernel. Change distribution. etc...
- Failed on some distributions call main init executable (like systemd/openrc/sysv) in initramfs.
FIX: Move this script to initramfs.
- More questions?
Ask me on Issues or Discussions.