Loopback mountin’…a specific partition in a disk image file

Loopback mounts are cool as heck. You can take a file containing an image of a partition and mount on top of your regular filesystem. But what if you have a file containing an entire disk image rather than just an image of a single partition? It’s common to deal with such files when dealing with User Mode Linux (UML) root filesystems.

Well, you can still do it. It turns out that losetup and hence mount which uses losetup to do loopback mounting, allow you to specify an offset option to specify the starting offset into the file from which to read. Now the only trick is to use fdisk to figure out where the partitions are:

$ fdisk -l -u -C 592 FedoraCore6-x86-root_fs 

Disk FedoraCore6-x86-root_fs: 0 MB, 0 bytes
255 heads, 63 sectors/track, 592 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes

                  Device Boot      Start         End      Blocks   Id  System
FedoraCore6-x86-root_fs1   *          63      208844      104391   83  Linux
FedoraCore6-x86-root_fs2          208845     6281414     3036285   8e  Linux LVM

$ echo $((63 * 512))
32256
$ sudo mount FedoraCore6-x86-root_fs fc6-loop -o loop,offset=32256

In this case, I was wanting to mount the first partition, which according to fdisk, starts at sector 63. In order to convert that to a byte offset, I simply multiplied 63 by 512 (the number of bytes per sector) to get a byte offset of 32256. Note that the second partition can’t be mounted this way, because it’s an LVM partition.

One thought on “Loopback mountin’…a specific partition in a disk image file

  1. That’s really useful!

    I found this page via google. I had a full disk image backup (containing all 7 partitions!) but wanted to extract only some files from it. This trick did the job.

    The only minor problem I had: mount at first failed because of no apparent reason. It turned out that the XFS partition I was trying to mount had the same UUID as a currently mounted one (since it was its dd backup), so i had to unmount that one first.

    Thank you for this tip.

Leave a Reply

Your email address will not be published. Required fields are marked *