[Atrix 4G General] sendust7:
Android Batch File to Partition SD Card

This batch command file invokes the /sbin/parted and /sbin/tune2fs tools hosted by ClockWorkMod (CWM) Recovery 5.0.2.0 to control partitioning of your SD card for a variety of purposes, some of which are listed below.

It is a good idea to review the command line options and parameters of the parted and tune2fs tools: On your phone (for example, via computer-enabled ADB or terminal emulator), get root and enter:

# /sbin/parted --help

# /sbin/tune2fs --help

I decided to make this a separate thread for a number of reasons:

Streamline the process of making room for second ROM in a dual boot configuration (see http://xdaforums.com/showthread.php?t=1651356 ). These commands can theoretically be rolled into a flashable zip to almost fully automate a dual-boot install of fixed ROMs, e.g. CM9 and CM7.

My Dual Boot threads have “sensory overload” written all over them

The amount of material presented here is probably enough to warrant a separate thread

This is an Android-friendly alternative to GParted/MiniTool. For example there is no need to pull SD card, partition and reinsert. Of course, GParted has the pleasant GUI and gives you a chance to review your changes before committing, whereas the Android “parted” tool is highly utilitarian.

Batch commands are quickly and easily modified to create/remove/adjust partitions. The general consensus is that for large files, ext3 R/W speeds are faster than those of FAT32. And ext3[4] is more stable and can handle files exceeding the maximum 4GB allowed by FAT32.

Batch commands are easily modified to perform customizations other than dual boot

Some users may wish to host their entire system on SD card: ROM, music, pictures, movies etc.

Some users may wish to return their SD cards to a traditional single-boot state by running a subset of these batch commands

That said, I strongly advise that you NOT run these commands UNLESS you are comfortable with ALL of the following:

1. /sdcard (FAT32) and ALL subdirectories copied to /emmc/sdcard-backup. Ask yourself: Do I have enough space left on my 10GB emmc partition to host a full backup of /sdcard? AND, will there be enough room on the /sdcard filesystem to restore this backup AFTER downsizing my SD card's FAT32 partition?

2. Any existing ext* partitions of SD card are blown away, for example those generated by a previous dual boot zip install. In other words, you lose /system, /cache and /data from an existing sdcard-based ROM.

3. You MAY lose all data on your (backed-up) FAT32 partition depending on what boundaries were used for resizing.

Note that I do not include a command to push /emmc/sdcard-backup back onto SD card. That is because:

There may not be enough room on /sdcard after a FAT32 downsize, and

There may be a data conflict when flashing a different dual boot configuration as opposed to an updated one. But at least there is a full backup of /sdcard FAT32 --- assuming emmc has enough room to host it.

Presently there is no logic to detect the storage limits of the /emmc and /sdcard filesystems. I will leave that to you.

SO... if for ANY reason you are NOT comfortable with the potential changes outlined above, I recommend that you please STOP HERE and consider researching this topic further until you are comfortable.

For those willing and able to give this a try, I strongly recommend first performing an ADB-assisted run (i.e. with a computer hosting Android SDK Platform Tools) using an SD card with at least 4GB open (to be safe) AND with enough room on emmc to host a FULL backup of your entire /sdcard filesystem (/sdcard and all its subdirectories).

Fully charge your battery! CWM draws a lot of current, even if nothing else is running.

Boot phone to CWM

Connect phone to computer

Launch ADB shell to access phone

# adb shell (can do this on any ADB-enabled machine)

Get root (enter “su” at phone shell command prompt) and copy/paste/enter each and every command (those not prefixed by "echo") in your computer's command window:

#! /bin/sh

echo "Android batch script to create ext partitions for SD card-based ROM"

echo
echo "First unmount all filesystems of primary (emmc) ROM"
umount /system > /dev/null 2>&1
umount /cache > /dev/null 2>&1
umount /data > /dev/null 2>&1

echo
echo "Mount external (sdcard) and internal (emmc) memory"
mount /sdcard > /dev/null 2>&1
mount /dev/block/mmcblk0p18 /emmc > /dev/null 2>&1

echo
echo "Create sdcard-backup directory on emmc with current date and time"
echo "Recommend delete any unnecessary backups of sdcard at this point"
bkdir="sdcard-backup-`date +%d-%b-%Y-%H%M%S`"
mkdir /emmc/$bkdir > /dev/null 2>&1

echo
echo Backup directory: /emmc/$bkdir

echo
echo "Press ENTER to continue..." ; read

echo
echo "Backing up contents of sdcard FAT32 to emmc"
cp -a /sdcard/* /emmc/$bkdir/

echo
echo Done...
ls -ld /emmc/$bkdir

echo
echo "Unmount internal and external memory"
umount /emmc > /dev/null 2>&1
umount /sdcard > /dev/null 2>&1

echo
echo "Show existing partition map of SD card"
parted /dev/block/mmcblk1 print

echo
echo "Press ENTER to continue..." ; read

echo
echo "Delete any existing ext* partitions and show results"
parted /dev/block/mmcblk1 rm 4 > /dev/null 2>&1
parted /dev/block/mmcblk1 rm 3 > /dev/null 2>&1
parted /dev/block/mmcblk1 rm 2 > /dev/null 2>&1
parted /dev/block/mmcblk1 print

echo
echo "Press ENTER to continue..." ; read

echo
echo "Resize START/STOP values MUST be expressed in MB"
echo "Downsize FAT32 and generate new ext2 partitions"
echo "Resize START/STOP values MUST be expressed in MB!"
echo "This example shows TYPICAL repartitioning of 16GB card..."
parted /dev/block/mmcblk1 resize 1 4.194 10700
parted /dev/block/mmcblk1 mkpartfs primary ext2 10.7GB 11.1GB
parted /dev/block/mmcblk1 mkpartfs primary ext2 11.1GB 11.8GB
parted /dev/block/mmcblk1 mkpartfs primary ext2 11.8GB 13.9GB

echo
echo "Upgrade from ext2 to ext3"
tune2fs -j /dev/block/mmcblk1p2
tune2fs -j /dev/block/mmcblk1p3
tune2fs -j /dev/block/mmcblk1p4

echo
echo "Review changes"
parted /dev/block/mmcblk1 print

echo
echo "Press ENTER to continue..." ; read

echo
echo "Final review. Mount SD card ROM filesystems"
mount /dev/block/mmcblk1p2 /system > /dev/null 2>&1
mount /dev/block/mmcblk1p3 /cache > /dev/null 2>&1
mount /dev/block/mmcblk1p4 /data > /dev/null 2>&1

echo
echo "Show used and available space (in MB) on all mounted filesystems"
df -m

echo
echo "Unmount SD card ROM filesystems"
umount /system
umount /cache
umount /data

echo
echo "Remount emmc ROM /cache (CWM mounts this by default)"
mount /cache > /dev/null 2>&1

echo
echo "Done. Can now (if necessary) wipe /cache and /data for primary ROM and flash dual boot zip"
echo

And now :D... here are unedited results of a successful batch run made on 11 Jun 2012 at 1630 PDST

~ # sh /tmp/part-sdc.sh
Android batch script to create ext partitions for SD card-based ROM

First unmount all filesystems of primary (emmc) ROM

Mount external (sdcard) and internal (emmc) memory

Create sdcard-backup directory on emmc with current date and time
Recommend delete any unnecessary backups of sdcard at this point

Backup directory: /emmc/sdcard-backup-11-Jun-2012-233034

Press ENTER to continue...

Backing up contents of sdcard FAT32 to emmc

Done...
drwxrwxrwx 16 root root 8192 Jun 11 23:36 /emmc/sdcard-backup-11-Jun-2012-233034

Unmount internal and external memory

Show existing partition map of SD card
Model: SD SU16G (sd/mmc)
Disk /dev/block/mmcblk1: 15.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 4194kB 10.7GB 10.7GB primary fat32 lba
2 10.7GB 11.1GB 395MB primary ext2
3 11.1GB 11.8GB 707MB primary ext2
4 11.8GB 13.9GB 2097MB primary ext2

Press ENTER to continue...

Delete any existing ext* partitions and show results
Model: SD SU16G (sd/mmc)
Disk /dev/block/mmcblk1: 15.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 4194kB 10.7GB 10.7GB primary fat32 lba

Press ENTER to continue...

Resize START/STOP values MUST be expressed in MB
Downsize FAT32 and generate new ext2 partitions
Resize START/STOP values MUST be expressed in MB!
This example shows TYPICAL repartitioning of 16GB card...
Information: You may need to update /etc/fstab.

Information: You may need to update /etc/fstab.

Information: You may need to update /etc/fstab.

Information: You may need to update /etc/fstab.

Upgrade from ext2 to ext3
tune2fs 1.41.6 (30-May-2009)
Creating journal inode: done
This filesystem will be automatically checked every 30 mounts or
0 days, whichever comes first. Use tune2fs -c or -i to override.
tune2fs 1.41.6 (30-May-2009)
Creating journal inode: done
This filesystem will be automatically checked every 30 mounts or
0 days, whichever comes first. Use tune2fs -c or -i to override.
tune2fs 1.41.6 (30-May-2009)
Creating journal inode: done
This filesystem will be automatically checked every 30 mounts or
0 days, whichever comes first. Use tune2fs -c or -i to override.

Review changes
Model: SD SU16G (sd/mmc)
Disk /dev/block/mmcblk1: 15.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 4194kB 10.7GB 10.7GB primary fat32 lba
2 10.7GB 11.1GB 395MB primary ext3
3 11.1GB 11.8GB 707MB primary ext3
4 11.8GB 13.9GB 2097MB primary ext3

Press ENTER to continue...

Final review. Mount SD card ROM filesystems

Show used and available space (in MB) on all mounted filesystems
Filesystem 1M-blocks Used Available Use% Mounted on
tmpfs 408 0 408 0% /dev
/dev/block/mmcblk1p2 353 8 326 2% /system
/dev/block/mmcblk1p3 632 16 582 3% /cache
/dev/block/mmcblk1p4 1874 32 1742 2% /data

Unmount SD card ROM filesystems

Remount emmc ROM /cache (CWM mounts this by default)

Done. Can now (if necessary) wipe /cache and /data for primary ROM and flash dual boot zip

~ #

All you brave testers out there, please let me know if you see anything amiss. Thanks!

Disclaimer

Standard disclaimers apply. In short, I am not responsible for any harm you or your phone may incur by using any or all of this material.

Enjoy. :D

References

http://androidos.in/2009/11/manually-partition-sd-card-for-android/

http://www.gnu.org/software/parted/manual/html_chapter/parted_2.html

http://linux.die.net/man/8/tune2fs