Skip to content
July 24, 2015 / lasseathome

Avoid Killing SD Card on Raspberry Pi

Having read that there is a risk of killing the SD card with a lot of writes caused me to set up a system so that it writes the big log files and data to a normal hard drive while keeping the regular and important system information on the SD card. The reason is that I have a temperature logger based on Telldus Tellstick Duo that listens to sensor events on the 433 MHz band and writes temperature log data several times a minute. This could, according to reports and discussions on the internet, cause a burn out of the solid state card, I do not know if the risk is real for my case but I often reason it’s better to be safe than sorry. I did it three years ago on my first RPi but did not write down the procedure so here I go again documenting for anyone who is interested at the same time as I do it for my new RPi 2B. This is the hardware that I have:

  1. Rapberry Pi 2 B.
  2. External USB harddrive.
  3. USB hub with power adapter.
  4. Telldus Tellstick Duo (not important for this).
  5. A 7-port USB hub with power supply 5V 2.5A that powers the Raspberry Pi, Tellstick Duo, and USB hard drive.
  6. For completeness I also have a WiFi network adapter DWA-121 and wireless integrated keyboard/trackpad the dogles for these are connected directly to the RPi (not important for this).
  7. A separate laptop with Ubuntu Mate with SD card reader and USB connectors.

The additional USB hub (5) with external power source was necessary to get enough power for the USB hard drive to startup.

Starting up

Downloaded Raspbian Jessie via torrent and wrote it to the card using the Ubuntu laptop. Identified the device for the SD card with df and removed the additional letters and wrote the image with dd

df -h
# output ... 
# /dev/mmcblk0p3   27M  444K   25M   2% /media/SETTINGS
# /dev/mmcblk0p6  6,3G  2,6G  3,5G  43% /media/root
# /dev/mmcblk0p5   60M   19M   42M  32% /media/boot1
sudo dd bs=4M if=2015-11-21-raspbian-jessie.img of=/dev/mmcblk0

Booted up the RPi. The first and most important thing: Added password to user pi.
Then: Changed locale, changed keyboard, grew the file system. Reboot, run an update and an upgrade, and installed emacs and prepared the keyboard for swap Caps & Ctrl.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install emacs
cd /etc/default/
sudo nano keyboard
# added options XKBOPTIONS="...,ctrl:swapcaps"

Now I have a base system that will give me a basic working configuration so I can start working with the file system.

Placing /var/ on the external USB disk

Now it is time to get down to the more serious business that can do damage to the system so I work with the file system off-line on a separate machine to it up. I shut down the RPi and inserted the USB HDD drive and SD card in the Linux Laptop. In short the core idea is to place all the data in /var on the USB HDD. This is done with the motive that the files that change the most are placed in the /var/ area in unix systems. For example the log files, the www files, the database data, etc. Therefore I intend to place the USB HDD there, with the intention to have all my file sharing and data stored there. So I attached the USB HDD to the Linux machine and did the following.

  1. Formatted the HDD to an ext4 system using gparted on the linux machine, and gave it label VAR.
  2. Went to the directory of the memory card that contain the root partition and into /SDCardMountPath/etc/
  3. Added a line to the fstab file, extracted the UUID of the disk using blkid.
    sudo emacs fstab&
    blkid
    #output
    # ...
    # /dev/sdb1: LABEL="VAR" UUID="87f1540c-dac0-4911-98c1-1a23666cafed" TYPE="ext4" PARTUUID="439c27a4-01"
    # the resulting line for /etc/fstab was then
    # UUID=87f1540c-dac0-4911-98c1-1a23666cafed /var               ext4    errors=remount-ro 0       1
    
  4. Moved all data from the card’s /var/ directory to the HDD
    cd /SDCardMountPath/var/
    sudo mv * /HDDMountPath/

    This took some time to complete.

  5. Double checked that the /SDCardMountPath/var/ directory was empty after the move so that a file system can be mounted there at next RPi boot.
  6. Ran sync;sync to sync the filesystems and unmounted and removed them from the Laptop.
  7. Inserted the card in the RPi and the USB HDD in the USB hub. Booted up the RPi. At boot an fschk was run and all worked fine so the RPi 2 B is up with the HDD mounted at /var/, ready to be filled.

Now I have a system where /var/ is stored on a HDD that can tolerate more writes than an SSD disk can, so I can safely log Gigabytes of data. For curiosity I can inform the interested reader that I started logging with my first Raspberry Pi 1 B, with a similar setup to an USB Disk, in August 2013 and the data is now in January 2016, 640 MB, and there is not yet any problem with the RPi and SC card.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: