benni.tech

How to repair a Raspberry Pi disk image

2023-02-22

Issue: the Raspberry Pi (RPi) has crashed and the boot process ends up in a kernel panic. The reason is unclear.

Connect the serial output from the RPi to an FTDI adapter to see the output. The exact model doesn't matter; a cheap one is fine. Wire the pins as follows:

FTDI PinRPi PinRPi Pin Name
GND06Ground
TX (D0)08TX (GPIO14)
RX (D1)10RX (GPIO15)

References for serial setup

Configure serial boot

Set /boot/cmdline.txt:

console=serial0,115200 console=tty1 earlycon=pl011,mmio32,0x3f201000 root=PARTUUID=6c586e13-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

Add to /boot/config.txt to disable the mini UART and enable the real UART:

dtoverlay=disable-bt

Reference: Serial port setup in Raspberry Pi OS

Read the boot output

Boot the RPi and open the serial line with minicom (or screen):

minicom -b 115200 -D /dev/ttyUSB0

If you see a random kernel panic, rescue the data from the SD card:

dd if=/dev/sde of=sde-card-copy.img bs=4096 conv=notrunc,noerror status=progress

For damaged media, prefer ddrescue:

ddrescue -n -r 3 /dev/sdc sdc-card-copy.img mapfile

References for disk backup