Added support for XZ compression

This commit is contained in:
nhkhai 2020-01-24 18:22:37 +08:00 committed by GitHub
parent 4f61a3244a
commit 1a731f48b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 12 deletions

View File

@ -69,6 +69,8 @@ Usage: $0 [-sdrpzh] 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
-x: XZ compress image after shrinking
-e: XZ extreme compress image after shrinking
-z: Gzip compress image after shrinking -z: Gzip compress image after shrinking
EOM EOM
echo "$help" echo "$help"
@ -82,6 +84,8 @@ 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 " -x: XZ compress image after shrinking"
echo " -e: XZ extreme compress image after shrinking"
echo " -z: Gzip compress image after shrinking" echo " -z: Gzip compress image after shrinking"
echo " -h: display help text" echo " -h: display help text"
exit -1 exit -1
@ -90,6 +94,8 @@ usage() {
should_skip_autoexpand=false should_skip_autoexpand=false
debug=false debug=false
repair=false repair=false
xzip_compress=false
exzip_compress=false
gzip_compress=false gzip_compress=false
prep=false prep=false
@ -99,6 +105,8 @@ while getopts ":sdrpzh" opt; do
d) debug=true;; d) debug=true;;
r) repair=true;; r) repair=true;;
p) prep=true;; p) prep=true;;
x) xzip_compress=true;;
e) exzip_compress=true;;
z) gzip_compress=true;; z) gzip_compress=true;;
h) help;; h) help;;
*) usage ;; *) usage ;;
@ -115,11 +123,11 @@ fi
echo "${0##*/} $version" echo "${0##*/} $version"
#Args # Args
src="$1" src="$1"
img="$1" img="$1"
#Usage checks # Usage checks
if [[ -z "$img" ]]; then if [[ -z "$img" ]]; then
usage usage
fi fi
@ -132,7 +140,7 @@ if (( EUID != 0 )); then
exit -3 exit -3
fi fi
#Check that what we need is installed # 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; do
command -v $command >/dev/null 2>&1 command -v $command >/dev/null 2>&1
if (( $? != 0 )); then if (( $? != 0 )); then
@ -141,7 +149,7 @@ for command in parted losetup tune2fs md5sum e2fsck resize2fs; do
fi fi
done done
#Copy to new file if requested # Copy to new file if requested
if [ -n "$2" ]; then if [ -n "$2" ]; then
info "Copying $1 to $2..." info "Copying $1 to $2..."
cp --reflink=auto --sparse=always "$1" "$2" cp --reflink=auto --sparse=always "$1" "$2"
@ -154,7 +162,7 @@ if [ -n "$2" ]; then
img="$2" img="$2"
fi fi
# cleanup at script exit # Cleanup at script exit
trap cleanup ERR EXIT trap cleanup ERR EXIT
#Gather info #Gather info
@ -170,7 +178,7 @@ blocksize=$(echo "$tune2fs_output" | grep '^Block size:' | tr -d ' ' | cut -d ':
logVariables $LINENO tune2fs_output currentsize blocksize logVariables $LINENO 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
mountdir=$(mktemp -d) mountdir=$(mktemp -d)
@ -254,8 +262,7 @@ if [[ $prep == true ]]; then
umount "$mountdir" umount "$mountdir"
fi fi
# Make sure filesystem is ok
#Make sure filesystem is ok
checkFilesystem checkFilesystem
if ! minsize=$(resize2fs -P "$loopback"); then if ! minsize=$(resize2fs -P "$loopback"); then
@ -270,7 +277,7 @@ if [[ $currentsize -eq $minsize ]]; then
exit -11 exit -11
fi fi
#Add some free space to the end of the filesystem # Add some free space to the end of the filesystem
extra_space=$(($currentsize - $minsize)) extra_space=$(($currentsize - $minsize))
logVariables $LINENO extra_space logVariables $LINENO extra_space
for space in 5000 1000 100; do for space in 5000 1000 100; do
@ -281,7 +288,7 @@ for space in 5000 1000 100; do
done done
logVariables $LINENO minsize logVariables $LINENO minsize
#Shrink filesystem # Shrink filesystem
info "Shrinking filesystem" info "Shrinking filesystem"
resize2fs -p "$loopback" $minsize resize2fs -p "$loopback" $minsize
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
@ -294,7 +301,7 @@ if [[ $? != 0 ]]; then
fi fi
sleep 1 sleep 1
#Shrink partition # Shrink partition
partnewsize=$(($minsize * $blocksize)) partnewsize=$(($minsize * $blocksize))
newpartend=$(($partstart + $partnewsize)) newpartend=$(($partstart + $partnewsize))
logVariables $LINENO partnewsize newpartend logVariables $LINENO partnewsize newpartend
@ -310,7 +317,7 @@ if ! parted -s "$img" unit B mkpart primary "$partstart" "$newpartend"; then
exit -14 exit -14
fi fi
#Truncate the file # Truncate the file
info "Shrinking image" info "Shrinking image"
if ! endresult=$(parted -ms "$img" unit B print free); then if ! endresult=$(parted -ms "$img" unit B print free); then
rc=$? rc=$?
@ -326,6 +333,20 @@ if ! truncate -s "$endresult" "$img"; then
exit -16 exit -16
fi fi
if [[ $xzip_compress == true ]]; then
info "XZipping the shrunk image"
if [[ ! $(xz -9v "$img") ]]; then
img=$img.xz
fi
fi
if [[ $exzip_compress == true ]]; then
info "Extreme XZipping the shrunk image"
if [[ ! $(xz -9ve "$img") ]]; then
img=$img.xz
fi
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