From bbb30234179a70266288e4e28ef3b64a22091b63 Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Mon, 23 Sep 2019 14:35:17 +0200 Subject: [PATCH 01/27] Create log2ram.initd Init-File for Gentoo/OpenRC Init-System --- log2ram.initd | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 log2ram.initd diff --git a/log2ram.initd b/log2ram.initd new file mode 100644 index 0000000..b3c83d9 --- /dev/null +++ b/log2ram.initd @@ -0,0 +1,31 @@ +#!/sbin/openrc-run + +# Init-Skript for log2ram +# This skript is designed to work on Gentoo Linux with OpenRC + +description="Store logfiles in RAM to mimimize writes to disk." + +. /etc/log2ram.conf # Include configuration of log2ram + +depend() { + need localmount + before logger +} + +start() { + ebegin "Starting Log2Ram" + /usr/local/bin/log2ram start + eend $? +} + +stop() { + ebegin "Stopping Log2Ram" + /usr/local/bin/log2ram stop + eend $? +} + +reload() { + ebegin "Syncing logs to disk" + /usr/local/bin/log2ram write + eend $? +} From 4adeeedb7e09dc9bc33932bce9fa874aa6b9fba3 Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Mon, 23 Sep 2019 15:02:23 +0200 Subject: [PATCH 02/27] Create log2ram.openrc_cron Cron-file for Gentoo/OpenRC --- log2ram.openrc_cron | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 log2ram.openrc_cron diff --git a/log2ram.openrc_cron b/log2ram.openrc_cron new file mode 100644 index 0000000..8462932 --- /dev/null +++ b/log2ram.openrc_cron @@ -0,0 +1,3 @@ +#!/usr/bin/env sh + +rc-service log2ram restart From 1df730ac7af1dcd5e37bc11720927c2df85d07a7 Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Mon, 23 Sep 2019 15:03:09 +0200 Subject: [PATCH 03/27] Update log2ram.initd --- log2ram.initd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/log2ram.initd b/log2ram.initd index b3c83d9..4472952 100644 --- a/log2ram.initd +++ b/log2ram.initd @@ -24,7 +24,7 @@ stop() { eend $? } -reload() { +restart() { ebegin "Syncing logs to disk" /usr/local/bin/log2ram write eend $? From 12662bc6ee23d014bc096d67a7976a4bf0f72fac Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Mon, 23 Sep 2019 15:52:35 +0200 Subject: [PATCH 04/27] Update install.sh try to detect init-system and install accordingly --- install.sh | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/install.sh b/install.sh index 065ee0e..8c0e596 100755 --- a/install.sh +++ b/install.sh @@ -1,18 +1,43 @@ #!/usr/bin/env sh -systemctl -q is-active log2ram && { echo "ERROR: log2ram service is still running. Please run \"sudo service log2ram stop\" to stop it."; exit 1; } [ "$(id -u)" -eq 0 ] || { echo "You need to be ROOT (sudo can be used)"; exit 1; } +# See if we can find out the init-system +echo "Try to detect init..." +if [ "$(systemctl --version)" != '' ] ; then + INIT='systemd' +elif [ "$(rc-service --version)" != '' ] ; then + INIT='openrc' +fi + +case "$INIT" in + systemd) + systemctl -q is-active log2ram && { echo "ERROR: log2ram service is still running. Please run \"sudo service log2ram stop\" to stop it."; exit 1; } ;; + openrc) + rc-service log2ram status && { echo "ERROR: log2ram service is still running. Please run \"sudo rc-service log2ram stop\" to stop it."; exit 1; } ;; + *) echo 'ERROR: could not detect init-system' ; exit 1 + ;; +esac + # log2ram mkdir -p /usr/local/bin/ -install -m 644 log2ram.service /etc/systemd/system/log2ram.service install -m 755 log2ram /usr/local/bin/log2ram install -m 644 log2ram.conf /etc/log2ram.conf install -m 644 uninstall.sh /usr/local/bin/uninstall-log2ram.sh -systemctl enable log2ram +if [ "$INIT" = 'systemd' ] ; then + install -m 644 log2ram.service /etc/systemd/system/log2ram.service + systemctl enable log2ram +elif [ "$INIT" = 'openrc' ] ; then + install -m 755 log2ram.initd /etc/init.d/log2ram + rc-update add log2ram boot +fi # cron -install -m 755 log2ram.cron /etc/cron.daily/log2ram +if [ "$INIT" = 'systemd' ] ; then + install -m 755 log2ram.cron /etc/cron.daily/log2ram +elif [ "$INIT" = 'openrc' ] ; then + install -m 755 log2ram.openrc_cron /etc/cron.daily/log2ram +fi install -m 644 log2ram.logrotate /etc/logrotate.d/log2ram # Remove a previous log2ram version From e26854eb7419ef5c44a7059683979a319c2740b5 Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Mon, 23 Sep 2019 15:55:51 +0200 Subject: [PATCH 05/27] Update log2ram.initd config file is not needed here --- log2ram.initd | 2 -- 1 file changed, 2 deletions(-) diff --git a/log2ram.initd b/log2ram.initd index 4472952..a73742c 100644 --- a/log2ram.initd +++ b/log2ram.initd @@ -5,8 +5,6 @@ description="Store logfiles in RAM to mimimize writes to disk." -. /etc/log2ram.conf # Include configuration of log2ram - depend() { need localmount before logger From 5abe5bd301d715bbef31695cb630850b078f6792 Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Mon, 23 Sep 2019 16:02:56 +0200 Subject: [PATCH 06/27] Update uninstall.sh Try to detect init-system and remove files accordingly --- uninstall.sh | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/uninstall.sh b/uninstall.sh index 38bc2a0..865eb12 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -1,21 +1,33 @@ #!/usr/bin/env sh -if [ "$(id -u)" -eq 0 ] -then +[ "$(id -u)" -eq 0 ] || { echo "You need to be ROOT (sudo can be used)"; exit 1; } + +# See if we can find out the init-system +echo "Try to detect init..." +if [ "$(systemctl --version)" != '' ] ; then + INIT='systemd' +elif [ "$(rc-service --version)" != '' ] ; then + INIT='openrc' +fi + +if [ "$INIT" = 'systemd' ] ; then service log2ram stop systemctl disable log2ram rm /etc/systemd/system/log2ram.service - rm /usr/local/bin/log2ram - rm /etc/log2ram.conf - rm /etc/cron.daily/log2ram - rm /etc/logrotate.d/log2ram - - if [ -d /var/hdd.log ]; then - rm -r /var/hdd.log - fi - echo "Log2Ram is uninstalled, removing the uninstaller in progress" - rm /usr/local/bin/uninstall-log2ram.sh - echo "##### Reboot isn't needed #####" -else - echo "You need to be ROOT (sudo can be used)" +elif [ "$INIT" = 'openrc' ] ; then + rc-service log2ram stop + rc-update del log2ram boot + rm /etc/init.d/log2ram fi + +rm /usr/local/bin/log2ram +rm /etc/log2ram.conf +rm /etc/cron.daily/log2ram +rm /etc/logrotate.d/log2ram + +if [ -d /var/hdd.log ]; then + rm -r /var/hdd.log +fi +echo "Log2Ram is uninstalled, removing the uninstaller in progress" +rm /usr/local/bin/uninstall-log2ram.sh +echo "##### Reboot isn't needed #####" From 88d027bfa92b3686beddd6edb28a74e4f796eed4 Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Mon, 23 Sep 2019 16:42:17 +0200 Subject: [PATCH 07/27] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index deed830..133e6e9 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Log2Ram -Like ramlog for systemd (on debian 8 jessie for example). +Like ramlog for systemd (on debian 8 jessie for example). adapted to detect OpenRC when running on Gentoo-Linux (Hopefully) Usefull for **RaspberryPi** for not writing on the SD card all the time. You need it because your SD card doesn't want to suffer anymore! From fdb0cfee4e60cf0f626425e5bc5eb8cb87cbe9c2 Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Mon, 23 Sep 2019 16:43:06 +0200 Subject: [PATCH 08/27] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 133e6e9..0e61af3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Log2Ram -Like ramlog for systemd (on debian 8 jessie for example). adapted to detect OpenRC when running on Gentoo-Linux (Hopefully) +Like ramlog for systemd (on debian 8 jessie for example). Adapted to detect OpenRC init-system when running on Gentoo-Linux (Hopefully). For now only Systemd and OpenRC can be handled. Usefull for **RaspberryPi** for not writing on the SD card all the time. You need it because your SD card doesn't want to suffer anymore! From 3740b50ec72d3d6a9c7d6290ad5399e888e656dc Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Tue, 24 Sep 2019 12:59:27 +0200 Subject: [PATCH 09/27] Update install.sh Suppress "file not found" error when testing for init --- install.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/install.sh b/install.sh index 8c0e596..c0cad46 100755 --- a/install.sh +++ b/install.sh @@ -3,10 +3,10 @@ [ "$(id -u)" -eq 0 ] || { echo "You need to be ROOT (sudo can be used)"; exit 1; } # See if we can find out the init-system -echo "Try to detect init..." -if [ "$(systemctl --version)" != '' ] ; then +echo "Try to detect init and running log2ram service..." +if [ "$(systemctl --version 2> /dev/null)" != '' ] ; then INIT='systemd' -elif [ "$(rc-service --version)" != '' ] ; then +elif [ "$(rc-service --version 2> /dev/null)" != '' ] ; then INIT='openrc' fi From 9c42a06d257adc830758de087a17b40952d2868b Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Tue, 24 Sep 2019 13:40:26 +0200 Subject: [PATCH 10/27] Update install.sh Suppress rc-service status message --- install.sh | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/install.sh b/install.sh index c0cad46..64ace0c 100755 --- a/install.sh +++ b/install.sh @@ -1,9 +1,9 @@ #!/usr/bin/env sh -[ "$(id -u)" -eq 0 ] || { echo "You need to be ROOT (sudo can be used)"; exit 1; } +[ "$(id -u)" -eq 0 ] || { echo 'You need to be ROOT (sudo can be used)' ; exit 1 ; } # See if we can find out the init-system -echo "Try to detect init and running log2ram service..." +echo 'Try to detect init and running log2ram service...' if [ "$(systemctl --version 2> /dev/null)" != '' ] ; then INIT='systemd' elif [ "$(rc-service --version 2> /dev/null)" != '' ] ; then @@ -12,13 +12,14 @@ fi case "$INIT" in systemd) - systemctl -q is-active log2ram && { echo "ERROR: log2ram service is still running. Please run \"sudo service log2ram stop\" to stop it."; exit 1; } ;; + systemctl -q is-active log2ram && { echo 'ERROR: log2ram service is still running. Please run "sudo service log2ram stop" to stop it.' ; exit 1 ; } ;; openrc) - rc-service log2ram status && { echo "ERROR: log2ram service is still running. Please run \"sudo rc-service log2ram stop\" to stop it."; exit 1; } ;; + rc-service log2ram status 2>1 >/dev/null && { echo 'ERROR: log2ram service is still running. Please run "sudo rc-service log2ram stop" to stop it.' ; exit 1 ; } ;; *) echo 'ERROR: could not detect init-system' ; exit 1 ;; esac - + +echo "installing files for ${INIT}..." # log2ram mkdir -p /usr/local/bin/ install -m 755 log2ram /usr/local/bin/log2ram @@ -46,5 +47,5 @@ rm -rf /var/log.hdd # Make sure we start clean rm -rf /var/hdd.log -echo "##### Reboot to activate log2ram #####" -echo "##### edit /etc/log2ram.conf to configure options ####" +echo '##### Reboot to activate log2ram! #####' +echo '##### Edit /etc/log2ram.conf to configure options #####' From e563d2c5ed94a5243678aef011cdd263c0be784a Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Tue, 24 Sep 2019 13:43:54 +0200 Subject: [PATCH 11/27] Update install.sh Indicate that we install log2ram on the detected init-system --- install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.sh b/install.sh index 64ace0c..871bd0e 100755 --- a/install.sh +++ b/install.sh @@ -19,7 +19,7 @@ case "$INIT" in ;; esac -echo "installing files for ${INIT}..." +echo "Installing log2ram for $INIT init-system" # log2ram mkdir -p /usr/local/bin/ install -m 755 log2ram /usr/local/bin/log2ram From f547ca5c1580a02f13f4c759b345745fda899463 Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Tue, 24 Sep 2019 13:45:36 +0200 Subject: [PATCH 12/27] Update log2ram.initd Typo --- log2ram.initd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/log2ram.initd b/log2ram.initd index a73742c..0df69b8 100644 --- a/log2ram.initd +++ b/log2ram.initd @@ -3,7 +3,7 @@ # Init-Skript for log2ram # This skript is designed to work on Gentoo Linux with OpenRC -description="Store logfiles in RAM to mimimize writes to disk." +description="Store logfiles in RAM to minimize writes to disk." depend() { need localmount From ea323c852d677f8077916305346267280922658b Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Thu, 26 Sep 2019 09:53:27 +0200 Subject: [PATCH 13/27] Update log2ram.initd Custom function write() to avoid restart of log2ram via cron --- log2ram.initd | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/log2ram.initd b/log2ram.initd index 0df69b8..8cf4aa6 100644 --- a/log2ram.initd +++ b/log2ram.initd @@ -5,6 +5,9 @@ description="Store logfiles in RAM to minimize writes to disk." +# Command is available when the service is started +extra_started_commands="write" + depend() { need localmount before logger @@ -22,7 +25,7 @@ stop() { eend $? } -restart() { +write() { ebegin "Syncing logs to disk" /usr/local/bin/log2ram write eend $? From c062acb15ad380c91e189345a31500c7a5275d32 Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Thu, 26 Sep 2019 09:54:16 +0200 Subject: [PATCH 14/27] Update log2ram.openrc_cron Use custom function write() in intscript --- log2ram.openrc_cron | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/log2ram.openrc_cron b/log2ram.openrc_cron index 8462932..712d085 100644 --- a/log2ram.openrc_cron +++ b/log2ram.openrc_cron @@ -1,3 +1,3 @@ #!/usr/bin/env sh -rc-service log2ram restart +rc-service log2ram write From 366afe36fb12409141afe950ac363f5905f350c3 Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Thu, 26 Sep 2019 10:04:18 +0200 Subject: [PATCH 15/27] Update log2ram Add a log statement with date and performed action; Eg. "2019-09-26 09:58:08 - Syncing to disk" --- log2ram | 3 +++ 1 file changed, 3 insertions(+) diff --git a/log2ram b/log2ram index 2f0aab3..44a3734 100755 --- a/log2ram +++ b/log2ram @@ -8,6 +8,7 @@ RAM_LOG=/var/log LOG_NAME="log2ram.log" LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}" LOG_OUTPUT="tee -a $LOG2RAM_LOG" +printf -v NOW '%(%F %H:%M:%S)T' -1 # Actual Date and Time isSafe () { [ -d $HDD_LOG/ ] || echo "ERROR: $HDD_LOG/ doesn't exist! Can't sync." @@ -17,6 +18,7 @@ isSafe () { syncToDisk () { isSafe + echo "$NOW - Syncing to disk" | $LOG_OUTPUT if [ "$USE_RSYNC" = true ]; then rsync -aXv --inplace --no-whole-file --delete-after $RAM_LOG/ $HDD_LOG/ 2>&1 | $LOG_OUTPUT else @@ -37,6 +39,7 @@ syncFromDisk () { exit 1 fi + echo "$NOW - Syncing from disk" | $LOG_OUTPUT if [ "$USE_RSYNC" = true ]; then rsync -aXv --inplace --no-whole-file --delete-after $HDD_LOG/ $RAM_LOG/ 2>&1 | $LOG_OUTPUT else From aa9beee67a6e97c8e4450203e245baf8c1cd4361 Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Thu, 26 Sep 2019 10:12:35 +0200 Subject: [PATCH 16/27] Update log2ram Better visibility of log message and only log to log file --- log2ram | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/log2ram b/log2ram index 44a3734..3fb444e 100755 --- a/log2ram +++ b/log2ram @@ -8,7 +8,7 @@ RAM_LOG=/var/log LOG_NAME="log2ram.log" LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}" LOG_OUTPUT="tee -a $LOG2RAM_LOG" -printf -v NOW '%(%F %H:%M:%S)T' -1 # Actual Date and Time +printf -v NOW '%(%F %H:%M:%S)T' -1 # Actual date and time isSafe () { [ -d $HDD_LOG/ ] || echo "ERROR: $HDD_LOG/ doesn't exist! Can't sync." @@ -18,7 +18,7 @@ isSafe () { syncToDisk () { isSafe - echo "$NOW - Syncing to disk" | $LOG_OUTPUT + echo "==> $NOW - Syncing to disk" >> $LOG2RAM_LOG if [ "$USE_RSYNC" = true ]; then rsync -aXv --inplace --no-whole-file --delete-after $RAM_LOG/ $HDD_LOG/ 2>&1 | $LOG_OUTPUT else @@ -39,7 +39,7 @@ syncFromDisk () { exit 1 fi - echo "$NOW - Syncing from disk" | $LOG_OUTPUT + echo "==> $NOW - Syncing from disk" >> $LOG2RAM_LOG if [ "$USE_RSYNC" = true ]; then rsync -aXv --inplace --no-whole-file --delete-after $HDD_LOG/ $RAM_LOG/ 2>&1 | $LOG_OUTPUT else From e993fa880e0352688505ba9230cf9b21cbcdb11c Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Thu, 26 Sep 2019 14:59:45 +0200 Subject: [PATCH 17/27] Update log2ram.initd Replaced dependency "neet localmount" by "after localmount" --- log2ram.initd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/log2ram.initd b/log2ram.initd index 8cf4aa6..0aba816 100644 --- a/log2ram.initd +++ b/log2ram.initd @@ -9,7 +9,7 @@ description="Store logfiles in RAM to minimize writes to disk." extra_started_commands="write" depend() { - need localmount + after localmount before logger } From 6294b7312631f6cb69de6feb8672e0e9dc977498 Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Fri, 27 Sep 2019 08:40:38 +0200 Subject: [PATCH 18/27] Update log2ram Use date as fallback if printf -v fails. reason is, that printf -v is not defined in POSIX --- log2ram | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/log2ram b/log2ram index 3fb444e..6c40fc9 100755 --- a/log2ram +++ b/log2ram @@ -8,7 +8,7 @@ RAM_LOG=/var/log LOG_NAME="log2ram.log" LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}" LOG_OUTPUT="tee -a $LOG2RAM_LOG" -printf -v NOW '%(%F %H:%M:%S)T' -1 # Actual date and time +printf -v NOW '%(%F %H:%M:%S)T' -1 || NOW=$(date '+%F %H:%M:%S') # Actual date and time (date as fallback) isSafe () { [ -d $HDD_LOG/ ] || echo "ERROR: $HDD_LOG/ doesn't exist! Can't sync." From 1ada2916d73215f02eaea504e0403e9b226ef2ed Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Fri, 27 Sep 2019 08:56:11 +0200 Subject: [PATCH 19/27] Update log2ram Double quote to prevent globbing for variables. Replace '! -z' by '-n' for better readability. Function isSafe: Only check once for existing dir. --- log2ram | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/log2ram b/log2ram index 6c40fc9..4329394 100755 --- a/log2ram +++ b/log2ram @@ -5,14 +5,13 @@ HDD_LOG=/var/hdd.log RAM_LOG=/var/log -LOG_NAME="log2ram.log" +LOG_NAME='log2ram.log' LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}" LOG_OUTPUT="tee -a $LOG2RAM_LOG" -printf -v NOW '%(%F %H:%M:%S)T' -1 || NOW=$(date '+%F %H:%M:%S') # Actual date and time (date as fallback) +printf -v NOW '%(%F %H:%M:%S)T' -1 || NOW="$(date '+%F %H:%M:%S')" # Actual date and time (date as fallback) isSafe () { - [ -d $HDD_LOG/ ] || echo "ERROR: $HDD_LOG/ doesn't exist! Can't sync." - [ -d $HDD_LOG/ ] || exit 1 + [ ! -d "$HDD_LOG/" ] && { echo "ERROR: $HDD_LOG/ doesn't exist! Can't sync." ; exit 1 ;} } syncToDisk () { @@ -29,12 +28,12 @@ syncToDisk () { syncFromDisk () { isSafe - if [ ! -z "$(du -sh -t "$SIZE" $HDD_LOG/ | cut -f1)" ]; then - echo "ERROR: RAM disk too small. Can't sync." + if [ -n "$(du -sh -t "$SIZE" $HDD_LOG/ | cut -f1)" ]; then + echo 'ERROR: RAM disk too small. Can\'t sync.' umount -l $RAM_LOG/ umount -l $HDD_LOG/ if [ "$MAIL" = true ]; then - echo "LOG2RAM : No place on RAM anymore, fallback on the disk" | mail -s 'Log2Ram Error' root; + echo 'LOG2RAM : No place on RAM anymore, fallback on the disk' | mail -s 'Log2Ram Error' root; fi exit 1 fi @@ -55,29 +54,29 @@ wait_for () { createZramLogDrive () { # Check Zram Class created - if [ ! -d "/sys/class/zram-control" ]; then + if [ ! -d '/sys/class/zram-control' ]; then modprobe zram RAM_DEV='0' else RAM_DEV=$(cat /sys/class/zram-control/hot_add) fi - echo ${COMP_ALG} > /sys/block/zram${RAM_DEV}/comp_algorithm - echo ${LOG_DISK_SIZE} > /sys/block/zram${RAM_DEV}/disksize - echo ${SIZE} > /sys/block/zram${RAM_DEV}/mem_limit - mke2fs -t ext4 /dev/zram${RAM_DEV} + echo "${COMP_ALG}" > "/sys/block/zram${RAM_DEV}/comp_algorithm" + echo "${LOG_DISK_SIZE}" > "/sys/block/zram${RAM_DEV}/disksize" + echo "${SIZE}" > "/sys/block/zram${RAM_DEV}/mem_limit" + mke2fs -t ext4 "/dev/zram${RAM_DEV}" } case "$1" in start) - [ -d $HDD_LOG/ ] || mkdir $HDD_LOG/ + [ ! -d "$HDD_LOG/" ] && mkdir $HDD_LOG/ mount --bind $RAM_LOG/ $HDD_LOG/ mount --make-private $HDD_LOG/ wait_for $HDD_LOG if [ "$ZL2R" = true ]; then createZramLogDrive - mount -t ext4 -o nosuid,noexec,nodev,user=log2ram /dev/zram${RAM_DEV} ${RAM_LOG}/ + mount -t ext4 -o nosuid,noexec,nodev,user=log2ram "/dev/zram${RAM_DEV}" ${RAM_LOG}/ else - mount -t tmpfs -o nosuid,noexec,nodev,mode=0755,size=${SIZE} log2ram $RAM_LOG/ + mount -t tmpfs -o "nosuid,noexec,nodev,mode=0755,size=${SIZE}" log2ram $RAM_LOG/ fi wait_for $RAM_LOG syncFromDisk From fe281aaaf9f547fe4b2869a8216e512693237106 Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Fri, 27 Sep 2019 09:23:44 +0200 Subject: [PATCH 20/27] Update log2ram Fix quoting --- log2ram | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/log2ram b/log2ram index 4329394..c81259b 100755 --- a/log2ram +++ b/log2ram @@ -29,7 +29,7 @@ syncFromDisk () { isSafe if [ -n "$(du -sh -t "$SIZE" $HDD_LOG/ | cut -f1)" ]; then - echo 'ERROR: RAM disk too small. Can\'t sync.' + echo "ERROR: RAM disk too small. Can't sync." umount -l $RAM_LOG/ umount -l $HDD_LOG/ if [ "$MAIL" = true ]; then From bd960d06dbc1de146fbe0387b77c98e941778e4f Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Fri, 27 Sep 2019 18:51:21 +0200 Subject: [PATCH 21/27] Update log2ram.conf Set a bigger default for ramlog size and add a note that tmpfs is not allocating unused space --- log2ram.conf | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/log2ram.conf b/log2ram.conf index d5fbd2c..fda12b7 100644 --- a/log2ram.conf +++ b/log2ram.conf @@ -5,7 +5,8 @@ # 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 +# Note that tmpfs doesn't reserve this memory, but allocates only the needed memory. +SIZE=100M # This variable can be set to true if you prefer "rsync" rather than "cp". # I use the command cp -u and rsync -X, so I don't copy the all folder every time for optimization. From 0b40db7d30e506162bd39760ee877748cc2cf0f7 Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Sat, 28 Sep 2019 15:12:28 +0200 Subject: [PATCH 22/27] Update log2ram Add some quotes to variables and remove { where not needed --- log2ram | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/log2ram b/log2ram index c81259b..b53eb0c 100755 --- a/log2ram +++ b/log2ram @@ -2,8 +2,8 @@ . /etc/log2ram.conf -HDD_LOG=/var/hdd.log -RAM_LOG=/var/log +HDD_LOG='/var/hdd.log' +RAM_LOG='/var/log' LOG_NAME='log2ram.log' LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}" @@ -56,13 +56,13 @@ createZramLogDrive () { # Check Zram Class created if [ ! -d '/sys/class/zram-control' ]; then modprobe zram - RAM_DEV='0' + RAM_DEV=0 else - RAM_DEV=$(cat /sys/class/zram-control/hot_add) + RAM_DEV="$(cat /sys/class/zram-control/hot_add)" fi - echo "${COMP_ALG}" > "/sys/block/zram${RAM_DEV}/comp_algorithm" - echo "${LOG_DISK_SIZE}" > "/sys/block/zram${RAM_DEV}/disksize" - echo "${SIZE}" > "/sys/block/zram${RAM_DEV}/mem_limit" + echo "$COMP_ALG" > "/sys/block/zram${RAM_DEV}/comp_algorithm" + echo "$LOG_DISK_SIZE" > "/sys/block/zram${RAM_DEV}/disksize" + echo "$SIZE" > "/sys/block/zram${RAM_DEV}/mem_limit" mke2fs -t ext4 "/dev/zram${RAM_DEV}" } @@ -97,7 +97,7 @@ case "$1" in ;; *) - echo "Usage: log2ram {start|stop|write}" >&2 + echo 'Usage: log2ram {start|stop|write}' >&2 exit 1 ;; esac From e879db921e66b9e1db4d84cbbb3aef5b04fdc3ed Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Sat, 28 Sep 2019 17:40:08 +0200 Subject: [PATCH 23/27] Update README.md Note how to stop log2ram with OpenRC --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0e61af3..abdee0e 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ _____ **REBOOT** before installing anything else (for example apache2) ## Upgrade -You need to stop log2ram (`service log2ram stop`) and start the [install](#install). +You need to stop log2ram (`service log2ram stop` with systemd or `rc-service log2ram stop` with OpenRC) and start the [install](#install). ## Customize #### variables : From 8e1a0e441897b77a66a690a152580a42e337400a Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Sat, 28 Sep 2019 17:57:10 +0200 Subject: [PATCH 24/27] Update install.sh Fix redirection --- install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.sh b/install.sh index 871bd0e..7ea5fd4 100755 --- a/install.sh +++ b/install.sh @@ -14,7 +14,7 @@ case "$INIT" in systemd) systemctl -q is-active log2ram && { echo 'ERROR: log2ram service is still running. Please run "sudo service log2ram stop" to stop it.' ; exit 1 ; } ;; openrc) - rc-service log2ram status 2>1 >/dev/null && { echo 'ERROR: log2ram service is still running. Please run "sudo rc-service log2ram stop" to stop it.' ; exit 1 ; } ;; + rc-service log2ram status >/dev/null 2>&1 && { echo 'ERROR: log2ram service is still running. Please run "sudo rc-service log2ram stop" to stop it.' ; exit 1 ; } ;; *) echo 'ERROR: could not detect init-system' ; exit 1 ;; esac From 099899a5537a012bc237a7e4431eccedb646632c Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Sun, 29 Sep 2019 17:17:07 +0200 Subject: [PATCH 25/27] Update install.sh Add a check for the size of /var/log and show a warning it it is too small --- install.sh | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/install.sh b/install.sh index 7ea5fd4..3bff2c4 100755 --- a/install.sh +++ b/install.sh @@ -1,6 +1,8 @@ #!/usr/bin/env sh -[ "$(id -u)" -eq 0 ] || { echo 'You need to be ROOT (sudo can be used)' ; exit 1 ; } +[ "$(id -u)" -eq 0 ] || { echo 'You need to be ROOT (sudo can be used)' ; exit 1 ;} + +. /log2ram.conf # Include config to check if size is enought (See below) # See if we can find out the init-system echo 'Try to detect init and running log2ram service...' @@ -12,9 +14,9 @@ fi case "$INIT" in systemd) - systemctl -q is-active log2ram && { echo 'ERROR: log2ram service is still running. Please run "sudo service log2ram stop" to stop it.' ; exit 1 ; } ;; + systemctl -q is-active log2ram && { echo 'ERROR: log2ram service is still running. Please run "sudo service log2ram stop" to stop it.' ; exit 1 ;} ;; openrc) - rc-service log2ram status >/dev/null 2>&1 && { echo 'ERROR: log2ram service is still running. Please run "sudo rc-service log2ram stop" to stop it.' ; exit 1 ; } ;; + rc-service log2ram status >/dev/null 2>&1 && { echo 'ERROR: log2ram service is still running. Please run "sudo rc-service log2ram stop" to stop it.' ; exit 1 ;} ;; *) echo 'ERROR: could not detect init-system' ; exit 1 ;; esac @@ -47,5 +49,11 @@ rm -rf /var/log.hdd # Make sure we start clean rm -rf /var/hdd.log +# Check if var SIZE is sufficient and show a warning when too small +if [ -n "$(du -sh -t "$SIZE" /var/log | cut -f1)" ] ; then # /var/log should be ok on all systems + echo 'WARNING: Variable SIZE in /etc/log2ram.conf is too small to store the /var/log!' + echo 'Actual size of /var/log is:' ; du -sh /var/log +fi + echo '##### Reboot to activate log2ram! #####' echo '##### Edit /etc/log2ram.conf to configure options #####' From 956a48add1e335b2fb8269da7ea0b0797a06626c Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Tue, 1 Oct 2019 14:50:47 +0200 Subject: [PATCH 26/27] Update log2ram Add option -v to cp command to have something to view in the log --- log2ram | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/log2ram b/log2ram index b53eb0c..8e8f3a0 100755 --- a/log2ram +++ b/log2ram @@ -21,7 +21,7 @@ syncToDisk () { if [ "$USE_RSYNC" = true ]; then rsync -aXv --inplace --no-whole-file --delete-after $RAM_LOG/ $HDD_LOG/ 2>&1 | $LOG_OUTPUT else - cp -rfup $RAM_LOG/ -T $HDD_LOG/ 2>&1 | $LOG_OUTPUT + cp -rfupv $RAM_LOG/ -T $HDD_LOG/ 2>&1 | $LOG_OUTPUT fi } @@ -42,7 +42,7 @@ syncFromDisk () { if [ "$USE_RSYNC" = true ]; then rsync -aXv --inplace --no-whole-file --delete-after $HDD_LOG/ $RAM_LOG/ 2>&1 | $LOG_OUTPUT else - cp -rfup $HDD_LOG/ -T $RAM_LOG/ 2>&1 | $LOG_OUTPUT + cp -rfupv $HDD_LOG/ -T $RAM_LOG/ 2>&1 | $LOG_OUTPUT fi } From 19beafe6aeae4406285b99ce291bb6d4943a0cae Mon Sep 17 00:00:00 2001 From: MegaV0lt Date: Wed, 2 Oct 2019 12:39:33 +0200 Subject: [PATCH 27/27] Update log2ram.openrc_cron Avoid a conflict with logrotate --- log2ram.openrc_cron | 1 + 1 file changed, 1 insertion(+) diff --git a/log2ram.openrc_cron b/log2ram.openrc_cron index 712d085..565cd5d 100644 --- a/log2ram.openrc_cron +++ b/log2ram.openrc_cron @@ -1,3 +1,4 @@ #!/usr/bin/env sh +sleep 5 # Delay to avoid conflict with logrotate rc-service log2ram write