Created
May 6, 2019 22:04
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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