Skip to content

Commit a8ce0e9

Browse files
committed
updated shrinkwrap:
* some minor cleanups * can now compress final image with gzip * can now skip defrag stage
1 parent aefdd04 commit a8ce0e9

File tree

2 files changed

+84
-13
lines changed

2 files changed

+84
-13
lines changed

man/shrink_wrap.1

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,16 @@ WARNING: This tool makes the assumption:
1515
2. The partition is ext4
1616
3. It is a raw disk image
1717

18+
.SH OPTIONS
19+
.TP
20+
.BR \-d ", --no-defrag"
21+
Skip defrag stage
22+
.TP
23+
.BR \-z ", --gzip"
24+
Compress final image with gzip
25+
.TP
26+
.BR \-g ", --gz-options"
27+
Options to pass on to gzip, see \fIgzip\fR(1) for more info
28+
1829
.SH SEE ALSO
19-
\fImount_image\fR(1) \fIinit_image\fR(1)
30+
\fImount_image\fR(1) \fIinit_image\fR(1) \fIgzip\fR(1)

shrinkwrap_image.sh

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ LOOP_DEV=$(losetup -f)
1313
PART_N=1
1414
MOUNT_POINT="$(mktemp -d)"
1515
ROOT_METHOD="sudo"
16+
USE_GZIP="false"
17+
DEFRAG="true"
18+
GZIP_OPTIONS=""
1619
# /Defaults #
1720

1821
help_and_exit(){
@@ -28,7 +31,17 @@ WARNING: This tool makes the assumption:
2831
2. The partition is ext4
2932
3. 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
3346
EOF
3447
exit 4
@@ -138,7 +151,7 @@ EOF
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+
150201
main() {
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
182227
if [ ${?} -ne 0 ];then
183228
warn "Could not stop loop device ${LOOP_DEV}, you should do this manually"
184229
exit_code+=1
185230
fi
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
@@ -197,5 +256,6 @@ fi
197256
fi
198257

199258
}
200-
201-
main "${@}"
259+
PARMS=""
260+
switch_checker "${@}"
261+
main "${PARMS}"

0 commit comments

Comments
 (0)