Resizing partitions (Universal Mode)

Jani-Jan

Moderator
Staff member
I wrote this How-To several months ago and published in a spanish forum.
Now I've recovered to share here as it may interest someone.

This was originally written in spanish and now, our friendly companion @nachordez
has been kind enough to translate it to english. Thank you very much for your help.


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I'll try to explain here how to change the size of our device partitions.

Though the presented data are referred to a 16 GB, a p5110 in this case, they are easy to adapt to a 8 GB one, and/or any other model, with some light corrections.

There can be other ways, but this one has the advantage that depends only on not writing wrong data, and that's easily achieved with a little extra concentration during our work.
Anyway, it's needed to follow very strictly this how-to.
In case of total failure, we should restore the tab through the flashing of a Stock version using the pit file.
ALL the data not saved in the external MicroSD card WILL BE LOST, 'cause we'll delete the /system, /data and /cache partitions.


What is needed:
  • A computer.
  • A properly running adb program.
  • Recovery installed.
  • External MicroSD card installed and with available space.
  • Connection cable.
  • Full Battery.
  • For 3G (GSM) models, the original “modem.bin” file, obtained from a stock ROM.
The modem.bin file is not really needed as we can get it from our tablet with next commanddd count=40960 bs=512 if=/dev/block/mmcblk0p8 of=/external_sd/modem.binAll process is done from computer, except a short intervention at end, done from tablet.

This how-to is planned for a AOSP-like ROM, such as CyanogenMod (for example).
In the case of a Stock ROM, the partition sizes we are adjusting will be too short for it.


Before starting:
We have to check that there is enough free space in the MicroSD card, and we have to do a backup through recovery, choosing EXTERNAL SDcard.
If the internal one is used, IT WILL BE LOST DURING PROCESS.

This step is very important, to recover the ROM without re-install from zero.
So, let me say it again: EVERY USER DATA that has being not COPIED to the EXTERNAL SDcard, WILL BE LOST.
After next steps, ONLY the external MicroSD will be conserved without erasing.

So, we check once again that everything is saved, and copy to the external MicroSD (if our tabis a 3G model) the “modem.bin” file that will be needed afterwards.


So... Let's start hacking!:

We always wrote in our PC.
We reboot our tab in recovery mode, and connect the cable.
To enter the tab from our computer:
> adb shell

Once entered correctly on the tablet, we like more clear ls command:
> alias ls='ls -an'

Now we access the partition table:
> parted /dev/block/mmcblk0

We'll get something like:
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)The command line for parted is (parted), so, every time a line starts so, that what follows this is a command.

We ask for information about current partitions:(parted) p
Model: MMC MAG2GA (sd/mmc)
Disk /dev/block/mmcblk0: 15.8GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number... Start... ....End......... Size...........File system...... Name.... Flags
1............4194kB.... 25.2MB.... 21.0MB...... ext4................ EFS
2........... 25.2MB.... 27.3MB....2097kB............................... SBL1
3........... 27.3MB.....29.4MB....2097kB....................... ........ SBL2
4........... 29.4MB.... 37.7MB....8389kB............................... PARAM
5........... 37.7MB.... 46.1MB... 8389kB............................... KERNEL
6........... 46.1MB.... 54.5MB... 8389kB............................... RECOVERY
7............54.5MB...789.0MB.. 734.0MB....... ext4................CACHE
8......... 789.0MB.. 810.0MB.... 21.0MB.............................. MODEM
9......... 810.0MB.., 2278MB... 1468MB....... ext4............... FACTORYFS
10........ 2278MB.... 15.2GB.... 12.9GB........ext4............... DATAFS
11......... 15.2GB..... 15.8GB.... 537MB....... ext4............... HIDDEN
Comments:
  • Analizing the current partitions we can see this tablet is a 16 GB one:
  • /cache (CACHE) has 734 MB assigned
  • /system (FACTORYFS) has 1468 MB assigned
  • /data (DATAFS) has 12,9 GB assigned
  • There's also a funny 537 MB partition called HIDDEN: that's where the Samsung video, musical theme and demo pictures are stored.
  • If I don't mistake, I think I extracted them time ago, and they were just about 14 MB. In our case, we'll opt for destroying that!
In this how-to we'll assign:
  • /cache (CACHE) 400 MB
  • /system (FACTORYFS) 600 MB
  • /data (DATAFS) the sum of: its current size + 394 MB from CACHE + 868 MB from FACTORYFS + 536 MB from HIDDEN.
So, we'll grow our /DATAFS space in 1798 MB, which will mean more than 14 GB free space.
I use in this example 600 MB for /system was what I did in my tab.
In real world, 240 MB /cache and 500 MB /system are more than enough.

As we'll see later, all this numbers are just aproximations, not completely exact, and probably you're thinking: “My maths do not agree with this numbers”. Mine do not, also, as a fact.

Let's see all that more slowly:
  • 21+2+2+8+8+8+21+1 (for the 'hidden' partitions) give us 71 MB.
  • If we add 71 + 400 +600 we'll get 1071 MB.
  • If we have 16 GB and we use 1 GB, more than 15 GB should rest.
  • On one hand: 1 GB are 1048 MB. So, 16 GB should be 16768 MB, but we have just 15709.
  • That has a easy explanation: The hard disk makers started to measure 1 GB as 1000 MB (kind of a commercial trick). So, just beginning with that, 768 MB have disappeared in thin air.
  • On the other hand, we have 34 initial sectors to sustain the partition table, alternative sectors for errors recovering, rounding of numbers in sectors to partitions assignations, etc.
We have 11 partitions just now:.............................................. And they should get like that:

01 00021 MB................................................ ...........................01 00021 MB
02 00002 MB................................................ ...........................02 00002 MB
03 00002 MB................................................ ...........................03 00002 MB
04 00008 MB................................................ ...........................04 00008 MB
05 00008 MB................................................ ...........................05 00008 MB
06 00008 MB................................................ ...........................06 00008 MB
07 00734 MB................................................ ...........................07 00400 MB *
08 00021 MB................................................ ...........................08 00021 MB
09 01468 MB................................................ ...........................09 00600 MB *
10 12900 MB................................................ ...........................10 14638 MB *
11 00537 MB................................................ ...........................11 00537 MB
.....15709 MB................................................ ................................15709 MB

  • The difference can seem small compared to the original partitioning, nevertheless will allow us to get all our usual apps installed and, even so, preserve a free space higher than we had previously, even before than start to install anything. That's saying: even more than with a pure CM just installed and not even configured.
  • Obviously, if we translate all that to a 8 GB model, the proportional gain is much higher.
  • Also, consider that an AOSP rom like CM is not bigger than 460 MB in /system, and that cache will need just 60 MB for dalvik and what we can download from google-play at a certain time. 170 MB should be enough, unless we want to download an app bigger than 100 MB. The bigger ones I've saw are around 90-105 MB.


In this moment, we'll have to decide if we want to follow on or not.
Till now, I was just fun, but nothing has being 'broken'.

Disclaimer: If you continue reading next post, and you do what's there exposed, it will be ONLY under YOUR RESPONSIBILITY. You've being warned...


thanks for original poster and for more detail is here
 

Jani-Jan

Moderator
Staff member
This guide was written for the Huawei y6 with a gpt partition table, mounts that use partition names not numbers and the userdata partition is the last one on disk however with a little adaption this should work for most gpt devices.

If you are unsure if your system uses partition names for the mount system look in /fstab."hardware" to see if your devices are labelled in the following manner. "/dev/block/bootdevice/by-name/system".
If the fstab file does not exist all is not lost look in your root directory at the .rc files for the command mounting system.
If you are unsure ask in the thread before doing anything. Better to ask than be stuck with a brick.

This guide covers modification to only system and userdata partitions but if you use great care the basics covered here will allow for modification to other partitions as long as you understand the risk and know that backups are your friend.

If you are unable to follow instructions and end up with a bricked device that is your own fault and attempts to blame me will be laughed at.

I obtained the attached parted binary from here

So you have flashed a custom ROM and without all the OEM bloatware you now have some unused free space in your system partition just sitting there gathering dust.
You are stuck with this space... Wrong.
You can repartition your device with a little paitence, time and work.
Read along if you are curious about how to get the space you deserve.

Prerequisites:
Free space (Duh).
The ability to follow instructions and to read ALL steps before you start so you know what your plan is.
An unlocked bootloader which you already have if you flashed a custom ROM.
A custom recovery (twrp preferred).
Parted binary (see attachment at the bottom of this post).
A computer with adb installed and working (there are many guides out there if you don't have it, Google is your friend).
A calculator (well I'm sure your computer has one but just so you know).
Some knowledge using terminal commands.

Risk of Bricking your device:
Low if you backup and can double check typing and calulations.
High if you can't do one of the above.

Because parted does not allow resizing of ext4 partitions you must delete partitions you want to change then recreate them.
All steps must be performed while in recovery because you are going to have to unmount any partition before you can change it.

Step 1:
Boot into recovery and make a backup. You MUST do this otherwise you will have nothing to restore.

Step 2:
Plug your device into your computer.
Copy the parted binary into your working directory on your PC.
Open your Command Prompt (Windows) or Terminal (Linux) and navigate to where adb is installed and you also put parted, or if you have adb in your PATH just go to where parted is.
Run
Code:
~ # adb devices
to ensure your device is present.
Run
Code:
~ # adb root
to get root priviliges.
Run
Code:
~ # adb push parted /sbin/parted
/sbin is in your devices PATH so you don't have to worry about typing /parted.
Run
Code:
~ # adb shell
Run
Code:
~ # chmod 755 /sbin/parted
If you get an error about a read only file system run
Code:
mount -o remount,rw /
then try again.

Step 3: Now you need to do some homework to find your sector size, sector count and how much free space you have available.

DO NOT shrink your filesystem to smaller than you need to operate. I always add at LEAST 50 mb to the used space however I recommend you think for the future, are you planning on restoring to stock?, are you planning on flashing OpenGAPPS?, are you planning on moving third party apps into the system?, are you planning on adding media or custom boot animations to the system?
So in the case below I would not make the partition any smaller than 800 MB.

Code:
~ # df
Filesystem Size Used Free Blksize
/system 1.2G 748.5M 441.8M 4096
/data 5.2G 3.5G 1.6G 4096
/cache 147.2M 4.7M 142.5M 4096

Now issue the following command to get your disks sector size in bytes remember to note this down.

Code:
~ # blockdev --getss /dev/block/mmcblk0
512
Time to use parted.
Issue the following commands to get some info on your current partitions.
"unit s" tells parted to show sectors instead of blocks, bytes etc.

Code:
~ # parted /dev/block/mmcblk0
(parted) unit s
(parted) print

Number Start End Size File system Name Flags
22 876562s 1191132s 314571s ext4 cache
23 1191133s 3990839s 2799707s ext4 system
24 3990840s 15269854s 11279015s ext4 userdata
Remember to not the partition number as you will need it later.
Remember to note your partition name with exact case, if you changed "system" to "System" you will have problems.
Also note the start sector of your system partition for the later steps.
Also note the end sector number for your userdata partition for the later steps.
Now get your calulator ready.
Some things to keep in mind here are that the number of sectors times sector size is the size of your partition in bytes and your starting sector must be 1 after the end of the previous partition.
So the following equation will give you the numbers you need.

Desired size in MB * 1024 * 1024 / Sector Size = Number of sectors
Starting sector + Number of sectors - 1 = Last Sector

If you want a 1.1 Gb partition, you have 512 byte sectors and the starting sector is 1191133 you would then use the following equation.

1100 * 1024 * 1024 / 512 = 2252800
1191133 + 2252800 - 1 = 3443932


Step 4:
Now using the info you have gathered you can change your partitions.
So I would issue the following commands to delete, create and name the system partition with the size I want.

Code:
(parted) rm 23

(parted) mkpart 23 ext4 1191133 3443932

(parted) name 23 system
Now to resize the data partition.
You need to start with the first available sector after the end of system and end with the last available sector noted earlier.

Code:
(parted) rm 24

(parted) mkpart 24 ext4 3443933 15269854

(parted) name 24 userdata
Now that your partitions are all done you can exit parted with the following.
Code:
(parted) quit
Step 5:
Now restore the backup you made earlier.
If you get an error restoring which usually only happens if you have altered partition numbers just reboot back into recovery and restore then so the kernel gets to update its own info.

Reboot.

Congratulations you now have repartitioned your device.

Don't forget to thank this post if you are grateful.

for more detail XDA
 
Top