mybin

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

shrinkimg (2737B)


      1 #!/bin/bash
      2 
      3 # shrink the (possibly compressed) ext[2-4] disk image $1
      4 # and compress it to $2, overwriting it
      5 
      6 # TODO: support files with multiple partitions
      7 
      8 # TODO: support zerofree
      9 
     10 # TODO: support FAT, but hard to tell it apart from partition tables
     11 
     12 if [ "$#" -ne 2 ] || ! [ -f "$1" ]; then
     13   echo "Usage: $0 INPUT.img(.xz|.gz|) OUTPUT.img.xz" >&2
     14   exit 8
     15 fi
     16 
     17 FILE="$1"
     18 OUT="$2"
     19 DIR=$(dirname $(readlink -f "$OUT"))
     20 # create temporary file in directory of output
     21 TEMP=$(mktemp -p "$DIR")
     22 OSIZE=$(du -h "$FILE" | cut -f1)
     23 
     24 TYPE=$(file "$FILE" | cut -d ':' -f2)
     25 
     26 case $TYPE in
     27   *'XZ compressed'*)
     28     unxz -kvc "$FILE" >$TEMP
     29   ;;
     30   *'gzip compressed'*)
     31     pv -N "gunzip" "$FILE" | gzip -dc >$TEMP
     32   ;;
     33 #  *'boot sector'*)
     34 #    pv -N "cp" "$FILE" >$TEMP
     35 #  ;;
     36   *'ext'[2-4]' filesystem data'*)
     37     pv -N "cp" "$FILE" >$TEMP
     38   ;;
     39   *)
     40     echo "unknown type for $FILE: $TYPE"
     41     echo "aborting"
     42     exit 1
     43   ;;
     44 esac
     45 
     46 echo "extracted/copied to $TEMP"
     47 
     48 TYPE2=$(file "$TEMP" | cut -d ':' -f2)
     49 OSPACE=$(du -h "$TEMP" |cut -f1)
     50 
     51 case $TYPE2 in
     52   *'ext'[2-4]' filesystem data'*)
     53     echo "processing $TEMP as an ext filesystem"
     54     e2fsck -C0 -tt -fp "$TEMP" || {
     55       echo "e2fsck errors in $TEMP, please fix"
     56       echo "aborting WITHOUT removing $TEMP"
     57       exit 2
     58     }
     59     resize2fs -p -M "$TEMP" || {
     60       echo "error when resizing $TEMP";
     61       echo "aborting WITHOUT removing $TEMP"
     62       exit 3
     63     }
     64   ;;
     65 #  *'boot sector'*)
     66 #    echo "processing $TEMP as a FAT filesystem"
     67 #    fsck.vfat -p "$TEMP" || {
     68 #      echo "fsck errors in $TEMP, please fix"
     69 #      echo "aborting WITHOUT removing $TEMP"
     70 #      exit 2
     71 #    }
     72 #    SIZE=$(fatresize -i "$TEMP" | grep '^Min' | cut -d: -f2)
     73 #    fatresize -s $SIZE -p "$TEMP" || {
     74 #      echo "error when resizing $TEMP";
     75 #      echo "aborting WITHOUT removing $TEMP"
     76 #      exit 3
     77 #    }
     78 #    SIZEA=$(fatresize -i "$TEMP" | grep '^Size' | cut -d: -f2)
     79 #    truncate -s $SIZEA fatfs || {
     80 #      echo "error when truncating $TEMP";
     81 #      echo "aborting WITHOUT removing $TEMP"
     82 #      exit 4
     83 #    }
     84 #  ;;
     85   *)
     86     echo "unknown type after decompressing to $TEMP: $TYPE2"
     87     echo "aborting WITHOUT removing $TEMP"
     88     exit 1
     89   ;;
     90 esac
     91 
     92 NSPACE=$(du -h "$TEMP" |cut -f1)
     93 
     94 case "$OUT" in
     95   *.xz)
     96     echo "compressing with xz"
     97     (xz -9cv "$TEMP" >"$OUT") || {
     98       echo "error when compressing $TEMP, please fix";
     99       echo "aborting WITHOUT removing $TEMP"
    100       exit 4
    101     }
    102     rm "$TEMP"
    103     ;;
    104   *)
    105     echo "moving"
    106     mv "$TEMP" "$OUT"
    107   ;;
    108 esac
    109 
    110 NSIZE=$(du -h "$OUT" | cut -f1)
    111 
    112 echo "successfully shrunk $FILE to $OUT"
    113 echo "uncompressed apparent size went from $OSPACE bytes to $NSPACE bytes"
    114 echo "compressed apparent size went from $OSIZE bytes to $NSIZE bytes"
    115 exit 0
    116