Skip to content

[RPI] disable writes to sd card to reduce wear #12

@ubap

Description

@ubap

The single biggest threat to a Raspberry Pi's long-term stability is the limited lifespan of an SD card, which is directly impacted by write operations.

Raspbian, being a full-featured Linux distribution, performs a number of background write operations that will cause wear on your SD card. Reads, as you know, cause virtually no wear.

Here are the primary culprits of SD card wear from Raspbian, ranked from most to least impactful, and how to mitigate them.


The Main Culprits of SD Card Writes

1. System and Application Logging (Highest Impact)

This is, by far, the most frequent source of writes on a default Raspbian installation. Nearly every service, system event, authentication attempt, and kernel message is written to a file in the /var/log directory.

  • Files: /var/log/syslog, /var/log/auth.log, /var/log/daemon.log, /var/log/kern.log, etc.
  • Frequency: Constant. Can be multiple times per second depending on system activity.
  • Impact: High. This steady stream of small writes is exactly the kind of workload that wears out consumer-grade SD cards.

2. Swap File Usage (The Silent Killer)

If your Raspberry Pi runs out of physical RAM, Raspbian will start using a "swap file" on the SD card as slow, virtual memory.

  • File: /var/swap
  • Frequency: Only happens under high memory pressure.
  • Impact: Catastrophic. If your system is regularly swapping, it is performing thousands of read/write operations constantly. This is the fastest way to destroy an SD card. This is especially a risk on older Pis with 1GB of RAM or less.

3. File Access Times (atime)

By default, every time a file is read, the Linux kernel performs a write operation to update that file's metadata with the "last access time" (atime). This is a classic example of a hidden write.

  • Files: All files on the system.
  • Frequency: Every time any file is read by any process.
  • Impact: Medium. It creates a lot of small, often unnecessary writes across the entire filesystem.

4. Temporary Files

Many applications and system services create temporary files in directories like /tmp and /var/tmp.

  • Impact: Usually low, but can be high if you run scripts or applications that do a lot of temporary file processing.

5. Package Management

Running commands like sudo apt-get update and sudo apt-get upgrade involves downloading and writing many files to the SD card.

  • Impact: High during the operation, but very infrequent, so the overall contribution to wear is low.

How to Drastically Reduce SD Card Wear (The Solutions)

You can configure your Raspbian system to be much gentler on the SD card.

1. Move Logs to RAM (Most Effective Solution)

You can configure Raspbian to write logs to a small, temporary filesystem in RAM (tmpfs). The logs will be lost on reboot, but this is a perfectly acceptable trade-off for a dedicated server where you can check logs while it's running.

The easiest way is to use a utility like log2ram.

# Install log2ram
sudo apt-get install log2ram

This utility will automatically configure /var/log to be mounted in RAM and will periodically sync the logs back to the SD card (e.g., once an hour and on shutdown) to balance wear reduction with some persistence.

2. Disable Swap Entirely

If your Go application has a predictable memory footprint and your Pi has enough RAM (2GB+ is usually safe), you can completely disable swap.

# Stop the current swap service
sudo dphys-swapfile swapoff

# Uninstall the service file
sudo dphys-swapfile uninstall

# Disable the service from starting on boot
sudo systemctl disable dphys-swapfile

Warning: If your application's memory usage spikes and there is no swap, the Linux kernel's "Out of Memory Killer" will terminate a process (likely your Go app) to free up RAM.

3. Disable Access Time (noatime)

You can tell the kernel not to perform a write every time a file is read. This is a simple, safe, and very effective optimization.

1. Edit the filesystem table:

sudo nano /etc/fstab

2. Find the line for your root partition. It will look something like this:
PARTUUID=xxxxxxxx-02 / ext4 defaults,noatime,errors=remount-ro 0 1

3. Add noatime to the options, separated by a comma. If it's already there, you're all set! If not, change defaults to defaults,noatime.

4. Reboot for the change to take effect.

4. Use a High-Quality SD Card

This is a hardware solution, but it's critical. Do not use a generic, cheap SD card. Purchase a "High Endurance" or "Industrial" grade card. These are specifically designed for write-heavy workloads like dashcams and servers and have much more durable memory cells and better wear-leveling algorithms.

How to Monitor Disk Writes

You can see which processes are writing to your SD card in real-time.

# Install the tool
sudo apt-get update
sudo apt-get install iotop

# Run it to see I/O usage
sudo iotop

This will give you a top-like view of disk I/O, allowing you to identify any unexpected sources of writes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions