Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4774

Raspberry Pi OS • Re: How to make sure power down never results in corruption?

$
0
0
as neilgl said, you can use the Overlay file system.

preferences - raspberry pi configuration - performance

turn on the overlay file system. Basically, makes the SD card read only.
the issue is you dont get the updates applied.... so you have to turn it off, then update, then turn it back on.

i wrote scripts to do that under Bullseye
I'm pretty sure the scripts will not work under Bookworm.... See Thagrols guide on "running a program at boot" (or something like that)
But the approach ought to work.

my (very old) writeup:
------------------------------------------------------begin OLD writeup
Auto-Updater for RPI4 using overlay Filesystem (rev 1 – Oct 2020)

Introduction:
This project was to automatically update a raspberry PI (ver 4) that is using the overlay filesystem (overlayfs). When the overlay FS is active, the RPI4 is running on a filesystem that cannot be modified. So any updates will go away on next reboot. This project fixes that. I hope

Modified system files required:
This process relies on the modified ‘raspi-config’ script as modified by SCOTTN20. His modification adds 2 items to raspi-config so you can enable and disable the overlayfs from command line (or a script). The new switches are:
raspi-config –enable-overlayfs
raspi-config –disable-overlayfs


Instructions on how to modify /usr/bin.raspi-config are on the Raspberry PI forums at this link
https://www.raspberrypi.org/forums/view ... s#p1697258


How it works:
This uses two(2) scripts, as described below:
1) rpi-updater-part1.sh - this script uses a call to ‘sudo apt-get update’ then ‘sudo apt-get -s fullupgrade’ to determine if any packages need to be upgraded (simulates an upgrade). If the result is not “0” meaning 0 packages to upgrade, it calls ‘raspi-config –disable-overlay’ waits a bit, and reboots.

2) rpi-updater-part2.sh - this script is invoked by rc.local on boot. Steps taken are:
1. It first looks for the log files in the current folder, and if one exists with today’s date, exits. This is to prevent an infinite loop should something go wrong.
2. If the overlayfs is ON, exits.
3. If the overlayfs is not on, it sets the /boot folder to read-write by re-mounting it
4. only updates if the WIRED ethernet is UP. (i.e. no wifi updates- wired only)
5. invokes ‘sudo apt-get update’
6. invokes ‘sudo apt-get full-upgrade’
7. if errors occurred in previous step (full-upgrade), invokes ‘sudo apt-get –fix-broken install’. (this may occur if dependencies are not met during an upgrade)
8. invokes ‘sudo apt-get autoremove’
9. invokes ‘sudo apt-get autoclean’
10. Checks the firmware version, if not current, writes a warning to the log file
11. Checks to see if raspi-config changed during the updates.
12. If so, the modified raspi-config that can be used to re-enable the overlay is gone…. Write the error to the log and exit.
13. If raspi-config has not been changed, then re-enable the overlay by invoking ‘sudo raspi-config –enable-overlayfs’ wait 30 seconds, and reboot.
14. Thus we return to the overlayfs after the updates.

The log file:
The “part2” script logs everything to a log file named todays date as YYYYMMDD.txt
This will be located in the folder where the part2 script is. One should check these log files to see how things went. Also, if a log file for today exists, the part2 script will do nothing. I added this check to prevent an endless reboot loop if something does wrong ala murphy’s law.

The Fatal Error:
The fatal error that can occur happens if raspi-config is replaced by an updated version (during apt-get full-upgrade). In this case, the modified raspi-config that was used to disable and enable the overlayfs is gone…. So the script can no longer turn the overlay on and off. If this occurs, a warning is written to the log file, and the system ends up with the overlay off. To recover, you need to:
1. use “preferences-raspberry pi configuration” to set the boot sector to RW
2. reboot
3. re-modify /usr/bin/raspi-config and re-add the modifications.
4. Re-enable the overlayfs
5. reboot


How to SETUP:
(example as user pi, installing into a folder)
A) create a folder ‘updater’ under home.
B) Copy both scripts to that folder (rpi-updater-part1.sh and rpi-updater-part2.sh)
C) make both executable with ‘chmod +x rpi-updater-part1.sh’ and ‘chmod +x rpi-updater-part2.sh’ (or use permissions from the file manager GUI)
D) Modify usr/bin/raspi-config to add the –enable-overlayfs and –disableoverlayfs options.
E) Modify /etc/rc.local adding a call to script2 ( add ‘cd /home/pi/updater’ then ‘./rpi-updater-part2.sh’ to the file
F) make rc.local executable with ‘sudo chmod +x /etc/rc.local’
G) turn on the overlay file system under menu “preferences – raspberry pi configuration” the “performance” tab – turn on the overlay and make boot read-only.
H) Reboot.

How to RUN:
You can run the script “rpi-updater-part1.sh” directly OR schedule a cron job to run it at some interval. An example cron entry is included in this archive (running once a week, monday, at 0500).


TerribleTed (tedtorres@juno.com)

------------------------------------------------------------------end writeup

Script #1

Code:

#!/bin/sh#  Updater script part 1 for RPI4.  # this script automates updating an RPI when overlay file system is ON.# normally, the overlay FS renders updates ineffective# due to the overlay FS#  # ! ! NOTE: THIS REQUIRES the modified Raspi-config as described below ! !#     Raspi-config as modified by "Scottn20" description at:# https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=279530&p=1697258&hilit=overlay+%26+updates#p1697258## This Script:#    invokes 'apt-get update'#    invokes 'apt-get -s upgrade' -s=simulate#      $myexit is the number of updates available.#    if myexit is non-zero, THEN#       - disable overlay file system by call to modified raspi-config#       - reboots# # # TerribleTed, ted.torres@gmail.com  on Oct 6 2020######################################################### updatecd /home/pi/updaterdate>lastcheck.txtecho "getting updates......">>lastcheck.txtsudo apt-get updateecho $? return from apt-get update>>lastcheck.txt# check for updates# get number of packages to be upgradedmyexit=$(apt-get upgrade -s |grep -P '^\d+ upgraded'|cut -d" " -f1)echo $myexit packages to update>>lastcheck.txt# if [ "$myexit" != "0" ]; thenecho $myexit packages to be updatedsleep 10# get overlay statusfoo=$(cat /boot/cmdline.txt)myoverlay=$(echo $foo | cut -d'=' -f 1)lookfor2="boot"if [ "$myoverlay" = "$lookfor2" ]; then# overlay is onecho " ! ! Overlay is ACTIVE - removing"sudo raspi-config --disable-overlayfssleep 10echo Rebooting in 5 secsleep 5rebootelse# overlay is off echo overlay is off... normal updates will be OK.fielse # exit code was "0"echo $myexit packages to be updatedsleep 5fi
Script #2

Code:

#!/bin/sh#  part 2 of Updater script for RPI4.  # This script writes a log file, named the run date (as YYYYMMDD.txt)## ! ! NOTE: THIS REQUIRES the modified Raspi-config as described below !#     "Scottn20" description at:# https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=279530&p=1697258&hilit=overlay+%26+updates#p1697258## note: this script will not run if it has already run today# as evidenced by the log file for today existing.# this is to prevent endless loop if the user manually turns off# the overlay file system. in that case, it will run once, then quit## step 1- checks for overlay file system. if overlay is ON, exits.#         remounts /boot as  RW (read-write...was ReadOnly)# step 2- Checks to see that WIRED ethernet is UP. only updates#         on wired ethernet.# step 3 - if all is ready, calls update, full-upgrade, autoremove, autoclean.# step 4 - checks the firmware version, and upgrades if needed.# step 5 - if raspi-config has not changed, re-enables the overlay, and reboots.#           if it has changed, we cannot re-enable the overlay... log it and exit# # All actions logged to the above log file.# # Caution: if the updates replace raspi-config, that will break this.#         evidenced by ending up with a 'no overlay' after the reboot in step 5.#         ... you would need to modify raspi-config as above...#         that is, until Scottn20's mods become mainstream### TerribleTed, ted.torres@gmail.com  on Oct 6 2020########################################################mynow=$(date '+%Y%m%d').txt#only run if the log file for today does not existif [ -f "$mynow" ]; then    echo "$mynow exists.... exiting"else echo "$mynow does not exist...updating"lookfor2="boot"# get overlay statusfoo=$(cat /boot/cmdline.txt)myoverlay=$(echo $foo | cut -d'=' -f 1)if [ "$myoverlay" = "$lookfor2" ]; then# overlay is onecho " ! ! Overlay is ACTIVE - updates will have no effect!- Exiting"else#overlay is OFF - proceed# write date to log filedate>$mynow# write to log echo " Overlay is NOT ACTIVE - updates OK"echo " Overlay is NOT ACTIVE - updates OK" >>$mynow# set /boot to RWsudo mount -o remount,rw /bootecho " Set /boot to RW filesystem"echo " Set /boot to RW filesystem" >>$mynowsleep 10# get state of ethernetmyup=$(cat /sys/class/net/eth0/operstate)lookfor="up"if [ "$myup" = "$lookfor" ]; thenecho "---- ETH0 wired is UP"echo "---- ETH0 wired is UP">>$mynow# get date of /usr/bin/raspi-config to check later for changeb4raspi=$(stat -c %Y /usr/bin/raspi-config)#echo "---- UPDATE"echo "---- UPDATE">>$mynowsudo apt-get update >>$mynow 2>&1echo " ">>$mynowecho "---- UPGRADE"echo "---- UPGRADE">>$mynowsudo apt-get -y full-upgrade>>$mynow 2>&1if [ $? != 0 ]; thenecho output of upgrade errorcode is $?echo output of upgrade errorcode is $?>>$mynowecho Error in upgade... running fix-brokenecho Error in upgade... running fix-broken>>$mynow# errors, try to fix broken packagessudo apt-get -y --fix-broken install>>$mynow 2>&1echo .....done running fix-brokenecho .....done running fix-broken>>$mynowfiecho " ">>$mynowecho "---- AUTOREMOVE"echo "---- AUTOREMOVE">>$mynowsudo apt-get -y autoremove >>$mynow 2>&1echo " ">>$mynowecho "---- AUTOCLEAN"echo "---- AUTOCLEAN">>$mynowsudo apt-get autoclean >>$mynow 2>&1echo "---- DONE! "echo "---- DONE! ">>$mynow## now check Firmwareecho " Checking Firmware">>$mynow 2>&1echo " Checking Firmware"# get firmware status# note that this saves to a file, which is later deletedsudo rpi-eeprom-update -m $PWD/firmstat.txtfoo=$(cat $PWD/firmstat.txt)rm -f $PWD/firmstat.txtmyfirm1=$(echo $foo | cut -d'"' -f 4)myfirm2=$(echo $foo | cut -d'"' -f 6)if [ "$myfirm1" = "$myfirm2" ]; then# firmware ver matchecho " Firmware UP TO DATE- Exiting">>$mynow 2>&1echo " Firmware UP TO DATE- Exiting"#sleep 5else# firmware vers do not match - echo " Firmware NOT UPDATED">>$mynow# write to log fileecho " ! ! Firmware NEEDS Updating">>$mynowecho " use  sudo rpi-eeprom-update -a ">>$mynowecho " to update the firmware....">>$mynowfi# if raspi-config has changed, fatal error, will not be able to#  restore the overlay file system..# get AFTER date of /usr/bin/raspi-configafterraspi=$(stat -c %Y /usr/bin/raspi-config)### if the b4raspi is the same as afterraspi, restore#   the overlay FS# if not.... bummer. write to log and exitif [ "$b4raspi" = "$afterraspi" ]; then# b4 and after are the same.# restore the overlay #echo " ..restoring overlay FS"echo " ..restoring overlay FS">>$mynowsudo raspi-config --enable-overlayfs# sleep 30 just in case overlay generation takes a whileecho " ..waiting 30 seconds - just in case"sleep 30rebootelse# raspi-config has changed.echo " CANNOT restore overlay FS"echo " CANNOT restore overlay FS">>$mynowecho " raspi-config has been changed">>$mynowecho " and must be re-modified !">>$mynowfielseecho "---- ETH0 wired is DOWN.. No updates processed"echo "---- ETH0 wired is DOWN.. No updates processed">>$mynowfififi

Statistics: Posted by terribleted — Sun Dec 15, 2024 2:51 pm



Viewing all articles
Browse latest Browse all 4774

Trending Articles