From cbf0fd6f3493ade99899e55470530b8ac156c585 Mon Sep 17 00:00:00 2001 From: OmegaSquad82 <34405234+OmegaSquad82@users.noreply.github.com> Date: Sat, 11 Jan 2020 23:26:19 +0100 Subject: [PATCH] Run multithreaded compressors if available --- README.md | 8 ++++---- pishrink.sh | 16 +++++++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7f7747c..63f40f9 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. Gzip/xz use maximum compression level. +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 and thus memory. If pigz is in path it will be used instead of gzip. ## Usage ## ``` @@ -8,7 +8,7 @@ sudo pishrink.sh [-sdrzh] imagefile.img [newimagefile.img] -s: Skip autoexpand -d: Debug mode on -r: Use advanced repair options - -z: Gzip compress image after shrinking + -z: Gzip compress image after shrinking (uses pigz if available) -x: xz compress image after shrinking -h: display help text ``` @@ -18,8 +18,8 @@ If you specify the `newimagefile.img` parameter, the script will make a copy of * `-s` will skip the autoexpanding part of the process. * `-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. +* `-z` will Gzip compress the image after shrinking. If pigz is installed it will use that instead. The `.gz` extension will be added to the filename. +* `-x` will xz compress the image after shrinking using as many threads as cpu cores available. The `.xz` extension will be added to the filename. ## Prerequisites ## diff --git a/pishrink.sh b/pishrink.sh index 7edc1c8..21eb940 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -69,7 +69,7 @@ Usage: $0 [-sdrpzxh] imagefile.img [newimagefile.img] -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 + -z: Gzip compress image after shrinking (uses pigz if available) -x: xz compress image after shrinking EOM echo "$help" @@ -83,7 +83,7 @@ usage() { 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 " -z: Gzip compress image after shrinking (uses pigz if available)" echo " -x: xz compress image after shrinking" echo " -h: display help text" exit -1 @@ -254,7 +254,7 @@ if [[ $prep == true ]]; then info "Syspreping: Removing logs, apt archives, dhcp leases and ssh hostkeys" mountdir=$(mktemp -d) mount "$loopback" "$mountdir" - rm -rf "$mountdir/var/cache/apt/archives/*" "$mountdir/var/lib/dhcpcd5/*" "$mountdir/var/log/*" "$mountdir/var/tmp/*" "$mountdir/tmp/*" "$mountdir/etc/ssh/*_host_*" + rm -rf "$mountdir/var/cache/apt/archives/*" "$mountdir/var/lib/dhcpcd5/*" "$mountdir/var/log/*" "$mountdir/var/tmp/*" "$mountdir/tmp/*" "$mountdir/etc/ssh/*_host_*" umount "$mountdir" fi @@ -330,16 +330,22 @@ if ! truncate -s "$endresult" "$img"; then exit -16 fi +# https://stackoverflow.com/a/53798785 +function is_bin_in_path { + builtin type -P "$1" &> /dev/null +} + if [[ $gzip_compress == true ]]; then info "Gzipping the shrunk image" - if [[ ! $(gzip -f9 "$img") ]]; then + is_bin_in_path pigz && prg='pigz' || prg='gzip' + if [[ ! $($prg -f9 "$img") ]]; then img="$img".gz fi fi if [[ $xz_compress == true ]]; then info "compressing the shrunk image with xz" - if [[ ! $(xz -v -9 "$img") ]]; then + if [[ ! $(xz -v -T0 -9 "$img") ]]; then img="$img".xz fi fi