Logo
IT Dienstleistungen

Mount single partition from dd disk image

ever created a dd image of a whole disk and needed to access a single partition? this script contains the necessary steps with an easy menu

dd_mount

#!/bin/bash
# (c) 2012 Manuel Krischer, <manuel.krischer@gmail.com>
# ****************************************************************
# mount single partition of a whole disk dd image as loop device
# (root needed!)
#
# ex: to create image from /dev/sda, use 
# "dd if=/dev/sda of=/home/user/hdd_image.dd bs=32M"
# with this script, you can access the partition from the image
# BETA! Use on own risk
# ***************************************************************
 
# check parameters
CHECK=0
if [[ $# == 2 ]]; then
        if [[ -f $1 ]]; then
                if [[ -d $2 ]]; then
                        CHECK=1
                fi
        fi
fi
if (( $CHECK!=1)); then
        echo "you need a dd image and a mount path as parameter"
        echo -e "\t ex: $0 image.dd /mnt"
        exit
fi
 
#try to get sector size from fdisk output
SECTOR=`fdisk -l -u $1 | grep "Sector size" | cut -d' ' -f4`
echo SECTORSIZE: $SECTOR
echo "********************************************************************************"
fdisk -l -u $1 | grep $1
PARTS=`fdisk -l -u $1 | grep $1 | wc -l`
PARTS=$(($PARTS-1))
echo "********************************************************************************"
 
# check for valid entry
PART=-1
until (( $PART>0&&$PART<$PARTS )); do
        echo "Choose one of your (1-$PARTS) Partitions to mount:"
        read PART
done
 
#get offset from chosen partition
OFFSETNR=`fdisk -l -u $1 | grep $1$PART | sed 's/ \+/ /g' | cut -d' '  -f2`
# check if partion has bootable flag an swap
if [[ $OFFSETNR == "*" ]]; then
        OFFSETNR=`fdisk -l -u $1 | grep  $1$PART | sed 's/ \+/ /g' | cut -d' ' -f3`
fi
 
#Calculate the offset
REALOFFSET=$(($SECTOR * $OFFSETNR))
 
echo -e "PARTITION:\t $PART"
echo -e "PART.-OFFSET:\t $OFFSETNR"
echo -e "SECTORSIZE:\t $SECTOR"
echo -e "REAL OFFSET:\t $REALOFFSET"
 
#try to mount the loop device
echo mount -o loop,offset=$REALOFFSET $1 $2

Seiten-Werkzeuge