Go to ...

Techpository

A Better Technical Repository

RSS Feed

Linux: Mounting a partition within a disk image


When dealing with partition mounting within an image file it is always best to examine the partition table of the original disk.
To do this type:

fdisk -l /dev/sda (/dev/sda is the standard first hard drive device location.  Modify this accordingly to meet your drive situation)

Disk /dev/sda: 1024 MB, 1024966656 bytes
255 heads, 63 sectors/track, 124 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1          20      160618+  83  Linux
/dev/sda2              21         124      835380   83  Linux

Create an image of the disk

dd if=/dev/sda of=test.dd

Verify your image integrity (sanity)

fdisk -C 124 test.dd

WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).

Command (m for help):

Press “p” and “enter”

Command (m for help): p

Disk test.dd: 0 MB, 0 bytes
255 heads, 63 sectors/track, 124 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x73e7e9f6

Device Boot      Start         End      Blocks   Id  System
test.dd1               1          18      144553+  83  Linux
test.dd2              19         124      851445   83  Linux

These typically should be identicle.  If not you may still be able to mount the partition

Let’s say that we want to mount the second partition.  We can do this by calculating the offset.

First do the following:

fdisk -l -u -C 124 test.dd

Disk test.dd: 0 MB, 0 bytes
255 heads, 63 sectors/track, 1 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x73e7e9f6

Device Boot      Start         End      Blocks   Id  System
test.dd1              63      289169      144553+  83  Linux
test.dd2          289170     1992059      851445   83  Linux

The starting sector is at 289170.  Since we can see from the above partition structure that our Sector size it 512 bytes.  To calculate the “byte” offset that we will need to use multiply the following:
289170 x 512 = 148055040
So 148055040 is our offset

sudo mount -t ext2 -o loop,offset=148055040 test.dd /mnt/
[sudo] password for user:
[email protected]:~/home/example$ cd /mnt
[email protected]:/mnt$ ls
boot  etc  lib  lost+found  mnt  root  usr  var

We now have full access to the second partition within this disk image.