How to check volume is mounted or not using python with a dynamic volume name

Posted by SR query on Stack Overflow See other posts from Stack Overflow or by SR query
Published on 2012-10-18T10:52:02Z Indexed on 2012/10/18 11:00 UTC
Read the original article Hit count: 155

Filed under:
import subprocess

def volumeCheck(volume_name):
    """This function will check volume name is mounted or not.
    """
volume_name = raw_input('Enter volume name:')
volumeCheck(volume_name)

print 'volume_name=',volume_name    
p = subprocess.Popen(['df', '-h'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p1, err = p.communicate()
pattern = p1

if pattern.find(volume_name):
    print 'volume found'
else:
    print 'volume not found'

While running i always got wrong result "volume found".

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  7.9M  242M   4% /var/volatile
tmpfs                 250M     0  250M   0% /dev/shm
tmpfs                 250M     0  250M   0% /media/ram
**/dev/mapper/vg9-lv9  1016M   65M  901M   7% /VolumeData/sp
/dev/mapper/vg10-lv10
                     1016M   65M  901M   7% /VolumeData/cp**
root@sr-query:/# 
root@sr-query:/# 

root@sr-query:/# python volume_check.py

Enter volume name:raid_10volume

volume_name= raid_10volume
**volume found**
root@sr-query:/# 

I enterd raid_10volume its not listed here please check the df -h command out put(only 2 volume there sp and cp) , then how it reached else part. what is wrong in my code? Thanks in advance.

is there any other way to do this work ! ?

© Stack Overflow or respective owner

Related posts about python