From 4db08b8e529452a45ca31f317855d9292022793e Mon Sep 17 00:00:00 2001 From: organic-ip <65312228+organic-ip@users.noreply.github.com> Date: Thu, 14 May 2020 23:20:20 +0200 Subject: [PATCH 1/3] work with mount point read-only mode --- log2ram | 53 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/log2ram b/log2ram index a5eb026..52706e3 100755 --- a/log2ram +++ b/log2ram @@ -10,19 +10,40 @@ isSafe () { [ -d $HDD_LOG/ ] || exit 1 } +remountRW() { + touch $HDD_LOG/$$ 2>/dev/null >/dev/null + RESU=$? + INITIAL_STATUS=ro + if [ "$RESU" != "0" ] ; then + mount -o remount,rw ${HDD_LOG} + else + INITIAL_STATUS=rw + rm $HDD_LOG/$$ 2>/dev/null + fi + # for return + echo $INITIAL_STATUS +} + +remountOriginal() { + OPTION=$1 + mount -o remount,${OPTION} ${HDD_LOG} +} + syncToDisk () { isSafe - + INITIAL_STATE=$(remountRW) + if [ "$USE_RSYNC" = true ]; then rsync -aXv --inplace --no-whole-file --delete-after $RAM_LOG/ $HDD_LOG/ 2>&1 | tee -a $LOG2RAM_LOG else cp -rfup $RAM_LOG/ -T $HDD_LOG/ 2>&1 | tee -a $LOG2RAM_LOG fi + remountOriginal ${INITIAL_STATE} } syncFromDisk () { isSafe - + TP_SIZE=$SIZE if [ "$ZL2R" = true ]; then TP_SIZE=$LOG_DISK_SIZE @@ -43,6 +64,7 @@ syncFromDisk () { else cp -rfup $HDD_LOG/ -T $RAM_LOG/ 2>&1 | tee -a $LOG2RAM_LOG fi + } wait_for () { @@ -65,6 +87,22 @@ createZramLogDrive () { mke2fs -t ext4 /dev/zram${RAM_DEV} } +make_log_dir () { + # if create mount failed, try to remount in rw the parent directory + # and restore original status + if [ ! -d $HDD_LOG/ ] ; then + mkdir $HDD_LOG/ 2>/dev/null /dev/null + RESU=$? + if [ "$RESU" -ne "0" ] ; then + MOUNT_POINT=$(findmnt -T ` dirname $HDD_LOG/ ` -n --raw | cut -d ' ' -f 1 ) + mount -o remount,rw ${MOUNT_POINT} + sleep 0.1 + mkdir $HDD_LOG/ + mount -o remount,ro ${MOUNT_POINT} + fi + fi +} + case "$1" in start) IFS=';' @@ -74,17 +112,18 @@ case "$1" in RAM_LOG=$i HDD_LOG=$PATH_FIRST_PART/hdd.$PATH_LAST_PART LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}" - - [ -d $HDD_LOG/ ] || mkdir $HDD_LOG/ - mount --bind $RAM_LOG/ $HDD_LOG/ + + make_log_dir + + 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,noatime,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,noatime,nodev,mode=0755,size=${SIZE} log2ram $RAM_LOG/ fi wait_for $RAM_LOG syncFromDisk From 2264d5859710a6e935e73b564f9f85367963e90d Mon Sep 17 00:00:00 2001 From: organic-ip <65312228+organic-ip@users.noreply.github.com> Date: Thu, 14 May 2020 23:23:26 +0200 Subject: [PATCH 2/3] add lighttpd.service in list --- log2ram.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/log2ram.service b/log2ram.service index f07bbb5..70c4681 100644 --- a/log2ram.service +++ b/log2ram.service @@ -1,7 +1,7 @@ [Unit] Description=Log2Ram DefaultDependencies=no -Before=basic.target rsyslog.service syslog-ng.service syslog.target systemd-journald.service sysinit.target shutdown.target zram-swap-conf.service apache2.service +Before=basic.target rsyslog.service syslog-ng.service syslog.target systemd-journald.service sysinit.target shutdown.target zram-swap-conf.service apache2.service lighttpd.service After=local-fs.target Conflicts=shutdown.target reboot.target halt.target RequiresMountsFor=/var/log /var/hdd.log From 013ab903e50e184a149fb7373f444eab6143cd1a Mon Sep 17 00:00:00 2001 From: organic-ip <65312228+organic-ip@users.noreply.github.com> Date: Tue, 19 May 2020 23:27:13 +0200 Subject: [PATCH 3/3] indentation fix --- log2ram | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/log2ram b/log2ram index 52706e3..c26f1de 100755 --- a/log2ram +++ b/log2ram @@ -25,8 +25,8 @@ remountRW() { } remountOriginal() { - OPTION=$1 - mount -o remount,${OPTION} ${HDD_LOG} + OPTION=$1 + mount -o remount,${OPTION} ${HDD_LOG} } syncToDisk () { @@ -112,13 +112,11 @@ case "$1" in RAM_LOG=$i HDD_LOG=$PATH_FIRST_PART/hdd.$PATH_LAST_PART LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}" - make_log_dir - 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,noatime,nodev,user=log2ram /dev/zram${RAM_DEV} ${RAM_LOG}/