Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/python3 

2# 

3# Copyright (C) Citrix Systems Inc. 

4# 

5# This program is free software; you can redistribute it and/or modify 

6# it under the terms of the GNU Lesser General Public License as published 

7# by the Free Software Foundation; version 2.1 only. 

8# 

9# This program 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 

12# GNU Lesser General Public License for more details. 

13# 

14# You should have received a copy of the GNU Lesser General Public License 

15# along with this program; if not, write to the Free Software Foundation, Inc., 

16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 

17# 

18# Miscellaneous scsi utility functions 

19# 

20 

21import util 

22import os 

23import re 

24import xs_errors 

25import base64 

26import time 

27import errno 

28import glob 

29import mpath_cli 

30 

31PREFIX_LEN = 4 

32SUFFIX_LEN = 12 

33SECTOR_SHIFT = 9 

34SCSI_ID_BIN = '/usr/lib/udev/scsi_id' 

35 

36 

37def gen_hash(st, len): 

38 hs = 0 

39 for i in st: 

40 hs = ord(i) + (hs << 6) + (hs << 16) - hs 

41 return str(hs)[0:len] 

42 

43 

44def gen_uuid_from_serial(iqn, serial): 

45 if len(serial) < SUFFIX_LEN: 

46 raise util.CommandException(1) 

47 prefix = gen_hash(iqn, PREFIX_LEN) 

48 suffix = gen_hash(serial, SUFFIX_LEN) 

49 str = prefix.encode("hex") + suffix.encode("hex") 

50 return str[0:8] + '-' + str[8:12] + '-' + str[12:16] + '-' + str[16:20] + '-' + str[20:32] 

51 

52 

53def gen_serial_from_uuid(iqn, uuid): 

54 str = uuid.replace('-', '') 

55 prefix = gen_hash(iqn, PREFIX_LEN) 

56 if str[0:(PREFIX_LEN * 2)].decode("hex") != prefix: 

57 raise util.CommandException(1) 

58 return str[(PREFIX_LEN * 2):].decode("hex") 

59 

60 

61def getsize(path): 

62 dev = getdev(path) 

63 sysfs = os.path.join('/sys/block', dev, 'size') 

64 size = 0 

65 if os.path.exists(sysfs): 

66 try: 

67 f = open(sysfs, 'r') 

68 size = (int(f.readline()) << SECTOR_SHIFT) 

69 f.close() 

70 except: 

71 pass 

72 return size 

73 

74 

75def getuniqueserial(path): 

76 dev = getdev(path) 

77 try: 

78 cmd = ["md5sum"] 

79 txt = util.pread3(cmd, getSCSIid(path)) 

80 return txt.split(' ')[0] 

81 except: 

82 return '' 

83 

84 

85def gen_uuid_from_string(src_string): 

86 if len(src_string) < (PREFIX_LEN + SUFFIX_LEN): 

87 raise util.CommandException(1) 

88 return (src_string[0:8] + '-' + 

89 src_string[8:12] + '-' + 

90 src_string[12:16] + '-' + 

91 src_string[16:20] + '-' + 

92 src_string[20:32]) 

93 

94 

95def SCSIid_sanitise(str): 

96 text = str.strip() 

97 return re.sub(r"\s+", "_", text) 

98 

99 

100def getSCSIid(path): 

101 """Get the SCSI id of a block device 

102 

103 Input: 

104 path -- (str) path to block device; can be symlink 

105 

106 Return: 

107 scsi_id -- (str) the device's SCSI id 

108 

109 Raise: 

110 util.CommandException 

111 """ 

112 

113 if not path.startswith('/dev/'): 113 ↛ 114line 113 didn't jump to line 114, because the condition on line 113 was never true

114 util.SMlog("getSCSIid: fixing invalid input {}".format(path), 

115 priority=util.LOG_WARNING) 

116 path = '/dev/' + path.lstrip('/') 

117 

118 stdout = util.pread2([SCSI_ID_BIN, '-g', '--device', path]) 

119 

120 return SCSIid_sanitise(stdout) 

121 

122 

123def compareSCSIid_2_6_18(SCSIid, path): 

124 serial = getserial(path) 

125 len_serial = len(serial) 

126 if (len_serial == 0) or (len_serial > (len(SCSIid) - 1)): 

127 return False 

128 list_SCSIid = list(SCSIid) 

129 list_serial = list_SCSIid[1:(len_serial + 1)] 

130 serial_2_6_18 = ''.join(list_serial) 

131 if (serial == serial_2_6_18): 

132 return True 

133 else: 

134 return False 

135 

136 

137def getserial(path): 

138 dev = os.path.join('/dev', getdev(path)) 

139 try: 

140 cmd = ["sginfo", "-s", dev] 

141 text = re.sub(r"\s+", "", util.pread2(cmd)) 

142 except: 

143 raise xs_errors.XenError('EIO', \ 

144 opterr='An error occured querying device serial number [%s]' \ 

145 % dev) 

146 try: 

147 return text.split("'")[1] 

148 except: 

149 return '' 

150 

151 

152def getmanufacturer(path): 

153 cmd = ["sginfo", "-M", path] 

154 try: 

155 for line in filter(match_vendor, util.pread2(cmd).split('\n')): 

156 return line.replace(' ', '').split(':')[-1] 

157 except: 

158 return '' 

159 

160 

161def cacheSCSIidentifiers(): 

162 SCSI = {} 

163 SYS_PATH = "/dev/disk/by-scsibus/*" 

164 for node in glob.glob(SYS_PATH): 

165 if not re.match(r'.*-\d+:\d+:\d+:\d+$', node): 165 ↛ 166line 165 didn't jump to line 166, because the condition on line 165 was never true

166 continue 

167 dev = os.path.realpath(node) 

168 HBTL = os.path.basename(node).split("-")[-1].split(":") 

169 line = "NONE %s %s %s %s 0 %s" % \ 

170 (HBTL[0], HBTL[1], HBTL[2], HBTL[3], dev) 

171 ids = line.split() 

172 SCSI[ids[6]] = ids 

173 return SCSI 

174 

175 

176def scsi_dev_ctrl(ids, cmd): 

177 f = None 

178 for i in range(0, 10): 

179 try: 

180 str = "scsi %s-single-device %s %s %s %s" % \ 

181 (cmd, ids[1], ids[2], ids[3], ids[4]) 

182 util.SMlog(str) 

183 f = open('/proc/scsi/scsi', 'w') 

184 print(str, file=f) 

185 f.close() 

186 return 

187 except IOError as e: 

188 util.SMlog("SCSI_DEV_CTRL: Failure, %s [%d]" % (e.strerror, e.errno)) 

189 if f is not None: 

190 f.close() 

191 f = None 

192 if e.errno == errno.ENXIO: 

193 util.SMlog("Device has disappeared already") 

194 return 

195 time.sleep(6) 

196 continue 

197 raise xs_errors.XenError('EIO', \ 

198 opterr='An error occured during the scsi operation') 

199 

200 

201def getdev(path): 

202 realpath = os.path.realpath(path) 

203 if match_dm(realpath): 203 ↛ 204line 203 didn't jump to line 204, because the condition on line 203 was never true

204 newpath = realpath.replace("/dev/mapper/", "/dev/disk/by-id/scsi-") 

205 else: 

206 newpath = path 

207 return os.path.realpath(newpath).split('/')[-1] 

208 

209 

210def get_devices_by_SCSIid(SCSIid): 

211 devices = os.listdir(os.path.join('/dev/disk/by-scsid', SCSIid)) 

212 if 'mapper' in devices: 

213 devices.remove('mapper') 

214 return devices 

215 

216 

217def rawdev(dev): 

218 device = getdev(dev) 

219 if device.startswith('dm-') and device[3:].isdigit(): 

220 return device 

221 

222 return re.sub('[0-9]*$', '', device) 

223 

224 

225def getSessionID(path): 

226 for line in filter(match_session, util.listdir(path)): 

227 return line.split('-')[-1] 

228 

229 

230def match_session(s): 

231 regex = re.compile("^SESSIONID-") 

232 return regex.search(s, 0) 

233 

234 

235def match_vendor(s): 

236 regex = re.compile("^Vendor:") 

237 return regex.search(s, 0) 

238 

239 

240def match_dm(s): 

241 regex = re.compile("mapper/") 

242 return regex.search(s, 0) 

243 

244 

245def match_sd(s): 

246 regex = re.compile("/dev/sd") 

247 return regex.search(s, 0) 

248 

249 

250def _isSCSIdev(dev): 

251 if match_dm(dev): 

252 path = dev.replace("/dev/mapper/", "/dev/disk/by-id/scsi-") 

253 else: 

254 path = dev 

255 return match_sd(os.path.realpath(path)) 

256 

257 

258def add_serial_record(session, sr_ref, devstring): 

259 try: 

260 conf = session.xenapi.SR.get_sm_config(sr_ref) 

261 conf['devserial'] = devstring 

262 session.xenapi.SR.set_sm_config(sr_ref, conf) 

263 except: 

264 pass 

265 

266 

267def get_serial_record(session, sr_ref): 

268 try: 

269 conf = session.xenapi.SR.get_sm_config(sr_ref) 

270 return conf['devserial'] 

271 except: 

272 return "" 

273 

274 

275def devlist_to_serialstring(devlist): 

276 serial = '' 

277 for dev in devlist: 

278 try: 

279 devserial = "scsi-%s" % getSCSIid(dev) 

280 if not len(devserial) > 0: 

281 continue 

282 if len(serial): 

283 serial += ',' 

284 serial += devserial 

285 except: 

286 pass 

287 

288 return serial 

289 

290 

291def gen_synthetic_page_data(uuid): 

292 # For generating synthetic page data for non-raw LUNs 

293 # we set the vendor ID to XENSRC 

294 # Note that the Page 80 serial number must be limited 

295 # to 16 characters 

296 page80 = "" 

297 page80 += "\x00\x80" 

298 page80 += "\x00\x12" 

299 page80 += uuid[0:16] 

300 page80 += " " 

301 

302 page83 = "" 

303 page83 += "\x00\x83" 

304 page83 += "\x00\x31" 

305 page83 += "\x02\x01\x00\x2d" 

306 page83 += "XENSRC " 

307 page83 += uuid 

308 page83 += " " 

309 return ["", base64.b64encode(str.encode(page80)).decode(), 

310 base64.b64encode(str.encode(page83)).decode()] 

311 

312 

313def gen_raw_page_data(path): 

314 default = "" 

315 page80 = "" 

316 page83 = "" 

317 try: 

318 cmd = ["sg_inq", "-r", path] 

319 text = util.pread2(cmd) 

320 default = base64.b64encode(text) 

321 

322 cmd = ["sg_inq", "--page=0x80", "-r", path] 

323 text = util.pread2(cmd) 

324 page80 = base64.b64encode(text) 

325 

326 cmd = ["sg_inq", "--page=0x83", "-r", path] 

327 text = util.pread2(cmd) 

328 page83 = base64.b64encode(text) 

329 except: 

330 pass 

331 return [default, page80, page83] 

332 

333 

334def update_XS_SCSIdata(vdi_uuid, data): 

335 # XXX: PR-1255: passing through SCSI data doesn't make sense when 

336 # it will change over storage migration. It also doesn't make sense 

337 # to preserve one array's identity and copy it when a VM moves to 

338 # a new array because the drivers in the VM may attempt to contact 

339 # the original array, fail and bluescreen. 

340 

341 xenstore_data = {} 

342 xenstore_data["vdi-uuid"] = vdi_uuid 

343 if len(data[0]): 343 ↛ 344line 343 didn't jump to line 344, because the condition on line 343 was never true

344 xenstore_data["scsi/0x12/default"] = data[0] 

345 

346 if len(data[1]): 346 ↛ 349line 346 didn't jump to line 349, because the condition on line 346 was never false

347 xenstore_data["scsi/0x12/0x80"] = data[1] 

348 

349 if len(data[2]): 349 ↛ 352line 349 didn't jump to line 352, because the condition on line 349 was never false

350 xenstore_data["scsi/0x12/0x83"] = data[2] 

351 

352 return xenstore_data 

353 

354 

355def rescan(ids, fullrescan=True): 

356 for id in ids: 

357 refresh_HostID(id, fullrescan) 

358 

359 

360def _genHostList(procname): 

361 # loop through and check all adapters 

362 ids = [] 

363 try: 

364 for dir in util.listdir('/sys/class/scsi_host'): 

365 filename = os.path.join('/sys/class/scsi_host', dir, 'proc_name') 

366 if os.path.exists(filename): 

367 f = open(filename, 'r') 

368 if f.readline().find(procname) != -1: 

369 ids.append(dir.replace("host", "")) 

370 f.close() 

371 except: 

372 pass 

373 return ids 

374 

375 

376def _genReverseSCSIidmap(SCSIid, pathname="scsibus"): 

377 util.SMlog("map_by_scsibus: sid=%s" % SCSIid) 

378 

379 devices = [] 

380 for link in glob.glob('/dev/disk/by-%s/%s-*' % (pathname, SCSIid)): 

381 realpath = os.path.realpath(link) 

382 if os.path.exists(realpath): 

383 devices.append(realpath) 

384 return devices 

385 

386 

387def _genReverseSCSidtoLUNidmap(SCSIid): 

388 devices = [] 

389 for link in glob.glob('/dev/disk/by-scsibus/%s-*' % SCSIid): 

390 devices.append(link.split('-')[-1]) 

391 return devices 

392 

393 

394def _dosgscan(): 

395 regex = re.compile(r"([^:]*):\s+scsi([0-9]+)\s+channel=([0-9]+)\s+id=([0-9]+)\s+lun=([0-9]+)") 

396 scan = util.pread2(["/usr/bin/sg_scan"]).split('\n') 

397 sgs = [] 

398 for line in scan: 

399 m = regex.match(line) 

400 if m: 

401 device = m.group(1) 

402 host = m.group(2) 

403 channel = m.group(3) 

404 sid = m.group(4) 

405 lun = m.group(5) 

406 sgs.append([device, host, channel, sid, lun]) 

407 return sgs 

408 

409 

410def refresh_HostID(HostID, fullrescan): 

411 LUNs = glob.glob('/sys/class/scsi_disk/%s*' % HostID) 

412 li = [] 

413 for l in LUNs: 

414 chan = re.sub(":[0-9]*$", '', os.path.basename(l)) 

415 if chan not in li: 415 ↛ 413line 415 didn't jump to line 413, because the condition on line 415 was never false

416 li.append(chan) 

417 

418 if len(li) and not fullrescan: 418 ↛ 419line 418 didn't jump to line 419, because the condition on line 418 was never true

419 for c in li: 

420 if not refresh_scsi_channel(c): 

421 fullrescan = True 

422 

423 if fullrescan: 423 ↛ exitline 423 didn't return from function 'refresh_HostID', because the condition on line 423 was never false

424 util.SMlog("Full rescan of HostID %s" % HostID) 

425 path = '/sys/class/scsi_host/host%s/scan' % HostID 

426 if os.path.exists(path): 426 ↛ 427line 426 didn't jump to line 427, because the condition on line 426 was never true

427 try: 

428 scanstring = "- - -" 

429 f = open(path, 'w') 

430 f.write('%s\n' % scanstring) 

431 f.close() 

432 if len(li): 

433 # Channels already exist, allow some time for 

434 # undiscovered LUNs/channels to appear 

435 time.sleep(2) 

436 except: 

437 pass 

438 # Host Bus scan issued, now try to detect channels 

439 if util.wait_for_path("/sys/class/scsi_disk/%s*" % HostID, 5): 439 ↛ exitline 439 didn't return from function 'refresh_HostID', because the condition on line 439 was never false

440 # At least one LUN is mapped 

441 LUNs = glob.glob('/sys/class/scsi_disk/%s*' % HostID) 

442 li = [] 

443 for l in LUNs: 

444 chan = re.sub(":[0-9]*$", '', os.path.basename(l)) 

445 if chan not in li: 445 ↛ 443line 445 didn't jump to line 443, because the condition on line 445 was never false

446 li.append(chan) 

447 for c in li: 

448 refresh_scsi_channel(c) 

449 

450 

451def refresh_scsi_channel(channel): 

452 DEV_WAIT = 5 

453 util.SMlog("Refreshing channel %s" % channel) 

454 util.wait_for_path('/dev/disk/by-scsibus/*-%s*' % channel, DEV_WAIT) 

455 LUNs = glob.glob('/dev/disk/by-scsibus/*-%s*' % channel) 

456 try: 

457 rootdevs = util.dom0_disks() 

458 except: 

459 util.SMlog("Failed to query root disk, failing operation") 

460 return False 

461 

462 # a) Find a LUN to issue a Query LUNs command 

463 li = [] 

464 Query = False 

465 for lun in LUNs: 

466 try: 

467 hbtl = lun.split('-')[-1] 

468 h = hbtl.split(':') 

469 l = util.pread2(["/usr/bin/sg_luns", "-q", lun]).split('\n') 

470 li = [] 

471 for i in l: 

472 if len(i): 

473 li.append(int(i[0:4], 16)) 

474 util.SMlog("sg_luns query returned %s" % li) 

475 Query = True 

476 break 

477 except: 

478 pass 

479 if not Query: 

480 util.SMlog("Failed to detect or query LUN on Channel %s" % channel) 

481 return False 

482 

483 # b) Remove stale LUNs 

484 current = glob.glob('/dev/disk/by-scsibus/*-%s:%s:%s*' % (h[0], h[1], h[2])) 

485 for cur in current: 

486 lunID = int(cur.split(':')[-1]) 

487 newhbtl = ['', h[0], h[1], h[2], str(lunID)] 

488 if os.path.realpath(cur) in rootdevs: 

489 # Don't touch the rootdev 

490 if lunID in li: 

491 li.remove(lunID) 

492 continue 

493 

494 # Check if LUN is stale, and remove it 

495 if not lunID in li: 

496 util.SMlog("Stale LUN detected. Removing HBTL: %s" % newhbtl) 

497 scsi_dev_ctrl(newhbtl, "remove") 

498 util.wait_for_nopath(cur, DEV_WAIT) 

499 continue 

500 else: 

501 li.remove(lunID) 

502 

503 # Check if the device is still present 

504 if not os.path.exists(cur): 

505 continue 

506 

507 # Query SCSIid, check it matches, if not, re-probe 

508 cur_SCSIid = os.path.basename(cur).split("-%s:%s:%s" % (h[0], h[1], h[2]))[0] 

509 real_SCSIid = getSCSIid(cur) 

510 if cur_SCSIid != real_SCSIid: 

511 util.SMlog("HBTL %s does not match, re-probing" % newhbtl) 

512 scsi_dev_ctrl(newhbtl, "remove") 

513 util.wait_for_nopath(cur, DEV_WAIT) 

514 scsi_dev_ctrl(newhbtl, "add") 

515 util.wait_for_path('/dev/disk/by-scsibus/%s-%s' % (real_SCSIid, hbtl), DEV_WAIT) 

516 pass 

517 

518 # c) Probe for any LUNs that are not present in the system 

519 for l in li: 

520 newhbtl = ['', h[0], h[1], h[2], str(l)] 

521 newhbtlstr = "%s:%s:%s:%s" % (h[0], h[1], h[2], str(l)) 

522 util.SMlog("Probing new HBTL: %s" % newhbtl) 

523 scsi_dev_ctrl(newhbtl, "add") 

524 util.wait_for_path('/dev/disk/by-scsibus/*-%s' % newhbtlstr, DEV_WAIT) 

525 

526 return True 

527 

528 

529def refreshdev(pathlist): 

530 """ 

531 Refresh block devices given a path list 

532 """ 

533 for path in pathlist: 

534 dev = getdev(path) 

535 sysfs = os.path.join('/sys/block', dev, 'device/rescan') 

536 if os.path.exists(sysfs): 

537 try: 

538 with open(sysfs, 'w') as f: 

539 f.write('1') 

540 except: 

541 pass 

542 

543 

544def refresh_lun_size_by_SCSIid(SCSIid): 

545 """ 

546 Refresh all devices for the SCSIid. 

547 Returns True if all known devices and the mapper device are up to date. 

548 """ 

549 

550 def get_primary_device(SCSIid): 

551 mapperdevice = os.path.join('/dev/mapper', SCSIid) 

552 if os.path.exists(mapperdevice): 

553 return mapperdevice 

554 else: 

555 devices = get_devices_by_SCSIid(SCSIid) 

556 if devices: 

557 return devices[0] 

558 else: 

559 return None 

560 

561 def get_outdated_size_devices(currentcapacity, devices): 

562 devicesthatneedrefresh = [] 

563 for device in devices: 

564 if getsize(device) != currentcapacity: 

565 devicesthatneedrefresh.append(device) 

566 return devicesthatneedrefresh 

567 

568 def refresh_devices_if_needed(primarydevice, SCSIid, currentcapacity): 

569 devices = get_devices_by_SCSIid(SCSIid) 

570 if "/dev/mapper/" in primarydevice: 

571 devices = set(devices + mpath_cli.list_paths(SCSIid)) 

572 devicesthatneedrefresh = get_outdated_size_devices(currentcapacity, 

573 devices) 

574 if devicesthatneedrefresh: 

575 # timing out avoids waiting for min(dev_loss_tmo, fast_io_fail_tmo) 

576 # if one or multiple devices don't answer 

577 util.timeout_call(10, refreshdev, devicesthatneedrefresh) 

578 if get_outdated_size_devices(currentcapacity, 

579 devicesthatneedrefresh): 

580 # in this state we shouldn't force resizing the mapper dev 

581 raise util.SMException("Failed to get %s to agree on the " 

582 "current capacity." 

583 % devicesthatneedrefresh) 

584 

585 def refresh_mapper_if_needed(primarydevice, SCSIid, currentcapacity): 

586 if "/dev/mapper/" in primarydevice \ 

587 and get_outdated_size_devices(currentcapacity, [primarydevice]): 

588 mpath_cli.resize_map(SCSIid) 

589 if get_outdated_size_devices(currentcapacity, [primarydevice]): 

590 raise util.SMException("Failed to get the mapper dev to agree " 

591 "on the current capacity.") 

592 

593 try: 

594 primarydevice = get_primary_device(SCSIid) 

595 if primarydevice: 

596 currentcapacity = sg_readcap(primarydevice) 

597 refresh_devices_if_needed(primarydevice, SCSIid, currentcapacity) 

598 refresh_mapper_if_needed(primarydevice, SCSIid, currentcapacity) 

599 else: 

600 util.SMlog("scsiutil.refresh_lun_size_by_SCSIid(%s) could not " 

601 "find any devices for the SCSIid." % SCSIid) 

602 return True 

603 except: 

604 util.logException("Error in scsiutil.refresh_lun_size_by_SCSIid(%s)" 

605 % SCSIid) 

606 return False 

607 

608 

609def refresh_lun_size_by_SCSIid_on_slaves(session, SCSIid): 

610 for slave in util.get_all_slaves(session): 

611 util.SMlog("Calling on-slave.refresh_lun_size_by_SCSIid(%s) on %s." 

612 % (SCSIid, slave)) 

613 resulttext = session.xenapi.host.call_plugin( 

614 slave, 

615 "on-slave", 

616 "refresh_lun_size_by_SCSIid", 

617 {'SCSIid': SCSIid}) 

618 if "True" == resulttext: 

619 util.SMlog("Calling on-slave.refresh_lun_size_by_SCSIid(%s) on" 

620 " %s succeeded." % (SCSIid, slave)) 

621 else: 

622 message = ("Failed in on-slave.refresh_lun_size_by_SCSIid(%s) " 

623 "on %s." % (SCSIid, slave)) 

624 raise util.SMException("Slave %s failed in on-slave.refresh_lun_" 

625 "size_by_SCSIid(%s) " % (slave, SCSIid)) 

626 

627 

628def remove_stale_luns(hostids, lunid, expectedPath, mpath): 

629 try: 

630 for hostid in hostids: 

631 # get all LUNs of the format hostid:x:y:lunid 

632 luns = glob.glob('/dev/disk/by-scsibus/*-%s:*:*:%s' % (hostid, lunid)) 

633 

634 # try to get the scsiid for each of these luns 

635 for lun in luns: 

636 try: 

637 getSCSIid(lun) 

638 # if it works, we have a problem as this device should not 

639 # be present and be valid on this system 

640 util.SMlog("Warning! The lun %s should not be present and" \ 

641 " be valid on this system." % lun) 

642 except: 

643 # Now do the rest. 

644 pass 

645 

646 # get the HBTL 

647 basename = os.path.basename(lun) 

648 hbtl_list = basename.split(':') 

649 hbtl = basename.split('-')[1] 

650 

651 # the first one in scsiid-hostid 

652 hbtl_list[0] = hbtl_list[0].split('-')[1] 

653 

654 expectedPath = expectedPath + '*' + hbtl 

655 if not os.path.exists(expectedPath): 

656 # wait for sometime and check if the expected path exists 

657 # check if a rescan was done outside of this process 

658 time.sleep(2) 

659 

660 if os.path.exists(expectedPath): 

661 # do not remove device, this might be dangerous 

662 util.SMlog("Path %s appeared before checking for " \ 

663 "stale LUNs, ignore this LUN %s." % (expectedPath, lun)) 

664 continue 

665 

666 # remove the scsi device 

667 l = [os.path.realpath(lun), hbtl_list[0], hbtl_list[1], \ 

668 hbtl_list[2], hbtl_list[3]] 

669 scsi_dev_ctrl(l, 'remove') 

670 

671 # if multipath is enabled, do a best effort cleanup 

672 if mpath: 

673 try: 

674 path = os.path.basename(os.path.realpath(lun)) 

675 mpath_cli.remove_path(path) 

676 except Exception as e: 

677 util.SMlog("Failed to remove path %s, ignoring " \ 

678 "exception as path may not be present." % path) 

679 except Exception as e: 

680 util.SMlog("Exception removing stale LUNs, new devices may not come" \ 

681 " up properly! Error: %s" % str(e)) 

682 

683 

684def sg_readcap(device): 

685 device = os.path.join('/dev', getdev(device)) 

686 readcapcommand = ['/usr/bin/sg_readcap', '-b', device] 

687 (rc, stdout, stderr) = util.doexec(readcapcommand) 

688 if rc == 6: 

689 # retry one time for "Capacity data has changed" 

690 (rc, stdout, stderr) = util.doexec(readcapcommand) 

691 if rc != 0: 691 ↛ 692line 691 didn't jump to line 692, because the condition on line 691 was never true

692 raise util.SMException("scsiutil.sg_readcap(%s) failed" % (device)) 

693 match = re.search('(^|.*\n)(0x[0-9a-fA-F]+) (0x[0-9a-fA-F]+)\n$', stdout) 

694 if not match: 694 ↛ 695line 694 didn't jump to line 695, because the condition on line 694 was never true

695 raise util.SMException("scsiutil.sg_readcap(%s) failed to parse: %s" 

696 % (device, stdout)) 

697 (blockcount, blocksize) = match.group(2, 3) 

698 return (int(blockcount, 0) * int(blocksize, 0))