Make pishrink always try to repair the filesystem

There were far too many people having to repair the filesystems by hand. Since the image would be mostly unusable anyway, I feel confident doing it on the image without copying.
This commit is contained in:
Drew Bonasera 2019-08-27 01:13:11 -04:00
parent 27f8780508
commit 97b9a5db39
1 changed files with 20 additions and 63 deletions

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
version="v0.1.1" version="v0.1.2"
CURRENT_DIR=$(pwd) CURRENT_DIR=$(pwd)
SCRIPTNAME="${0##*/}" SCRIPTNAME="${0##*/}"
@ -10,28 +10,6 @@ function info() {
echo "$SCRIPTNAME: $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() { function error() {
echo -n "$SCRIPTNAME: ERROR occured in line $1: " echo -n "$SCRIPTNAME: ERROR occured in line $1: "
shift shift
@ -56,43 +34,28 @@ function logVariables() {
local v var local v var
for var in "$@"; do for var in "$@"; do
eval "v=\$$var" eval "v=\$$var"
echo "$var: $v" >> $LOGFILE echo "$var: $v" >> "$LOGFILE"
done done
fi fi
} }
function checkFilesystem() { function checkFilesystem() {
local stdTest="(( rc < 4 ))"
[[ $paranoia == true ]] && stdTest="(( rc == 0 ))"
local rc
info "Checking filesystem" info "Checking filesystem"
retry "e2fsck -pfttv \"$loopback\"" 3 "$stdTest" e2fsck -pf "$loopback"
rc=$? (( $? < 4 )) && return
(( ! rc )) && return info "Filesystem error detected!"
info "Filesystem error detected" info "Trying to recover corrupted filesystem"
e2fsck -y "$loopback"
(( $? < 4 )) && return
if [[ $paranoia != true ]]; then if [[ $repair == true ]]; then
error $LINENO "e2fsck failed. Filesystem corrupted. Try option -r or option -p." info "Trying to recover corrupted filesystem - Phase 2"
exit -9 e2fsck -fy -b 32768 "$loopback"
(( $? < 4 )) && return
fi fi
error $LINENO "Filesystem recoveries failed. Giving up..."
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 exit -9
} }
@ -102,8 +65,7 @@ help() {
read -r -d '' help << EOM read -r -d '' help << EOM
-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
-r: Try to repair corrupted filesystem -r: Use advanced repair option
-p: Try to repair corrupted filesystem in paranoia mode
EOM EOM
echo "$help" echo "$help"
exit -1 exit -1
@ -111,10 +73,9 @@ EOM
usage() { usage() {
echo "Usage: $0 [-sdrph] imagefile.img [newimagefile.img]" echo "Usage: $0 [-sdrph] imagefile.img [newimagefile.img]"
echo "-s: skip autoexpand" echo "-s: Skip autoexpand"
echo "-d: debug mode on" echo "-d: Debug mode on"
echo "-r: try to repair filesystem errors" echo "-r: Use advanced repair options"
echo "-p: try to repair filesystem errors (paranoia mode)"
echo "-h: display help text" echo "-h: display help text"
exit -1 exit -1
} }
@ -122,14 +83,12 @@ usage() {
should_skip_autoexpand=false should_skip_autoexpand=false
debug=false debug=false
repair=false repair=false
paranoia=false
while getopts ":sdrph" opt; do while getopts ":sdrh" opt; do
case "${opt}" in case "${opt}" in
s) should_skip_autoexpand=true ;; s) should_skip_autoexpand=true ;;
d) debug=true;; d) debug=true;;
r) repair=true;; r) repair=true;;
p) paranoia=true;;
h) help;; h) help;;
*) usage ;; *) usage ;;
esac esac
@ -277,9 +236,7 @@ else
fi fi
#Make sure filesystem is ok #Make sure filesystem is ok
if [[ $repair == true ]]; then
checkFilesystem checkFilesystem
fi
if ! minsize=$(resize2fs -P "$loopback"); then if ! minsize=$(resize2fs -P "$loopback"); then
rc=$? rc=$?