Friday, March 16, 2012

Partitioning a 2TB disk with 4K sectors with Linux

How I got the logical and physical sectors to align properly on a new 2TB disk with 4K sectors.  Posting for posterity.

When adding a new disk to my workstation recently I encountered a warning from the GNOME disk utility about the sectors being misaligned and there was little that I could change in the way of settings in the utility to fix the problem.  So I went to Google and through my searching this is what I learned and what ended up working.

The disk I was replacing was an older 1.5TB Seagate drive (ST31500341AS) and the new 2TB Seagate drive (ST2000DM001) had 4K physical sectors.  Blah blah blah...  If the logical and physical sectors don't align you might have to request two large physical sectors (8K of data) for the smaller logical sector (512-bytes of data).  This would be a big performance hit.  According the to Segate website both the old and the new drives had something called SmartAlign that was supposed to handle all of this extra work for me.

I don't have a lot of time to monkey around with this right now so I was looking for a quick fix, and I found it on an IBM site of all places.  To make this quick clean off any of the misaligned partitions in the disk utility (take note of the device name and such from here if you have it) and then fire up the terminal.

Start up fdisk as follows replacing /dev/sdb with the actual device.

fdisk -H 224 -S 56 /dev/sdb

According to the IBM article, this "changes the cylinder/head/sector (CHS) geometry to guarantee proper 4096-byte alignment when the program aligns to cylinders..."

Now we create the partition like so.


gavaletz@gavaletz:~$ sudo fdisk -H 224 -S 56 /dev/sdb


The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.


Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
p
First cylinder (1-311465, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-311465, default 311465):
Using default value 311465


Command (m for help): t
Partition number (1-5): 1
Hex code (type L to list codes): 83
Command (m for help): w
The partition table has been altered!


Calling ioctl() to re-read partition table.
Syncing disks.


Now you can go back to the disk utility and format the partition as you normally would.  This time you should not get the warning that you did before.  For adding the disk to the file system have a look at this article.