fstab and systemd automount overlay

On an embedded device based on Yocto Linux my rootfs is RO, while I have an additional partition for RW data. Now I want to automount at boot an overlay onto /etc stored on a different partition. Here is my fstab:

/dev/mmcblk0p6 /data_local ext4 defaults,sync,noexec,rw 0 2
[...]
overlay /etc overlay defaults,lowerdir=/etc,upperdir=/data_local/overlayfs/upper/etc,workdir=/data_local/overlayfs/workdir,X-mount.mkdir,x-systemd.requires=/data_local,x-systemd.before=local-fs.target,x-systemd.before=systemd-networkd 0 0

However, this fails because the upperdir and workdir directories are missing on first boot. How can I let fstab or systemd.mount automatically create these directories?

Asked By: Martin H.

||

I ended up using the overlayfs-etc.bbclass feature from Yocto instead, which is available since Yocto 4.0. Documentation at: https://docs.yoctoproject.org/ref-manual/classes.html#ref-classes-overlayfs-etc

The bbclass patches the init process in /sbin/init to create the folders at runtime before mounting the overlay. See: https://git.yoctoproject.org/poky/plain/meta/files/overlayfs-etc-preinit.sh.in

Adding it to you image is really simple:

Add to your machine.conf:

OVERLAYFS_ETC_MOUNT_POINT = "/data_local"
OVERLAYFS_ETC_DEVICE = "/dev/mmcblk0p6"
OVERLAYFS_ETC_FSTYPE = "ext4"
OVERLAYFS_ETC_MOUNT_OPTIONS = "defaults,sync"

Add to your image:

IMAGE_FEATURES:append = " overlayfs-etc"

Of course you must make sure your boot medium has an extra read-write mounted partition already available (in the image flashed to SD card) – in my case mmcblk0p6.

Answered By: Martin H.
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.