linuxlvm

Map lvm volume to Physical volume


lsblk provides output in this fornat:

NAME                        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sr0                          11:0    1  1024M  0 rom
sda                           8:0    0   300G  0 disk
sda1                          8:1    0   500M  0 part /boot
sda2                          8:2    0 299.5G  0 part
vg_data1-lv_root (dm-0)  253:0    0    50G  0 lvm  /
vg_data2-lv_swap (dm-1)  253:1    0   7.7G  0 lvm  [SWAP]
vg_data3-LogVol04 (dm-2) 253:2    0  46.5G  0 lvm
vg_data4-LogVol03 (dm-3) 253:3    0  97.7G  0 lvm  /map1
vg_data5-LogVol02 (dm-4) 253:4    0  97.7G  0 lvm  /map2
sdb                           8:16   0    50G  0 disk

for a mounted volume say /map1 how do i directly get the physical volume associated with it. Is there any direct command to fetch the information?


Solution

  • I need to emphasize that there is no direct relation between a mountpoint (logical volume) and a physical volume in LVM. This is one of its design goals.

    However you can traverse the associations between the logical volume, the volume group and physical volumes assigned to that group. However this only tells you: The data is stored on one of those physical volumes, but not where exactly.


    I couldn't find a command which can produce the output directly. However you can tinker something using mount, lvdisplay, vgdisplay and awk|sed:

    mp=/mnt vgdisplay -v $(lvdisplay $(mount | awk -vmp="$mp" '$3==mp{print $1}') | awk '/VG Name/{print $3}')
    

    I'm using the environment variable mp to pass the mount point to the command. (You need to execute the command as root or using sudo)

    For my test-scenario it outputs:

      ...
      --- Volume group ---
      VG Name               vg1
      System ID             
      Format                lvm2
      Metadata Areas        2
      Metadata Sequence No  2
      VG Access             read/write
      VG Status             resizable
      ...
      VG Size               992.00 MiB
      PE Size               4.00 MiB
      Total PE              248
      Alloc PE / Size       125 / 500.00 MiB
      Free  PE / Size       123 / 492.00 MiB
      VG UUID               VfOdHF-UR1K-91Wk-DP4h-zl3A-4UUk-iB90N7
    
      --- Logical volume ---
      LV Path                /dev/vg1/testlv
      LV Name                testlv
      VG Name                vg1
      LV UUID                P0rgsf-qPcw-diji-YUxx-HvZV-LOe0-Iq0TQz
      ...
      Block device           252:0
    
      --- Physical volumes ---
      PV Name               /dev/loop0     
      PV UUID               Qwijfr-pxt3-qcQW-jl8q-Q6Uj-em1f-AVXd1L
      PV Status             allocatable
      Total PE / Free PE    124 / 0
    
      PV Name               /dev/loop1     
      PV UUID               sWFfXp-lpHv-eoUI-KZhj-gC06-jfwE-pe0oU2
      PV Status             allocatable
      Total PE / Free PE    124 / 123
    

    If you only want to display the physical volumes you might pipe the results of the above command to sed:

    above command | sed -n '/--- Physical volumes ---/,$p'