def getTableByChild(self,device):
     """Get table by child partitions"""
     syspath = getUdevDeviceInfo(name=device).get('DEVPATH','')
     if not syspath.startswith('/sys'):
         syspath = pathJoin('/sys',syspath)
     shortnameDevice = path.basename(device)
     childs = filter(lambda x:x.startswith(shortnameDevice),
              listDirectory(syspath))
     if childs:
         child = pathJoin(syspath,childs[0])
         return getUdevDeviceInfo(path=child).get('ID_PART_ENTRY_SCHEME','')
     return ""
    def get_os_device_hash(self):
        """Generate hash information about device"""
        def onlyDisk(devpath):
            """Check by udevadm that devpath is device (disk)"""
            prop = getUdevDeviceInfo(devpath)
            return prop.get("ID_TYPE","")=="disk" and \
                   prop.get("DEVTYPE","")=="disk"

        # get usb device by '/dev/disk/by-id'(usb devices contain 'usb' in name)
        diskIdPath = '/dev/disk/by-id'
        if path.exists(diskIdPath):
            usbdevices = \
             map(lambda x: readlink(path.join(diskIdPath,x)).rpartition('/')[2],
             filter(lambda x: x.startswith('usb-'),listDirectory(diskIdPath)))
        else:
            usbdevices = []
        # get devices from /sys/block directories(discard mem,sr,loop and other)
        sysBlockPath = '/sys/block'
        devices = map(lambda x:path.join(sysBlockPath,x),
                  filter(lambda x: onlyDisk(path.join(sysBlockPath,x)),
                  filter(lambda x: not self.reWrongDevice.search(x),
                  listDirectory(sysBlockPath))))
        device_hash = {}
        # filling hash
        for mapnum,device in enumerate(sorted(devices,key=self.separateDevice)):
            # get information by udev
            props = getUdevDeviceInfo(device)
            if not "DEVNAME" in props:
                continue
            # DEVNAME - /dev/(device_name)
            device = props['DEVNAME']
            device_hash[device] = {}
            # get partition table
            # (if PART_TABLE_TYPE absent then get by child partition)
            device_hash[device]['table'] = props.get('ID_PART_TABLE_TYPE',
                                           self.getTableByChild(device))
            # enumerate disk for legecy grub
            device_hash[device]['map'] = mapnum
            # if device is usb device
            if path.basename(device) in usbdevices:
                # check for usb flash (removeable fiel in sysfs contains "1")
                removablePath = '/sys/block/%s/removable'%path.basename(device)
                if os.access(removablePath,R_OK) and \
                    open(removablePath,'r').read().strip() == "1":
                    devtype = "flash" 
                else:
                    devtype = "usb-hdd" 
            else:
                devtype = "hdd" 
            # set detect device type (hdd,flash or usb-hdd)
            device_hash[device]['type'] = devtype
        return device_hash
Example #3
0
 def get_os_root_dev(self):
     """Root filesystem device"""
     record = open('/proc/cmdline','rb').read().strip()
     re_resRealRoot=re.search('(?:^|\s)real_root=(\S+)(\s|$)',record)
     re_resFakeRoot=re.search('(?:^|\s)root=(\S+)(\s|$)',record)
     # param real_root priority that root
     re_res = re_resRealRoot or re_resFakeRoot
     if re_res:
         rootparam=re_res.group(1)
         # check root for /dev/sd view
         if re.match("^\/dev\/[a-z]+.*$", rootparam):
             return getUdevDeviceInfo(
                name=rootparam.strip()).get('DEVNAME',rootparam)
         # check root set by uuid
         if re.match("^UUID=.*$",rootparam):
             uuid = rootparam[5:].strip("\"'")
             blkidProcess = process('/sbin/blkid','-c','/dev/null','-U',
                                 uuid)
             if blkidProcess.success():
                 return getUdevDeviceInfo(
                    name=blkidProcess.read().strip()).get('DEVNAME','')
         # check root set by label
         if re.match("^LABEL=.*$",rootparam):
             uuid = rootparam[6:].strip("\"'")
             blkidProcess = process('/sbin/blkid','-c','/dev/null','-L',
                                 uuid)
             if blkidProcess.success():
                 return getUdevDeviceInfo(
                    name=blkidProcess.read().strip()).get('DEVNAME','')
     # get device mounted to root
     dfLines = self._runos("LANG=C df /")
     if not dfLines:
         return ""
     if type(dfLines) == types.ListType and len(dfLines)>1:
         root_dev = dfLines[1].split(" ")[0].strip()
         if root_dev:
             return {'none':'/dev/ram0'}.get(root_dev,root_dev)
     return ""
 def onlyDisk(devpath):
     """Check by udevadm that devpath is device (disk)"""
     prop = getUdevDeviceInfo(devpath)
     return prop.get("ID_TYPE","")=="disk" and \
            prop.get("DEVTYPE","")=="disk"