The main requirement for this project is to be able to reset the rpi over ssh with a single command and have the Pi come back on the network with a fresh OS. It is mostly useful for development and testing where you are not keeping persistent data or configurations on the pi.
Be warned, don’t use this if you intend the Pi to be used as a production system, it will blat the pi and the sdcard.
These days, almost all my installations are done using ansible and tested using test-kitchen. However good end-to-end testing benefit from being able to start with a fresh build of your base OS system. This is difficult with raspberry pi, as having to restore sdcards when I want fresh Pi, makes this something of a chore.
Obviously virtualization/containerization makes this easy for x86_64 systems. However raspberry pis are on the Arm architecture, and emulating this on an x86_64 based linux desktop is not that straightforward. So an alternative solution is to create a restore partition which can be used factory reset the Pi back to its original state. We can then use this as the
provisioner
during setup in molecule or test-kitchen.
Or it can just be used to reset the pi when you have messed it up!!
This article was inspired by this blog post (note: site seems gone 2021-02-21), which provided the main idea. Other steps culled from stack exchange and the raspberry pi forums.
If you are not interested in the details, then I will start with a short demo on remotely reseting, and the second section on creating the recovery image.
I have a script create-factory-reset which modifies the base image to add the tools for resetting. It modifies the raspbian image to produce a new image which you use to flash to your rpi. once installed, it works like this.
What it does…?
Once a raspberry Pi is booted from the modified image, you can login to the machine over ssh, as root, issue the reset command;
The Pi will proceed to reset itself and reboot…
And if you wait 10 minutes or so… It will replace the /
file system with a
pristine copy of raspbian (and enabled ssh access at the same time), and you will
be able to login again to fresh Raspberry Pi over ssh;
How to do this…
Follow these steps;
Checkout the repo with the script.
The script to create the factory-reset partition here:
First clone the repo
Examine the files
Download raspian stretch lite, and unzip it into the script directory
Verify loopback is clear of other devices
Currently the script will detach any loopback devices, so make sure you don’t
have anything on /dev/loop0
etc.
should return nothing…
Run the setup script to generate the img
file;
This process creates an img file;
2018-03-13-raspbian-stretch-lite.restore.img
Write the restore img
to an sdcard.
The recovery partition is currently 4G
so you probably want an sdcard at least 32GB
, though you might get away with 16GB
if you aren’t planning to install much else.
First determine which device is your sdcard. Generally I check dmesg
immediately
after I plug the card. The output will contain the identifier for the new device.
;
You can double check that the device you found corresponds to the size you expect for the sdcard;
WARNING!!! The next step will blat any data on the sdcard!!!!
So my card is at /dev/sdd
. I write the restore img to that device;
Boot the Raspberry PI from the sdcard
Now when you boot from the sdcard image, you have additional commands available;
WARNING!!! The next step will blat the rpi and any data on it!!!!
Factory reset the Raspberry Pi
use the --reset
option to /boot/factory_reset
to trigger restoration;
The pi will boot to the recovery partition and restore itself to factory condition;
And now when the Pi reboots, it will boot to a fresh copy of Raspbian lite with everything restored to factory settings;
If you want to restore other distros, such as the full fat desktop, you will probably have to checkout the next part in this series. How to create the image