#!/bin/bash 
# taken from http://www.nabble.com/%22loopback-mount%22-hard-drive-image-created-with-dd--td14945355.html

INFILE="$1" 
PARTITION="$2" 
MOUNTPOINT="$3" 
OPT="$4" 

if [ "$INFILE" = "" ] ; then echo "Usage: $0 infile partition mountpoint <options>" ; exit ; fi 
if [ "$PARTITION" = "" ] ; then echo "Usage: $0 infile partition mountpoint <options>" ; exit ; fi 
if [ "$MOUNTPOINT" = "" ] ; then echo "Usage: $0 infile partition mountpoint <options>" ; exit ; fi 

if [ "$OPT" != "" ] ; then OPT2=",${OPT}" ; fi 

PSTARTB=`sfdisk -d "$INFILE" | grep "start=" | head -n"$PARTITION" | tail -n1 | sed 's/.*start=[ ]*//' | sed 's/,.*//'` 
PSTART=$[ $PSTARTB * 512 ] 
#PSIZEB=`sfdisk -d "$INFILE" | grep "size=" | head -n4 | tail -n1 | sed 's/.*size=[ ]*//' | sed 's/,.*//'` 
#PSIZE=`echo "$PSIZEB * 512" | bc` 
mount "$INFILE" "$MOUNTPOINT" -o loop,offset="$PSTART""$OPT2" 
#,sizelimit="$PSIZE""$OPT2" 

