Run multithreaded compressors if available

This commit is contained in:
OmegaSquad82 2020-01-11 23:26:19 +01:00
parent 8b49e9b616
commit cbf0fd6f34
2 changed files with 15 additions and 9 deletions

View File

@ -1,6 +1,6 @@
# PiShrink # # 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 ## ## Usage ##
``` ```
@ -8,7 +8,7 @@ sudo pishrink.sh [-sdrzh] imagefile.img [newimagefile.img]
-s: Skip autoexpand -s: Skip autoexpand
-d: Debug mode on -d: Debug mode on
-r: Use advanced repair options -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 -x: xz compress image after shrinking
-h: display help text -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. * `-s` will skip the autoexpanding part of the process.
* `-d` will create a logfile `pishrink.log` which may help for problem analysis. * `-d` will create a logfile `pishrink.log` which may help for problem analysis.
* `-r` will attempt to repair the filesystem if regular repairs fail * `-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. * `-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. The `.xz` 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 ## ## Prerequisites ##

View File

@ -69,7 +69,7 @@ Usage: $0 [-sdrpzxh] imagefile.img [newimagefile.img]
-d: Write debug messages in a debug log file -d: Write debug messages in a debug log file
-r: Use advanced filesystem repair option if the normal one fails -r: Use advanced filesystem repair option if the normal one fails
-p: Remove logs, apt archives, dhcp leases and ssh hostkeys -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 -x: xz compress image after shrinking
EOM EOM
echo "$help" echo "$help"
@ -83,7 +83,7 @@ usage() {
echo " -d: Debug mode on" echo " -d: Debug mode on"
echo " -r: Use advanced repair options" echo " -r: Use advanced repair options"
echo " -p: Remove logs, apt archives, dhcp leases and ssh hostkeys" 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 " -x: xz compress image after shrinking"
echo " -h: display help text" echo " -h: display help text"
exit -1 exit -1
@ -330,16 +330,22 @@ if ! truncate -s "$endresult" "$img"; then
exit -16 exit -16
fi fi
# https://stackoverflow.com/a/53798785
function is_bin_in_path {
builtin type -P "$1" &> /dev/null
}
if [[ $gzip_compress == true ]]; then if [[ $gzip_compress == true ]]; then
info "Gzipping the shrunk image" 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 img="$img".gz
fi fi
fi fi
if [[ $xz_compress == true ]]; then if [[ $xz_compress == true ]]; then
info "compressing the shrunk image with xz" info "compressing the shrunk image with xz"
if [[ ! $(xz -v -9 "$img") ]]; then if [[ ! $(xz -v -T0 -9 "$img") ]]; then
img="$img".xz img="$img".xz
fi fi
fi fi