Added Option to use xz for compression

This commit is contained in:
OmegaSquad82 2020-01-11 13:25:14 +01:00
parent 4f61a3244a
commit 8b49e9b616
2 changed files with 18 additions and 5 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. 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 ## ## Usage ##
``` ```
@ -9,6 +9,7 @@ sudo pishrink.sh [-sdrzh] imagefile.img [newimagefile.img]
-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
-x: xz compress image after shrinking
-h: display help text -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. * `-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. 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 ## ## Prerequisites ##

View File

@ -63,26 +63,28 @@ fi
help() { help() {
local help local help
read -r -d '' help << EOM 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 -s: Don't expand filesystem when image is booted the first time
-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
-x: xz compress image after shrinking
EOM EOM
echo "$help" echo "$help"
exit -1 exit -1
} }
usage() { usage() {
echo "Usage: $0 [-sdrpzh] imagefile.img [newimagefile.img]" echo "Usage: $0 [-sdrpzxh] imagefile.img [newimagefile.img]"
echo "" echo ""
echo " -s: Skip autoexpand" echo " -s: Skip autoexpand"
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"
echo " -x: xz compress image after shrinking"
echo " -h: display help text" echo " -h: display help text"
exit -1 exit -1
} }
@ -91,15 +93,17 @@ should_skip_autoexpand=false
debug=false debug=false
repair=false repair=false
gzip_compress=false gzip_compress=false
xz_compress=false
prep=false prep=false
while getopts ":sdrpzh" opt; do while getopts ":sdrpzxh" opt; do
case "${opt}" in case "${opt}" in
s) should_skip_autoexpand=true ;; s) should_skip_autoexpand=true ;;
d) debug=true;; d) debug=true;;
r) repair=true;; r) repair=true;;
p) prep=true;; p) prep=true;;
z) gzip_compress=true;; z) gzip_compress=true;;
x) xz_compress=true;;
h) help;; h) help;;
*) usage ;; *) usage ;;
esac esac
@ -329,7 +333,14 @@ fi
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 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
fi fi