def test_and_set_nested(host, timeout=600): """ Verifies that the host has nested virtualization set for kvm module :param host: :param timeout: :return: ProcessResult """ cmd = "cat /sys/module/kvm_intel/parameters/nested" res = Command(cmd, host=host)(showout=False) if res.output.strip() != "Y": # Reboot the masters machine glob_logger.info("rebooting {} to set nested param".format(host)) rebooter(host, timeout=timeout) time.sleep(45) # Fudge factor here... pinger(host, timeout=timeout) # After reboot, make sure nested support is there path = "/sys/module/kvm_intel/parameters/nested" cmd = "cat {}".format(path) try: res = Command(cmd, host=host)(showout=False) if res.output.strip() != "Y": glob_logger.error("{}={}".format(path, res.output.strip())) raise sce.ConfigException("Nested support still not enabled") except CommandException: raise sce.ConfigException("Nested support still not enabled") return res
def verify_modprobe(host, proc_type="intel", set=False): """ Checks if nested option is set for kvm_intel|amd, and optionally set it in /etc/modprobe.d/dist.conf :param host: (str) IP address of host :param proc_type: one of 'intel' or 'amd' :param set: (bool) chose to create dist.conf or not :return: ProcessResult of setting dist.conf or match from regex """ line = "options kvm_{} nested=y".format(proc_type) def set_options(cfg): cmd = "echo {} >> /etc/modprobe.d/dist.conf".format(cfg) res = Command(cmd, host=host)(showout=False) return res == 0 # /etc/modprobe.d/dist.conf may not exist try: res = Command("cat /etc/modprobe.d/dist.conf", host=host)(showout=False) except CommandException: r = False if set: r = set_options(line) return r else: patt = re.compile(line) m = patt.search(res.output) if not set or (set and m): return m return set_options(line)
def rebooter(host, timeout=360): """ Reboots a machine and pings the host periodically until it is up :param host: IP address of machine to reboot :param timeout: timeout in seconds :return: None """ res = Command("reboot", host=host)(throws=False) # Wait until pings are unsuccessful start_time = time.time() end_time = start_time + timeout while True: pinger = Command("ping -W 4 -c 4 {}".format(host), stderr=PIPE) result = pinger(showout=False, throws=False) if result != 0: break if time.time() > end_time: raise Exception("Machine did not reboot")
def check_kvm_file(host): """ Checks of the /dev/kvm special file exists on host :param host: (str) IP address of machine :return: ProcessResult object (or throws ConfigException) """ glob_logger.info("Checking /dev/kvm") res = Command("file /dev/kvm", host=host)() if "cannot open" in res.output: raise sce.ConfigException("no /dev/kvm on {}".format(host)) return res
def set_host_model(hyper_ip, dom_name, user="******"): """ Can be used as fn arg to set_nested_vm_support :param hyper_ip: the IP address of hypervisor machine :param dom_name: the libvirt domain name :param user: user to connect to libvirt as :return: ProcessResult of executing virt-xml command """ glob_logger.info("Setting host_model mode for {}".format(dom_name)) cmd = "virt-xml --connect=qemu+ssh://{}@{}/system {} --edit --cpu " \ "host-model-only,+vmx" cmd = cmd.format(user, hyper_ip, dom_name) return Command(cmd)()
def test_and_set_kvm_module(bare_m): """ Checks if kvm_intel|amd is loaded. if not will try to modprobe it :param bare_m: address of baremetal machine :return: (str) cpu type intel or amd """ p = verify_nested_kvm(bare_m) if p is None: # kvm is not loaded Command("modprobe kvm", host=bare_m)() p = verify_nested_kvm(bare_m) if p is None: raise sce.ConfigException("Can not load kvm module") return p
def set_host_passthrough(hyper_ip, dom_name, user="******"): """ Sets a domain'nova_tests <cpu> element to use mode host-passthrough :param hyper_ip: (str) IP address of host with hypervisor :param dom_name: (str) the libvirt domain name :param user: (str) user to connect to libvirt hypervisor :return: ProcessTresult """ # FIXME: How do we do this just using libvirt? This adds a dependency # on virt-xml # Edit the domain'nova_tests xml to use host-passthrough mode glob_logger.info("Setting host-passthrough mode for {}".format(dom_name)) cmd = "virt-xml --connect=qemu+ssh://{}@{}/system {} --edit --cpu " \ "host-passthrough,clearxml=yes".format(user, hyper_ip, dom_name) res = Command(cmd)() return res
def pinger(host, timeout=300): """ :param host: IP address of machine to ping to :param timeout: timeout in seconds :return: None """ ping = Command("ping -W 4 -c 4 {}".format(host), stderr=PIPE) start_time = time.time() end_time = start_time + timeout while True: glob_logger.info("waiting for {} to come back up...".format(host)) res = ping(showout=False, throws=False) if res == 0: glob_logger.info("{} is back up".format(host)) break time.sleep(10) if time.time() > end_time: err = "Machine did not come back after {} seconds".format(timeout) raise Exception(err)
def verify_nested_kvm(host): """ Goes through loaded modules to see if kvm_intel or kvm_amd is loaded :param host: (str) IP Address of host :return: The CPU type (str) intel or amd """ glob_logger.info("Checking is kvm and kvm-intel or kvm-amd is running...") lsmod = Command("lsmod", host=host)(showout=False) patt = re.compile(r"kvm_(intel|amd)") out = lsmod.output for line in lsmod.output.split("\n")[1:]: m = patt.search(line) if m: proc = m.groups()[0] break else: raise sce.ConfigException("kvm module is not loaded") return proc
def set_options(cfg): cmd = "echo {} >> /etc/modprobe.d/dist.conf".format(cfg) res = Command(cmd, host=host)(showout=False) return res == 0