4

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?

Nida Sahar
  • 698
  • 4
  • 13
  • 29

4 Answers4

10

There is no direct command to show that information for a mount. You can run

lvdisplay -m

Which will show which physical volumes are currently being used by the logical volume.

Remember, thought, that there is no such thing as a direct association between a logical volume and a physical volume. Logical volumes are associated with volume groups. Volume groups have a pool of physical volumes over which they can distribute any volume group. If you always want to know that a given lv is on a given pv, you have to restrict the vg to only having that one pv. That rather misses the point. You can use pvmove to push extents off a pv (sometimes useful for maintenance) but you can't stop new extents being created on it if logical volumes are extended or created.

As to why there is no such potentially useful command...

LVM is not ZFS. ZFS is a complete storage and filesystem management system, managing both storage (at several levels of abstraction) and the mounting of filesystems. LVM, in contrast, is just one layer of the Linux Virtual File System. It provides a layer of abstraction on top of physical storage devices and makes no assumption about how the logical volumes are used.

itsbruce
  • 4,825
  • 26
  • 35
  • 2
    "you can't stop new extents being created on it if logical volumes are extended or created" -- No, this is wrong. You can pass the PV name to lvextend, to use extents on a particular physical volume. Despite that, upvoted, because `lvdisplay -m` works well. – Raman Apr 13 '19 at 01:24
  • 1
    According to the man page, lvcreate allows you to optionally specify certain PV(s) from the VG which the LV is then restriced to, so it seems there can be a direct association between an LV and a PV. – Bob Nov 13 '21 at 14:39
7

Leaving the grep/awk/cut/whatever to you, this will show which PVs each LV actually uses:

lvs -o +devices

You'll get a separate line for each PV used by a given LV, so if an LV has extents on three PVs you will see three lines for that LV. The PV device node path is followed by the starting extent(I think) of the data on that PV in parentheses.

Otis
  • 71
  • 1
  • 1
  • 2
    Worth mentioning that the number in brackets e.g. `/dev/sda1(0)` is not the number of extents used on that device, but the first extent of the range. Use `lvdisplay -m` to see the entire range. – Raman Apr 13 '19 at 01:31
4

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'
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    But lvdisplay provides only logical volume information, doesnt display physical volume name – Nida Sahar Feb 18 '15 at 09:16
  • @NidaSahar You are right. It required more attention to get the desired results. Updated my answer. – hek2mgl Feb 18 '15 at 09:55
  • Maybe I'm thick headed, but I don't see how this output shows which PV map to which LV. I see a list of LV and a list of PV and no mapping. Can someone spell it out? – jrwren Oct 24 '21 at 22:09
  • According to the man page, lvcreate allows you to optionally specify certain PV(s) from the VG which the LV is then restriced to, so it seems there can be a direct relation between an LV and a PV. – Bob Nov 13 '21 at 14:40
0
dev=$(df /map1  | tail -n 1|awk  '{print $1}')
echo $dev | grep -q ^/dev/mapper && lvdisplay -m $dev 2>/dev/null  | awk '/Physical volume/{print $3}' || echo $dev
  • What is the question? – Wouter Verhelst Jan 25 '22 at 10:05
  • Actually the above does work! The first line picks out the device in `/dev/mapper` that corresponds to the LV that is mounted at `/map1` storing it in the `dev` variable {it will be something like `/dev/mapper/`"volume group name"`-`"logical volume name" } and the second line checks that the value contains `/dev/mapper` and if so uses `lvdisplay -m` to get the details for that logical volume and then picks out the line(s) that reports the "physical volume(s)" detail... only thing to watch out is that `lvdisplay` may need to be run with super-user powers (`sudo`)! – SlySven Jul 10 '23 at 21:13