[Nexus 5 General] surfrock66:
[Guide] Repartition Nexus5 to increase system partition - Space for Rom & Stock Gapps

Before I begin...don't do this if you don't know what you're doing. If you know what you're doing still don't do this. This is dangerous, and in general people don't even make good guides for this likely because it's SUCH a stupid thing to do. Samsung phones support PIT re partitioning, but for something like a Nexus, there is no easy guide. You can and likely will brick your phone...at best you will certainly wipe all data.

I wanted to install a nougat rom on my cracked-screen Nexus5, but in flashing it AND stock gapps would error out since there isn't enough room on the /system partition for both. The Nexus5 comes with a 1GB system partition which was fine way back in the day, but isn't really fine anymore. In order to pull space from the large userdata partition, we need to do some linux trickery. I chose to make /system 2GB, which may be overkill, but this phone is going to be a baby monitor/white noise machine for a 5 month old so who cares.

The prerequisite for this process is a TWRP recovery, and that's pretty much it. Ideally, fdisk would be baked in with busybox or the parted utility would be on the phone and you could use the resize function...every time I tried to use busybox's fdisk led to errors or commands wouldn't work, and parted's resize command can't deal with ext4.

The high-level procedure here is, since filesystems must be contiguous and in order so they can be addressed properly, we need to delete every partition inclusively from system to userdata, then recreate them with new storage offsets. To visualize this, here's the storage layout as it started out on my Nexus 5:

Model: MMC SEM32G (sd/mmc)
Disk /dev/block/mmcblk0: 31.3GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 524kB 67.6MB 67.1MB fat16 modem
2 67.6MB 68.7MB 1049kB sbl1
3 68.7MB 69.2MB 524kB rpm
4 69.2MB 69.7MB 524kB tz
5 69.7MB 70.3MB 524kB sdi
6 70.3MB 70.8MB 524kB aboot
7 70.8MB 72.9MB 2097kB pad
8 72.9MB 73.9MB 1049kB sbl1b
9 73.9MB 74.4MB 524kB tzb
10 74.4MB 75.0MB 524kB rpmb
11 75.0MB 75.5MB 524kB abootb
12 75.5MB 78.6MB 3146kB modemst1
13 78.6MB 81.8MB 3146kB modemst2
14 81.8MB 82.3MB 524kB metadata
15 82.3MB 99.1MB 16.8MB misc
16 99.1MB 116MB 16.8MB ext4 persist
17 116MB 119MB 3146kB imgdata
18 119MB 142MB 23.1MB laf
19 142MB 165MB 23.1MB boot
20 165MB 188MB 23.1MB recovery
21 188MB 191MB 3146kB fsg
22 191MB 192MB 524kB fsc
23 192MB 192MB 524kB ssd
24 192MB 193MB 524kB DDR
25 193MB 1267MB 1074MB ext4 system
26 1267MB 1298MB 31.5MB crypto
27 1298MB 2032MB 734MB ext4 cache
28 2032MB 31.3GB 29.2GB ext4 userdata
29 31.3GB 31.3GB 5632B grow

Looking at that, we want to increase system (partition 25), shift crypto (partition 26), shift cache (partition 27), and shrink userdata (partition 28). If you try this on a different phone, you'll have different partitions to move.

I did this from a Debian desktop using adb, but you can use any platform that has adb. You need to download the parted binary linked below, a nexus5 Nougat rom (or any rom I guess), and a gapps package (I chose stock). Here's the commands I used:

wget http://iwf1.com/iwf-repo/parted.rar
unrar e parted.rar
sudo adb push parted /
sudo adb shell
~ # chmod +x parted
~ # ./parted /dev/block/mmcblk0 p
~ # umount /data
~ # umount /sdcard
~ # umount /cache
~ # ./parted /dev/block/mmcblk0 rm 25
~ # ./parted /dev/block/mmcblk0 rm 26
~ # ./parted /dev/block/mmcblk0 rm 27
~ # ./parted /dev/block/mmcblk0 rm 28
~ # ./parted /dev/block/mmcblk0 mkpart primary 193MB 2291MB
~ # ./parted /dev/block/mmcblk0 mkpart extended 2291MB 2322MB
~ # ./parted /dev/block/mmcblk0 mkpart primary 2322MB 3056MB
~ # ./parted /dev/block/mmcblk0 mkpart primary 3056MB 30.8GB
~ # ./parted /dev/block/mmcblk0 p
~ # ./parted /dev/block/mmcblk0 name 25 system
~ # ./parted /dev/block/mmcblk0 name 26 crypto
~ # ./parted /dev/block/mmcblk0 name 27 cache
~ # ./parted /dev/block/mmcblk0 name 28 userdata
~ # mke2fs -b 4096 -T ext4 /dev/block/mmcblk0p25
~ # mke2fs -b 4096 -T ext4 /dev/block/mmcblk0p27
~ # mke2fs -b 4096 -T ext4 /dev/block/mmcblk0p28
~ # ./parted /dev/block/mmcblk0 p
~ # mount -a
~ # exit
# Download from here: http://www.androiddevs.net/downloads/
sudo adb push aosp_hammerhead-7.1-nougat-*.zip /data/
# Download from here: http://opengapps.org/
sudo adb push open_gapps-arm-7.1-stock-*.zip /data/
sudo adb reboot recovery
# Install the nougat rom through twrp...this will resize the /system partition back to 1GB!
sudo adb shell
~ # umount /system
~ # resize2fs -f /dev/block/mmcblk0p25 2000M
~ # mount -a
# Install opengapps in twrp
~ # exit
# Reboot into system through TWRP GUI

There were some logging errors with the SantoshM nougat rom I tried, but they had no impact. I am unmounting /sdcard and that's where it's trying to stash the logs, hence errors.

You can see that the offsets for the new partitions are 1024MB higher than the originals, meaning the partition will be 1GB bigger. Here's my final partition table:

Model: MMC SEM32G (sd/mmc)
Disk /dev/block/mmcblk0: 31.3GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 524kB 67.6MB 67.1MB fat16 modem
2 67.6MB 68.7MB 1049kB sbl1
3 68.7MB 69.2MB 524kB rpm
4 69.2MB 69.7MB 524kB tz
5 69.7MB 70.3MB 524kB sdi
6 70.3MB 70.8MB 524kB aboot
7 70.8MB 72.9MB 2097kB pad
8 72.9MB 73.9MB 1049kB sbl1b
9 73.9MB 74.4MB 524kB tzb
10 74.4MB 75.0MB 524kB rpmb
11 75.0MB 75.5MB 524kB abootb
12 75.5MB 78.6MB 3146kB modemst1
13 78.6MB 81.8MB 3146kB modemst2
14 81.8MB 82.3MB 524kB metadata
15 82.3MB 99.1MB 16.8MB misc
16 99.1MB 116MB 16.8MB ext4 persist
17 116MB 119MB 3146kB imgdata
18 119MB 142MB 23.1MB laf
19 142MB 165MB 23.1MB boot
20 165MB 188MB 23.1MB recovery
21 188MB 191MB 3146kB fsg
22 191MB 192MB 524kB fsc
23 192MB 192MB 524kB ssd
24 192MB 193MB 524kB DDR
25 193MB 2291MB 2098MB ext4 system
26 2291MB 2322MB 31.0MB crypto
27 2322MB 3056MB 734MB ext4 cache
28 3056MB 31.3GB 28.2GB ext4 userdata
29 31.3GB 31.3GB 5632B grow

When we resize /system, we're not quite giving it the whole space...I recommend running "resize2fs /dev/block/mmcblk0p25 2048M" and seeing the error...it's easier to adjust the actual filesystem size than the partition size, so we fudge it a bit here so long as it's safe. If this test is unsuccessful, it will tell you that you're allocating more blocks than you have. If it's successful, it'll tell you to run e2fsck...this is normal, and running the resize2fs command with the -f flag overrides that warning...but it'll also allow you to allocate too many blocks, hence why you run a test first as a sanity check. For this exact procedure, 2000M is the exact maximum size.

All in all, this technique can likely be adapted to other android phones. The key is making sure you resize system by the amount you take from data, and making sure you offset all partitions in between linearly by the correct amount. I'm not sure how updates will work...anything that basically lays down a partition is gonna cause issues since it'll try to resize to the default partition size. Anything just laying down new files should be fine.

Happy hacking!