Added support for gzip and xz only

This commit is contained in:
framp 2020-02-07 16:49:22 +01:00
parent 965fc206d7
commit ab32008981
2 changed files with 58 additions and 26 deletions

View File

@ -6,12 +6,14 @@ In addition the shrinked image can be compressed with gzip, pigz, xz or pxz to c
## Usage ## ## Usage ##
``` ```
sudo pishrink.sh [-sdirzh] imagefile.img [newimagefile.img] sudo pishrink.sh [-sdirzh] imagefile.img [newimagefile.img]
-s: Skip autoexpand -s Don't expand filesystem when image is booted the first time
-i TOOL: Zip tool to use to compress image. TOOLS can be one of gzip pigz xz pxz (Default: gzip) -d Write debug messages in a debug log file
-d: Debug mode on -r Use advanced filesystem repair option if the normal one fails
-r: Use advanced repair options -p Remove logs, apt archives, dhcp leases and ssh hostkeys
-z: Compress image after shrinking -v Be verbose
-h: display help text -z Compress image after shrinking
-i TOOL Zip tool to use to compress image. TOOLS can be one of $ZIPTOOLS (Default: gzip)
-a Use parallel compress feature of compression tool
``` ```
If you specify the `newimagefile.img` parameter, the script will make a copy of `imagefile.img` and work off that. You will need enough space to make a full copy of the image to use that option. If you specify the `newimagefile.img` parameter, the script will make a copy of `imagefile.img` and work off that. You will need enough space to make a full copy of the image to use that option.
@ -19,7 +21,10 @@ 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 compress the image after shrinking using gzip, pigz, xz or pxz. Default is `gzip`. `.gz` or `.xz` extension will be added to the filename. * `-z` will compress the image after shrinking using gzip or xz. Default is `gzip`. `.gz` or `.xz` extension will be added to the filename.
* `-a` will use option -f9 for gzip and option -T0 for xz.
Default options for parallel compression can be overwritten by defining PISHRINK_GZIP or PSHRINK_XZ environment variable.
## Prerequisites ## ## Prerequisites ##
If you are trying to shrink a [NOOBS](https://github.com/raspberrypi/noobs) image it will likely fail. This is due to [NOOBS partitioning](https://github.com/raspberrypi/noobs/wiki/NOOBS-partitioning-explained) being significantly different than Raspbian's. Hopefully PiShrink will be able to support NOOBS in the near future. If you are trying to shrink a [NOOBS](https://github.com/raspberrypi/noobs) image it will likely fail. This is due to [NOOBS partitioning](https://github.com/raspberrypi/noobs/wiki/NOOBS-partitioning-explained) being significantly different than Raspbian's. Hopefully PiShrink will be able to support NOOBS in the near future.

View File

@ -2,13 +2,14 @@
version="v0.1.2" version="v0.1.2"
CURRENT_DIR=$(pwd) CURRENT_DIR="$(pwd)"
SCRIPTNAME="${0##*/}" SCRIPTNAME="${0##*/}"
LOGFILE=${CURRENT_DIR}/${SCRIPTNAME%.*}.log MYNAME="${SCRIPTNAME%.*}"
LOGFILE="${CURRENT_DIR}/${SCRIPTNAME%.*}.log"
REQUIRED_TOOLS="parted losetup tune2fs md5sum e2fsck resize2fs" REQUIRED_TOOLS="parted losetup tune2fs md5sum e2fsck resize2fs"
ZIPTOOLS=("gzip pigz xz pxz") ZIPTOOLS=("gzip xz")
declare -A ZIPOPTIONS=( [gzip]="-f9" [pigz]="-9" [xz]="-9" [pxz]="-9") # options for zip tools declare -A ZIP_PARALLEL_OPTIONS=( [gzip]="-f9" [xz]="-T0" ) # options for zip tools in parallel mode
declare -A ZIPEXTENSIONS=( [gzip]="gz" [pigz]="gz" [xz]="xz" [pxz]="xz") # extensions of zipped files declare -A ZIPEXTENSIONS=( [gzip]="gz" [xz]="xz" ) # extensions of zipped files
function info() { function info() {
echo "$SCRIPTNAME: $1 ..." echo "$SCRIPTNAME: $1 ..."
@ -67,14 +68,16 @@ fi
help() { help() {
local help local help
read -r -d '' help << EOM read -r -d '' help << EOM
Usage: $0 [-sdirpzh] imagefile.img [newimagefile.img] Usage: $0 [-sdiarpzvh] 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
-i TOOL Zip tool to use to compress image. TOOLS can be one of $ZIPTOOLS (Default: gzip)
-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
-v Be verbose
-z Compress image after shrinking -z Compress image after shrinking
-i TOOL Zip tool to use to compress image. TOOLS can be one of $ZIPTOOLS (Default: gzip)
-a Use parallel compress feature of compression tool
EOM EOM
echo "$help" echo "$help"
exit -1 exit -1
@ -84,19 +87,24 @@ should_skip_autoexpand=false
debug=false debug=false
repair=false repair=false
compress=false compress=false
parallel=false
verbose=false
prep=false prep=false
ziptool="gzip" ziptool="gzip"
required_tools="$REQUIRED_TOOLS" required_tools="$REQUIRED_TOOLS"
while getopts ":si:drpzh" opt; do while getopts ":adhipr:svz" opt; do
case "${opt}" in case "${opt}" in
s) should_skip_autoexpand=true ;; a) parallel=true;;
d) debug=true;; d) debug=true;;
r) repair=true;; h) help;;
p) prep=true;;
z) compress=true;;
i) ziptool="$OPTARG";; i) ziptool="$OPTARG";;
h,*) help;; p) prep=true;;
r) repair=true;;
s) should_skip_autoexpand=true ;;
z) compress=true;;
v) verbose=true;;
*) help;;
esac esac
done done
shift $((OPTIND-1)) shift $((OPTIND-1))
@ -175,7 +183,6 @@ currentsize="$(echo "$tune2fs_output" | grep '^Block count:' | tr -d ' ' | cut -
blocksize="$(echo "$tune2fs_output" | grep '^Block size:' | tr -d ' ' | cut -d ':' -f 2)" blocksize="$(echo "$tune2fs_output" | grep '^Block size:' | tr -d ' ' | cut -d ':' -f 2)"
logVariables $LINENO beforesize parted_output partnum partstart tune2fs_output currentsize blocksize logVariables $LINENO beforesize parted_output partnum partstart tune2fs_output currentsize blocksize
#Check if we should make pi expand rootfs on next boot #Check if we should make pi expand rootfs on next boot
if [ "$should_skip_autoexpand" = false ]; then if [ "$should_skip_autoexpand" = false ]; then
#Make pi expand rootfs on next boot #Make pi expand rootfs on next boot
@ -186,6 +193,7 @@ if [ "$should_skip_autoexpand" = false ]; then
echo "Creating new /etc/rc.local" echo "Creating new /etc/rc.local"
mv "$mountdir/etc/rc.local" "$mountdir/etc/rc.local.bak" mv "$mountdir/etc/rc.local" "$mountdir/etc/rc.local.bak"
#####Do not touch the following lines##### #####Do not touch the following lines#####
cat <<\EOF1 > "$mountdir/etc/rc.local" cat <<\EOF1 > "$mountdir/etc/rc.local"
#!/bin/bash #!/bin/bash
do_expand_rootfs() { do_expand_rootfs() {
@ -333,10 +341,29 @@ if ! truncate -s "$endresult" "$img"; then
fi fi
if [[ $compress == true ]]; then if [[ $compress == true ]]; then
info "Using $ziptool on the shrunk image using options ${ZIPOPTIONS[$ziptool]}" options=""
if [[ ! $($ziptool ${ZIPOPTIONS[$ziptool]} "$img") ]]; then envVarname="${MYNAME^^}_${ziptool^^}"
img=$img.${ZIPEXTENSIONS[$ziptool]} if [[ $parallel == true ]]; then
options="${ZIP_PARALLEL_OPTIONS[$ziptool]}"
[[ -v $envVarname ]] && options="${!envVarname}"
[[ $verbose == true ]] && options="$options -v"
info "Using $ziptool on the shrunk image using options ${options}"
if ! $ziptool ${options} "$img"; then
rc=$?
error $LINENO "$ziptool failed with rc $rc"
exit -18
fi fi
else # sequential
info "Using $ziptool on the shrunk image"
[[ -v $envVarname ]] && options="${!envVarname}"
[[ $verbose == true ]] && options="$options -v"
if ! $ziptool ${options} $img; then
rc=$?
error $LINENO "$ziptool failed with rc $rc"
exit -19
fi
fi
img=$img.${ZIPEXTENSIONS[$ziptool]}
fi fi
aftersize=$(ls -lh "$img" | cut -d ' ' -f 5) aftersize=$(ls -lh "$img" | cut -d ' ' -f 5)