Skip to content

Instantly share code, notes, and snippets.

@lilianmallardeau
Created October 30, 2023 00:47
Show Gist options
  • Save lilianmallardeau/04368e95bc97eb9738fba13df3bbfe4a to your computer and use it in GitHub Desktop.
Save lilianmallardeau/04368e95bc97eb9738fba13df3bbfe4a to your computer and use it in GitHub Desktop.
raspi-reset
#!/bin/bash
BS=64M
DISK_ID=0x7788c428
ROOT_DEV=/dev/mmcblk0
BOOTFS_BACKUP=${ROOT_DEV}p3
BOOTFS_TARGET=${ROOT_DEV}p1
ROOTFS_BACKUP=${ROOT_DEV}p2
ROOTFS_TARGET=${ROOT_DEV}p4
if [ $EUID -ne 0 ]; then
echo Must be run as root 1>&2
exit 1
fi
echo Remounting disks read-only...
echo 1 > /proc/sys/kernel/sysrq
echo u > /proc/sysrq-trigger
echo Overwritting bootfs...
dd if=$BOOTFS_BACKUP of=$BOOTFS_TARGET bs=$BS status=progress
echo Overwritting rootfs...
dd if=$ROOTFS_BACKUP of=$ROOTFS_TARGET bs=$BS status=progress
echo Restoring original disk id...
sfdisk --disk-id $ROOT_DEV $DISK_ID
# echo Checking and resizing filesystem...
# e2fsck -f $ROOTFS_TARGET
# resize2fs $ROOTFS_TARGET
echo Done! Rebooting
sleep 1
echo b > /proc/sysrq-trigger
@lilianmallardeau
Copy link
Author

How to remotely reflash a Raspberry Pi

  • Install an OS on your SD card with the Raspberry Pi Imager

  • Before booting the Raspberry Pi with the SD card, open the card with GParted or another partition editor. The partition schema should look like this:

Partition schema of the SD card just after flashing it
  • Copy the bootfs partition, by right-clicking on it, then paste it in the beginning of the free space on the right

  • Do the same for the rootfs partition, and apply the changes. The resulting partition schema should look like this:

Partition schema of the SD card after having copied the partitions

The partitions must be numbered in this order: partition 3 should be the cloned bootfs, and partition 4 the cloned rootfs.

  • Mount both bootfs partitions, and in the cmdline.txt file on each partition, change root=PARTUUID=xxxxxxxx-02 to root=PARTUUID=xxxxxxxx-04. The idea here is that we'll use the partition 4 for the actual system root, and use partitions 2 and 3 as a backup that we'll restore to partitions 1 and 4 when resetting the system. We need to use the last partition as the filesystem root, because during first boot the root partition will be extended to span the whole free space, and it won't work if it isn't the last one.

  • We also need to change /etc/fstab accordingly. Mount the two rootfs partitions, and in /etc/fstab in both partitions, change PARTUUID=xxxxxxxx-02 / to PARTUUID=xxxxxxxx-04 /.

  • Run sudo sfdisk --disk-id /dev/mmcblk0 (replace /dev/mmcblk0 with your SD card's path), and note the output value. We'll need it in the next step.

  • Mount both rootfs partitions, and copy this script to /sbin/raspi-reset in both partitions. In the script, replace the value of DISK_ID with the output of the previous command.

#!/bin/bash
BS=64M
DISK_ID=0x7788c428  # <-- Replace with the output of the sfdisk command!
ROOT_DEV=/dev/mmcblk0
BOOTFS_BACKUP=${ROOT_DEV}p3
BOOTFS_TARGET=${ROOT_DEV}p1
ROOTFS_BACKUP=${ROOT_DEV}p2
ROOTFS_TARGET=${ROOT_DEV}p4

if [ $EUID -ne 0 ]; then
    echo Must be run as root 1>&2
    exit 1
fi

echo Remounting disks read-only...
echo 1 > /proc/sys/kernel/sysrq
echo u > /proc/sysrq-trigger

echo Overwritting bootfs...
dd if=$BOOTFS_BACKUP of=$BOOTFS_TARGET bs=$BS status=progress

echo Overwritting rootfs...
dd if=$ROOTFS_BACKUP of=$ROOTFS_TARGET bs=$BS status=progress

echo Restoring original disk id...
sfdisk --disk-id $ROOT_DEV $DISK_ID

# echo Checking and resizing filesystem...
# e2fsck -f $ROOTFS_TARGET
# resize2fs $ROOTFS_TARGET

echo Done! Rebooting
sleep 1
echo b > /proc/sysrq-trigger

Now, you can use the SD card with your Raspberry Pi. When you want to reset the system, just run sudo raspi-reset! It may be a good idea to run it inside a screen or tmux session, so in case your internet connection goes down during the process, your Raspberry Pi isn't left in an unusable state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment