def lookup_disk(serial): """ Search for a disk by its serial number using /dev/disk/by-id/ """ # Get all volumes in /dev/disk/by-id/ containing the serial string. The # string specified can be either in the short or long serial format # hack, some serials have spaces, udev usually converts ' ' -> '_' serial_udev = serial.replace(' ', '_') LOG.info('Processing serial %s via udev to %s', serial, serial_udev) disks = list( filter(lambda x: serial_udev in x, os.listdir("/dev/disk/by-id/"))) if not disks or len(disks) < 1: raise ValueError("no disk with serial '%s' found" % serial_udev) # Sort by length and take the shortest path name, as the longer path names # will be the partitions on the disk. Then use os.path.realpath to # determine the path to the block device in /dev/ disks.sort(key=lambda x: len(x)) LOG.debug('lookup_disks found: %s', disks) path = os.path.realpath("/dev/disk/by-id/%s" % disks[0]) # /dev/dm-X if multipath.is_mpath_device(path): info = udevadm_info(path) path = os.path.join('/dev/mapper', info['DM_NAME']) # /dev/sdX elif multipath.is_mpath_member(path): mp_name = multipath.find_mpath_id_by_path(path) path = os.path.join('/dev/mapper', mp_name) if not os.path.exists(path): raise ValueError("path '%s' to block device for disk with serial '%s' \ does not exist" % (path, serial_udev)) LOG.debug('block.lookup_disk() returning path %s', path) return path
def test_find_mpath_id_by_path(self): """find_mpath_id_by_path returns the mp_id if specified device is member. """ mp_id = 'mpatha' paths = ['device=bar multipath=mpatha', 'device=wark multipath=mpathb'] self.m_subp.return_value = ("\n".join(paths), "") self.assertEqual(mp_id, multipath.find_mpath_id_by_path('/dev/bar'))
def test_find_mpath_id_by_path_returns_none_not_found(self): """find_mpath_id_by_path returns None if specified device is not a member. """ mp_id = 'mpatha' paths = [ 'device=bar multipath=%s' % mp_id, 'device=wark multipath=%s' % mp_id ] self.m_subp.return_value = ("\n".join(paths), "") self.assertIsNone(multipath.find_mpath_id_by_path('/dev/xxx'))