Buildroot只读文件系统下,如何实现/etc目录的可持久化写入
Nice problem to solve—read-only root filesystems are great for stability on IoT devices, but balancing that with necessary writable configs is tricky. Let’s break down the best approaches for your Orange Pi Zero + Buildroot setup, focusing on avoiding full root FS remounts:
1. Bind Mount Individual Writable Files (Best for Your Exact Use Case)
Since you only need to persist two specific files (/etc/wpa_supplicant.conf and your custom config file), this is the most lightweight and efficient method. Here's how to set it up:
- Partition your storage: Create a small, dedicated writable partition (e.g., ext4) on your SD card (say
/dev/mmcblk0p2). This will hold your persistent files. - Mount the writable partition: Add an entry to your
/etc/fstab(or init script) to mount this partition at a fixed location like/mnt/rwon boot:/dev/mmcblk0p2 /mnt/rw ext4 defaults,noatime 0 2 - Prepare the persistent files: Copy the initial versions of
wpa_supplicant.confand your custom config file into/mnt/rw/etc/(create theetcsubdir if needed). - Bind mount the files: Add these lines to your boot init script (like
/etc/init.d/S99persist-config) to map the persistent files into/etc:mount --bind /mnt/rw/etc/wpa_supplicant.conf /etc/wpa_supplicant.conf mount --bind /mnt/rw/my_custom_config.conf /etc/my_custom_config.conf
Now, any writes to those two /etc files will actually go to the writable partition, no full root FS remount required. It’s simple, low-overhead, and exactly targeted to your needs.
2. Overlay Filesystem for /etc (More Flexible)
If you ever need to modify other files in /etc later, an OverlayFS setup is better for scalability. It stacks a read-only lower layer (your original /etc) with a writable upper layer (on your dedicated partition):
- Enable OverlayFS in Buildroot: Make sure your kernel has
CONFIG_OVERLAY_FSenabled (check Kernel Configuration in Buildroot), and that you have the overlay mount tool available (busybox includes it by default). - Set up the mount: In your init script or fstab, mount OverlayFS over
/etc:mount -t overlay overlay -o lowerdir=/etc,upperdir=/mnt/rw/etc,workdir=/mnt/rw/work /etclowerdir: The original read-only/etcfrom your Buildroot imageupperdir: The directory on your writable partition where modified files are storedworkdir: A temporary directory required by OverlayFS (must be on the same filesystem as upperdir)
This way, all changes to/etcare persisted in the upperdir, while the root FS stays read-only. It’s more flexible than bind mounts but slightly more complex.
3. Avoid These Less Ideal Options
- Remounting root FS read-write temporarily: This defeats the purpose of a read-only system and introduces stability risks (e.g., corruption if power is lost during writes).
- tmpfs with sync scripts: Using tmpfs for
/etcand syncing specific files to disk on shutdown works, but you risk losing data if the device powers off unexpectedly. Only use this if persistence isn’t critical.
Buildroot Configuration Tips
- When setting up your root filesystem, mark it as read-only in Buildroot’s System Configuration (set "Root filesystem read-only" to yes).
- Ensure your writable partition is properly formatted and included in your disk image—you can use Buildroot’s "Custom partition table" option to define it.
内容的提问来源于stack exchange,提问作者flip79

