mount partition from drive image with loopback device

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
# Some time ago, I wrote a little script to automate that process, maybe
# it will help you also. (And yes, I know that the code isn't that pretty)
# Direct copy & paste:
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"