Archives

All posts for the month November, 2017

BACKGROUND

Raspberry Pi computers…   they are used for many DIY projects and are even used in production environments for various roles from IoT (Internet of Things) to network servers of all kinds.  They’re great for small industrial uses as well as IoT applications and the reason why is because they have a small power footprint and no moving parts, making them extremely robust.  The Achilles heel however, is the SD card that is used in them!  Seemingly, there are many accounts abound that tell tales of card corruption which can bring down a RPi based application.

Being a seasoned Linux user, I noticed one thing that may be to blame: growing logs & swap file usage in /var/swap & /var/log.   SD cards have a limited amount of write cycles and when they’re used up, the card will likely need to be binned, and a new one written and replaced.  It seems clear that most of the read/write activity seems to be done in the /var directory and so I figure that a better solution to this problem is to setup a USB drive (SSD or thumbdrive), format it for EXT4, and mount it permanently as /var in /etc/fstab.  This article will explain how to do just that, and by doing so, you will significantly prolong the life of your sd card!  Read on:

SOLUTION

The first thing you will notice when you plug a USB drive into your Pi is that it will most likely be formatted as FAT (we want to change this).  You can see the details of your USB drive by running fdisk -l and viewing the output below:

As you can see, in my example, the USB drive is an 8GB size and is already formatted as a “linux” filesystem, but in many cases you would see FAT or NTFS here.  Our goal is to delete any partitions on the USB and create a new one that looks like this example.   You can use any size drive you want, I recommend 8GB or larger – and larger is always better!

After you have created a single Linux partition on your USB device, you then want to write the changes to the USB and then format it.  In this example, the drive is /dev/sda.  We want to format the first partition so that partition will be /dev/sda1 and so we will issue the following command once we write the partition and close fdisk:

mkfs.ext4 -L var /dev/sda1  NOTE: be SURE you know which device you are about to format!!!   we assume your device will be /dev/sda but it could be different so check when you run fdisk -l.   The format can take a few minutes to a while depending on the size of your USB device, so be patient and wait until you are returned to a prompt.  Once back at the prompt, you will have a formatted EXT4 partition on your USB device, and you will be ready for the next step, copying your /var directory to the USB.

To copy the contents of /var to your USB, become root or use sudo and then mount the USB to a mountpoint.  I suggest we create /mnt/usb and mount to that to keep things simple.   execute:   sudo mount /dev/sda1 /mnt/usb then you will now be ready to move your /var contents to the USB.

To move your /var contents to the USB execute:  mv /var/* /mnt/usb/. NOTE: you may get an error on moving the swapfile, this is OK, do not worry about it.  We need to then check /mnt/usb and verify that all our /var stuff is in there by running ls /mnt/usb  and you should see the same directories and files in there as you did in /var.   You are now ready to setup your USB device as the permanent home for the /var location.  To do this, you need to modify /etc/fstab and add an entry to the file showing the usb device mounted as /var as follows (use your favorite text editor – carefully!):

Once you have the fstab file looking similar to this example, you can then save the file, and reboot your Raspberry Pi.   When the Pi reboots, it will now mount /var to the USB drive and no longer use the SD card for swap or for logs and log rotation.   This will greatly save a huge number of write cycles to your SD card!  After you make this change you can also ensure swapping is turned off by running these commands:

sudo dphys-swapfile swapoff
sudo dphys-swapfile uninstall
sudo update-rc.d dphys-swapfile remove

I hope you find this info useful!