log2ram was designed to save wear on the SD card of a Raspberry Pi but can be used similarly to prevent unneccessary writes to an SSD.

Logs will be written to a RAM disk instead of constantly writing to the SD card.

  • check it is running: systemctl status log2ram.service
  • config file is at /etc/log2ram.conf – (control size etc)
  • check also with sudo df -h (list will include log2ram and show size / used etc)
  • log files remain at /var.log but are now in ram and saved out once a day.

Install Log2Ram (deb)

Add Log2ram repository on your Raspberry Pi or other DEB-based systems like Ubuntu using command:

$ echo "deb http://packages.azlux.fr/debian/ buster main" | sudo tee /etc/apt/sources.list.d/azlux.list

Add the GPG key:
The given apt way of installing no longer works cos they changed how to add repos or keys or something, so I have to do it manually.

curl -L https://github.com/azlux/log2ram/archive/master.tar.gz | tar zxf -
cd log2ram-master
chmod +x install.sh && sudo ./install.sh
cd ..
rm -r log2ram-master

Or do it the other way:
Update sources list:

$ sudo apt update

Finally, install Log2ram using command:

$ sudo apt install log2ram

You can also install Log2ram manually using source.

Download the latest Log2ram version using command:

$ curl -Lo log2ram.tar.gz https://github.com/azlux/log2ram/archive/master.tar.gz

Extract the downloaded file:

$ tar xf log2ram.tar.gz

This command will extract the contents of Log2ram archive file in a folder called “log2ram-master”. Cd into this folder:

$ cd log2ram-master

Make the installation script executable:

$ chmod +x install.sh

Finally, install Log2ram using command:

$ sudo ./install.sh

Remove the log2ram-master folder:

$ cd ..
$ rm -r log2ram-master

Now, reboot your system to activate Log2ram.

$ sudo reboot

Save Or Write Log Files In RAM Using Log2ram In Linux

The Log2ram script creates a /var/log mount point in RAM. So all log files written in the /var/log folder will not actually be written in the the physical disk. Instead, all logs are written directly to the RAM i.e. tmpfs. The log file for log2ram itself will be written in /var/log/log2ram.log file.

A cron job (/etc/cron.daily/log2ram) will run every day to synchronize the contents of the tmpfs to the SD card or HDD and/or SSD. The script will also synchronize the contents of tmpfs  when you shutdown the system. This way the Log2ram script helps you to prevent excessive writing in SD card.

You can verify if the files are really written in tmpfs, use any disk usage checking programs like df like below:

$ sudo df -h

You will see an output like below:

log2ram 40M  532K 40M 2% /var/log

You can also use the “mount” command to verify the /var/log mount point is present in the tmpfs (RAM).

$ sudo mount

Sample output:

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

Configure Log2ram

Log2ram works out of the box with default values. You can, however, customize its parameters however you please. The default configuration file of Log2ram available at /etc/log2ram.conf. If you open it on any text editing programs, you will see a couple parameters as listed below.

  • SIZE – This parameter defines how much space you want to allocate in the RAM to store log files. The default value is 40M.
  • USE_RSYNC – If you want to synchronize the contents of RAM using rsync program, just set this parameter value as true. By default, Log2ram uses “cp” command to copy contents of tmpfs to disk.
  • MAIL – This parameter is used to notify error messages. You can also disable the error system mail if there is not enough place on RAM. To disable it, simply set its value as false.
  • ZL2R – This parameter enables zram compatibility in your system. It is disabled by default. To enable this option, check this link.

Update Log2ram

To update Log2ram to new available version, simply run “apt update” command.

If you’ve installed it manually, stop Log2ram service:

$ sudo systemctl stop log2ram

And then start the installation again as shown above.

Uninstall Log2ram

To remove Log2ram program, run:

$ sudo apt purge --remove log2ram

If it was installed manually, run:

$ chmod +x /usr/local/bin/uninstall-log2ram.sh
$ sudo /usr/local/bin/uninstall-log2ram.sh