/var/log on tmpfs - openhab2 directory issue

I’m trying to extend the life of my sdcard on the Raspberry Pi used for openhab2. I set up a tmpfs for /var/log as recommended by several people in the RPI forums. However, openhab2 is not happy because its log directory doesn’t exist inside this as it is wiped out every time at reboot or power cycle. What is the best solution to this? I can think of several ways to resolve it:

  1. Modify the openhab2 startup script to create the directory on the fly. (But likely my modification will be overwritten next time I upgrade openhab2.)
  2. Modify another startup script that runs prior to the openhab2 startup to create the log directory.
  3. Configure openhab2 to put its logs somewhere else (how?)
  4. Don’t use a tmpfs

Any recommendations?

  1. use a USB SSD: Booting Raspberry Pi 3 (RPi3) from USB mass storage without sd card

Mount /var/log/openhab as tmpfs

I could mount only /var/log/openhab as tmpfs, but I was hoping to pick up everything else in /var/log which gets quite a bit of write traffic. Or it would be possible to mount /var/log as tmpfs, then /var/log/openhab as another mount point on top of /var/log? Then it could have different permissions and be automatically created. I’ll have to take a look at it.

I had the same issue with OH1 and ended up by modifying the startup script to let it create the subdirectory if it does not exist yet. Something similar should be possible for OH2.

Sounds like something you should contribute as a Pull Request :wink: (@Benjy ?)

1 Like

Certainly! It should be a simply case of adding the line and ownership to the init.d file for sysVinit users and using the ExecStartPre line on the systemd unit file for systemd systems.

In general though, many services break if /var/log/ is made tmpfs, when you make this you should also add a few lines to your system boot scripts to add these files and if necessary give permissions.

e.g. in your /etc/rc.local file:

for dir in apt cups dist-upgrade fsck gdm installer samba openhab2 nginx fail2ban
do
  if [ ! -e /var/log/$dir ] ; then
    mkdir /var/log/$dir
  fi
done

# Set owners for the newly created log directories
touch /var/log/openhab2/openhab.log
chown -R openhab:openhab /var/log/openhab2/

Thanks for this @Benjy . I thought that this would be easy as many posts in the RPI forums suggest using tmpfs for /var/log, but it is harder than it seems. I will give this a try in rc.local. I think I’ll also unmount the tmpfs and see what was there before so I know if I’m missing any other directories.

I’m also going to switch to a much larger SD card as that should help with the wear-leveling. And work on an automated backup plan…

Easily available at:

You might also want to have a look here :slight_smile:

Do you stop the openhab2 server to do this backup? RaspiBackup says: “All running services should be stopped before the backup is started and restarted when the backup finished to get a consistent backup.”

Actually I don’t :grin:
The only thing you won’t have backed up properly are the logs in this case.

In the old days, where I still had a SD card in my RPi, I had to use the restore function of raspiBackup twice: it worked without any problems.

Have fun.

1 Like

I also faced with such problem and found the following solution:

When mounting ‘/var/log’ as ‘tmpfs’, you need to edit the below file, to add all the files/directories your current services need to find.

/usr/lib/tmpfiles.d/var.conf

For example, I added nginx, mosquitto, openhab2, etc log directories as below:

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# See tmpfiles.d(5) for details

q /var 0755 - - -

L /var/run - - - - ../run

d /var/log 0755 - - -
f /var/log/wtmp 0664 root utmp -
f /var/log/btmp 0600 root utmp -

d /var/cache 0755 - - -

d /var/lib 0755 - - -

d /var/spool 0755 - - -

d /var/log/apt 0755 root root -
d /var/log/letsencrypt 0700 root root -
d /var/log/mosquitto 0757 root root -
d /var/log/nginx 0757 root root -
d /var/log/openhab2 0757 root root -
d /var/log/nginx 0757 root root -
d /var/log/samba 0750 root root -
1 Like