ia64/xen-unstable

changeset 18679:4b5823f292ea

xend: Fix and clean up vscsi_util.py and other files

Signed-off-by: Masaki Kanno <kanno.masaki@jp.fujitsu.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Oct 21 11:43:21 2008 +0100 (2008-10-21)
parents ecdbcd27490f
children 3b99705155c1
files tools/python/xen/util/vscsi_util.py tools/python/xen/xm/create.py tools/python/xen/xm/main.py
line diff
     1.1 --- a/tools/python/xen/util/vscsi_util.py	Tue Oct 21 11:39:57 2008 +0100
     1.2 +++ b/tools/python/xen/util/vscsi_util.py	Tue Oct 21 11:43:21 2008 +0100
     1.3 @@ -36,28 +36,27 @@ SYSFS_SCSI_DEV_TYPEID_PATH = '/type'
     1.4  SYSFS_SCSI_DEV_REVISION_PATH = '/rev'
     1.5  SYSFS_SCSI_DEV_SCSILEVEL_PATH = '/scsi_level'
     1.6  
     1.7 -def _vscsi_hctl_block(name, scsi_devices):
     1.8 -    """ block-device name is convert into hctl. (e.g., '/dev/sda',
     1.9 -    '0:0:0:0')"""
    1.10 +def _vscsi_get_devname_by(name, scsi_devices):
    1.11 +    """A device name is gotten by the HCTL.
    1.12 +    (e.g., '0:0:0:0' to '/dev/sda')
    1.13 +    """
    1.14 +
    1.15      try:
    1.16          search = re.compile(r'' + name + '$', re.DOTALL)
    1.17      except Exception, e:
    1.18          raise VmError("vscsi: invalid expression. " + str(e))
    1.19 -    chk = 0
    1.20 -    for hctl, block, sg, scsi_id in scsi_devices:
    1.21 +
    1.22 +    for hctl, devname, sg, scsi_id in scsi_devices:
    1.23          if search.match(hctl):
    1.24 -            chk = 1
    1.25 -            break
    1.26 +            return (hctl, devname)
    1.27  
    1.28 -    if chk:
    1.29 -        return (hctl, block)
    1.30 -    else:
    1.31 -        return (None, None)
    1.32 +    return (None, None)
    1.33  
    1.34  
    1.35 -def _vscsi_block_scsiid_to_hctl(phyname, scsi_devices):
    1.36 -    """ block-device name is convert into hctl. (e.g., '/dev/sda',
    1.37 -    '0:0:0:0')"""
    1.38 +def _vscsi_get_hctl_by(phyname, scsi_devices):
    1.39 +    """An HCTL is gotten by the device name or the scsi_id.
    1.40 +    (e.g., '/dev/sda' to '0:0:0:0')
    1.41 +    """
    1.42      
    1.43      if re.match('/dev/sd[a-z]+([1-9]|1[0-5])?$', phyname):
    1.44          # sd driver
    1.45 @@ -72,77 +71,52 @@ def _vscsi_block_scsiid_to_hctl(phyname,
    1.46          # scsi_id -gu
    1.47          name = phyname
    1.48  
    1.49 -    chk = 0
    1.50 -    for hctl, block, sg, scsi_id in scsi_devices:
    1.51 -        if block == name:
    1.52 -            chk = 1
    1.53 -            break
    1.54 -        elif sg == name:
    1.55 -            chk = 1
    1.56 -            break
    1.57 -        elif scsi_id == name:
    1.58 -            chk = 1
    1.59 -            break
    1.60 +    for hctl, devname, sg, scsi_id in scsi_devices:
    1.61 +        if name in [devname, sg, scsi_id]:
    1.62 +            return (hctl, devname)
    1.63  
    1.64 -    if chk:
    1.65 -        return (hctl, block)
    1.66 -    else:
    1.67 -        return (None, None)
    1.68 +    return (None, None)
    1.69  
    1.70  
    1.71  def vscsi_get_scsidevices():
    1.72      """ get all scsi devices"""
    1.73  
    1.74 -    # KAF: Stubbed out for now due to bogus use of os.chdir() and because
    1.75 -    # the devices.append() line can fail due to sg and scsi_id not defined.
    1.76 -    return []
    1.77 -
    1.78      devices = []
    1.79      sysfs_mnt = utils.find_sysfs_mount() 
    1.80  
    1.81      for dirpath, dirnames, files in os.walk(sysfs_mnt + SYSFS_SCSI_PATH):
    1.82          for hctl in dirnames:
    1.83              paths = os.path.join(dirpath, hctl)
    1.84 -            block = "-"
    1.85 +            devname = None
    1.86 +            sg = None
    1.87 +            scsi_id = None
    1.88              for f in os.listdir(paths):
    1.89 -                if re.match('^block', f):
    1.90 -                    os.chdir(os.path.join(paths, f))
    1.91 -                    block = os.path.basename(os.getcwd())
    1.92 -                elif re.match('^tape', f):
    1.93 -                    os.chdir(os.path.join(paths, f))
    1.94 -                    block = os.path.basename(os.getcwd())
    1.95 -                elif re.match('^scsi_changer', f):
    1.96 -                    os.chdir(os.path.join(paths, f))
    1.97 -                    block = os.path.basename(os.getcwd())
    1.98 -                elif re.match('^onstream_tape', f):
    1.99 -                    os.chdir(os.path.join(paths, f))
   1.100 -                    block = os.path.basename(os.getcwd())
   1.101 +                realpath = os.path.realpath(os.path.join(paths, f))
   1.102 +                if  re.match('^block', f) or \
   1.103 +                    re.match('^tape', f) or \
   1.104 +                    re.match('^scsi_changer', f) or \
   1.105 +                    re.match('^onstream_tape', f):
   1.106 +                    devname = os.path.basename(realpath)
   1.107  
   1.108                  if re.match('^scsi_generic', f):
   1.109 -                    os.chdir(os.path.join(paths, f))
   1.110 -                    sg = os.path.basename(os.getcwd())
   1.111 +                    sg = os.path.basename(realpath)
   1.112                      lines = os.popen('/sbin/scsi_id -gu -s /class/scsi_generic/' + sg).read().split()
   1.113 -                    if len(lines) == 0:
   1.114 -                        scsi_id = '-'
   1.115 -                    else:
   1.116 +                    if len(lines):
   1.117                          scsi_id = lines[0]
   1.118  
   1.119 -            devices.append([hctl, block, sg, scsi_id])
   1.120 +            devices.append([hctl, devname, sg, scsi_id])
   1.121  
   1.122      return devices
   1.123  
   1.124  
   1.125 -def vscsi_search_hctl_and_block(device):
   1.126 -
   1.127 -    scsi_devices = vscsi_get_scsidevices()
   1.128 +def vscsi_get_hctl_and_devname_by(target, scsi_devices = None):
   1.129 +    if scsi_devices is None:
   1.130 +        scsi_devices = vscsi_get_scsidevices()
   1.131  
   1.132 -    tmp = device.split(':')
   1.133 -    if len(tmp) == 4:
   1.134 -        (hctl, block) = _vscsi_hctl_block(device, scsi_devices)
   1.135 +    if len(target.split(':')) == 4:
   1.136 +        return _vscsi_get_devname_by(target, scsi_devices)
   1.137      else:
   1.138 -        (hctl, block) = _vscsi_block_scsiid_to_hctl(device, scsi_devices)
   1.139 -
   1.140 -    return (hctl, block)
   1.141 +        return _vscsi_get_hctl_by(target, scsi_devices)
   1.142  
   1.143  
   1.144  def get_scsi_vendor(pHCTL):
   1.145 @@ -216,9 +190,9 @@ def get_all_scsi_devices():
   1.146              'sg_name': scsi_info[2],
   1.147              'scsi_id': None
   1.148          }
   1.149 -        if scsi_info[1] != '-':
   1.150 +        if scsi_info[1] is not None:
   1.151              scsi_dev['dev_name'] = scsi_info[1] 
   1.152 -        if scsi_info[3] != '-':
   1.153 +        if scsi_info[3] is not None:
   1.154              scsi_dev['scsi_id'] = scsi_info[3] 
   1.155  
   1.156          scsi_dev['vendor_name'] = \
     2.1 --- a/tools/python/xen/xm/create.py	Tue Oct 21 11:39:57 2008 +0100
     2.2 +++ b/tools/python/xen/xm/create.py	Tue Oct 21 11:43:21 2008 +0100
     2.3 @@ -703,11 +703,8 @@ def configure_vscsis(config_devs, vals):
     2.4  
     2.5      scsi_devices = vscsi_util.vscsi_get_scsidevices()
     2.6      for (p_dev, v_dev, backend) in vals.vscsi:
     2.7 -        tmp = p_dev.split(':')
     2.8 -        if len(tmp) == 4:
     2.9 -            (p_hctl, block) = vscsi_util._vscsi_hctl_block(p_dev, scsi_devices)
    2.10 -        else:
    2.11 -            (p_hctl, block) = vscsi_util._vscsi_block_scsiid_to_hctl(p_dev, scsi_devices)
    2.12 +        (p_hctl, devname) = \
    2.13 +            vscsi_util.vscsi_get_hctl_and_devname_by(p_dev, scsi_devices)
    2.14  
    2.15          if p_hctl == None:
    2.16              raise ValueError("Cannot find device \"%s\"" % p_dev)
    2.17 @@ -723,7 +720,7 @@ def configure_vscsis(config_devs, vals):
    2.18                          ['state', 'Initialising'], \
    2.19                          ['devid', devid], \
    2.20                          ['p-dev', p_hctl], \
    2.21 -                        ['p-devname', block], \
    2.22 +                        ['p-devname', devname], \
    2.23                          ['v-dev', v_dev] ])
    2.24  
    2.25          if vscsi_lookup_devid(devidlist, devid) == 0:
     3.1 --- a/tools/python/xen/xm/main.py	Tue Oct 21 11:39:57 2008 +0100
     3.2 +++ b/tools/python/xen/xm/main.py	Tue Oct 21 11:43:21 2008 +0100
     3.3 @@ -2491,20 +2491,20 @@ def parse_scsi_configuration(p_scsi, v_h
     3.4      if len(v) != 4:
     3.5          raise OptionError("Invalid argument: %s" % v_hctl)
     3.6  
     3.7 +    p_hctl = None
     3.8 +    devname = None
     3.9      if p_scsi is not None:
    3.10 -        (p_hctl, block) = vscsi_util.vscsi_search_hctl_and_block(p_scsi)
    3.11 -        if p_hctl == None:
    3.12 +        (p_hctl, devname) = \
    3.13 +            vscsi_util.vscsi_get_hctl_and_devname_by(p_scsi)
    3.14 +        if p_hctl is None:
    3.15              raise OptionError("Cannot find device '%s'" % p_scsi)
    3.16 -    else:
    3.17 -        p_hctl = ''
    3.18 -        block = ''
    3.19  
    3.20      scsi = ['vscsi']
    3.21      scsi.append(['dev', \
    3.22                   ['state', state], \
    3.23                   ['devid', int(v[0])], \
    3.24                   ['p-dev', p_hctl], \
    3.25 -                 ['p-devname', block], \
    3.26 +                 ['p-devname', devname], \
    3.27                   ['v-dev', v_hctl] \
    3.28                 ])
    3.29