Last active
October 16, 2024 13:16
-
-
Save yayuniversal/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 | |
ROOT_DEV=/dev/mmcblk0 | |
BOOTFS_BACKUP=${ROOT_DEV}p3 | |
BOOTFS_TARGET=${ROOT_DEV}p1 | |
ROOTFS_BACKUP=${ROOT_DEV}p2 | |
ROOTFS_TARGET=${ROOT_DEV}p4 | |
print_yellow() { | |
echo -e "\033[1;33m${1}\033[0m" | |
} | |
print_blue() { | |
echo -e "\033[1;34m${1}\033[0m" | |
} | |
if [ $EUID -ne 0 ]; then | |
echo "Must be run as root" 1>&2 | |
exit 1 | |
fi | |
if [ ! -f /etc/rpi-disk-id ]; then | |
echo "/etc/rpi-disk-id not found" 1>&2 | |
exit 2 | |
fi | |
DISK_ID="$(cat /etc/rpi-disk-id)" | |
print_yellow "Remounting disks read-only..." | |
echo 1 > /proc/sys/kernel/sysrq | |
echo u > /proc/sysrq-trigger | |
print_yellow "\nOverwritting bootfs..." | |
dd if=$BOOTFS_BACKUP of=$BOOTFS_TARGET bs=$BS status=progress | |
print_yellow "\nOverwritting rootfs..." | |
dd if=$ROOTFS_BACKUP of=$ROOTFS_TARGET bs=$BS status=progress | |
print_yellow "\nRestoring original disk id..." | |
sfdisk --disk-id $ROOT_DEV $DISK_ID | |
# print_yellow "\nChecking and resizing filesystem..." | |
# e2fsck -f $ROOTFS_TARGET | |
# resize2fs $ROOTFS_TARGET | |
print_blue "\nDone! 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
When flashing an SD card with the Raspberry Pi Imager, it will create two partitions :
bootfs
androotfs
. We will make backups of these partitions on the SD card, so we can restore them when needed (so we will end up with 4 partitions instead of 2).Install an OS on your SD card with the Raspberry Pi Imager. Do not boot the Raspberry with the SD card yet!
Mount the
bootfs
partition. In thecmdline.txt
file, 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.When done, unmount the partition.
Mount the
rootfs
partition, and in/etc/fstab
, changePARTUUID=xxxxxxxx-02 /
toPARTUUID=xxxxxxxx-04 /
.From the root of the
rootfs
partition, runsudo sfdisk --disk-id /dev/mmcblk0 > etc/rpi-disk-id
(change/dev/mmcblk0
to the SD card's block device).Download the
raspi-reset
script (in the gist) tousr/local/sbin/
in therootfs
partition, and give it execute permission (chmod +x raspi-reset
).When done unmount the
rootfs
partition.From here, both partitions should be unmounted.
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: partitions 1 and 2 should be the original bootfs and rootfs, partition 3 should be the cloned bootfs, and partition 4 the cloned rootfs.
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.