Skip to content

Instantly share code, notes, and snippets.

@mvduin
Created March 14, 2017 19:33
Show Gist options
  • Save mvduin/7662f03425ac19f2c771da6a4437c825 to your computer and use it in GitHub Desktop.
Save mvduin/7662f03425ac19f2c771da6a4437c825 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
shopt -s lastpipe extglob
if [[ $# != 1 ]]; then
echo "Usage: ${BASH_SOURCE} <device or image file>" >&2
false
fi
dev="$1"
/sbin/dumpe2fs -h "$1" | grep 'time:'
/sbin/e2fsck -f -p "$1" || [[ $? == 1 ]]
/sbin/resize2fs -M "$1"
declare -A info
/sbin/dumpe2fs -h "$dev" | grep -P ': *\d+$' | \
while IFS=: read -r k v; do
info["${k// /_}"]="${v##*( )}"
done
bytes=$(( ${info[Block_size]:?} * ${info[Block_count]:?} ))
sectors=$(( bytes / 512 ))
if [[ -f "$dev" ]]; then
echo "truncating file to $bytes bytes..."
truncate --no-create --size=$bytes "$dev"
else
echo "new size is $sectors sectors"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment