Raspberry Pi | Janne Honkonen - Janne Honkonen - Official pages of the Janne Honkonen, visual artist

Making reliable Raspberry Pi – A comprehensive guide

Making Raspberry Pi reliable may take a little time, but makes a big difference. Whenever you are using RasPi as a server, smart home HUB or weather station, here are the best practices to make your Pi last long and work well.

In addition to different several blogs and articles describing this process, I have also added solutions to ease up the read and execute burden and data-storage burden of the SD card making your Pi even more reliable. This is a “comprehensive guide” which combines content and ideas from the several different kind sources mentioned in the end.

1. Preparations

  • Make sure you are using the high-quality SD card. Cheap SD cards have a tendency to corrupt things up, so better to pay little and keep your system running much more reliable and longer.
  • Prepare ventilation: Pi heats up like any other computer. Make sure the place you put your Pi has a good ventilation and use cooling elements if possible.
  • Also: USE a backup power supply. I have 8000mAh battery which can be both charged and discharged at the same time meaning it will always be up and running and on the working condition.

2. Making sure accessing your data (and code) does not screw things up

  • Read and write cycles are the death of your SD card. The best solution to this is not to write. If you need to store data temporarily, store it into /run/shm as a file, which is a shared memory location in the Linux. This means you can read and write data very quickly and it does not affect the card because it is all in the memory. Just note that shared memory is cleared every time your reboot.
    • If you have used or thought of using MySQL as a data storage, forget it. Tinkering with for example with some PHP is a better solution. In one of my projects, I simply used PHP code to export MySQL data into JSON data fields, which I then stored in a file which was synced to a /run/shm on every boot of the Pi.
    • As stated earlier, shared memory is an excellent tool to play with. In addition to storing data temporarily to the /run/shm, you can also store any codebase (.py, .php etc.) you need to run regularly to ease up the read cycles of the card. One solution I prefer to use it automatically copy all needed files to the /run/shm when the Pi starts up.
  • For a longer data storage, I recommend using cloud-based storage from your preferred cloud server provider.
    • Syncing files from the online cloud during boot of the Pi and then storing them to the shared memory is also one good solution.
  • I did not add an example to this because practice doing this is very much depending on the way you are developing your code with the Pi and the kind of environment you are planning to use it with. Just remember my golden rule: If you can run, read or write it in shared memory, do it in shared memory.

3. Making your Raspberry Pi read only

  • Now that you have surely made sure that the Pi does not store any files to the actual SD, it is time to make up it is read-only to prevent any system-based writes to the card.

Make sure you are up to date with your Pi

  • apt-get update to make use that your package sources are up to date
  • apt-get upgrade to make sure all packages in order.
  • Reboot in case there are any core changes

Remove any non-essential files and packages

  • apt-get remove –purge wolfram-engine triggerhappy anacron logrotate dphys-swapfile xserver-common lightdm
  • insserv -r x11-common; apt-get autoremove –purge

Replace your log system with the circular memory buffer

  • insserv -r x11-common; apt-get autoremove –purge
  • no need to tail your /var/log anymore. Simply use logread command to read the logs.

Also, move some system files to the temporary file system so system won’t get angry

  • rm -rf /var/lib/dhcp/ /var/run /var/spool /var/lock /etc/resolv.conf will delete actual files
  • ln -s /tmp /var/lib/dhcp will make symbolic link from the file to the /tmp
  • ln -s /tmp /var/run  will make a symbolic link from the file to the /tmp
  • ln -s /tmp /var/spool will make symbolic link from the file to the /tmp
  • ln -s /tmp /var/lock will make symbolic link from the file to the /tmp
  • touch /tmp/dhcpcd.resolv.conf will create a empty file to tmp
  • ln -s /tmp/dhcpcd.resolv.conf /etc/resolv.conf makes a symbolic link to earlier

On Debian Jessie, you need to move random-seed to writable location

  • rm /var/lib/systemd/random-seed will delete the seed file
  • ln -s /tmp/random-seed /var/lib/systemd/random-seed creates seed file to temporary location
  • make sure that system creates file during boot by editing its systemd service file
    • edit /lib/systemd/system/systemdrandomseed.service
    • add ExecStartPre=/bin/echo “” >/tmp/randomseed to the service section

Edit fstab (file system definitions) to make temporary files and log (to be safe) in memory instead of card

  • edit /etc/fstab
  • add tmpfs /var/log tmpfs nodev,nosuid 0 0 to the end of the file
  • add tmpfs /var/tmp tmpfs nodev,nosuid 0 0 to the end of the file
  • add tmpfs /tmp tmpfs nodev,nosuid 0 0 to the end of the file
  • add ro to the parameters of the mmcblk0p1 and mmcblk0p2
    • example: /dev/mmcblk0p1 /boot vfat defaults,ro
    • example: /dev/mmcblk0p2 / ext4 defaults,noatime,ro
    • Disable your swap file and filesystem check and make your system read-only
      • edit /boot/cmdline.txt
      • add fastboot noswap ro to the end of the line

4. General improvements

Make sure your Pi stays on the right time

  • Install network time protocol to use with apt-get install NTP
  • check and configure your timezone with raspi-config

Sometimes even secured Pi gets stuck or processes go nuts. For this we can enable watchdog to boot the Pi when necessary with the following:

  • enable watchdog and install it on the use
    • use command modprobe bcm2708_wdog
    • and install the package with apt-get install watchdog
    • add bcm2708_wdog module to load on a boot
      • edit /etc/modules
      • add bcm2708_wdog to the end
  • edit /etc/watchdog.conf
    • add (or uncomment) watchdog-device = /dev/watchdog .. this points to the watchdog module enabled earlier
    • add (or uncomment) max-load-1 = 2 .. this will boot device if the 1-minute load is more than >2, which usually means hung up system
  • make watchdog start at the boot
    • insserv watchdog
  • with the Raspbian Jessie, you need to add this to watchdog systemd service file
    • edit /lib/systemd/system/watchdog.service
    • Modify it by following
      • [Install]

enable the system to reboot if kernel panic occurs

  • by experience note: may lead to infinite boot cycle if kernel panic occurs on the boot– make sure system boots up nicely before activating this one
  • edit /etc/sysctl.conf
  • add kernel.panic = 10 to the end

5. Beauty tips

  • If you are are a hardcore dev like me, you will often visit your Pi and known when it is in read or read/write mode, is precious. From the article of the petr.io blog, this is the best way to do it:
    • Edit /etc/bash.bashrc
    • Add following to the end of the file
      • # set variable identifying the filesystem you work in (used in the prompt below)
        fs_mode=$(mount | sed -n -e "s/^.* on \/ .*(\(r[w|o]\).*/\1/p")
        # alias ro/rw 
        alias ro='mount -o remount,ro / ; fs_mode=$(mount | sed -n -e "s/^.* on \/ .*(\(r[w|o]\).*/\1/p")'
        alias rw='mount -o remount,rw / ; fs_mode=$(mount | sed -n -e "s/^.* on \/ .*(\(r[w|o]\).*/\1/p")'
        # setup fancy prompt
        export PS1='\[\033[01;32m\]\u@\h${fs_mode:+($fs_mode)}\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
        # aliases for mounting boot volume
        alias roboot='mount -o remount,ro /boot'
        alias rwboot='mount -o remount,rw /boot'
    • This will show your mount status on the shell’s prompt, as well as gives you chance easily to change between a read/write (just type rw) or just read mode (just type ro) if you need to make changes to your code. Also, similarly to boot using roboot and rwboot.

Here. If did everything correctly and followed my practices, your Pi should be up and be running in read-only mode, using shared memory to access temporary data and executing code in the shared memory. With a good SD card, your system should keep on running for years as long as have electricity available. If you have any questions or ideas, leave them in the comments. I will also fix this article if needed and upgrade as the time goes by.

Following articles were used as a source or help while writing this article:

© Janne Honkonen - www.jannehonkonen.com