Note: The mercurial server is disabled at the moment while I investigate whether it can run with an acceptably low CPU load – Mike.
Snapshot reset for Raspberry Pi
This scheme makes it possible to take a snapshot of the filesystem state of a Raspberry Pi and restore that state later. It's useful for testing and debugging scripts that install the packages needed for some fixed purpose, like the Bare Metal project.
- Copy the Raspi OS lite image to an SD card of adequate size.
- Now leave some space after the root partition that results and make another ext4 partition that will become the live partition for the installation. Copy the contents of the root partition here. On a 16GB card, I allowed 4GB for the recovery partion, and 12GB or so for the live partition.
- I fumbled this part a bit and made a temporary partition as a place-holder, ending up with the boot partition numbered 1 and the two root partitions numbered 2 and 4.
- Boot the SD card in an RPi. It will come up with partition 2 as root.
- Because the card does not contain exactly two partitions, the root partition resizing won't be done automatically, and you will have to resize both partitions manually. For partition 2, you will need first to expand the entry in the partition table, then expand the file system: use
parted
andresize2fs
. Partition 4 is already as large as needed in the partition table, but the file system needs expanding to fill it. Note that, although it did not resize the partitions, the script will have rewritten the UUID of the disk. - Copy
/boot/cmdline.txt
to/boot/cmdline.recover
, then edit/boot/cmdline.txt
to change the root file system fromPARTUUID=xxxxxx-02
toPARTUUID=xxxxxx-04
. Also mount partition 4 and edit/etc/fstab
to point to the correct UUID and partition 4 as the root file system. - Reboot, and you will be running with the live file system. Install whatever packages you want to make part of the snapshot, including the GUI if you want it. Switch to starting the GUI at boot if you like.
- Copy
/boot/cmdline.txt
to/boot/cmdline.live
. - To make a snapshot, copy
/boot/cmdline.recover
to/boot/cmdline.txt
and reboot. You are now in recovery mode. - Use
sudo mount /dev/mmcblk0p4 /mnt
followed bysudo tar xvfz image.tar --acls -C mnt .
to create a snapshot. You need to save access control lists because of a very few files that have them: notably the directory/media/pi
, although this will be recreated by Gnome with the correct permissions if you delete it. - You can restore a snapshot by deleting the contents of the live partition and untarring
image.tar
. Make sure that the root of the partition is owned byroot
with permissions 755 after this, or there will be problems. (Maybe I caused the problem by the way I copied the initial contents of partition 4.) - To boot the restored system, copy
/boot/cmdline.live
toboot/cmdline.txt
and reboot.
No doubt more of this could be automated when it settles down into a routine. By shrinking the partition, it should be possible to set the whole thing up with a script run just after booting the standard Lite image for the first time, and to automate a snapshot or a restore using sutiable init scripts.
This blog post is helpful: http://www.limepepper.co.uk/raspberry-pi/2018/04/15/Remote-factory-reset-for-raspberry-pi-2.html