These days, almost all my installations are done using Ansible or Chef and tested using test-kitchen or molecule. However good end-to-end tests benefit from being able to start with a fresh build of your base OS system. 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
provisionerduring 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, which provided the main idea. Other steps culled from stack exchange and the raspberry pi forums.
The main requirement for this project is to be able to reset the rpi over ssh with a single command. 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.
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…?
Login to the machine over ssh, as as root, issue the reset command;
The Pi will reset itself…
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 is included as part of an
ansible role which I use to setup my rpis. For the factory reset mode, the scripts
are in the
~/files directory of that repo.
First clone the repo
Switch to the
Download raspian stretch lite, and unzip it into the
Verify loopback is clear of other devices
Currently the script will detach any loopback devices, so make sure you don’t
have anything on
should return nothing…
Run the setup script to generate the
This process creates an img file;
Write the restore
img to an sdcard.
The recovery partition is currently
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
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
--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