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 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 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 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 getDeviceType(syspath=None,name=None): """Get device type (disk,partition,lvm,raid)""" if name: prop = getUdevDeviceInfo(name=name) syspath = prop.get('DEVPATH','') else: 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 getPartitionSize(syspath=None,name=None,inBytes=False): """ Get partition size """ if name: dev = convertNameToSyspath(name) else: dev = syspath SECTORSIZE=512 sizeFile = pathJoin(dev,"size") if path.exists(sizeFile): size = int(open(sizeFile,'r').read().strip())*SECTORSIZE if inBytes: return str(size) else: return humanreadableSize(size) return ""
def convertNameToSyspath(name): dev = getUdevDeviceInfo(name=name).get('DEVPATH',"") if dev and not dev.startswith('/sys'): dev = pathJoin('/sys',dev) return dev