mirror of
https://github.com/azlux/log2ram.git
synced 2023-10-10 13:37:24 +02:00
Merge pull request #94 from MegaV0lt/master
Include detection of OpenRC (Gentoo Linux) and automatically (un)install files
This commit is contained in:
commit
851dd0219e
@ -1,5 +1,5 @@
|
|||||||
# Log2Ram
|
# 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!
|
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)
|
**REBOOT** before installing anything else (for example apache2)
|
||||||
## Upgrade
|
## 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
|
## Customize
|
||||||
#### variables :
|
#### variables :
|
||||||
|
48
install.sh
48
install.sh
@ -1,18 +1,46 @@
|
|||||||
#!/usr/bin/env sh
|
#!/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
|
# log2ram
|
||||||
mkdir -p /usr/local/bin/
|
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 755 log2ram /usr/local/bin/log2ram
|
||||||
install -m 644 log2ram.conf /etc/log2ram.conf
|
install -m 644 log2ram.conf /etc/log2ram.conf
|
||||||
install -m 644 uninstall.sh /usr/local/bin/uninstall-log2ram.sh
|
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
|
# 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
|
install -m 644 log2ram.logrotate /etc/logrotate.d/log2ram
|
||||||
|
|
||||||
# Remove a previous log2ram version
|
# Remove a previous log2ram version
|
||||||
@ -21,5 +49,11 @@ rm -rf /var/log.hdd
|
|||||||
# Make sure we start clean
|
# Make sure we start clean
|
||||||
rm -rf /var/hdd.log
|
rm -rf /var/hdd.log
|
||||||
|
|
||||||
echo "##### Reboot to activate log2ram #####"
|
# Check if var SIZE is sufficient and show a warning when too small
|
||||||
echo "##### edit /etc/log2ram.conf to configure options ####"
|
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
42
log2ram
@ -2,45 +2,47 @@
|
|||||||
|
|
||||||
. /etc/log2ram.conf
|
. /etc/log2ram.conf
|
||||||
|
|
||||||
HDD_LOG=/var/hdd.log
|
HDD_LOG='/var/hdd.log'
|
||||||
RAM_LOG=/var/log
|
RAM_LOG='/var/log'
|
||||||
|
|
||||||
LOG_NAME="log2ram.log"
|
LOG_NAME='log2ram.log'
|
||||||
LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}"
|
LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}"
|
||||||
LOG_OUTPUT="tee -a $LOG2RAM_LOG"
|
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 () {
|
isSafe () {
|
||||||
[ -d $HDD_LOG/ ] || echo "ERROR: $HDD_LOG/ doesn't exist! Can't sync."
|
[ ! -d "$HDD_LOG/" ] && { echo "ERROR: $HDD_LOG/ doesn't exist! Can't sync." ; exit 1 ;}
|
||||||
[ -d $HDD_LOG/ ] || exit 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
syncToDisk () {
|
syncToDisk () {
|
||||||
isSafe
|
isSafe
|
||||||
|
|
||||||
|
echo "==> $NOW - Syncing to disk" >> $LOG2RAM_LOG
|
||||||
if [ "$USE_RSYNC" = true ]; then
|
if [ "$USE_RSYNC" = true ]; then
|
||||||
rsync -aXv --inplace --no-whole-file --delete-after $RAM_LOG/ $HDD_LOG/ 2>&1 | $LOG_OUTPUT
|
rsync -aXv --inplace --no-whole-file --delete-after $RAM_LOG/ $HDD_LOG/ 2>&1 | $LOG_OUTPUT
|
||||||
else
|
else
|
||||||
cp -rfup $RAM_LOG/ -T $HDD_LOG/ 2>&1 | $LOG_OUTPUT
|
cp -rfupv $RAM_LOG/ -T $HDD_LOG/ 2>&1 | $LOG_OUTPUT
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
syncFromDisk () {
|
syncFromDisk () {
|
||||||
isSafe
|
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."
|
echo "ERROR: RAM disk too small. Can't sync."
|
||||||
umount -l $RAM_LOG/
|
umount -l $RAM_LOG/
|
||||||
umount -l $HDD_LOG/
|
umount -l $HDD_LOG/
|
||||||
if [ "$MAIL" = true ]; then
|
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
|
fi
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo "==> $NOW - Syncing from disk" >> $LOG2RAM_LOG
|
||||||
if [ "$USE_RSYNC" = true ]; then
|
if [ "$USE_RSYNC" = true ]; then
|
||||||
rsync -aXv --inplace --no-whole-file --delete-after $HDD_LOG/ $RAM_LOG/ 2>&1 | $LOG_OUTPUT
|
rsync -aXv --inplace --no-whole-file --delete-after $HDD_LOG/ $RAM_LOG/ 2>&1 | $LOG_OUTPUT
|
||||||
else
|
else
|
||||||
cp -rfup $HDD_LOG/ -T $RAM_LOG/ 2>&1 | $LOG_OUTPUT
|
cp -rfupv $HDD_LOG/ -T $RAM_LOG/ 2>&1 | $LOG_OUTPUT
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,29 +54,29 @@ wait_for () {
|
|||||||
|
|
||||||
createZramLogDrive () {
|
createZramLogDrive () {
|
||||||
# Check Zram Class created
|
# Check Zram Class created
|
||||||
if [ ! -d "/sys/class/zram-control" ]; then
|
if [ ! -d '/sys/class/zram-control' ]; then
|
||||||
modprobe zram
|
modprobe zram
|
||||||
RAM_DEV='0'
|
RAM_DEV=0
|
||||||
else
|
else
|
||||||
RAM_DEV=$(cat /sys/class/zram-control/hot_add)
|
RAM_DEV="$(cat /sys/class/zram-control/hot_add)"
|
||||||
fi
|
fi
|
||||||
echo ${COMP_ALG} > /sys/block/zram${RAM_DEV}/comp_algorithm
|
echo "$COMP_ALG" > "/sys/block/zram${RAM_DEV}/comp_algorithm"
|
||||||
echo ${LOG_DISK_SIZE} > /sys/block/zram${RAM_DEV}/disksize
|
echo "$LOG_DISK_SIZE" > "/sys/block/zram${RAM_DEV}/disksize"
|
||||||
echo ${SIZE} > /sys/block/zram${RAM_DEV}/mem_limit
|
echo "$SIZE" > "/sys/block/zram${RAM_DEV}/mem_limit"
|
||||||
mke2fs -t ext4 /dev/zram${RAM_DEV}
|
mke2fs -t ext4 "/dev/zram${RAM_DEV}"
|
||||||
}
|
}
|
||||||
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
start)
|
start)
|
||||||
[ -d $HDD_LOG/ ] || mkdir $HDD_LOG/
|
[ ! -d "$HDD_LOG/" ] && mkdir $HDD_LOG/
|
||||||
mount --bind $RAM_LOG/ $HDD_LOG/
|
mount --bind $RAM_LOG/ $HDD_LOG/
|
||||||
mount --make-private $HDD_LOG/
|
mount --make-private $HDD_LOG/
|
||||||
wait_for $HDD_LOG
|
wait_for $HDD_LOG
|
||||||
if [ "$ZL2R" = true ]; then
|
if [ "$ZL2R" = true ]; then
|
||||||
createZramLogDrive
|
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
|
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
|
fi
|
||||||
wait_for $RAM_LOG
|
wait_for $RAM_LOG
|
||||||
syncFromDisk
|
syncFromDisk
|
||||||
@ -95,7 +97,7 @@ case "$1" in
|
|||||||
;;
|
;;
|
||||||
|
|
||||||
*)
|
*)
|
||||||
echo "Usage: log2ram {start|stop|write}" >&2
|
echo 'Usage: log2ram {start|stop|write}' >&2
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
@ -5,7 +5,8 @@
|
|||||||
# If it's not enough, log2ram will not be able to use ram. Check you /var/log size folder.
|
# 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.
|
# 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.
|
# 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".
|
# 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.
|
# 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
32
log2ram.initd
Normal 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
4
log2ram.openrc_cron
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
|
sleep 5 # Delay to avoid conflict with logrotate
|
||||||
|
rc-service log2ram write
|
42
uninstall.sh
42
uninstall.sh
@ -1,21 +1,33 @@
|
|||||||
#!/usr/bin/env sh
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
if [ "$(id -u)" -eq 0 ]
|
[ "$(id -u)" -eq 0 ] || { echo "You need to be ROOT (sudo can be used)"; exit 1; }
|
||||||
then
|
|
||||||
|
# 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
|
service log2ram stop
|
||||||
systemctl disable log2ram
|
systemctl disable log2ram
|
||||||
rm /etc/systemd/system/log2ram.service
|
rm /etc/systemd/system/log2ram.service
|
||||||
rm /usr/local/bin/log2ram
|
elif [ "$INIT" = 'openrc' ] ; then
|
||||||
rm /etc/log2ram.conf
|
rc-service log2ram stop
|
||||||
rm /etc/cron.daily/log2ram
|
rc-update del log2ram boot
|
||||||
rm /etc/logrotate.d/log2ram
|
rm /etc/init.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)"
|
|
||||||
fi
|
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 #####"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user