Mount filesystem from image

From Attie's Wiki
Jump to: navigation, search

Use the following script to mount a partition contained within an image file!

#!/bin/bash
 
if [ "$#" -lt "1" ]; then
  echo "usage: $0 <disk_image> [partition] [mount_point]"
  exit 1
fi
 
if [ ! -e "$1" ]; then
  echo "'$1' does not exist..."
  exit 1
fi
 
if [ ! -f "$1" ]; then
  echo "'$1' is not a regular file..."
  exit 1
fi
 
PARTITIONS=`sfdisk -l -uS $1 2> /dev/null | grep -e "^$1" | sed -e 's/ \* /   /'`
 
if [ "$#" = "1" ]; then
  echo "$PARTITIONS"
  exit 0
fi
 
PARTITION=`echo "$PARTITIONS" | grep -e "^$1$2"`
 
if [ "$#" = "2" ]; then
  echo "$PARTITION"
  exit 0
fi
 
if [ ! -d "$3" ]; then
  echo "'$3' is not a directory..."
  exit 1
fi
 
OFFSET=`echo "$PARTITION" | awk '{print $2 * 512}'`
 
echo "Mounting $1$2 on $3..."
sudo mount -o loop,offset=$OFFSET $1 $3

Example Output

$ ./test image.dd
image.dd1            63    144584     144522   c  W95 FAT32 (LBA)
image.dd2        144585   4835564    4690980  83  Linux
image.dd3       4835565   7759394    2923830  83  Linux
image.dd4             0         -          0   0  Empty
$ ./test image.dd 2
image.dd2        144585   4835564    4690980  83  Linux
$ ./test image.dd 2 ./t
$ ls ./t
bin  boot  dev  etc  home  lib  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox