Add function for detect device parameters. (LVM and RAID)

Fix getPartitionSize from partition with zero size.
Added
countPartitions - get count partition on devices by /sysfs.
getLvmGroups - get all LVM groups.
getLvmPartitions - get partitions using by LVM, specified by vg_name and
                   lv_name.
getPartitionDevice - get real deivce using by partition,lvm or mdraid
getDeviceType - get scheme of device (specified by syspath).
                Example: disk-partition-raid-lvm
getRaidPartitions - get raid partitions.
getPartitionType - get partition table (primary,extended,logical)
getOsProberHash - generate hash for installed OS on all partitions.
develop
Mike Hiretsky 13 years ago
parent ba78707547
commit a073ca298a

@ -24,8 +24,9 @@ from cl_vars_share import varsShare, clLocale
from os.path import exists as pathexists
from os import path
from cl_utils import isMount, genpassword, \
getAvailableVideo, \
listDirectory,isPkgInstalled,lspci, readLinesFile
getAvailableVideo, process, \
listDirectory,isPkgInstalled,lspci, readLinesFile, \
getUdevDeviceInfo
from utils import ip
from encrypt import getHash

@ -1037,6 +1037,73 @@ def getSquashList():
return map(lambda x:{"lzma":"xz"}.get(x,x),
list(set(usesSquashFs) & wantMethod))
def countPartitions(devname):
"""Count partition for specified device"""
syspath = getUdevDeviceInfo(name=devname).get('DEVPATH','')
if not syspath:
return 0
deviceName = path.basename(syspath)
if not syspath.startswith("/sys"):
syspath = pathJoin("/sys",syspath)
return len(filter(lambda x:x.startswith(deviceName),
listDirectory(syspath)))
def getLvmGroups():
"""Get LVM groups"""
pvdisplayCmd = getProgPath('/sbin/pvdisplay')
pvdata = process(pvdisplayCmd,"-C","-o", "vg_name","--noh")
return filter(lambda x:x,pvdata.read().split())
def getLvmPartitions(vg_name,lv_name,cache=[]):
"""Get lvm partitions"""
if not cache:
pvdisplayCmd = getProgPath('/sbin/pvdisplay')
pvdata = process(pvdisplayCmd,"-C","-o",
"vg_name,lv_name,pv_name","--noh")
if pvdata.success():
cache.extend(
filter(lambda x:x and len(x)==3,
map(lambda x:x.split(),
pvdata.read().split('\n'))))
if cache:
res = map(lambda x:x[2],
filter(lambda x:x[0]==vg_name and x[1]==lv_name,cache))
if res:
return res
return []
def getPartitionDevice(syspath):
"""Get real parent device by partition,lvm,mdraid"""
prop = getUdevDeviceInfo(path=syspath)
# real device
if prop.get('ID_TYPE',"") == "disk" and \
prop.get('DEVTYPE',"") == "disk":
return prop.get('DEVNAME',"")
# partition
if prop.get('DEVTYPE') == "partition":
return getPartitionDevice(path.dirname(syspath))
# md raid
if prop.get('MD_LEVEL',"").startswith("raid"):
if not syspath.startswith('/sys'):
syspath = pathJoin('/sys',syspath)
syspath = pathJoin(syspath,"md")
for rd in filter(lambda x:path.basename(x).startswith('rd'),
listDirectory(syspath,fullPath=True)):
rdBlockPath = path.join(rd,"block")
if path.exists(rdBlockPath):
return getPartitionDevice(path.realpath(rdBlockPath))
else:
return ""
# lvm
if prop.get('DM_LV_NAME',"") != "":
parts = getLvmPartitions(prop.get('DM_VG_NAME',''),
prop.get('DM_LV_NAME',''))
if parts:
propPartLvm = getUdevDeviceInfo(name=parts[0])
if 'DEVPATH' in propPartLvm:
return getPartitionDevice(propPartLvm['DEVPATH'])
return ""
def getAvailableX11Drivers(prefix="/"):
"""Get available x11 drivers (Depricated Function)"""
xorg_modules_dir = path.join(prefix,'usr/lib/xorg/modules/drivers')
@ -1088,18 +1155,146 @@ def getUdevDeviceInfo(path="",name=""):
def getPartitionSize(dev):
"""Get partition size"""
SECTORSIZE=512
syspath = getUdevDeviceInfo(name=dev).get('DEVPATH','')
if syspath:
sizeFile = pathJoin("/sys",syspath,"size")
if path.exists(sizeFile):
size = float(open(sizeFile,'r').read().strip())*SECTORSIZE
suffix = ((1024**0,""),
(1024**1,"KiB"),
(1024**2,"MiB"),
(1024**3,"GiB"),
(1024**4,"TiB"),
(1024**5,"PiB"))
suffix = filter(lambda x:size >=x[0],suffix)[-1]
if suffix:
return "%.1f %s"%(size/suffix[0],suffix[1])
sizeFile = pathJoin(dev,"size")
if path.exists(sizeFile):
size = float(open(sizeFile,'r').read().strip())*SECTORSIZE
suffix = ((1024**0,""),
(1024**1,"KiB"),
(1024**2,"MiB"),
(1024**3,"GiB"),
(1024**4,"TiB"),
(1024**5,"PiB"))
suffix = filter(lambda x:size >=x[0],suffix)
if suffix:
suffix = suffix[-1]
return "%.1f %s"%(size/suffix[0],suffix[1])
return "0"
def getDeviceType(syspath):
"""Get device type (disk,partition,lvm,raid)"""
prop = getUdevDeviceInfo(path=syspath)
# real device
if prop.get('ID_CDROM',""):
return "cdrom"
if prop.get('ID_TYPE',"") == "disk" and \
prop.get('DEVTYPE',"") == "disk":
return "disk"
# partition
if prop.get('DEVTYPE') == "partition":
return getDeviceType(path.dirname(syspath))+"-partition"
# md raid
if prop.get('MD_LEVEL',"").startswith("raid"):
if not syspath.startswith('/sys'):
syspath = pathJoin('/sys',syspath)
syspath = pathJoin(syspath,"md")
for rd in filter(lambda x:path.basename(x).startswith('rd'),
listDirectory(syspath,fullPath=True)):
rdBlockPath = path.join(rd,"block")
if path.exists(rdBlockPath):
return getDeviceType(path.realpath(rdBlockPath))+"-raid"
else:
return "loop"
# lvm
if prop.get('DM_LV_NAME',"") != "":
parts = getLvmPartitions(prop.get('DM_VG_NAME',''),
prop.get('DM_LV_NAME',''))
if parts:
propPartLvm = getUdevDeviceInfo(name=parts[0])
if 'DEVPATH' in propPartLvm:
return getDeviceType(propPartLvm['DEVPATH'])+"-lvm"
return "loop"
def getRaidPartitions(raidpath):
"""Get raid partitions"""
prop = getUdevDeviceInfo(path=raidpath)
raidParts = []
if prop.get('MD_LEVEL',"").startswith("raid"):
if not raidpath.startswith('/sys'):
raidpath = pathJoin('/sys',raidpath)
raidpath = pathJoin(raidpath,"md")
for rd in filter(lambda x:path.basename(x).startswith('rd'),
listDirectory(raidpath,fullPath=True)):
rdpath = path.join(raidpath,rd,"block")
if path.exists(rdpath):
raidParts.append(
getUdevDeviceInfo(path=path.realpath(rdpath)).get(
"DEVNAME",''))
return filter(lambda x:x,raidParts)
def getPartitionType(prop):
"""Get type of dos part table (primary,extended or logical)"""
if prop.get('ID_PART_TABLE_TYPE') == 'dos':
partId = prop.get('ID_PART_ENTRY_TYPE','')
partNumber = prop.get('ID_PART_ENTRY_NUMBER','')
if partId and partNumber:
if partId == "0x5":
return "extended"
elif int(partNumber)>4:
return "logical"
else:
return "primary"
return prop.get('ID_PART_TABLE_TYPE','')
def detectBuild(pathname,dictInfo):
"""Detect build by root passwd 'root'"""
shadowPath = pathJoin(pathname,'/etc/shadow')
if r"root:$1$JMvNh5xg$VnV1DyJdTcwuZ0hp5YiJG0:14349:0:::::" in \
readFile(shadowPath):
dictInfo['type'] = ' assemble'
elif path.exists(pathJoin(pathname,"delta")) and \
path.exists(pathJoin(pathname,"workspace")):
dictInfo['type'] = " builder"
issue = readFile(pathJoin(pathname,'etc/gentoo-release'))
if "Server" in issue:
if "Scratch" in issue:
dictInfo['name'] = "CSS"
else:
dictInfo['name'] = "CDS"
elif "Desktop" in issue:
if "XFCE" in issue:
dictInfo['name'] = "CLDX"
elif "KDE" in issue:
dictInfo['name'] = "CLD"
elif "GNOME" in issue:
dictInfo['name'] = "CLDG"
elif "Scratch" in issue:
dictInfo['name'] = "CLS"
else:
dictInfo['type'] = ''
return dictInfo
def getOsProberHash(getContentFunc=None):
"""Get partition content by os-prober"""
os_prober = getProgPath('/usr/bin/os-prober')
if os_prober:
DEV,LONG,SHORT,TYPE = 0,1,2,3
osProberList = \
map(lambda x:[getUdevDeviceInfo(name=x[DEV]).get('DEVNAME',''),
x[LONG],x[SHORT],x[TYPE]],
filter(lambda x:len(x)>=4,
map(lambda x:x.split(":"),
process(os_prober))))
for osRecord in osProberList:
if "Gentoo" in osRecord[SHORT] and getContentFunc:
osDescr = getContentFunc(osRecord[DEV],addFunc=detectBuild)
if "name" in osDescr and "march" in osDescr and \
"build" in osDescr and "ver" in osDescr and \
(osDescr["ver"] != "0" or osDescr["build"]):
if osDescr['build']:
osDescr['build'] = "-%s"%osDescr['build']
else:
osDescr['build'] = "-%s"%osDescr['ver']
osRecord[SHORT] = \
"{name}-{march}{build}{type}".format(**osDescr)
else:
osRecord[SHORT] = "Gentoo"
elif "Gentoo" in osRecord[SHORT] and "Calculate" in osRecord[LONG]:
osRecord[SHORT] = "Calculate"
osProberHash = \
dict(
map(lambda x:(x[DEV],x[SHORT]),
osProberList))
else:
osProberHash = {}
return osProberHash

Loading…
Cancel
Save