[Desire Android Development] sibere:
So.. You've plugged your SD to your linux box and want to created aligned partitions with fdisk?

Follow exactly what I'm doing and your partitions will be MB aligned.

At the end you'll get a FAT32 partition for your data and an ext4 partition for data2xxxx scripts.

If you need more partitions, just adapt this tutorial to you needs.

I know some scripts or recoveries do this automatically so no need to remind me ;)

I initially posted this in SuperNova ROM thread but it might worth a separate thread.

I've done this with an old 1GB usb key just to show you the commands, you'll have to adapt to your SD depending on it's size.

When I plugged the flash drive, it got recognized by my system as /dev/sde. It can be different on your system and once again you'll have to figure-out yourself the correct device.

Here as an example I'm splitting the flash drive in 2 partitions. 1st (512Mb) will be for the fat32 partition and will have to be formatted later. The rest is the future ext4 and is about 512MB too.

I'm gonna use 2 magic parameters with fdisk, in order to get 1024Kb clusters (2048x512 bytes) and align easily the partitions. 64 heads and 32 sectors should give 64x32=2048 so that is going to be my params here :)

fdisk -H64 -S32 /dev/sde

Command (m for help): p

Disk /dev/sde: 1054 MB, 1054605312 bytes
64 heads, 32 sectors/track, 1005 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk identifier: 0x00000000

Device Boot Start End Blocks Id System
/dev/sde1 * 1 1005 1028043+ 6 FAT16
Partition 1 has different physical/logical beginnings (non-Linux?):
phys=(0, 3, 45) logical=(0, 7, 10)
Partition 1 has different physical/logical endings:
phys=(509, 63, 63) logical=(1004, 3, 32)
Partition 1 does not end on cylinder boundary.

So here I have an old partition on my key (sde1). I delete it first and check that there is no more partition on the drive.

Repeat the process if you have more than 1 partition on your SD.

Make sure that it's clean before proceeding to the next step.

Command (m for help): d
Selected partition 1

Command (m for help): p

Disk /dev/sde: 1054 MB, 1054605312 bytes
64 heads, 32 sectors/track, 1005 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk identifier: 0x00000000

Device Boot Start End Blocks Id System

Empty! Great! The key is clean. Let's partition it now

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1005, default 1): 2
Last cylinder or +size or +sizeM or +sizeK (2-1005, default 1005): 512

Command (m for help): p

Disk /dev/sde: 1054 MB, 1054605312 bytes
64 heads, 32 sectors/track, 1005 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk identifier: 0x00000000

Device Boot Start End Blocks Id System
/dev/sde1 2 512 523264 83 Linux

Ok now I have a partition starting on cylinder 2 and 512Mb in size. If your SD is 16GB, you can use for instance a size of 15GB to leave 1GB for the ext4 partition.

That would give approximately 15x1024=15360 instead of 512. Got it?

Pay attention to the value below "End". Here it is 512 so I will use the next cylinder (513) for the 2nd partition:

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (1-1005, default 1): 513
Last cylinder or +size or +sizeM or +sizeK (513-1005, default 1005):
Using default value 1005

Command (m for help): p

Disk /dev/sde: 1054 MB, 1054605312 bytes
64 heads, 32 sectors/track, 1005 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk identifier: 0x00000000

Device Boot Start End Blocks Id System
/dev/sde1 2 512 523264 83 Linux
/dev/sde2 513 1005 504832 83 Linux

Now I have 2 partitions aligned to 1MB but declared as "linux". Let's change that to FAT32 for partition 1.

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): c
Changed system type of partition 1 to c (W95 FAT32 (LBA))

Command (m for help): p

Disk /dev/sde: 1054 MB, 1054605312 bytes
64 heads, 32 sectors/track, 1005 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Disk identifier: 0x00000000

Device Boot Start End Blocks Id System
/dev/sde1 2 512 523264 c W95 FAT32 (LBA)
/dev/sde2 513 1005 504832 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.
Syncing disks

That's all.

Now format 1st partition to FAT32 with this guide:

credits to kd8cgo

Making an aligned FAT32 format

Anyone can figure out alignment of the partition from many sources already posted on the web.
One thing I could not find was a guide on how to align that pesky FAT32 format so that each
cluster (allocation unit) is aligned inside the boundaries of erase blocks. Well, the Linux mkfs.vfat
utility has all the options we need to make this aligned format possible!

Please view this primer to FAT32 to get an idea of how the system is laid out on your disk.
You normally have the first sector in the partition which is your Volume ID, which sits in the
reserved space which is normally the first 32 sectors in the partition. This is followed
by (2) copies of the File Allocation Tables, which vary in length when created depending on
a variety of factors, including the chosen allocation unit size. The FAT size remains constant
after creation. Please keep in mind for this discussion the physical size of a sector, which is 512 bytes.

The Golden Nugget
We will be changing the reserved sectors from the default value of 32, to a number that
we will calculate from the reported size of the FAT tables after formatting. The goal will be to
make the FAT tables end right at a 128K boundary, so each cluster of the file system will fall
neatly within erase blocks on our disk!

First we will format our disk using FAT32 paying no mind to reserved sectors. This will
report to us our FAT size as so:

user@host:~$ sudo mkfs.vfat -F 32 -n UserData -s 32 -v /dev/sde1
mkfs.vfat 3.0.7 (24 Dec 2009)
/dev/sdd1 has 224 heads and 56 sectors per track,
logical sector size is 512,
using 0xf8 media descriptor, with 31309312 sectors;
file system has 2 32-bit FATs and 32 sectors per cluster.
FAT size is 7641 sectors, and provides 977937 clusters.
Volume ID is 40c250bd, volume label UserData .

The juicy bits are that 2nd to last line, it tells you the size on disk of 1 FAT table.

7641 sector FAT x 2 FATs x 512 bytes/sector = 7,824,384 bytes

The above formula shows you the exact amount of space the FAT tables are using at
the beginning of your disk. This number is not usually going to be evenly divisible by
128K (131,072 bytes) as you can see 7,824,384 / 131072 = 59.695 erase block sized
chunks. What we need to do is force the end of those FAT tables to end right at
60 blocks to do so we:

131,072 x 60 = 7,864,320 bytes in 60 erase blocks

7,864,320 - 7,824,384 = 39,936 bytes remainder

39,936 / 512 = 78 sectors remain

New reserved sector count for alignment = 78

Those are all the fundamentals required to align a FAT32 partition, so that
clusters on the disk fall in line with the erase blocks of the physical cell medium.

An example of the format command required:

sudo mkfs.vfat -F 32 -R 78 -n UserData -s 32 -v /dev/sde1

Breakdown:

sudo - super user privledges
mkfs.vfat - create a FAT file system
-F 32 - 32 bit FAT (FAT32)
-R 78 - Use 78 Reserved Sectors (instead of 32)
-n UserData - drive label, up to 11 characters
-s 32 - 32 sectors per cluster 32 x 512 - 16K allocation unit size
-v /dev/sdex - Device to format

Note: If you're getting a R=0, use 256 (256x512bytes=128Kb) as the minimum R value for FAT32 is 2.

Format 2nd partition with the following command, replacing xxx with your device

mke2fs -T ext4 -b 4096 -E stride=64,stripe-width=64,hash_alg=tea -O extent,^huge_file -m 0 -L userdata /dev/xxx2

You should be good to go now!