[G1 Android Development] Firerat:
Full Firerat Modified a2sd Code

It actually does a little more that just be friendly to MT3G, it will attempt to keep Dalvik-cache on /data

#!/system/bin/sh
#
# Apps2SD using symlinks and bind mounts
# shade@chemlab.org (cyanogen) Thanks Cy! & ccyrowski
# modified by Firerat 2010-03-24
# Skip a2sd functions if not a G1 ( mt3g in mind )
# Conditional added to location of Dalvik-cache, keep in internal memory where possible
# Firerat 2010-03-30 - added moving apps back to data for mt3g's
# Firerat-TODO - echo for when ran on terminal

#############################################################
RM_CMD="busybox rm"
MV_CMD="busybox mv"
CP_CMD="busybox cp"
LS_CMD="busybox ls"
LN_CMD="busybox ln"
MKDIR_CMD="busybox mkdir"
MOUNT_CMD="busybox mount"
UMOUNT_CMD="busybox umount"
SWAPON_CMD="busybox swapon"
# nb the df sticks out, toolbox df output is easier to cut up
DF_CMD="toolbox df"
DU_CMD="busybox du"
SED_CMD="busybox sed"
GREP_CMD="busybox grep"
CUT_CMD="busybox cut"
GETPROP_CMD="getprop"
SETPROP_CMD="setprop"
E2FSCK_CMD="e2fsck"
CHMOD_CMD="busybox chmod"
CHOWN_CMD="busybox chown"
SYNC_CMD="sync"
rohardware=`$GETPROP_CMD ro.hardware`
#############################################################

echo "+++ Welcome to Android `$GETPROP_CMD ro.build.version.release` / `getprop ro.modversion`";

sysctl -p

# check if not a G1 ( assuming mt3g ) - Firerat 2010-03-23
if [ "$rohardware" != "trout" ];
then
FindSwap
ReturnAppsToData
$SETPROP_CMD cm.filesystem.ready 1;
exit 0;

elif [ -e /dev/block/mmcblk0p2 ];
then
MountExt
# clean up any old symlinks, create data directories
for i in dalvik-cache data;
do
if [ -h /data/$i ];
then
$RM_CMD /data/$i;
fi
if [ ! -d /data/$i ];
then
$MKDIR_CMD /data/$i;
$CHOWN_CMD 1000:1000 /data/$i;
$CHMOD_CMD 771 /data/$i;
fi
done;

# don't allow /data/data on sd because of upgrade issues - move it if possible
if [ -d /system/sd/data ];
then
$CP_CMD -a /system/sd/data/* /data/data/;
$RM_CMD -rf /system/sd/data;
fi

# move apps and dalvik cache from internal memory to sdcard
# when possible keep dalvik-cache on internal memory Firerat 2010-03-23
# for i in app app_s app-private dalvik-cache;
# Firerat-TODO in the same way I try to keep Dalvik-Cache on internal, do same for apps
# therefore keeping things as fast as possible
# and if I get my head round aufs then can maybe do some moving based on priority list
# hell, could even build list based on useage if it's recorded in the dbs
for i in app app_s app-private ;
do
if [ ! -d /system/sd/$i ];
then
$MKDIR_CMD /system/sd/$i;
fi

$CHOWN_CMD 1000:1000 /system/sd/$i;
$CHMOD_CMD 771 /system/sd/$i

if [ -d /data/$i ] && [ ! -h /data/$i ];
then
$CP_CMD -a /data/$i/* /system/sd/$i/;
$RM_CMD -f /data/$i/*;
fi
done;

# symlink app dirs - they must be on the same filesystem
for i in app app_s app-private;
do
if [ -d /data/$i ] && [ ! -h /data/$i ];
then
$RM_CMD -rf /data/$i;
$LN_CMD -s /system/sd/$i /data/$i;
fi
done;

# bind mount dalvik-cache so we can still boot without the sdcard
# when possible keep dalvik-cache on internal memory Firerat 2010-02-24
# $MOUNT_CMD -o bind /system/sd/dalvik-cache /data/dalvik-cache;
# $CHOWN_CMD 1000:1000 /data/dalvik-cache;
# $CHMOD_CMD 771 /data/dalvik-cache;

DalvikCache

# clean up old whiteouts
for i in local misc property system tombstones data;
do
if [ -h /system/sd/$i ]; then $RM_CMD -f /system/sd/$i; fi
done;

# please don't put odex files in the app directory people!
# it causes dexopt to crash when switching builds!
$RM_CMD -f /system/sd/app/*.odex
# Firerat 2010-03-30 , not looked at this , but wrapper dexopt to ignore *.odex ?
else

# replace symlinks with directories so we can boot without sd
for i in app app-private;
do
if [ -h /data/$i ];
then
$RM_CMD -f /data/$i;
$MKDIR_CMD /data/$i;
$CHOWN_CMD 1000:1000 /data/$i;
$CHMOD_CMD 771 /data/$i;
fi
done;
fi
$SYNC_CMD;

FindSwap

#Set Fifo - King Klick - thanks Wes Garner!
for i in ` $LS_CMD -1 /sys/block/mtdblock*` /sys/block/mmcblk0
do
echo 1 > $i/queue/iosched/fifo_batch
done
# Firerat 2010-03-30 ^ should be in init.rc , or some other post boot script

$SETPROP_CMD cm.filesystem.ready 1;

# for now this can stay
if [ -e /data/fix_permissions.sh ];
then
sh /data/fix_permissions.sh
$RM_CMD /data/fix_permissions.sh
fi

### Firerat - Begin functions

MountExt ()
{
# Firerat Check /dev/block/mmcblk0p2 is mounted , so script can be run in terminal
if [ `$MOUNT_CMD |$GREP_CMD -q "/dev/block/mmcblk0p2";echo $?` != "0" ];
then
# fsck the sdcard filesystem first
$E2FSCK_CMD -fy /dev/block/mmcblk0p2;
# set property with exit code in case an error occurs
$SETPROP_CMD cm.e2fsck.errors $?;
# mount and set perms
$MOUNT_CMD -o noatime,nodiratime -t auto /dev/block/mmcblk0p2 /system/sd;
$CHOWN_CMD 1000:1000 /system/sd;
$CHMOD_CMD 771 /system/sd;
$SYNC_CMD

fi
return
}

FindSwap ()
{
if [ "`$SED_CMD -n '$=' /proc/swaps`" -gt "1" ];
then
echo "Swap appears to be already setup, skipping checks"
return
else
for isitswap in ` $LS_CMD /dev/block/mmcblk0p*` ; do
$SWAPON_CMD $isitswap 2>/dev/null
if [ "$?" = "0" ];
then
$MOUNT_CMD -o rw,remount /system
# Firerat 2010-03-30 - check for cache hack
if [ -d /system/csys ]; then
$MOUNT_CMD -o rw,remount /system/csys
fi
$SED_CMD s~/dev/block/mmcblk0p.~$isitswap~ /system/bin/user.conf -i
$MOUNT_CMD -o ro,remount /system
# Firerat 2010-03-30 - check for cache hack
if [ -d /system/csys ]; then
$MOUNT_CMD -o ro,remount /system/csys
fi
break
else
echo "$isitswap is not swap"
fi
done
fi
return
}

DalvikCache ()
{
# doubt they are going to change, but shoud set the 'paths' as variables
DataTotal_K=`$DF_CMD|$GREP_CMD \/data\:|$CUT_CMD -d " " -f2|$SED_CMD s/K//`
DataFree_K=`$DF_CMD|$GREP_CMD \/data\:|$CUT_CMD -d " " -f6|$SED_CMD s/K//`
DataDalvik_K=`$DU_CMD /data/dalvik-cache/|$CUT_CMD -f1`
# Pickup min free on data from config in future
Min_Data_Free_MB=10
Buffer_K=`expr $Min_Data_Free_MB \* 1024`
DataApp_K=`$DU_CMD /system/app/|$CUT_CMD -f1`
System_App_K=`$DU_CMD /data/app/|$CUT_CMD -f1`
if [ -d /system/sd/dalvik-cache ];
then
SD_Dalvik_K=`busybox $DU_CMD /system/sd/dalvik-cache|$CUT_CMD -f1`
else
SD_Dalvik_K="0"
fi
# Firerat-TODO - I'll finish these some other time
#echo "Data Partition size : ${DataTotal_K}K"
#echo "Free on Data Partition : ${DataFree_K}K"
#echo $DataDalvik_K
#echo $Min_Data_Free_MB
#echo $Buffer_K
#echo $DataApp_K
#echo $System_App_K
#echo $SD_Dalvik_K

if [ ! -d /system/sd/dalvik-cache ] && [ "$DataDalvik_K" -lt "1024" ] && [ "`expr \( $DataApp_K + $System_App_K \) \/2`" -lt "$DataFree_K" ];
then
DalvikToIntMem
else
# if DalvikCache + a bit is too big, move to SD card
if [ "`expr $DataDalvik_K + $Buffer_K`" -gt "`expr $DataFree_K + $DataDalvik_K`" ];
then
DalvikToSdCard
else
if [ "`expr "$SD_Dalvik_K" + "$Buffer_K"`" -lt "$DataFree_K" ];
then
DalvikToIntMem
fi
fi
fi
DalvikComplete
return
}

DalvikToIntMem ()
{

if [ -d /system/sd/dalvik-cache ];
then
# hmm, I should be not lazy look before I leap
$MKDIR_CMD /data/dalvik-cache-temp
$CHOWN_CMD 1000:1000 /data/dalvik-cache-temp
$CHMOD_CMD 771 /data/dalvik-cache-temp
$CP_CMD -a /system/sd/dalvik-cache/* /data/dalvik-cache-temp/
$UMOUNT_CMD /data/dalvik-cache
$RM_CMD -rf /data/dalvik-cache
$MV_CMD /data/dalvik-cache-temp /data/dalvik-cache
$RM_CMD -rf /system/sd/dalvik-cache
else
if [ ! -d /data/dalvik-cache ];
then
$MKDIR_CMD /data/dalvik-cache
fi
$CHOWN_CMD 1000:1000 /data/dalvik-cache
$CHMOD_CMD 771 /data/dalvik-cache
fi
return
}

DalvikToSdCard ()
{
$MKDIR_CMD /system/sd/dalvik-cache/
$CP_CMD -a /data/dalvik-cache/* /system/sd/dalvik-cache/
$RM_CMD /data/dalvik-cache/*
return
}

DalvikComplete ()
{
if [ -d /system/sd/dalvik-cache ] && [ "`$MOUNT_CMD |$GREP_CMD -q "/data/dalvik-cache";echo $?`" != "0" ];
then
$MOUNT_CMD -o bind /system/sd/dalvik-cache /data/dalvik-cache;
fi
$CHOWN_CMD 1000:1000 /data/dalvik-cache;
$CHMOD_CMD 771 /data/dalvik-cache;
return
}

ReturnAppsToData ()
{
# Firerat 2010-03-30
# only running this for none G1s, like mt3g
# allows this patched a2sd to be used on existing install that emulates G1 with broken keyboard
# will only move apps / dalvik i\f there is enough space on /data
# I can probably use this to move apps an a G1, but for now only calling if not g1
if [ -e /dev/block/mmcblk0p2 ] && [ "$isitswap" != "/dev/block/mmcblk0p2" ];
then
Min_Data_Free_MB=10
Buffer_K=`expr $Min_Data_Free_MB \* 1024`
DataFree_K=`$DF_CMD|$GREP_CMD \/data\:|$CUT_CMD -d " " -f6|$SED_CMD s/K//`
TotalAppsOnSD="0"
MountExt
# check to make sure apps on sd are not too large
for i in app app_s app-private dalvik-cache; do
if [ ! -d /system/sd/$i ];
then
SizeOnSD$i=`busybox $DU_CMD /system/sd/$i|$CUT_CMD -f1`
else
SizeOnSD$i="0"
fi
TotalAppsOnSD=`expr "$TotalAppsOnSD" + "SizeOnSD$i"`
done

if [ "$TotalAppsOnSD" -lt "`expr "$DataFree_K" - "$Buffer_K"`" ];
then
# Firerat 2010-03-30 NB, I'm not moving Dalvik-c here , as I already have a function for that
for i in app app_s app-private; do
if [ -d /system/sd/$i ] && [ -L /data/$i ];
then
# Firerat 2020-03-31 - Copy to temp, then remove link and move temp in place
$CP_CMD -a /system/sd/$i /data/$i-temp
$RM_CMD /data/$i
$MV_CMD /data/$i-temp /data/$i

if [ "$?" = "0" ];
then
$RM_CMD -rf /system/sd/$i
fi
fi
done
DalvikToIntMem
$UMOUNT_CMD /dev/block/mmcblk0p2
return
else
DalvikComplete
return
fi
else
return
fi
}

Original Code

#!/system/bin/sh
#
# Apps2SD using symlinks and bind mounts
# shade@chemlab.org (cyanogen) Thanks Cy! & ccyrowski
#

echo "+++ Welcome to Android `getprop ro.build.version.release` / `getprop ro.modversion`";

sysctl -p

if [ -e /dev/block/mmcblk0p2 ];
then
# fsck the sdcard filesystem first
e2fsck -fy /dev/block/mmcblk0p2;

# set property with exit code in case an error occurs
setprop cm.e2fsck.errors $?;

# mount and set perms
busybox mount -o noatime,nodiratime -t auto /dev/block/mmcblk0p2 /system/sd;
busybox chown 1000:1000 /system/sd;
busybox chmod 771 /system/sd;

#busybox mount /sdcard;

# clean up any old symlinks, create data directories
for i in dalvik-cache data;
do
if [ -h /data/$i ];
then
rm /data/$i;
fi;
if [ ! -d /data/$i ];
then
mkdir /data/$i;
busybox chown 1000:1000 /data/$i;
busybox chmod 771 /data/$i;
fi;
done;

# don't allow /data/data on sd because of upgrade issues - move it if possible
if [ -d /system/sd/data ];
then
busybox cp -a /system/sd/data/* /data/data/;
busybox rm -rf /system/sd/data;
fi;

# move apps and dalvik cache from internal memory to sdcard
for i in app app_s app-private dalvik-cache;
do
if [ ! -d /system/sd/$i ];
then
mkdir /system/sd/$i;
fi

busybox chown 1000:1000 /system/sd/$i;
busybox chmod 771 /system/sd/$i

if [ -d /data/$i ] && [ ! -h /data/$i ];
then
busybox cp -a /data/$i/* /system/sd/$i/;
busybox rm -f /data/$i/*;
fi;
done;

# symlink app dirs - they must be on the same filesystem
for i in app app_s app-private;
do
if [ -d /data/$i ] && [ ! -h /data/$i ];
then
busybox rm -rf /data/$i;
busybox ln -s /system/sd/$i /data/$i;
fi;
done;

# bind mount dalvik-cache so we can still boot without the sdcard
busybox mount -o bind /system/sd/dalvik-cache /data/dalvik-cache;
busybox chown 1000:1000 /data/dalvik-cache;
busybox chmod 771 /data/dalvik-cache;

# clean up old whiteouts
for i in local misc property system tombstones data;
do
if [ -h /system/sd/$i ]; then rm -f /system/sd/$i; fi
done;

# please don't put odex files in the app directory people!
# it causes dexopt to crash when switching builds!
busybox rm -f /system/sd/app/*.odex

else

# replace symlinks with directories so we can boot without sd
for i in app app-private;
do
if [ -h /data/$i ];
then
rm -f /data/$i;
mkdir /data/$i;
busybox chown 1000:1000 /data/$i;
busybox chmod 771 /data/$i;
fi;
done;
fi;

sync;

setprop cm.filesystem.ready 1;