Mindtwist.de

...let your mind twist!

How to identify devices that belong to a lvm2 volume group

Sometimes you need to determine, which devices belong to a specific lvm2 volume. For example if you have recorded device statistics with collectl and want to create graphs from the data. Soon you will find out, that the lvm commands will list devices with another notation (e.g. mpath device names) and you need to determine the basic device names. This little script will help you with this.

The Script

The following script determines the SCSI major and minor number of the device that is listed in the lvm2 pvs command for a specific volume group. After that the kernel representation of all disks is printed on screen.

 1 #!/usr/bin/env python
 2 # Display device mapper devices associated with a lvm2 volume group.
 3 # Copyright 2010 by Reiner Rottmann. Released under the BSD License.
 4 import sys
 5 import os
 6 import re
 7 if len(sys.argv) == 1:
 8   print 'ERROR: No lvm2 volume group specified.'
 9   print sys.argv[0], ': Display device mapper devices associated with a lvm2 volume group.'
10   print 'Copyright 2010 by Reiner Rottmann. Released under the BSD License.'
11   print ''
12   print 'Usage:', sys.argv[0], '[VG]...'
13   print ''
14   print 'Example:'
15   print '#', sys.argv[0], 'vgsaplocal'
16   print 'dm-17'
17   print ''
18   print 'Available volume groups:'
19   print ''
20   pvs = os.popen('/sbin/lvm vgs')
21   # skip first line
22   line = pvs.readline()
23   while 1:
24      line = pvs.readline()
25      #  vg00              1  10   0 wz--n- 136.59G  15.50G
26      #  ^^^^
27      if not line: break
28      print line.split()[0]
29   print ''
30   sys.exit(1)
31 else:
32   vg = sys.argv[1]
33 fd = open('/proc/partitions', 'r')
34 pt = fd.read()
35 fd.close()
36 pvs = os.popen('/sbin/lvm pvs').read()
37 #  /dev/sda2  vg00 lvm2 a-   49.00g 20.00g
38 # ^^^^^^^^^^
39 regex = re.compile('.*'+vg+'.*',re.MULTILINE)
40 devices = regex.findall(pvs)
41 for dev in devices:
42   dev = dev.split()[0]
43   ls = os.popen('ls -alL '+dev).read()
44   # brw-rw----. 1 root disk 8, 2 Jun  2 11:07 /dev/sda2
45   #                         ^^^^
46   regex = re.compile('\d+,\s{1}\d+')
47   maj, min = "".join(regex.findall(ls)).split(",")
48   #   8        2   51379200 sda2
49   #   ^^^^^^^^^^
50   regex = re.compile('.*'+maj+'\s+'+min+'.*')
51   dm = "".join(regex.findall(pt)).split()[3]
52   print dm

The script in action

[root@srv306 tmp]# ./pv2dm.py vgsapA35data
dm-20
dm-21
dm-22
dm-23
dm-24
dm-25
dm-26
dm-27
dm-28
dm-29
dm-30
dm-31
dm-32
 

Linux Magazine

Linux Magazine News
  • Rocks Releases Mamba

    The latest version of Rocks cluster distribution – an open source toolkit for real and virtual clusters – has been released.

  • PowerTOP Releases v2.0

    PowerTOP releases v2.0 of its Linux tool, with improved diagnostics and user interface.