Mounting Raw Disk Images

I have made a few raw disk images for backup purposes or because I needed to dump the data off the drive before it failed. With images you typically write them back to a drive but what if you don't want to? How do you mount a partition from the disk image?

Requirements

  • Disk image
  • FS Drivers for said disk iamge

Mounting

Optionally check the image file.


file /srv/drive_images/sdc.img
sdc.img: DOS/MBR boot sector MS-MBR Windows 7 english at offset 0x163 "Invalid partition table" at offset 0x17b "Error loading operating system" at offset 0x19a "Missing operating system"; partition 1 : ID=0xc, active, start-CHS (0x0,32,33), end-CHS (0x3ff,254,63), startsector 2048, 61765632 sectors

If you have a raw image of a partition, you won't see the extra information about start sector and number of sectors. With raw image files we can treat them as physical disks and therefore we can run the fdisk command on them to get the partition table.


fdisk -l /srv/drive_images/sdc.img
Disk sdc.img: 29.45 GiB, 31625052160 bytes, 61767680 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
Disklabel type: dos
Disk identifier: 0x00000000

Device            Boot Start      End  Sectors  Size Id Type
sdc.img1 	  *     2048 61767679 61765632 29.5G  c W95 FAT32 (LBA)

When images have more than one partition, there will be items in the device column saying sdc.img2, sdc.img3, and so on for each partition. In order to mount a partition within the raw image we need to get the offset. To get this, we have to first figure out where the partition starts, which is at sector 2048. Files do not have sectors and therefore we cannot use them when mounting. Instead we must convert them to bytes. Each sector is 512 bytes and we use that number multiplied by the offset, 2048. So, 2048 * 512 = 1048576 bytes


mount -o ro,loop,offset=1048576 /srv/disk_images/sdc.img /mnt/loop

Mounting raw images of partitions is a lot easier and doesn't require any conversion.


mount -o ro,loop /srv/disk_images/sdc1.img /mnt/loop