Skip to content

Instantly share code, notes, and snippets.

@n8henrie
Created May 6, 2019 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n8henrie/ad32137792e16800ffb962ab61a1aa84 to your computer and use it in GitHub Desktop.
Save n8henrie/ad32137792e16800ffb962ab61a1aa84 to your computer and use it in GitHub Desktop.
Script to mount all partitions from an image file into a temporary directory
#!/bin/bash
set -Eeuf -o pipefail
if [ ! "$(whoami)" = root ]; then
echo "Please run as root"
exit 1
fi
TMP="$(mktemp -d)"
chmod 777 "$TMP"
echo "Mounting at: $TMP"
FDISK="$(fdisk -lu "$1")"
SECTORSIZE="$({ grep 'Sector size' | grep --only-matching '[[:digit:]]\+' | head -n 1; } <<<"$FDISK")"
PART=1
while read -r line; do
mkdir -p "$TMP/$PART"
START="$(awk '{print $2}' <<<"$line")"
SIZE="$(awk '{print $4}' <<<"$line")"
mount -o loop,offset="$(( "$SECTORSIZE" * "$START" )),sizelimit=$(( "$SECTORSIZE" * "$SIZE" ))" "$1" "$TMP/$PART"
((PART++))
done < <(awk '/^Device/ { flag=1; next } flag' <<<"$FDISK")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment