Power failure: how to make sure that RPi is nicely shutdown

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.

Moderator: leecollings

Post Reply
Number8
Posts: 374
Joined: Friday 23 May 2014 7:55
Target OS: Linux
Domoticz version: 2022.1
Location: Saint Pierre de Jards
Contact:

Power failure: how to make sure that RPi is nicely shutdown

Post by Number8 »

Hello to all,
I'd like to report a solution I setup for 3 RPI running Domoticz. The idea is to hook up an inexpensive solution as an alternative to a UPS when this is not really needed. What does this HAT: when a main power failure condition arises, after 10 seconds it triggers a shutdown. When the power resumes (including during shutdown sequence), it will send a hardware signal to wake up RPi. We all know that having to proper shutdown sequence avoid the high probability to corrupt the SD card. I have a forth Domotiz running on a NUC Intel gear, that is acting as the master. This one is connected to a large UPS. However, I cannot guarantee there will be enough juice time before main power supply resumes. I'm currently setting up a 4th Domoticz whose sole role will be to manage power down sequence of all equipment connected to UPS's.

So far I bought 5 unit to Thomas Giegling http://safe-power.appspot.com/. Thomas is a very nice fellow, very prompt to answer questions.
Debian buster on NUC and three RPi with buster.
grasvezel
Posts: 2
Joined: Tuesday 13 November 2018 12:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Power failure: how to make sure that RPi is nicely shutdown

Post by grasvezel »

I realize it is not exactly an answer to your question, but in my experience the risk of SD card corruption when running Domoticz on a Pi is significantly reduced if you put the Domoticz logfile on a ramdisk.
Running Domoticz on Pi3 with RFlink, ZStick, OTGW, Hue, and a bunch of ESP's running ESP Easy
lost
Posts: 665
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Power failure: how to make sure that RPi is nicely shutdown

Post by lost »

grasvezel wrote: Friday 16 April 2021 10:54 I realize it is not exactly an answer to your question, but in my experience the risk of SD card corruption when running Domoticz on a Pi is significantly reduced if you put the Domoticz logfile on a ramdisk.
Never screwed a journaling FS like Ext4 with a power fail. That's designed for this! If issues occurs, that's IMO most probably with some uSD integrated storage controller firmware that are not robust enough/buggy.

What may happen as a boot blocker, indeed, is having an initial boot loader (doing the job of a PC BIOS) with minimal Ext4 support (i.e only a simple reader) that cannot handle journal replay if needed. If journal-replay need was concerning blocks allocated to files needed at early boot, you then have an unbootable system. This may occur if power fail occurs when apt upgrading for instance.

Anyway, on a PI, you can extract the uSD. So in such case, just plug the uSD in a Linux PC with full Ext support will do this replay at mount & this will be printed in dmesg/syslog if this needed to be done. If so, just unmount the SD and set it back to PI, should just work again!

Not having log files for post mortem debug because this was setup in a tmpfs is IMO not helpful.

Another reason to do this could be flash wear... But there is some 'knobs' in file-system or virtual memory management to be able to allow better merge writes, delay commits. IMO, that's a better compromise.

For instance, I add this in my /etc/sysctl.conf

Code: Select all

vm.dirty_ratio=60
vm.dirty_expire_centisecs=120000
Can be tested/tuned using sysfs before setting values enforced at boot.

With a good uSD (i.e. branded A2 for applicative use in smartphones, so tuned for OS system continuous use and not for temporary storage with mostly sequential writes), you may also speed up SD interface (from 50MHz default up to 100 max):

Add this (83MHz, maybe a bit conservative setting) in /boot/config.txt

Code: Select all

dtparam=sd_overclock=83
So less writes & less time to handle writes that's less probability to have a power fail in the middle of one. Combined with journaling feature of Ext4 (don't listen to those that advice using a non journaling FS, they did not understand why they were designed at all), that'll be OK.
grasvezel
Posts: 2
Joined: Tuesday 13 November 2018 12:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Power failure: how to make sure that RPi is nicely shutdown

Post by grasvezel »

Good tips, thanks for that. I wasn't aware of the incomplete ext4 implementation used at boot. Still, writing stuff to SD that you will not need after the next reboot seems pointless to me.
Running Domoticz on Pi3 with RFlink, ZStick, OTGW, Hue, and a bunch of ESP's running ESP Easy
manjh
Posts: 749
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Power failure: how to make sure that RPi is nicely shutdown

Post by manjh »

grasvezel wrote: Friday 16 April 2021 10:54 I realize it is not exactly an answer to your question, but in my experience the risk of SD card corruption when running Domoticz on a Pi is significantly reduced if you put the Domoticz logfile on a ramdisk.
Interesting! I run Domoticz on a Pi 4 8GB, so plenty of RAM to play around with.
Is there a description somewhere about how to set this up?
Hans
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: Power failure: how to make sure that RPi is nicely shutdown

Post by EddyG »

I use this on all my Pi's https://github.com/azlux/log2ram
Put Domoticz log also in /var/log
Put /tmp in RAM with in fstab

Code: Select all

tmpfs                                       /tmp            tmpfs   nodev,noatime,mode=0777,size=256M 0       0
And use Industrial grade SD-cards.
I have a Raspberry Pi 2 (with Jessie) running continuously for more that 3 years now, without any problem/error or crash.
manjh
Posts: 749
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Power failure: how to make sure that RPi is nicely shutdown

Post by manjh »

EddyG wrote: Saturday 17 April 2021 12:34 I use this on all my Pi's https://github.com/azlux/log2ram
Put Domoticz log also in /var/log
Put /tmp in RAM with in fstab

Code: Select all

tmpfs                                       /tmp            tmpfs   nodev,noatime,mode=0777,size=256M 0       0
And use Industrial grade SD-cards.
I have a Raspberry Pi 2 (with Jessie) running continuously for more that 3 years now, without any problem/error or crash.
I'll have a look at this as soon as I am back home next week.
Also, I am considering using an SSD as boot device, this way there is no need for an SD card any longer, and it is quite a bit faster. I think I have a 32GB SSD lying around somewhere, this should be sufficient.
Hans
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest