· 8 years ago · Jul 13, 2018, 10:44 PM
1#!/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 "error: $1" ; echo ; exit 1; }
9
10ShowHelp() {
11cat <<DONEHELP
12
13$SCRIPTNAME v$SCRIPTREV created by $MYNAME
14
15if you use this script in your work, please give some credit. thanks.
16
17requirements: cm-recovery-v1.4
18
19usage: $SCRIPTNAME [options]
20
21
22options:
23
24 --fatsize|-fs SIZE[MG] set the size of the fat32 partition to <SIZE>.
25 default=total sdcard size - (ext + swap)
26
27 --extsize|-es SIZE[MG] set the size of the ext partition to <SIZE>.
28 default=$EXTSIZE
29
30 --swapsize|-ss SIZE[MG] set the size of the swap partition to <SIZE>.
31 if set to 0, no swap partition will be created.
32 default=$SWAPSIZE
33
34 --extfs|-efs TYPE set the filesystem of ext partition to <TYPE>.
35 valid types=ext2, ext3, ext4
36 default=$EXTFS
37
38
39 --upgradefs|-ufs TYPE upgrades existing ext partition to <TYPE>.
40 this operation will NOT wipe your sdcard and
41 cannot be used with any partition creation options.
42 valid types=ext3, ext4
43
44 --downgradefs|-dfs TYPE downgrades existing ext partition to <TYPE>.
45 this operation will NOT wipe your sdcard and
46 cannot be used with any partition creation options.
47 valid types=ext2
48
49
50 --help|-h display this help
51
52 --printonly|-po display sdcard information
53
54 --silent|-s do not prompt user, not even initial warning.
55
56 -x no logging, must be first argument
57
58
59examples:
60 $SCRIPTNAME creates swap=$SWAPSIZE ext2=$EXTSIZE fat32=remaining free space
61 $SCRIPTNAME -efs ext4 creates swap=$SWAPSIZE ext4=$EXTSIZE fat32=remaining free space
62 $SCRIPTNAME -fs 1.5G -efs ext3 creates swap=$SWAPSIZE ext3=$EXTSIZE fat32=1536MB
63 $SCRIPTNAME -es 256M -ss 0 creates no swap ext2=256MB fat32=remaining free space
64 $SCRIPTNAME -ufs ext4 upgrades ext partition to ext4
65
66DONEHELP
67}
68
69ShowWarning() {
70 echo "WARNING! WARNING! WARNING!"
71 echo "filesystem operations after"
72 echo "this point may wipe your sdcard."
73 echo
74 [ -z "$SILENTRUN" ] && UserAbort && ShowError "script canceled by user"
75}
76
77UserAbort() {
78 echo -n "do you want to continue? (Y/n) "
79 read response
80 echo
81 [ "$response" != "Y" ]
82}
83
84
85CheckReqs() {
86 echo -n "checking script requirements..."
87 # check for valid sdcard
88 [ -e $SDPATH ] || ShowError "$SDPATH does not exist!"
89
90 # look for necessary programs
91 [ -e $CMPARTED ] || ShowError "$CMPARTED does not exist!"
92 [ -e $CMTUNE2FS ] || ShowError "$CMTUNE2FS does not exist!"
93 [ -e $CME2FSCK ] || ShowError "$CME2FSCK does not exist!"
94
95 # verify cm-v1.4
96 PARTEDREV=`"$CMPARTED" "$SDPATH" version | grep Parted | cut -d" " -f3`
97 [ "$PARTEDREV" == "1.8.8.1.179-aef3" ] || ShowError "you are not using cyanogen recovery v1.4!"
98 echo "done"
99 echo
100}
101
102CheckUpgradeOnly() {
103 if [ -n "$UEXTFSONLY" ] ; then
104 [ -n "$CREATEPART" ] && ShowError "cannot use upgrade option when creating partitions, use -efs instead"
105 [ -n "$DEXTFSONLY" ] && ShowError "cannot upgrade AND downgrade, it just doesn't make sense"
106 echo "you have chosen to upgrade $EXTPATH to $UEXTFSONLY."
107 echo "this action will NOT delete any data from sdcard."
108 echo
109 [ -z "$SILENTRUN" ] && UserAbort && ShowError "script canceled by user"
110 UpgradeExt "$UEXTFSONLY"
111 ShowCardInfo
112 fi
113}
114
115CheckDowngradeOnly() {
116 if [ -n "$DEXTFSONLY" ] ; then
117 [ -n "$CREATEPART" ] && ShowError "cannot use downgrade option when creating partitions"
118 [ -n "$UEXTFSONLY" ] && ShowError "cannot downgrade AND upgrade, it just doesn't make sense"
119 echo "you have chosen to downgrade $EXTPATH to $DEXTFSONLY."
120 echo "this action will NOT delete any data from sdcard."
121 echo
122 [ -z "$SILENTRUN" ] && UserAbort && ShowError "script canceled by user"
123 DowngradeExt "$DEXTFSONLY"
124 ShowCardInfo
125 fi
126}
127
128CheckTableType() {
129 TABLETYPE=`"$CMPARTED" "$SDPATH" print | grep Table: | cut -d" " -f3`
130 if [ "$TABLETYPE" != "msdos" ] ; then
131 [ "$TABLETYPE" == "loop" ] && echo "your sdcard's partition table type is $TABLETYPE."
132 [ "$TABLETYPE" != "loop" ] && echo "partition 1 may not be aligned to cylinder boundaries."
133 echo
134 echo "to continue partitioning, it is necessary that you switch to 'msdos' partition table."
135 echo "this action will wipe your sdcard."
136 echo
137 [ -z "$SILENTRUN" ] && UserAbort && ShowError "script canceled by user"
138 TABLEISLOOP="1"
139 elif [ "$TABLETYPE" == "msdos" ] ; then
140 # just a reminder..in a later version, i may implement resizing of partitions, so this
141 # will be unnecessary. but until then...
142 echo "your sdcard's partition table type is $TABLETYPE."
143 echo
144 echo "to continue partitioning, it is necessary to remove all existing partitions."
145 echo "this action will wipe your sdcard."
146 echo
147 [ -z "$SILENTRUN" ] && UserAbort && ShowError "script canceled by user"
148 TABLEISMSDOS="1"
149 fi
150}
151
152FixTable() {
153 if [ "$TABLEISLOOP" == "1" -o "$TABLEISMSDOS" == "1" ] ; then
154 [ -n "$TABLEISLOOP" ] && echo -n "setting partition table to msdos..."
155 [ -n "$TABLEISMSDOS" ] && echo -n "removing all partitions..."
156 # sneaky, sneaky..
157 "$CMPARTED" -s "$SDPATH" mklabel msdos 2>&1 >>"$LOGFILE"
158 echo "done"
159 echo
160 fi
161}
162
163ValidateExtArg() {
164 # validating argument
165 [ "$1" != "ext2" ] && [ "$1" != "ext3" ] && [ "$1" != "ext4" ] && ShowError "'$1' is not a valid filesystem"
166 # return valid argument
167 FUNC_RET="$1"
168}
169
170ValidateSizeArg() {
171 # check for zero-length arg to protect expr length
172 [ -z "$1" ] && ShowError "zero-length argument passed to size-validator"
173
174 SIZEMB=
175 ARGLEN=`expr length $1`
176 SIZELEN=$(($ARGLEN-1))
177 SIZEARG=`expr substr $1 1 $SIZELEN`
178 SIZEUNIT=`expr substr $1 $ARGLEN 1`
179
180 # check if SIZEARG is an integer
181 if [ $SIZEARG -eq $SIZEARG 2> /dev/null ] ; then
182 # look for G
183 [ "$SIZEUNIT" == "G" ] && SIZEMB=$(($SIZEARG * 1024))
184 # look for M
185 [ "$SIZEUNIT" == "M" ] && SIZEMB=$SIZEARG
186 # no units on arg
187 [ -z "$SIZEMB" ] && SIZEMB=$1
188 # check if SIZEARG is a floating point number, GB only
189 elif [ `expr index "$SIZEARG" .` != 0 ] && [ "$SIZEUNIT" == "G" ] ; then
190 INT=`echo "$SIZEARG" | cut -d"." -f1`
191 FRAC=`echo "$SIZEARG" | cut -d"." -f2`
192 SIGDIGITS=`expr length $FRAC`
193 [ -z "$INT" ] && INT=0
194 INTMB=$(($INT * 1024))
195 FRACMB=$((($FRAC * 1024) / (10**$SIGDIGITS)))
196 SIZEMB=$(($INTMB + $FRACMB))
197 # it's not a valid size
198 else
199 ShowError "$1 is not a valid size"
200 fi
201
202 # set partition creation var
203 CREATEPART="$SIZEMB"
204
205 # return valid argument in MB
206 FUNC_RET="$SIZEMB"
207}
208
209UnmountAll () {
210 # unmount all partitions so we can work with /dev/block/mmcblk0
211 # i'm assuming no more than 3 partitions
212 # maybe make a little more elegant later
213 echo -n "unmounting all partitions..."
214 umount "$FATPATH" > /dev/null 2>&1 >>"$LOGFILE"
215 umount "$EXTPATH" > /dev/null 2>&1 >>"$LOGFILE"
216 umount "$SWAPPATH" > /dev/null 2>&1 >>"$LOGFILE"
217 echo "done"
218 echo
219}
220
221
222CalculatePartitions() {
223 # get size of sdcard & do the math
224 # unit MB flag ensures that large print reports MB only
225 SDSIZEMB=`"$CMPARTED" "$SDPATH" unit MB print | grep mmcblk0 | cut -d" " -f3`
226 SDSIZE=${SDSIZEMB%MB}
227 [ -n "$FATSIZE" ] || FATSIZE=$(($SDSIZE-$EXTSIZE-$SWAPSIZE))
228 EXTEND=$(($FATSIZE + $EXTSIZE))
229 SWAPEND=$(($EXTEND + $SWAPSIZE))
230
231 # check for fatsize of 0
232 [ "$FATSIZE" == "0" ] && ShowError "must have a fat32 partition greater than 0MB"
233
234 [ -z $SDSIZE ] && ShowError "zero-length argument passed to partition-calculator"
235
236 # make sure we're not being asked to do the impossible
237 [ $(($FATSIZE + $EXTSIZE +$SWAPSIZE)) -gt $SDSIZE ] && ShowError "sum of requested partitions is greater than sdcard size"
238}
239
240ShowActions () {
241 echo
242 echo "total size of sdcard=$SDSIZEMB"
243 echo
244 echo "the following actions will be performed:"
245 echo " -create $FATSIZE""MB fat32 partition"
246 [ "$EXTSIZE" != "0" ] && echo " -create $EXTSIZE""MB ext2 partition"
247 [ "$SWAPSIZE" != "0" ] && echo " -create $SWAPSIZE""MB swap partition"
248 [ "$EXTFS" != "ext2" ] && echo " -ext2 partition will be upgraded to $EXTFS"
249 echo
250 [ -z "$SILENTRUN" ] && UserAbort && ShowError "script canceled by user"
251 echo
252}
253
254PartitionSdCard() {
255 echo "performing selected actions..."
256 echo
257
258 if [ "FATSIZE" != "0" ] ; then
259 echo -n "creating fat32 partition..."
260 "$CMPARTED" -s "$SDPATH" mkpartfs primary fat32 0 "$FATSIZE"MB 2>&1 >>"$LOGFILE"
261 echo "done"
262 fi
263
264 if [ "$EXTSIZE" != "0" ] ; then
265 echo -n "creating ext2 partition..."
266 "$CMPARTED" -s "$SDPATH" mkpartfs primary ext2 "$FATSIZE"MB "$EXTEND"MB 2>&1 >>"$LOGFILE"
267 echo "done"
268 fi
269
270 if [ "$SWAPSIZE" != "0" ] ; then
271 echo -n "creating swap partition..."
272 "$CMPARTED" -s "$SDPATH" mkpartfs primary linux-swap "$EXTEND"MB "$SWAPEND"MB 2>&1 >>"$LOGFILE"
273 echo "done"
274 fi
275
276 echo
277}
278
279UpgradeExt() {
280 # check for no upgrade
281 [ "$1" == "ext2" ] && return
282 # check for ext partition
283 [ ! -e "$EXTPATH" ] && ShowError "$EXTPATH does not exist"
284
285 # have to use -m switch for this check b/c parted incorrectly
286 # reports all ext partitions as ext2 when running print <number>
287 CHECKEXTFS=`"$CMPARTED" -m "$SDPATH" print | grep ext | cut -d":" -f5`
288 [ "$CHECKEXTFS" == "$1" ] && ShowError "$EXTPATH is already $1"
289
290 # grabbed the code bits for ext3 from upgrade_fs(credit:cyanogen)
291 # check for ext2...must upgrade to ext3 first b4 ext4
292 if [ "$1" == "ext3" -o "$1" == "ext4" ] ; then
293 echo -n "adding journaling to $EXTPATH..."
294 umount /system/sd > /dev/null 2>&1 >>"$LOGFILE"
295 "$CME2FSCK" -p "$EXTPATH" 2>&1 >>"$LOGFILE"
296 "$CMTUNE2FS" -c0 -i0 -j "$EXTPATH" 2>&1 >>"$LOGFILE"
297 echo "done"
298 fi
299
300 # and got convert to ext4 from xda-forum(credit:Denkai)
301 if [ "$1" == "ext4" ] ; then
302 echo -n "converting $EXTPATH to ext4 filesystem..."
303 umount /system/sd > /dev/null 2>&1 >>"$LOGFILE"
304 "$CMTUNE2FS" -O extents,uninit_bg,dir_index "$EXTPATH" 2>&1 >>"$LOGFILE"
305 "$CME2FSCK" -fpDC0 "$EXTPATH" 2>&1 >>"$LOGFILE"
306 echo "done"
307 fi
308 echo ; echo
309}
310
311DowngradeExt() {
312 # check for ext partition
313 [ ! -e "$EXTPATH" ] && ShowError "$EXTPATH does not exist"
314
315 # have to use print for this check b/c parted incorrectly
316 # reports all ext partitions as ext2 when running print <number>
317 CHECKEXTFS=`"$CMPARTED" -m "$SDPATH" print | grep ext | cut -d":" -f5`
318 [ "$CHECKEXTFS" == "$1" ] && ShowError "$EXTPATH is already $1"
319
320 if [ "$CHECKEXTFS" == "ext4" -o "$1" == "ext3"] ; then
321 # interweb says downgrading from ext4 is not possible
322 # without a backup/restore procedure.
323 # if i figure it out, i'll implement it.
324 ShowError "downgrading from ext4 is not currently supported"
325 fi
326
327 if [ "$1" == "ext2" ] ; then
328 echo -n "removing journaling from $EXTPATH..."
329 umount /system/sd > /dev/null 2>&1 >>"$LOGFILE"
330 "$CMTUNE2FS" -O ^has_journal "$EXTPATH" 2>&1 >>"$LOGFILE"
331 "$CME2FSCK" -fp "$EXTPATH" 2>&1 >>"$LOGFILE"
332 echo "done"
333 fi
334 echo ; echo
335}
336
337ShowCardInfo() {
338 echo "retrieving current sdcard information..."
339 echo
340 parted "$SDPATH" print
341 exit 0
342}
343
344
345SCRIPTNAME="sdparted"
346SCRIPTREV="0.5.2"
347MYNAME="51dusty"
348
349SILENTRUN=
350CREATEPART=
351FUNC_RET=
352UEXTFSONLY=
353DEXTFSONLY=
354
355TABLEISLOOP=
356TABLEISMSDOS=
357
358SDSIZE=
359SDSIZEMB=
360SDPATH="/dev/block/mmcblk0"
361
362FATSIZE=
363FATTYPE="fat32"
364FATPATH="/dev/block/mmcblk0p1"
365
366EXTSIZE=512
367EXTFS="ext2"
368EXTPATH="/dev/block/mmcblk0p2"
369
370SWAPSIZE=32
371SWAPTYPE="linux-swap"
372SWAPPATH="/dev/block/mmcblk0p3"
373
374CMPARTED="/sbin/parted"
375CMTUNE2FS="/sbin/tune2fs"
376CME2FSCK="/sbin/e2fsck"
377
378echo ; # give the output some breathing room
379
380# check for arguments
381while [ $# -gt 0 ] ; do
382 case "$1" in
383
384 -h|--help) ShowHelp ; exit 0 ;;
385
386 -fs|--fatsize) shift ; ValidateSizeArg "$1" ; FATSIZE="$FUNC_RET" ;;
387 -es|--extsize) shift ; ValidateSizeArg "$1" ; EXTSIZE="$FUNC_RET" ;;
388 -ss|--swapsize) shift ; ValidateSizeArg "$1" ; SWAPSIZE="$FUNC_RET" ;;
389 -efs|--extfs) shift ; ValidateExtArg "$1" ; EXTFS="$FUNC_RET" ; CREATEPART="$1" ;;
390
391 -ufs|--upgradefs) shift ; ValidateExtArg "$1" ; UEXTFSONLY="$FUNC_RET" ;;
392 -dfs|--downgradefs) shift ; ValidateExtArg "$1" ; DEXTFSONLY="$FUNC_RET" ;;
393
394 -s|--silent) SILENTRUN="$1" ;;
395
396 -po|--printonly) ShowCardInfo ;;
397
398 *) ShowHelp ; ShowError "unknown argument '$1'" ;;
399 esac
400 shift
401done
402
403# make sure sdcard exists and all needed files are here
404CheckReqs
405
406# warning!
407ShowWarning
408
409# unmount all
410UnmountAll
411
412# upgrade only?
413CheckUpgradeOnly
414
415# downgrade only?
416CheckDowngradeOnly
417
418# verify msdos table, warn before convert
419CheckTableType
420
421# fix table if necessary..
422# or remove all partitions if table is ok
423# to prepare for new partitions
424FixTable
425
426# do some math
427CalculatePartitions
428
429# display actions to be carried out and last chance to cancel, if not silent
430ShowActions
431
432# perform partitioning actions
433PartitionSdCard
434
435# upgrade fs if necessary
436UpgradeExt "$EXTFS"
437
438# say goodbye and show print output
439ShowCardInfo