1 August 2009 - 13:42Mount LVM-based volumes from loopback full disk images
Recently I needed to extract some files from the root partition in a full disk backup image taken with dd. I didn’t notice when I took the disk image, but the disk only contained two primary partitions: /boot and an LVM physical volume containing the rest of the partitions as LVM logical volumes. I don’t work with LVM much manually, so I had to look up the commands to get it to find physical volumes and activate volume groups. Here’s the full process of mounting LVM logical volumes from a full disk image:
- There are two ways to get to the LVM partition on this disk, and I’ll cover both: 1) the manual offset finding way and 2) the easy way.
- First, the easy way: make sure the loopback module is inserted with the
max_partparameter, which causes the automatic creation of loopback subdevices for individual partitions. An easy way to make sure is to remove it and re-insert with the right parameter:modprobe -r loop && modprobe loop max_part=63 - Next, mount the whole disk image loopback:
losetup /dev/loop0 sda.img. Now you should see/dev/loop0p1,/dev/loop0p2, etc. for all of the individual partitions. Now you’re already done — you can go directly to the next section to deal with LVM directly. - If you can’t do the easy method, mount the whole disk image loopback to look at the partition offsets:
losetup /dev/loop0 sda.img - Now that /dev/loop0 looks just like the block device image, so check out the partition table sector offsets with fdisk:
fdisk -u -l /dev/loop0. I use sector offsets (-u flag) rather than cylinders because they are easier to work with and some partitions may not fall on cylinder boundaries. My image shows something like this:
Disk /dev/loop0: 250 GB, 250056737280 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488392065 sectors
Units = sectors of 1 * 512 = 512 bytes
Device Boot Start End Blocks Id System
/dev/loop0p1 * 63 401624 200781 83 Linux
/dev/loop0p2 401625 488392064 243987187 8e Linux LVM
losetup -d /dev/loop0 and set /dev/loop0 to just the LVM partition by adding the partition offset. Here, the second partition starts at sector 401625, and each sector is 512 bytes, so the offset is 205632000. Run losetup /dev/loop0 sda.img -o205632000Now whichever method you used, you have a loopback device with the LVM physical volume partition: /dev/loop0p? (2 in my case) if you used the easy way, or /dev/loop0 if you used the manual offset method. Get LVM to recognize the physical volume and activate the volume groups:
- Tell LVM to scan for new physical volumes:
lvm pvscan - Activate the volume groups:
lvm vgchange -ay(it will print something like2 logical volume(s) in volume group "VolGroup00" now active). - Now you can finally mount the LVM logical volumes. Run
lvm lvsto list the logical volumes. Each should appear in /dev/mapper, typically with the device name (volume group name)-(logical volume name), like VolGroup00-LogVol00. - When you are done, unmount all logical volumes and deactivate the volume groups with
lvm vgchange -an. Now you can reclaim the loopback device by usinglosetup -d /dev/loop0.
1 Comment | Tags: Linux