|
@@ -15,19 +15,21 @@ isSafe () { |
|
|
|
|
|
|
|
|
syncToDisk () { |
|
|
syncToDisk () { |
|
|
isSafe |
|
|
isSafe |
|
|
rsync -a $RAM_LOG/ $HDD_LOG/ 2>&1 | tee $LOG_NAME |
|
|
|
|
|
|
|
|
rsync -arzh $ZRAM_LOG/ $HDD_LOG/ 2>&1 | tee $LOG_NAME |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
syncFromDisk () { |
|
|
syncFromDisk () { |
|
|
isSafe |
|
|
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 |
|
|
echo "ERROR: RAM disk too small. Can't sync." >> $LOG_NAME |
|
|
umount -l $RAM_LOG/ |
|
|
|
|
|
|
|
|
umount -l $ZRAM_LOG/ |
|
|
umount -l $HDD_LOG/ |
|
|
umount -l $HDD_LOG/ |
|
|
exit 1 |
|
|
exit 1 |
|
|
fi |
|
|
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 () { |
|
|
wait_for () { |
|
@@ -40,37 +42,30 @@ 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' |
|
|
|
|
|
|
|
|
ZRAM_DEV='0' |
|
|
else |
|
|
else |
|
|
RAM_DEV=$(cat /sys/class/zram-control/hot_add) |
|
|
|
|
|
|
|
|
ZRAM_DEV=$(cat /sys/class/zram-control/hot_add) |
|
|
fi |
|
|
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 () { |
|
|
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() { |
|
|
pruneOldLog() { |
|
|
isSafe |
|
|
isSafe |
|
|
getUsage |
|
|
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 |
|
|
fi |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
@@ -78,12 +73,12 @@ pruneOldLog() { |
|
|
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 $ZRAM_LOG/ $HDD_LOG/ |
|
|
mount --make-private $HDD_LOG/ |
|
|
mount --make-private $HDD_LOG/ |
|
|
wait_for $HDD_LOG |
|
|
wait_for $HDD_LOG |
|
|
createZramLogDrive |
|
|
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 |
|
|
syncFromDisk |
|
|
;; |
|
|
;; |
|
|
|
|
|
|
|
@@ -91,7 +86,7 @@ case "$1" in |
|
|
syncToDisk |
|
|
syncToDisk |
|
|
#ZRAM_LOG=$(awk '$2 == "/var/log" {print $1}' /proc/mounts) |
|
|
#ZRAM_LOG=$(awk '$2 == "/var/log" {print $1}' /proc/mounts) |
|
|
#ZRAM_LOG=$(echo ${ZRAM_LOG} | grep -o -E '[0-9]+') |
|
|
#ZRAM_LOG=$(echo ${ZRAM_LOG} | grep -o -E '[0-9]+') |
|
|
umount -l $RAM_LOG/ |
|
|
|
|
|
|
|
|
umount -l $ZRAM_LOG/ |
|
|
umount -l $HDD_LOG/ |
|
|
umount -l $HDD_LOG/ |
|
|
# Unsure as even with Root permision denied |
|
|
# Unsure as even with Root permision denied |
|
|
#echo ${ZRAM_LOG} > /sys/class/zram-control/hot_remove |
|
|
#echo ${ZRAM_LOG} > /sys/class/zram-control/hot_remove |
|
|