Skip to content

Instantly share code, notes, and snippets.

@yayuniversal
Last active December 21, 2024 04:02
Show Gist options
  • Save yayuniversal/04368e95bc97eb9738fba13df3bbfe4a to your computer and use it in GitHub Desktop.
Save yayuniversal/04368e95bc97eb9738fba13df3bbfe4a to your computer and use it in GitHub Desktop.
raspi-reset
#!/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
@yayuniversal
Copy link
Author

raspi-reset has moved here:
https://github.com/yayuniversal/raspi-reset

Check the repo for future updates!

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