5,596,354 Members 39,108 Now Online
XDA Developers Android and Mobile Development Forum

[GUIDE] SD card partitioning for rooted phones

Tip us?
 
BlaY0
Old
(Last edited by BlaY0; 10th January 2011 at 10:32 PM.) Reason: Some corrections...
#1  
BlaY0's Avatar
Recognized Developer - OP
Thanks Meter 563
Posts: 1,552
Join Date: Sep 2007
Location: Medvode
Default [GUIDE] SD card partitioning for rooted phones

Behold... a long awaited partitioning guide

WARNING! This GUIDE is to actually learn something not just to copy/paste commands!

Quote:
Requirements
  • rooted phone
  • busybox installed
  • parted (optional)
  • backup your SD card (optional)
  • calculator
Quote:
Background

Before we begin partitioning, we need to elaborate some key points:
  1. block storage units are divided into logical blocks known as sectors
  2. sector has a size of 512 bytes
  3. NAND flash chips are divided into blocks known as erase blocks
  4. our SD cards consist of those NAND flash chips and controller
  5. erase block on our SD cards has a size of 128 kB, that's 256 sectors
  6. CHS (cylinder, head, sector) alignment has an insignificant importance here
  7. 1st sector is sector 0 (not 1) and is used as MBR (master boot record)
  8. 1st partition begins at cylinder boundary to maintain MS-DOS compatibility
  9. raw access to block storage units is done via special block device files under /dev/block directory
  10. our SD card is represented by block device file /dev/block/mmcblk0
Quote:
Instructions

Here I will provide you with two methods of partitioning. For 1st method you will be using fdisk utility which is part of busybox and for 2nd a standalone utility called parted will be used. Both methods can be used in normal mode via adb shell or some terminal app. I will explain both methods using adb shell as it is more convenient and handy than typing commands via touch keyboard on your phone.

1st thing to do before you begin is to unmount your SD card via "Settings->SD & phone storage" and then you issue "adb shell" command ony your PC. 2nd thing you will do is erasing of your SD card (actually you will erase just first few erase blocks of your SD card) using dd utility:
Code:
dd if=/dev/zero of=/dev/block/mmcblk0 bs=131072 count=16
...that will overwrite 1st 2 MB of your SD card with null characters. Next you may begin with partitioning.
Quote:
fdisk
As I already stated, fdisk is a (interactive) utility that is part of busybox so I will assume it is available under /system/xbin directory. Now you can run fdisk with device file of your SD card as parameter/argument:
Code:
fdisk /dev/block/mmcblk0
...this will bring you some notes on your screen you should not worry about and a command prompt:
Code:
Command (m for help):
...which you can leave at any time by pressing CTRL+C. Next you will change unit display type to sectors:
Code:
Command (m for help): u
Changing display/entry units to sectors
...and print your SD's current info (this is info of my SD card actually, yours may vary):
Code:
Command (m for help): p

Disk /dev/block/mmcblk0: 8018 MB, 8018460672 bytes
4 heads, 16 sectors/track, 244704 cylinders, total 15661056 sectors
Units = sectors of 1 * 512 = 512 bytes

              Device Boot      Start         End      Blocks  Id System
...and you write down the number of sectors. In my case it is 15661056 sectors of 512 bytes which is exactly 7647 MB if we divide them by 2048. For example you would take 7000 MB for fat32 1st partition and 647 MB for ext 2nd partition. and it is handy that way coz megabytes are divisible by our SD card's erase block size which is 128 kB as stated before. Calculation would give you start sector for 2nd partition and this would be 14336000 (7000*2048).

Now you need to create 2 primary partitions:
Code:
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
...now there's a catch. You will be offeread a start of 1st partition at 1st to 2nd cylinder boundary which is sector 16 in my case and you push it to SD card's erase block boundary (256):
Code:
First sector (16-15661055, default 16): 256
Last sector or +size or +sizeM or +sizeK (256-15661055, default 15661055): 14335999
...and continue to the next partition which should also be primary:
Code:
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First sector (16-15661055, default 16): 14336000
Last sector or +size or +sizeM or +sizeK (14336000-15661055, default 15661055): 15661055
...now print what you have just done:
Code:
Command (m for help): p

Disk /dev/block/mmcblk0: 8018 MB, 8018460672 bytes
4 heads, 16 sectors/track, 244704 cylinders, total 15661056 sectors
Units = sectors of 1 * 512 = 512 bytes

              Device Boot      Start         End      Blocks  Id System
/dev/block/mmcblk0p1             256    14335999     7167872  83 Linux
/dev/block/mmcblk0p2        14336000    15661055      662528  83 Linux
...it looks OK but you need to change 1st partition's hex id which needs to be fat32 (c):
Code:
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 (Win95 FAT32 (LBA))
...now you're am set, print again your configuration and write changes to SD card:
Code:
Command (m for help): p

Disk /dev/block/mmcblk0: 8018 MB, 8018460672 bytes
4 heads, 16 sectors/track, 244704 cylinders, total 15661056 sectors
Units = sectors of 1 * 512 = 512 bytes

              Device Boot      Start         End      Blocks  Id System
/dev/block/mmcblk0p1             256    14335999     7167872   c Win95 FAT32 (LBA)
/dev/block/mmcblk0p2        14336000    15661055      662528  83 Linux

Command (m for help): w
The partition table has been altered!
There's a possibility you would need to shutdown and power on again your phone at this point. Do not reboot via adb or some 3rd party app!
Quote:
parted
Parted is one of interactive partitioning utilities that can also use external formatting utilities. It can be found in some recovery images but can be copied to your internal phone storage and run from there in normal mode too. To run it you have to use your SD card's device file as a parameter/argument:
Code:
parted /dev/block/mmcblk0
...and you will be presented with an interactive shell:
Code:
GNU Parted 1.8.8.1.179-aef3
Using /dev/block/mmcblk0
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
I probably shouldn't mention that there's an interactive help available and that it is invoked by issuing "help" into shell's command prompt. Next thing to do is making a MS-DOS disklabel:
Code:
(parted) mklabel msdos
...and switch to display sector as a unit:
Code:
(parted) unit s
Now you can print some useful info:
Code:
(parted) print all
Model: SD USD (sd/mmc)
Disk /dev/block/mmcblk0: 15661056s
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start      End        Size       Type     File system  Flags
Mind and write down the size in sectors (15661056 in my case).If you divide number of sectors by 2048, you get how big in MB is actually your SD card (7647 in my case).You should mind that erase block of your SD card is 128 kB and all of your partitions should start at the beginnings of those erase blocks. It is safe to say that 1st partition should begin at sector 256 and 2nd at any MB boundary. Let say you want 512 MB big ext partition and the rest for fat32 one. Mind tho that 1st partition is to be fat32! So we say 7135 MB for fat32 1st partition and 512 MB for ext 2nd partition. Now you calculate the start sector of 2nd partition... number of MB for 1st partition multiplied by 2048 should give you the number (14612480). And you are set for partitioning:
Code:
(parted) mkpart primary fat32 256 14612479
(parted) mkpart primary ext2 14612480 15661055
...and print result:
Code:
(parted) print all
Model: SD USD (sd/mmc)
Disk /dev/block/mmcblk0: 15661056s
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start      End        Size       Type     File system  Flags
 1      256s       14612479s  14612224s  primary  fat32        lba
 2      14612480s  15661055s  1048576s   primary  ext2
...and quit:
Code:
(parted) quit
At this point you have partitioned your SD card but not yet formatted it. Format fat32 partition with mkfs.vfat and ext partition with mkfs.ext2:
Code:
mkfs.vfat /dev/block/mmcblk0p1
...and:
Code:
mkfs.ext2 -m0 -b4096 /dev/block/mmcblk0p2
If there is a mke2fs utility on your phone system (standalone - not part of busybox), you may use it to format second partition as ext3:
Code:
mke2fs -j -m0 -b4096 /dev/block/mmcblk0p2
...or even as ext4 (if your mke2fs supports that):
Code:
mke2fs -j -m0 -b4096  -Oextents,uninit_bg,dir_index /dev/block/mmcblk0p2
The Following 29 Users Say Thank You to BlaY0 For This Useful Post: [ Click to Expand ]
 
Learners Lisence
Old
#2  
Learners Lisence's Avatar
Senior Member
Thanks Meter 3
Posts: 180
Join Date: Jun 2010
Blayo,

thanks for the post. You always manage to take things to an entirely different level of understanding

Is this guide for the successful implementation of the latest data2ext scripts in roms ? in comparison to methods like the Rom Manager and partition through recovery ?
Device: HTC Legend
Status: Rooted and S-Off
Rom: Customed
OC - Yes, slightly
 
BlaY0
Old
#3  
BlaY0's Avatar
Recognized Developer - OP
Thanks Meter 563
Posts: 1,552
Join Date: Sep 2007
Location: Medvode
No, it is general guide to better understand partitioning etc.
 
Dr.Romca
Old
#4  
Dr.Romca's Avatar
Senior Member
Thanks Meter 209
Posts: 765
Join Date: Dec 2008
Location: Prague
Quote:
Originally Posted by BlaY0 View Post
No, it is general guide to better understand partitioning etc.
I totally agree! After going through parted I think it's the best way to partition your SD, you have complete control!
I can't wait will my new SD card arrives, and give this a shot!
The Kingston 16GB class 10 sucks even when set-up to the best parameters and the reason for that is simple: Although class 10, it is like 4 times slower than my 8GD sandisk mobile ultra Class 4 when random writing and 3 times slower when reading...
So Thanks BlaY0 for this cool guide/lesson
HTC One, ViperOne, Nokia X3-02.5 for work
Before: HTC One X, HTC Desire S (my wife's now...), HTC Legend, Asus P750, SE W800i, SE K700i, SE T610i, Panasonic GD67, Ericsson T10...
_________________________________
Click "THANKS" if your read anything helpfull, that's the least you can do
My BatteryMOD, BootAnim and Splash
 
tasar
Old
#5  
Senior Member
Thanks Meter 8
Posts: 159
Join Date: Mar 2007
I have problem with fdisk . when i press p i got this info and there a no sector:

PHP Code:
Command (for help): p
p

Disk 
/dev/block/mmcblk016.0 GB16001269760
4 heads
16 sectors/track488320 cylinders
Units 
cylinders of 64 512 32768 bytes 
 
BlaY0
Old
#6  
BlaY0's Avatar
Recognized Developer - OP
Thanks Meter 563
Posts: 1,552
Join Date: Sep 2007
Location: Medvode
Did you change units display to sectors?
The Following User Says Thank You to BlaY0 For This Useful Post: [ Click to Expand ]
 
tasar
Old
#7  
Senior Member
Thanks Meter 8
Posts: 159
Join Date: Mar 2007
Thanks, with the "u" option comes later in your manual
A last newbie question: i have now 2 part. and formated the FAT, but i dont know, how to get the "mke2fs" on the phone to format the Linux part.?
Sorry Blay0 but Linux is another Word for me...
 
BlaY0
Old
#8  
BlaY0's Avatar
Recognized Developer - OP
Thanks Meter 563
Posts: 1,552
Join Date: Sep 2007
Location: Medvode
Quote:
Originally Posted by tasar View Post
Thanks, with the "u" option comes later in your manual
Thanx, I have changed that.
Quote:
A last newbie question: i have now 2 part. and formated the FAT, but i dont know, how to get the "mke2fs" on the phone to format the Linux part.?
Sorry Blay0 but Linux is another Word for me...
If you have busybox on your phone you also have mke2fs or mkfs.ext2 as these two are part of it. If you have CM based ROM there should already be standalone e2fsprogs in /system/bin directory and if you have a stock based one, you can find mke2fs_recvy + e2fsck_recvy in /system/bin directory. In B ROM you have all e2fsprogs available in /system/xbin directory.
The Following User Says Thank You to BlaY0 For This Useful Post: [ Click to Expand ]
 
tasar
Old
#9  
Senior Member
Thanks Meter 8
Posts: 159
Join Date: Mar 2007
Many thanks!!! Now i install your 0.5 :)
 
curto
Old
#10  
curto's Avatar
Senior Member
Thanks Meter 10
Posts: 142
Join Date: Aug 2010
Code:
# mkfs.vfat /dev/block/mmcblk0p1
mkfs.vfat /dev/block/mmcblk0p1
mkfs.vfat: not found
help?

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes