Skip to content

Storage Management for Raspberry Pi

Effective storage management is crucial for Raspberry Pi reliability and performance. This guide covers everything from choosing the right storage media to optimizing filesystem performance and creating automated backups.

Storage Options Compared

Storage Type Speed (Sequential) Speed (Random 4K) Reliability Cost Supported On
microSD (A2) 90-170 MB/s read 4000 IOPS ⚠️ Limited write cycles Low All models
USB 3.0 SSD 300-450 MB/s 50,000+ IOPS ✅ Very high Moderate Pi 4, Pi 5
NVMe SSD 800-3500 MB/s 100,000+ IOPS ✅ Very high Moderate Pi 5 (via M.2 HAT+)
USB Flash Drive 30-150 MB/s 1000-5000 IOPS ⚠️ Moderate Low All with USB
USB HDD 100-200 MB/s 100-200 IOPS ✅ High Low All with USB

The single best upgrade

Moving from a microSD card to an SSD (USB or NVMe) is the single most impactful upgrade you can make. Boot times drop from 30+ seconds to under 10, and the system feels dramatically more responsive.

Checking Your Storage

Listing Disks and Partitions

# List all block devices
lsblk
# Example output:
# NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
# sda           8:0    0 238.5G  0 disk
# └─sda1        8:1    0 238.5G  0 part /mnt/ssd
# mmcblk0     179:0    0  29.7G  0 disk
# ├─mmcblk0p1 179:1    0   512M  0 part /boot/firmware
# └─mmcblk0p2 179:2    0  29.2G  0 part /

# Detailed partition information
sudo fdisk -l

# Show filesystem usage
df -hT
# The -T flag shows the filesystem type (ext4, vfat, etc.)

Monitoring Disk Health

# Check SD card for errors in kernel log
sudo dmesg | grep -i "mmc\|error\|fail"

# Install and use smartctl for USB/NVMe SSDs
sudo apt install smartmontools
sudo smartctl -a /dev/sda      # USB SSD
sudo smartctl -a /dev/nvme0    # NVMe SSD

# Check SD card read speed
sudo hdparm -Tt /dev/mmcblk0

# Quick benchmark
dd if=/dev/zero of=/tmp/test bs=4M count=256 conv=fdatasync status=progress
rm /tmp/test

Partition Management

Creating Partitions with fdisk

# Start fdisk on a USB drive (be careful to select the right device!)
sudo fdisk /dev/sda

# Common fdisk commands:
# p  — Print partition table
# n  — Create new partition
# d  — Delete a partition
# t  — Change partition type
# w  — Write changes and exit
# q  — Quit without saving

Creating Partitions with parted (Supports GPT)

For drives larger than 2TB or when you need GPT:

1
2
3
4
5
6
7
8
# Create a GPT partition table
sudo parted /dev/sda mklabel gpt

# Create a single partition using all space
sudo parted /dev/sda mkpart primary ext4 0% 100%

# Verify
sudo parted /dev/sda print

Formatting Partitions

# Format as ext4 (recommended for Linux)
sudo mkfs.ext4 -L "MyDrive" /dev/sda1

# Format as f2fs (optimized for flash storage)
sudo apt install f2fs-tools
sudo mkfs.f2fs -l "FlashDrive" /dev/sda1

# Format as exFAT (for cross-platform compatibility)
sudo apt install exfatprogs
sudo mkfs.exfat -L "Shared" /dev/sda1

# Format as FAT32 (maximum compatibility, 4GB file size limit)
sudo mkfs.vfat -F 32 -n "USB" /dev/sda1

Filesystem Selection Guide

Filesystem Best For Max File Size Journal Flash Optimized
ext4 General Linux use 16 TB Yes No
f2fs SD cards, USB flash 3.94 TB Yes ✅ Yes
btrfs NAS, snapshots, RAID 16 EB CoW Partial
exFAT Cross-platform sharing 128 PB No No
FAT32 Boot partition, compatibility 4 GB No No
XFS Large files, media servers 8 EB Yes No

Recommendations:

  • Root filesystem on SD card: ext4 (default) or f2fs for better flash performance
  • Root filesystem on SSD: ext4
  • NAS/file server: btrfs (snapshots, checksums) or ext4 (simplicity)
  • Shared USB drive: exFAT (works on Windows, Mac, Linux)

Using f2fs for SD Cards

f2fs (Flash-Friendly File System) is designed specifically for flash storage like SD cards:

# Install f2fs tools
sudo apt install f2fs-tools

# Format your data partition as f2fs
sudo mkfs.f2fs /dev/sda1

# Mount with optimal options
sudo mount -t f2fs -o noatime,nodiscard /dev/sda1 /mnt/data

# Add to /etc/fstab for auto-mount:
# /dev/sda1  /mnt/data  f2fs  noatime,nodiscard  0  2

Mounting and Unmounting

Temporary Mount

# Create mount point
sudo mkdir -p /mnt/usb

# Mount a drive
sudo mount /dev/sda1 /mnt/usb

# Mount with specific options
sudo mount -o noatime,nodiratime /dev/sda1 /mnt/usb

# Unmount safely (wait for all writes to finish)
sudo umount /mnt/usb

Permanent Mount (fstab)

To automatically mount drives at boot, edit /etc/fstab:

1
2
3
4
5
6
# Find the UUID of your drive (more reliable than /dev/sdX)
sudo blkid /dev/sda1
# Output: /dev/sda1: UUID="a1b2c3d4-..." TYPE="ext4" LABEL="MyDrive"

# Edit fstab
sudo nano /etc/fstab

Add a line:

# <device>                                <mount point>  <type>  <options>           <dump> <pass>
UUID=a1b2c3d4-e5f6-7890-abcd-ef1234567890 /mnt/data      ext4    defaults,noatime,nofail 0      2

Always use nofail

The nofail option prevents boot failures if the drive is disconnected. Without it, your Pi won't boot if the USB drive is missing.

1
2
3
4
5
# Test your fstab entry without rebooting
sudo mount -a

# Verify
df -h /mnt/data

SD Card Longevity Tips

SD cards have a limited number of write cycles. Here's how to extend their lifespan:

1. Minimize Writes

1
2
3
4
# Mount /tmp as tmpfs (RAM disk)
# Add to /etc/fstab:
tmpfs /tmp     tmpfs defaults,noatime,nosuid,size=100M 0 0
tmpfs /var/tmp tmpfs defaults,noatime,nosuid,size=50M  0 0

2. Reduce Logging

1
2
3
4
5
6
7
# Limit journal size
sudo nano /etc/systemd/journald.conf
# Set:
# SystemMaxUse=50M
# SystemMaxFileSize=10M

sudo systemctl restart systemd-journald

3. Disable Swap on SD Card (Use ZRAM instead)

Raspberry Pi OS Bookworm configures ZRAM swap automatically by default. On legacy OS versions, you had to disable the default SD-card-based swap file manually:

1
2
3
# Only required on older legacy OS versions:
sudo dphys-swapfile swapoff
sudo systemctl disable dphys-swapfile
On Bookworm, this service is absent or inactive by default, and swap is routed to the compressed memory device /dev/zram0 automatically.

4. Use noatime Mount Option

# Edit /etc/fstab and add noatime to your root partition:
# PARTUUID=xxx-02  /  ext4  defaults,noatime  0  1

5. Monitor SD Card Writes

1
2
3
4
5
6
# Check total bytes written
cat /sys/block/mmcblk0/stat | awk '{print "Read:", $3*512/1024/1024/1024, "GB, Write:", $7*512/1024/1024/1024, "GB"}'

# Monitor I/O in real-time
sudo apt install iotop
sudo iotop -o

SSD Setup

USB SSD Boot (Pi 4)

For detailed instructions, see How to Move Rootfs to External USB Storage.

Quick overview:

1
2
3
4
5
6
# 1. Flash Raspberry Pi OS to the SSD using Raspberry Pi Imager
# 2. Boot from SD card, then:
sudo raspi-config
# Advanced Options → Boot Order → USB Boot

# 3. Shut down, remove SD card, connect SSD, boot

NVMe SSD Setup (Pi 5)

See How to Boot Pi 5 from NVMe SSD for the complete guide.

Network Storage

NFS Client Setup

Mount a network share from another computer:

1
2
3
4
5
6
7
8
9
# Install NFS client
sudo apt install nfs-common

# Create mount point and mount
sudo mkdir -p /mnt/nas
sudo mount -t nfs 192.168.1.50:/shared /mnt/nas

# Auto-mount via fstab:
# 192.168.1.50:/shared  /mnt/nas  nfs  defaults,nofail,_netdev  0  0

Samba (SMB) Client

Mount a Windows or Samba share:

# Install SMB client
sudo apt install cifs-utils

# Mount a share
sudo mount -t cifs //192.168.1.50/share /mnt/nas -o username=user,password=pass

# Auto-mount via fstab (store credentials securely):
# Create credentials file
echo "username=user" | sudo tee /etc/samba/credentials
echo "password=pass" | sudo tee -a /etc/samba/credentials
sudo chmod 600 /etc/samba/credentials

# Add to fstab:
# //192.168.1.50/share  /mnt/nas  cifs  credentials=/etc/samba/credentials,nofail,_netdev  0  0

For setting up your Pi as a NAS server, see Network Attached Storage Setup Guide.

Backup Strategies

Quick rsync Backup

1
2
3
4
5
6
7
8
9
# Backup to external USB drive
rsync -avz --delete \
  --exclude='/proc' --exclude='/sys' --exclude='/dev' \
  --exclude='/tmp' --exclude='/run' --exclude='/mnt' \
  --exclude='/media' --exclude='/lost+found' \
  / /mnt/backup/

# Backup specific directories
rsync -avz ~/projects/ /mnt/backup/projects/

Automated Daily Backup

# Create backup script
cat << 'EOF' | sudo tee /usr/local/bin/daily-backup.sh
#!/bin/bash
BACKUP_DIR="/mnt/backup/$(date +%Y%m%d)"
mkdir -p "$BACKUP_DIR"
rsync -avz --delete \
  --exclude='/proc' --exclude='/sys' --exclude='/dev' \
  --exclude='/tmp' --exclude='/run' --exclude='/mnt' \
  / "$BACKUP_DIR/" 2>&1 | tail -5
echo "Backup completed: $(date)" >> /var/log/backup.log
EOF
sudo chmod +x /usr/local/bin/daily-backup.sh

# Add to crontab (run at 3 AM daily)
echo "0 3 * * * /usr/local/bin/daily-backup.sh" | sudo crontab -

For comprehensive backup strategies, see Automated Backup Strategies.

Troubleshooting

"Read-only file system" Error

1
2
3
4
5
6
7
# This usually means filesystem corruption
# Remount as read-write temporarily:
sudo mount -o remount,rw /

# Check and repair filesystem (requires reboot):
sudo touch /forcefsck
sudo reboot

Disk Full

# Find what's using space
du -ah / 2>/dev/null | sort -rh | head -20

# Quick cleanup
sudo apt clean
sudo apt autoremove
sudo journalctl --vacuum-size=50M

# Find and remove old log files
sudo find /var/log -name "*.gz" -delete
sudo find /var/log -name "*.1" -delete

USB Drive Not Detected

1
2
3
4
5
6
7
8
9
# Check kernel messages
dmesg | tail -20

# Check USB devices
lsusb

# If the drive is detected but not mounted:
sudo fdisk -l
sudo mount /dev/sda1 /mnt/usb