Skip to content

Partitioning the SD Card using ADB

Forest edited this page Nov 18, 2015 · 15 revisions

Introduction

There are many ways to partition an SD Card, some prefer to use a program or under Ubuntu using Parted/Gparted. Even CWM includes a tool to do the partitioning. The drawback however is that sometimes programs tend to cause dificulties or require a PC with a card reader, etc. A while back, when I partitioned my SD Card I just did not want to install extra stuff on my PC and when trying the method via CWM the feature itself had problems.

Why should I partition my SD Card?

CyanogedMod 11 (KitKat) and newer Android versions and derivates (AOSP 5.x and CyanogenMod 12 (Lollipop)) take away a fairly large amount of internal storage from our phones. Additionally apps tend to get bigger and are often not movable to the SD Card by normal means. To overcome this limitation there are ways to expand the internal phone storage by assigning part of the SD Card to the phone internal storage partition.

For this to work you first will have to create a special partition on your SD Card. How to do it will be explained in this guide. Once that is done you can use an app like link2Sd or Mount2Sd to expand your storage on CyanogenMod 11. AOSP 5.x and CyanogenMod 12 require a partition like this by default and therefore don't require installing any of the previous mentioned apps.

What else do I have to know?

  • Partitioning the SD Card will completely erase it! This means that you should back up EVERYTHING you want to keep.
  • Since we are increasing the overall "internal" storage of the phone booting may take longer.

Let's start!

What you will need

  • Custom recovery with parted and mkfs (included in LegacyXperia Kernel)
  • Phone with SD Card inserted
  • ADB and ADB Drivers (These come bundled in the Android SDK) See the Installing LX guide.

Preparation

On your phone:

  • Enable Android debugging: Settings > Developer options > Android debugging
  • Give root to ADB: Settings > Developer options > Root access : apps and adb

Under Windows:
Open a command prompt and navigate to the folder where adb.exe is located. You do this with the following command:

cd <drive letter>:\<path to adb folder>\

You can also right click on an empty space inside the adb.exe folder > Open command window here.

Connect your phone to your computer via USB. Reboot into recovery mode manually or by typing the following command into the command prompt:

adb shell reboot recovery

A dialog may appear on your phone to allow access from your PC

Step 1

Once in the recovery type the following to start the shell:

adb shell

Your screen should look something like this (Obviously the path may be different)

D:\Xperia Arc S\Tools\adb_fastboot\adb shell
/# _

NOTE: I've had the case that some random characters appeared before the #, just ignore.

Step 2

Open parted to start the partitioning tool by typing the following command into the shell:

parted /dev/block/mmcblk0

Your screen should look like this

/# parted /dev/block/mmcblk0
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) _

NOTE: Indepth information about parted can be found here.

Step 3

Display the SD Card information. Shell Command:

print

Your screen should look like this

/# parted /dev/block/mmcblk0
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) print
print
print
Model: SD SU08G (sd/mmc)
Disk: /dev/block/mmcblk0: 7948MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system     Flags
 1      512B    7948MB  7948MB  primary  fat32           lba

(parted) _

IMPORTANT: Write down the exact number of the size of your SD Card. Mine is 7948MB.

NOTE: On bigger SD Cards you can use the command unit MiB to display everything in MB rather than GB.

NOTE2: If you come across a SD Card that is already partitioned it should look something like this:

Number  Start   End     Size    Type     File system     Flags
 1      512B    6796MB  6796MB  primary  fat32           lba
 2      6796MB  7820MB  1024MB  primary  ext2
 3      7820MB  7948MB  128MB   primary  linux-swap(v1)

(parted) _

NOTE3: If your SD Card is blank you can skip to Step 5

Step 4

Remove all partitions. If you have multiple partitions, remove each one at a time. You most likely will only have one partition so type:

rm 1

If you have more than one type rm x where x is the partition number. Type print when you are done to check if everything has been removed.

Your screen should look like this

Number  Start   End     Size    Type     File system     Flags

(parted) _

Step 5

Create your new partitions. You can make these any size, but the most common setup is 32 MB linux-swap partition, 512 MB ext2 partition, and remaining free space as a fat32 partition. In order to work properly, the partitions must be created in this order: fat32, ext2, linux-swap.

Now you will have to do some math. We will take the output of my SD Card from Step 3 as an example:

  7948MB  Total SD Card size  
-  128MB  linux-swap  
= 7820MB  Rest size  
- 1024MB  ext2  
= 6796MB  Rest size (for fat32)  

Now we have to create the partitions. Use the following command to do so:

mkpartfs primary <file system> <start> <end>

For my SD Card with the calculated values it would look like this:

mkpartfs primary fat32 0 6796
mkpartfs primary ext2 6796 7820
mkpartfs primary linux-swap 7820 7948

NOTE: The order in which you type the commants is important! First create the fat32 - partition then ext2 and finally linux-swap.

Step 6

Check if everything went well. Type print again and you should see your newly created partitions. Typing quit will return you to the shell. If you don't wish to continue with the optional filesystem upgrade you can now reboot your phone.

Your partitions are ready to be used with any sd-ext linking App. Remember that ext2,ext3, ext4 and f2fs partitions are "invisible" to Windows, so you only will see the fat32 partition when you plug in your phone. In my case Windows will tell me it's 7GB in size because the other 1GB belongs to ext2.

Enjoy!

Upgrading the ext2 filesystem (Optional)

You can further upgrade your filesystem either to ext3/ext4 or f2fs. Which one you choose is up to you. Migrating from ext4 to f2fs by following the instructions for ext2 to f2fs is also possible.

Upgrade ext2 to ext4

To upgrade your ext2 partition to an ext3 and ext4 filesystem you will use the tune2fs command. Remember to type this command into the shell (/# _) via ADB and NOT into the parted tool.

The command to upgrade to ext3 looks as follows:

tune2fs -j /dev/block/mmcblk0p2

to upgrade to ext4 type this:

tune2fs -O extents,uninit_bg,dir_index /dev/block/mmcblk0p2

Just to make sure run e2fsck to make sure the filesystem was upgraded and is without errors. Type the following:

e2fsck -fpDC0 /dev/block/mmcblk0p2

Check your work

From the shell open parted again and use the print command to list your SD Card details. Type:

parted /dev/block/mmcblk0

then

print

Your screen should look like this

/# parted /dev/block/mmcblk0
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) print
print
print
Model: SD SU08G (sd/mmc)
Disk: /dev/block/mmcblk0: 7948MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system     Flags
 1      512B    6796MB  6796MB  primary  fat32           lba
 2      6796MB  7820MB  1024MB  primary  ext4
 3      7820MB  7948MB  128MB   primary  linux-swap(v1)

(parted) _

Now you can quit and reboot your phone.

Success!

Upgrade ext2 to f2fs

As an alternative you can create a f2fs partition by formatting the created ext2 filesystem to f2fs. To do this you simply need to open a shell via ADB and type:

mkfs.f2fs -l f2fs /dev/block/mmcblk0p2

Make sure that /system is mounted because the f2fs - tool is located under /system/bin.

Wait for it to finish, type quit and reboot your phone.

Success!

Credits

Thanks to Taylor Wimberly at www.androidandme.com for the original guide, I kindly borrowed without his permission. It was for a good cause however.