Example #1
0
    def BlockDevices(self, device=''):
        """Collects information about block devices.

    This method combines information from:
      (1) Reading through the SYSTEMFILE_DEV_DISKBY directories.
      (2) Executing the 'lsblk' command provided by osutils.ListBlockDevices.

    Returns:
      A list of named tuples. Each tuple has the following fields:
        device: The name of the block device.
        size: The size of the block device in bytes.
        ids: A list of ids assigned to this device.
        labels: A list of labels assigned to this device.
    """
        devicefilter = os.path.basename(device)

        # Data collected from the SYSTEMFILE_DEV_DISKBY directories.
        ids = {}
        labels = {}

        # Data collected from 'lsblk'.
        sizes = {}

        # Collect diskby information.
        for prop, diskdir in SYSTEMFILE_DEV_DISKBY.iteritems():
            cmd = ['find', diskdir, '-lname', '*%s' % devicefilter]
            cmd_result = cros_build_lib.RunCommand(cmd, log_output=True)

            if not cmd_result.output:
                continue

            results = cmd_result.output.split()
            for result in results:
                devicename = os.path.abspath(osutils.ResolveSymlink(result))
                result = os.path.basename(result)

                # Ensure that each of our data dicts have the same keys.
                ids.setdefault(devicename, [])
                labels.setdefault(devicename, [])
                sizes.setdefault(devicename, 0)

                if 'ids' == prop:
                    ids[devicename].append(result)
                elif 'labels' == prop:
                    labels[devicename].append(result)

        # Collect lsblk information.
        for device in osutils.ListBlockDevices(in_bytes=True):
            devicename = os.path.join('/dev', device.NAME)
            if devicename in ids:
                sizes[devicename] = int(device.SIZE)

        return [
            RESOURCE_BLOCKDEVICE(device, sizes[device], ids[device],
                                 labels[device]) for device in ids.iterkeys()
        ]
Example #2
0
 def testListBlockDevices(self):
   """Tests that we can list all block devices correctly."""
   self.rc.AddCmdResult(partial_mock.Ignore(), output=self.FULL_OUTPUT)
   devices = osutils.ListBlockDevices()
   self.assertEqual(devices[0].NAME, 'sda')
   self.assertEqual(devices[0].RM, '0')
   self.assertEqual(devices[0].TYPE, 'disk')
   self.assertEqual(devices[0].SIZE, '128G')
   self.assertEqual(devices[3].NAME, 'sdc')
   self.assertEqual(devices[3].RM, '1')
   self.assertEqual(devices[3].TYPE, 'disk')
   self.assertEqual(devices[3].SIZE, '7.4G')
Example #3
0
    def ListAllRemovableDevices(self):
        """Returns a list of removable devices.

    Returns:
      A list of device names (e.g. ['sdb', 'sdc']).
    """
        devices = osutils.ListBlockDevices()
        removable_devices = []
        for d in devices:
            if d.TYPE == 'disk' and d.RM == '1':
                removable_devices.append(d.NAME)

        return removable_devices