Created
October 30, 2023 00:47
-
-
Save lilianmallardeau/04368e95bc97eb9738fba13df3bbfe4a to your computer and use it in GitHub Desktop.
raspi-reset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Copy the
bootfs
partition, by right-clicking on it, then paste it in the beginning of the free space on the rightDo the same for the
rootfs
partition, and apply the changes. The resulting partition schema should look like this: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 thecmdline.txt
file on each partition, changeroot=PARTUUID=xxxxxxxx-02
toroot=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 tworootfs
partitions, and in/etc/fstab
in both partitions, changePARTUUID=xxxxxxxx-02 /
toPARTUUID=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 ofDISK_ID
with the output of the previous command.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 ascreen
ortmux
session, so in case your internet connection goes down during the process, your Raspberry Pi isn't left in an unusable state.