From bf8f16bba6ce5a697e74af0a22a8974b08620ac7 Mon Sep 17 00:00:00 2001 From: HyP3r Date: Tue, 13 Dec 2022 15:11:13 +0100 Subject: [PATCH] Added awareness of systemd-journald (#200) * Reformatted and refactored some files * Added awareness of systemd-journald - Added dependency for bash in debian control file, because this implementation uses bash arrays - Changed in all shell scripts the interpreter to bash - Replaced "service log2ram stop" with "systemctl stop log2ram" - Replaced "-a" with "] && [" - Increased SIZE and LOG_DISK_SIZE to 128M and 256M because journald can use a lot of space - Introduced a new switch JOURNALD_AWARE --- README.md | 2 +- debian/control | 1 + debian/preinst | 3 +-- install.sh | 4 ++-- log2ram | 31 +++++++++++++++++++++++++++---- log2ram.conf | 12 +++++++++--- uninstall.sh | 2 +- 7 files changed, 42 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 5494aba..ca3249e 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ If you do not get any line as response of these commands, something is not worki ## Upgrading -You need to stop Log2Ram (`service log2ram stop`) and execute the [installation](#installation) process. If you used APT, this will be done automatically. +You need to stop Log2Ram (`systemctl stop log2ram`) and execute the [installation](#installation) process. If you used APT, this will be done automatically. ## Customization diff --git a/debian/control b/debian/control index ad706e5..9c6de23 100755 --- a/debian/control +++ b/debian/control @@ -1,5 +1,6 @@ Package: log2ram Version: VERSION-TO-REPLACE +Depends: bash (>= 4.3) Section: net Priority: optional Architecture: all diff --git a/debian/preinst b/debian/preinst index 986b7d0..23f733e 100755 --- a/debian/preinst +++ b/debian/preinst @@ -1,5 +1,4 @@ -#!/bin/bash - +#!/usr/bin/env bash systemctl -q is-active log2ram.service && systemctl stop log2ram.service systemctl -q is-active log2ram-daily.timer && systemctl stop log2ram-daily.timer rm -rf /var/hdd.log diff --git a/install.sh b/install.sh index 2585fa7..cb87d6e 100755 --- a/install.sh +++ b/install.sh @@ -1,7 +1,7 @@ -#!/usr/bin/env sh +#!/usr/bin/env bash systemctl -q is-active log2ram && { - echo "ERROR: log2ram service is still running. Please run \"sudo service log2ram stop\" to stop it." + echo "ERROR: log2ram service is still running. Please run \"sudo systemctl stop log2ram\" to stop it." exit 1 } [ "$(id -u)" -eq 0 ] || { diff --git a/log2ram b/log2ram index e38817c..c548ece 100755 --- a/log2ram +++ b/log2ram @@ -1,4 +1,4 @@ -#!/usr/bin/env sh +#!/usr/bin/env bash . /etc/log2ram.conf @@ -16,13 +16,36 @@ is_safe() { [ -d "$HDD_LOG" ] || exit 1 } +## @fn journald_logrotate() +## @brief Logrotate the journal if the current RAM_LOG path is part of the journald directory +journald_logrotate() { + if ! [ -x "$(command -v journalctl)" ] || ! [ "$JOURNALD_AWARE" = true ]; then + return 1 + fi + + if journalctl --header | grep "File path" | grep "$RAM_LOG" >/dev/null 2>&1; then + journalctl --rotate + return 0 + else + return 1 + fi +} + ## @fn sync_to_disk() ## @brief Sync memory back to hard disk sync_to_disk() { is_safe - if [ -z "${NO_RSYNC}" -a -x "$(command -v rsync)" ]; then - rsync -aXv --inplace --no-whole-file --delete-after "$RAM_LOG"/ "$HDD_LOG"/ 2>&1 | tee -a "$LOG2RAM_LOG" + optional_params=() + + if journald_logrotate; then + optional_params+=("--include=journal/*/*@*.journal") + optional_params+=("--exclude=journal/*/*") + fi + + if [ -z "${NO_RSYNC}" ] && [ -x "$(command -v rsync)" ]; then + rsync -aXv --inplace --no-whole-file --delete-after "${optional_params[@]}" "$RAM_LOG"/ "$HDD_LOG"/ 2>&1 | + tee -a "$LOG2RAM_LOG" else cp -rfup "$RAM_LOG"/ -T "$HDD_LOG"/ 2>&1 | tee -a "$LOG2RAM_LOG" fi @@ -48,7 +71,7 @@ sync_from_disk() { exit 1 fi - if [ -z "${NO_RSYNC}" -a -x "$(command -v rsync)" ]; then + if [ -z "${NO_RSYNC}" ] && [ -x "$(command -v rsync)" ]; then rsync -aXv --inplace --no-whole-file --delete-after "$HDD_LOG"/ "$RAM_LOG"/ 2>&1 | tee -a "$LOG2RAM_LOG" else cp -rfup "$HDD_LOG"/ -T "$RAM_LOG"/ 2>&1 | tee -a "$LOG2RAM_LOG" diff --git a/log2ram.conf b/log2ram.conf index 932b5e4..406de83 100644 --- a/log2ram.conf +++ b/log2ram.conf @@ -5,7 +5,7 @@ # If it's not enough, log2ram will not be able to use ram. Check you /var/log size folder. # The default is 40M and is basically enough for a lot of applications. # You will need to increase it if you have a server and a lot of log for example. -SIZE=40M +SIZE=128M # Select the log syncing method between the log directory on disk and in the RAM. # The variable may be uncommented out, setting it to false, if "cp" is preferred over "rsync". @@ -18,10 +18,16 @@ SIZE=40M # Change it to false and you will have only a log if there is no place on RAM anymore. MAIL=true -# Variable for folders to put in RAM. You need to specify the real folder `/path/folder` , the `/path/hdd.folder` will be automatically created. Multiple path can be separeted by `;`. Do not add the final `/` ! +# Variable for folders to put in RAM. You need to specify the real folder `/path/folder` , the `/path/hdd.folder` will +# be automatically created. Multiple path can be separeted by `;`. Do not add the final `/` ! # example : PATH_DISK="/var/log;/home/test/FolderInRam" PATH_DISK="/var/log" +# Should log2ram consider journald and do a log rotate before copying the log files back? Please note that for this +# rsync is mandatory. Also make sure that you have configured SystemMaxUse in journald.conf, because the size of this +# ram folder is will probably not be as large as journald will use by default +JOURNALD_AWARE=true + # **************** Zram backing conf ************************************************* # ZL2R Zram Log 2 Ram enables a zram drive when ZL2R=true ZL2R=false is mem only tmpfs @@ -35,4 +41,4 @@ COMP_ALG=lz4 # LOG_DISK_SIZE is expected compression ratio of alg chosen multiplied by log SIZE # lzo/lz4=2.1:1 compression ratio zlib=2.7:1 zstandard=2.9:1 # Really a guestimate of a bit bigger than compression ratio whilst minimising 0.1% mem usage of disk size -LOG_DISK_SIZE=100M +LOG_DISK_SIZE=256M diff --git a/uninstall.sh b/uninstall.sh index ab89109..2775e87 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env sh +#!/usr/bin/env bash if dpkg -l log2ram 2>/dev/null; then echo "Please run : apt remove log2ram"