Looking for feedback on how I have reduced the SD writes on my raspberry Pi2 and openhabian and if they are likely to cause issues in the future as I build the server up to have more abilities. I should stress the main reason for doing this is not wear and tear of the SD card, but instead for reasons to help prevent corruption when dirty shutdowns are made. ie pulling the power out when the kernel locks up or the power fails. I have a good UPS which auto records power events and I see around 12 a month on average where I live, so they are very common.
I found the main writes were caused by:
/var/log/openhab/events.log
/var/log/openhab/openhab.log
/var/lib/samba/wins.dat
/var/cache/samba/
Less often writes caused by:
/var/log/syslog
/var/log/daemon.log
/var/log/auth.log
/var/log/mosquitto/mosquitto.log (if using mosquitto for MQTT)
After making some basic changes, my green LED for SD access on the rpi will go for minutes with no flashes. I tried to keep the changes minimal and easy to make and reverse.
sudo mkdir /sambaBUP/
sudo cp -af /var/lib/samba/. /sambaBUP/
sudo nano /etc/rc.local
add the following to rc.local file which the last command above will open the file for editing:
chown openhab:openhab /tmp/
cp -af /sambaBUP/. /var/lib/samba/
ln -sf /tmp/events.log /var/log/openhab2/events.log
ln -sf /tmp/openhab.log /var/log/openhab2/openhab.log
ln -sf /tmp/syslog /var/log/syslog
ln -sf /tmp/daemon.log /var/log/daemon.log
ln -sf /tmp/auth.log /var/log/auth.log
ln -sf /tmp/mosquitto.log /var/log/mosquitto/mosquitto.log
Run this command:
sudo nano /etc/fstab
inside the fstab file add this and also add noatime to every mount. If you are using the latest raspbian build this is the default but it wont hurt to add it.
tmpfs /tmp tmpfs defaults,nosuid,nodev,noatime,size=50m 0 0
tmpfs /var/cache/samba tmpfs defaults,nosuid,nodev,noatime,size=5m 0 0
tmpfs /var/lib/samba tmpfs defaults,nosuid,nodev,noatime,size=5m 0 0
Type âsudo rebootâ and that should be everything done. Any issues you can disable lines by using a â#â at the start of the line should you want the log files to once again be sent to the SD card. Still testing to see if 100mB is enough for the log files without lowering the logs from Info to Error. So far all seems good.
To check what files get written use these commands:
sudo apt-get update
sudo apt-get install inotify-tools
sudo nano /proc/sys/fs/inotify/max_user_watches
inotifywait -mr --exclude 0 -e modify,attrib,close_write,move,create,delete /
You need to increase the number in the file /proc/sys/fs/inotify/max_user_watches if you want to do a global watch, otherwise you can watch a specified folder. Add two â0â to the end of the number and save and exit nano. Press Ctrl+C to stop watching with the last command. This will list all file writes even if they are not on the SD card.
EDIT: As others have pointed out your persistence will write and can not be stopped, so if setting this up you need to point it to a place like a NAS or USB. Because flash uses blocks to store data, any write that is interrupted due to power loss will cause corruption in other files. By shifting the persistence writes to another location you will keep a stable Openhab system for far longer.