#!/bin/bash
# Matt McCarrell 2009

disk=$1
mount_dir=$2

LOSETUP=/sbin/losetup
KPARTX=/sbin/kpartx
MOUNT=`which mount`
MKDIR=`which mkdir`
SED=`which sed`
GREP=`which grep`
ECHO=`which echo`

$LOSETUP -f ${disk}

if [ ! -d "${mount_dir}" ]
then 
  ${MKDIR} ${mount_dir}
fi

loop=`${LOSETUP} -a | ${GREP} ${disk} | ${SED} 's/:[^>]*//g'`
rootfs="`${ECHO} ${loop} | ${SED} 's/.*\///g'`p2"

${KPARTX} -av ${loop}

${MOUNT} /dev/mapper/${rootfs} ${mount_dir}
