Log2Ram: Extending SD Card Lifetime for Raspberry Pi LoRaWAN Gateway

My LoRaWAN gateway (“Contributing an IoT LoRaWAN Raspberry Pi RAK831 Gateway to The Things Network” is running and working great now for more than a month and it already has transmitted more than 30k messages:

Gateway Overview

Gateway Overview

This creates a lot of log entries on the micro SD card of the Raspberry Pi. To avoid writing too many times log data, I have installed Log2Ram.

By default, the Raspberry Pi uses a micro SD card as storage device. The SD card as a FLASH device is subject of ‘wear-out’: each FLASH block can only be erased and written a limited number of time, and after that it fails. The ‘wear leveling’ of the disk device will allocate and use a new flash block in that case, but after some time there are no free blocks any more and the system will fail.

A running gateway will create lots of log messages stored on the card, and I have read reports that depending on the card and card size it might fail after 6-18 months.

As most of the data written as a gateway are the Linux log files, there is a solution to reduce the number of writes to the SD card using Log2Ram. It creates the mount point /var/log in RAM, so any writes to /var/log will be written to the RAM disk. Every hour, a cron job will write the RAM disk to FLASH, or at the time of a shutdown. So this greatly reduces the stress on the SD card.

Installation and setup is easy:

lot2ram installation

log2ram installation

Go to the pi home directory:

cd /home/pi

Clone there the log2ram git repository:

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

Change directory to the downloaded repository folder:

cd log2ram

Make the installation script executable:

chmod +x install.sh

Install it:

sudo ./install.sh

Change the log size value to 128M:

sudo nano /etc/log2ram.conf
Log Size 128 MByte

Log Size 128 MByte

Save the file (CTRL-X, yes, ENTER), then reboot:

sudo reboot

To check if it was working, use

df -h

This should show the new disk:

log2ram disk

log2ram disk

In addition to this, check with

mount

which should show our mount point:

log2ram on /var/log type tmpfs (rw,nosuid,nodev,noexec,relatime,size=131072k,mode=755)

That’s it! With this, writing to FLASH should now be greatly reduced. Time for muffins!

Happy Logging 🙂

Links

39 thoughts on “Log2Ram: Extending SD Card Lifetime for Raspberry Pi LoRaWAN Gateway

  1. Hi Erich,
    Are you using any hardware / software on your Pi to avoid SD card corruption if the power gets interrupted?
    Thanks, Ian

    Like

    • Power outage is a very rare event in Switzerland. I power the Pi with PoE which is attached to a UPS. But I’m think about adding a small Pi UPS, that way it can shutdown the Pi with a pin. It is possible to assign a pin which can be used to power down the Linux: I’m using that already with a Kinetis K22 (tinyK22) board.

      Like

  2. Pingback: Give Your Raspberry Pi SD Card a Break: Log to RAM | Hackaday

  3. Hi Eric,

    You mention a cron job that can write the logs from RAM to SD card every hour, but I don’t see any setup for that.
    Is this something automatically set up, or it has to be defined in addition?

    Like

  4. Are you aware of any actual SD card failures in a Raspberry Pi? Statistics? I’ve had one running continuously since December 2012 with extensive logging to an 8GB card with no problems. I was persuaded to not use RasPi in a commercial product because the SD would be unreliable, but I’m not sure that’s the case with quality (SanDisk or Samsung) cards.

    Like

    • I’m running openHAB on my Raspberry Pi, and SD card failure is a big talking point in the user community. Statistics would be difficult to get, since most people just plug in a new SD without ever reporting the failure, but there are enough people claiming it that I think the risk is real. As you say, using higher-quality cards probably mitigates that risk.

      Like

  5. Howdy Erich, I just tried this on my Pi Zero W that is Running FlightRadar24 and it didn’t seem to create the mount for it. The GIT pulled and it seemed to install then reboot, then I edit the selected file but after rebooting there is not an entry under `df -h` for log2ram.

    Like

  6. Great job, this is a really good idea and perfect implementation. Thank you! I’ll have this on all my Raspberries for sure.

    Like

  7. Have a look at https://github.com/StuartIanNaylor/zram-config as it has a number of advantages over Log2Ram.

    Some problems with Log2Ram :-
    Firstly the hourly write out of logs is near pointless as its only there for a full system crash as is written on stop. Its near pointless as the log info of a unlikely full system crash is still likely to be lost unless you where fortunate enough that it happened as the hourly write had just taken place.
    Also any log that has changed gets copied in full on every hour which sort of negates it purpose and strangely its for no real reason
    Also it stores all the oldlogs in memory which isn’t required but copies all this on start again for no real reason.

    zram-config above does zswap, zram & a zlog but in a much better manner.
    It uses zram in the upper of an OverlayFS mount with the original dir in a lower bind bind so because of the copyup of COW only changes are in zram.
    The same principle is used for zdir and large directories with zero writes in extremely small memory footprints can be accomplished.
    It also uses the olddir directive of logrotate and ships oldlogs to persistant and again saves far more ram than log2ram.
    You can create any number of zswaps, zdir and a zlog via /etc/ztab and will greatly reduce sd write whilst using a much smaller memory than Log2ram and is much less likely for /var/log to hit the limit and fail as log2ram often does.

    Like

  8. I think, as the number of sectors written to the sdcard is NOT reduced by writing them at another time (e.g. delayed, grouped or whatever), the wear level doesn’t get any better.
    I think, you have to throw away data to reduce wear leveling.
    So I think, you have to disable the writes to sdcard completely or may be sent the logs to a remote syslog server or via email or similar (but don’t save a temporary file).

    Delaying and grouping writes is used to increase performance on disks where access time is significant (= hard disks). It is also useful to reduce power dissipation in some cases.

    Also, consider something like laptop-mode-tools…

    Like

    • Agreed: the less data the better. On the writes it depends how they are cached (or synced with the disk). So if there is a small amount of data, but the data is flushed/written all the time as it happens with the log files, this will greatly increase the wear of the disk.

      Liked by 1 person

  9. Pingback: Tutorial: MCUXpresso SDK with Linux, Part 1: Installation and Build | MCU on Eclipse

  10. You are right writing a lot of small data chunks to your sd card limits the life time because of the sd card block size.

    A better way is to just increase the commit rate of the file system

    /etc/fstab
    /dev/sda5 / ext4 defaults,noatime,commit=60 0 1

    default is 5 seconds

    If you set a to a value of 60 – 300 a normal sd card should life forever in your system.

    Like

  11. Pingback: Building a Raspberry Pi UPS and Serial Login Console with tinyK22 (NXP K22FN512) | MCU on Eclipse

  12. sadly, that disk would not appear.
    retried all the lines manually (with sudo) no effect.
    checking with crontab -e (and sudo crontab -e)it turns out nothing was written there, but that should not matter for the install.
    Ended up using the fstab commit

    Like

  13. After running Log2ram disk for a couple of weeks in noticed that df -h will always give 100% on use.
    Is this normal?
    Shouldn’t the crone job flush the ramdisk every hour?
    crontab -l will not show me anything regarding Log2ram.
    What do I miss here?

    Like

  14. Hello Erich. Thank you for this informative and useful article. I installed it today and noted that you may have missed a step – or at least missed one that linux experts might not be aware of. The file log2ram.conf is in the directory /home/pi/log2ram after running the installation script. Therefore, there should be an extra step inserted after installation of log2ram

    cp /home/pi/log2ram.conf /etc/.

    then

    Change the log size value to 128M:

    etc.

    Like

  15. I use RPi 4 for a kiosk. SD card failed within weeks. I disabled SD card writes using a very simple method and it’s been working fine ever since. Also added a 15 second watch dog timer.

    To disable writes to SD Card:
    At the GUI, click on Pi–>Preferences–>Raspberry Pi Configuration
    Go to the “Performance” tab and you’ll see an option “Overlay File System.” Click the “Configure…” button.
    Select “Overlay: Enabled” and “Boot Partition: Read-only.”
    Click “OK” and wait while the system works. It may take a minute or more to complete. This is normal.
    Reboot when prompted
    Source: https://learn.adafruit.com/read-only-raspberry-pi

    To enable the WDT,
    1) Enable the hardware watchdog on your Pi and reboot
    sudo su
    echo ‘dtparam=watchdog=on’ >> /boot/config.txt
    reboot
    After this reboot the hardware device will be visible to the system. The next steps install the software side of this to communicate with the watchdog.
    2) Install the watchdog system service
    sudo apt-get update
    sudo apt-get install watchdog
    3) Configure the watchdog service
    sudo su
    echo ‘watchdog-device = /dev/watchdog’ >> /etc/watchdog.conf
    echo ‘watchdog-timeout = 15’ >> /etc/watchdog.conf
    echo ‘max-load-1 = 24’ >> /etc/watchdog.conf
    4) Enable the service
    sudo systemctl enable watchdog
    sudo systemctl start watchdog
    sudo systemctl status watchdog

    Now next time your Raspberry Pi freezes, the hardware watchdog will restart it automatically after 15 seconds.
    If you want to test this you can try running a fork bomb on your shell:
    sudo bash -c ‘:(){ :|:& };:’
    WARNING Running this code will render your Raspberry Pi unaccessible until it’s reset by the watchdog.

    Liked by 1 person

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.