#!/bin/sh
# mount_img.sh
# 2016-02-18
# by Gernot Walzl
# This script mounts a partition contained by an image of an sd card.
# The image can be read using dd:
# dd bs=1M if=/dev/mmcblk0 of=sdcard.img
#
# The following command lists available partitions:
# fidsk -l sdcard.img
if [ "$(id -u)" != "0" ]; then
echo "Error: This script must be run as root"
exit 1
fi
IMG="$1"
PART="$2"
DIR="$3"
if [ -z "$IMG" -o -z "$PART" -o -z "$DIR" ]; then
echo "Usage: $0 image partitionnumber dir"
echo "Example: $0 raspbian-jessie.img 2 raspberrypi/root-fs"
exit 1
fi
STARTSECT=$(fdisk -l "$IMG" | grep "^$IMG$PART" | awk '{print $2}')
OFFSET=$(($STARTSECT * 512))
mount -o loop,offset=$OFFSET $IMG $DIR