blob: 730d5dc3426f7632e463efeddb467d3069da9544 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001#!/sbin/sh
2
3# do logging, if not excluded with -x
4LOGFILE="/data/sdparted.log"
5[ "$1" != "-x" ] && echo "$0" "$@" >> "$LOGFILE" && "$0" -x "$@" 2>&1 | tee -a "$LOGFILE" && exit
6shift
7
8ShowError() { echo ; echo " err: $1" ; echo ; exit 1 ; }
9
10ShowMessage() { echo ; echo " msg: $1" ; }
11
12ShowHelp() {
13
14cat <<DONEHELP
15
16$SCRIPTNAME v$SCRIPTREV created by $MYNAME
17
18if you use this script in your work, please give some credit. thanks.
19
20requirements: cm-recovery-v1.4
21
22usage: $SCRIPTNAME [options]
23
24
25options:
26
27 --fatsize|-fs SIZE[MG] set the size of the fat32 partition to <SIZE>.
28 default=total sdcard size - (ext + swap)
29
30 --extsize|-es SIZE[MG] set the size of the ext partition to <SIZE>.
31 default=$EXTSIZE
32
33 --swapsize|-ss SIZE[MG] set the size of the swap partition to <SIZE>.
34 if set to 0, no swap partition will be created.
35 default=$SWAPSIZE
36
37 --extfs|-efs TYPE set the filesystem of ext partition to <TYPE>.
38 valid types=ext2, ext3, ext4
39 default=$EXTFS
40
41
42 --upgradefs|-ufs TYPE upgrades existing ext partition to <TYPE>.
43 this operation will NOT wipe your sdcard and
44 cannot be used with any partition creation options.
45 valid types=ext3, ext4
46
47 --downgradefs|-dfs TYPE downgrades existing ext partition to <TYPE>.
48 this operation will NOT wipe your sdcard and
49 cannot be used with any partition creation options.
50 valid types=ext2
51
52
53 --interactive|-i interactive mode
54
55 --help|-h display this help
56
57 --printonly|-po display sdcard information
58
59 --silent|-s do not prompt user, not even initial warning.
60
61
62examples:
63 $SCRIPTNAME creates swap=$SWAPSIZE ext2=$EXTSIZE fat32=remaining free space
64 $SCRIPTNAME -efs ext4 creates swap=$SWAPSIZE ext4=$EXTSIZE fat32=remaining free space
65 $SCRIPTNAME -fs 1.5G -efs ext3 creates swap=$SWAPSIZE ext3=$EXTSIZE fat32=1536
66 $SCRIPTNAME -es 256M -ss 0 creates no swap ext2=256 fat32=remaining free space
67 $SCRIPTNAME -ufs ext4 upgrades ext partition to ext4
68
69DONEHELP
70
71}
72
73UserAbort() {
74
75 WHILEEXIT=
76
77 while [ -z "$WHILEEXIT" ]
78 do
79 echo -n "do you want to continue? (Y/n) "
80 read response
81 echo
82 [ "$response" = "Y" ] || [ "$response" = "n" ] || [ "$response" = "N" ] && WHILEEXIT="$response"
83 done
84
85 echo "$response" > /dev/null 2>&1 >>"$LOGFILE"
86
87 [ "$response" != "Y" ]
88
89}
90
91UnmountAll () {
92
93 # unmount all partitions so we can work with $SDPATH
94 # i'm assuming no more than 3 partitions
95 # maybe make a little more elegant later
96 echo -n "unmounting all partitions..."
97 umount "$FATPATH" > /dev/null 2>&1 >>"$LOGFILE"
98 umount "$EXTPATH" > /dev/null 2>&1 >>"$LOGFILE"
99 umount "$SWAPPATH" > /dev/null 2>&1 >>"$LOGFILE"
100 echo "done"
101 echo
102
103}
104
105
106CheckReqs() {
107
108 echo -n "checking script requirements..."
109 # check for valid sdcard
110 [ -e $SDPATH ] || ShowError "$SDPATH does not exist!"
111
112 # look for necessary programs
113 [ -e $CMPARTED ] || ShowError "$CMPARTED does not exist!"
114 [ -e $CMTUNE2FS ] || ShowError "$CMTUNE2FS does not exist!"
115 [ -e $CME2FSCK ] || ShowError "$CME2FSCK does not exist!"
116
117 # verify cm-v1.4
118 PARTEDREV=`"$CMPARTED" "$SDPATH" version | grep Parted | cut -d" " -f3`
119 [ "$PARTEDREV" == "1.8.8.1.179-aef3" ] || ShowError "you are not using parted v1.8.8.1.179-aef3!"
120 echo "done"
121 echo
122
123}
124
125CheckTableType() {
126
127 TABLETYPE=`"$CMPARTED" "$SDPATH" print | grep Table: | cut -d" " -f3`
128
129 [ "$TABLETYPE" == "loop" -o "$TABLETYPE" == "msdos" ] && TTISOK=1 || TTISOK=0
130 [ "$TABLETYPE" == "loop" ] && TTISLOOP=1 || TTISLOOP=0
131 [ "$TABLETYPE" == "msdos" ] && TTISMSDOS=1 || TTISMOSDOS=0
132
133}
134
135ValidateExtArg() {
136
137 FUNC_RET="nonzero"
138
139 # validating argument
140 [ "$1" != "ext2" ] && [ "$1" != "ext3" ] && [ "$1" != "ext4" ] && FUNC_RET=
141
142 [ -z "$FUNC_RET" ] && [ -z "$IMODE" ] && ShowError "$1 is not a valid filesystem."
143 [ -z "$FUNC_RET" ] && [ -n "$IMODE" ] && ShowMessage "$1 is not a valid filesystem."
144
145 # return valid argument
146 [ -n "$FUNC_RET" ] && FUNC_RET="$1"
147
148}
149
150ValidateSizeArg() {
151
152 # check for zero-length arg to protect expr length
153 [ -z "$1" ] && ShowError "zero-length argument passed to size-validator"
154
155 SIZEMB=
156 ARGLEN=`expr length $1`
157 SIZELEN=$(($ARGLEN-1))
158 SIZEARG=`expr substr $1 1 $SIZELEN`
159 SIZEUNIT=`expr substr $1 $ARGLEN 1`
160
161 # check if SIZEARG is an integer
162 if [ $SIZEARG -eq $SIZEARG 2> /dev/null ] ; then
163 # look for G
164 [ "$SIZEUNIT" == "G" ] && SIZEMB=$(($SIZEARG * 1024))
165 # look for M
166 [ "$SIZEUNIT" == "M" ] && SIZEMB=$SIZEARG
167 # no units on arg AND prevents using bogus size units
168 [ -z "$SIZEMB" ] && [ $SIZEUNIT -eq $SIZEUNIT 2> /dev/null ] && SIZEMB=$1
169 # check if SIZEARG is a floating point number, GB only
170 elif [ `expr index "$SIZEARG" .` != 0 ] && [ "$SIZEUNIT" == "G" ] ; then
171 INT=`echo "$SIZEARG" | cut -d"." -f1`
172 FRAC=`echo "$SIZEARG" | cut -d"." -f2`
173 SIGDIGITS=`expr length $FRAC`
174
175 [ -z "$INT" ] && INT=0
176 INTMB=$(($INT * 1024))
177 FRACMB=$((($FRAC * 1024) / (10**$SIGDIGITS)))
178 SIZEMB=$(($INTMB + $FRACMB))
179 # it's not a valid size
180 else
181 [ -z "$IMODE" ] && ShowError "$1 is not a valid size"
182 fi
183
184 [ -z "$SIZEMB" ] && [ -n "$IMODE" ] && ShowMessage "$1 is not a valid size"
185
186 # return valid argument in MB
187 FUNC_RET=$SIZEMB
188
189}
190
191CalculatePartitions() {
192
193 # get size of sdcard in MB & do some math
194 SDSIZEMB=`"$CMPARTED" "$SDPATH" unit MB print | grep $SDPATH | cut -d" " -f3`
195 SDSIZE=${SDSIZEMB%MB}
196 [ -n "$FATSIZE" ] || FATSIZE=$(($SDSIZE - $EXTSIZE - $SWAPSIZE))
197 EXTEND=$(($FATSIZE + $EXTSIZE))
198 SWAPEND=$(($EXTEND + $SWAPSIZE))
199
200 # check for fatsize of 0
201 [ $FATSIZE -le 0 ] && ShowError "must have a fat32 partition greater than 0MB"
202
203 # check for zero-length sdsize...
204 # indicative of parted not reporting length
205 # correctly b/c of error on sdcard
206 [ -z "$SDSIZE" ] && ShowError "zero-length argument passed to partition-calculator"
207
208 # make sure we're not being asked to do the impossible
209 [ $(($FATSIZE + $EXTSIZE + $SWAPSIZE)) -gt $SDSIZE ] && [ -z "$IMODE" ] && ShowError "sum of requested partitions is greater than sdcard size"
210
211}
212
213
214UpgradeDowngradeOnly() {
215
216 if [ -n "$UEXTFSONLY" ] ; then
217 echo
218 [ -n "$CREATEPART" ] && ShowError "cannot use upgrade option when creating partitions, use -efs instead"
219 [ -n "$DEXTFSONLY" ] && ShowError "cannot upgrade AND downgrade, it just doesn't make sense"
220 echo "you have chosen to upgrade $EXTPATH to $UEXTFSONLY."
221 echo "this action will NOT delete any data from sdcard."
222 echo
223 [ -z "$SILENTRUN" ] && UserAbort && ShowError "script canceled by user"
224 echo
225 UpgradeExt "$UEXTFSONLY"
226 ShowCardInfo
227 elif [ -n "$DEXTFSONLY" ] ; then
228 echo
229 [ -n "$CREATEPART" ] && ShowError "cannot use downgrade option when creating partitions."
230 [ -n "$UEXTFSONLY" ] && ShowError "cannot downgrade AND upgrade, it just doesn't make sense."
231 echo "you have chosen to downgrade $EXTPATH to $DEXTFSONLY."
232 echo "this action will NOT delete any data from sdcard."
233 echo
234 [ -z "$SILENTRUN" ] && UserAbort && ShowError "script canceled by user"
235 echo
236 DowngradeExt "$DEXTFSONLY"
237 ShowCardInfo
238 fi
239
240}
241
242PrepareSdCard() {
243
244 echo
245 if [ $TTISOK -eq 0 ] ; then
246 echo "partition 1 may not be aligned to cylinder boundaries."
247 echo "to continue, this must be corrected."
248 elif [ $TTISLOOP -gt 0 ] ; then
249 echo "your sdcard's partition table type is $TABLETYPE."
250 echo "to continue, partition table must be set to 'msdos'."
251 elif [ $TTISMSDOS -gt 0 ] ; then
252 # just a reminder..in a later version,
253 # i may implement resizing of partitions,
254 # so this will be unnecessary. but until then...
255 echo "to continue, all existing partitions must be removed."
256 else
257 # this is not good, and should never happen
258 # if it does, there is a serious problem
259 ShowError "sdcard failed table type check."
260 fi
261
262 echo
263 echo "this action will remove all data from your sdcard."
264 echo
265 [ -z "$SILENTRUN" ] && UserAbort && ShowError "script canceled by user"
266
267 [ $TTISOK -eq 0 ] && echo -n "correcting cylinder boundaries..."
268 [ $TTISLOOP -gt 0 ] && echo -n "setting partition table to msdos..."
269 [ $TTISMSDOS -gt 0 ] && echo -n "removing all partitions..."
270
271 "$CMPARTED" -s "$SDPATH" mklabel msdos 2>&1 >>"$LOGFILE"
272 echo "done"
273 echo
274
275}
276
277ShowActions() {
278
279 echo
280 echo "total size of sdcard=$SDSIZEMB"
281 echo
282 echo "the following actions will be performed:"
283 echo " -create $FATSIZE""MB fat32 partition"
284 [ $EXTSIZE -gt 0 ] && echo " -create $EXTSIZE""MB ext2 partition"
285 [ $SWAPSIZE -gt 0 ] && echo " -create $SWAPSIZE""MB swap partition"
286 [ "$EXTFS" != "ext2" ] && echo " -ext2 partition will be upgraded to $EXTFS"
287 echo
288 [ -z "$SILENTRUN" ] && UserAbort && ShowError "script canceled by user"
289 echo
290
291}
292
293ShowCardInfo() {
294
295 echo "sddevice located in /sdcard/sddev.log"
296 echo $SDPATH > /sdcard/sddev.log
297
298 CheckTableType
299
300 echo
301 echo "retrieving current sdcard information..."
302
303 if [ $TTISOK -gt 0 ] ; then
304 echo
305 parted "$SDPATH" print
306 echo
307 echo "script log is located @ /data/sdparted.log"
308 exit 0
309 else
310 echo
311 echo "partition 1 may not be aligned to cylinder boundaries."
312 ShowError "cannot complete print operation."
313 fi
314 echo
315
316}
317
318
319PartitionSdCard() {
320
321 echo "performing selected actions..."
322 echo
323
324 if [ $FATSIZE -gt 0 ] ; then
325 echo -n "creating fat32 partition..."
326 "$CMPARTED" -s "$SDPATH" mkpartfs primary fat32 0 "$FATSIZE"MB 2>&1 >>"$LOGFILE"
327 echo "done"
328 fi
329
330 if [ $EXTSIZE -gt 0 ] ; then
331 echo -n "creating ext2 partition..."
332 "$CMPARTED" -s "$SDPATH" mkpartfs primary ext2 "$FATSIZE"MB "$EXTEND"MB 2>&1 >>"$LOGFILE"
333 "$CMTUNE2FS" -L sd-ext "$EXTPATH" 2>&1 >>"$LOGFILE"
334 echo "done"
335 fi
336
337 if [ $SWAPSIZE -gt 0 ] ; then
338 echo -n "creating swap partition..."
339 "$CMPARTED" -s "$SDPATH" mkpartfs primary linux-swap "$EXTEND"MB "$SWAPEND"MB 2>&1 >>"$LOGFILE"
340 echo "done"
341 fi
342 echo
343
344}
345
346UpgradeExt() {
347
348 # check for no upgrade
349 [ "$1" == "ext2" ] && return
350 # check for ext partition
351 [ ! -e "$EXTPATH" ] && ShowError "$EXTPATH does not exist"
352
353 # have to use -m switch for this check b/c parted incorrectly
354 # reports all ext partitions as ext2 when running print <number>
355 CHECKEXTFS=`"$CMPARTED" -m "$SDPATH" print | grep ext | cut -d":" -f5`
356 [ "$CHECKEXTFS" == "$1" ] && ShowError "$EXTPATH is already $1"
357
358 # grabbed the code bits for ext3 from upgrade_fs(credit:cyanogen)
359 # check for ext2...must upgrade to ext3 first b4 ext4
360 if [ "$1" == "ext3" -o "$1" == "ext4" ] ; then
361 echo -n "adding journaling to $EXTPATH..."
362 umount /system/sd > /dev/null 2>&1 >>"$LOGFILE"
363 "$CME2FSCK" -p "$EXTPATH" 2>&1 >>"$LOGFILE"
364 "$CMTUNE2FS" -c0 -i0 -j "$EXTPATH" 2>&1 >>"$LOGFILE"
365 echo "done"
366 fi
367
368 # and got convert to ext4 from xda-forum(credit:Denkai)
369 if [ "$1" == "ext4" ] ; then
370 echo -n "converting $EXTPATH to ext4 filesystem..."
371 umount /system/sd > /dev/null 2>&1 >>"$LOGFILE"
372 "$CMTUNE2FS" -O extents,uninit_bg,dir_index "$EXTPATH" 2>&1 >>"$LOGFILE"
373 "$CME2FSCK" -fpDC0 "$EXTPATH" 2>&1 >>"$LOGFILE"
374 echo "done"
375 fi
376 echo
377
378}
379
380DowngradeExt() {
381
382 # check for ext partition
383 [ ! -e "$EXTPATH" ] && ShowError "$EXTPATH does not exist"
384
385 # have to use print for this check b/c parted incorrectly
386 # reports all ext partitions as ext2 when running print <number>
387 CHECKEXTFS=`"$CMPARTED" -m "$SDPATH" print | grep ext | cut -d":" -f5`
388 [ "$CHECKEXTFS" == "$1" ] && ShowError "$EXTPATH is already $1"
389
390 if [ "$CHECKEXTFS" == "ext4" -o "$1" == "ext3" ] ; then
391 # interweb says downgrading from ext4 is not possible
392 # without a backup/restore procedure.
393 # if i figure it out, i'll implement it.
394 ShowError "downgrading from ext4 is not currently supported"
395 fi
396
397 if [ "$1" == "ext2" ] ; then
398 echo -n "removing journaling from $EXTPATH..."
399 umount /system/sd > /dev/null 2>&1 >>"$LOGFILE"
400 "$CMTUNE2FS" -O ^has_journal "$EXTPATH" 2>&1 >>"$LOGFILE"
401 "$CME2FSCK" -fp "$EXTPATH" 2>&1 >>"$LOGFILE"
402 echo "done"
403 fi
404 echo
405
406}
407
408
409Interactive() {
410
411cat <<DONEINSTRUCT
412
413sdparted interactive mode
414
415rules:
4161. no answer means you accept default value
4172. enter '0' for no partition
418
419DONEINSTRUCT
420
421 UserAbort && ShowError "script canceled by user"
422
423 CalculatePartitions
424
425 GetSwapSize
426
427 FATSIZE=
428
429 CalculatePartitions
430
431 GetExtSize
432
433 GetExtType
434
435 FATSIZE=
436
437 CalculatePartitions
438
439 GetFatSize
440
441}
442
443GetSwapSize() {
444
445 SWAPTEST=
446
447 while [ -z "$SWAPTEST" ]
448 do
449 echo
450 echo -n "swap partition size [default=$SWAPSIZE]: "
451 read SWAPRESP
452
453 [ -z "$SWAPRESP" ] && SWAPRESP="$SWAPSIZE"
454 echo "$SWAPRESP" > /dev/null 2>&1 >>"$LOGFILE"
455
456 ValidateSizeArg "$SWAPRESP"
457 SWAPTEST="$FUNC_RET"
458 [ -n "$SWAPTEST" ] && [ $SWAPTEST -gt $SDSIZE ] && ShowMessage "$SWAPRESP > available space($(($SDSIZE))M)." && SWAPTEST=
459 done
460
461 SWAPSIZE=$SWAPTEST
462
463}
464
465GetExtSize() {
466
467 EXTTEST=
468
469 while [ -z "$EXTTEST" ]
470 do
471 echo
472 echo -n "ext partition size [default=$EXTSIZE]: "
473 read EXTRESP
474
475 [ -z "$EXTRESP" ] && EXTRESP="$EXTSIZE"
476 echo "$EXTRESP" > /dev/null 2>&1 >>"$LOGFILE"
477
478 ValidateSizeArg "$EXTRESP"
479 EXTTEST="$FUNC_RET"
480
481 [ -n "$EXTTEST" ] && [ $EXTTEST -gt $(($SDSIZE - $SWAPSIZE)) ] && ShowMessage "$EXTRESP > available space($(($SDSIZE - $SWAPSIZE))M)." && EXTTEST=
482 done
483
484 EXTSIZE=$EXTTEST
485
486}
487
488GetExtType() {
489
490 FSTEST=
491
492 while [ -z "$FSTEST" ]
493 do
494 echo
495 echo -n "ext partition type [default=$EXTFS]: "
496 read FSRESP
497
498 [ -z "$FSRESP" ] && FSRESP="$EXTFS"
499 echo "$FSRESP" > /dev/null 2>&1 >>"$LOGFILE"
500
501 ValidateExtArg "$FSRESP"
502 FSTEST="$FUNC_RET"
503 done
504
505 EXTFS="$FSTEST"
506
507}
508
509GetFatSize() {
510
511 FATTEST=
512
513 while [ -z "$FATTEST" ]
514 do
515 echo
516 echo -n "fat partition size [default=$FATSIZE]: "
517 read FATRESP
518
519 [ -z "$FATRESP" ] && FATRESP="$FATSIZE"
520 echo "$FATRESP" > /dev/null 2>&1 >>"$LOGFILE"
521
522 ValidateSizeArg "$FATRESP"
523 FATTEST="$FUNC_RET"
524
525 [ -n "$FATTEST" ] && [ $FATTEST -gt $FATSIZE ] && ShowMessage "$FATRESP > available space($(($SDSIZE - $SWAPSIZE - $EXTSIZE))M)." && FATTEST=
526 [ -n "$FATTEST" ] && [ $FATTEST -le 0 ] && ShowMessage "must have a fat32 partition greater than 0MB" && FATTEST=
527 done
528
529 FATSIZE=$FATTEST
530
531}
532
533
534SCRIPTNAME="sdparted"
535SCRIPTREV="0.6"
536MYNAME="51dusty"
537
538IMODE=
539SILENTRUN=
540CREATEPART=
541FUNC_RET=
542
543UEXTFSONLY=
544DEXTFSONLY=
545
546TTISOK=
547TTISLOOP=
548TTISMSDOS=
549
550SDSIZE=
551SDSIZEMB=
552SDINFO=$(cat /etc/fstab | grep /sdcard | awk '{print $1}')
553if [ -L "$SDINFO" ]
554then
555 SDPATH=$(ls -l $SDINFO | awk '{print $11}')
556else
557 SDPATH=$SDINFO
558fi
559# we may now have an SDPATH, let's make sure its on mmcblkX or mmcblkXp1
560CHECK_SDPATH1=$(echo $SDPATH | grep mmcblk.$)
561CHECK_SDPATH2=$(echo $SDPATH | grep mmcblk.p1$)
562if [ -z "$CHECK_SDPATH1" ]
563then
564 if [ -z "$CHECK_SDPATH2" ]
565 then
566 echo fail1
567 unset SDPATH
568 else
569 LEN=${#SDPATH}
570 BLKLEN=$(expr $LEN - 2)
571 SDPATH=${SDPATH:0:$BLKLEN}
572 fi
573fi
574
575
576FATSIZE=
577FATTYPE="fat32"
578FATPATH=$SDPATH"p1"
579
580EXTSIZE=512
581EXTFS="ext2"
582EXTPATH=$SDPATH"p2"
583EXTEND=
584
585SWAPSIZE=32
586SWAPTYPE="linux-swap"
587SWAPPATH=$SDPATH"p3"
588SWAPEND=
589
590CMPARTED="/sbin/parted"
591CMTUNE2FS="/sbin/tune2fs"
592CME2FSCK="/sbin/e2fsck"
593
594# give the output some breathing room
595echo "$SCRIPTREV" >> "$LOGFILE"
596echo
597
598# check for arguments
599while [ $# -gt 0 ] ; do
600 case "$1" in
601
602 -h|--help) ShowHelp ; exit 0 ;;
603
604 -fs|--fatsize) shift ; ValidateSizeArg "$1" ; FATSIZE="$FUNC_RET" ; CREATEPART="$1" ;;
605 -es|--extsize) shift ; ValidateSizeArg "$1" ; EXTSIZE="$FUNC_RET" ; CREATEPART="$1" ;;
606 -ss|--swapsize) shift ; ValidateSizeArg "$1" ; SWAPSIZE="$FUNC_RET" ; CREATEPART="$1" ;;
607 -efs|--extfs) shift ; ValidateExtArg "$1" ; EXTFS="$FUNC_RET" ; CREATEPART="$1" ;;
608
609 -ufs|--upgradefs) shift ; ValidateExtArg "$1" ; UEXTFSONLY="$FUNC_RET" ;;
610 -dfs|--downgradefs) shift ; ValidateExtArg "$1" ; DEXTFSONLY="$FUNC_RET" ;;
611
612 -i|--interactive) IMODE="$1" ;;
613
614 -s|--silent) SILENTRUN="$1" ;;
615
616 -po|--printonly) ShowCardInfo ;;
617
618 *) ShowHelp ; ShowError "unknown argument '$1'" ;;
619
620 esac
621 shift
622done
623
624# can't do silent when in interactive mode
625[ -n "$IMODE" ] && SILENTRUN=
626
627# make sure sdcard exists and all needed files are here
628CheckReqs
629
630# unmount all
631UnmountAll
632
633# upgrade only? downgrade only?
634UpgradeDowngradeOnly
635
636# check table
637CheckTableType
638
639# prep card
640PrepareSdCard
641
642# check for interactive mode
643[ -n "$IMODE" ] && Interactive
644
645# do some math
646CalculatePartitions
647
648# last chance to cancel
649ShowActions
650
651# partition card
652PartitionSdCard
653
654# upgrade fs if necessary
655UpgradeExt "$EXTFS"
656
657# say goodbye and show print output
658ShowCardInfo