如何使用Python来确定哪个Linux设备/分区包含给定的文件系统?
例如
>>> get_filesystem_device('/')
/dev/sda
>>> get_filesystem_partition('/')
/dev/sda1
最佳答案
它不是最卑鄙的,但这会让你开始:
#!/usr/bin/python
import os,stat,subprocess,shlex,re,sys
dev=os.stat('/')[stat.ST_DEV]
major=os.major(dev)
minor=os.minor(dev)
out = subprocess.Popen(shlex.split("df /"),stdout=subprocess.PIPE).communicate()
m=re.search(r'(/[^\s]+)\s',str(out))
if m:
mp= m.group(1)
else:
print "cannot parse df"
sys.exit(2)
print "'/' mounted at '%s' with dev number %i,%i" % (mp,major,minor)
在OS X上:
'/' mounted at '/dev/disk0s2' with dev number 14,2
在Ubuntu上:
'/' mounted at '/dev/sda1' with dev number 8,1