Showing posts with label LVM. Show all posts
Showing posts with label LVM. Show all posts

Monday, September 18, 2023

Mounting File Systems in a Disk Image on Linux

On Linux systems, we can create disk image using the dd command. This post lists the steps to mount file systems, in particular, LVM volumes in an image of a whole disk, which is often created as follows,


dd if=/dev/sdb of=/mnt/disk1/sdb.img bs=1M status=progress

Assuming the disk has multiple partitions, how do we mount the file systems on these partitions? The following are the steps,


# 1. mount the disk where the disk image is
#    we assume the disk is /dev/sdb1, and we mount
#    it on directory win
sudo mount /dev/sdb1 win

# 2. map the partitions to loopback devices
#    here we assume the disk image is win/disks/disk1.img
sudo losetup -f -P win/disks/disk1.img

# 3. list the LVM volumes
sudo lvdisplay

# 4. suppose from the input of the above command, 
#    the volumne is shown as /dev/mylvm/lvol0,
#    and we want it mounted on directory lvol0
sudo mount /dev/mylvm/lvol0 lvol0

# 5. do something we want ...


# 6. unmount the volume
sudo umount lvol0

# 7. deactivate LVM volume
#    we can query, confirm the volume group by
#    vgdisplay
sudo vgchange -a n mylvm

# 8. detatch the loopback device
#    assuming the device is /dev/loop0
sudo losetup -d /dev/loop0

# 9. umount the disk
sudo umount win

Sunday, May 7, 2017

Mounting Logical Volume Management (LVM) Volumes

I have two hard drives from an old Linux machine. These hard drives were under one Logical Volume Management volume. On another Linux host, I found these tools were very useful to get the data out.

  • Scan LVM volumes

    sudo lvscan

  • Activate LVM volumes

    modprobe dm-mod
    vgchange -ay

  • List and remove mapped LVM devices

    dmsetup ls
    dmsetup remove <device>

    where an example of "device" can be VolGroup00-LogVol01

Troubleshooting

When you see an error message like the following when you try to mount a volume,


# mount /dev/VolGroup00/LogVol00 mnt/
  mount: /dev/mapper/VolGroup00-LogVol00 is write-protected, mounting read-only
  mount: /dev/mapper/VolGroup00-LogVol00: can't read superblock

One solution that has worked for me many times is to remove the mapped LVM devices using the command we discussed in the above. For instance,

# dmsetup remove VolGroup00-LogVol00

If you perform a lvscan, you will find that the volume is inactive,

# lvscan
  inactive          '/dev/VolGroup00/LogVol00' [459.53 GiB] inherit

To activate the volume, we can run the following

vgchange -ay

Now you shall see that the volume is active,

# lvscan
  ACTIVE            '/dev/VolGroup00/LogVol00' [459.53 GiB] inherit

We can now mount the volume,

# mount /dev/VolGroup00/LogVol00 mnt/