Skip to content

Storage Management for Raspberry Pi

Start by identifying what is mounted and where your files are stored. This lesson separates safe inspection and file backups from destructive partitioning, formatting and boot migration.

Before you start

You need a working Raspberry Pi OS login and the terminal and permissions lessons. Run all commands on the Pi.

You do not need a spare drive for the first exercises. A separate backup device is needed to protect against losing the OS card; two folders on the same card only demonstrate copying and restoration.

Do not format a drive to follow this lesson

Formatting and partition-table changes erase data. This beginner lesson does not require mkfs, raw-disk writes, root filesystem conversion or repartitioning. Device names such as /dev/sda1 are not universal or stable.

1. Distinguish a disk, partition and mount point

Term Meaning Example only
Disk Whole block device /dev/mmcblk0
Partition Region on that disk /dev/mmcblk0p2
Filesystem Organisation of files inside a partition ext4
Mount point Directory through which you access that filesystem /

A USB disk may have several partitions. Mounting a partition makes its files visible through a directory; it does not mean formatting it.

2. Inspect the current Raspberry Pi storage

1
2
3
4
lsblk -o NAME,SIZE,TYPE,FSTYPE,LABEL,UUID,MOUNTPOINTS
findmnt /
findmnt /boot/firmware
df -hT /

Find the partition mounted at / and do not use it as an experimental target. df reports space in a mounted filesystem; lsblk describes block devices. A card's advertised capacity and displayed binary units need not match numerically.

Check: you can point to the root filesystem and the boot partition in the output, and explain which one holds your home directory on a normal installation.

To inspect the size of your own files, without deleting anything:

du -sh "$HOME"

This may take time or report inaccessible entries. Disk usage is not a wear measurement or a reliable prediction of remaining card life. Kernel I/O errors warrant investigation, not an automatic remount as read-write.

3. Practise a backup and restore

Create an isolated exercise directory:

1
2
3
4
5
6
7
8
9
storage_dir=$(mktemp -d "$HOME/pi-storage-practice.XXXXXX")
cd "$storage_dir"
mkdir source backup restored
printf 'Keep this project note\n' > source/note.txt
cp -a source/. backup/
cmp source/note.txt backup/note.txt
cp -a backup/. restored/
cmp source/note.txt restored/note.txt
cat restored/note.txt

cp -a preserves ordinary file attributes when permitted; source/. means the contents of that directory. The two cmp commands should produce no output, and the restored note should read Keep this project note.

This is not yet a disaster-recovery backup. All three folders are on the same storage device. Real protection requires a separate device or remote destination, multiple generations where appropriate, and a restore test.

If using rsync for larger project directories, inspect a dry run first:

rsync -avn source/ backup/

This optional command requires rsync. The trailing slash copies the source directory's contents; -n prevents changes. Remove only n when you understand the proposed copy. Do not add --delete to a first backup: it can delete destination files.

A backup of a running database may be inconsistent even if file copying succeeds. Use that application's export/snapshot procedure. Copying / while excluding a few directories is not automatically a bootable OS backup.

4. Optional: identify a USB data partition

Use a spare drive containing a known, already formatted data partition. Do not perform this on the only copy of important data.

Before connecting it, run the lsblk command above and record the devices. Connect the drive, run it again, then match size, label, filesystem and UUID, not just the newest sdX name.

Desktop installations may mount removable storage automatically. Check MOUNTPOINTS before mounting it again. USB adapters, power and storage firmware can affect recognition; do not conclude that a missing mount means the drive must be formatted.

5. Optional: mount read-only first

The following uses a placeholder, not a real device name. Replace YOUR-DATA-UUID with the UUID of the verified data partition, and ensure it is not the OS root or boot partition.

Create a mount point, then mount:

1
2
3
4
sudo mkdir -p /mnt/pi-course-usb
sudo mount -o ro /dev/disk/by-uuid/YOUR-DATA-UUID /mnt/pi-course-usb
findmnt /mnt/pi-course-usb
ls -la /mnt/pi-course-usb

These commands change the mount state and require administrator privileges. A read-only mount prevents ordinary file writes, but some filesystems can perform recovery during mount; it is not a forensic write blocker. Do not use a damaged disk that needs professional recovery for this exercise.

Check: findmnt identifies the intended partition and shows ro in the options. You can inspect known files. If mounting fails, read the error and verify the filesystem type and existing mounts; do not format the device as a shortcut.

To finish, leave the mount point in every terminal and close applications using it:

1
2
3
cd "$HOME"
sudo umount /mnt/pi-course-usb
findmnt /mnt/pi-course-usb

The final findmnt should produce no mount entry and return nonzero. Wait for unmount to succeed before unplugging. For target is busy, close open files and terminals in that directory rather than forcing unmount.

6. Know when a persistent mount is appropriate

Do not edit /etc/fstab merely to complete the read-only exercise. It affects future mounts and boot behaviour.

For a planned persistent data drive, use its actual UUID, correct filesystem type and a unique mount point. Back up the file, validate syntax with findmnt --verify, and test the specific mount before rebooting. An optional removable drive can use nofail, but that does not make applications safe when the drive is absent: they may write into the underlying directory on the OS card.

Never copy a sample root or boot entry over the installed entries. Use the dedicated storage migration guide for changing boot storage.

7. Diagnose low space without automatic deletion

Start with df -hT /, then inspect the project directory or application known to use space. Deleted-but-open files, mounts and root-owned directories can explain differences between df and du.

Avoid beginner “cleanup” scripts that remove all logs, uninstall dependencies or rewrite mount options. Preserve error logs until you understand the failure. A read-only root filesystem after I/O errors may indicate damage; do not immediately force it writable or run a repair tool on a mounted filesystem.

ZRAM, OverlayFS, NVMe boot and filesystem tuning solve specific needs, not basic file-management prerequisites. In particular, do not infer swap defaults from a release name: inspect /usr/sbin/swapon --show on the actual system before changing anything. The absolute path also works when the administrative program directory is not in your shell's PATH.

Troubleshooting

Symptom First action
Drive not listed Compare lsblk, connection, power and kernel messages
Listed but not mounted Check filesystem and current mounts; mount only the identified partition
Permission denied on mounted files Check ownership and filesystem-specific permission rules
Disk full Identify usage before deleting; check backups and application retention
Read-only filesystem Preserve data and investigate errors; use an offline recovery plan
Backup command succeeds Restore a sample and compare contents; success alone does not prove completeness

The file-copy exercise and storage inspection can be validated on the existing Pi without mounting new hardware. USB mounts, persistent configuration and disk recovery are separate procedures, not claimed as performed for this revision.

Check your understanding and continue

Can you identify the root partition without guessing its name? Can you restore a file from your exercise copy? Why does a same-card copy not protect against card loss?

You now have the Linux foundations for C++ Hardware Programming. For a real backup plan, continue with Automated Backup Strategies; review its destination and restore procedure before automating anything.

Donate