Merge branch 'offer_filesystem_repair_option' of https://github.com/framps/PiShrink

This commit is contained in:
Drew Bonasera 2019-08-26 23:35:15 -04:00
commit 562142b841
2 changed files with 102 additions and 15 deletions

View File

@ -2,9 +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 [-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 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.

View File

@ -1,13 +1,39 @@
#!/bin/bash
version="v0.1"
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,24 +61,81 @@ function logVariables() {
fi
}
usage() { echo "Usage: $0 [-sd] 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 ":sd" 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
@ -78,6 +161,10 @@ if (( EUID != 0 )); then
error $LINENO "You need to be running as root."
exit -3
fi
if [[ -z "$2" ]] && [[ $repair == true || $paranoia == true ]]; then
error $LINENO "Option -r and -p require to specify newimagefile.img."
exit -3
fi
#Check that what we need is installed
for command in parted losetup tune2fs md5sum e2fsck resize2fs; do
@ -194,11 +281,8 @@ else
fi
#Make sure filesystem is ok
info "Checking filesystem"
if ! e2fsck -p -f "$loopback"; then
rc=$?
error $LINENO "fsck failed with rc $rc"
exit -9
if [[ $repair == true ]]; then
checkFilesystem
fi
if ! minsize=$(resize2fs -P "$loopback"); then