def diff(vhd_fn1, vhd_fn2): cmd = "vhd-util check -n %s" % (vhd_fn1) util.doexec(cmd, 0) cmd = "vhd-util check -n %s" % (vhd_fn2) util.doexec(cmd, 0) cmd = LIBVHDIO_CMD + "diff %s %s" % (vhd_fn1, vhd_fn2) return util.doexec(cmd, 0)
def zeroReferenceFile(reference_fn, size_mb): if os.path.isfile(reference_fn): cmdrm = "rm %s" % (reference_fn) util.doexec(cmdrm,0) cmd = "dd if=/dev/zero of=%s bs=1M count=%d" % (reference_fn, size_mb) util.doexec(cmd, 0) return
def zeroReferenceFile(reference_fn, size_mb): if os.path.isfile(reference_fn): cmdrm = "rm %s" % (reference_fn) util.doexec(cmdrm, 0) cmd = "dd if=/dev/zero of=%s bs=1M count=%d" % (reference_fn, size_mb) util.doexec(cmd, 0) return
def _fill_range(vhd_fn, reference_fn, pattern_fn, off, len, pattern_len, pattern_off): if pattern_off < off: raise Exception("invalid usage of _fill_range") # _zero(reference_fn, off, pattern_off - off) cmd = "dd conv=notrunc if=%s of=%s bs=1 seek=%d" % (pattern_fn, reference_fn, pattern_off) util.doexec(cmd, 0) # _zero(reference_fn, pattern_off + pattern_len, len - pattern_len - (pattern_off - off)) cmd = LIBVHDIO_CMD + "dd if=%s of=%s bs=1 seek=%d" % (pattern_fn, vhd_fn, pattern_off) util.doexec(cmd, 0)
def sg_readcap(device): device = os.path.join('/dev', getdev(device)) readcapcommand = ['/usr/bin/sg_readcap', '-b', device] (rc,stdout,stderr) = util.doexec(readcapcommand) if rc == 6: # retry one time for "Capacity data has changed" (rc,stdout,stderr) = util.doexec(readcapcommand) if rc != 0: raise util.SMException("util.sg_readcap(%s) failed" % (device)) (blockcount,blocksize) = stdout.split() return (int(blockcount, 0) * int(blocksize, 0))
def _is_valid_multipath_device(sid): # Check if device is already multipathed (ret, stdout, stderr) = util.doexec(['/usr/sbin/multipath', '-ll', sid]) if not stdout + stderr: (ret, stdout, stderr) = util.doexec(['/usr/sbin/multipath', '-a', sid]) if ret == 1: util.SMlog("Failed to add {}: wwid could be explicitly " "blacklisted\n Continue with multipath disabled for " "this SR".format(sid)) return False by_scsid_path = "/dev/disk/by-scsid/" + sid if os.path.exists(by_scsid_path): devs = os.listdir(by_scsid_path) else: util.SMlog( "Device {} is not ready yet, skipping multipath check".format( by_scsid_path)) return False ret = 1 # Some paths might be down, check all associated devices for dev in devs: devpath = os.path.join(by_scsid_path, dev) real_path = util.get_real_path(devpath) (ret, stdout, stderr) = util.doexec(['/usr/sbin/multipath', '-c', real_path]) if ret == 0: break if ret == 1: # This is very fragile but it is not a good sign to fail without # any output. At least until multipath 0.4.9, for example, # multipath -c fails without any log if it is able to retrieve the # wwid of the device. # In this case it is better to fail immediately. if not stdout + stderr: # Attempt to cleanup wwids file before raising try: (ret, stdout, stderr) = util.doexec(['/usr/sbin/multipath', '-w', sid]) except OSError: util.SMlog("Error removing {} from wwids file".format(sid)) raise xs_errors.XenError( 'MultipathGenericFailure', '"multipath -c" failed without any' ' output on {}'.format(real_path)) util.SMlog( "When dealing with {} multipath status returned:\n " "{}{} Continue with multipath disabled for this SR".format( sid, stdout, stderr)) return False return True
def fill(vhd_fn, reference_fn, size_mb, pattern): zeroReferenceFile( reference_fn, size_mb) #writing zeroes to reference file to avoid WAW propblem size = size_mb * 1024 * 1024 fraction = 100 if pattern == PATTERN_EMPTY: cmd = "dd if=/dev/zero of=%s bs=1M count=%d" % (reference_fn, size_mb) util.doexec(cmd, 0) return pattern_string = "Random bits here >>%s<< end of random bits" % random.getrandbits( 100) pattern_len = len(pattern_string) f = open(PATTERN_FILE, 'w') f.write(pattern_string) f.close() if pattern == PATTERN_SHORT_STRING_BEGINNING: _fill_range(vhd_fn, reference_fn, PATTERN_FILE, 0, size, pattern_len, 0) elif pattern == PATTERN_SHORT_STRING_MIDDLE: _fill_range(vhd_fn, reference_fn, PATTERN_FILE, 0, size, pattern_len, size / 2) elif pattern == PATTERN_SHORT_STRING_END: _fill_range(vhd_fn, reference_fn, PATTERN_FILE, 0, size, pattern_len, size - pattern_len) elif pattern == PATTERN_BLOCKS_SEQUENTIAL: for i in range(size / VHD_BLOCK_SIZE): _fill_range(vhd_fn, reference_fn, PATTERN_FILE, i * VHD_BLOCK_SIZE, VHD_BLOCK_SIZE, pattern_len, i * VHD_BLOCK_SIZE + 1000) elif pattern == PATTERN_BLOCKS_REVERSE: for i in range(size / VHD_BLOCK_SIZE - 1, -1, -1): _fill_range(vhd_fn, reference_fn, PATTERN_FILE, i * VHD_BLOCK_SIZE, VHD_BLOCK_SIZE, pattern_len, i * VHD_BLOCK_SIZE + 1000) elif pattern == PATTERN_BLOCKS_RANDOM: block_seq = range(size / VHD_BLOCK_SIZE) random.shuffle(block_seq) for i in block_seq: print "Populating block %d" % i _fill_block(vhd_fn, reference_fn, i, PATTERN_FILE, pattern_len) elif pattern == PATTERN_BLOCKS_RANDOM_FRACTION: block_seq = range(1, (size / VHD_BLOCK_SIZE), fraction) random.shuffle(block_seq) for i in block_seq: print "Populating block %d" % i _fill_block(vhd_fn, reference_fn, i, PATTERN_FILE, pattern_len) else: raise Exception("Invalid pattern number: %d" % pattern) os.unlink(PATTERN_FILE)
def _fill_range(vhd_fn, reference_fn, pattern_fn, off, len, pattern_len, pattern_off): if pattern_off < off: raise Exception("invalid usage of _fill_range") # _zero(reference_fn, off, pattern_off - off) cmd = "dd conv=notrunc if=%s of=%s bs=1 seek=%d" % ( pattern_fn, reference_fn, pattern_off) util.doexec(cmd, 0) # _zero(reference_fn, pattern_off + pattern_len, len - pattern_len - (pattern_off - off)) cmd = LIBVHDIO_CMD + "dd if=%s of=%s bs=1 seek=%d" % (pattern_fn, vhd_fn, pattern_off) util.doexec(cmd, 0)
def sg_readcap(device): device = os.path.join('/dev', getdev(device)) readcapcommand = ['/usr/bin/sg_readcap', '-b', device] (rc,stdout,stderr) = util.doexec(readcapcommand) if rc == 6: # retry one time for "Capacity data has changed" (rc,stdout,stderr) = util.doexec(readcapcommand) if rc != 0: raise util.SMException("scsiutil.sg_readcap(%s) failed" % (device)) match = re.search('(^|.*\n)(0x[0-9a-fA-F]+) (0x[0-9a-fA-F]+)\n$', stdout) if not match: raise util.SMException("scsiutil.sg_readcap(%s) failed to parse: %s" % (device, stdout)) (blockcount, blocksize) = match.group(2, 3) return (int(blockcount, 0) * int(blocksize, 0))
def sg_readcap(device): device = os.path.join('/dev', getdev(device)) readcapcommand = ['/usr/bin/sg_readcap', '-b', device] (rc, stdout, stderr) = util.doexec(readcapcommand) if rc == 6: # retry one time for "Capacity data has changed" (rc, stdout, stderr) = util.doexec(readcapcommand) if rc != 0: raise util.SMException("scsiutil.sg_readcap(%s) failed" % (device)) match = re.search('(^|.*\n)(0x[0-9a-fA-F]+) (0x[0-9a-fA-F]+)\n$', stdout) if not match: raise util.SMException("scsiutil.sg_readcap(%s) failed to parse: %s" % (device, stdout)) (blockcount, blocksize) = match.group(2, 3) return (int(blockcount, 0) * int(blocksize, 0))
def parse_config(vendor, product): device_config = None try: cmd = "show config" XenCertPrint("mpath cmd: %s" % cmd) (rc, stdout, stderr) = util.doexec(mpath_cli.mpathcmd, cmd) XenCertPrint("mpath output: %s" % stdout) d = parse_multipathd_config( [line + '\n' for line in stdout.split('\n')]) XenCertPrint("mpath config to dict: %s" % d) for _, device_value in d["devices"]: XenCertPrint("device attributes: %s" % device_value) attr_map = dict(device_value) if 'vendor' not in attr_map or 'product' not in attr_map: XenCertPrint( "warning: skip the device attributes because can not find mandatory key vendor or product" ) continue re_vendor = re.compile(attr_map['vendor'].strip('"')) re_product = re.compile(attr_map['product'].strip('"')) if (re_vendor.search(vendor) and re_product.search(product)): XenCertPrint("matched vendor and product") device_config = dict(multiPathDefaultsMap.items() + attr_map.items()) break except Exception, e: XenCertPrint( "Failed to get multipath config for vendor: %s and product: %s. Exception: %s" % (vendor, product, str(e)))
def check_conf_file(): (rc,stdout,stderr) = util.doexec(['/sbin/multipath','-h']) # Ugly way to check for malformed conf file if len(stdout): util.SMlog("Malformed multipath conf file") return 1 return 0
def fill(vhd_fn, reference_fn, size_mb, pattern): zeroReferenceFile(reference_fn, size_mb) #writing zeroes to reference file to avoid WAW propblem size = size_mb * 1024 * 1024 fraction = 100 if pattern == PATTERN_EMPTY: cmd = "dd if=/dev/zero of=%s bs=1M count=%d" % (reference_fn, size_mb) util.doexec(cmd, 0) return pattern_string = "Random bits here >>%s<< end of random bits" % random.getrandbits(100) pattern_len = len(pattern_string) f = open(PATTERN_FILE, 'w') f.write(pattern_string) f.close() if pattern == PATTERN_SHORT_STRING_BEGINNING: _fill_range(vhd_fn, reference_fn, PATTERN_FILE, 0, size, pattern_len, 0) elif pattern == PATTERN_SHORT_STRING_MIDDLE: _fill_range(vhd_fn, reference_fn, PATTERN_FILE, 0, size, pattern_len, size / 2) elif pattern == PATTERN_SHORT_STRING_END: _fill_range(vhd_fn, reference_fn, PATTERN_FILE, 0, size, pattern_len, size - pattern_len) elif pattern == PATTERN_BLOCKS_SEQUENTIAL: for i in range(size / VHD_BLOCK_SIZE): _fill_range(vhd_fn, reference_fn, PATTERN_FILE, i * VHD_BLOCK_SIZE, VHD_BLOCK_SIZE, pattern_len, i * VHD_BLOCK_SIZE + 1000) elif pattern == PATTERN_BLOCKS_REVERSE: for i in range(size / VHD_BLOCK_SIZE - 1, -1, -1): _fill_range(vhd_fn, reference_fn, PATTERN_FILE, i * VHD_BLOCK_SIZE, VHD_BLOCK_SIZE, pattern_len, i * VHD_BLOCK_SIZE + 1000) elif pattern == PATTERN_BLOCKS_RANDOM: block_seq = range(size / VHD_BLOCK_SIZE) random.shuffle(block_seq) for i in block_seq: print "Populating block %d" % i _fill_block(vhd_fn, reference_fn, i, PATTERN_FILE, pattern_len) elif pattern == PATTERN_BLOCKS_RANDOM_FRACTION: block_seq = range(1, (size / VHD_BLOCK_SIZE), fraction) random.shuffle(block_seq) for i in block_seq: print "Populating block %d" % i _fill_block(vhd_fn, reference_fn, i, PATTERN_FILE, pattern_len) else: raise Exception("Invalid pattern number: %d" % pattern) os.unlink(PATTERN_FILE)
def exn_on_failure(cmd, message): '''Executes via util.doexec the command specified. If the return code is non-zero, raises an ISCSIError with the given message''' (rc,stdout,stderr) = util.doexec(cmd) if rc==0: return (stdout,stderr) else: msg = 'rc: %d, stdout: %s, stderr: %s' % (rc,stdout,stderr) raise xs_errors.XenError('SMGeneral', opterr=msg)
def do_get_topology(cmd): util.SMlog("mpath cmd: %s" % cmd) (rc, stdout, stderr) = util.doexec(mpathcmd, cmd) util.SMlog("mpath output: %s" % stdout) lines = stdout.split('\n')[:-1] if len(lines): m = regex2.search(lines[0]) lines[0] = str(m.group(2)) return lines
def do_get_topology(cmd): util.SMlog("mpath cmd: %s" % cmd) (rc,stdout,stderr) = util.doexec(mpathcmd,cmd) util.SMlog("mpath output: %s" % stdout) lines = stdout.split('\n')[:-1] if len(lines): m=regex2.search(lines[0]) lines[0]=str(m.group(2)) return lines
def DiskDataTest(device, test_blocks, sect_of_block=DDT_DEFAULT_BLOCK_SIZE, test_time=0): iter_start = str(random.randint(0, 100000)) cmd = [ DISKDATATEST, 'write', device, str(sect_of_block), str(test_blocks), str(test_time), iter_start ] XenCertPrint("The command to be fired is: %s" % cmd) (rc, stdout, stderr) = util.doexec(cmd) if rc != 0: raise Exception("Disk test write error!") XenCertPrint("diskdatatest returned : %s" % stdout) lastString = stdout.strip().splitlines()[-1] total_blocks, write_blocks, write_elapsed, _ = lastString.split() total_blocks, write_blocks, write_elapsed = int(total_blocks), int( write_blocks), float(write_elapsed) cmd = [ DISKDATATEST, 'verify', device, str(sect_of_block), str(write_blocks), str(test_time), iter_start ] XenCertPrint("The command to be fired is: %s" % cmd) (rc, stdout, stderr) = util.doexec(cmd) if rc != 0: raise Exception("Disk test verify error!") XenCertPrint("diskdatatest returned : %s" % stdout) lastString = stdout.strip().splitlines()[-1] _, verify_blocks, verify_elapsed, sector_errors = lastString.split() verify_blocks, verify_elapsed, sector_errors = int(verify_blocks), float( verify_elapsed), int(sector_errors) if sector_errors != 0: raise Exception("Disk test verify error on %d sectors!", sector_errors) return total_blocks, write_blocks, write_elapsed, verify_blocks, verify_elapsed
def parse_config(vendor, product): try: retVal = True cmd = "show config" XenCertPrint("mpath cmd: %s" % cmd) (rc, stdout, stderr) = util.doexec(mpath_cli.mpathcmd, cmd) XenCertPrint("mpath output: %s" % stdout) stdout = stdout.rstrip('}\nmultipaths {\n}\nmultipathd> ') + '\t' XenCertPrint("mpath output after stripping: %s" % stdout) list = stdout.split("device {") skipThis = True for para in list: returnmap = {} XenCertPrint("The para is: %s" % para) if not skipThis: para = para.lstrip() para = para.rstrip('\n\t}\n\t') listParams = para.split('\n\t\t') XenCertPrint("ListParams: %s" % listParams) for paramPair in listParams: key = '' value = '' params = paramPair.split(' ') firstParam = True for param in params: if firstParam: key = param firstParam = False continue else: value += param value += ' ' value = value.strip() returnmap[key] = value if returnmap['product'].find('*') != -1: if returnmap['product'] != '*': regexproduct = re.compile(returnmap['product']) if returnmap[ 'vendor'] == vendor and regexproduct.search( product): break else: if returnmap['vendor'] == vendor: break else: if returnmap['vendor'] == vendor and returnmap[ 'product'] == product: break else: skipThis = False except Exception, e: XenCertPrint( "Failed to get multipath config for vendor: %s and product: %s. Exception: %s" % (vendor, product, str(e))) retVal = False
def doexec_locked(cmd): '''Executes via util.doexec the command specified whilst holding lock''' _lock = None if os.path.basename(cmd[0]) == 'iscsiadm': _lock = lock.Lock(LOCK_TYPE_RUNNING, 'iscsiadm') _lock.acquire() #util.SMlog("%s" % (cmd)) (rc, stdout, stderr) = util.doexec(cmd) if _lock != None and _lock.held(): _lock.release() return (rc, stdout, stderr)
def parse_config(vendor, product): try: retVal = True cmd = "show config" XenCertPrint("mpath cmd: %s" % cmd) (rc, stdout, stderr) = util.doexec(mpath_cli.mpathcmd, cmd) XenCertPrint("mpath output: %s" % stdout) stdout = stdout.rstrip("}\nmultipaths {\n}\nmultipathd> ") + "\t" XenCertPrint("mpath output after stripping: %s" % stdout) list = stdout.split("device {") skipThis = True for para in list: returnmap = {} XenCertPrint("The para is: %s" % para) if not skipThis: para = para.lstrip() para = para.rstrip("\n\t}\n\t") listParams = para.split("\n\t\t") XenCertPrint("ListParams: %s" % listParams) for paramPair in listParams: key = "" value = "" params = paramPair.split(" ") firstParam = True for param in params: if firstParam: key = param firstParam = False continue else: value += param value += " " value = value.strip() returnmap[key] = value returnmap["vendor"] = returnmap["vendor"].replace('"', "") returnmap["product"] = returnmap["product"].replace('"', "") if returnmap["product"].find("*") != -1: if returnmap["product"] != "*": regexproduct = re.compile(returnmap["product"]) if returnmap["vendor"] == vendor and regexproduct.search(product): break else: if returnmap["vendor"] == vendor: break else: if returnmap["vendor"] == vendor and returnmap["product"] == product: break else: skipThis = False except Exception, e: XenCertPrint( "Failed to get multipath config for vendor: %s and product: %s. Exception: %s" % (vendor, product, str(e)) ) retVal = False
def is_working(): cmd = "help" try: (rc, stdout, stderr) = util.doexec(mpathcmd, cmd) m = regex3.search(stdout) if m: return True else: return False except: return False
def doexec_locked(cmd): '''Executes via util.doexec the command specified whilst holding lock''' _lock = None if os.path.basename(cmd[0]) == 'iscsiadm': _lock = lock.Lock(LOCK_TYPE_RUNNING, 'iscsiadm') _lock.acquire() #util.SMlog("%s" % (cmd)) (rc,stdout,stderr) = util.doexec(cmd) if _lock <> None and _lock.held(): _lock.release() return (rc, stdout, stderr)
def _zero(fn, off, len): if len == 0: return partial_block = BLOCK_SIZE - (off % BLOCK_SIZE) if partial_block % BLOCK_SIZE: if partial_block > len: partial_block = len cmd = "dd conv=notrunc if=/dev/zero of=%s bs=1 seek=%d count=%d" % (fn, off, partial_block) util.doexec(cmd, 0) off += partial_block len -= partial_block if len == 0: return whole_blocks = len / BLOCK_SIZE if whole_blocks: cmd = "dd conv=notrunc if=/dev/zero of=%s bs=%d seek=%d count=%d" % \ (fn, BLOCK_SIZE, off / BLOCK_SIZE, whole_blocks) util.doexec(cmd, 0) off += whole_blocks * BLOCK_SIZE len -= whole_blocks * BLOCK_SIZE if len == 0: return cmd = "dd conv=notrunc if=/dev/zero of=%s bs=1 seek=%d count=%d" % (fn, off, len) util.doexec(cmd, 0)
def _zero(fn, off, len): if len == 0: return partial_block = BLOCK_SIZE - (off % BLOCK_SIZE) if partial_block % BLOCK_SIZE: if partial_block > len: partial_block = len cmd = "dd conv=notrunc if=/dev/zero of=%s bs=1 seek=%d count=%d" % ( fn, off, partial_block) util.doexec(cmd, 0) off += partial_block len -= partial_block if len == 0: return whole_blocks = len / BLOCK_SIZE if whole_blocks: cmd = "dd conv=notrunc if=/dev/zero of=%s bs=%d seek=%d count=%d" % \ (fn, BLOCK_SIZE, off / BLOCK_SIZE, whole_blocks) util.doexec(cmd, 0) off += whole_blocks * BLOCK_SIZE len -= whole_blocks * BLOCK_SIZE if len == 0: return cmd = "dd conv=notrunc if=/dev/zero of=%s bs=1 seek=%d count=%d" % ( fn, off, len) util.doexec(cmd, 0)
def is_working(): cmd="help" util.SMlog("mpath cmd: %s" % cmd) try: (rc,stdout,stderr) = util.doexec(mpathcmd,cmd) util.SMlog("mpath output: %s" % stdout) m=regex3.search(stdout) if m: return True else: return False except: return False
def is_working(): cmd = "help" util.SMlog("mpath cmd: %s" % cmd) try: (rc, stdout, stderr) = util.doexec(mpathcmd, cmd) util.SMlog("mpath output: %s" % stdout) m = regex3.search(stdout) if m: return True else: return False except: return False
def parse_config(vendor, product): try: retVal = True cmd="show config" XenCertPrint("mpath cmd: %s" % cmd) (rc,stdout,stderr) = util.doexec(mpath_cli.mpathcmd,cmd) XenCertPrint("mpath output: %s" % stdout) stdout = stdout.rstrip('}\nmultipaths {\n}\nmultipathd> ') + '\t' XenCertPrint("mpath output after stripping: %s" % stdout) list = stdout.split("device {") skipThis = True for para in list: returnmap = {} XenCertPrint("The para is: %s" % para) if not skipThis: para = para.lstrip() para = para.rstrip('\n\t}\n\t') listParams = para.split('\n\t\t') XenCertPrint("ListParams: %s" % listParams) for paramPair in listParams: key = '' value = '' params = paramPair.split(' ') firstParam = True for param in params: if firstParam: key = param firstParam = False continue else: value += param value += ' ' value = value.strip() returnmap[key] = value if returnmap['product'].find('*') != -1: if returnmap['product'] != '*': regexproduct = re.compile(returnmap['product']) if returnmap['vendor'] == vendor and regexproduct.search(product): break else: if returnmap['vendor'] == vendor: break else: if returnmap['vendor'] == vendor and returnmap['product'] == product: break else: skipThis = False except Exception, e: XenCertPrint("Failed to get multipath config for vendor: %s and product: %s. Exception: %s" % (vendor, product, str(e))) retVal = False
def _genMPPHBA(id): devs = scsiutil.cacheSCSIidentifiers() mppdict = {} for dev in devs: item = devs[dev] if item[1] == id: arr = scsiutil._genArrayIdentifier(dev) if not len(arr): continue try: cmd = ['/usr/sbin/mppUtil', '-a'] for line in util.doexec(cmd)[1].split('\n'): if line.find(arr) != -1: rec = line.split()[0] cmd2 = ['/usr/sbin/mppUtil', '-g',rec] li = [] for newline in util.doexec(cmd2)[1].split('\n'): if newline.find('hostId') != -1: li.append(_parseHostId(newline)) mppdict[dev.split('/')[-1]] = li except: continue return mppdict
def FindTimeToWriteData(devicename, sizeInMiB): ddOutFile = 'of=' + devicename XenCertPrint("Now copy %dMiB data from /dev/zero to this device and record the time taken to copy it." % sizeInMiB) cmd = ['dd', 'if=/dev/zero', ddOutFile, 'bs=4096', 'count=%d' % (sizeInMiB * 256)] try: (rc, stdout, stderr) = util.doexec(cmd,'') list = stderr.split('\n') timeTaken = list[2].split(',')[1] dataCopyTime = int(float(timeTaken.split()[0])) XenCertPrint("The IO test returned rc: %s stdout: %s, stderr: %s" % (rc, stdout, stderr)) XenCertPrint("Time taken to copy %dMiB to the device %s is %d" % (sizeInMiB, devicename, dataCopyTime)) return dataCopyTime except Exception, e: raise Exception(str(e))
def FindDiskDataTestEstimate(device, size): estimatedTime = 0 # Run diskdatatest in a report mode XenCertPrint("Run diskdatatest in a report mode with device %s to find the estimated time." % device) cmd = ["./diskdatatest", "report", "1", device] XenCertPrint("The command to be fired is: %s" % cmd) (rc, stdout, stderr) = util.doexec(cmd) if rc != 0: return 0 estimatedTime = int(stdout.split("\n")[len(stdout.split("\n")) - 1]) XenCertPrint("Extracted estimated time for writing a 512MB chunk on the device %s as %d" % (device, estimatedTime)) totalTime = ((size / 512) * estimatedTime) / 1000000 XenCertPrint("Total estimated time for testing IO with the device %s as %d" % (device, totalTime)) return totalTime
def FindTimeToWriteData(devicename, sizeInMiB): ddOutFile = "of=" + devicename XenCertPrint("Now copy %dMiB data from /dev/zero to this device and record the time taken to copy it." % sizeInMiB) cmd = ["dd", "if=/dev/zero", ddOutFile, "bs=4096", "count=%d" % (sizeInMiB * 256), "oflag=direct"] try: (rc, stdout, stderr) = util.doexec(cmd, "") list = stderr.split("\n") timeTaken = list[2].split(",")[1] dataCopyTime = int(float(timeTaken.split()[0])) XenCertPrint("The IO test returned rc: %s stdout: %s, stderr: %s" % (rc, stdout, stderr)) XenCertPrint("Time taken to copy %dMiB to the device %s is %d" % (sizeInMiB, devicename, dataCopyTime)) return dataCopyTime except Exception, e: raise Exception(str(e))
def exn_on_failure(cmd, message): '''Executes via util.doexec the command specified. If the return code is non-zero, raises an ISCSIError with the given message''' _lock = None if os.path.basename(cmd[0]) == 'iscsiadm': _lock = lock.Lock(LOCK_TYPE_RUNNING, 'iscsiadm') _lock.acquire() (rc,stdout,stderr) = util.doexec(cmd) if _lock <> None and _lock.held(): _lock.release() if rc==0: return (stdout,stderr) else: msg = 'rc: %d, stdout: %s, stderr: %s' % (rc,stdout,stderr) raise xs_errors.XenError('SMGeneral', opterr=msg)
def parse_fcoe_port_name_info(): fcoe_port_info = [] try: cmd= ['fcoeadm', '-i'] for line in util.doexec(cmd)[1].split('\n'): if line.find("Port Name") != -1: str1, str2 = line.split(":") str2 = str2.strip() port = int(str2,0) util.SMlog(" port is %d" % port) fcoe_port_info.append(port) except: pass return fcoe_port_info
def FindDiskDataTestEstimate(device, size): estimatedTime = 0 # Run diskdatatest in a report mode XenCertPrint("Run diskdatatest in a report mode with device %s to find the estimated time." % device) cmd = [DISKDATATEST, 'report', '1', device] XenCertPrint("The command to be fired is: %s" % cmd) (rc, stdout, stderr) = util.doexec(cmd) if rc == 0: lastString = (stdout.split('\n')[-1]) XenCertPrint("diskdatatest returned : %s" % lastString) estimatedTime = int(lastString.split(' ')[-1]) else: XenCertPrint("Diskdatatest return Error : %s" % stderr) estimateTime = 0 XenCertPrint("Total estimated time for testing IO with the device %s as %d" % (device, estimatedTime)) return estimatedTime
def parse_fcoe_port_name_info(): fcoe_port_info = [] fcoe_ports = glob.glob(os.path.join(SYSFS_NET_PATH, "eth*")) for port in fcoe_ports: try: cmd= ['fcoeadm', '-i', os.path.basename(port)] for line in util.doexec(cmd)[1].split('\n'): if line.find("Port Name") != -1: str1, str2 = line.split(":") str2 = str2.strip() port = int(str2,0) util.SMlog(" port is %d" % port) fcoe_port_info.append(port) break except: pass return fcoe_port_info
def parse_fcoe_port_name_info(): fcoe_port_info = [] fcoe_ports = glob.glob(os.path.join(SYSFS_NET_PATH, "eth*")) for port in fcoe_ports: try: cmd = ['fcoeadm', '-i', os.path.basename(port)] for line in util.doexec(cmd)[1].split('\n'): if line.find("Port Name") != -1: str1, str2 = line.split(":") str2 = str2.strip() port = int(str2, 0) util.SMlog(" port is %d" % port) fcoe_port_info.append(port) break except: pass return fcoe_port_info
def FindDiskDataTestEstimate(device, size): estimatedTime = 0 # Run diskdatatest in a report mode XenCertPrint( "Run diskdatatest in a report mode with device %s to find the estimated time." % device) cmd = ['./diskdatatest', 'report', '1', device] XenCertPrint("The command to be fired is: %s" % cmd) (rc, stdout, stderr) = util.doexec(cmd) if rc != 0: return 0 estimatedTime = int(stdout.split('\n')[len(stdout.split('\n')) - 1]) XenCertPrint( "Extracted estimated time for writing a 512MB chunk on the device %s as %d" % (device, estimatedTime)) totalTime = ((size / 512) * estimatedTime) / 1000000 XenCertPrint( "Total estimated time for testing IO with the device %s as %d" % (device, totalTime)) return totalTime
def parse_fcoe_eth_info(): fcoe_eth_info = {} # create a dictionary of rport to eth try: cmd = ["fcoeadm", "-l"] regex = re.compile("eth[0-9]") for line in util.doexec(cmd)[1].split("\n"): if line.find("Interface") != -1: searchObj = regex.search(line, 0) if searchObj: eth = searchObj.group() util.SMlog("eth: %s" % eth) if line.find("rport") != -1: str1, str2 = line.split(":", 1) fcoe_eth_info[str2.strip()] = eth eth = "" except: pass return fcoe_eth_info
def _openExclusive(dev, retry): try: return os.open("%s" % dev, os.O_RDWR | os.O_EXCL) except OSError as ose: opened_by = '' if ose.errno == 16: if retry: util.SMlog('Device %s is busy, one shot retry' % dev) time.sleep(2) return _openExclusive(dev, False) else: (rc, stdout, stderr) = util.doexec(['lsof', dev]) util.SMlog('Device %s is busy after retry, opened by %s' % (dev, stdout)) util.SMlog('Opening device %s failed with %d' % (dev, ose.errno)) raise xs_errors.XenError( 'SRInUse', opterr=('Device %s in use, please check your existing ' + 'SRs for an instance of this device') % dev)
def parse_fcoe_eth_info(): fcoe_eth_info = {} # create a dictionary of rport to eth try: cmd = ['fcoeadm', '-l'] regex = re.compile("eth[0-9]") for line in util.doexec(cmd)[1].split('\n'): if line.find("Interface") != -1: searchObj = regex.search(line, 0) if searchObj: eth = searchObj.group() util.SMlog("eth: %s" % eth) if line.find("rport") != -1: str1, str2 = line.split(":", 1) fcoe_eth_info[str2.strip()] = eth eth = "" except: pass return fcoe_eth_info
def is_blacklisted(dev): """This function returns True if the device is blacklisted according to multipath.conf rules. It cannot be used to check the current daemon rules because it could be running with an old configuration file in memory dev -- it is any string accepted by "multipath -c". A full path is sufficient """ (rc,stdout,stderr) = util.doexec(['/sbin/multipath','-v3','-c',dev]) # If the devices is blacklisted according to multipath.conf, the # string "wwid blacklisted" appears in the verbose output. # This is a very fragile mechanism and keeps changing. # What we want is a method to tell immediately if a device is # blacklisted according only to configuration file rules regardless # of daemon in-memory configuration. return "wwid blacklisted" in stdout
def is_blacklisted(dev): """This function returns True if the device is blacklisted according to multipath.conf rules. There is also the possibility that the path to the device we are checking is unavailable for some reason. In that case, is_blacklisted() cannot tell if the device is blacklisted or not and a WWIDException is raised. It cannot be used to check the current daemon rules because it could be running with an old configuration file in memory Input: dev -- it is any string accepted by "multipath -c". A full path is expected. Return: bool -- True or False, depending on whether the device is blacklisted or not. Raise: WWIDException """ (rc,stdout,stderr) = util.doexec(['/sbin/multipath','-v3','-c',dev]) if "scope is nul" in stdout: raise WWIDException("WARNING: Could not retrieve device's " "WWID. Path {} is down".format(dev)) # If the device is blacklisted according to multipath.conf, the # string "wwid blacklisted" appears in the verbose output. # This is a very fragile mechanism and keeps changing. # What we want is a method to tell immediately if a device is # blacklisted according only to configuration file rules regardless # of daemon in-memory configuration. return "wwid blacklisted" in stdout
def is_blacklisted(dev): """This function returns 0 if the device is not blacklisted according to multipath.conf rules. It cannot be used to check the current daemon rules because it could be running with an old configuration file in memory dev -- it is any string accepted by "multipath -c". A full path is sufficient """ (rc,stdout,stderr) = util.doexec(['/sbin/multipath','-c',dev]) # If the devices is truly blacklisted, there is nothing on stdout. # This is a very fragile mechanism and keeps changing. # What we want is a method to tell immediately if a device is # blacklisted according only to configuration file rules regardless # of daemon in-memory configuration. # Current "multipath -c" takes into account multipath/wwids file # but we do not care about it. if len(stdout) != 0: rc = 0 return rc != False
def is_blacklisted(dev): """This function returns 0 if the device is not blacklisted according to multipath.conf rules. It cannot be used to check the current daemon rules because it could be running with an old configuration file in memory dev -- it is any string accepted by "multipath -c". A full path is sufficient """ (rc, stdout, stderr) = util.doexec(["/sbin/multipath", "-c", dev]) # If the devices is truly blacklisted, there is nothing on stdout. # This is a very fragile mechanism and keeps changing. # What we want is a method to tell immediately if a device is # blacklisted according only to configuration file rules regardless # of daemon in-memory configuration. # Current "multipath -c" takes into account multipath/wwids file # but we do not care about it. if len(stdout) != 0: rc = 0 return rc != False
def extract(vhd_fn, out_fn): cmd = "vhd-util check -n %s" % (vhd_fn) util.doexec(cmd, 0) cmd = LIBVHDIO_CMD + "dd if=%s of=%s bs=4K" % (vhd_fn, out_fn) util.doexec(cmd, 0)
def mpexec(cmd): util.SMlog("mpath cmd: %s" % cmd) (rc, stdout, stderr) = util.doexec(mpathcmd, cmd) if stdout != "multipathd> ok\nmultipathd> " \ and stdout != "multipathd> " + cmd + "\nok\nmultipathd> ": raise MPathCLIFail
def list_maps(): cmd = "list maps" util.SMlog("mpath cmd: %s" % cmd) (rc, stdout, stderr) = util.doexec(mpathcmd, cmd) util.SMlog("mpath output: %s" % stdout) return map(lambda x: x.split(' ')[0], stdout.split('\n')[2:-1])
def _is_mpath_daemon_running(): cmd = ["pidof", "-s", "multipathd"] (rc, stdout, stderr) = util.doexec(cmd) return (rc == 0)
def getManufacturer(s): (rc,stdout,stderr) = util.doexec(['/sbin/modinfo', '-d', s]) if stdout: return stdout.strip() else: return "Unknown"
def _is_mpath_daemon_running(): cmd = ["/sbin/pidof", "-s", "/sbin/multipathd"] (rc,stdout,stderr) = util.doexec(cmd) return (rc==0)
def list_maps(): cmd="list maps" util.SMlog("mpath cmd: %s" % cmd) (rc,stdout,stderr) = util.doexec(mpathcmd,cmd) util.SMlog("mpath output: %s" % stdout) return map(lambda x: x.split(' ')[0], stdout.split('\n')[1:-1])
def mpexec(cmd): util.SMlog("mpath cmd: %s" % cmd) (rc,stdout,stderr) = util.doexec(mpathcmd,cmd) if stdout != "multipathd> ok\nmultipathd> " \ and stdout != "multipathd> "+cmd+"\nok\nmultipathd> ": raise MPathCLIFail