2

I am working on embedded linux. I am trying to protect my rootfs by making it read only and mount fs file from the sdcard over the root.

I need both fs to be merged. Any writes can be redirected to the fs file onto the sdcard. Reading from the read only rootfs still possible.

I tried the following:

$ cd /media/sdcard

$ mount userfs /

$ cd /

$ echo a > a.txt

But I receive error: -sh: a.txt: Read-only file system

Can any one help me to implement the needed functionality ?

  • Possible duplicate of [Best POSIX way to determine if a filesystem is mounted read only](https://stackoverflow.com/questions/4894743/best-posix-way-to-determine-if-a-filesystem-is-mounted-read-only) – pringi Mar 21 '19 at 13:10
  • 1
    Sounds like a job for **overlayfs**. – 0andriy Mar 21 '19 at 13:17

2 Answers2

2

To complete Ross answer, this is how I added overlayfs for /var/log:

add_overlayfs_mount() {

    mkdir -p ${IMAGE_ROOTFS}/data/overlay/log
    mkdir -p ${IMAGE_ROOTFS}/data/work/log

    echo '/dev/sda4       /data   ext4    defaults        0       0' >> ${IMAGE_ROOTFS}/etc/fstab

    echo 'ofslog /var/log overlay defaults,x-systemd.requires=data,lowerdir=/var/log,upperdir=/data/overlay/log,workdir=/data/work/log 0 2' >> ${IMAGE_ROOTFS}/etc/fstab
}

ROOTFS_POSTPROCESS_COMMAND += "add_overlayfs_mount ; "

You can also use VOLATILE_BINDS in some situations:

VOLATILE_BINDS_append = " \
    /data/etc/hostname /etc/hostname \n\
"
Nayfe
  • 2,130
  • 13
  • 18
0

Yes, overlayfs is exactly what you want.

Ross Burton
  • 3,516
  • 13
  • 12
  • 1
    Can you recommend some up to date Yocto documentation on how to setup overlayfs, possible Zeus and later? IMHO there is not much up to date reading material regarding overlayfs in yocto. – Martin H. Jan 15 '21 at 08:28