ia64/xen-unstable

view tools/python/xen/util/vscsi_util.py @ 18653:cbc254c59dd0

xend: Stub out vscsi_get_scsidevices() until issues are addressed.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Oct 20 15:05:48 2008 +0100 (2008-10-20)
parents 9404bcb6d32e
children 4b5823f292ea
line source
1 #!/usr/bin/env python
2 # -*- mode: python; -*-
4 #============================================================================
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of version 2.1 of the GNU Lesser General Public
7 # License as published by the Free Software Foundation.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #============================================================================
18 # Copyright (C) 2008 FUJITSU Limited
19 # Based on the blkif.py
20 #============================================================================
23 """Support for VSCSI Devices.
24 """
25 import os
26 import os.path
27 import sys
28 import re
29 import string
30 from xen.util import utils
32 SYSFS_SCSI_PATH = "/bus/scsi/devices"
33 SYSFS_SCSI_DEV_VENDOR_PATH = '/vendor'
34 SYSFS_SCSI_DEV_MODEL_PATH = '/model'
35 SYSFS_SCSI_DEV_TYPEID_PATH = '/type'
36 SYSFS_SCSI_DEV_REVISION_PATH = '/rev'
37 SYSFS_SCSI_DEV_SCSILEVEL_PATH = '/scsi_level'
39 def _vscsi_hctl_block(name, scsi_devices):
40 """ block-device name is convert into hctl. (e.g., '/dev/sda',
41 '0:0:0:0')"""
42 try:
43 search = re.compile(r'' + name + '$', re.DOTALL)
44 except Exception, e:
45 raise VmError("vscsi: invalid expression. " + str(e))
46 chk = 0
47 for hctl, block, sg, scsi_id in scsi_devices:
48 if search.match(hctl):
49 chk = 1
50 break
52 if chk:
53 return (hctl, block)
54 else:
55 return (None, None)
58 def _vscsi_block_scsiid_to_hctl(phyname, scsi_devices):
59 """ block-device name is convert into hctl. (e.g., '/dev/sda',
60 '0:0:0:0')"""
62 if re.match('/dev/sd[a-z]+([1-9]|1[0-5])?$', phyname):
63 # sd driver
64 name = re.sub('(^/dev/)|([1-9]|1[0-5])?$', '', phyname)
65 elif re.match('/dev/sg[0-9]+$', phyname):
66 # sg driver
67 name = re.sub('^/dev/', '', phyname)
68 elif re.match('/dev/st[0-9]+$', phyname):
69 # st driver
70 name = re.sub('^/dev/', '', phyname)
71 else:
72 # scsi_id -gu
73 name = phyname
75 chk = 0
76 for hctl, block, sg, scsi_id in scsi_devices:
77 if block == name:
78 chk = 1
79 break
80 elif sg == name:
81 chk = 1
82 break
83 elif scsi_id == name:
84 chk = 1
85 break
87 if chk:
88 return (hctl, block)
89 else:
90 return (None, None)
93 def vscsi_get_scsidevices():
94 """ get all scsi devices"""
96 # KAF: Stubbed out for now due to bogus use of os.chdir() and because
97 # the devices.append() line can fail due to sg and scsi_id not defined.
98 return []
100 devices = []
101 sysfs_mnt = utils.find_sysfs_mount()
103 for dirpath, dirnames, files in os.walk(sysfs_mnt + SYSFS_SCSI_PATH):
104 for hctl in dirnames:
105 paths = os.path.join(dirpath, hctl)
106 block = "-"
107 for f in os.listdir(paths):
108 if re.match('^block', f):
109 os.chdir(os.path.join(paths, f))
110 block = os.path.basename(os.getcwd())
111 elif re.match('^tape', f):
112 os.chdir(os.path.join(paths, f))
113 block = os.path.basename(os.getcwd())
114 elif re.match('^scsi_changer', f):
115 os.chdir(os.path.join(paths, f))
116 block = os.path.basename(os.getcwd())
117 elif re.match('^onstream_tape', f):
118 os.chdir(os.path.join(paths, f))
119 block = os.path.basename(os.getcwd())
121 if re.match('^scsi_generic', f):
122 os.chdir(os.path.join(paths, f))
123 sg = os.path.basename(os.getcwd())
124 lines = os.popen('/sbin/scsi_id -gu -s /class/scsi_generic/' + sg).read().split()
125 if len(lines) == 0:
126 scsi_id = '-'
127 else:
128 scsi_id = lines[0]
130 devices.append([hctl, block, sg, scsi_id])
132 return devices
135 def vscsi_search_hctl_and_block(device):
137 scsi_devices = vscsi_get_scsidevices()
139 tmp = device.split(':')
140 if len(tmp) == 4:
141 (hctl, block) = _vscsi_hctl_block(device, scsi_devices)
142 else:
143 (hctl, block) = _vscsi_block_scsiid_to_hctl(device, scsi_devices)
145 return (hctl, block)
148 def get_scsi_vendor(pHCTL):
149 try:
150 sysfs_mnt = utils.find_sysfs_mount()
151 sysfs_scsi_dev_path = \
152 os.path.join(sysfs_mnt + SYSFS_SCSI_PATH, pHCTL)
153 scsi_vendor = \
154 os.popen('cat ' + sysfs_scsi_dev_path + \
155 SYSFS_SCSI_DEV_VENDOR_PATH).read()
156 return scsi_vendor.splitlines()[0]
157 except:
158 return None
160 def get_scsi_model(pHCTL):
161 try:
162 sysfs_mnt = utils.find_sysfs_mount()
163 sysfs_scsi_dev_path = \
164 os.path.join(sysfs_mnt + SYSFS_SCSI_PATH, pHCTL)
165 scsi_model = \
166 os.popen('cat ' + sysfs_scsi_dev_path + \
167 SYSFS_SCSI_DEV_MODEL_PATH).read()
168 return scsi_model.splitlines()[0]
169 except:
170 return None
172 def get_scsi_typeid(pHCTL):
173 try:
174 sysfs_mnt = utils.find_sysfs_mount()
175 sysfs_scsi_dev_path = \
176 os.path.join(sysfs_mnt + SYSFS_SCSI_PATH, pHCTL)
177 scsi_typeid = \
178 os.popen('cat ' + sysfs_scsi_dev_path + \
179 SYSFS_SCSI_DEV_TYPEID_PATH).read()
180 return int(scsi_typeid.splitlines()[0])
181 except:
182 return None
184 def get_scsi_revision(pHCTL):
185 try:
186 sysfs_mnt = utils.find_sysfs_mount()
187 sysfs_scsi_dev_path = \
188 os.path.join(sysfs_mnt + SYSFS_SCSI_PATH, pHCTL)
189 scsi_revision = \
190 os.popen('cat ' + sysfs_scsi_dev_path + \
191 SYSFS_SCSI_DEV_REVISION_PATH).read()
192 return scsi_revision.splitlines()[0]
193 except:
194 return None
196 def get_scsi_scsilevel(pHCTL):
197 try:
198 sysfs_mnt = utils.find_sysfs_mount()
199 sysfs_scsi_dev_path = \
200 os.path.join(sysfs_mnt + SYSFS_SCSI_PATH, pHCTL)
201 scsi_scsilevel = \
202 os.popen('cat ' + sysfs_scsi_dev_path + \
203 SYSFS_SCSI_DEV_SCSILEVEL_PATH).read()
204 return int(scsi_scsilevel.splitlines()[0])
205 except:
206 return None
208 def get_all_scsi_devices():
210 scsi_devs = []
212 for scsi_info in vscsi_get_scsidevices():
213 scsi_dev = {
214 'physical_HCTL': scsi_info[0],
215 'dev_name': None,
216 'sg_name': scsi_info[2],
217 'scsi_id': None
218 }
219 if scsi_info[1] != '-':
220 scsi_dev['dev_name'] = scsi_info[1]
221 if scsi_info[3] != '-':
222 scsi_dev['scsi_id'] = scsi_info[3]
224 scsi_dev['vendor_name'] = \
225 get_scsi_vendor(scsi_dev['physical_HCTL'])
226 scsi_dev['model'] = \
227 get_scsi_model(scsi_dev['physical_HCTL'])
228 scsi_dev['type_id'] = \
229 get_scsi_typeid(scsi_dev['physical_HCTL'])
230 scsi_dev['revision'] = \
231 get_scsi_revision(scsi_dev['physical_HCTL'])
232 scsi_dev['scsi_level'] = \
233 get_scsi_scsilevel(scsi_dev['physical_HCTL'])
235 try:
236 lsscsi_info = os.popen('lsscsi ' + scsi_dev['physical_HCTL']).read().split()
237 scsi_dev['type'] = lsscsi_info[1]
238 except:
239 scsi_dev['type'] = None
241 scsi_devs.append(scsi_dev)
243 return scsi_devs