Mapping raw image files (from dd or ddrescue) with partition support in linux
When I do full backups of a hard drive, mostly for recovery reasons, I almost always use GNU ddrescue. When I want to access a single file off that image I’ve been using a great windows application called ImDisk. Open it up, point to the raw image on my nas, it will ask me what partition to mount. For NTFS partitions it auto mounts to my windows system and I can grab what ever I need.
But I wanted to do this in linux. I knew about losetup and mapping a loop device. But this does not work for images with partition tables. After little googling I found kpartx. Simple 1 line solution.
kpartx -a -v /path/to/img.file
This will look at that image file’s partition table (I can only confirm MBR support) and map to a loop device. Then it will map each partition at /dev/mapper/loopXpX. If needed you can symlink /dev/mapper/loopXpX to /dev/loopXpX, so application like gparted can understand the partition layout.
** Now to do this in FreeBSD (Like Nas4free/FreeNas)
mdconfig -a -t vnode -u 0 -f /path/to/img.file
this makes a MD device. Now you can mount /dev/mdXpX, or run other file system tools.