Skip to content

Commit 0e09169

Browse files
authored
work even on a readonly filesystem
Merge pull request #111 from organic-ip/master
2 parents 3efc8f3 + 013ab90 commit 0e09169

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

log2ram

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,40 @@ isSafe () {
1010
[ -d $HDD_LOG/ ] || exit 1
1111
}
1212

13+
remountRW() {
14+
touch $HDD_LOG/$$ 2>/dev/null >/dev/null
15+
RESU=$?
16+
INITIAL_STATUS=ro
17+
if [ "$RESU" != "0" ] ; then
18+
mount -o remount,rw ${HDD_LOG}
19+
else
20+
INITIAL_STATUS=rw
21+
rm $HDD_LOG/$$ 2>/dev/null
22+
fi
23+
# for return
24+
echo $INITIAL_STATUS
25+
}
26+
27+
remountOriginal() {
28+
OPTION=$1
29+
mount -o remount,${OPTION} ${HDD_LOG}
30+
}
31+
1332
syncToDisk () {
1433
isSafe
15-
34+
INITIAL_STATE=$(remountRW)
35+
1636
if [ "$USE_RSYNC" = true ]; then
1737
rsync -aXv --inplace --no-whole-file --delete-after $RAM_LOG/ $HDD_LOG/ 2>&1 | tee -a $LOG2RAM_LOG
1838
else
1939
cp -rfup $RAM_LOG/ -T $HDD_LOG/ 2>&1 | tee -a $LOG2RAM_LOG
2040
fi
41+
remountOriginal ${INITIAL_STATE}
2142
}
2243

2344
syncFromDisk () {
2445
isSafe
25-
46+
2647
TP_SIZE=$SIZE
2748
if [ "$ZL2R" = true ]; then
2849
TP_SIZE=$LOG_DISK_SIZE
@@ -43,6 +64,7 @@ syncFromDisk () {
4364
else
4465
cp -rfup $HDD_LOG/ -T $RAM_LOG/ 2>&1 | tee -a $LOG2RAM_LOG
4566
fi
67+
4668
}
4769

4870
wait_for () {
@@ -65,6 +87,22 @@ createZramLogDrive () {
6587
mke2fs -t ext4 /dev/zram${RAM_DEV}
6688
}
6789

90+
make_log_dir () {
91+
# if create mount failed, try to remount in rw the parent directory
92+
# and restore original status
93+
if [ ! -d $HDD_LOG/ ] ; then
94+
mkdir $HDD_LOG/ 2>/dev/null /dev/null
95+
RESU=$?
96+
if [ "$RESU" -ne "0" ] ; then
97+
MOUNT_POINT=$(findmnt -T ` dirname $HDD_LOG/ ` -n --raw | cut -d ' ' -f 1 )
98+
mount -o remount,rw ${MOUNT_POINT}
99+
sleep 0.1
100+
mkdir $HDD_LOG/
101+
mount -o remount,ro ${MOUNT_POINT}
102+
fi
103+
fi
104+
}
105+
68106
case "$1" in
69107
start)
70108
IFS=';'
@@ -74,17 +112,16 @@ case "$1" in
74112
RAM_LOG=$i
75113
HDD_LOG=$PATH_FIRST_PART/hdd.$PATH_LAST_PART
76114
LOG2RAM_LOG="${RAM_LOG}/${LOG_NAME}"
77-
78-
[ -d $HDD_LOG/ ] || mkdir $HDD_LOG/
79-
mount --bind $RAM_LOG/ $HDD_LOG/
115+
make_log_dir
116+
mount --bind $RAM_LOG/ $HDD_LOG/
80117
mount --make-private $HDD_LOG/
81118
wait_for $HDD_LOG
82-
119+
83120
if [ "$ZL2R" = true ]; then
84121
createZramLogDrive
85-
mount -t ext4 -o nosuid,noexec,nodev,user=log2ram /dev/zram${RAM_DEV} ${RAM_LOG}/
122+
mount -t ext4 -o nosuid,noexec,noatime,nodev,user=log2ram /dev/zram${RAM_DEV} ${RAM_LOG}/
86123
else
87-
mount -t tmpfs -o nosuid,noexec,nodev,mode=0755,size=${SIZE} log2ram $RAM_LOG/
124+
mount -t tmpfs -o nosuid,noexec,noatime,nodev,mode=0755,size=${SIZE} log2ram $RAM_LOG/
88125
fi
89126
wait_for $RAM_LOG
90127
syncFromDisk

log2ram.service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[Unit]
22
Description=Log2Ram
33
DefaultDependencies=no
4-
Before=basic.target rsyslog.service syslog-ng.service syslog.target systemd-journald.service sysinit.target shutdown.target zram-swap-conf.service apache2.service
4+
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
55
After=local-fs.target
66
Conflicts=shutdown.target reboot.target halt.target
77
RequiresMountsFor=/var/log /var/hdd.log

0 commit comments

Comments
 (0)