johntomko1 wrote: ↑Fri Mar 27, 2020 1:08 pm
A push button that, once the SD card boots, can read a GPIO pin to decide if the button is pushed or not. If it is, I can read from a mounted USB device that’s storing a copy of the SD card’s file system and “copy” everything from the USB drive to the SD card. The problem is that if something causes the SD card to not boot properly, I can’t actually reset like I could before by switching to an entirely different device.
Any thoughts on how to make an inaccessible SD card “unbrickable?” Or is that about as close as I‘m going to get?
In addition to the problem already mentioned (you can't overwrite the SD card your running on), your approach assumes that the SD card is still healthy enough to boot and run. SD card corruption often results in the Raspberry Pi failing to boot at all. I would suggest that running from a USB flash drive and using the SD card only for recovery is a more reliable approach. It would work something like this:
Create your SD card to do what it normally does. In addition, have it monitor a GPIO pin for a button closure. If a button closure is detected, execute the following:
Then run
usb-boot to replicate the SD card to a USB flash drive. Reboot and you'll be running from the USB flash drive.
If you want to reconstruct the USB flash drive, press the button and the Raspberry Pi will reboot into the SD card. Log in via SSH and run
usb-boot again, reboot, and you're starting fresh.
If you don't have network access to the Raspberry Pi or would like to totally automate the process, do the following instead of using
usb-boot:
Boot the SD card and run
image-backup and
image-set-partuuid to create /media/backup.img:
Code: Select all
image-backup -i /media/backup.img
image-set-partuuid /media/backup.img random
Create the following script and make it executable:
Code: Select all
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
dd bs=4M if=/media/backup.img of=/dev/sda
sync
yes | sdc-boot /dev/sda2
Insert the script pathto/name as the line just before the 'exit 0' in /etc/rc.local.
The next time you boot the SD card and each time you press the button, the USB flash drive will be reconstructed and booted.
Both of these methods have been tested and verified to work.
For maximum reliability, you should power the Raspberry Pi from a UPS with accompanying shutdown software should the UPS near exhaustion (
NUT is an excellent candidate).