Raspberry Pi SDCard Longevity

From packets2photons
Jump to navigation Jump to search

If you are reading this it is probably because you have had problems with SD Cards and your Raspberry Pi(s). In my experience, this manifests as a kernel panic and when you try to mount your SDcard to an external computer you get errors. To me, the cause of this is because many SD cards are not build to withstand the constant re-writes that occur to these cards.

This post is a good one and much of my recommendations are based on it.

Quality and Size

Start with the best quality and largest SDcard that you can afford for the project. For me at the moment, this is a 64Gig Sandis Ultra but this will change with time so do your research.

Check noatime

When a file is read in Linux a property is updated to indicate the last access date. This should already be off if you are using Rasbian, but please check that noatime is listed next to the fstab line

less /etc/fstab

Remove the Swap partition

The following command should remove the ability to write to a drive based swap partition.

sudo apt-get remove dphys-swapfile

Make sure you reboot after running this.

Log to RAM

Of course if you really need logs, then you can keep writing to the disk, but for my use, I have found non-persistent logs to be adequate.

git clone https://github.com/azlux/log2ram.git


cd log2ram


sudo ./install.sh

If you wish you can modify the size of the log partition with

sudo vim /etc/log2ram.conf

Then simply reboot. When the machine boots back up, you should see an entry similar to:

pi@rpiSender:~ $ df
Filesystem     1K-blocks    Used Available Use% Mounted on
log2ram           131072    3740    127332   3% /var/log

Backup your image

You can create an image by popping your sdcard into your machine

dd if=/dev/sda of=/opt/backup_sda.img

You can restore this image with:

dd if=/opt/backup_sda.img of=/dev/sda

Checking a drive

If you suspect a failing drive, then remove it, put it in another machine and run:

sudo badblocks -v /dev/sda

Obviously you will change /dev/sda to your drive but this should check for errors.