list the devices associated with logical volumes without using lvm2 package commands

31,796

Solution 1

There are two possibilities:

If you accept dmsetup as a non-lvm package command (at openSUSE the is a separate package device-mapper) then you can do this:

dmsetup table "${vg_name}-${lv_name}"

Or you do this:

start cmd: # ls -l /dev/mapper/linux-rootfs 
lrwxrwxrwx 1 root root 7 27. Jun 21:34 /dev/mapper/linux-rootfs -> ../dm-0

start cmd: # ls /sys/block/dm-0/slaves/
sda9

Solution 2

Folks , If you certainly needs to dive inside it then you can give thoughts on comparing Major and minor number

ubuntu@ubuntu-OptiPlex-3010:~$ sudo dmsetup ls
vgpool-lvstuff  (253, 0)

I created this logical volume using disk sda1

ubuntu@ubuntu-OptiPlex-3010:~$ sudo dmsetup deps vgpool-lvstuff
1 dependencies  : (8, 1)

(8, 1) gives me the (major,minor) number of disk on which lvm is dependent which I will compare using following command.

ubuntu@ubuntu-OptiPlex-3010:~$ sudo cat /proc/partitions
major minor  #blocks  name
8        0  488386584 sda
8        1     305368 sda1
8        2    3150112 sda2

Solution 3

List all mappers and get all information for each identifier in one line using native commands:

for file in $(ls -la /dev/mapper/* | grep "\->" | grep -oP "\-> .+" | grep -oP " .+"); do echo "MAPPER:"$(F=$(echo $file | grep -oP "[a-z0-9-]+");echo $F":"$(ls "/sys/block/${F}/slaves/");)":"$(df -h "/dev/mapper/${file}" | sed 1d); done;

Result like as:

MAPPER:dm-0:sdd1:/dev/mapper/luks-00000000-0000-0000-0000-000000000000 916G 487G 384G 56% /media/whk/Secure1
MAPPER:dm-1:sde1:/dev/mapper/luks-00000000-0000-0000-0000-000000000000 916G 487G 384G 56% /media/whk/Secure2

The las out is a df -h command.

Thanks to @hauke-laging for the comprension of the structure.

Share:
31,796

Related videos on Youtube

Deepak Ingole
Author by

Deepak Ingole

Updated on September 18, 2022

Comments

  • Deepak Ingole
    Deepak Ingole almost 2 years

    I want to list all the physical volume associated with logical volume.

    I know lvdisplay, pvscan, pvdisplay -m could do the job. but I don't want to use these commands. Is there any other way to do it without using lvm2 package commands?

    Any thoughts on comparing the major and minor numbers of devices?

    • Hauke Laging
      Hauke Laging almost 11 years
      The kernel numbers for a LV are not related to the base device(s). They can even change from one boot to the next.
  • Hauke Laging
    Hauke Laging almost 11 years
    @captain My /dev/mapper/linux-rootfs has been created with LVM. dmsetup uses just the part after /dev/mapper/. The LV rootfs in the VG linux gets the DM name linux-rootfs.
  • Deepak Ingole
    Deepak Ingole almost 11 years
    @ Hauke Laging Let me correct it I created volume named 'test' using dmsetup and tried command ls /sys/block/dm-0/slaves/.I am not seeing any output.
  • Hauke Laging
    Hauke Laging almost 11 years
    @captain And your test is dm-0?
  • Hauke Laging
    Hauke Laging almost 11 years
    @captain Strange. I am not familiar enough with the sysfs details in order to explain that. You could make that a separate question.
  • Deepak Ingole
    Deepak Ingole almost 11 years
    My mistake.ls /sys/block/dm-0/slaves/ worked for me.
  • Joe Grund
    Joe Grund over 5 years
    I've noticed that checking /sys/block/*/slaves/ may only show a single pv reference if a lv is smaller than a single pv. This is even if there are multiple pvs making up a given vg.