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