[Nexus S General] steven676:
Modifying your phone to use LVM

WARNING WARNING WARNING

While unlikely, you could brick your phone if you do this incorrectly!!!

Read this guide completely before attempting. If you don't understand the procedure, read it again until you do. If you still don't understand, wait for someone else to make this easier. If you're not sure you understand, you probably don't.

BACK UP EVERYTHING ON YOUR PHONE, INCLUDING THE CONTENTS OF /sdcard, BEFORE ATTEMPTING THIS PROCEDURE! This will erase everything on your phone.

The preassembled images attached to this post are for Nexus S GT-i9020T running stock Android 4.0.4 IMM76D ONLY. They may or may not work with other phones and ROMs. (See the next post for how to modify any ROM to use LVM.)

You will need:

A Nexus S with unlocked bootloader

A computer with fastboot and adb installed and working

A USB cable

Factory images for your phone, in case something goes wrong

Procedure:

Take a nandroid backup of your phone and copy it to your computer. We'll use it to restore the ROM later. You MUST copy the backup to your computer! This procedure will erase EVERYTHING on your phone, including the contents of /sdcard.

Download the boot and recovery images attached to this post. Reboot your phone into the bootloader, then flash the recovery to your phone using fastboot:

fastboot flash recovery recovery-lvm-clockwork-5.0.2.0-crespo.img

(GPL information: this is the stock kernel image from ClockworkMod 5.0.2.0. For LVM source, see this post.)

Boot into recovery.

Enter the shell on the device:

adb shell

In the shell on the device, do the following (# is your prompt):

# /lvm/sbin/lvm pvcreate /dev/block/mmcblk0p1 /dev/block/mmcblk0p2 /dev/block/mmcblk0p3
# /lvm/sbin/lvm vgcreate lvpool /dev/block/mmcblk0p1 /dev/block/mmcblk0p2 /dev/block/mmcblk0p3

This marks the partitions normally used for /system, /data, and /sdcard for use with LVM, and creates an LVM volume group named "lvpool" using those devices.

In the shell on the device, set up your desired logical volumes (partitions).

# /lvm/sbin/lvm lvcreate -L 320M -n system lvpool
# /lvm/sbin/lvm lvcreate -L 1G -n userdata lvpool
# /lvm/sbin/lvm lvcreate -L 10G -n media lvpool
[...]

This creates the mandatory logical volumes named "system", "userdata", and "media", which will be mounted on /system, /data, and /sdcard, respectively. Replace the argument to -L with the desired size of each partition. (/system needs to be at least 300 MB to fit the stock ROM.) You can create additional volumes similarly:

# /lvm/sbin/lvm lvcreate -L [size] -n [name] lvpool

replacing [size] with the desired size and [name] with the desired name. You have a little less than 15 GB space in the volume group to work with; depending on how many partitions you want to create, you can adjust the size of the "media" and "userdata" logical volumes as required.

Reboot back into recovery. Mount the USB storage on your computer. On your computer, format your phone's USB storage as you would a USB flash drive.

Copy your nandroid backup from your computer onto your phone.

Reboot back into recovery. Restore the nandroid backup.

Reboot into the bootloader. Flash the boot image to your phone via fastboot:

fastboot flash boot boot-lvm-crespo-IMM76D.img

(GPL information: this is the stock kernel image for IMM76D. Source is available from AOSP git. For LVM source, see this post.)

Reboot. You should have a working device running on LVM.

Additional tips and hints

To see the logical volumes you've created, use

# /lvm/sbin/lvm lvs

and to see how much free space you have remaining in the volume group, use

# /lvm/sbin/lvm vgs

If you want to delete a logical volume (for example, if you screwed up), use

# /lvm/sbin/lvm lvremove /dev/lvpool/name

(replacing "name" with the name of the LV you want to delete).

It's often useful to leave a bit of free space in the volume group (512 MB to 1 GB or so). This gives you a bit more flexibility to modify the layout or grow partitions in the future, and allows you to take snapshots of logical volumes.

Removing LVM from your phone

Flash a standard recovery such as the regular ClockworkMod recovery to your phone.

Do a full wipe/factory reset.

Restore your phone from a backup.

If things go really wrong, flash the factory image to your phone via fastboot.