Recover Raspberry Pi From Corrupt kernal.img


This issue seems to happen often on the rpicam device in the kitchen. The green light will flash 7 times on boot when the kernal.img is corrupt from the device not being shut down cleanly. I found these instructions to recover the sd card in this case. It involves booting the pi with a new sd card loaded with raspbian, put the corrupt sd card in a USB adapter, find the corrupt devices in /dev/* (found mine in /dev/sda6 and /dev/sda7).


This is based on the post from wvk here: https://forums.raspberrypi.com/viewtopic.php?t=178357

First I found the dev paths to /boot and /root on the currupt card
Before inserting the usb adapter with corrupt card:
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 59G 11G 46G 19% /
devtmpfs 333M 0 333M 0% /dev
tmpfs 462M 0 462M 0% /dev/shm
tmpfs 185M 816K 184M 1% /run
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/mmcblk0p1 253M 49M 204M 20% /boot
tmpfs 93M 24K 93M 1% /run/user/1000

After the usb adapter is plugged in:
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 59G 11G 46G 19% /
devtmpfs 333M 0 333M 0% /dev
tmpfs 462M 0 462M 0% /dev/shm
tmpfs 185M 852K 184M 1% /run
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/mmcblk0p1 253M 49M 204M 20% /boot
tmpfs 93M 24K 93M 1% /run/user/1000
/dev/sda6 253M 22M 231M 9% /media/pi/boot
/dev/sda7 12G 9.7G 1.3G 89% /media/pi/root

So, these are the corrupt sd cart partitions that we need to work with:
/dev/sda6 253M 22M 231M 9% /media/pi/boot
/dev/sda7 12G 9.7G 1.3G 89% /media/pi/root

Now, we run the script to do an "offline" firmware upgrade which replaces your corrupted version
$sudo mkdir -p /mnt/rescue/{boot,root}
$sudo mount /dev/sda6 /mnt/rescue/boot
$sudo mount /dev/sda7 /mnt/rescue/root

$sudo ROOT_PATH=/mnt/rescue/root BOOT_PATH=/mnt/rescue/boot SKIP_BACKUP=1 rpi-update
*** Raspberry Pi firmware updater by Hexxeh, enhanced by AndrewS and Dom
*** Performing self-update
*** Relaunching after update
*** Raspberry Pi firmware updater by Hexxeh, enhanced by AndrewS and Dom
*** We're running for the first time
#############################################################
WARNING: This update bumps to rpi-5.10.y linux tree
See: https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=288234
'rpi-update' should only be used if there is a specific
reason to do so - for example, a request by a Raspberry Pi
engineer or if you want to help the testing effort
and are comfortable with restoring if there are regressions.

$umount /mnt/rescue/{boot,root}

Finally, shutdown the pi, swap the sd cards, and boot. I did notice the pi rebooted once or twice after the first boot on the fixed card.

I hope this helps you as much as it helped me. Thanks to wvk on raspberrypi forums for his excellent suggestion!

Comments

Popular Posts