Fixing a RaspberryPi raspian SD Card

Recent­ly I ran into an issue with my Rasp­ber­ry Pi. It was not acces­si­ble over the net­work and the HDMI out­put showed no sig­nal, too. The pow­er LED on the RPi was lit, how­ev­er the green LED show­ing SD card activ­i­ty was not blink­ing when pow­er was attached.

On my RPi I was run­ning the Debian var­i­an Raspi­an how­ev­er it failed to boot. So I fired up a Debian PC and looked into the SD card. On the SD card there are two par­ti­tions as shown by fdisk:

Disk /dev/sdg: 15.8 GB, 15811476992 bytes
64 heads, 32 sectors/track, 15078 cylinders, total 30881791 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000c7b31
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdg1            8192      122879       57344    c  W95 FAT32 (LBA)
/dev/sdg2          122880    30881791    15379456   83  Linux

First, a small 8 MB FAT32 par­ti­tion which stores the boot­loader and the ker­nel image and then a larg­er ext4 par­ti­tion which is expand­ed on instal­la­tion to fill up the remain­ing space on the SD card (mine was 16 GB large).

Fine, so let us try to mount the sec­ond par­ti­tion to recov­er some data: mount -t ext4 -o ro /dev/sdg2 /media/sd but with­out luck, this was the output:

mount: wrong fs type, bad option, bad superblock on /dev/sdg2,
       missing codepage or helper program, or other error
       In some cases useful info is found in syslog - try
       dmesg | tail  or so

I did was I was told to do and checked the ker­nel logs using dmesg | tail. The last entry showed some­thing interesting:

[  707.493811] EXT4-fs (sdg2): bad geometry: block count 3844864 exceeds size of device (3844863 blocks)

Hmm, some­thing is wrong with the par­ti­tion, so let us run good old fsck /dev/sdg2 but this rose an error, too:

fsck from util-linux 2.20.1
e2fsck 1.42.5 (29-Jul-2012)
/dev/sdg2: recovering journal
The filesystem size (according to the superblock) is 3844864 blocks
The physical size of the device is 3844863 blocks
Either the superblock or the partition table is likely to be corrupt!
Abort? yes

So the par­ti­tion is cor­rupt, or at least the super block as it reports the wrong size. So maybe we can resize the par­ti­tion instead using resize2fs /dev/sdg2 which gave the promis­ing output:

resize2fs 1.42.5 (29-Jul-2012)
Resizing the filesystem on /dev/sdg2 to 3844863 (4k) blocks.
The filesystem on /dev/sdg2 is now 3844863 blocks long.

After this I was able to mount the par­ti­tion using mount -o ro /dev/sdg2 /media/sd. I unmount­ed it and reran fsck /dev/sdg2 on it which found some orphaned nodes and repaired it. I could then rein­sert the card into my RPi and it boot­ed and worked!

Please note: Your milage may vary as it can­not be made sure that data integri­ty is still OK. This would be pos­si­ble with check­summed file sys­tems like ZFS or but­trfs. It is always a good idea to have a backup.

Thomas

Chemist, Programmer, Mac and iPhone enthusiast. Likes coding in Python, Objective-C and other languages.

3 thoughts to “Fixing a RaspberryPi raspian SD Card”

  1. Thanks so much! This helped get my school’s weath­er cam­eras back online after some water got into their pow­er supplies.

  2. Much much thanks. You made my day. After I bought a new sd card I had this prob­lem. Now I fol­lowed your sug­ges­tions and every­this­ng runs fine.

Leave a Reply to Bryan Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.