How did I discover and recover USB key disk geometry (on Linux)

It's relatively easy to screw the partition table on a USB key. I happened to have a quality 16GB USB key that got it all wrong (due to some of my experiments in the past 🙂 - it was presenting itself as 250G drive. fdisk, sfdisk and hdparm all showed wrong disc geometry. I'd be happy to possess a little jewelry of 250GB, but I am afraid we'll have to wait a year or two for these to come. Sure I'd have problems should I try to use it as is for anything...Getting it all right turned out to be very simple - once I found what to do.
Let's make it easier and become root (I used Ubuntu):

sudo -i

Plug the USB thumb in question to a USB port.
Reveal the number of blocks - I hope it works for you same way it did in my case:

dmesg | less

Hit G to get to the bottom of dmesg output. We are interested in the number highlighted below:

[ 2399.378928] usb 1-1: new high speed USB device number 5 using ehci_hcd
[ 2399.516934] scsi11 : usb-storage 1-1:1.0
[ 2400.539223] scsi 11:0:0:0: Direct-Access     Kingston DT 101 G2        PMAP PQ: 0 ANSI: 0 CCS
[ 2400.572076] sd 11:0:0:0: Attached scsi generic sg5 type 0
[ 2402.145610] sd 11:0:0:0: [sdd]31342592 512-byte logical blocks: (16.0 GB/14.9 GiB)

Update: TESTED on 7 different USB-key drives OK. On Ubuntu 11.04 driver always showed the number of 512-bytes blocks correctly.

Let's do little math now:

#_of_cylinders = #_of_512b_blocks / sectors_per_cylinder / #_of_heads

Number of cylinders must be rounded down.

Here we have some flexibility. I tried 255 heads with 63 sectors per track and 32 sectors per track. The latter one seems to be preferred in some special cases. As a bonus it yielded better use of that particular device - less blocks left unused due to the necessary rounding mentioned above. Thus I ended with 3841/255/32 C/H/S geometry.

sfdisk did the rest:

sfdisk -f -C3841 -H255 -S32 /dev/sdX

Hit enter till you get a confirmation question, say Y to it so it writes the new partition table.

Now your USB key is ready to be (re)formatted using your favorite tools.

Leave a Reply

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