chris41g

test1

Jun 30th, 2011
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.39 KB | None | 0 0
  1. #!/sbin/sh
  2. #
  3. # This program is free software; you can redistribute it and/or modify it
  4. # under the terms of the GNU Library General Public License as published
  5. # by the Free Software Foundation; either version 2, or (at your option)
  6. # any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. # GNU General Public License for more details.
  12. #
  13. # License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>
  14. #
  15. # Written by:
  16. # Rodderik <[email protected]>
  17. # http://devphone.org
  18. #
  19. # sdparted v0.6 by 51dusty was a big help in writing this script. Thanks 51dusty!
  20. #
  21. # Version 0.1 - initial version
  22. #
  23.  
  24. #################
  25. # BEGIN OPTIONS #
  26. #################
  27. #for CWM ui output
  28. OUTFD=$(ps | grep -v "grep" | grep -o -E "update_binary(.*)" | cut -d " " -f 3);
  29. #define partition sizes in MB
  30. SYSTEMSIZE="270"
  31. DATASIZE="520"
  32. CACHESIZE="180"
  33. SDPATH="/dev/block/mmcblk0"
  34.  
  35. #define partition names
  36. SDNAME="SDCARD"
  37. SYSTEMNAME="SYSTEM"
  38. DATANAME="DATA"
  39. CACHENAME="CACHE"
  40.  
  41. #partition order (do not change these)
  42. #fat partition MUST be first
  43. SYSTEMPART="p2"
  44. DATAPART="p3"
  45. CACHEPART="p4"
  46.  
  47. #file system specific options
  48. FILESYSTEM="ext4"
  49. EXT4OPTS="^resize_inode,^ext_attr,^huge_file"
  50. FATOPTS="-S 2048 -s 1 -F 32"
  51. FATOPTSCACHE="-S 4096 -s 1 -F 16"
  52.  
  53. #take care of binaries
  54. PARTEDBIN="/sbin/parted"
  55. FATBIN="/sbin/fat.format"
  56. MEXT4BIN="/sbin/mkfs.ext4"
  57. TEXT4BIN="/sbin/tune2fs.ext4"
  58.  
  59. #script options
  60. LOGFILE="/data/dbpart.log"
  61. SUPRESS=
  62. DRYRUN=
  63.  
  64. ###############
  65. # END OPTIONS #
  66. ###############
  67.  
  68. # do logging, if not excluded with -x
  69. [ "$1" != "-x" ] && echo "$0" "$@" >> "$LOGFILE" && "$0" -x "$@" 2>&1 | tee -a "$LOGFILE" && exit
  70. shift
  71.  
  72.  
  73. echo1() {
  74.   if [ ${OUTFD} ]; then
  75.     echo "ui_print ${1} " 1>&$OUTFD;
  76.     echo "ui_print " 1>&$OUTFD;
  77.   else
  78.     if [ ${#} -lt 1 ]; then
  79.     echo .;
  80.     else
  81.     echo "${1}";
  82.     fi;
  83.   fi;
  84. }
  85.  
  86. DISPHELP () {
  87. cat <<DONEDISPHELP
  88. ${SCRIPTNAME} v${SCRIPTVER}
  89. Brought to you by ${SCRIPTAUTH}
  90.  
  91. This script aids in preparing an sdcard for dual booting with supported
  92. kernels.
  93.  
  94. Requirements:
  95. Sdcard 2 gigabytes or larger
  96. Dual boot supported kernel
  97. Tested with parted 1.8.8.1.179-aef3 compiled for android.
  98.  
  99. Usage:  ${SCRIPTNAME} [options]
  100.    
  101. Options:
  102.  
  103.  -fs|--filesystem    Sets desired file system for sdcard partitions.
  104.                  Valid types: rfs, ext4
  105.                      Default is 'ext4'.
  106.  
  107.  -s|--supress        Supress all information and user prompts.  Warnings are
  108.                      still displayed.  Useful for calling script externally.
  109.                      Default option is to NOT suppress information and prompts.
  110.    
  111.  -dr|--dryrun        Performs a dry run of the script that will show proposed
  112.                      partition sizes without making any changes to the actual
  113.                      sdcard. Automatically ignores --supress option.
  114.                      Default option is to NOT do a dry run.
  115.  
  116.  -h|--help           Display this help menu.
  117.  
  118. Examples:
  119. ${SCRIPTNAME}               Partitions sdcard default options.
  120. ${SCRIPTNAME} -fs rfs       Partitions sdcard with rfs and default options.
  121. ${SCRIPTNAME} -dr -fs rfs   Performs dry run and displays proposed partition
  122.                             information using rfs file system.
  123. ${SCRIPTNAME} -s            Supress all user input and information. This WILL
  124.                             DESTROY existing partitions the sdcard with out any
  125.                             warning or conformation.
  126.  
  127. DONEDISPHELP
  128. }
  129.  
  130. SHOWERROR() {
  131. echo1
  132. echo1 "Error: $1"
  133. echo1
  134. exit 1
  135. }
  136.  
  137. ECHOIT() {
  138. if [ -z $SUPRESS ]; then
  139.     echo1 "Info: $1"
  140. fi
  141. }
  142.  
  143. BINCHK () {
  144. #check for binaries
  145. if [ ! -e $PARTEDBIN ] && [ -f $PARTEDBIN ]; then
  146.     SHOWERROR "${PARTEDBIN} is missing."
  147. fi
  148. if [ ! -e $FATBIN ] && [ -f $FATBIN ] && [ $FILESYSTEM == "rfs" ]; then
  149.     SHOWERROR "${FATBIN} is missing."
  150. fi
  151. if [ ! -e $MEXT4BIN ] && [ -f $MEXT4BIN ] && [ $FILESYSTEM == "ext4" ]; then
  152.     SHOWERROR "${MEXT4BIN} is missing."
  153. fi
  154. if [ ! -e $TEXT4BIN ] && [ -f $TEXT4BIN ] && [ $FILESYSTEM == "ext4" ]; then
  155.     SHOWERROR "${TEXT4BIN} is missing."
  156. fi
  157. }
  158.  
  159. RUNSTRING () {
  160. #construct run string
  161. if [ -n $SUPRESS ]; then
  162.     DOPARTED="${PARTEDBIN} -s ${SDPATH}"
  163. else
  164.     DOPARTED="${PARTEDBIN} ${SDPATH}"
  165. fi
  166. }
  167.  
  168. FORMATEXT4 () {
  169. #format ext4
  170. ECHOIT "Formatting system"
  171. $MEXT4BIN -O ${EXT4OPTS} -L ${SYSTEMNAME} -J size=16 -b 4096 -m 0 -F -N 7500 ${SDPATH}${SYSTEMPART} > /dev/null 2>&1 >>"$LOGFILE"
  172. $TEXT4BIN -O +has_journal -c 30 -i 30d -m 0 -o journal_data_writeback ${SDPATH}${SYSTEMPART} 2>&1 >>"$LOGFILE"
  173.  
  174. ECHOIT "Formatting data"
  175. $MEXT4BIN -O ${EXT4OPTS} -L ${DATANAME} -J size=16 -b 4096 -m 0 -F -N 7500 ${SDPATH}${DATAPART} > /dev/null 2>&1 >>"$LOGFILE"
  176. $TEXT4BIN -O +has_journal -c 30 -i 30d -m 0 -o journal_data_writeback ${SDPATH}${DATAPART} 2>&1 >>"$LOGFILE"
  177.  
  178. ECHOIT "Formatting cache"
  179. $MEXT4BIN -O ${EXT4OPTS} -L ${CACHENAME} -J size=16 -b 4096 -m 0 -F -N 7500 ${SDPATH}${CACHEPART} > /dev/null 2>&1 >>"$LOGFILE"
  180. $TEXT4BIN -O +has_journal -c 30 -i 30d -m 0 -o journal_data_writeback ${SDPATH}${CACHEPART} 2>&1 >>"$LOGFILE"
  181. }
  182.  
  183. FORMATRFS () {
  184. #format rfs
  185. ECHOIT "Formatting system"
  186. $FATBIN $FATOPTS ${SDPATH}${SYSTEMPART} 2>&1 >>"$LOGFILE"
  187.  
  188. ECHOIT "Formatting data"
  189. $FATBIN $FATOPTS ${SDPATH}${DATAPART} 2>&1 >>"$LOGFILE"
  190.  
  191. ECHOIT "Formatting cache"
  192. $FATBIN $FATOPTSCACHE ${SDPATH}${CACHEPART} 2>&1 >>"$LOGFILE"
  193. }
  194.  
  195. UNMOUNTPARTS () {
  196. mount | grep /mmcblk | cut -d" " -f1 | while read line; do
  197.     ECHOIT "Unmounting ${line}"  
  198.     umount -f $line 2>&1 >>"$LOGFILE"
  199. done
  200. }
  201.  
  202. NUKEPARTS () {
  203. #nuke partitions
  204. ECHOIT "Nuking existing partition(s)"
  205. $DOPARTED rm 1 > /dev/null 2>&1
  206. $DOPARTED rm 2 > /dev/null 2>&1
  207. $DOPARTED rm 3 > /dev/null 2>&1
  208. $DOPARTED rm 4 > /dev/null 2>&1
  209. }
  210.  
  211. GETSDINFO () {
  212. #get sdcard size
  213. SDSIZEMB=`${DOPARTED} unit MB print | grep ${SDPATH} | cut -d" " -f3`
  214. SDSIZE=${SDSIZEMB%MB}
  215. }
  216.  
  217. CALCLAYOUT () {
  218. #calculate partition layout
  219. FPARTEND=$(($SDSIZE - $SYSTEMSIZE - $DATASIZE - $CACHESIZE))
  220. SPARTEND=$(($FPARTEND + $SYSTEMSIZE))
  221. DPARTEND=$(($SPARTEND + $DATASIZE))
  222. CPARTEND=$(($DPARTEND + $CACHESIZE))
  223. }
  224.  
  225. CREATEPARTS () {
  226. #create partitions
  227. ECHOIT "Creating partitions. This may take a while."
  228. ECHOIT "Creating partition 1."
  229. $DOPARTED mkpartfs primary fat32 0 ${FPARTEND}MB 2>&1 >>"$LOGFILE"
  230. ECHOIT "Creating partition 2."
  231. $DOPARTED mkpartfs primary ext2 ${FPARTEND}MB ${SPARTEND}MB 2>&1 >>"$LOGFILE"
  232. ECHOIT "Creating partition 3."
  233. $DOPARTED mkpartfs primary ext2 ${SPARTEND}MB ${DPARTEND}MB 2>&1 >>"$LOGFILE"
  234. ECHOIT "Creating partition 4."
  235. $DOPARTED mkpartfs primary ext2 ${DPARTEND}MB ${CPARTEND}MB 2>&1 >>"$LOGFILE"
  236. }
  237.  
  238. SANITYCHECKS () {
  239. #check sdcard minimum size
  240. if [ $SDSIZE -le 1800 ]; then
  241.     SHOWERROR "Could not read sdcard size correctly or sdcard is less than 2 gigabytes in size."
  242. fi
  243.  
  244. if [ $SDSIZE -lt $CPARTEND ]; then
  245.     SHOWERROR "Error calculating partition sizes."
  246. fi
  247.  
  248. if [ $FPARTEND -le 0 ]; then
  249.     SHOWERROR "Fat partition must be greater than O megabytes."
  250. fi
  251. }
  252.  
  253. CHECKFS() {
  254. # validating argument
  255. if [ "$1" != "rfs" ] && [ "$1" != "ext4" ]; then
  256.     SHOWERROR "$1 is not a valid filesystem."
  257. else
  258.     FILESYSTEM="$1"
  259. fi
  260. }
  261.  
  262. USERCANCEL() {
  263. ECHOIT "Canceled by user. Now exiting."
  264. exit 0
  265. }
  266.  
  267. SUPRESSCHK() {
  268. if [ ! -z $DRYRUN ]; then
  269.     SUPRESS=""
  270. fi
  271. }
  272.  
  273. SCRIPTNAME=$0
  274. SCRIPTVER="0.1"
  275. SCRIPTAUTH="Rodderik ([email protected])"
  276.  
  277. # command line parsing
  278. while [ $# -gt 0 ]; do
  279.   case "$1" in
  280.  
  281.     -h|--help) DISPHELP ; exit 0 ;;
  282.  
  283.     -fs|--filesystem) shift ; CHECKFS "$1" ;;
  284.  
  285.     -s|--supress) SUPRESS="$1" ;;
  286.  
  287.     -dr|--dryrun) DRYRUN="$1" ;;
  288.  
  289.     *) DISPHELP ; SHOWERROR "Unknown command-line argument '$1'" ;;
  290.  
  291.   esac
  292.   shift
  293. done
  294.  
  295.  
  296. #do stuff right meow!
  297. #disable supression for dry run
  298. SUPRESSCHK
  299.  
  300. # give the output some breathing room
  301. echo "-------------------------------------------------------------------------------" >> "$LOGFILE"
  302.  
  303. #check for binaries
  304. BINCHK
  305.  
  306. #construct run string
  307. RUNSTRING
  308.  
  309. #snag sdcard information
  310. GETSDINFO
  311.  
  312. #calculate new layout
  313. CALCLAYOUT
  314.  
  315. #double check ourselves
  316. SANITYCHECKS
  317.  
  318. if [ -z $SUPRESS ]; then
  319.     echo1 "${SCRIPTNAME} v${SCRIPTVER}"
  320.     echo1 "Brought to you by ${SCRIPTAUTH}"
  321.     echo1
  322.     echo1 "Disk ${SDPATH}: ${SDSIZEMB}"
  323.     echo1
  324.     echo1 "I am going to make the following changes to your sdcard:"
  325.     echo1
  326.     echo1 "Partition 1: Name: ${SDNAME} File System: fat32 Size: ${FPARTEND}MB"
  327.     echo1 "Partition 2: Name: ${SYSTEMNAME} File System: ${FILESYSTEM} Size: ${SYSTEMSIZE}MB"
  328.     echo1 "Partition 3: Name: ${DATANAME} File System: ${FILESYSTEM} Size: ${DATASIZE}MB"
  329.     echo1 "Partition 4: Name: ${CACHENAME} File System:  ${FILESYSTEM} Size: ${CACHESIZE}MB"
  330.     echo1
  331.     if [ -z $DRYRUN ]; then
  332.         echo1 "WARNING!!! THIS WILL DESTROY ALL DATA ON SDCARD!!!"
  333.         read -s -n1 -p "Press c to confirm, any other key to cancel. " key
  334.         case $key in
  335.         "C" | "c") ;;
  336.         *) USERCANCEL;;
  337.         esac
  338.     fi
  339. fi
  340.  
  341. if [ -z $DRYRUN ]; then
  342.     UNMOUNTPARTS
  343.     NUKEPARTS
  344.     CREATEPARTS
  345.     if [ $FILESYSTEM == "ext4" ]; then
  346.         FORMATEXT4
  347.     elif [ $FILESYSTEM == "rfs" ]; then
  348.         FORMATRFS
  349.     else
  350.         SHOWERROR "Unable to select filesystem type."
  351.     fi
  352.     ECHOIT "Operation seems to have completed successfully."
  353. fi
  354. exit 0
Advertisement
Add Comment
Please, Sign In to add comment