From my PATH: rawmount.sh

posted on 2013-03-05

I've used this script on and off for the last few years. The script is called rawmount.sh.

The script will automatically mount all the partitions inside a device image onto a generated path in /tmp.

You need to run the script as root to allow for the mounting of the loop devices when they have been created.

#!/bin/bash
if [ "x$1" == "x" ]; then
  echo "Usage: rawmount device_dump.raw"
  exit 1
fi

image=$1
kpartx -a "$image" || exit
#OLD losetup -f "$image" || exit
mountPoint="/tmp/mount_$$/"
echo "Image      : $image"
echo "Mount base : $mountPoint"
#loDev=`losetup --associated "$image"| cut -d ":" -f 1|tail -n1`
#echo "Lo device  : $loDev"
#parts=`fdisk -l "$loDev" |awk -- '/^\/dev.*\*/{print $1}'`


parts=`kpartx -l "$image"|cut -d : -f 1|tr -d '\n'`
i=0
echo "Partitions : $parts"
for device in $parts; do
   echo "Mounting: ${device}"
   mp="${mountPoint}${i}"
   mkdir -p "${mp}"
   mount "/dev/mapper/$device" "${mountPoint}${i}" || rmdir "${mp}"
   let i=i+1
done
#Hide our failures
rmdir "${mountPoint}" 2>/dev/null >/dev/null

You can download the script here