mount_list = ['/dev/mapper/vg8-lv8-eSPAN_MAX_d4:eSPAN_MAX_d4:99', '/dev/vg10/lv10:cp:99', '/dev/vg9/lv9:sp:99']
mount_list
is a list li[1] = '/dev/vg10/lv10:cp:99'.
How can I get the number followed by vg10 or lv10 only when it contains a volume ( cp
, sp
are volumes)? It's a bit confusing, so I'll explain completely.
#!/usr/bin/python
import subprocess
import sys
def volumeCheck(volume_name):
"""This function will check volume name is mounted or not.
"""
cmd = ['cat', '/etc/auto_mount_list']
cmd1 = ['cat', '/etc/mdadm.conf']
cmd2 = ['cat', '/proc/mdstat']
vname = sys.argv[1]
p = subprocess.Popen(['df', '-h'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p1, err = p.communicate()
pattern = p1
p2 = subprocess.Popen(cmd,shell=False,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p3, err = p2.communicate()
mount_list = p3.split()
#if mount_list.find(vname) != -1 here i need the number of vg or lv only when it conatin sys.argv[1]
p4 = subprocess.Popen(cmd1,shell=False,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p5, err = p4.communicate()
mdadm_list = p5.split()
p6 = subprocess.Popen(cmd2,shell=False,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p7, err = p6.communicate()
new_vol = '/VolumeData/' + sys.argv[1]
#if pattern.find(new_vol) != -1 : this also give the proper output but below code is more efficiant.
if new_vol in p1:
print 'volume mounted'
else:
print 'volume not mounted'
if __name__ == '__main__':
volumeCheck(sys.argv[1])
Here is the output of my Python program:
root@sr-query:/# python volume_check.py cp
['/dev/mapper/vg8-lv8-eSPAN_MAX_d4:eSPAN_MAX_d4:99', '/dev/vg10/lv10:cp:99', '/dev/vg9/lv9:sp:99']
volume mounted
root@sr-query:/# df -h
Filesystem Size Used Avail Use% Mounted on
rootfs 938M 473M 418M 54% /
/dev/md0 938M 473M 418M 54% /
none 250M 4.9M 245M 2% /dev
/dev/md2 9.7M 1.2M 8.0M 13% /usr/config
/dev/md7 961M 18M 895M 2% /downloads
tmpfs 250M 8.2M 242M 4% /var/volatile
tmpfs 250M 0 250M 0% /dev/shm
tmpfs 250M 0 250M 0% /media/ram
/dev/mapper/vg10-lv10
1016M 65M 901M 7% /VolumeData/cp
root@sr-query:/# python volume_check.py new_volume
['/dev/mapper/vg8-lv8-eSPAN_MAX_d4:eSPAN_MAX_d4:99', '/dev/vg10/lv10:cp:99', '/dev/vg9/lv9:sp:99']
volume not mounted
root@sr-query:/#
Now it's clear that python volume_check.py new_volume
(its argument) is not mounted and also not in the auto mount list. But python volume_check.py cp
is mounted and found in auto mount list... in this 2nd case I need to take the vg and lv number from the auto mount list .
How can I change my code to obtain this solution? Thanks in advance.
Not sure if I understand your question correctly, but I think you want to extract the number from the li[1]
string, correct? If so, just use a regex:
>>> import re
>>> s = '/dev/vg10/lv11:cp:99'
>>> m = re.search('/vg(?P<vg>\d+)/lv(?P<lv>\d+):\w+:(?P<number>\d+)', s)
>>> print m.groupdict()
{'lv': '11', 'number': '99', 'vg': '10'}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.