Tips and Tricks Uncategorized

Format An Unformattable Flash Drive In OS X

It’s always satisfying to fix something that seems impossibly broken.  I recently fixed a broken flash drive that could not be reformatted.  I first tried to repair the disk using Disk Utility, but that option was completely greyed out (same thing in the Recovery HD).  So then I tried to erase it but was presented with the error “OS X can’t repair the disk “MYDISK”.”


Next, I tried to partition the disk and received the error message “Disk Erase failed: Disk Erase failed with the error: Unable to write to the last block of the device.”


After some searching, I came across this helpful post.  Here are the steps I used to fix it:

Find The Disk Device File Associated With The Drive

First, you need to find the device file that corresponds with your flash drive.  Run this command:

diskutil list

You should see some entries like /dev/disk0 , or /dev/disk1 .  Take note of the one that is assigned to your flash drive.  It will be used in the next steps.

Format The Drive Before The System Completely Grabs Onto It

This is the weird part.  You will be using this command (make sure to substitute your device file if it is different) to write random data to the flash drive (which is essentially what reformatting does anyway):

First, become root:

sudo su

Repeatedly run this command (using the up arrow to access it again), while plugging in your flash drive until the command prompt disappears (meaning it is writing to your flash drive).  If you get the resource busy error.  Unplug the drive and try again.  You just have to get the command to run just as the flash drive is plugged in.

cat /dev/random > /dev/disk1

Take a look here for the full instructions and details as they explain everything very well.

28 replies on “Format An Unformattable Flash Drive In OS X”

It would be faster to do the wipe using /dev/urandom. Or if you want real fast speed and don’t care about forensics, /dev/zero. I personally use the following.
# sudo dd if=/dev/zero of=/dev/rdisk1 bs=1m
“rdisk” is faster then disk

I can surmise that /dev/zero just writes a bunch of zeros to the disk, but what is the difference between /dev/urandom and /dev/random that makes it so much faster?

/dev/random will block after the entropy
pool is exhausted. It will remain blocked until additional data has
been collected from the sources of entropy that are available. This can
slow down random data generation.

/dev/urandom will not block. Instead it will reuse the internal pool to produce more pseudo-random bits.

/dev/urandom is best used when:

-You just want a large file with random data for some kind of testing.

-You are using the dd command to wipe data off a disk by replacing it with random data.

-Almost everywhere else where you don’t have a really good reason to use /dev/random instead.

/dev/random is likely to be the better choice when:

-Randomness is critical to the security of cryptography in your application – one-time pads, key generation.

guys im young, and i do not understand what all of this complicated stuff means, can someone please explain how avoid this error in an easier way please.

Hmm. I remember when I first did this, it took a while to get the timing just right. But I’m glad you got it to go.

before my computer recognizes the drive, it says “Operation not supported.” So I keep repeating the code hoping I can catch it at that right time. Instead, once my computer recognizes my drive, I get the message “No such file or directory.” What am I doing wrong?

It’s probably a timing issue and there is no guarantee it will work depending on the level of corruption. Try again to run the command at just the right time.

i got permission denied. i already done those things. please help!!!! it’s flashdisk not flashdrive. i cannot do from diskutility

Ha! I had the same reaction when it worked for me, too. I figured it was worth a blog post. I’m glad it helped someone!

Its been more than 12 hours and my mac is still formatting my 32gig USB drive with /dev/random This is not sparta, this is Madness.

Have the same problem.

Got to the point where the command got to the disk before the system, but when I ran the disk utility it still couldn’t erase or restore the disk.

Hardware problem already, you think?

Thanks for the post, dude BTW.

hey please be clear i’m basic i’ve never used terminal, i don’t know how to begin with terminal
so i opened terminal and i typed # sudo dd if=/dev/zero of=/dev/rdisk2 bs=1m
nothing happened.

how does it work can you explain a bit properly

I think the post is clear about what is happening. It’s writing random data to the disk in an effort to overwrite any corrupt bits. This is a challenging trick to pull off sometimes and really just a last-ditch-effort.

it seemed to work for a while, the command prompt was empty, i was sure it worked. but then after a few minutes i got “cat: stdout” Input/output error”. what did i do wrong?

Leave a Reply