Update
This commit is contained in:
parent
b0ffc3afd9
commit
0fea4cc2a7
50
pishrink.sh
50
pishrink.sh
|
@ -2,6 +2,35 @@
|
||||||
|
|
||||||
version="v0.1"
|
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() {
|
function info() {
|
||||||
echo "$1..."
|
echo "$1..."
|
||||||
}
|
}
|
||||||
|
@ -16,16 +45,24 @@ function cleanup() {
|
||||||
if losetup $loopback &>/dev/null; then
|
if losetup $loopback &>/dev/null; then
|
||||||
losetup -d "$loopback"
|
losetup -d "$loopback"
|
||||||
fi
|
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() {
|
function logVariables() {
|
||||||
if [ "$debug" = true ]; then
|
if [ "$debug" = true ]; then
|
||||||
echo "--- Line $1" >> "$LOGFILE"
|
echo "Line $1" >> "$LOGFILE"
|
||||||
shift
|
shift
|
||||||
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
|
||||||
}
|
}
|
||||||
|
@ -44,18 +81,19 @@ while getopts ":sd" opt; do
|
||||||
done
|
done
|
||||||
shift $((OPTIND-1))
|
shift $((OPTIND-1))
|
||||||
|
|
||||||
echo "${0##*/} $version"
|
|
||||||
|
|
||||||
CURRENT_DIR=$(pwd)
|
CURRENT_DIR=$(pwd)
|
||||||
SCRIPTNAME="${0##*/}"
|
SCRIPTNAME="${0##*/}"
|
||||||
LOGFILE=${CURRENT_DIR}/${SCRIPTNAME%.*}.log
|
LOGFILE=${CURRENT_DIR}/${SCRIPTNAME%.*}.log
|
||||||
|
|
||||||
if [ "$debug" = true ]; then
|
if [ "$debug" = true ]; then
|
||||||
info "Creating log file $LOGFILE"
|
info "Creating log file $LOGFILE"
|
||||||
|
rm $LOGFILE &>/dev/null
|
||||||
exec 1> >(stdbuf -i0 -o0 -e0 tee -a "$LOGFILE" >&1)
|
exec 1> >(stdbuf -i0 -o0 -e0 tee -a "$LOGFILE" >&1)
|
||||||
exec 2> >(stdbuf -i0 -o0 -e0 tee -a "$LOGFILE" >&2)
|
exec 2> >(stdbuf -i0 -o0 -e0 tee -a "$LOGFILE" >&2)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo "${0##*/} $version"
|
||||||
|
|
||||||
#Args
|
#Args
|
||||||
img="$1"
|
img="$1"
|
||||||
|
|
||||||
|
@ -64,11 +102,11 @@ if [[ -z "$img" ]]; then
|
||||||
usage
|
usage
|
||||||
fi
|
fi
|
||||||
if [[ ! -f "$img" ]]; then
|
if [[ ! -f "$img" ]]; then
|
||||||
echo "ERROR in $LINENO: $img is not a file..."
|
error $LINENO "$img is not a file..."
|
||||||
exit -2
|
exit -2
|
||||||
fi
|
fi
|
||||||
if (( EUID != 0 )); then
|
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
|
exit -3
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue