Merge pull request #94 from MegaV0lt/master

Include detection of OpenRC (Gentoo Linux) and automatically (un)install files
This commit is contained in:
azlux 2020-01-02 17:08:38 +01:00 committed by GitHub
commit 851dd0219e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 130 additions and 45 deletions

View File

@ -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 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!
@ -35,7 +35,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 :

View File

@ -1,18 +1,46 @@
#!/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; }
[ "$(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...'
if [ "$(systemctl --version 2> /dev/null)" != '' ] ; then
INIT='systemd'
elif [ "$(rc-service --version 2> /dev/null)" != '' ] ; 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 >/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
echo "Installing log2ram for $INIT init-system"
# 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
@ -21,5 +49,11 @@ 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 ####"
# 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 #####'

42
log2ram
View File

@ -2,45 +2,47 @@
. /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"
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)
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 () {
isSafe
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
cp -rfup $RAM_LOG/ -T $HDD_LOG/ 2>&1 | $LOG_OUTPUT
cp -rfupv $RAM_LOG/ -T $HDD_LOG/ 2>&1 | $LOG_OUTPUT
fi
}
syncFromDisk () {
isSafe
if [ ! -z "$(du -sh -t "$SIZE" $HDD_LOG/ | cut -f1)" ]; then
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
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
cp -rfup $HDD_LOG/ -T $RAM_LOG/ 2>&1 | $LOG_OUTPUT
cp -rfupv $HDD_LOG/ -T $RAM_LOG/ 2>&1 | $LOG_OUTPUT
fi
}
@ -52,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'
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
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
@ -95,7 +97,7 @@ case "$1" in
;;
*)
echo "Usage: log2ram {start|stop|write}" >&2
echo 'Usage: log2ram {start|stop|write}' >&2
exit 1
;;
esac

View File

@ -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.

32
log2ram.initd Normal file
View File

@ -0,0 +1,32 @@
#!/sbin/openrc-run
# Init-Skript for log2ram
# This skript is designed to work on Gentoo Linux with OpenRC
description="Store logfiles in RAM to minimize writes to disk."
# Command is available when the service is started
extra_started_commands="write"
depend() {
after localmount
before logger
}
start() {
ebegin "Starting Log2Ram"
/usr/local/bin/log2ram start
eend $?
}
stop() {
ebegin "Stopping Log2Ram"
/usr/local/bin/log2ram stop
eend $?
}
write() {
ebegin "Syncing logs to disk"
/usr/local/bin/log2ram write
eend $?
}

4
log2ram.openrc_cron Normal file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env sh
sleep 5 # Delay to avoid conflict with logrotate
rc-service log2ram write

View File

@ -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 #####"