Using Log2RAM on the Raspberry Pi

This guide will show you how to use the log2ram service on your Raspberry Pi.

Raspberry Pi Log2RAM

Log2ram is a bunch of scripts written by azlux to offload the log files on your Raspberry Pi to the RAM.

One of the most significant advantages of offloading your RAM is that it improves your SD Card’s potential lifespan.

Log files are one of the things written to most by the various pieces of software you install.

By pushing the files to your RAM, you can control how often they are written to the SD Card. You will still be able to access these files on the RAM as if they sat on your SD Card.

One of the downsides of using Log2RAM on your Raspberry Pi is that if it loses power before it synchronizes to the disk, you will lose those logs.

The reason for this is that RAM is volatile memory and requires constant power to retain the data stored within it.

Additionally, if you are using an older Raspberry Pi that is already somewhat limited on RAM, you may often run into out of memory errors.

However, with the new Raspberry Pi 4 having up to 8GB of RAM, Log2RAM should no longer significantly take away from your resources.

Equipment

You can check out the list below to see the equipment we used to run the Log2RAM service on our Raspberry Pi.

Recommended

Optional

This tutorial was tested on a Raspberry Pi 400, running an updated version of Raspberry Pi OS.

Preparing your Raspberry Pi for Log2RAM

Before installing Log2RAM and having logs written to RAM instead of memory, we need to prepare our Raspberry Pi’s operating system.

1. Before we get started with Log2RAM, let us update the operating system.

You can update the operating system by using the following two commands.

sudo apt update
sudo apt full-upgrade

2. For the Log2RAM service to work the best on your Raspberry Pi, they recommend installing the rsync software.

Rsync is a piece of software designed to synchronize the contents between a source and destination folder.

We can install rsync to our Pi by running the following command.

sudo apt install rsync

Installing the Log2RAM Service on a Raspberry Pi

Setting up and installing Log2RAM to your Raspberry Pi is a straightforward process as the provided scripts handle everything.

Over the next few steps, we will get the Log2RAM service up and running.

3. Now that we have everything we need, let us download the latest version of the log2ram script.

We can use wget to download the archive of the latest code available from the projects GitHub.

wget https://github.com/azlux/log2ram/archive/master.tar.gz -O log2ram.tar.gz

4. With the Log2RAM archive downloaded to your device, we need to extract it.

We can extract the tar archive by making use of the command below.

tar xf log2ram.tar.gz

5. Let us change into the directory that we just freshly extracted using the tar program.

We can change to that directory by running the command below.

cd /home/pi/log2ram-master

6. Finally, we can run the log2ram install script.

This install script copies everything to where they need to be and configures it to start at boot.

sudo ./install.sh

We need to run this command with the superuser so that it has access to system locations.

7. While you now have the log2ram service installed to your Raspberry Pi, it currently isn’t doing anything.

Due to the way Log2RAM works, you will need to restart your Raspberry Pi.

To restart your device, you can use the command below.

sudo reboot

When you reboot your Raspberry Pi, the Log2RAM script will create a mount point at “/var/log” that exists on your RAM.

Every time a program writes to a log file at “/var/log” it will be written to the RAM instead of the SD card.

The script will also make a backup of your log files that are stored at “/var/hdd.log“. This backup will be saved to daily and when your Raspberry Pi is correctly shutdown.

Configuring Log2RAM on your Raspberry Pi

Now that we have installed the Log2RAM service on to the Raspberry Pi, we can now configure its settings.

1. The config file for Log2RAM is stored at “/etc/log2ram.conf“.

We can begin modifying this file using the nano text editor by using the following command.

sudo nano /etc/log2ram.conf

2. Within this file, there are two options that you want to take note of.

The first option is “SIZE“. Using this option, you can specify the amount of RAM that should be allocated to the mount.

Typically 40 megabytes should be good enough for most users as text files are typically not that large.

However, if you are running software that generates many logs, you might consider increasing this to a larger amount, such as 128M.

SIZE=40M

The other option that you may want to investigate is the “PATH_DISK” option. You can use this option to add additional paths that you wish to have mounted to RAM.

To add additional paths to the RAM disk, separate it by using a semicolon (;). The path must not include the ending forward-slash (/).

PATH_DISK="/var/log"

3. Once you have finished making changes to the config file, you will need to save it.

To save the file, you can press CTRL + X, then Y, followed by the ENTER key.

4. For these changes to take effect, the Raspberry Pi will need to be restarted.

Restarting ensures that your Raspberry Pi’s logs are saved to the disk and that your setting changes take effect.

sudo reboot

Conclusion

You should now have gotten the Log2RAM service up and running on your Raspberry Pi.

This service helps you extend your SD card’s potential life span by reducing the amount of data being written to it.

Your Raspberry Pi’s logs will now be written to the temporary disk located within the RAM. When you shut down, Log2RAM will save these files by synchronizing them back to the disk.

If you want to find the log files that are synchronized to your SD Card, you can find them at “/var/hdd.log/“.

Log2RAM will also synchronize these files daily if a restart or shutdown has not happened in the meantime.

If you have had any issues with getting Log2RAM working on your Pi, feel free to leave a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *

11 Comments

  1. Avatar for Sam
    Sam on

    Why is not rsync enabled in
    sudo nano /etc/log2ram.conf

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Sam,

      By default, rsync is enabled. The option shown within the config file is commented out and will only disable rsync if you remove the comment.

      Kind regards,
      Emmet

  2. Avatar for Mathieu
    Mathieu on

    Thank you for this incredible write up. You are going to save me so many sd cards.

  3. Avatar for Aman Tiwari
    Aman Tiwari on

    Can I delete log2ram-master folder after installation?

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Aman,

      Yes you can if you want to clean up a bit after the installation. Those files are no longer required.

      Cheers,
      Emmet

  4. Avatar for Sander ten Brinke
    Sander ten Brinke on

    Hello.

    As you say it just writes back-ups to /var/hdd.log, meaning it will actually just write logs twice. This seems counter productive to me because this could wear the SD Card down quicker.

    Do I see this wrong? Perhaps I should make the back-up location an external drive or something?

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Sander,

      I believe you may have misunderstood the way this works.

      The script will only write the logs to the disk once a day. The only other time it might write to the disk is upon a normal shutdown.

      Between these daily synchronizations all writes occur purely to the RAM.

      Cheers,
      Emmet

  5. Avatar for Jethro Tombs
    Jethro Tombs on

    Thanks for the tutorial. Will Fail2ban still work correctly if the logs are only being written daily or can fail2ban be configured to look at logs2ram logs in RAM?
    Thanks

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Jethro,

      With the way logs2ram works, the Fail2Ban software will just read the logs that exist inside the RAM.

      log2ram takes over the logs directory, so to Fail2Ban its still accessing the files as it normally would, without any idea that its reading from the RAM.

      Cheers,

      Emmet

  6. Avatar for Walter Herbert
    Walter Herbert on

    I have installed Log2RAM on a Raspberry Pi 2 V1.2 and a Pi 4 8 GB Ram with 256 Gbyte SSD and it worked without any problem.
    Thanks
    Regards

  7. Avatar for Tim
    Tim on

    Thank you for posting this. I am now booting off a SSD instead of a SD card because I was worried about SD card wearing out. I think that SSD is much safer choice but I have opted to install this even running an SSD because why not? Could extend the life of the SSD just like the SD.