From b0ffc3afd97ac4faf1355ca5378dc8bfb29790a2 Mon Sep 17 00:00:00 2001 From: framp Date: Fri, 17 Aug 2018 21:48:21 +0200 Subject: [PATCH 1/5] Initial debug and error checking version --- pishrink.sh | 141 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 119 insertions(+), 22 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index 1d68900..535290e 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -1,23 +1,61 @@ #!/bin/bash +version="v0.1" + +function info() { + echo "$1..." +} + +function error() { + echo -n "ERROR occured in line $1: " + shift + echo "$@" +} + function cleanup() { if losetup $loopback &>/dev/null; then losetup -d "$loopback" fi } -usage() { echo "Usage: $0 [-s] imagefile.img [newimagefile.img]"; exit -1; } +function logVariables() { + if [ "$debug" = true ]; then + echo "--- Line $1" >> "$LOGFILE" + shift + local v var + for var in "$@"; do + eval "v=\$$var" + echo "--- $var: $v" >> $LOGFILE + done + fi +} + +usage() { echo "Usage: $0 [-sd] imagefile.img [newimagefile.img]"; exit -1; } should_skip_autoexpand=false +debug=false -while getopts ":s" opt; do +while getopts ":sd" opt; do case "${opt}" in s) should_skip_autoexpand=true ;; + d) debug=true;; *) usage ;; esac done shift $((OPTIND-1)) +echo "${0##*/} $version" + +CURRENT_DIR=$(pwd) +SCRIPTNAME="${0##*/}" +LOGFILE=${CURRENT_DIR}/${SCRIPTNAME%.*}.log + +if [ "$debug" = true ]; then + info "Creating log file $LOGFILE" + exec 1> >(stdbuf -i0 -o0 -e0 tee -a "$LOGFILE" >&1) + exec 2> >(stdbuf -i0 -o0 -e0 tee -a "$LOGFILE" >&2) +fi + #Args img="$1" @@ -26,11 +64,11 @@ if [[ -z "$img" ]]; then usage fi if [[ ! -f "$img" ]]; then - echo "ERROR: $img is not a file..." + echo "ERROR in $LINENO: $img is not a file..." exit -2 fi if (( EUID != 0 )); then - echo "ERROR: You need to be running as root." + echo "ERROR in $LINENO: You need to be running as root." exit -3 fi @@ -38,17 +76,17 @@ fi for command in parted losetup tune2fs md5sum e2fsck resize2fs; do which $command 2>&1 >/dev/null if (( $? != 0 )); then - echo "ERROR: $command is not installed." + error $LINENO "$command is not installed." exit -4 fi done #Copy to new file if requested if [ -n "$2" ]; then - echo "Copying $1 to $2..." + info "Copying $1 to $2..." cp --reflink=auto --sparse=always "$1" "$2" if (( $? != 0 )); then - echo "ERROR: Could not copy file..." + error $LINENO "Could not copy file..." exit -5 fi old_owner=$(stat -c %u:%g "$1") @@ -60,15 +98,36 @@ fi trap cleanup ERR EXIT #Gather info +info "Gatherin data" beforesize=$(ls -lh "$img" | cut -d ' ' -f 5) -parted_output=$(parted -ms "$img" unit B print | tail -n 1) +logVariables $LINENO beforesize +if ! parted_output=$(parted -ms "$img" unit B print); then + rc=$? + error $LINENO "parted failed with rc $rc" + exit -6 +fi +parted_output=$(tail -n 1 <<< $parted_output) +logVariables $LINENO parted_output + partnum=$(echo "$parted_output" | cut -d ':' -f 1) partstart=$(echo "$parted_output" | cut -d ':' -f 2 | tr -d 'B') -loopback=$(losetup -f --show -o $partstart "$img") -tune2fs_output=$(tune2fs -l "$loopback") +logVariables $LINENO partnum partstart + +info "Mounting image" +if ! loopback=$(losetup -f --show -o $partstart "$img"); then + rc=$? + error $LINENO "losetup failed with rc $rc" + exit -7 +fi +if ! tune2fs_output=$(tune2fs -l "$loopback"); then + error $LINENO "tunefs failed" + exit -8 +fi currentsize=$(echo "$tune2fs_output" | grep '^Block count:' | tr -d ' ' | cut -d ':' -f 2) blocksize=$(echo "$tune2fs_output" | grep '^Block size:' | tr -d ' ' | cut -d ':' -f 2) +logVariables $LINENO tune2fs_output currentsize blocksize + #Check if we should make pi expand rootfs on next boot if [ "$should_skip_autoexpand" = false ]; then #Make pi expand rootfs on next boot @@ -146,43 +205,81 @@ else fi #Make sure filesystem is ok -e2fsck -p -f "$loopback" -minsize=$(resize2fs -P "$loopback" | cut -d ':' -f 2 | tr -d ' ') +info "Checking filesystem" +if ! e2fsck -p -f "$loopback"; then + rc=$? + error $LINENO "fsck failed with rc $rc" + exit -9 +fi + +if ! minsize=$(resize2fs -P "$loopback"); then + rc=$? + error $LINENO "resize2fs failed with rc $rc" + exit -10 +fi +minsize=$(cut -d ':' -f 2 <<< $minsize | tr -d ' ') +logVariables $LINENO minsize if [[ $currentsize -eq $minsize ]]; then - echo "ERROR: Image already shrunk to smallest size" - exit -6 + error $LINENO "Image already shrunk to smallest size" + exit -11 fi #Add some free space to the end of the filesystem extra_space=$(($currentsize - $minsize)) +logVariables $LINENO extra_space for space in 5000 1000 100; do if [[ $extra_space -gt $space ]]; then minsize=$(($minsize + $space)) break fi done +logVariables $LINENO minsize #Shrink filesystem +info "Shrinking filesystem" resize2fs -p "$loopback" $minsize if [[ $? != 0 ]]; then - echo "ERROR: resize2fs failed..." + error $LINENO "resize2fs failed" mount "$loopback" "$mountdir" mv "$mountdir/etc/rc.local.bak" "$mountdir/etc/rc.local" umount "$mountdir" losetup -d "$loopback" - exit -7 + exit -12 fi sleep 1 #Shrink partition partnewsize=$(($minsize * $blocksize)) newpartend=$(($partstart + $partnewsize)) -parted -s -a minimal "$img" rm $partnum >/dev/null -parted -s "$img" unit B mkpart primary $partstart $newpartend >/dev/null +logVariables $LINENO partnewsize newpartend +if ! parted -s -a minimal "$img" rm $partnum; then + rc=$? + error $LINENO "parted failed with rc $rc" + exit -13 +fi + +if ! parted -s "$img" unit B mkpart primary $partstart $newpartend; then + rc=$? + error $LINENO "parted failed with rc $rc" + exit -14 +fi #Truncate the file -endresult=$(parted -ms "$img" unit B print free | tail -1 | cut -d ':' -f 2 | tr -d 'B') -truncate -s $endresult "$img" -aftersize=$(ls -lh "$img" | cut -d ' ' -f 5) +info "Shrinking image" +if ! endresult=$(parted -ms "$img" unit B print free); then + rc=$? + error $LINENO "parted failed with rc $rc" + exit -15 +fi -echo "Shrunk $img from $beforesize to $aftersize" +endresult=$(tail -1 <<< $endresult | cut -d ':' -f 2 | tr -d 'B') +logVariables $LINENO endresult +if ! truncate -s $endresult "$img"; then + rc=$? + error $LINENO "trunate failed with rc $rc" + exit -16 + +aftersize=$(ls -lh "$img" | cut -d ' ' -f 5) +logVariables $LINENO aftersize + +info "Shrunk $img from $beforesize to $aftersize" From 0fea4cc2a70b97b483d8357cec9e80ce36e7a465 Mon Sep 17 00:00:00 2001 From: framp Date: Fri, 17 Aug 2018 22:15:16 +0200 Subject: [PATCH 2/5] Update --- pishrink.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index 535290e..35425ec 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -2,6 +2,35 @@ version="v0.1" +# nice function to get user who invoked this script via sudo +# Borrowed from http://stackoverflow.com/questions/4598001/how-do-you-find-the-original-user-through-multiple-sudo-and-su-commands +# adapted to return current user if no sudoers is used + +function findUser() { + + if [[ -z "$SUDO_USER" || "$SUDO_USER" == "root" ]]; then + echo $USER + return + fi + + thisPID=$$ + origUser=$(whoami) + thisUser=$origUser + + while [ "$thisUser" = "$origUser" ]; do + if [ "$thisPID" = "0" ]; then + thisUser="root" + break + fi + ARR=($(ps h -p$thisPID -ouser,ppid;)) + thisUser="${ARR[0]}" + myPPid="${ARR[1]}" + thisPID=$myPPid + done + + getent passwd "$thisUser" | cut -d: -f1 +} + function info() { echo "$1..." } @@ -13,19 +42,27 @@ function error() { } function cleanup() { - if losetup $loopback &>/dev/null; then - losetup -d "$loopback" - fi + if losetup $loopback &>/dev/null; then + losetup -d "$loopback" + fi + if [ "$debug" = true ]; then + # give logfile back to user + local user=$(findUser) + if [[ $user != "root" ]]; then + chown --reference=/home/$user "$LOGFILE" + fi + fi + } function logVariables() { if [ "$debug" = true ]; then - echo "--- Line $1" >> "$LOGFILE" + echo "Line $1" >> "$LOGFILE" shift local v var for var in "$@"; do eval "v=\$$var" - echo "--- $var: $v" >> $LOGFILE + echo "$var: $v" >> $LOGFILE done fi } @@ -44,18 +81,19 @@ while getopts ":sd" opt; do done shift $((OPTIND-1)) -echo "${0##*/} $version" - CURRENT_DIR=$(pwd) SCRIPTNAME="${0##*/}" LOGFILE=${CURRENT_DIR}/${SCRIPTNAME%.*}.log if [ "$debug" = true ]; then info "Creating log file $LOGFILE" + rm $LOGFILE &>/dev/null exec 1> >(stdbuf -i0 -o0 -e0 tee -a "$LOGFILE" >&1) exec 2> >(stdbuf -i0 -o0 -e0 tee -a "$LOGFILE" >&2) fi +echo "${0##*/} $version" + #Args img="$1" @@ -64,11 +102,11 @@ if [[ -z "$img" ]]; then usage fi if [[ ! -f "$img" ]]; then - echo "ERROR in $LINENO: $img is not a file..." + error $LINENO "$img is not a file..." exit -2 fi if (( EUID != 0 )); then - echo "ERROR in $LINENO: You need to be running as root." + error $LINENO "You need to be running as root." exit -3 fi From c68ff6d502ecc819094116a858cf573f09941790 Mon Sep 17 00:00:00 2001 From: framp Date: Sun, 19 Aug 2018 17:46:55 +0200 Subject: [PATCH 3/5] Added option -r --- pishrink.sh | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/pishrink.sh b/pishrink.sh index 35425ec..5a768ae 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -1,6 +1,6 @@ #!/bin/bash -version="v0.1" +version="v0.1.1" # nice function to get user who invoked this script via sudo # Borrowed from http://stackoverflow.com/questions/4598001/how-do-you-find-the-original-user-through-multiple-sudo-and-su-commands @@ -67,15 +67,17 @@ function logVariables() { fi } -usage() { echo "Usage: $0 [-sd] imagefile.img [newimagefile.img]"; exit -1; } +usage() { echo "Usage: $0 [-sdr] imagefile.img [newimagefile.img]"; exit -1; } should_skip_autoexpand=false debug=false +repair=false -while getopts ":sd" opt; do +while getopts ":sdr" opt; do case "${opt}" in s) should_skip_autoexpand=true ;; d) debug=true;; + r) repair=true;; *) usage ;; esac done @@ -109,6 +111,10 @@ if (( EUID != 0 )); then error $LINENO "You need to be running as root." exit -3 fi +if [[ -z "$2" && $repair == true ]]; then + error $LINENO "Option -r requires to specify newimagefile.img." + exit -3 +fi #Check that what we need is installed for command in parted losetup tune2fs md5sum e2fsck resize2fs; do @@ -244,9 +250,13 @@ fi #Make sure filesystem is ok info "Checking filesystem" -if ! e2fsck -p -f "$loopback"; then +if ! e2fsck -pf "$loopback"; then rc=$? - error $LINENO "fsck failed with rc $rc" + info "e2fsck failed with rc $rc. Filesystem is corrupt. Trying to fix filesystem" + if ! e2fsck -yv "$loopback"; then + rc=$? + error $LINENO "e2fsck -y failed with rc $rc. Giving up to fix corrupted filesystem." + fi exit -9 fi From 700bf3cdcc4fbd69d8517bb04bc1e1a5de6bd627 Mon Sep 17 00:00:00 2001 From: framp Date: Sun, 19 Aug 2018 18:09:58 +0200 Subject: [PATCH 4/5] Updated README --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9c19738..1c899e7 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,11 @@ 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 [-sdr] 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. +Option `-d` will create a logfile `pishrink.log` which may help for problem analysis. +Option `-r` will try to recover a corrupted filesystem. You have to specify the `newimagefile.img` parameter and a recovery is done on a copy of the image. ## Prerequisites ## If you are trying to shrink a [NOOBS](https://github.com/raspberrypi/noobs) image it will likely fail. This is due to [NOOBS paritioning](https://github.com/raspberrypi/noobs/wiki/NOOBS-partitioning-explained) being significantly different than Raspian's. Hopefully PiShrink will be able to support NOOBS in the near future. From 1dfe90f650cc386ad3a54ab1d41788c5f6108ae2 Mon Sep 17 00:00:00 2001 From: framp Date: Tue, 30 Jul 2019 15:33:33 +0200 Subject: [PATCH 5/5] Added additional e2fscks with option -r and -p --- README.md | 5 +- pishrink.sh | 137 +++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 100 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 1c899e7..aae2ec6 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,15 @@ 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 [-sdr] imagefile.img [newimagefile.img]` +`sudo pishrink.sh [-sdrp] 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. Option `-d` will create a logfile `pishrink.log` which may help for problem analysis. Option `-r` will try to recover a corrupted filesystem. You have to specify the `newimagefile.img` parameter and a recovery is done on a copy of the image. +Option `-p` will try to recover a corrupted filesystem in paranoia mode. You have to specify the `newimagefile.img` parameter and a recovery is done on a copy of the image. ## Prerequisites ## -If you are trying to shrink a [NOOBS](https://github.com/raspberrypi/noobs) image it will likely fail. This is due to [NOOBS paritioning](https://github.com/raspberrypi/noobs/wiki/NOOBS-partitioning-explained) being significantly different than Raspian'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. If using Ubuntu, you will likely see an error about `e2fsck` being out of date and `metadata_csum`. The simplest fix for this is to use Ubuntu 16.10 and up, as it will save you a lot of hassle in the long run. diff --git a/pishrink.sh b/pishrink.sh index 7c445c5..cdcd228 100755 --- a/pishrink.sh +++ b/pishrink.sh @@ -2,12 +2,38 @@ version="v0.1.1" +CURRENT_DIR=$(pwd) +SCRIPTNAME="${0##*/}" +LOGFILE=${CURRENT_DIR}/${SCRIPTNAME%.*}.log + function info() { - echo "$1..." + echo "$SCRIPTNAME: $1..." +} + +# Returns 0 for success, <> 0 for failure +function retry() { # command maxretry failuretest + + local tries=1 + local command="$1" # command to retry + local maxRetry=$2 # number of retries + local successtest="$3" # success test + + while (( tries <= maxRetry )); do + info "Trying to recover corrupted filesystem. Trial $tries" + eval "$command" + rc=$? + eval "$successtest" + if (( ! $? )); then + info "Recovered filesystem error" + return 0 + fi + (( tries++ )) + done + return 1 } function error() { - echo -n "ERROR occured in line $1: " + echo -n "$SCRIPTNAME: ERROR occured in line $1: " shift echo "$@" } @@ -35,26 +61,81 @@ function logVariables() { fi } -usage() { echo "Usage: $0 [-sdr] imagefile.img [newimagefile.img]"; exit -1; } +function checkFilesystem() { + + local stdTest="(( rc < 4 ))" + [[ $paranoia == true ]] && stdTest="(( rc == 0 ))" + + local rc + info "Checking filesystem" + retry "e2fsck -pfttv \"$loopback\"" 3 "$stdTest" + rc=$? + + (( ! rc )) && return + + info "Filesystem error detected" + + if [[ $paranoia != true ]]; then + error $LINENO "e2fsck failed. Filesystem corrupted. Try option -r or option -p." + exit -9 + fi + + info "Trying to recover corrupted filesystem (Phase1)" + retry "e2fsck -pftt \"$loopback\"" 3 "stdTest" + (( ! $? )) && return + + info "Trying to recover corrupted filesystem (Phase2)." + retry "e2fsck -yv \"$loopback\"" 3 "$stdTest" + (( ! $? )) && return + + info "Trying to recover corrupted filesystem (Phase3)." + retry "e2fsck -fttvy -b 32768 \"$loopback\"" 3 "$stdTest" + (( ! $? )) && return + + error $LINENO "Filesystem recoveries failed. Giving up to fix corrupted filesystem." + exit -9 + +} + +help() { + local help + read -r -d '' help << EOM +-s: Don't expand filesystem when image is booted the first time +-d: Write debug messages in a debug log file +-r: Try to repair corrupted filesystem +-p: Try to repair corrupted filesystem in paranoia mode +EOM + echo $help + exit -1 +} + +usage() { + echo "Usage: $0 [-sdrph] imagefile.img [newimagefile.img]" + echo "-s: skip autoexpand" + echo "-d: debug mode on" + echo "-r: try to repair filesystem errors" + echo "-p: try to repair filesystem errors (paranoia mode)" + echo "-h: display help text" + exit -1 +} should_skip_autoexpand=false debug=false repair=false +paranoia=false -while getopts ":sdr" opt; do +while getopts ":sdrph" opt; do case "${opt}" in s) should_skip_autoexpand=true ;; d) debug=true;; r) repair=true;; + p) paranoia=true;; + h) help;; *) usage ;; esac done shift $((OPTIND-1)) -CURRENT_DIR=$(pwd) -SCRIPTNAME="${0##*/}" -LOGFILE=${CURRENT_DIR}/${SCRIPTNAME%.*}.log - if [ "$debug" = true ]; then info "Creating log file $LOGFILE" rm $LOGFILE &>/dev/null @@ -80,8 +161,8 @@ if (( EUID != 0 )); then error $LINENO "You need to be running as root." exit -3 fi -if [[ -z "$2" && $repair == true ]]; then - error $LINENO "Option -r requires to specify newimagefile.img." +if [[ -z "$2" ]] && [[ $repair == true || $paranoia == true ]]; then + error $LINENO "Option -r and -p require to specify newimagefile.img." exit -3 fi @@ -113,29 +194,11 @@ trap cleanup ERR EXIT #Gather info info "Gatherin data" beforesize=$(ls -lh "$img" | cut -d ' ' -f 5) -logVariables $LINENO beforesize -if ! parted_output=$(parted -ms "$img" unit B print); then - rc=$? - error $LINENO "parted failed with rc $rc" - exit -6 -fi -parted_output=$(tail -n 1 <<< $parted_output) -logVariables $LINENO parted_output - +parted_output=$(parted -ms "$img" unit B print | tail -n 1) partnum=$(echo "$parted_output" | cut -d ':' -f 1) partstart=$(echo "$parted_output" | cut -d ':' -f 2 | tr -d 'B') -logVariables $LINENO partnum partstart - -info "Mounting image" -if ! loopback=$(losetup -f --show -o $partstart "$img"); then - rc=$? - error $LINENO "losetup failed with rc $rc" - exit -7 -fi -if ! tune2fs_output=$(tune2fs -l "$loopback"); then - error $LINENO "tunefs failed" - exit -8 -fi +loopback=$(losetup -f --show -o $partstart "$img") +tune2fs_output=$(tune2fs -l "$loopback") currentsize=$(echo "$tune2fs_output" | grep '^Block count:' | tr -d ' ' | cut -d ':' -f 2) blocksize=$(echo "$tune2fs_output" | grep '^Block size:' | tr -d ' ' | cut -d ':' -f 2) @@ -218,15 +281,8 @@ else fi #Make sure filesystem is ok -info "Checking filesystem" -if ! e2fsck -pf "$loopback"; then - rc=$? - info "e2fsck failed with rc $rc. Filesystem is corrupt. Trying to fix filesystem" - if ! e2fsck -yv "$loopback"; then - rc=$? - error $LINENO "e2fsck -y failed with rc $rc. Giving up to fix corrupted filesystem." - exit -9 - fi +if [[ $repair == true ]]; then + checkFilesystem fi if ! minsize=$(resize2fs -P "$loopback"); then @@ -295,6 +351,7 @@ if ! truncate -s $endresult "$img"; then rc=$? error $LINENO "trunate failed with rc $rc" exit -16 +fi aftersize=$(ls -lh "$img" | cut -d ' ' -f 5) logVariables $LINENO aftersize