Mount a disk image containing LVM

Hard_DiskIf you have an image with standard partitions, you can mount any partition as a loopback device using the offset given by fdisk:

user@machine:/home/user#  fdisk -l disk.img

Disk disk.img: 750.2 GB, 750156374016 bytes
255 heads, 63 sectors/track, 91201 cylinders, total 1465149168 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: 0x00073e63

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   204802047   102400000    7  HPFS/NTFS/exFAT
/dev/sda2       204804094  1465147391   630171649    5  Extended
/dev/sda5       204804096  1457340415   626268160   83  Linux
/dev/sda6      1457342464  1465147391     3902464   82  Linux swap / Solaris

The sda5 partition starts on sector 204804096. We know from fdisk -l that one sector is made of 512 bytes, so we need to multiply that number: 204804096 * 512 = 104859697152

Now we can mount the partition using the specified offset (in bytes):

mount -o loop,offset=104859697152 -t ext4 /dev/sda5 /mnt

LVM

However, if there is LVM inside that image, the above described procedure won’t work. In this case, we need to use the whole image as a loop device first:

user@machine:/home/user#  fdisk -l lvmdisk.raw

Disk lvmdisk.raw: 250.1 GB, 250058268160 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488395055 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: 0x000bad8e

Device Boot      Start         End      Blocks   Id  System
/home/user/lvmdisk.raw1   *          63      208844      104391   83  Linux
/home/user/lvmdisk.raw2          208845   488392064   244091610   8e  Linux LVM

losetup is used to set up and control the loop device on /dev/loop0, while kpartx maps the partitions of the image and maps them as virtual block devices in /dev/mapper.

losetup /dev/loop0 lvmdisk.raw
kpartx -a /dev/loop0

We’ll now be able to see the partitions in /dev/mapper/ as /dev/mapper/loop0p1 and /dev/mapper/loop0p2. Now, we can work with volume groups as usual

vgscan
vgchange -ay VolGroup00
mount -t ext4 /dev/mapper/VolGroup00-LogVol00 /mnt
Tagged with: ,