diff --git a/README.md b/README.md index f7a75fc..f75e76a 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,12 @@ 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. ## Usage ## -`sudo pishrink.sh [-s] imagefile.img [newimagefile.img]` +`sudo pishrink.sh [-s] [-z] imagefile.img [newimagefile.img]` If the `-s` option is given the script will skip the autoexpanding part of the process. 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 the `-z` option is specified, the script will compress the output file to a tarball (*.tar.gz). Note: it does not automatically delete the original `.img` file. + ## 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. diff --git a/pishrink.sh b/pishrink.sh index 1d68900..f5a1338 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -6,13 +6,15 @@ function cleanup() { fi } -usage() { echo "Usage: $0 [-s] imagefile.img [newimagefile.img]"; exit -1; } +usage() { echo "Usage: $0 [-s] [-z] imagefile.img [newimagefile.img]"; exit -1; } should_skip_autoexpand=false +compress_output=false -while getopts ":s" opt; do +while getopts ":s:z" opt; do case "${opt}" in s) should_skip_autoexpand=true ;; + z) compress_output=true ;; *) usage ;; esac done @@ -35,7 +37,7 @@ if (( EUID != 0 )); then fi #Check that what we need is installed -for command in parted losetup tune2fs md5sum e2fsck resize2fs; do +for command in parted losetup tune2fs md5sum e2fsck resize2fs tar; do which $command 2>&1 >/dev/null if (( $? != 0 )); then echo "ERROR: $command is not installed." @@ -186,3 +188,11 @@ truncate -s $endresult "$img" aftersize=$(ls -lh "$img" | cut -d ' ' -f 5) echo "Shrunk $img from $beforesize to $aftersize" + +#Compress the new image +if [ "$compress_output" = true ]; then + img_dir=$(dirname "$img"); + img_filename=$(basename -- "$img"); + echo "Compressing " + tar -C "$img_dir" -czvf "$img_dir/${img_filename%.*}.tar.gz" "$img_filename" +fi