enter image description hereI have an img file, which is packaged in a centos virtual machine. I don't know how to open it. Now I have "parted" it on another machine and found that it is an lvm structure. The question now is how do I mount the lvm part inside the .img file inside my external USB drive


2 Answers 2


Use losetup to associate the image file with a free /dev/loopN device:

losetup -P -f /mnt/usb/vdisk1.img

This will report the name of the actual loop device used: if there are no other loop devices in use, it will report /dev/loop0 and so you will be able to access the partitions within the image as /dev/loop0p1, /dev/loop0p2 and /dev/loop0p3. If the reported loop device name is different, the names of the partition devices will also vary accordingly.

(With the following examples, I assume that the loop device was /dev/loop0.)

Since the first partition is flagged bios_grub, it probably contains just raw embedded BIOS GRUB code, not a filesystem. Therefore there will be no need to inspect it.

If you need to access the second partition in the image, you will be able to do it now:

mkdir /mnt/image/partition2
mount /dev/loop0p2 /mnt/image/partition2

For the LVM in the third partition, first run vgscan to ensure the LVM PV is detected, then vgchange -ay to activate it:

vgchange -ay

If these commands report a duplicate volume group error, then the VG on the image has the same name as an already-activated VG in the local system, and the VG on the image will not activate unless the name conflict is removed first. To rename a VG when you have two VGs with the same name, you'll need the VG UUID: use vgs -o +VG_UUID to view the VG UUIDs, then use vgrename to rename the duplicate, using the UUID to identify the VG to rename. Then run vgchange -ay to try and activate it again.

When vgchange -ay runs without errors, use lvs to view the names of the logical volumes on the image. Now you will be able to mount them using mount /dev/<VG>/<LV> <mountpoint> as usual.

To disconnect the image after you're done with it:

  • first unmount all filesystems you've mounted from the image
  • then deactivate the volume group with vgchange -an <VG>
  • finally detach the loop device: losetup --detach /dev/loop0

Now you will be able to unmount the USB drive without device is busy errors.


Use losetup to create a loop device from the image

losetup -P -f /mnt/usb/vdisk1.img

the content of the image will be available as /dev/loopX, if the LVM structure on it isn't auto activated use vgs and/or vgscan to get name of the volume group, activate it using vgchange -ay <name> and then mount the logical volumes (you can get list of them using lvs and mount them using mount /dev/<vgname>/<lvname> <mountpoint>).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.