Skip to contentSkip to conversation

Fix syntax problem of vscsi_util

PATCH In review Masaki Kanno 2 participants 8 messages Oct 21, 2008 – Oct 22, 2008

lore
Share & tools
lore
#0 lore raw

cover Fix syntax problem of vscsi_util

Hi,

These patches fix the syntax problem reported from Ian. Also they use os.path.realpath() instead of os.chdir() and os.getcwd(). And they clean up vscsi_util.py and related other files.

I wrote them on xen-unstable staging tree.
1/2 : Revert changeset 18646.
2/2 : Fix and clean up vscsi_util.py and other files.

Best regards,
Kan

#1 lore raw

PATCH 1/2 Revert changeset 18646

Revert changeset 18646

tools/python/xen/util/vscsi_util.py +0 -4
diff -r 1eba0c88655f tools/python/xen/util/vscsi_util.py
@@ -93,10 +93,6 @@ def vscsi_get_scsidevices():
 def vscsi_get_scsidevices():
     """ get all scsi devices"""
 
-    # KAF: Stubbed out for now due to bogus use of os.chdir() and because
-    # the devices.append() line can fail due to sg and scsi_id not defined.
-    return []
-
     devices = []
     sysfs_mnt = utils.find_sysfs_mount() 
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#2 lore raw

PATCH 2/2 Fix and clean up vscsi_util.py and other files

In reply to Masaki Kanno

Fix and clean up vscsi_util.py and other files

tools/python/xen/util/vscsi_util.py +42 -64
diff -r ae30f00e813a tools/python/xen/util/vscsi_util.py
@@ -36,28 +36,27 @@ SYSFS_SCSI_DEV_REVISION_PATH = '/rev'
 SYSFS_SCSI_DEV_REVISION_PATH = '/rev'
 SYSFS_SCSI_DEV_SCSILEVEL_PATH = '/scsi_level'
 
-def _vscsi_hctl_block(name, scsi_devices):
-    """ block-device name is convert into hctl. (e.g., '/dev/sda',
-    '0:0:0:0')"""
+def _vscsi_get_devname_by(name, scsi_devices):
+    """A device name is gotten by the HCTL.
+    (e.g., '0:0:0:0' to '/dev/sda')
+    """
+
     try:
         search = re.compile(r'' + name + '$', re.DOTALL)
     except Exception, e:
         raise VmError("vscsi: invalid expression. " + str(e))
-    chk = 0
-    for hctl, block, sg, scsi_id in scsi_devices:
+
+    for hctl, devname, sg, scsi_id in scsi_devices:
         if search.match(hctl):
-            chk = 1
-            break
-
-    if chk:
-        return (hctl, block)
-    else:
-        return (None, None)
-
-
-def _vscsi_block_scsiid_to_hctl(phyname, scsi_devices):
-    """ block-device name is convert into hctl. (e.g., '/dev/sda',
-    '0:0:0:0')"""
+            return (hctl, devname)
+
+    return (None, None)
+
+
+def _vscsi_get_hctl_by(phyname, scsi_devices):
+    """An HCTL is gotten by the device name or the scsi_id.
+    (e.g., '/dev/sda' to '0:0:0:0')
+    """
     
     if re.match('/dev/sd[a-z]+([1-9]|1[0-5])?$', phyname):
         # sd driver
@@ -72,22 +71,11 @@ def _vscsi_block_scsiid_to_hctl(phyname,
         # scsi_id -gu
         name = phyname
 
-    chk = 0
-    for hctl, block, sg, scsi_id in scsi_devices:
-        if block == name:
-            chk = 1
-            break
-        elif sg == name:
-            chk = 1
-            break
-        elif scsi_id == name:
-            chk = 1
-            break
-
-    if chk:
-        return (hctl, block)
-    else:
-        return (None, None)
+    for hctl, devname, sg, scsi_id in scsi_devices:
+        if name in [devname, sg, scsi_id]:
+            return (hctl, devname)
+
+    return (None, None)
 
 
 def vscsi_get_scsidevices():
@@ -99,46 +87,36 @@ def vscsi_get_scsidevices():
     for dirpath, dirnames, files in os.walk(sysfs_mnt + SYSFS_SCSI_PATH):
         for hctl in dirnames:
             paths = os.path.join(dirpath, hctl)
-            block = "-"
+            devname = None
+            sg = None
+            scsi_id = None
             for f in os.listdir(paths):
-                if re.match('^block', f):
-                    os.chdir(os.path.join(paths, f))
-                    block = os.path.basename(os.getcwd())
-                elif re.match('^tape', f):
-                    os.chdir(os.path.join(paths, f))
-                    block = os.path.basename(os.getcwd())
-                elif re.match('^scsi_changer', f):
-                    os.chdir(os.path.join(paths, f))
-                    block = os.path.basename(os.getcwd())
-                elif re.match('^onstream_tape', f):
-                    os.chdir(os.path.join(paths, f))
-                    block = os.path.basename(os.getcwd())
+                realpath = os.path.realpath(os.path.join(paths, f))
+                if  re.match('^block', f) or \
+                    re.match('^tape', f) or \
+                    re.match('^scsi_changer', f) or \
+                    re.match('^onstream_tape', f):
+                    devname = os.path.basename(realpath)
 
                 if re.match('^scsi_generic', f):
-                    os.chdir(os.path.join(paths, f))
-                    sg = os.path.basename(os.getcwd())
+                    sg = os.path.basename(realpath)
                     lines = os.popen('/sbin/scsi_id -gu -s /class/scsi_generic/' + sg).read().split()
-                    if len(lines) == 0:
-                        scsi_id = '-'
-                    else:
+                    if len(lines):
                         scsi_id = lines[0]
 
-            devices.append([hctl, block, sg, scsi_id])
+            devices.append([hctl, devname, sg, scsi_id])
 
     return devices
 
 
-def vscsi_search_hctl_and_block(device):
-
-    scsi_devices = vscsi_get_scsidevices()
-
-    tmp = device.split(':')
-    if len(tmp) == 4:
-        (hctl, block) = _vscsi_hctl_block(device, scsi_devices)
+def vscsi_get_hctl_and_devname_by(target, scsi_devices = None):
+    if scsi_devices is None:
+        scsi_devices = vscsi_get_scsidevices()
+
+    if len(target.split(':')) == 4:
+        return _vscsi_get_devname_by(target, scsi_devices)
     else:
-        (hctl, block) = _vscsi_block_scsiid_to_hctl(device, scsi_devices)
-
-    return (hctl, block)
+        return _vscsi_get_hctl_by(target, scsi_devices)
 
 
 def get_scsi_vendor(pHCTL):
@@ -212,9 +190,9 @@ def get_all_scsi_devices():
             'sg_name': scsi_info[2],
             'scsi_id': None
         }
-        if scsi_info[1] != '-':
+        if scsi_info[1] is not None:
             scsi_dev['dev_name'] = scsi_info[1] 
-        if scsi_info[3] != '-':
+        if scsi_info[3] is not None:
             scsi_dev['scsi_id'] = scsi_info[3] 
 
         scsi_dev['vendor_name'] = \
tools/python/xen/xm/create.py +3 -6
diff -r ae30f00e813a tools/python/xen/xm/create.py
@@ -703,11 +703,8 @@ def configure_vscsis(config_devs, vals):
 
     scsi_devices = vscsi_util.vscsi_get_scsidevices()
     for (p_dev, v_dev, backend) in vals.vscsi:
-        tmp = p_dev.split(':')
-        if len(tmp) == 4:
-            (p_hctl, block) = vscsi_util._vscsi_hctl_block(p_dev, scsi_devices)
-        else:
-            (p_hctl, block) = vscsi_util._vscsi_block_scsiid_to_hctl(p_dev, scsi_devices)
+        (p_hctl, devname) = \
+            vscsi_util.vscsi_get_hctl_and_devname_by(p_dev, scsi_devices)
 
         if p_hctl == None:
             raise ValueError("Cannot find device \"%s\"" % p_dev)
@@ -723,7 +720,7 @@ def configure_vscsis(config_devs, vals):
                         ['state', 'Initialising'], \
                         ['devid', devid], \
                         ['p-dev', p_hctl], \
-                        ['p-devname', block], \
+                        ['p-devname', devname], \
                         ['v-dev', v_dev] ])
 
         if vscsi_lookup_devid(devidlist, devid) == 0:
tools/python/xen/xm/main.py +6 -6
diff -r ae30f00e813a tools/python/xen/xm/main.py
@@ -2491,20 +2491,20 @@ def parse_scsi_configuration(p_scsi, v_h
     if len(v) != 4:
         raise OptionError("Invalid argument: %s" % v_hctl)
 
+    p_hctl = None
+    devname = None
     if p_scsi is not None:
-        (p_hctl, block) = vscsi_util.vscsi_search_hctl_and_block(p_scsi)
-        if p_hctl == None:
+        (p_hctl, devname) = \
+            vscsi_util.vscsi_get_hctl_and_devname_by(p_scsi)
+        if p_hctl is None:
             raise OptionError("Cannot find device '%s'" % p_scsi)
-    else:
-        p_hctl = ''
-        block = ''
 
     scsi = ['vscsi']
     scsi.append(['dev', \
                  ['state', state], \
                  ['devid', int(v[0])], \
                  ['p-dev', p_hctl], \
-                 ['p-devname', block], \
+                 ['p-devname', devname], \
                  ['v-dev', v_hctl] \
                ])
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

Acked-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] Fix and clean up vscsi_util.py and other files

Masaki Kanno writes ("[PATCH 2/2] Fix and clean up vscsi_util.py and other files"):

Quoted 2 lines

Fix and clean up vscsi_util.py and other files

Thanks. This looks like a definite improvement. I think it should make the vscsi stuff more or less harmless in situations where it's not being used.

So:

However, are you sure that the approach taken in vscsi_util.py is the right one ? The set of scsi devices available on a system may change over time, so it is not correct to try to build a list at startup. Also, doing so will slow down startup (including startup time of the xm utility) on systems with many scsi devices even when pvscsi is not in use, and exposes non-pvscsi users to bugs in the pvscsi system (as previously discussed).

Surely there must be some way to do this lookup directly in /sys or whereever at the point of use, when we're instructed to pass through a specific device ?

Ian.

#4 lore raw

Re: Re: [PATCH 2/2] Fix and clean up vscsi_util.py andother files

Hi Ian,

Quoted 11 lines · Ian Jackson

Masaki Kanno writes ("[PATCH 2/2] Fix and clean up vscsi_util.py and other files"):

Quoted 2 lines

Fix and clean up vscsi_util.py and other files

Thanks. This looks like a definite improvement. I think it should make the vscsi stuff more or less harmless in situations where it's not being used.

So:

Thanks.

Quoted 3 lines

However, are you sure that the approach taken in vscsi_util.py is the right one ? The set of scsi devices available on a system may change over time, so it is not correct to try to build a list at startup.

"startup" which you pointed out is xm create, isn't it? Or xend start?

If xm create, I think that the xm utility does not affect non-pvscsi users. Non-pvscsi users return at line 702, so the xm utility is not called by xm create.

696 def configure_vscsis(config_devs, vals):

697     """Create the config for vscsis (virtual scsi devices).
698     """
699     devidlist = []
700     config_scsi = []
701     if len(vals.vscsi) == 0:

702 return 0
703
704 scsi_devices = vscsi_util.vscsi_get_scsidevices()
705 for (p_dev, v_dev, backend) in vals.vscsi:
706 (p_hctl, devname) = \
707 vscsi_util.vscsi_get_hctl_and_devname_by(p_dev, scsi_devices)

Best regards,
Kan

Quoted 15 lines

Also, doing so will slow down startup (including startup time of the xm utility) on systems with many scsi devices even when pvscsi is not in use, and exposes non-pvscsi users to bugs in the pvscsi system (as previously discussed).

Surely there must be some way to do this lookup directly in /sys or whereever at the point of use, when we're instructed to pass through a specific device ?

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#5 lore raw

Re: Re: [PATCH 2/2] Fix and clean up vscsi_util.py andother files

Masaki Kanno writes ("Re: [Xen-devel] Re: [PATCH 2/2] Fix and clean up vscsi_util.py andother files"):

Quoted 2 lines

"startup" which you pointed out is xm create, isn't it? Or xend start?

The code seems to be run in both contexts, but I only checked with some grepping rather than actual execution.

Quoted 3 lines

If xm create, I think that the xm utility does not affect non-pvscsi users. Non-pvscsi users return at line 702, so the xm utility is not called by xm create.

Ah, yes.

But don't you still need to think about the possibility that devices may appear and disappear (or even both!) during command execution or during the lifetime of xend ?

Ian.

#6 lore raw

Re: Re: [PATCH 2/2] Fix and clean up vscsi_util.pyandother files

Quoted 12 lines · Ian Jackson

Masaki Kanno writes ("Re: [Xen-devel] Re: [PATCH 2/2] Fix and clean up vscsi_util.py andother files"):

Quoted 2 lines

"startup" which you pointed out is xm create, isn't it? Or xend start?

The code seems to be run in both contexts, but I only checked with some grepping rather than actual execution.

Quoted 3 lines

If xm create, I think that the xm utility does not affect non-pvscsi users. Non-pvscsi users return at line 702, so the xm utility is not called by xm create.

Quoted 5 lines

Ah, yes.

But don't you still need to think about the possibility that devices may appear and disappear (or even both!) during command execution or during the lifetime of xend ?

Hi Ian,

Yes, I also consider more consideration may be needed. Could you give us (me, Kamada and Horikoshi) time to consider?

Best regards,
Kan

#7 lore raw

Re: Re: [PATCH 2/2] Fix and clean up vscsi_util.pyandother files

Masaki Kanno writes ("Re: [Xen-devel] Re: [PATCH 2/2] Fix and clean up vscsi_util.pyandother files"):

Quoted 7 lines
Quoted 3 lines · Ian Jackson

But don't you still need to think about the possibility that devices may appear and disappear (or even both!) during command execution or during the lifetime of xend ?

Yes, I also consider more consideration may be needed. Could you give us (me, Kamada and Horikoshi) time to consider?

Of course :-).

Regards,
Ian.