Added missing tail when retrieving partnum
This commit is contained in:
parent
9a2c7fe949
commit
3c3f68f59f
22
pishrink.sh
22
pishrink.sh
|
@ -178,13 +178,14 @@ trap cleanup ERR EXIT
|
||||||
#Gather info
|
#Gather info
|
||||||
info "Gathering data"
|
info "Gathering data"
|
||||||
beforesize="$(ls -lh "$img" | cut -d ' ' -f 5)"
|
beforesize="$(ls -lh "$img" | cut -d ' ' -f 5)"
|
||||||
if ! parted_output="$(parted -ms "$img" unit B print)"; then
|
parted_output="$(parted -ms "$img" unit B print)"
|
||||||
rc=$?
|
rc=$?
|
||||||
|
if (( $rc )); then
|
||||||
error $LINENO "parted failed with rc $rc"
|
error $LINENO "parted failed with rc $rc"
|
||||||
info "Possibly invalid image. Run 'parted $img unit B print' manually to investigate"
|
info "Possibly invalid image. Run 'parted $img unit B print' manually to investigate"
|
||||||
exit -6
|
exit -6
|
||||||
fi
|
fi
|
||||||
partnum="$(echo "$parted_output" | cut -d ':' -f 1)"
|
partnum="$(echo "$parted_output" | tail -n 1 | cut -d ':' -f 1)"
|
||||||
partstart="$(echo "$parted_output" | tail -n 1 | cut -d ':' -f 2 | tr -d 'B')"
|
partstart="$(echo "$parted_output" | tail -n 1 | cut -d ':' -f 2 | tr -d 'B')"
|
||||||
loopback="$(losetup -f --show -o "$partstart" "$img")"
|
loopback="$(losetup -f --show -o "$partstart" "$img")"
|
||||||
tune2fs_output="$(tune2fs -l "$loopback")"
|
tune2fs_output="$(tune2fs -l "$loopback")"
|
||||||
|
@ -308,8 +309,9 @@ logVariables $LINENO minsize
|
||||||
#Shrink filesystem
|
#Shrink filesystem
|
||||||
info "Shrinking filesystem"
|
info "Shrinking filesystem"
|
||||||
resize2fs -p "$loopback" $minsize
|
resize2fs -p "$loopback" $minsize
|
||||||
if [[ $? != 0 ]]; then
|
rc=$?
|
||||||
error $LINENO "resize2fs failed"
|
if (( $rc )); then
|
||||||
|
error $LINENO "resize2fs failed with rc $rc"
|
||||||
mount "$loopback" "$mountdir"
|
mount "$loopback" "$mountdir"
|
||||||
mv "$mountdir/etc/rc.local.bak" "$mountdir/etc/rc.local"
|
mv "$mountdir/etc/rc.local.bak" "$mountdir/etc/rc.local"
|
||||||
umount "$mountdir"
|
umount "$mountdir"
|
||||||
|
@ -322,30 +324,34 @@ sleep 1
|
||||||
partnewsize=$(($minsize * $blocksize))
|
partnewsize=$(($minsize * $blocksize))
|
||||||
newpartend=$(($partstart + $partnewsize))
|
newpartend=$(($partstart + $partnewsize))
|
||||||
logVariables $LINENO partnewsize newpartend
|
logVariables $LINENO partnewsize newpartend
|
||||||
if ! parted -s -a minimal "$img" rm "$partnum"; then
|
parted -s -a minimal "$img" rm "$partnum"
|
||||||
rc=$?
|
rc=$?
|
||||||
|
if (( $rc )); then
|
||||||
error $LINENO "parted failed with rc $rc"
|
error $LINENO "parted failed with rc $rc"
|
||||||
exit -13
|
exit -13
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! parted -s "$img" unit B mkpart primary "$partstart" "$newpartend"; then
|
parted -s "$img" unit B mkpart primary "$partstart" "$newpartend"
|
||||||
rc=$?
|
rc=$?
|
||||||
|
if (( $rc )); then
|
||||||
error $LINENO "parted failed with rc $rc"
|
error $LINENO "parted failed with rc $rc"
|
||||||
exit -14
|
exit -14
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#Truncate the file
|
#Truncate the file
|
||||||
info "Shrinking image"
|
info "Shrinking image"
|
||||||
if ! endresult=$(parted -ms "$img" unit B print free); then
|
endresult=$(parted -ms "$img" unit B print free)
|
||||||
rc=$?
|
rc=$?
|
||||||
|
if (( $rc )); then
|
||||||
error $LINENO "parted failed with rc $rc"
|
error $LINENO "parted failed with rc $rc"
|
||||||
exit -15
|
exit -15
|
||||||
fi
|
fi
|
||||||
|
|
||||||
endresult=$(tail -1 <<< "$endresult" | cut -d ':' -f 2 | tr -d 'B')
|
endresult=$(tail -1 <<< "$endresult" | cut -d ':' -f 2 | tr -d 'B')
|
||||||
logVariables $LINENO endresult
|
logVariables $LINENO endresult
|
||||||
if ! truncate -s "$endresult" "$img"; then
|
truncate -s "$endresult" "$img"
|
||||||
rc=$?
|
rc=$?
|
||||||
|
if (( $rc )); then
|
||||||
error $LINENO "trunate failed with rc $rc"
|
error $LINENO "trunate failed with rc $rc"
|
||||||
exit -16
|
exit -16
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in New Issue