From 8b49e9b6164a7b7a8cb30a898560947a6ef202da Mon Sep 17 00:00:00 2001 From: OmegaSquad82 <34405234+OmegaSquad82@users.noreply.github.com> Date: Sat, 11 Jan 2020 13:25:14 +0100 Subject: [PATCH] Added Option to use xz for compression --- README.md | 4 +++- pishrink.sh | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b69a134..7f7747c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # PiShrink # -PiShrink is a bash script that automatically shrink a pi image that will then resize to the max size of the SD card on boot. This will make putting the image back onto the SD card faster and the shrunk images will compress better. +PiShrink is a bash script that automatically shrink a pi image that will then resize to the max size of the SD card on boot. This will make putting the image back onto the SD card faster and the shrunk images will compress better. Gzip/xz use maximum compression level. ## Usage ## ``` @@ -9,6 +9,7 @@ sudo pishrink.sh [-sdrzh] imagefile.img [newimagefile.img] -d: Debug mode on -r: Use advanced repair options -z: Gzip compress image after shrinking + -x: xz compress image after shrinking -h: display help text ``` @@ -18,6 +19,7 @@ If you specify the `newimagefile.img` parameter, the script will make a copy of * `-d` will create a logfile `pishrink.log` which may help for problem analysis. * `-r` will attempt to repair the filesystem if regular repairs fail * `-z` will Gzip compress the image after shrinking. The `.gz` extension will be added to the filename. +* `-x` will xz compress the image after shrinking. The `.xz` extension will be added to the filename. ## Prerequisites ## diff --git a/pishrink.sh b/pishrink.sh index d2f922a..7edc1c8 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -63,26 +63,28 @@ fi help() { local help read -r -d '' help << EOM -Usage: $0 [-sdrpzh] imagefile.img [newimagefile.img] +Usage: $0 [-sdrpzxh] imagefile.img [newimagefile.img] -s: Don't expand filesystem when image is booted the first time -d: Write debug messages in a debug log file -r: Use advanced filesystem repair option if the normal one fails -p: Remove logs, apt archives, dhcp leases and ssh hostkeys -z: Gzip compress image after shrinking + -x: xz compress image after shrinking EOM echo "$help" exit -1 } usage() { - echo "Usage: $0 [-sdrpzh] imagefile.img [newimagefile.img]" + echo "Usage: $0 [-sdrpzxh] imagefile.img [newimagefile.img]" echo "" echo " -s: Skip autoexpand" echo " -d: Debug mode on" echo " -r: Use advanced repair options" echo " -p: Remove logs, apt archives, dhcp leases and ssh hostkeys" echo " -z: Gzip compress image after shrinking" + echo " -x: xz compress image after shrinking" echo " -h: display help text" exit -1 } @@ -91,15 +93,17 @@ should_skip_autoexpand=false debug=false repair=false gzip_compress=false +xz_compress=false prep=false -while getopts ":sdrpzh" opt; do +while getopts ":sdrpzxh" opt; do case "${opt}" in s) should_skip_autoexpand=true ;; d) debug=true;; r) repair=true;; p) prep=true;; z) gzip_compress=true;; + x) xz_compress=true;; h) help;; *) usage ;; esac @@ -329,7 +333,14 @@ fi if [[ $gzip_compress == true ]]; then info "Gzipping the shrunk image" if [[ ! $(gzip -f9 "$img") ]]; then - img=$img.gz + img="$img".gz + fi +fi + +if [[ $xz_compress == true ]]; then + info "compressing the shrunk image with xz" + if [[ ! $(xz -v -9 "$img") ]]; then + img="$img".xz fi fi