Table of Contents
How to extend the lifespan of memory cards on the Raspberry Pi with Log2Ram
It is common for memory cards to fail after a time of regular use on Raspberry Pi computers. Luckily we have a couple of tips that explain how the lifespan can be increased.
- Raspberry Pi 2 model B (or later)
- Raspberry Pi OS 4.19 (or later) based on Debian Buster
Reducing wear and tear
It is first and foremost recommended to buy memory cards from trusted manufacturers to ensure data integrity. Secondly it is important to use power adaptors with the correct voltage setting. Under-powered adaptors might cause unexpected power dips leading to write errors and data corruption. The most common cause by far is, however, the card breaking from repeated write operations. Consumer cards only handle a certain number of write cycles until the memory cells start to fail. Within the cards exist technology that mitigates this risk, but sooner or later they will fail.
A common misconception is that idle machines do not write any data to disk. It is quite the opposite. On Linux systems log files are constantly written to the disk by the operating system and via applications that run in the background. It is therefore preferable to write the files somewhere else in order to reduce wear and tear of the card.
One solution is to write the files to an external disk or Network Attached Storage (NAS). Another trick is to point the log files to a RAM disk instead which immediately prevents the file system from being hammered. You have to be quick, however, if you wish to view any of the files because they will disappear as soon as the system is rebooted!
Log2Ram is a collection of small scripts which attempt to solve the problem of persistent disk write operations
as well as retrieval of log files between reboots. The script creates a
var/log mount point in RAM meaning that anything
that is normally written into this folder on the memory card will instead be re-directed to RAM. To avoid the permanent
loss of log files, any data will be saved to the memory card every 24 hours. By doing so the number of write
cycles reduce drastically while still keeping the log files intact. It is also possible to adjust the save interval by moving
log2ram script from the
/etc/cron.daily folder into a more frequent folder
/etc/cron.hourly). It is further possible to define the amount of RAM Log2Ram should reserve to the log
files (default is 40 MB) in the file
Log2Ram can be installed either automatically with the APT package manager or manually by downloading a tar ball from the author’s GitHub page. (Only type the commands shown after the dollar $ prompt symbol. The hash # symbol indicates what the result looks like after a command has been run.)
Installing with APT (recommended)
We will start by adding the Log2Ram repository to our APT sources list.
$ echo "deb http://packages.azlux.fr/debian/ buster main" | sudo tee /etc/apt/sources.list.d/azlux.list # deb http://packages.azlux.fr/debian/ buster main
Next we will download a public key which allows us to install Log2Ram on our system.
$ wget -vO - https://azlux.fr/repo.gpg.key | sudo apt-key add - # --2020-07-23 11:44:37-- https://azlux.fr/repo.gpg.key # Resolving azlux.fr (azlux.fr)... 22.214.171.124, 2a01:728:401:13::1:1 # Connecting to azlux.fr (azlux.fr)|126.96.36.199|:443... connected. # HTTP request sent, awaiting response... 200 OK # Length: 3134 (3.1K) [application/octet-stream] # Saving to: ‘STDOUT’ # - # 100%[====================================================================================================================>] 3.06K --.-KB/s in 0.001s # 2020-07-23 11:44:38 (2.06 MB/s) - written to stdout [3134/3134] # OK
If you for some reason get an error the most likely reason is that the CA certificates are missing from the system. It could also mean that you are behind a corporate proxy which uses its own certificates. Our tried and tested brute-force solution is to always upgrade to the latest version of Raspberry Pi OS. Sorry!
# GnuTLS: A TLS fatal alert has been received. # Unable to establish SSL connection. # gpg: no valid OpenPGP data found.
We will now update the packages…
$ sudo apt update # ... # Get:5 http://packages.azlux.fr/debian buster/main armhf Packages [2,671 B] # Fetched 21.6 kB in 11s (1,945 B/s) # Reading package lists... Done # Building dependency tree # Reading state information... Done # All packages are up to date.
… and install Log2Ram.
$ sudo apt install log2ram # Reading package lists... Done # Building dependency tree # Reading state information... Done # The following NEW packages will be installed: # log2ram # 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. # Need to get 4,328 B of archives. # After this operation, 0 B of additional disk space will be used. # Get:1 http://packages.azlux.fr/debian buster/main armhf log2ram all 1.5.2 [4,328 B] # Fetched 4,328 B in 0s (29.0 kB/s) # Selecting previously unselected package log2ram. # (Reading database ... 58797 files and directories currently installed.) # Preparing to unpack .../archives/log2ram_1.5.2_all.deb ... # Unpacking log2ram (1.5.2) ... # Setting up log2ram (1.5.2) ... # Created symlink /etc/systemd/system/sysinit.target.wants/log2ram.service → /etc/systemd/system/log2ram.service. ##### Reboot to activate log2ram ##### ##### edit /etc/log2ram.conf to configure options ####
Reboot the system for the changes to take effect.
$ sudo reboot
We can also do a manual install. Make sure that you are in your home directory.
$ cd ~
Fetch the tar ball from GitHub, where the Log2Ram source files are located.
$ curl -Lo log2ram.tar.gz https://github.com/azlux/log2ram/archive/master.tar.gz # % Total % Received % Xferd Average Speed Time Time Time Current # Dload Upload Total Spent Left Speed # 100 121 100 121 0 0 292 0 --:--:-- --:--:-- --:--:-- 294 # 100 6895 0 6895 0 0 6308 0 --:--:-- 0:00:01 --:--:-- 219k
Extract the files.
$ tar xf log2ram.tar.gz
Go to the newly extracted directory.
$ cd log2ram-master
Make the install script executable and run it.
$ chmod +x install.sh && sudo ./install.sh
Exit the installation directory and delete it because it is no longer needed.
$ cd .. $ rm -r log2ram-master
Reboot the system for the changes to take effect.
$ sudo reboot
Verifying That It Works
After the system has been rebooted we will verify that everything works by running the
df -h command.
As we can see Log2Ram has been mounted on
$ df -h Filesystem Size Used Avail Use% Mounted on /dev/root 3.5G 2.9G 447M 87% / devtmpfs 185M 0 185M 0% /dev tmpfs 217M 0 217M 0% /dev/shm tmpfs 217M 3.2M 214M 2% /run tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 217M 0 217M 0% /sys/fs/cgroup /dev/mmcblk0p1 253M 55M 198M 22% /boot log2ram 40M 3.8M 37M 10% /var/log <----- IT WORKS! tmpfs 44M 0 44M 0% /run/user/100
We can also verify using the
$ mount ... log2ram on /var/log type tmpfs (rw,nosuid,nodev,noexec,noatime,size=40960k,mode=755) ...
Your system has now been updated.
This guide has shown how to extend the lifespan of memory cards on the Raspberry Pi with a small script called Log2Ram. With the help of this tool the number of write cycles to the memory card can reduce drastically while still keeping the log files intact.
For more in-depth information about Log2Ram , visit the authors GitHub page at:
comments powered by Disqus
- How to measure the network speed between two computers with iPerf
- How to turn your Raspberry Pi into a Jukebox
- How to connect to GitHub with SSH
- How to compile OpenEmu for macOS
- How to install Gitk for macOS
- 10 Useful Git Commands
- How to access a Plex Server behind CGNAT with ngrok
- 10 Influential Pixel Artists
- How to customise the bash prompt
- Merge and Rebase in Git