def refreshUdev(): """Run command which refresh information about device in udev""" getUdevDeviceInfo.clearCache() udevadm = getProgPath('/sbin/udevadm') if udevadm: blkidFile = '/etc/blkid.tab' try: if path.exists(blkidFile): os.unlink(blkidFile) except: pass process(udevadm,"trigger","--subsystem-match","block").success()
def refreshLVM(): """Run command which refresh information about LVM""" vgscan = getProgPath('/sbin/vgscan') vgchange = getProgPath('/sbin/vgchange') lvchange = getProgPath('/sbin/lvchange') if vgscan and vgchange and lvchange: process(vgscan).success() process(vgchange,'-ay').success() process(vgchange,'--refresh').success() for group in getLvmGroups(): process(lvchange,'-ay',group).success() process(lvchange,'--refresh',group).success()
def __call__(self,path="",name=""): """Get device info by syspath of name""" typeQuery = "--name" if name else "--path" value = name or os.path.realpath(path) keyCache = "%s=%s"%(typeQuery,value) if not keyCache in self.cache: if not self.udevadmCmd: self.udevadmCmd = getProgPath('/sbin/udevadm') data = \ dict( filter(lambda x:x[0], map(lambda x:x.partition("=")[0::2], process(self.udevadmCmd,"info","--query","property", typeQuery,value).read().split("\n")))) tm = time.time() keyNameCache = data.get('DEVNAME','') keyPathCache = data.get('DEVPATH','') if keyNameCache: keyNameCache = "--name="+keyNameCache if keyPathCache: keyPathCache = "--path=/sys"+keyPathCache if "DEVNAME" in data and not keyNameCache in self.cache: self.cache[keyNameCache] = data if "DEVPATH" in data and not keyPathCache in self.cache: self.cache[keyPathCache] = data return data else: #print "fromCache",keyCache return self.cache[keyCache]
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
def lspci(filtername=None,shortInfo=False): """Get hash of lspci, filtred by filtername. If shortInfo, then type,vendor and name get only first word pcidata(domain,bus,slot,func) 'type' 'vendor' 'name'""" reData = re.compile(r'(\S+)\s"([^"]+)"\s+"([^"]+)"\s+"([^"]+)"',re.S) if filtername: if hasattr(filtername,'__call__'): filterfunc = filtername else: filterfunc = lambda x: filtername in x else: filterfunc = lambda x:x if shortInfo: sfunc = lambda x:x.partition(" ")[0] else: sfunc = lambda x:x lspciProg = checkUtils('/usr/sbin/lspci') processLsPci = process(lspciProg,"-m") retData = {} for device in map(lambda x:x.groups(), filter(lambda x:x, map(reData.search, filter(filterfunc, processLsPci)))): retData[device[0]] = {'type':sfunc(device[1]),\ 'vendor':sfunc(device[2]),\ 'name':sfunc(device[3])} return retData
def getIpAndMask(interface="eth0"): """Get ip and mask from interface""" ifconfig = process('/sbin/ifconfig',interface) res = re.search(r"inet addr:(\S+)\s.*Mask:(\S+)",ifconfig.read(),re.S) if res: return res.groups() else: return ("","")
def getKernelUid(device): """Get Kernel UID by UUID of device""" blkidProcess = process('/sbin/blkid','-c','/dev/null','-s','UUID', '-o','value',device) res = blkidProcess.read().strip() if res: return res[:8] else: return device.replace("/","")
def getRouteTable(onlyIface=[]): """Get route table, exclude specifed iface""" ipProg = checkUtils('/sbin/ip') routes = process(ipProg,"route") if onlyIface: filterRe = re.compile("|".join(map(lambda x:r"dev %s"%x,onlyIface))) routes = filter(filterRe.search,routes) for line in routes: network,op,line = line.partition(" ") routeParams = map(lambda x:x.strip(),line.split()) # (network,{'via':value,'dev':value}) if network: yield (network,dict(zip(routeParams[0::2],routeParams[1::2])))
def getUUIDDict(revers=False): """Get dict UUID -> dev""" blkidProcess = process("/sbin/blkid","-s","UUID","-c","/dev/null") if revers: datafunc = lambda x,y: (y,x) else: datafunc = lambda x,y: (x,y) DEV,UUID = 0,1 reSplit = re.compile('^([^:]+):.*UUID="([^"]+)"',re.S) return dict( map(lambda x:datafunc("UUID=%s"%x[UUID], getUdevDeviceInfo(name=x[DEV]).get('DEVNAME',x[DEV])), map(lambda x:x.groups(), filter(lambda x:x, map(reSplit.search, blkidProcess)))))
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 getLvmGroups(): """Get LVM groups""" pvdisplayCmd = getProgPath('/sbin/pvdisplay') pvdata = process(pvdisplayCmd,"-C","-o", "vg_name","--noh") return filter(lambda x:x,pvdata.read().split())