blob: d2a46a452e71e6aaa8e66e9e3aa4900944bec583 [file] [log] [blame]
Koushik Duttaceddcd52010-08-23 16:13:14 -07001#!/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 CheckTableType
296
297 echo
298 echo "retrieving current sdcard information..."
299
300 if [ $TTISOK -gt 0 ] ; then
301 echo
302 parted "$SDPATH" print
303 echo
304 echo "script log is located @ /data/sdparted.log"
305 exit 0
306 else
307 echo
308 echo "partition 1 may not be aligned to cylinder boundaries."
309 ShowError "cannot complete print operation."
310 fi
311 echo
312
313}
314
315
316PartitionSdCard() {
317
318 echo "performing selected actions..."
319 echo
320
321 if [ $FATSIZE -gt 0 ] ; then
322 echo -n "creating fat32 partition..."
323 "$CMPARTED" -s "$SDPATH" mkpartfs primary fat32 0 "$FATSIZE"MB 2>&1 >>"$LOGFILE"
324 echo "done"
325 fi
326
327 if [ $EXTSIZE -gt 0 ] ; then
328 echo -n "creating ext2 partition..."
329 "$CMPARTED" -s "$SDPATH" mkpartfs primary ext2 "$FATSIZE"MB "$EXTEND"MB 2>&1 >>"$LOGFILE"
330 echo "done"
331 fi
332
333 if [ $SWAPSIZE -gt 0 ] ; then
334 echo -n "creating swap partition..."
335 "$CMPARTED" -s "$SDPATH" mkpartfs primary linux-swap "$EXTEND"MB "$SWAPEND"MB 2>&1 >>"$LOGFILE"
336 echo "done"
337 fi
338 echo
339
340}
341
342UpgradeExt() {
343
344 # check for no upgrade
345 [ "$1" == "ext2" ] && return
346 # check for ext partition
347 [ ! -e "$EXTPATH" ] && ShowError "$EXTPATH does not exist"
348
349 # have to use -m switch for this check b/c parted incorrectly
350 # reports all ext partitions as ext2 when running print <number>
351 CHECKEXTFS=`"$CMPARTED" -m "$SDPATH" print | grep ext | cut -d":" -f5`
352 [ "$CHECKEXTFS" == "$1" ] && ShowError "$EXTPATH is already $1"
353
354 # grabbed the code bits for ext3 from upgrade_fs(credit:cyanogen)
355 # check for ext2...must upgrade to ext3 first b4 ext4
356 if [ "$1" == "ext3" -o "$1" == "ext4" ] ; then
357 echo -n "adding journaling to $EXTPATH..."
358 umount /system/sd > /dev/null 2>&1 >>"$LOGFILE"
359 "$CME2FSCK" -p "$EXTPATH" 2>&1 >>"$LOGFILE"
360 "$CMTUNE2FS" -c0 -i0 -j "$EXTPATH" 2>&1 >>"$LOGFILE"
361 echo "done"
362 fi
363
364 # and got convert to ext4 from xda-forum(credit:Denkai)
365 if [ "$1" == "ext4" ] ; then
366 echo -n "converting $EXTPATH to ext4 filesystem..."
367 umount /system/sd > /dev/null 2>&1 >>"$LOGFILE"
368 "$CMTUNE2FS" -O extents,uninit_bg,dir_index "$EXTPATH" 2>&1 >>"$LOGFILE"
369 "$CME2FSCK" -fpDC0 "$EXTPATH" 2>&1 >>"$LOGFILE"
370 echo "done"
371 fi
372 echo
373
374}
375
376DowngradeExt() {
377
378 # check for ext partition
379 [ ! -e "$EXTPATH" ] && ShowError "$EXTPATH does not exist"
380
381 # have to use print for this check b/c parted incorrectly
382 # reports all ext partitions as ext2 when running print <number>
383 CHECKEXTFS=`"$CMPARTED" -m "$SDPATH" print | grep ext | cut -d":" -f5`
384 [ "$CHECKEXTFS" == "$1" ] && ShowError "$EXTPATH is already $1"
385
386 if [ "$CHECKEXTFS" == "ext4" -o "$1" == "ext3" ] ; then
387 # interweb says downgrading from ext4 is not possible
388 # without a backup/restore procedure.
389 # if i figure it out, i'll implement it.
390 ShowError "downgrading from ext4 is not currently supported"
391 fi
392
393 if [ "$1" == "ext2" ] ; then
394 echo -n "removing journaling from $EXTPATH..."
395 umount /system/sd > /dev/null 2>&1 >>"$LOGFILE"
396 "$CMTUNE2FS" -O ^has_journal "$EXTPATH" 2>&1 >>"$LOGFILE"
397 "$CME2FSCK" -fp "$EXTPATH" 2>&1 >>"$LOGFILE"
398 echo "done"
399 fi
400 echo
401
402}
403
404
405Interactive() {
406
407cat <<DONEINSTRUCT
408
409sdparted interactive mode
410
411rules:
4121. no answer means you accept default value
4132. enter '0' for no partition
414
415DONEINSTRUCT
416
417 UserAbort && ShowError "script canceled by user"
418
419 CalculatePartitions
420
421 GetSwapSize
422
423 FATSIZE=
424
425 CalculatePartitions
426
427 GetExtSize
428
429 GetExtType
430
431 FATSIZE=
432
433 CalculatePartitions
434
435 GetFatSize
436
437}
438
439GetSwapSize() {
440
441 SWAPTEST=
442
443 while [ -z "$SWAPTEST" ]
444 do
445 echo
446 echo -n "swap partition size [default=$SWAPSIZE]: "
447 read SWAPRESP
448
449 [ -z "$SWAPRESP" ] && SWAPRESP="$SWAPSIZE"
450 echo "$SWAPRESP" > /dev/null 2>&1 >>"$LOGFILE"
451
452 ValidateSizeArg "$SWAPRESP"
453 SWAPTEST="$FUNC_RET"
454 [ -n "$SWAPTEST" ] && [ $SWAPTEST -gt $SDSIZE ] && ShowMessage "$SWAPRESP > available space($(($SDSIZE))M)." && SWAPTEST=
455 done
456
457 SWAPSIZE=$SWAPTEST
458
459}
460
461GetExtSize() {
462
463 EXTTEST=
464
465 while [ -z "$EXTTEST" ]
466 do
467 echo
468 echo -n "ext partition size [default=$EXTSIZE]: "
469 read EXTRESP
470
471 [ -z "$EXTRESP" ] && EXTRESP="$EXTSIZE"
472 echo "$EXTRESP" > /dev/null 2>&1 >>"$LOGFILE"
473
474 ValidateSizeArg "$EXTRESP"
475 EXTTEST="$FUNC_RET"
476
477 [ -n "$EXTTEST" ] && [ $EXTTEST -gt $(($SDSIZE - $SWAPSIZE)) ] && ShowMessage "$EXTRESP > available space($(($SDSIZE - $SWAPSIZE))M)." && EXTTEST=
478 done
479
480 EXTSIZE=$EXTTEST
481
482}
483
484GetExtType() {
485
486 FSTEST=
487
488 while [ -z "$FSTEST" ]
489 do
490 echo
491 echo -n "ext partition type [default=$EXTFS]: "
492 read FSRESP
493
494 [ -z "$FSRESP" ] && FSRESP="$EXTFS"
495 echo "$FSRESP" > /dev/null 2>&1 >>"$LOGFILE"
496
497 ValidateExtArg "$FSRESP"
498 FSTEST="$FUNC_RET"
499 done
500
501 EXTFS="$FSTEST"
502
503}
504
505GetFatSize() {
506
507 FATTEST=
508
509 while [ -z "$FATTEST" ]
510 do
511 echo
512 echo -n "fat partition size [default=$FATSIZE]: "
513 read FATRESP
514
515 [ -z "$FATRESP" ] && FATRESP="$FATSIZE"
516 echo "$FATRESP" > /dev/null 2>&1 >>"$LOGFILE"
517
518 ValidateSizeArg "$FATRESP"
519 FATTEST="$FUNC_RET"
520
521 [ -n "$FATTEST" ] && [ $FATTEST -gt $FATSIZE ] && ShowMessage "$FATRESP > available space($(($SDSIZE - $SWAPSIZE - $EXTSIZE))M)." && FATTEST=
522 [ -n "$FATTEST" ] && [ $FATTEST -le 0 ] && ShowMessage "must have a fat32 partition greater than 0MB" && FATTEST=
523 done
524
525 FATSIZE=$FATTEST
526
527}
528
529
530SCRIPTNAME="sdparted"
531SCRIPTREV="0.6"
532MYNAME="51dusty"
533
534IMODE=
535SILENTRUN=
536CREATEPART=
537FUNC_RET=
538
539UEXTFSONLY=
540DEXTFSONLY=
541
542TTISOK=
543TTISLOOP=
544TTISMSDOS=
545
546SDSIZE=
547SDSIZEMB=
548if [ -z "$SDPATH" ]
549then
550 SDPATH="/dev/block/mmcblk0"
551else
552 echo Found SDPATH=$SDPATH
553fi
554
555FATSIZE=
556FATTYPE="fat32"
557FATPATH=$SDPATH"p1"
558
559EXTSIZE=512
560EXTFS="ext2"
561EXTPATH=$SDPATH"p2"
562EXTEND=
563
564SWAPSIZE=32
565SWAPTYPE="linux-swap"
566SWAPPATH=$SDPATH"p3"
567SWAPEND=
568
569CMPARTED="/sbin/parted"
570CMTUNE2FS="/sbin/tune2fs"
571CME2FSCK="/sbin/e2fsck"
572
573# give the output some breathing room
574echo "$SCRIPTREV" >> "$LOGFILE"
575echo
576
577# check for arguments
578while [ $# -gt 0 ] ; do
579 case "$1" in
580
581 -h|--help) ShowHelp ; exit 0 ;;
582
583 -fs|--fatsize) shift ; ValidateSizeArg "$1" ; FATSIZE="$FUNC_RET" ; CREATEPART="$1" ;;
584 -es|--extsize) shift ; ValidateSizeArg "$1" ; EXTSIZE="$FUNC_RET" ; CREATEPART="$1" ;;
585 -ss|--swapsize) shift ; ValidateSizeArg "$1" ; SWAPSIZE="$FUNC_RET" ; CREATEPART="$1" ;;
586 -efs|--extfs) shift ; ValidateExtArg "$1" ; EXTFS="$FUNC_RET" ; CREATEPART="$1" ;;
587
588 -ufs|--upgradefs) shift ; ValidateExtArg "$1" ; UEXTFSONLY="$FUNC_RET" ;;
589 -dfs|--downgradefs) shift ; ValidateExtArg "$1" ; DEXTFSONLY="$FUNC_RET" ;;
590
591 -i|--interactive) IMODE="$1" ;;
592
593 -s|--silent) SILENTRUN="$1" ;;
594
595 -po|--printonly) ShowCardInfo ;;
596
597 *) ShowHelp ; ShowError "unknown argument '$1'" ;;
598
599 esac
600 shift
601done
602
603# can't do silent when in interactive mode
604[ -n "$IMODE" ] && SILENTRUN=
605
606# make sure sdcard exists and all needed files are here
607CheckReqs
608
609# unmount all
610UnmountAll
611
612# upgrade only? downgrade only?
613UpgradeDowngradeOnly
614
615# check table
616CheckTableType
617
618# prep card
619PrepareSdCard
620
621# check for interactive mode
622[ -n "$IMODE" ] && Interactive
623
624# do some math
625CalculatePartitions
626
627# last chance to cancel
628ShowActions
629
630# partition card
631PartitionSdCard
632
633# upgrade fs if necessary
634UpgradeExt "$EXTFS"
635
636# say goodbye and show print output
637ShowCardInfo