ia64/xen-unstable

changeset 19222:34812acece03

xend: Improve information-gathering processing of SCSI devices by using lsscsi

In the case of xm scsi-attach, when the SCSI devices are specified in
HCTL form or device name form, the processing is faster.

If lsscsi command is not installed, the processing works by using
information of /sys as before.

Signed-off-by: Masaki Kanno <kanno.masaki@jp.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Feb 17 11:12:50 2009 +0000 (2009-02-17)
parents f87d008bd011
children 634b425cafa3
files tools/python/xen/util/vscsi_util.py
line diff
     1.1 --- a/tools/python/xen/util/vscsi_util.py	Tue Feb 17 11:11:17 2009 +0000
     1.2 +++ b/tools/python/xen/util/vscsi_util.py	Tue Feb 17 11:12:50 2009 +0000
     1.3 @@ -78,8 +78,38 @@ def _vscsi_get_hctl_by(phyname, scsi_dev
     1.4      return (None, None)
     1.5  
     1.6  
     1.7 -def vscsi_get_scsidevices():
     1.8 -    """ get all scsi devices"""
     1.9 +def _vscsi_get_scsiid(sg):
    1.10 +    scsi_id = os.popen('/sbin/scsi_id -gu -s /class/scsi_generic/' + sg).read().split()
    1.11 +    if len(scsi_id):
    1.12 +        return scsi_id[0]
    1.13 +    return None
    1.14 +
    1.15 +
    1.16 +def _vscsi_get_scsidevices_by_lsscsi(option = ""):
    1.17 +    """ get all scsi devices information by lsscsi """
    1.18 +
    1.19 +    devices = []
    1.20 +
    1.21 +    for scsiinfo in os.popen('lsscsi -g %s' % option).readlines():
    1.22 +        s = scsiinfo.split()
    1.23 +        hctl = s[0][1:-1]
    1.24 +        try:
    1.25 +            devname = s[-2].split('/dev/')[1]
    1.26 +        except IndexError:
    1.27 +            devname = None
    1.28 +        try:
    1.29 +            sg = s[-1].split('/dev/')[1]
    1.30 +            scsi_id = _vscsi_get_scsiid(sg)
    1.31 +        except IndexError:
    1.32 +            sg = None
    1.33 +            scsi_id = None
    1.34 +        devices.append([hctl, devname, sg, scsi_id])
    1.35 +
    1.36 +    return devices
    1.37 +
    1.38 +
    1.39 +def _vscsi_get_scsidevices_by_sysfs():
    1.40 +    """ get all scsi devices information by sysfs """
    1.41  
    1.42      devices = []
    1.43      sysfs_mnt = utils.find_sysfs_mount() 
    1.44 @@ -100,18 +130,29 @@ def vscsi_get_scsidevices():
    1.45  
    1.46                  if re.match('^scsi_generic', f):
    1.47                      sg = os.path.basename(realpath)
    1.48 -                    lines = os.popen('/sbin/scsi_id -gu -s /class/scsi_generic/' + sg).read().split()
    1.49 -                    if len(lines):
    1.50 -                        scsi_id = lines[0]
    1.51 -
    1.52 +                    scsi_id = _vscsi_get_scsiid(sg)
    1.53              devices.append([hctl, devname, sg, scsi_id])
    1.54  
    1.55      return devices
    1.56  
    1.57  
    1.58 +def vscsi_get_scsidevices():
    1.59 +    """ get all scsi devices information """
    1.60 +
    1.61 +    devices = _vscsi_get_scsidevices_by_lsscsi("")
    1.62 +    if devices:
    1.63 +        return devices
    1.64 +    return _vscsi_get_scsidevices_by_sysfs()
    1.65 +
    1.66 +
    1.67  def vscsi_get_hctl_and_devname_by(target, scsi_devices = None):
    1.68      if scsi_devices is None:
    1.69 -        scsi_devices = vscsi_get_scsidevices()
    1.70 +        if len(target.split(':')) == 4:
    1.71 +            scsi_devices = _vscsi_get_scsidevices_by_lsscsi(target)
    1.72 +        elif target.startswith('/dev/'): 
    1.73 +            scsi_devices = _vscsi_get_scsidevices_by_lsscsi("| grep %s" % target)
    1.74 +        else:
    1.75 +            scsi_devices = vscsi_get_scsidevices()
    1.76  
    1.77      if len(target.split(':')) == 4:
    1.78          return _vscsi_get_devname_by(target, scsi_devices)