openhab / openhab-syno-spk

openHAB Synology SPK Install Package
Eclipse Public License 2.0
161 stars 42 forks source link

Allow Synology to pause harddisks #91

Open ms777 opened 7 years ago

ms777 commented 7 years ago

openhab2 seems to permanently write to the log files, and, if used, to files for the persistence services.

This script creates a memory file system to avoid hard disk access

#!/bin/sh
# On startup create a tmpfs and link some openHAB2 folders to it
# This is to allow the DS to power down their hard disks
# For execution during system start and system shutdown put it into the following folder: 
# /usr/local/etc/rc.d/

OPENHAB2_ROOT="/volume1/@appstore/openHAB2"

# where the tmpfs should be created
TMPFS="/volume1/public/openHAB2/tmpfs"

# must be subfolders of $OPENHAB2_ROOT
FOLDERS_FOR_TMPFS="userdata/logs userdata/persistence/rrd4j"

ECHO_FILE="/volume1/public/"$(basename $0)'.log'
ECHO_PREF=$(date +%Y-%m-%d' '%T)'> '

echo $ECHO_PREF"-----------------------------------------------------------------" >> $ECHO_FILE
echo $ECHO_PREF"called with "$1  >> $ECHO_FILE

case $1 in
start)
# create a tmpfs 
    if $(mount | grep "none on $TMPFS type tmpfs" >> /dev/null); then
        echo $ECHO_PREF"start: $TMPFS is mounted => start unmounting $TMPFS" >> $ECHO_FILE
        umount $TMPFS
    fi
    if [ -d "$TMPFS" ]; then 
        echo $ECHO_PREF"start: directory $TMPFS exists => removing" >> $ECHO_FILE
        rm -rf $TMPFS
    fi
    mkdir $TMPFS
    mount -t tmpfs -o size=20M none $TMPFS 
    chmod 777 $TMPFS
    chown -hR openhab2:users $TMPFS

# for each folder in $FOLDERS_FOR_TMPFS: delete in $OPENHAB2_ROOT, create in $TMPFS, create symbolic links  
    for f in $FOLDERS_FOR_TMPFS
    do
        echo $ECHO_PREF"creating $TMPFS/$f" >> $ECHO_FILE; 
        if [ ! -d "$TMPFS/$f" ]; then mkdir -p $TMPFS/$f; fi

        echo $ECHO_PREF"deleting $OPENHAB2_ROOT/$f" >> $ECHO_FILE; 
        if [ -d "$OPENHAB2_ROOT/$f" ]; then rm -rf $OPENHAB2_ROOT/$f; fi

        echo $ECHO_PREF"creating $OPENHAB2_ROOT/$f" >> $ECHO_FILE; 
        mkdir -p $OPENHAB2_ROOT/$f

        echo $ECHO_PREF"linking $TMPFS/$f to $OPENHAB2_ROOT/$f" >> $ECHO_FILE; 
        mount --bind $TMPFS/$f $OPENHAB2_ROOT/$f 

        chmod 777 $OPENHAB2_ROOT/$f
        chown -hR openhab2:users $OPENHAB2_ROOT/$f
        chmod 777 $TMPFS/$f
        chown -hR openhab2:users $TMPFS/$f
    done
    ;;

stop)
    for f in $FOLDERS_FOR_TMPFS
    do
        if $(mount | grep "none on $OPENHAB2_ROOT/$f type tmpfs" > /dev/null); then
            echo $ECHO_PREF"stop: $OPENHAB2_ROOT/$f is mounted => start unmounting $OPENHAB2_ROOT/$f" >> $ECHO_FILE
            umount $OPENHAB2_ROOT/$f
        fi
    done

    if $(mount | grep "none on $TMPFS type tmpfs" > /dev/null); then
        echo $ECHO_PREF"stop: $TMPFS is mounted => start unmounting $TMPFS" >> $ECHO_FILE
        umount $TMPFS
    fi
    if [ -d "$TMPFS" ]; then 
        echo $ECHO_PREF"stop: directory $TMPFS exists => removing" >> $ECHO_FILE
        rm -rf $TMPFS
    fi
    ;;

*)
    echo "Usage: $0 [start|stop]"
    ;;
esac
--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/49157099-allow-synology-to-pause-harddisks?utm_campaign=plugin&utm_content=tracker%2F19715072&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F19715072&utm_medium=issues&utm_source=github).
SchoppAx commented 7 years ago

Hi @ms777,

I'm not familiar with this kind of system functionality.

Am I right, that we copy or create the script at the installation and call it each time openhab starts/stops?

ms777 commented 7 years ago

... no. This must be called at Synology boot time with the parameter "start", and should be called with "stop" prior to synology shutdown.

This script is placed in /usr/local/etc/rc.d/ (see the comment section). Then the above happens automagically I am not sure if this should be integrated into the installer, as there probably are too many variations of to-be-saved folders possible. Just wanted to post it somewhere ...

tsw86 commented 7 years ago

An alternative is to plug in a usb key in your Synology and log there. That way my hard drives will enter hibernate mode. I made a pdf which can be downloaded here: https://community.openhab.org/t/synology-diskstation/1446/124

Update 2017-09-29: verified - this still works with latest pre-release 2.2.0.007-SNAPSHOT

DarkLite1 commented 7 years ago

It would be great if this could be included. Allows the disk to go into hibernate. During installation, can this not be copied to '/usr/local/etc/rc.d/'? I think it would benefit a lot of Synology users.

SchoppAx commented 7 years ago

Yes, that's the plan :-)

But we have to make the script dynamic. For example not everybody has the files on volume1.

We will include the file inside the sources and set placeholders that will be replaced on the installation.

DarkLite1 commented 7 years ago

I can't even find the current events.log file on my NAS, checked the following as indicated on the OpenHAB doc site:

Nothing there, the two installation files are present though. Am I missing something here?

cniweb commented 7 years ago

@DarkLite1 you found it here:

DS3615xs> ls -la /volume1/@appstore/openHAB2/userdata/logs/
drwxr-xr-x    2 openhab2 root          4096 Oct 22 20:38 .
drwxrwxr-x    9 openhab2 root          4096 Oct 22 20:38 ..
-rw-r--r--    1 openhab2 users            0 Oct 22 20:38 audit.log
-rw-r--r--    1 openhab2 users            0 Oct 22 20:38 events.log
-rw-r--r--    1 openhab2 root          1377 Oct 22 20:38 openhab.log

We have added it in the README.md file: https://github.com/openhab/openhab-syno-spk/commit/6fd04a34a706edecaf031f4b99556474a0687ace

DarkLite1 commented 7 years ago

Thank you guys, appreciate it :+1:

wirebug commented 6 years ago

I am not familiar with the installation process of an spk, so please be kind ;-) Is it possible to extract the install path into a config file -> http://wiki.bash-hackers.org/howto/conffile (eg. located in the openhab root) for the script? If a user decides to move his logs to another location, a simple edit of the config file would do the trick..

tsw86 commented 6 years ago

I am using the 2.3.0.005 release and the hard drives still go into hibernate mode. However I'm seeing that after a certain amount of time, the log files abruptly end and (that is what I suspect) my Z-wave binding does not initialize properly (my 'things' won't go online anymore).

My NAS is shut down every night and on startup the log files are not appended any more after the sudden ending of them. I do have set the log level to 'DEBUG', not sure if that is the cause. For OpenHAB to write again in (new) log files, I had to manually delete the files with an abrupt ending. Then after a period of time, the log files would end abruptly again.

I then moved my log files to an external USB drive again and since then I no longer have issues with the log files and my Z-wave 'things' are online.