chris41g

test

Jun 29th, 2011
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.35 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.     echo "${1}";
  79.   fi;
  80. }
  81.  
  82. DISPHELP () {
  83. cat <<DONEDISPHELP
  84. ${SCRIPTNAME} v${SCRIPTVER}
  85. Brought to you by ${SCRIPTAUTH}
  86.  
  87. This script aids in preparing an sdcard for dual booting with supported
  88. kernels.
  89.  
  90. Requirements:
  91. Sdcard 2 gigabytes or larger
  92. Dual boot supported kernel
  93. Tested with parted 1.8.8.1.179-aef3 compiled for android.
  94.  
  95. Usage:  ${SCRIPTNAME} [options]
  96.    
  97. Options:
  98.  
  99.  -fs|--filesystem    Sets desired file system for sdcard partitions.
  100.                  Valid types: rfs, ext4
  101.                      Default is 'ext4'.
  102.  
  103.  -s|--supress        Supress all information and user prompts.  Warnings are
  104.                      still displayed.  Useful for calling script externally.
  105.                      Default option is to NOT suppress information and prompts.
  106.    
  107.  -dr|--dryrun        Performs a dry run of the script that will show proposed
  108.                      partition sizes without making any changes to the actual
  109.                      sdcard. Automatically ignores --supress option.
  110.                      Default option is to NOT do a dry run.
  111.  
  112.  -h|--help           Display this help menu.
  113.  
  114. Examples:
  115. ${SCRIPTNAME}               Partitions sdcard default options.
  116. ${SCRIPTNAME} -fs rfs       Partitions sdcard with rfs and default options.
  117. ${SCRIPTNAME} -dr -fs rfs   Performs dry run and displays proposed partition
  118.                             information using rfs file system.
  119. ${SCRIPTNAME} -s            Supress all user input and information. This WILL
  120.                             DESTROY existing partitions the sdcard with out any
  121.                             warning or conformation.
  122.  
  123. DONEDISPHELP
  124. }
  125.  
  126. SHOWERROR() {
  127. echo1
  128. echo1 "Error: $1"
  129. echo1
  130. exit 1
  131. }
  132.  
  133. ECHOIT() {
  134. if [ -z $SUPRESS ]; then
  135.     echo1 "Info: $1"
  136. fi
  137. }
  138.  
  139. BINCHK () {
  140. #check for binaries
  141. if [ ! -e $PARTEDBIN ] && [ -f $PARTEDBIN ]; then
  142.     SHOWERROR "${PARTEDBIN} is missing."
  143. fi
  144. if [ ! -e $FATBIN ] && [ -f $FATBIN ] && [ $FILESYSTEM == "rfs" ]; then
  145.     SHOWERROR "${FATBIN} is missing."
  146. fi
  147. if [ ! -e $MEXT4BIN ] && [ -f $MEXT4BIN ] && [ $FILESYSTEM == "ext4" ]; then
  148.     SHOWERROR "${MEXT4BIN} is missing."
  149. fi
  150. if [ ! -e $TEXT4BIN ] && [ -f $TEXT4BIN ] && [ $FILESYSTEM == "ext4" ]; then
  151.     SHOWERROR "${TEXT4BIN} is missing."
  152. fi
  153. }
  154.  
  155. RUNSTRING () {
  156. #construct run string
  157. if [ -n $SUPRESS ]; then
  158.     DOPARTED="${PARTEDBIN} -s ${SDPATH}"
  159. else
  160.     DOPARTED="${PARTEDBIN} ${SDPATH}"
  161. fi
  162. }
  163.  
  164. FORMATEXT4 () {
  165. #format ext4
  166. ECHOIT "Formatting system"
  167. $MEXT4BIN -O ${EXT4OPTS} -L ${SYSTEMNAME} -J size=16 -b 4096 -m 0 -F -N 7500 ${SDPATH}${SYSTEMPART} > /dev/null 2>&1 >>"$LOGFILE"
  168. $TEXT4BIN -O +has_journal -c 30 -i 30d -m 0 -o journal_data_writeback ${SDPATH}${SYSTEMPART} 2>&1 >>"$LOGFILE"
  169.  
  170. ECHOIT "Formatting data"
  171. $MEXT4BIN -O ${EXT4OPTS} -L ${DATANAME} -J size=16 -b 4096 -m 0 -F -N 7500 ${SDPATH}${DATAPART} > /dev/null 2>&1 >>"$LOGFILE"
  172. $TEXT4BIN -O +has_journal -c 30 -i 30d -m 0 -o journal_data_writeback ${SDPATH}${DATAPART} 2>&1 >>"$LOGFILE"
  173.  
  174. ECHOIT "Formatting cache"
  175. $MEXT4BIN -O ${EXT4OPTS} -L ${CACHENAME} -J size=16 -b 4096 -m 0 -F -N 7500 ${SDPATH}${CACHEPART} > /dev/null 2>&1 >>"$LOGFILE"
  176. $TEXT4BIN -O +has_journal -c 30 -i 30d -m 0 -o journal_data_writeback ${SDPATH}${CACHEPART} 2>&1 >>"$LOGFILE"
  177. }
  178.  
  179. FORMATRFS () {
  180. #format rfs
  181. ECHOIT "Formatting system"
  182. $FATBIN $FATOPTS ${SDPATH}${SYSTEMPART} 2>&1 >>"$LOGFILE"
  183.  
  184. ECHOIT "Formatting data"
  185. $FATBIN $FATOPTS ${SDPATH}${DATAPART} 2>&1 >>"$LOGFILE"
  186.  
  187. ECHOIT "Formatting cache"
  188. $FATBIN $FATOPTSCACHE ${SDPATH}${CACHEPART} 2>&1 >>"$LOGFILE"
  189. }
  190.  
  191. UNMOUNTPARTS () {
  192. mount | grep /mmcblk | cut -d" " -f1 | while read line; do
  193.     ECHOIT "Unmounting ${line}"  
  194.     umount -f $line 2>&1 >>"$LOGFILE"
  195. done
  196. }
  197.  
  198. NUKEPARTS () {
  199. #nuke partitions
  200. ECHOIT "Nuking existing partition(s)"
  201. $DOPARTED rm 1 > /dev/null 2>&1
  202. $DOPARTED rm 2 > /dev/null 2>&1
  203. $DOPARTED rm 3 > /dev/null 2>&1
  204. $DOPARTED rm 4 > /dev/null 2>&1
  205. }
  206.  
  207. GETSDINFO () {
  208. #get sdcard size
  209. SDSIZEMB=`${DOPARTED} unit MB print | grep ${SDPATH} | cut -d" " -f3`
  210. SDSIZE=${SDSIZEMB%MB}
  211. }
  212.  
  213. CALCLAYOUT () {
  214. #calculate partition layout
  215. FPARTEND=$(($SDSIZE - $SYSTEMSIZE - $DATASIZE - $CACHESIZE))
  216. SPARTEND=$(($FPARTEND + $SYSTEMSIZE))
  217. DPARTEND=$(($SPARTEND + $DATASIZE))
  218. CPARTEND=$(($DPARTEND + $CACHESIZE))
  219. }
  220.  
  221. CREATEPARTS () {
  222. #create partitions
  223. ECHOIT "Creating partitions. This may take a while."
  224. ECHOIT "Creating partition 1."
  225. $DOPARTED mkpartfs primary fat32 0 ${FPARTEND}MB 2>&1 >>"$LOGFILE"
  226. ECHOIT "Creating partition 2."
  227. $DOPARTED mkpartfs primary ext2 ${FPARTEND}MB ${SPARTEND}MB 2>&1 >>"$LOGFILE"
  228. ECHOIT "Creating partition 3."
  229. $DOPARTED mkpartfs primary ext2 ${SPARTEND}MB ${DPARTEND}MB 2>&1 >>"$LOGFILE"
  230. ECHOIT "Creating partition 4."
  231. $DOPARTED mkpartfs primary ext2 ${DPARTEND}MB ${CPARTEND}MB 2>&1 >>"$LOGFILE"
  232. }
  233.  
  234. SANITYCHECKS () {
  235. #check sdcard minimum size
  236. if [ $SDSIZE -le 1800 ]; then
  237.     SHOWERROR "Could not read sdcard size correctly or sdcard is less than 2 gigabytes in size."
  238. fi
  239.  
  240. if [ $SDSIZE -lt $CPARTEND ]; then
  241.     SHOWERROR "Error calculating partition sizes."
  242. fi
  243.  
  244. if [ $FPARTEND -le 0 ]; then
  245.     SHOWERROR "Fat partition must be greater than O megabytes."
  246. fi
  247. }
  248.  
  249. CHECKFS() {
  250. # validating argument
  251. if [ "$1" != "rfs" ] && [ "$1" != "ext4" ]; then
  252.     SHOWERROR "$1 is not a valid filesystem."
  253. else
  254.     FILESYSTEM="$1"
  255. fi
  256. }
  257.  
  258. USERCANCEL() {
  259. ECHOIT "Canceled by user. Now exiting."
  260. exit 0
  261. }
  262.  
  263. SUPRESSCHK() {
  264. if [ ! -z $DRYRUN ]; then
  265.     SUPRESS=""
  266. fi
  267. }
  268.  
  269. SCRIPTNAME="dbpart.sh"
  270. SCRIPTVER="0.1"
  271. SCRIPTAUTH="Rodderik ([email protected])"
  272.  
  273. # command line parsing
  274. while [ $# -gt 0 ]; do
  275.   case "$1" in
  276.  
  277.     -h|--help) DISPHELP ; exit 0 ;;
  278.  
  279.     -fs|--filesystem) shift ; CHECKFS "$1" ;;
  280.  
  281.     -s|--supress) SUPRESS="$1" ;;
  282.  
  283.     -dr|--dryrun) DRYRUN="$1" ;;
  284.  
  285.     *) DISPHELP ; SHOWERROR "Unknown command-line argument '$1'" ;;
  286.  
  287.   esac
  288.   shift
  289. done
  290.  
  291.  
  292. #do stuff right meow!
  293. #disable supression for dry run
  294. SUPRESSCHK
  295.  
  296. # give the output some breathing room
  297. echo "-------------------------------------------------------------------------------" >> "$LOGFILE"
  298.  
  299. #check for binaries
  300. BINCHK
  301.  
  302. #construct run string
  303. RUNSTRING
  304.  
  305. #snag sdcard information
  306. GETSDINFO
  307.  
  308. #calculate new layout
  309. CALCLAYOUT
  310.  
  311. #double check ourselves
  312. SANITYCHECKS
  313.  
  314. if [ -z $SUPRESS ]; then
  315.     echo1 "${SCRIPTNAME} v${SCRIPTVER}"
  316.     echo1 "Brought to you by ${SCRIPTAUTH}"
  317.     echo1
  318.     echo1 "Disk ${SDPATH}: ${SDSIZEMB}"
  319.     echo1
  320.     echo1 "I am going to make the following changes to your sdcard:"
  321.     echo1
  322.     echo1 "Partition 1: Name: ${SDNAME} File System: fat32 Size: ${FPARTEND}MB"
  323.     echo1 "Partition 2: Name: ${SYSTEMNAME} File System: ${FILESYSTEM} Size: ${SYSTEMSIZE}MB"
  324.     echo1 "Partition 3: Name: ${DATANAME} File System: ${FILESYSTEM} Size: ${DATASIZE}MB"
  325.     echo1 "Partition 4: Name: ${CACHENAME} File System:  ${FILESYSTEM} Size: ${CACHESIZE}MB"
  326.     echo1
  327.     if [ -z $DRYRUN ]; then
  328.         echo1 "WARNING!!! THIS WILL DESTROY ALL DATA ON SDCARD!!!"
  329.         read -s -n1 -p "Press c to confirm, any other key to cancel. " key
  330.         case $key in
  331.         "C" | "c") ;;
  332.         *) USERCANCEL;;
  333.         esac
  334.     fi
  335. fi
  336.  
  337. if [ -z $DRYRUN ]; then
  338.     UNMOUNTPARTS
  339.     NUKEPARTS
  340.     CREATEPARTS
  341.     if [ $FILESYSTEM == "ext4" ]; then
  342.         FORMATEXT4
  343.     elif [ $FILESYSTEM == "rfs" ]; then
  344.         FORMATRFS
  345.     else
  346.         SHOWERROR "Unable to select filesystem type."
  347.     fi
  348.     ECHOIT "Operation seems to have completed successfully."
  349. fi
  350. exit 0
Advertisement
Add Comment
Please, Sign In to add comment