Tutorial: Repairing Corrupted SD Cards for the Raspberry Pi on Mac

Tutorial: Repairing Corrupted SD Cards for the Raspberry Pi on Macbrokensd

Have you ever had your Raspberry Pi stopped booting up?   The red and the yellow lights flash a few times and then quit?   These are classic symptoms of a corrupted SD Card.

This posting will walk through how to fix a corrupted SD Card for your Raspberry Pi using a Mac and VirtualBox.   This is a fairly complicated procedure, but most of it only has to be done once and then you can fix corrupted cards quickly and efficiently.

Why do SD Cards get Corrupted?

When the SD Card is being written, there are windows of vulnerability.  There seem to be four main scenarios during which SD Cards become corrupted:

  1. Powering your Raspberry Pi down without doing a “sudo halt“.   Do this enough, you will corrupt your card.
  2. Poor quality SD Cards.   Fake SanDisk cards especially.
  3. Marginal power supplies.  Watch the Red LED on newer Raspberry Pi’s.   If it is blinking, there are issues.   Poor power quality from the mains can also screw up your Raspberry Pi
  4. Overclocking your Pi.   If you push it too far, things start not quite working correctly.

OK, what can I do with a Corrupted SD Card?

1562-00The first thing you can do is BEFORE you get corrupted, back up your SD Card on a regular basis.   If you spend hours configuring an SD Card, then back it up.   Changing your software?   Back it up.   It will save you a lot of time if you end up corrupting or destroying the SD Card.  Here is how to do it on a Mac.

Sometimes, the SD Card is actually damaged.  In that case, you aren’t going to be able to fix the card.  However, you can get your files off the SD Card in many cases.  Check out this posting.

Fixing a Corrupted SD Card

Overall, this is the procedure for fixing a corrupted SD Card using a Mac.   The process is similar for a PC, but requires different instructions to install and get VirtualBox working.

  1. Back up your SD Card, if possible.    Here is how to do it on a Mac.
  2. Install VirtualBox on your Mac
  3. Install Ubuntu in Virtual Box
  4. Attach SD Card to Ubuntu
  5. Run “fsck” to repair the SD Card

 

Install VirtualBox on your Mac

VirtualBox is a “virtualizer” which means that it allows you to install an operating system in a “Box” inside another operating system.  It is open source and free to use.   In this case we will be installing Linux (Ubuntu) on a Mac OS X platform.

Step 1: Download the VirtualBox .dmg file for the Mac OS X  from this page:

Step 2: Click on the downloaded .dmg file.

Step 3: Double click on the VirtualBox.pkg icon

Screen Shot 2016-01-25 at 7.52.07 AM

 

 

 

 

 

 

 

Step 4: Follow the instructions to install VirtualBox in the applications directory.

Step 5: Now download the installation file (.iso) for Ubuntu from this link:    Note: Make sure you use this link – ubuntu-1510-desktop-i386.iso.  If this link isn’t available, go to https://mirror.pnl.gov/releases/ and find the latest release.   The default download link on Ubuntu server will download an amd64 image, which WILL NOT WORK on your Mac OS X.

Step 6: Start up VirtualBox from Applications

Step 7: Click on “New”

Screen Shot 2016-01-25 at 8.07.00 AM

 

 

 

 

 

 

 

Step 8: Enter “Ubuntu” as the name.  Linux and 64 bit will be automatically selected.  Click Continue.

Screen Shot 2016-01-25 at 8.11.16 AM

 

 

 

 

 

 

 

Step 9: Click on continue until you are done, using all the defaults.

Step 10: Click on settings and then on Storage and then on Controller: SATA

 

Step 11: Enter 2 in port count!   This is an important step not to miss!  Click “OK” to close

Screen Shot 2016-01-25 at 8.13.21 AM

 

 

 

 

 

 

 

Step 12: Ready to install Ubuntu now!   Click the Start Button

 

Step 13:  When you are promoted to select an image, select the ubuntu .iso image downloaded in Step 5.

Screen Shot 2016-01-25 at 8.24.51 AM

 

 

 

 

 

 

 

Step 14:  Select “Install Ubuntu” from the screen, then “Continue” from the next screen and then keeping the defaults, finally “Install Now”.

Screen Shot 2016-01-25 at 8.27.36 AM

 

 

 

 

 

 

 

Step 15: Click “Continue” when asked to erase the virtual disk.  Don’t worry, this is not your Mac OS X disk.

Step 16: Select your Timezone.  Hit “Continue”

Screen Shot 2016-01-25 at 8.30.14 AM

 

 

 

 

 

 

 

Step 17: Put in your name and choose a password.   Make sure you can remember it! Now hit “Continue” and you are installing Ubuntu.

Screen Shot 2016-01-25 at 8.32.07 AM

 

 

 

 

 

 

 

Step 18: Now click “Restart Now” when the installation is finished. If Ubuntu does not start (which happens often the first time), then hit the “x” in the corner and select “Power Off the Machine”.

Screen Shot 2016-01-25 at 8.52.07 AM

 

 

 

 

 

 

 

Screen Shot 2016-01-25 at 8.56.18 AM

 

Step 19: Now you have your Ubuntu Virtual Machine running.  Click the “X’ on the window and select “Send the shutdown signal”.  Then click “Shutdown”

 

 

 

 

 

 

Adding the SD Card to your Ubuntu Virtual Machine

In order to mount and fix your corrupted SD Card, you need to add it to the Ubuntu virtual machine.  This is the complicated part of this procedure.

Step 1:  Insert your SD Card into the reader on your Mac OS X machine.  Below is on the Mac Mini.   On a Mac Book, it will be on the side.

IMG_7116

 

 

 

 

 

 

 

Step 2: Open up a terminal window and type:

diskutil list

Step 3: You will see a list similar to this.  Look for your SD card and note the /dev path (/dev/disk4 in this case)

Screen Shot 2016-01-25 at 9.06.18 AM

 

 

 

 

 

 

Step 4: In the terminal window type (replacing /dev/diskX with what you found in Step  3):

diskutil unmountDisk /dev/diskX

Screen Shot 2016-01-25 at 9.11.15 AM

 

 

 

 

 

Step 5: change directory in the terminal window to the “VirtualBox VMs”  on our development machine this command is:

cd "/Users/development/VirtualBox VMs"

Note the quotes around the directory name.  Adding spaces in directory names and filenames causes all sorts of issues.   The user on our machine is “development”.   The user name on your machine will be different.

Step 6: Now we come to the magic.  Type the following (replacing diskX with what you found in Step 3):

sudo VBoxManage internalcommands createrawvmdk -filename ./sd-card.vmdk -rawdisk /dev/diskX

Step 7:   Now we have an .vmdk file pointing to your raw SD Card device.   We next have to set the permissions on this file so your Ubuntu VirtualBox machine can read the file.

sudo chmod 777 /dev/diskX
sudo chmod 777 ./sd-card.vmdk

Screen Shot 2016-01-25 at 9.16.24 AM

 

 

 

 

 

 

Step 8:  Next we have to add the SD Card (a SATA device) in the Ubuntu virtual machine storage configuration built in the previous section.

Step 9: Click on Settings in VirtualBox and select Storage.  If port count is “1” change it to “2”

Screen Shot 2016-01-25 at 9.21.23 AM

 

 

 

 

 

 

 

Step 10:  Click on the icon on the right of the Controller: SATA disk and select “Choose existing disk”.   Select the .vmdk file built in Step 6 above.   If it reports that it is busy (Mac OS X likes to remount things for some reason), repeat Step 4 and quickly do Step 10 again.

Step 11:  Click OK

Step 12: Start your Ubuntu Virtual machine with the start button.

Now finally, we can repair the SD Card.

Repairing the SD Card

Step 1: Select the top most icon on the Ubuntu Desktop.  Type “Terminal” in the search line.

Screen Shot 2016-01-25 at 11.32.48 AM

 

 

 

 

 

 

 

Step 2: Select the Terminal Icon on the top line.  You will get a terminal window.

Step 3: type

sudo lsblk -l

into the terminal window (you may have to enter your password from Step 17 in the section above)

Screen Shot 2016-01-25 at 9.49.41 AM

 

 

 

 

 

 

Step 4: Note the name “/dev/sdb”.   That is your SD Card on the Ubuntu system.   Note it may be different, but if you followed these directions, it should be the same.

Step 5:  Now the magic.  Finally.  We will fix the sdb2 partition on sdb.   That is the linux system.  Type the following in the terminal window:

sudo fsck -fy /dev/sdb2

This may take a while, depending on the speed of your machine.  Note that the following picture shows you what you get on a clean, good SD Card.   If you have issues, it will show you what it is doing.

Screen Shot 2016-01-25 at 9.56.42 AM

 

rasberry-pi-b_-with-sd-cardConclusion

Now you should have a repaired Raspberry Pi SD Card.   If it doesn’t work, you may have bigger issues.  Start by looking at more “fsck repair” pages on the web.   If the card is bad, look at mounting the SD Card under your Ubuntu Virtual machine and going into the file system to try to recover your most important files.

The next time you need to repair an SD Card, you should only have to plug it into your Mac OS X and then start Ubuntu from VirtualBox.  Note that the image you built for the SD Card repair probably won’t boot without an SD Card plugged in.  If you want to use Ubuntu without the SD Card, build another image in VirtualBox.   You don’t have to delete the SD Card Ubuntu image.

 

2 Comments

  1. Hi.
    All this stuff is redundant, because fsck is bundled with OS X. All you need is to open Terminal.app, use `diskutil list` to find out what’s your device and run
    sudo fsck -fy /dev/

    • We have been unable to get fsck to work correctly on a Raspberry Pi SD Card with OS X El Capitan.

      Developments-Mac-mini:~ development$ fsck -fy /dev/disk4
      fsck usage: fsck [-fdnypq] [-l number]
      Developments-Mac-mini:~ development$

      Doesn’t work. That would be great, however, if it did! Lots less work.

      SDL

Comments are closed.