mybin

my ~/bin
git clone https://a3nm.net/git/mybin/
Log | Files | Refs | README

commit 0c1bef13bdf85b195f9995fd597c7e548bd48652
parent 76f7f6ce8f13da1475128c1f4f2404830df7cc9b
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Mon,  5 Oct 2015 00:36:36 +0200

TODOs, multiple output formats, use builtin progress, commented FAT

Diffstat:
shrinkimg | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
1 file changed, 59 insertions(+), 23 deletions(-)

diff --git a/shrinkimg b/shrinkimg @@ -3,6 +3,12 @@ # shrink the (possibly compressed) ext[2-4] disk image $1 # and compress it to $2, overwriting it +# TODO: support files with multiple partitions + +# TODO: support zerofree + +# TODO: support FAT, but hard to tell it apart from partition tables + if [ "$#" -ne 2 ] || ! [ -f "$1" ]; then echo "Usage: $0 INPUT.img(.xz|.gz|) OUTPUT.img.xz" >&2 exit 8 @@ -17,15 +23,16 @@ OSIZE=$(du -h "$FILE" | cut -f1) TYPE=$(file "$FILE" | cut -d ':' -f2) -echo "creating $TEMP" - case $TYPE in *'XZ compressed'*) - pv -N "unxz" "$FILE" | xz -dc >$TEMP + unxz -kvc "$FILE" >$TEMP ;; *'gzip compressed'*) pv -N "gunzip" "$FILE" | gzip -dc >$TEMP ;; +# *'boot sector'*) +# pv -N "cp" "$FILE" >$TEMP +# ;; *'ext'[2-4]' filesystem data'*) pv -N "cp" "$FILE" >$TEMP ;; @@ -36,12 +43,45 @@ case $TYPE in ;; esac +echo "extracted/copied to $TEMP" + TYPE2=$(file "$TEMP" | cut -d ':' -f2) +OSPACE=$(du -h "$TEMP" |cut -f1) case $TYPE2 in *'ext'[2-4]' filesystem data'*) - # do nothing + echo "processing $TEMP as an ext filesystem" + e2fsck -C0 -tt -fp "$TEMP" || { + echo "e2fsck errors in $TEMP, please fix" + echo "aborting WITHOUT removing $TEMP" + exit 2 + } + resize2fs -p -M "$TEMP" || { + echo "error when resizing $TEMP"; + echo "aborting WITHOUT removing $TEMP" + exit 3 + } ;; +# *'boot sector'*) +# echo "processing $TEMP as a FAT filesystem" +# fsck.vfat -p "$TEMP" || { +# echo "fsck errors in $TEMP, please fix" +# echo "aborting WITHOUT removing $TEMP" +# exit 2 +# } +# SIZE=$(fatresize -i "$TEMP" | grep '^Min' | cut -d: -f2) +# fatresize -s $SIZE -p "$TEMP" || { +# echo "error when resizing $TEMP"; +# echo "aborting WITHOUT removing $TEMP" +# exit 3 +# } +# SIZEA=$(fatresize -i "$TEMP" | grep '^Size' | cut -d: -f2) +# truncate -s $SIZEA fatfs || { +# echo "error when truncating $TEMP"; +# echo "aborting WITHOUT removing $TEMP" +# exit 4 +# } +# ;; *) echo "unknown type after decompressing to $TEMP: $TYPE2" echo "aborting WITHOUT removing $TEMP" @@ -49,27 +89,23 @@ case $TYPE2 in ;; esac -OSPACE=$(du -h "$TEMP" |cut -f1) - -e2fsck -C0 -tt -fp "$TEMP" || { - echo "e2fsck errors in $TEMP, please fix" - echo "aborting WITHOUT removing $TEMP" - exit 2 -} -resize2fs -p -M "$TEMP" || { - echo "error when resizing $TEMP"; - echo "aborting WITHOUT removing $TEMP" - exit 3 -} - NSPACE=$(du -h "$TEMP" |cut -f1) -(pv -N "xz" "$TEMP" | xz -9c >"$OUT") || { - echo "error when compressing $TEMP, please fix"; - echo "aborting WITHOUT removing $TEMP" - exit 4 -} -rm $TEMP +case "$OUT" in + *.xz) + echo "compressing with xz" + (xz -9cv "$TEMP" >"$OUT") || { + echo "error when compressing $TEMP, please fix"; + echo "aborting WITHOUT removing $TEMP" + exit 4 + } + rm "$TEMP" + ;; + *) + echo "moving" + mv "$TEMP" "$OUT" + ;; +esac NSIZE=$(du -h "$OUT" | cut -f1)