Mounting complete hard drive imagePosted: February 3, 2012
So for backup purposes you decide to make a disk image of your whole hard drive, not just a particular partition.
You can do that very easily, using dd utility. Let’s say we want to make a complete disk image of our first SATA drive. Then we just enter something like this:
- sudo dd if=/dev/sda of=/someplace/sda.img
Ok. Now we have a disk image called sda.img in the directory /someplace. If we later decide to restore the hard drive to its state at the moment when the disk image was created we can just do the opposite to what we did. I.e. something like this:
- sudo dd if=/someplace/sda.img of=/dev/sda
Easy so far.
Now, things get a bit tricky when you want to access individual files stored at a particular partition buried somewhere in your complete hard drive disk image. Still, it can be done pretty quickly in an easy enough way.
First do some setup (the “apt-get install” part you only need to do the first time you are doing this):
- sudo modprobe dm_mod
- sudo apt-get install kpartx
- sudo losetup /dev/loop0 /someplace/sda.img; sudo kpartx -a -v /dev/loop0
At this moment you already have your disk image mounted at the loop device. Now:
- sudo kpartx -a -v /dev/loop0
This will give you a list of partitions available at loop device and their mapping names looking something like this:
add map loop0p1 (252:0): 0 20980827 linear /dev/loop0 63 add map loop0p2 (252:1): 0 467412992 linear /dev/loop0 20981760
And finally just create some mount points for the partitions (if needed) and go ahead and mount them. For example, to mount the second partition by its mapping name loop0p2 at a mount point /someplace/loop2:
- sudo mount /dev/mapper/loop0p2 /someplace/loop2
That is all.