Close

How to store persistent data on a read-only SD card.

A project log for Roktrack - Pylon Guided Mower

A mower not only for your yard, but also for your community.

yuta-suitoYuta Suito 08/24/2023 at 21:250 Comments

When using the Raspberry Pi in an embedded system, make the SD card read-only to prevent system corruption. In this case, data such as logs and images cannot be stored. In this article, I will show you how to save data to this SD card.

Create a file system for data

Plug the SD card into a desktop Linux machine separate from the Raspberry Pi. unmount, resize, and shrink the root file system using Gnome Disks or similar. This will create a free space, so create a file system in that area with FAT.

Auto mount setting

After returning the sd card to the raspberry pi, edit /etc/fstab and set the file system for data to be mounted automatically.

pi@roktrack1:~ $ cat /etc/fstab
proc            /proc           proc    defaults          0       0
PARTUUID=dc22932d-01  /boot           vfat    defaults          0       2
PARTUUID=dc22932d-02  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that
PARTUUID=dc22932d-03  /data           vfat    auto,exec,umask=000  0    1

 Add the last line of the above configuration to your fstab. When you do so, modify PARTUUID to the ID of your environment. You have now created a persitent area on your read-only Raspberry Pi.

Convert to read-only

sudo raspi-config nonint enable_overlayfs
sudo systemctl reboot

 After several dozen power-off tests, the system area was not corrupted and did not become unbootable. Files in the file system for data may be corrupted, but even partial corruption of data such as logs and images will have only localized effects.

Discussions