#!/system/xbin/bb/sh if [ "$1" != "--nolog" ] ; then echo "$0" "$@" >> /data/sdsplit.log "$0" --nolog "$@" 2>&1 | tee -a /data/sdsplit.log exit fi shift var_get(){ eval echo '"$'"$1"'"' ; } # v1.0 # varname > value var_set() { eval "$1='$2'" ; } # v1.0 # varname val stderr(){ echo "$@" >&2 ; } msg() { stderr "--------------------------------------" ; stderr "+$1" ; } warn() { stderr "==WARNING: $1" ; } error() { stderr >&2 ; stderr "===ERROR: $1" ; exit ; } used() { du -s "$1" | (read k x; echo $k) ; } free() { df "$1" |tail -1 | (read x1 x2 x3 x4 x5 k x7 ; echo $k) ; } createsparse() # BUG: this will be one byte larger than requested { # file size dd if=/dev/zero of="$1" bs=1 count=1 seek="$2" } exeget() # download an executable { # exe url msg msg "$3, see if we can download it from $2" (cd /cache ; wget "$2") [ ! -d "$DL" ] && mkdir "$DL" [ ! -d "$DLB" ] && mkdir "$DLB" cp /cache/`basename "$2"` "$DLB/$1" rm /cache/`basename "$2"` chmod 555 "$DLB/$1" } exetest_mkdosfs() { # exep if [ -z "$SW_NP" ] ; then # Let's create a sparse test partition to see if this mkdosfs # can handle it. testp="$TESTP" createsparse "$testp" "$FSIZE" else # Since we are not creating the partition now, it must already # be created so we can test the real partition. testp="$MPF" fi rtn=`"$1" -N "$testp" 2>&1 | grep "lseek returned -1"` echo "$rtn" [ ! -z "$rtn" ] ; rtn=$? [ "$testp" = "$TESTP" ] && rm "$testp" 2> /dev/null return $rtn } exetest_mke2fs() { # exep "$1" -h 2> /dev/null err=$? [ $err -ne 1 -a $err -ne 0 ] } execheck() { # exename force msg_download msg "Checking validity of $1" if exetest_"$1" `var_get EXE_"$1"` ; then exeget "$1" `var_get EXEURL_"$1"` "$3" if exetest_"$1" "$DLB/$1" ; then [ ! -z "$2" ] || error "Cannot find a valid $1 executable" else var_set EXE_"$1" "$DLB/$1" fi fi } backup_sd_err() { echo "==ERROR: Could not backup $MPF to $1." echo echo "Check that you have enough space in $1 to fit all of the contents of $MPF. If not, either delete some files from $MPF or choose a new location for your backup." echo k=`used "$MPF"` echo " Used space on $MPF: ${k}K" k=`free "$1"` echo " Free space on $1 partition: ${k}K" exit } backup_sd() { msg "Backing up $MPF to $BDIR" [ ! -d "$BDIR" ] && mkdir -p "$BDIR" cp -a "$MPF" "$1" && return rm -r "$BDIR$MPF" backup_sd_err "$1" >&2 } umount_err() { echo "==ERROR: Could not unmount $MPF!" echo echo "Perhaps an application is holding a file open? Stop running applications and try again." echo exit } fdiska() { # line ... (while [ $# -gt 0 ] ; do echo "$1" ; shift ; done) | { fdisk "$DEV" || return 1 ; } return 0 } partition() { msg "Partitioning sdcard" x=`df|grep "^$MPF":` [ ! -z "$x" ] && { umount "$MPF" || umount_err >&2 ; } msg " Blank out the 4 first blocks of the sdcard so that mountd does not try to remount it on fdisk write preventing a kernel partition table re-read." dd if=/dev/zero of="$DEV" count=4 msg " Wipe partition table and create FAT$FAT $FSIZE/EXT2 $ESIZE partitions" [ ! -z "$ESIZE" ] && ESIZE="+$ESIZE" fdiska o n p 1 '' "+$FSIZE" t "$FT" p n p 2 '' "$ESIZE" t 2 83 p w || error "Unable to create new partitions" } mkfses() { msg "Creating FAT$FAT Filesystem" "$EXE_mkdosfs" -F "$FAT" "$DEV1" msg "Creating EXT2 Filesystem" "$EXE_mke2fs" "$DEV2" msg "Mounting FAT Filesystem" mount "$DEV1" "$MPF" msg "Mounting EXT2 Filesystem" if [ -f "$KOD/ext2.ko" ] ; then insmod "$KOD/ext2.ko" else stderr "Did not find ext2.ko, (normal on JF1.5)" fi mount "$DEV2" "$MPE" msg "You should now have a FAT partition on $MPF and an EXT2 partition on $MPE. If things worked, you should see an entry for $MPF and $MPE below:" echo >&2 df | grep -e "$MPF" -e "$MPE" >&2 echo >&2 } yN() { read r ; [ "$r" = "y" -o "$r" = "Y" -o "$r" = "yes" -o "$r" = "YES" ] ; } lcp() { err='' while read f ; do cp -dr "$BDIR$MPF/$f" "$MPF" || err=1 done echo "Permission setting errors are normal on a FAT system" >&2 [ -z "$err" ] || return 1 } restore() { msg "Restoring $BDIR$MPF to $MPF" ls -a "$BDIR$MPF" | grep -v '^.$'|grep -v '^..$'| lcp || error "restore failed!" echo "Would you like to remove the backup under $BDIR$MPF? y/N" >&2 yN && rm -rf "$BDIR$MPF" } configs_error() { mount -o ro,remount -t yaffs2 /dev/block/mtdblock3 /system error "$1" } fbackup() { # -c file [ "$1" = "-c" ] && { cfg=configs_ ; shift ; } [ ! -f "$1" ] && return if [ -f "$1".orig ] ; then echo "WARNING: $1 backup $1.orig already exists!" >&2 echo >&2 echo "Do you want to overwrite it? y/N" >&2 yN || return rm "$1".orig fi cp "$1" "$1".orig || "$cfg"error "Unable to backup $1" } configs() { msg "Backing up and Updating $CFGI" mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system fbackup -c "$CFGI" cat > "$CFGI" < "$CFGM" <&2 <&2 < GNU GPL2 or above EOF } VER=1.7 PROG=`basename $0` MPF=/sdcard MPE=/system/sd DEV=/dev/block/mmcblk0 DEV1=/dev/block/mmcblk0p1 DEV2=/dev/block/mmcblk0p2 FT=b FAT=32 FSIZE="500M" ESIZE="" CFGI=/system/init.rc CFGM=/system/etc/mountd.conf UNAME=`uname -r` KOD="/system/modules/$UNAME/kernel/fs/ext2" BDIR=/data TESTP="/sqlite_stmt_journals/test_partition" # Must be on a FS supporting sparse files DL=/data/local DLB="$DL"/bin EXE_mkdosfs=mkdosfs EXE_mke2fs=mke2fs TFN=64.105.21.209 # theficks.name EXEURL_mkdosfs="http://$TFN/bin/lib/droid/mkdosfs" EXEURL_mke2fs="http://$TFN/bin/lib/droid/mke2fs" [ $# -eq 0 ] && HELPWARN=1 while [ $# -gt 0 ] ; do case "$1" in -h) Usage ; exit ;; --help) Help ; exit ;; -b|--backup) clearactions ; SW_NB='' ;; -c|--configs) clearactions ; SW_NC='' ;; -p|--partition) clearactions ; SW_NP='' ;; -m|--mkfs) clearactions ; SW_NM='' ;; -r|--restore) clearactions ; SW_NR='' ;; -nd|--nodata) SW_NB="$1" ; SW_NR="$1" ;; -nb|--nobackup) SW_NB="$1" ;; -nc|--noconfigs) SW_NC="$1" ;; -np|--nopartition) SW_NP="$1" ;; -nm|--nomkfs) SW_NM="$1" ;; -nr|--norestore) SW_NR="$1" ;; -f|--force) SW_FORCE="$1" ;; -fm|--forcemkfs) SW_FORCEM="$1" ;; -fu|--forceumount|--forceunmount) SW_FORCEU="$1" ;; -pme|--pmke2fs) shift ; EXE_mke2fs="$1" ;; -pmd|--pmkdosfs) shift ; EXE_mkdosfs="$1" ;; -pe|--pext2) shift ; MPE="$1" ;; -pb|--pbackup) shift ; BDIR="$1" echo "$BDIR" | grep "^$MPF/" > /dev/null && error "backup dir ($BDIR) cannot be a subdirectory of $MPF!" [ "$BDIR" = "$MPF" ] && error "cannot backup $MPF to itself!" [ -d "$BDIR$MPF" ] && error "backup dir ($BDIR$MPF) already exists!" ;; -f32|--fat32) FAT=32 ; FT=b ;; -f16|--fat16) FAT=16 ; FT=6 ;; -fs|--fatsize) shift ; FSIZE="$1" ;; -es|--ext2size) shift ; ESIZE="$1" ;; *) Usage ; error "invalid argument $1" ; exit ;; esac shift done msg "You have chosen to perform the following actions:" [ -z "$SW_NB" ] && stderr ".BACKUP /sdcard contents to $BDIR" [ -z "$SW_NC" ] && stderr ".CONFIGURE system to mount EXT2 partition" [ -z "$SW_NC" ] && stderr " !WARNING! Do NOT do this on JF1.5 builds!" [ -z "$SW_NP" ] && stderr ".REPARTITION sdcard: $FSIZE FAT /$ESIZE EXT2" [ -z "$SW_NP" ] && stderr " !WARNING! Will DELETE data on sdcard!" [ -z "$SW_NM" ] && stderr ".MAKE FAT$FAT and EXT2 filesystems" [ -z "$SW_NR" ] && stderr ".RESTORE $BDIR to sdcard" if [ -z "$SW_FORCE" ] ; then stderr [ ! -z "$HELPWARN" ] && stderr "(try $0 --help if you wanted the help instead)" stderr "ARE YOU SURE YOU WANT TO PROCEED? y/N" yN || exit fi if [ -z "$SW_NM" ] ; then execheck mkdosfs "$SW_FORCEM" "Cannot find a mkdosfs executable that can handle your partition size." execheck mke2fs "$SW_FORCEM" "Cannot find a mke2fs executable." fi if [ -z "$SW_NP" -o -z "$SW_NP" ] ; then x=`df|grep "^$MPE":` if [ ! -z "$x" ] ; then [ -z "$SW_FORCEU" ] && error "A partition is already mounted a $MPE. See --forceumount option." umount "$MPE" || error "Cannot unmount $MPE" fi fi [ -z "$SW_NB" ] && backup_sd "$BDIR" [ -z "$SW_NC" ] && configs [ -z "$SW_NP" ] && partition [ -z "$SW_NM" ] && mkfses [ -z "$SW_NR" ] && restore