def get_kernel_debuginfo_rpm_link(): knl_dbginfo_re = params.get("knl_dbginfo_re") tag = params.get("brew_tag") kbuild_name = params.get("kernel_build_name", "kernel") latest_pkg_cmd = "brew latest-pkg %s %s" % (tag, kbuild_name) o = utils.system_output(latest_pkg_cmd, timeout=360) build = re.findall("kernel[^\s]+", o)[0] logging.debug("Latest package on brew for tag %s is %s" % (tag, build)) buildinfo = utils.system_output("brew buildinfo %s" % build, timeout=360) try: knl_dbginfo_links = re.findall(knl_dbginfo_re, buildinfo, re.I) except IndexError: raise error.TestError("Could not get kernel-debuginfo package " "brew link matching pattern '%s'" % knl_dbginfo_re) knl_dbginfo_urls = [] for l in knl_dbginfo_links: link = get_brew_url(l, download_root) knl_dbginfo_urls.append(link) return knl_dbginfo_urls
def setupInitSymlink(self): logging.debug('Symlinking init scripts') autodir = os.environ.get('AUTODIR') rc = os.path.join(autodir, 'tools/autotest') if os.path.isfile(rc) and os.path.islink(rc): # nothing to do return # see if system supports event.d versus inittab if os.path.exists('/etc/event.d'): # NB: assuming current runlevel is default initdefault = utils.system_output('/sbin/runlevel').split()[1] elif os.path.exists('/etc/inittab'): initdefault = utils.system_output('grep :initdefault: /etc/inittab') initdefault = initdefault.split(':')[1] else: initdefault = '2' try: utils.system('ln -sf %s /etc/init.d/autotest' % rc) utils.system('ln -sf %s /etc/rc%s.d/S99autotest' % (rc, initdefault)) logging.debug('Labeling init scripts with unconfined_exec_t') utils.system('chcon -h system_u:object_r:unconfined_exec_t:s0 /etc/init.d/autotest') utils.system('chcon -h system_u:object_r:unconfined_exec_t:s0 /etc/rc%s.d/S99autotest' % initdefault) autotest_init = os.path.join(autodir, 'tools/autotest') ret = os.system('chcon system_u:object_r:unconfined_exec_t:s0 %s' % autotest_init) logging.debug('chcon returned <%s>', ret) except: logging.warning('Linking init scripts failed')
def set_chap_auth_target(self): """ set up authentication information for every single initiator, which provides the capability to define common login information for all Endpoints in a TPG """ auth_cmd = "targetcli /iscsi/%s/tpg1/ " % self.target attr_cmd = ("set attribute %s %s %s" % ("demo_mode_write_protect=0", "generate_node_acls=1", "cache_dynamic_acls=1")) utils.system(auth_cmd + attr_cmd) # Set userid userid_cmd = "%s set auth userid=%s" % (auth_cmd, self.chap_user) output = utils.system_output(userid_cmd) if self.chap_user not in output: raise error.TestFail("Failed to set user. (%s)", output) # Set password passwd_cmd = "%s set auth password=%s" % (auth_cmd, self.chap_passwd) output = utils.system_output(passwd_cmd) if self.chap_passwd not in output: raise error.TestFail("Failed to set password. (%s)", output) # Save configuration utils.system("targetcli / saveconfig")
def get_user_ugid(username): """ return user uid and gid as a list """ user_uid = utils.system_output("id -u %s" % username).split() user_gid = utils.system_output("id -g %s" % username).split() return(user_uid, user_gid)
def export_target(self): """ Export target in localhost for emulated iscsi """ if not os.path.isfile(self.emulated_image): utils.system(self.create_cmd) cmd = "tgtadm --lld iscsi --mode target --op show" try: output = utils.system_output(cmd) except error.CmdError: utils.system("service tgtd restart") output = utils.system_output(cmd) if not re.findall("%s$" % self.target, output, re.M): logging.debug("Need to export target in host") output = utils.system_output(cmd) used_id = re.findall("Target\s+(\d+)", output) emulated_id = 1 while str(emulated_id) in used_id: emulated_id += 1 self.emulated_id = str(emulated_id) cmd = "tgtadm --mode target --op new --tid %s" % self.emulated_id cmd += " --lld iscsi --targetname %s" % self.target utils.system(cmd) cmd = "tgtadm --mode logicalunit --op new " cmd += "--tid %s --lld iscsi --lun 1 " % self.emulated_id cmd += "--backing-store %s" % self.emulated_image utils.system(cmd) cmd = "tgtadm --lld iscsi --op bind --mode target " cmd += "--tid %s -I ALL" % self.emulated_id utils.system(cmd) self.export_flag = True else: self.emulated_id = re.findall("Target\s+(\d+):\s+%s$" % self.target, output)
def get_target_id(self): """ Get target id from image name. """ cmd = "targetcli ls /iscsi 1" target_info = utils.system_output(cmd) target = None for line in re.split("\n", target_info)[1:]: if re.findall("o-\s\S+\s[\.]+\s\[TPGs:\s\d\]$", line): # eg: iqn.2015-05.com.example:iscsi.disk try: target = re.findall("iqn[\.]\S+:\S+", line)[0] except IndexError: logging.info("No found target in %s", line) continue else: continue cmd = "targetcli ls /iscsi/%s/tpg1/luns" % target luns_info = utils.system_output(cmd) for lun_line in re.split("\n", luns_info): if re.findall("o-\slun\d+", lun_line): if self.emulated_image in lun_line: break else: target = None return target
def get_kernel_rpm_link(): method = params.get("method", "link") if method not in ["link", "brew"]: raise error.TestError("Unknown installation method %s" % method) if method == "link": return (params.get("kernel_version"), params.get("kernel_rpm"), params.get("firmware_rpm")) error.context("Get latest kernel package link from brew", logging.info) # fetch the newest packages from brew # FIXME: really brain dead method to fetch the kernel version # kernel_vesion = re... + hint from configuration file # is there any smart way to fetch the `uname -r` from # brew build? rh_kernel_hint = "[\d+][^\s]+" kernel_re = params.get("kernel_re") tag = params.get("brew_tag") kbuild_name = params.get("kernel_build_name", "kernel") latest_pkg_cmd = "brew latest-pkg %s %s" % (tag, kbuild_name) o = utils.system_output(latest_pkg_cmd, timeout=360) build = re.findall("kernel[^\s]+", o)[0] logging.debug("Latest package on brew for tag %s is %s" % (tag, build)) buildinfo = utils.system_output("brew buildinfo %s" % build, timeout=360) # install kernel-firmware firmware_url = None if "firmware" in buildinfo: logging.info("Found kernel-firmware") fw_pattern = ".*firmware.*" try: fw_brew_link = re.findall(fw_pattern, buildinfo)[0] except IndexError: raise error.TestError("Could not get kernel-firmware package" " brew link matching pattern '%s'" % fw_pattern) firmware_url = get_brew_url(fw_brew_link, download_root) knl_pattern = kernel_re % rh_kernel_hint try: knl_brew_link = re.findall(knl_pattern, buildinfo, re.I)[0] except IndexError: raise error.TestError("Could not get kernel package brew link" " matching pattern '%s'" % knl_pattern) kernel_url = get_brew_url(knl_brew_link, download_root) debug_re = kernel_re % ("(%s)" % rh_kernel_hint) try: kernel_version = re.findall(debug_re, kernel_url, re.I)[0] except IndexError: raise error.TestError("Could not get kernel version matching" " pattern '%s'" % debug_re) kernel_version += "." + params.get("kernel_suffix", "") return kernel_version, kernel_url, firmware_url
def export_target(self): """ Export target in localhost for emulated iscsi """ if not os.path.isfile(self.emulated_image): utils.system(self.create_cmd) else: emulated_image_size = os.path.getsize(self.emulated_image) / 1024 if emulated_image_size != self.emulated_expect_size: # No need to remvoe, rebuild is fine utils.system(self.create_cmd) cmd = "tgtadm --lld iscsi --mode target --op show" try: output = utils.system_output(cmd) except error.CmdError: utils.system("service tgtd restart") output = utils.system_output(cmd) if not re.findall("%s$" % self.target, output, re.M): logging.debug("Need to export target in host") output = utils.system_output(cmd) used_id = re.findall("Target\s+(\d+)", output) emulated_id = 1 while str(emulated_id) in used_id: emulated_id += 1 self.emulated_id = str(emulated_id) cmd = "tgtadm --mode target --op new --tid %s" % self.emulated_id cmd += " --lld iscsi --targetname %s" % self.target utils.system(cmd) cmd = "tgtadm --lld iscsi --op bind --mode target " cmd += "--tid %s -I ALL" % self.emulated_id utils.system(cmd) else: target_strs = re.findall("Target\s+(\d+):\s+%s$" % self.target, output, re.M) self.emulated_id = target_strs[0].split(':')[0].split()[-1] cmd = "tgtadm --lld iscsi --mode target --op show" try: output = utils.system_output(cmd) except error.CmdError: # In case service stopped utils.system("service tgtd restart") output = utils.system_output(cmd) # Create a LUN with emulated image if re.findall(self.emulated_image, output, re.M): # Exist already logging.debug("Exported image already exists.") self.export_flag = True return else: luns = len(re.findall("\s+LUN:\s(\d+)", output, re.M)) cmd = "tgtadm --mode logicalunit --op new " cmd += "--tid %s --lld iscsi " % self.emulated_id cmd += "--lun %s " % luns cmd += "--backing-store %s" % self.emulated_image utils.system(cmd) self.export_flag = True
def xfs_tunables(dev): """Call xfs_grow -n to get filesystem tunables.""" # Have to mount the filesystem to call xfs_grow. tmp_mount_dir = tempfile.mkdtemp() cmd = 'mount %s %s' % (dev, tmp_mount_dir) utils.system_output(cmd) xfs_growfs = os.path.join(os.environ['AUTODIR'], 'tools', 'xfs_growfs') cmd = '%s -n %s' % (xfs_growfs, dev) try: out = utils.system_output(cmd) finally: # Clean. cmd = 'umount %s' % dev utils.system_output(cmd, ignore_status=True) os.rmdir(tmp_mount_dir) # The output format is given in report_info (xfs_growfs.c) # "meta-data=%-22s isize=%-6u agcount=%u, agsize=%u blks\n" # " =%-22s sectsz=%-5u attr=%u\n" # "data =%-22s bsize=%-6u blocks=%llu, imaxpct=%u\n" # " =%-22s sunit=%-6u swidth=%u blks\n" # "naming =version %-14u bsize=%-6u\n" # "log =%-22s bsize=%-6u blocks=%u, version=%u\n" # " =%-22s sectsz=%-5u sunit=%u blks, lazy-count=%u\n" # "realtime =%-22s extsz=%-6u blocks=%llu, rtextents=%llu\n" tune2fs_dict = {} # Flag for extracting naming version number keep_version = False for line in out.splitlines(): m = re.search('^([-\w]+)', line) if m: main_tag = m.group(1) pairs = line.split() for pair in pairs: # naming: version needs special treatment if pair == '=version': # 1 means the next pair is the version number we want keep_version = True continue if keep_version: tune2fs_dict['naming: version'] = pair # Resets the flag since we have logged the version keep_version = False continue # Ignores the strings without '=', such as 'blks' if '=' not in pair: continue key, value = pair.split('=') tagged_key = '%s: %s' % (main_tag, key) if re.match('[0-9]+', value): tune2fs_dict[tagged_key] = int(value.rstrip(',')) else: tune2fs_dict[tagged_key] = value.rstrip(',') return tune2fs_dict
def copy_and_install_rpm(session, url, upgrade=False): rpm_name = os.path.basename(url) if url.startswith("http"): download_cmd = "wget %s" % url utils.system_output(download_cmd) rpm_src = rpm_name else: rpm_src = utils_misc.get_path(test.bindir, url) vm.copy_files_to(rpm_src, "/tmp/%s" % rpm_name) install_rpm(session, "/tmp/%s" % rpm_name, upgrade)
def glusterd_start(): """ Check for glusterd status """ cmd = "service glusterd status" output = utils.system_output(cmd, ignore_status=True) if 'stopped' in output: cmd = "service glusterd start" error.context("Starting gluster dameon failed") output = utils.system_output(cmd)
def glusterd_start(): """ Check for glusterd status and start it """ cmd = "service glusterd status" output = utils.system_output(cmd, ignore_status=True) # The blank before 'active' makes a distinction with 'inactive' if ' active' not in output or 'running' not in output: cmd = "service glusterd start" error.context("Starting gluster dameon failed") output = utils.system_output(cmd)
def get_systemmap(): """Return the full path to System.map Ahem. This is crap. Pray harder. Bad Martin. """ map = '/boot/System.map-%s' % utils.system_output('uname -r') if os.path.isfile(map): return map map = '/lib/modules/%s/build/System.map' % utils.system_output('uname -r') if os.path.isfile(map): return map return None
def get_vmlinux(): """Return the full path to vmlinux Ahem. This is crap. Pray harder. Bad Martin. """ vmlinux = '/boot/vmlinux-%s' % utils.system_output('uname -r') if os.path.isfile(vmlinux): return vmlinux vmlinux = '/lib/modules/%s/build/vmlinux' % utils.system_output('uname -r') if os.path.isfile(vmlinux): return vmlinux return None
def get_pf_vf_info(self): """ Get pf and vf related information in this host that match ``self.pf_filter_re``. for every pf it will create following information: pf_id: The id of the pf device. occupied: Whether the pf device assigned or not vf_ids: Id list of related vf in this pf. ethname: eth device name in host for this pf. :return: return a list contains pf vf information. :rtype: list of dict """ base_dir = "/sys/bus/pci/devices" cmd = "lspci | awk '/%s/ {print $1}'" % self.pf_filter_re pf_ids = [i for i in utils.system_output(cmd).splitlines()] pf_vf_dict = [] for pf_id in pf_ids: pf_info = {} vf_ids = [] full_id = utils_misc.get_full_pci_id(pf_id) pf_info["pf_id"] = full_id pf_info["occupied"] = False d_link = os.path.join("/sys/bus/pci/devices", full_id) txt = utils.system_output("ls %s" % d_link) re_vfn = "(virtfn[0-9])" paths = re.findall(re_vfn, txt) for path in paths: f_path = os.path.join(d_link, path) vf_id = os.path.basename(os.path.realpath(f_path)) vf_ids.append(vf_id) pf_info["vf_ids"] = vf_ids pf_vf_dict.append(pf_info) if_out = utils.system_output("ifconfig -a") re_ethname = "\w+(?=: flags)|eth[0-9](?=\s*Link)" ethnames = re.findall(re_ethname, if_out) for eth in ethnames: cmd = "ethtool -i %s | awk '/bus-info/ {print $2}'" % eth pci_id = utils.system_output(cmd) if not pci_id: continue for pf in pf_vf_dict: if pci_id in pf["pf_id"]: pf["ethname"] = eth return pf_vf_dict
def _release_dev(self, pci_id): """ Release a single PCI device. :param pci_id: PCI ID of a given PCI device. :type pci_id: string :return: True if successfully release the device. else false. :rtype: bool """ base_dir = "/sys/bus/pci" short_id = pci_id[5:] vendor_id = utils_misc.get_vendor_from_pci_id(short_id) drv_path = os.path.join(base_dir, "devices/%s/driver" % pci_id) if self.device_driver in os.readlink(drv_path): error.context("Release device %s to host" % pci_id, logging.info) driver = self.dev_unbind_drivers[pci_id] cmd = "echo '%s' > %s/new_id" % (vendor_id, driver) logging.info("Run command in host: %s" % cmd) try: output = utils.system_output(cmd, timeout=60) except error.CmdError: msg = "Command %s fail with output %s" % (cmd, output) logging.error(msg) return False stub_path = os.path.join(base_dir, "drivers/%s" % self.device_driver) cmd = "echo '%s' > %s/unbind" % (pci_id, stub_path) logging.info("Run command in host: %s" % cmd) try: output = utils.system_output(cmd, timeout=60) except error.CmdError: msg = "Command %s fail with output %s" % (cmd, output) logging.error(msg) return False driver = self.dev_unbind_drivers[pci_id] cmd = "echo '%s' > %s/bind" % (pci_id, driver) logging.info("Run command in host: %s" % cmd) try: output = utils.system_output(cmd, timeout=60) except error.CmdError: msg = "Command %s fail with output %s" % (cmd, output) logging.error(msg) return False if self.is_binded_to_stub(pci_id): return False return True
def __init__(self, params, root_dir="/tmp"): os_dep.command("iscsiadm") self.target = params.get("target") self.export_flag = False if params.get("portal_ip"): self.portal_ip = params.get("portal_ip") else: self.portal_ip = utils.system_output("hostname") if params.get("iscsi_thread_id"): self.id = params.get("iscsi_thread_id") else: self.id = utils.generate_random_string(4) self.initiator = params.get("initiator") if params.get("emulated_image"): self.initiator = None os_dep.command("tgtadm") emulated_image = params.get("emulated_image") self.emulated_image = os.path.join(root_dir, emulated_image) self.emulated_id = "" self.emulated_size = params.get("image_size") self.unit = self.emulated_size[-1].upper() self.emulated_size = self.emulated_size[:-1] # maps K,M,G,T => (count, bs) emulated_size = {'K': (1, 1), 'M': (1, 1024), 'G': (1024, 1024), 'T': (1024, 1048576), } if emulated_size.has_key(self.unit): block_size = emulated_size[self.unit][1] size = int(self.emulated_size) * emulated_size[self.unit][0] self.create_cmd = ("dd if=/dev/zero of=%s count=%s bs=%sK" % (self.emulated_image, size, block_size))
def get_image_info(image_name): """ Get the details of the volume(image) from qemu-img info """ qemu_img = utils_misc.get_path(test.bindir, params.get("qemu_img_binary")) if not os.path.exists(qemu_img): raise error.TestError("Binary of 'qemu-img' not found") cmd = "%s info %s" % (qemu_img, image_name) format_vol = utils.system_output(cmd) reg1 = re.compile(r'image:\s+(\S+)') reg2 = re.compile(r'file format:\s+(\S+)') reg3 = re.compile(r'virtual size:\s+(\S+.*)') reg4 = re.compile(r'disk size:\s+(\S+.*)') image_info = {} for line in format_vol.splitlines(): match1 = re.search(reg1, line) match2 = re.search(reg2, line) match3 = re.search(reg3, line) match4 = re.search(reg4, line) if match1 is not None: image_info['name'] = match1.group(1) if match2 is not None: image_info['format'] = match2.group(1) if match3 is not None: image_info['capacity'] = match3.group(1) if match4 is not None: image_info['allocation'] = match4.group(1) return image_info
def probe_cpus(): """ This routine returns a list of cpu devices found under /sys/devices/system/cpu. """ cmd = 'find /sys/devices/system/cpu/ -maxdepth 1 -type d -name cpu*' return utils.system_output(cmd).splitlines()
def create_mirror_port(mirror_port, target_port, direction, ovs): """ Execute ovs port mirror command and check port really in mirror status. :parm mirror_port: port name in ovs in mirror status. :parm target_port: port name in ovs be mirroring. :parm direction: mirror direction, all, only input or output. :parm ovs: ovs port name. """ mirror_cmd = make_mirror_cmd(mirror_port, target_port, direction, ovs) uuid = utils.system_output(mirror_cmd) output = utils.system_output("ovs-vsctl list mirror") if uuid not in output: logging.debug("Create OVS Mirror CMD: %s " % mirror_cmd) logging.debug("Ovs Info: %s " % output) raise error.TestFail("Setup mirorr port failed")
def apply_patch(self, patch): """ Apply a patch to the code base. Patches are expected to be made using level -p1, and taken according to the code base top level. :param patch: Path to the patch file. """ try: utils.system_output("patch -p1 < %s" % patch) except: logging.error("Patch applied incorrectly. Possible causes: ") logging.error("1 - Patch might not be -p1") logging.error("2 - You are not at the top of the autotest tree") logging.error("3 - Patch was made using an older tree") logging.error("4 - Mailer might have messed the patch") sys.exit(1)
def setup_pv(self, vg): """ Create a physical volume devices; :param params["pv_name"]: Physical volume devices path or mount point; :param vg: VolumeGroup object; :return: list of PhysicalVolume object; """ pvs = [] cmd = "lvm pvs -opv_name,pv_size %s %s" % (COMMON_OPTS, vg.name) output = utils.system_output(cmd) for line in output.splitlines(): pv_name, pv_size = line.split() pv = self.get_vol(pv_name, "pvs") if pv is None: pv = PhysicalVolume(pv_name, pv_size) pv.create() self.register(pv) else: logging.info("PhysicalVolume(%s) really exists" % pv_name + "skip to create it") pv.set_vg(vg) pvs.append(pv) else: for pv_name in self.params["pv_name"].split(): pv = PhysicalVolume(pv_name, 0) pv.create() self.register(pv) pv.set_vg(vg) pvs.append(pv) for pv in pvs: pv.set_vg(vg) return pvs
def check_watchdog_support(): """ check the host qemu-kvm support watchdog device Test Step: 1. Send qemu command 'qemu -watchdog ?' 2. Check the watchdog type that the host support. """ qemu_binary = utils_misc.get_qemu_binary(params) watchdog_type_check = params.get( "watchdog_type_check", " -watchdog '?'") qemu_cmd = qemu_binary + watchdog_type_check # check the host support watchdog types. error.context("Checking whether or not the host support WDT '%s'" % watchdog_device_type, logging.info) watchdog_device = utils.system_output("%s 2>&1" % qemu_cmd, retain_output=True) if watchdog_device: if re.findall(watchdog_device_type, watchdog_device, re.I): logging.info("The host support '%s' type watchdog device" % watchdog_device_type) else: raise error.TestFail("Host not support watchdog device type %s " % watchdog_device_type) logging.info("The host support watchdog device type is: '%s'" % watchdog_device) else: raise error.TestFail("No watchdog device support in the host!")
def setup_pv(self, vg): """ Setup physical volume device if exists return it directly; """ pvs = [] emulate_image_file = self.get_emulate_image_name() cmd = "losetup -j %s" % emulate_image_file output = utils.system_output(cmd) try: pv_name = re.findall("(/dev/loop\d+)", output, re.M | re.I)[-1] pv = self.get_vol(pv_name, "pvs") except IndexError: pv = None if pv is None: img_file = self.make_emulate_image() pv_name = self.make_volume(img_file) pv_size = self.params["pv_size"] pv = PhysicalVolume(pv_name, pv_size) pv.create() self.register(pv) else: logging.warn("PhysicalVolume(%s) really exists" % pv_name + "skip to create it") pv.set_vg(vg) pvs.append(pv) return pvs
def listps(num, order): """ Select top 'num' process order by $order from system. :param num: number of process, if 'num' equal '0' list all process :param order: order list by a process attribute(eg, pcpu or vsz ...) :return: list of process """ cmd = 'ps max -o pid,ppid,pcpu,pmem,rss,vsz,lwp,nlwp,lstart,cmd' cmd += ' --sort -%s' % order title = 'Process/Threads details:' ps_info = utils.system_output(cmd, verbose=False).splitlines() if num != 0: cnt, idx = 0, 0 title = 'Top %d process/threads details, ' % num title += 'order by %s:' % order for line in ps_info: idx += 1 if re.match(r'^\s+\d', line): cnt += 1 if cnt > num: break ps_info = ps_info[:idx] ps_info.insert(0, title) return ps_info
def module_is_loaded(module_name): module_name = module_name.replace('-', '_') modules = utils.system_output('/sbin/lsmod').splitlines() for module in modules: if module.startswith(module_name) and module[len(module_name)] == ' ': return True return False
def get_hotplug_nic_ip(vm, nic, session, is_linux_guest=True): def __get_address(): try: index = [ _idx for _idx, _nic in enumerate( vm.virtnet) if _nic == nic][0] return vm.wait_for_get_address(index, timeout=90) except IndexError: raise error.TestError( "Nic '%s' not exists in VM '%s'" % (nic["nic_name"], vm.name)) except (virt_vm.VMIPAddressMissingError, virt_vm.VMAddressVerificationError): renew_ip_address(session, nic["mac"], is_linux_guest) return nic_ip = utils_misc.wait_for(__get_address, timeout=360) if nic_ip: return nic_ip cached_ip = vm.address_cache.get(nic["mac"]) arps = utils.system_output("arp -aen") logging.debug("Can't get IP address:") logging.debug("\tCached IP: %s" % cached_ip) logging.debug("\tARP table: %s" % arps) return None
def module_is_loaded(module_name): module_name = module_name.replace("-", "_") modules = utils.system_output("/sbin/lsmod").splitlines() for module in modules: if module.startswith(module_name) and module[len(module_name)] == " ": return True return False
def run(test, params, env): """ cpuinfo query test: 1). run query cmd. e.g -cpu ?cpuid 2). check the expected info is inclued in the cmd output. 3). raise error if defined info is missing. """ qemu_binary = utils_misc.get_qemu_binary(params) error.context("run query cmd") qcmd = params.get("query_cmd") if qcmd is None: raise error.TestError("query cmd is missing," "pls check query_cmd in config file") cmd = qemu_binary + qcmd output = utils.system_output(cmd) error.context("check if expected info is included in output of %s " % cmd) cpuinfos = params.get("cpu_info", "Conroe").split(",") missing = [] for cpuinfo in cpuinfos: if cpuinfo not in output: missing.append(cpuinfo) if missing: raise error.TestFail("%s is missing in the output\n %s" % (", ".join(missing), output))
def start_netperf_client(i=0): logging.info("Netperf_%s" % i) try: netperf_output = utils.system_output(netperf_cmd) open("Netperf_%s" % i, "w").write(netperf_output) except OSError: pass
def check_for_kernel_feature(feature): config = running_config() if not config: raise TypeError("Can't find kernel config file") if magic.guess_type(config) == 'application/x-gzip': grep = 'zgrep' else: grep = 'grep' grep += ' ^CONFIG_%s= %s' % (feature, config) if not utils.system_output(grep, ignore_status=True): raise ValueError("Kernel doesn't have a %s feature" % (feature))
def iscsi_get_sessions(): """ Get the iscsi sessions activated """ cmd = "iscsiadm --mode session" output = utils.system_output(cmd, ignore_status=True) sessions = [] if "No active sessions" not in output: for session in output.splitlines(): ip_addr = session.split()[2].split(',')[0] target = session.split()[3] sessions.append((ip_addr, target)) return sessions
def get_target_account_info(self): """ Get the target account information """ cmd = "tgtadm --lld iscsi --mode target --op show" target_info = utils.system_output(cmd) pattern = r"Target\s+\d:\s+%s" % self.target pattern += ".*Account information:\s(.*)ACL information" try: target_account = re.findall(pattern, target_info, re.S)[0].strip().splitlines() except IndexError: target_account = [] return map(str.strip, target_account)
def delete_target(self): """ Delete target from host. """ cmd = "tgtadm --lld iscsi --mode target --op show" output = utils.system_output(cmd) if re.findall("%s$" % self.target, output, re.M): if self.emulated_id: cmd = "tgtadm --lld iscsi --mode target --op delete " cmd += "--tid %s" % self.emulated_id utils.system(cmd) if self.restart_tgtd: cmd = "service tgtd restart" utils.system(cmd)
def add_rpc_insecure(filepath): """ Allow glusterd RPC authority insecure """ cmd = "cat %s" % filepath content = utils.system_output(cmd) match = re.findall(r'rpc-auth-allow-insecure on', content) logging.info("match is %s", match) if not match: logging.info("not match") cmd = "sed -i '/end-volume/i \ \ \ \ option rpc-auth-allow-insecure on' %s" % filepath utils.system(cmd) utils.system("service glusterd restart; sleep 2")
def delete_target(self): """ Delete target from host. """ # Delete block if self.device is not None: cmd = "targetcli /backstores/fileio ls" output = utils.system_output(cmd) if re.findall("%s" % self.device, output, re.M): dev_del = ("targetcli /backstores/fileio/ delete %s" % self.device) utils.system(dev_del) # Delete IQN cmd = "targetcli ls /iscsi 1" output = utils.system_output(cmd) if re.findall("%s" % self.target, output, re.M): del_cmd = "targetcli /iscsi delete %s" % self.target utils.system(del_cmd) # Clear all configuration to avoid restoring cmd = "targetcli clearconfig confirm=True" utils.system(cmd)
def get_all_support_flags(): """ Get all supported flags with qemu query cmd. """ qemu_binary = utils_misc.get_qemu_binary(params) cmd = qemu_binary + params.get("query_cmd", " -cpu ?") output = utils.system_output(cmd) flags_re = re.compile(params.get("pattern", "flags:(.*)")) flag_list = flags_re.search(output) flags = [] if flag_list: for flag in flag_list.groups(): flags += flag return set(map(utils_misc.Flag, flags))
def iscsi_login(target_name): """ Login to a target with the target name :param target_name: Name of the target """ cmd = "iscsiadm --mode node --login --targetname %s" % target_name output = utils.system_output(cmd) target_login = "" if "successful" in output: target_login = target_name return target_login
def setupInitSymlink(self): logging.debug('Symlinking init scripts') autodir = os.environ.get('AUTODIR') rc = os.path.join(autodir, 'tools/autotest') if os.path.isfile(rc) and os.path.islink(rc): # nothing to do return # see if system supports event.d versus inittab if os.path.exists('/etc/event.d'): # NB: assuming current runlevel is default initdefault = utils.system_output('/sbin/runlevel').split()[1] elif os.path.exists('/etc/inittab'): initdefault = utils.system_output( 'grep :initdefault: /etc/inittab') initdefault = initdefault.split(':')[1] else: initdefault = '2' try: utils.system('ln -sf %s /etc/init.d/autotest' % rc) utils.system('ln -sf %s /etc/rc%s.d/S99autotest' % (rc, initdefault)) logging.debug('Labeling init scripts with unconfined_exec_t') utils.system( 'chcon -h system_u:object_r:unconfined_exec_t:s0 /etc/init.d/autotest' ) utils.system( 'chcon -h system_u:object_r:unconfined_exec_t:s0 /etc/rc%s.d/S99autotest' % initdefault) autotest_init = os.path.join(autodir, 'tools/autotest') ret = os.system('chcon system_u:object_r:unconfined_exec_t:s0 %s' % autotest_init) logging.debug('chcon returned <%s>', ret) except: logging.warning('Linking init scripts failed')
def rebase_test(cmd): """ Subcommand 'qemu-img rebase' test Change the backing file of a snapshot image in "unsafe mode": Assume the previous backing file had missed and we just have to change reference of snapshot to new one. After change the backing file of a snapshot image in unsafe mode, the snapshot should work still. :param cmd: qemu-img base command. """ if 'rebase' not in utils.system_output(cmd + ' --help', ignore_status=True): raise error.TestNAError("Current kvm user space version does not" " support 'rebase' subcommand") sn_fmt = params.get("snapshot_format", "qcow2") sn1 = params["image_name_snapshot1"] sn1 = _get_image_filename(sn1, enable_gluster, sn_fmt) base_img = storage.get_image_filename(params, data_dir.get_data_dir()) _create(cmd, sn1, sn_fmt, base_img=base_img, base_img_fmt=image_format) # Create snapshot2 based on snapshot1 sn2 = params["image_name_snapshot2"] sn2 = _get_image_filename(sn2, enable_gluster, sn_fmt) _create(cmd, sn2, sn_fmt, base_img=sn1, base_img_fmt=sn_fmt) rebase_mode = params.get("rebase_mode") if rebase_mode == "unsafe": remove(sn1) _rebase(cmd, sn2, base_img, image_format, mode=rebase_mode) # Boot snapshot image after rebase img_format = sn2.split('.')[-1] img_name = ".".join(sn2.split('.')[:-1]) _boot(img_name, img_format) # Check sn2's format and backing_file actual_base_img = _info(cmd, sn2, "backing file") base_img_name = os.path.basename(base_img) if base_img_name not in actual_base_img: raise error.TestFail("After rebase the backing_file of 'sn2' is " "'%s' which is not expected as '%s'" % (actual_base_img, base_img_name)) status, output = _check(cmd, sn2) if not status: raise error.TestFail("Check image '%s' failed after rebase;" "got error: %s" % (sn2, output)) remove(sn2) remove(sn1)
def rbd_image_map(ceph_monitor, rbd_pool_name, rbd_image_name): """ Maps the specified image to a block device via rbd kernel module :params ceph_monitor: The specified monitor to connect to :params rbd_pool_name: The name of rbd pool :params rbd_image_name: The name of rbd image """ cmd = "rbd map %s --pool %s -m %s" % (rbd_image_name, rbd_pool_name, ceph_monitor) output = utils.system_output(cmd, verbose=True) if os.path.exist(os.path.join("/dev/rbd", rbd_pool_name, rbd_image_name)): return os.path.join("/dev/rbd", rbd_pool_name, rbd_image_name) else: logging.debug("Failed to map image to local: %s" % output) return None
def get_backingfile(self, method="monitor"): """ return backingfile of the device, if not return None; """ if method == "monitor": return self.vm.monitor.get_backingfile(self.device) qemu_img = utils_misc.get_qemu_img_binary(self.params) cmd = "%s info %s " % (qemu_img, self.get_image_file()) info = utils.system_output(cmd) try: matched = re.search(r"backing file: +(.*)", info, re.M) return matched.group(1) except AttributeError: logging.warn("No backingfile found, cmd output: %s" % info)
def iscsi_discover(portal_ip): """ Query from iscsi server for available targets :param portal_ip: Ip for iscsi server """ cmd = "iscsiadm -m discovery -t sendtargets -p %s" % portal_ip output = utils.system_output(cmd, ignore_status=True) session = "" if "Invalid" in output: logging.debug(output) else: session = output return session
def rebase_test(cmd): """ Subcommand 'qemu-img rebase' test Change the backing file of a snapshot image in "unsafe mode": Assume the previous backing file had missed and we just have to change reference of snapshot to new one. After change the backing file of a snapshot image in unsafe mode, the snapshot should work still. @param cmd: qemu-img base command. """ if not 'rebase' in utils.system_output(cmd + ' --help', ignore_status=True): raise error.TestNAError("Current kvm user space version does not" " support 'rebase' subcommand") sn_fmt = params.get("snapshot_format", "qcow2") sn1 = params.get("image_name_snapshot1") sn1 = virt_utils.get_path(test.bindir, sn1) + ".%s" % sn_fmt base_img = virt_storage.get_image_filename(params, test.bindir) _create(cmd, sn1, sn_fmt, base_img=base_img, base_img_fmt=image_format) # Create snapshot2 based on snapshot1 sn2 = params.get("image_name_snapshot2") sn2 = virt_utils.get_path(test.bindir, sn2) + ".%s" % sn_fmt _create(cmd, sn2, sn_fmt, base_img=sn1, base_img_fmt=sn_fmt) rebase_mode = params.get("rebase_mode") if rebase_mode == "unsafe": os.remove(sn1) _rebase(cmd, sn2, base_img, image_format, mode=rebase_mode) # Check sn2's format and backing_file actual_base_img = _info(cmd, sn2, "backing file") base_img_name = os.path.basename(params.get("image_name")) if not base_img_name in actual_base_img: raise error.TestFail("After rebase the backing_file of 'sn2' is " "'%s' which is not expected as '%s'" % (actual_base_img, base_img_name)) s, o = _check(cmd, sn2) if not s: raise error.TestFail("Check image '%s' failed after rebase;" "got error: %s" % (sn2, o)) try: os.remove(sn2) os.remove(sn1) except Exception: pass
def __reload_vgs(self): """ Create VolumeGroup objects for exist volumegroups; :return: list of Volume object """ vgs = [] cmd = "lvm vgs -opv_name,vg_name,vg_size %s" % COMMON_OPTS output = utils.system_output(cmd) for line in output.splitlines(): pv_name, vg_name, vg_size = line.split() pv = self.get_vol(pv_name, "pvs") vg = VolumeGroup(vg_name, vg_size, [pv]) pv.set_vg(vg) vgs.append(vg) return vgs
def iscsi_login(target_name, portal): """ Login to a target with the target name :param target_name: Name of the target :params portal: Hostname/Ip for iscsi server """ cmd = "iscsiadm --mode node --login --targetname %s" % target_name cmd += " --portal %s" % portal output = utils.system_output(cmd) target_login = "" if "successful" in output: target_login = target_name return target_login
def _check(cmd, img): """ Simple 'qemu-img check' function implementation. @param cmd: qemu-img base command. @param img: image to be checked """ cmd += " check %s" % img logging.info("Checking image '%s'...", img) try: output = utils.system_output(cmd) except error.CmdError, e: if "does not support checks" in str(e): return (True, "") else: return (False, str(e))
def ifup_down_interface(interface, action="up"): check_cmd = "ifconfig %s" % interface status = utils.system_output(check_cmd) if action == "up": if not check_network_interface_ip(interface): if "UP" in status.splitlines()[0]: utils.system("ifdown %s" % interface, timeout=120, ignore_status=True) utils.system("ifup %s" % interface, timeout=120, ignore_status=True) elif action == "down": if "UP" in status.splitlines()[0]: utils.system("ifdown %s" % interface, timeout=120, ignore_status=True) else: msg = "Unsupport action '%s' on network interface." % action raise error.TestError(msg)
def __reload_lvs(self): """ Create LogicalVolume objects for exist Logical volumes; :return: list of Volume object """ lvs = [] cmd = "lvm lvs -o lv_name,lv_size,vg_name %s" % COMMON_OPTS output = utils.system_output(cmd) for line in output.splitlines(): lv_name, lv_size, vg_name = line.split() vg = self.get_vol(vg_name, "vgs") lv = LogicalVolume(lv_name, lv_size, vg) vg.append_lv(lv) lvs.append(lv) return lvs
def _check(cmd, img): """ Simple 'qemu-img check' function implementation. :param cmd: qemu-img base command. :param img: image to be checked """ cmd += " check %s" % img error.context("Checking image '%s' by command '%s'" % (img, cmd), logging.info) try: output = utils.system_output(cmd, verbose=False) except error.CmdError, err: if "does not support checks" in str(err): return (True, "") else: return (False, str(err))
def get_target_id(self): """ Get target id from image name. Only works for emulated iscsi device """ cmd = "tgtadm --lld iscsi --mode target --op show" target_info = utils.system_output(cmd) target_id = "" for line in re.split("\n", target_info): if re.findall("Target\s+(\d+)", line): target_id = re.findall("Target\s+(\d+)", line)[0] if re.findall("Backing store path:\s+(/+.+)", line): if self.emulated_image in line: break else: target_id = "" return target_id
def get_epoch_seconds(self, session): """ Get epoch seconds from host and guest; """ regex = r"epoch:\s+(\d+)" host_epoch_time_cmd = self.params["host_epoch_time_cmd"] guest_epoch_time_cmd = self.params["guest_epoch_time_cmd"] try: guest_timestr = session.cmd_output(guest_epoch_time_cmd, timeout=120) host_timestr = utils.system_output(host_epoch_time_cmd) epoch_host, epoch_guest = map(lambda x: re.findall(regex, x)[0], [host_timestr, guest_timestr]) except IndexError: logging.debug("Host Time: %s," % guest_timestr + "Guest Time: %s" % guest_timestr) return map(float, [epoch_host, epoch_guest])
def cleanup(self): brctl_output = utils.system_output("brctl show") cleanup = False for line in brctl_output.split("\n"): if line.startswith(self.brname): # len == 4 means there is a TAP using the bridge # so don't try to clean it up if len(line.split()) < 4: cleanup = True break if cleanup: logging.debug( "Cleaning up KVM test private bridge %s", self.brname) self._stop_dhcp_server() self._disable_nat() self._bring_bridge_down() self._remove_bridge()
def get_vf_status(self, vf_id): """ Check whether one vf is assigned to VM. vf_id: vf id to check. @return: Return True if vf has already assinged to VM. Else return false. """ base_dir = "/sys/bus/pci" tub_path = os.path.join(base_dir, "drivers/pci-stub") vf_res_path = os.path.join(tub_path, "0000\:%s/resource*" % vf_id) cmd = "lsof %s" % vf_res_path output = utils.system_output(cmd, timeout=60, ignore_status=True) if 'qemu' in output: return True else: return False
def get_device_name(self): """ Get device name from the target name. """ cmd = "iscsiadm -m session -P 3" device_name = "" if self.logged_in(): output = utils.system_output(cmd) pattern = r"Target:\s+%s.*?disk\s(\w+)\s+\S+\srunning" % self.target device_name = re.findall(pattern, output, re.S) try: device_name = "/dev/%s" % device_name[0] except IndexError: logging.error("Can not find target '%s' after login.", self.target) else: logging.error("Session is not logged in yet.") return device_name
def get_device_name(self): """ Get device name from the target name. """ cmd = "iscsiadm -m session -P 3" device_name = "" if self.logged_in(): output = utils.system_output(cmd) pattern = r"Target:\s+%s\n.*?disk\s+(\w+)\s+" % self.target if re.findall(pattern, output, re.S): device_name = re.findall(pattern, output, re.S)[0] device_name = "/dev/%s" % device_name else: logging.debug("Can not find taget after login") else: logging.debug("Session is not login yet.") return device_name
def get_hwclock_seconds(utc=True): """ Return the hardware clock in seconds as a floating point value. Use Coordinated Universal Time if utc is True, local time otherwise. Raise a ValueError if unable to read the hardware clock. """ cmd = '/sbin/hwclock --debug' if utc: cmd += ' --utc' hwclock_output = utils.system_output(cmd, ignore_status=True) match = re.search(r'= ([0-9]+) seconds since .+ (-?[0-9.]+) seconds$', hwclock_output, re.DOTALL) if match: seconds = int(match.group(1)) + float(match.group(2)) logging.debug('hwclock seconds = %f' % seconds) return seconds raise ValueError('Unable to read the hardware clock -- ' + hwclock_output)
def iscsi_logout(target_name=None): """ Logout from a target. If the target name is not set then logout all targets. :params target_name: Name of the target. """ if target_name: cmd = "iscsiadm --mode node --logout -T %s" % target_name else: cmd = "iscsiadm --mode node --logout all" output = utils.system_output(cmd) target_logout = "" if "successful" in output: target_logout = target_name return target_logout
def get_same_group_devs(self, pci_id): """ Get the device that in same iommu group. :param pci_id: Device's pci_id :type pci_id: string :return: Return the device's pci id that in same group with pci_id. :rtype: List of string. """ pci_ids = [] base_dir = "/sys/bus/pci/devices" devices_link = os.path.join(base_dir, "%s/iommu_group/devices/" % pci_id) out = utils.system_output("ls %s" % devices_link) if out: pci_ids = out.split() return pci_ids
def get_backingfile(self, method="monitor"): """ return backingfile of the device, if not return None; """ backing_file = None if method == "monitor": backing_file = self.vm.monitor.get_backingfile(self.device) else: cmd = utils_misc.get_qemu_img_binary(self.params) image_file = self.get_image_file() cmd += " info %s " % image_file info = utils.system_output(cmd) matched = re.search(r"backing file: +(.*)", info, re.M) if matched: backing_file = matched.group(1) if backing_file: backing_file = os.path.abspath(backing_file) return backing_file