@@ -13,6 +13,9 @@ LOOP_DEV=$(losetup -f)
1313PART_N=1
1414MOUNT_POINT=" $( mktemp -d) "
1515ROOT_METHOD=" sudo"
16+ USE_GZIP=" false"
17+ DEFRAG=" true"
18+ GZIP_OPTIONS=" "
1619# /Defaults #
1720
1821help_and_exit (){
@@ -28,7 +31,17 @@ WARNING: This tool makes the assumption:
28312. The partition is ext4
29323. It is a raw disk image
3033
31- USAGE: shrinkwrap_image.sh <file.img>
34+ USAGE:
35+
36+ shrinkwrap_image.sh [-options] <file.img>
37+
38+ OPTIONS:
39+
40+ -d,--no-defrag Skip defrag stage
41+
42+ -z,--gzip Compress final image with gzip
43+
44+ -g,--gz-options Options to pass on to gzip
3245
3346EOF
3447 exit 4
138151}
139152
140153_shrink_image () {
141- local filename=${1}
154+ local filename=" ${1} "
142155 local -i filesize=${2}
143156 local -i local_exit=0
144157
@@ -147,6 +160,44 @@ _shrink_image() {
147160 return ${local_exit}
148161}
149162
163+ _gzip () {
164+ local -i local_exit=0
165+ local filename=" ${1} "
166+
167+ submsg " Compressing image with gzip"
168+ if [ -z ${GZIP_OPTIONS} ]; then
169+ gzip " ${filename} " || local_exit+=1
170+ else
171+ gzip ${GZIP_OPTIONS} " ${filename} " || local_exit+=1
172+ fi
173+
174+ return ${local_exit}
175+ }
176+
177+ switch_checker () {
178+ while [ ! -z " $1 " ]; do
179+ case " $1 " in
180+ --help|-\? )
181+ help_and_exit
182+ ;;
183+ --no-defrag|-d)
184+ DEFRAG=" false"
185+ ;;
186+ --gzip|-z)
187+ USE_GZIP=" true"
188+ ;;
189+ --gzip-options|-g)
190+ GZIP_OPTIONS=" ${2} "
191+ shift
192+ ;;
193+ * )
194+ PARMS+=" ${1} "
195+ ;;
196+ esac
197+ shift
198+ done
199+ }
200+
150201main () {
151202 local -i exit_code=0
152203 local filename=" ${1} "
@@ -164,28 +215,36 @@ main() {
164215 trap " cleanup_abort_fail Interrupt Recived!" 1 2 3 9 15
165216 # This ensures all the data is in continous sectors, which allows us
166217 # to shrink the drive further
167- _defrag
218+ [ $DEFRAG != " false " ] && _defrag
168219 if [ ${?} -ne 0 ]; then
169220 warn " Defrag failed"
170221 exit_code+=1
171222 fi
172223
173224 _resize_part || cleanup_abort_fail " Could not resize partition, you might need to clean up mount points locally"
174225
175- _shrink_image ${filename} ${disk_end}
176- if [ ${?} -ne 0 ]; then
177- warn " Could not shrink image"
178- exit_code+=1
179- fi
180-
181226 _destroy-loop
182227if [ ${?} -ne 0 ]; then
183228 warn " Could not stop loop device ${LOOP_DEV} , you should do this manually"
184229 exit_code+=1
185230fi
186-
187231 rmdir ${MOUNT_POINT} # mktemp directory
232+
233+ _shrink_image ${filename} ${disk_end}
234+ if [ ${?} -ne 0 ]; then
235+ warn " Could not shrink image"
236+ exit_code+=1
237+ fi
188238
239+ if [ ${USE_GZIP} == " true" ]; then
240+ _gzip " ${filename} "
241+ if [ $? -ne 0 ]; then
242+ warn " gzip failed!"
243+ exit_code+=1
244+ fi
245+
246+ filename+=" .gz"
247+ fi
189248 # Final report
190249 filesize_new=$( ls -sh ${filename} | cut -d " " -f 1)
191250 if [ ${exit_code} -eq 0 ]; then
197256 fi
198257
199258}
200-
201- main " ${@ } "
259+ PARMS=" "
260+ switch_checker " ${@ } "
261+ main " ${PARMS} "
0 commit comments