From e812907364f9a40adceb66de03f733a74852dd7a Mon Sep 17 00:00:00 2001 From: StuartIanNaylor Date: Fri, 15 Mar 2019 17:32:03 +0000 Subject: [PATCH] convert to zram --- install.sh | 8 ++++---- log2zram | 51 +++++++++++++++++++++++---------------------------- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/install.sh b/install.sh index 82448ec..936e932 100755 --- a/install.sh +++ b/install.sh @@ -14,7 +14,7 @@ systemctl enable log2zram # cron install -m 755 log2zram.hourly /etc/cron.hourly/log2zram -install -m 644 log2zram.logrotate /etc/logrotate.d/logz2ram +install -m 644 log2zram.logrotate /etc/logrotate.d/log2zram # Make sure we start clean rm -rf /var/hdd.log @@ -32,9 +32,9 @@ rm -r /var/log/*.old mkdir -p /var/hdd.log mkdir -p /var/log/oldlog # Prob better to use xcopy here with a --exclude -rsync -a --exclude 'oldlog' /var/log/ /var/hdd.log/ -#cp -rfup /var/log/ -T /var/hdd.log/ +rsync -arzh --exclude 'oldlog' /var/log/ /var/hdd.log/ +mkdir -p /var/hdd.log/oldlog sed -i '/^include.*/i olddir /var/log/oldlog' /etc/logrotate.conf echo "##### Reboot to activate log2ram #####" -echo "##### edit /etc/log2ram.conf to configure options #####" +echo "##### edit /etc/log2zram.conf to configure options #####" diff --git a/log2zram b/log2zram index 6733fbc..a9d76ee 100755 --- a/log2zram +++ b/log2zram @@ -15,19 +15,21 @@ isSafe () { syncToDisk () { isSafe - rsync -a $RAM_LOG/ $HDD_LOG/ 2>&1 | tee $LOG_NAME + rsync -arzh $ZRAM_LOG/ $HDD_LOG/ 2>&1 | tee $LOG_NAME } syncFromDisk () { isSafe - if [ ! -z "$(du -sh -t "$SIZE" $HDD_LOG/ | cut -f1)" ]; then + getUsage + LOG_SIZE=$(du -sk $HDD_LOG/ | awk '{ print $1 }') + if [ "$BYTES_AVAIL" -lt "$LOG_SIZE" ];then echo "ERROR: RAM disk too small. Can't sync." >> $LOG_NAME - umount -l $RAM_LOG/ + umount -l $ZRAM_LOG/ umount -l $HDD_LOG/ exit 1 fi - rsync -a --exclude 'oldlog' $HDD_LOG/ $RAM_LOG/ 2>&1 | tee $LOG_NAME + rsync -arzh --exclude 'oldlog' $HDD_LOG/ $ZRAM_LOG/ 2>&1 | tee $LOG_NAME } wait_for () { @@ -40,37 +42,30 @@ createZramLogDrive () { # Check Zram Class created if [ ! -d "/sys/class/zram-control" ]; then modprobe zram - RAM_DEV='0' + ZRAM_DEV='0' else - RAM_DEV=$(cat /sys/class/zram-control/hot_add) + ZRAM_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${ZRAM_DEV}/comp_algorithm + echo ${LOG_DISK_SIZE} > /sys/block/zram${ZRAM_DEV}/disksize + echo ${SIZE} > /sys/block/zram${ZRAM_DEV}/mem_limit + mke2fs -t ext4 /dev/zram${ZRAM_DEV} } getUsage () { - if [ "$ZL2R" = true ]; then - USAGE=$(zramctl -b | grep /var/log | awk '{ print $5 }') - else - USAGE=$(df "/var/log" | tail -1 | awk '{ print $3 }') - fi + BYTES_AVAIL=$(df "/var/log" | tail -1 | awk '{ print $4 }') + BYTES_USED=$(df "/var/log" | tail -1 | awk '{ print $3 }') + } -toBytes() { - echo $1 | echo $((`sed 's/.*/\L\0/;s/t/Xg/;s/g/Xm/;s/m/Xk/;s/k/X/;s/b//;s/X/ *1024/g'`)) -} pruneOldLog() { isSafe getUsage - PRUNE_BYTES=$(toBytes ${PRUNE_LEVEL}) - MEM_SIZE=$(toBytes ${SIZE}) - LOG_REMAINING=$(( ${MEM_SIZE} - ${USAGE} )) - if [ "$LOG_REMAINING" -lt "$PRUNE_BYTES" ];then - rsync -aXWv --delete --links $RAM_LOG/oldlog/ $HDD_LOG/oldlog/ 2>&1 | $LOG_OUTPUT - rm -rf $RAM_LOG/oldlog/* 2>&1 | $LOG_OUTPUT + LOG_REMAINING=$(( "( ${BYTES_AVAIL} * 100 ) / ( ${BYTES_AVAIL} + ${BYTES_USED} )" )) + if [ "$LOG_REMAINING" -lt "$PRUNE_LEVEL" ];then + rsync -arzh $ZRAM_LOG/oldlog/ $HDD_LOG/oldlog/ + rm -rf $ZRAM_LOG/oldlog/* fi } @@ -78,12 +73,12 @@ pruneOldLog() { case "$1" in start) [ -d $HDD_LOG/ ] || mkdir $HDD_LOG/ - mount --bind $RAM_LOG/ $HDD_LOG/ + mount --bind $ZRAM_LOG/ $HDD_LOG/ mount --make-private $HDD_LOG/ wait_for $HDD_LOG createZramLogDrive - mount -t ext4 -o nosuid,noexec,nodev,user=log2ram /dev/zram${RAM_DEV} ${RAM_LOG}/ - wait_for $RAM_LOG + mount -t ext4 -o nosuid,noexec,nodev,user=log2ram /dev/zram${ZRAM_DEV} ${ZRAM_LOG}/ + wait_for $ZRAM_LOG syncFromDisk ;; @@ -91,7 +86,7 @@ case "$1" in syncToDisk #ZRAM_LOG=$(awk '$2 == "/var/log" {print $1}' /proc/mounts) #ZRAM_LOG=$(echo ${ZRAM_LOG} | grep -o -E '[0-9]+') - umount -l $RAM_LOG/ + umount -l $ZRAM_LOG/ umount -l $HDD_LOG/ # Unsure as even with Root permision denied #echo ${ZRAM_LOG} > /sys/class/zram-control/hot_remove