Beispiel #1
0
def unmap_url_cache(cachedir, url, expected_hash, method="md5"):
    """
    Downloads a file from a URL to a cache directory. If the file is already
    at the expected position and has the expected hash, let's not download it
    again.

    :param cachedir: Directory that might hold a copy of the file we want to
            download.
    :param url: URL for the file we want to download.
    :param expected_hash: Hash string that we expect the file downloaded to
            have.
    :param method: Method used to calculate the hash string (md5, sha1).
    """
    # Let's convert cachedir to a canonical path, if it's not already
    cachedir = os.path.realpath(cachedir)
    if not os.path.isdir(cachedir):
        try:
            os.makedirs(cachedir)
        except Exception:
            raise ValueError("Could not create cache directory %s" % cachedir)
    file_from_url = os.path.basename(url)
    file_local_path = os.path.join(cachedir, file_from_url)

    file_hash = None
    failure_counter = 0
    while not file_hash == expected_hash:
        if os.path.isfile(file_local_path):
            file_hash = hash_file(file_local_path, method)
            if file_hash == expected_hash:
                # File is already at the expected position and ready to go
                src = file_from_url
            else:
                # Let's download the package again, it's corrupted...
                logging.error("Seems that file %s is corrupted, trying to " "download it again" % file_from_url)
                src = url
                failure_counter += 1
        else:
            # File is not there, let's download it
            src = url
        if failure_counter > 1:
            raise EnvironmentError(
                "Consistently failed to download the "
                "package %s. Aborting further download "
                "attempts. This might mean either the "
                "network connection has problems or the "
                "expected hash string that was determined "
                "for this file is wrong" % file_from_url
            )
        file_path = utils.unmap_url(cachedir, src, cachedir)

    return file_path
Beispiel #2
0
def unmap_url_cache(cachedir, url, expected_hash, method="md5"):
    """
    Downloads a file from a URL to a cache directory. If the file is already
    at the expected position and has the expected hash, let's not download it
    again.

    @param cachedir: Directory that might hold a copy of the file we want to
            download.
    @param url: URL for the file we want to download.
    @param expected_hash: Hash string that we expect the file downloaded to
            have.
    @param method: Method used to calculate the hash string (md5, sha1).
    """
    # Let's convert cachedir to a canonical path, if it's not already
    cachedir = os.path.realpath(cachedir)
    if not os.path.isdir(cachedir):
        try:
            os.makedirs(cachedir)
        except Exception:
            raise ValueError('Could not create cache directory %s' % cachedir)
    file_from_url = os.path.basename(url)
    file_local_path = os.path.join(cachedir, file_from_url)

    file_hash = None
    failure_counter = 0
    while not file_hash == expected_hash:
        if os.path.isfile(file_local_path):
            file_hash = hash_file(file_local_path, method)
            if file_hash == expected_hash:
                # File is already at the expected position and ready to go
                src = file_from_url
            else:
                # Let's download the package again, it's corrupted...
                logging.error("Seems that file %s is corrupted, trying to "
                              "download it again" % file_from_url)
                src = url
                failure_counter += 1
        else:
            # File is not there, let's download it
            src = url
        if failure_counter > 1:
            raise EnvironmentError("Consistently failed to download the "
                                   "package %s. Aborting further download "
                                   "attempts. This might mean either the "
                                   "network connection has problems or the "
                                   "expected hash string that was determined "
                                   "for this file is wrong" % file_from_url)
        file_path = utils.unmap_url(cachedir, src, cachedir)

    return file_path
Beispiel #3
0
def run_win_virtio_update(test, params, env):
    """
    Update virtio driver:
    1) Boot up guest with default devices and virtio_win iso
    2) Install virtio driver
    3) Check dirver info

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def check_cdrom(timeout):
        cdrom_chk_cmd = "echo list volume > cmd && echo exit >>"
        cdrom_chk_cmd += " cmd && diskpart /s cmd"
        vols = []
        start_time = time.time()
        while time.time() - start_time < timeout:
            vols_str = session.cmd(cdrom_chk_cmd)

            if len(re.findall("CDFS.*Healthy", vols_str)) >= cdrom_num:
                vols = re.findall(".*CDFS.*?Healthy.*\n", vols_str)
                break
        return vols

    if params.get("case_type") == "driver_install":
        error.context("Update the device type to default.", logging.info)
        default_drive_format = params.get("default_drive_format", "ide")
        default_nic_model = params.get("default_nic_model", "rtl8139")
        images = params.get("images")
        main_image = re.split("\s+", images)[0]
        nics = params.get("nics")
        main_nic = re.split("\s+", nics)[0]
        default_display = params.get("default_display", "vnc")

        default_parameters = {"default_drive_format": default_drive_format,
                              "default_nic_model": default_nic_model,
                              "default_display": default_display}

        for key in default_parameters:
            params[key[8:]] = default_parameters[key]

    if params.get("prewhql_install") == "yes":
        error.context("Prepare the prewhql virtio_win driver iso")
        url_virtio_win = params.get("url_virtio_win")
        if os.path.isdir("/tmp/virtio_win"):
            utils.system("rm -rf /tmp/virtio_win")
        utils.system("mkdir /tmp/virtio_win")

        virtio_win = utils.unmap_url("/tmp/virtio_win", url_virtio_win,
                                     "/tmp/virtio_win")
        if re.findall("zip$", url_virtio_win):
            utils.system("cd /tmp/virtio_win; unzip *; rm -f *.zip")

        virtio_version = params.get("virtio_version", "unknown")
        virtio_iso = params.get("cdrom_virtio", "/tmp/prewhql.iso")
        utils.system("mkisofs -J -o %s /tmp/virtio_win" % virtio_iso)

    drivers_install = re.split(";", params.get("drivers_install"))

    timeout = float(params.get("login_timeout", 240))

    install_cmds = {}
    check_str = {}
    check_cmds = {}
    op_cmds = {}
    setup_ps = False

    error.context("Fill up driver install command line", logging.info)
    for driver in drivers_install:
        params_driver = params.object_params(driver)
        mount_point = params_driver.get("mount_point")
        storage_path = params_driver.get("cdrom_virtio")
        re_hw_id = params_driver.get("re_hw_id", "(PCI.{14,50})\r\n")
        driver_install_cmd = params_driver.get("driver_install_cmd")
        if "hwidcmd" in driver_install_cmd:
            pattern_drive = params.get("pattern_drive",
                                       "\s+\w:(.[^\s]+)\s+hwidcmd")
            driver_path = re.findall(pattern_drive, driver_install_cmd)[0]
            driver_path = "/".join(driver_path.split("\\\\")[1:])
            storage_path = utils_misc.get_path(
                data_dir.get_data_dir(), storage_path)
            hw_id = utils_test.get_driver_hardware_id(driver_path,
                                                      mount_point=mount_point,
                                                      storage_path=storage_path,
                                                      re_hw_id=re_hw_id)
            install_cmds[driver] = re.sub("hwidcmd", hw_id,
                                          driver_install_cmd)
        else:
            install_cmds[driver] = driver_install_cmd

        check_str[driver] = params_driver.get("check_str")
        check_cmds[driver] = params_driver.get("check_cmd")
        op_cmds[driver] = params_driver.get("op_cmd")

        if "pecheck.py" in check_cmds[driver]:
            setup_ps = True

        if params.get("check_info") == "yes":
            mount_point = params.get("virtio_mount_point",
                                     "/tmp/virtio_win")
            iso_path = utils_misc.get_path(data_dir.get_data_dir(),
                                           params.get("cdrom_virtio"))
            utils.system("mount -o loop %s %s" % (iso_path, mount_point))
            pattern_driver = params_driver.get("pattern_driver")
            driver_path = re.findall(pattern_driver,
                                     driver_install_cmd)[0]
            driver_path = "/".join(driver_path.split("\\\\")[1:])
            storage_path = utils_misc.get_path(mount_point, driver_path)
            storage_path = os.path.dirname(storage_path)
            files = " ".join(os.listdir(storage_path))
            file_name = re.findall("\s+(.*?\.inf)", files)
            if file_name:
                file_name = utils_misc.get_path(storage_path,
                                                file_name[0])
            else:
                raise error.TestError("Can not find .inf file.")
            inf = open(file_name)
            inf_context = inf.read()
            inf.close()
            utils.system("umount %s" % mount_point)
            patterns_check_str = params_driver.get("check_str")
            check_str[driver] = {}
            for i in patterns_check_str.split(";"):
                check_n, check_p = i.split("::")
                check_str[driver][check_n] = re.findall(check_p,
                                                        inf_context)[0]
            check_cmds[driver] = {}
            for i in params_driver.get("check_cmd").split(";"):
                cmd_n, cmd_c = i.split("::")
                cmd_c = re.sub("DRIVER_PATH",
                               params_driver.get("sys_file_path", ""),
                               cmd_c)
                cmd_c = re.sub("DRIVER_PATTERN_%s" % cmd_n,
                               params_driver.get("info_pattern_%s" % cmd_n,
                                                 ""),
                               cmd_c)
                check_cmds[driver][cmd_n] = cmd_c

    error.context("Boot up guest with setup parameters", logging.info)
    vm = "vm1"
    vm_params = params.copy()
    env_process.preprocess_vm(test, vm_params, env, vm)
    vm = env.get_vm(vm)
    session = vm.wait_for_login(timeout=timeout)

    cdroms = params.get("cdroms")
    cdrom_num = len(re.split("\s+", cdroms.strip()))
    init_timeout = int(params.get("init_timeout", "60"))

    error.context("Check the cdrom is available")
    volumes = check_cdrom(init_timeout)
    vol_info = []
    for volume in volumes:
        vol_info += re.findall("Volume\s+\d+\s+(\w).*?(\d+)\s+\w+", volume)
    if len(volumes) > 1:
        if int(vol_info[0][1]) > int(vol_info[1][1]):
            vol_utils = vol_info[0][0]
            vol_virtio = vol_info[1][0]
        else:
            vol_utils = vol_info[1][0]
            vol_virtio = vol_info[0][0]
    else:
        vol_utils = vol_info[0][0]

    error.context("Install drivers", logging.info)
    for driver in drivers_install:
        if params.get("kill_rundll", "no") == "yes":
            kill_cmd = 'tasklist | find /I "rundll32"'
            status, tasks = session.cmd_status_output(kill_cmd)
            if status == 0:
                for i in re.findall("rundll32.*?(\d+)", tasks):
                    session.cmd('taskkill /PID %s' % i)
        if install_cmds:
            cmd = re.sub("WIN_UTILS", vol_utils, install_cmds[driver])
            cmd = re.sub("WIN_VIRTIO", vol_virtio, cmd)
            session.cmd(cmd)
            session = vm.reboot(session)

    if params.get("check_info") == "yes":
        fail_log = "Details check failed in guest."
        fail_log += " Please check the error_log. "
    else:
        fail_log = "Failed to install:"
    error_log = open("%s/error_log" % test.resultsdir, "w")
    fail_flag = False
    error.context("Check driver available in guest", logging.info)
    if setup_ps:
        setup_cmd = params.get("python_scripts")
        session.cmd(setup_cmd)

    for driver in drivers_install:
        error_log.write("For driver %s:\n" % driver)
        if isinstance(check_str[driver], dict):
            for i in check_str[driver]:
                output = session.cmd(check_cmds[driver][i])
                if not re.findall(check_str[driver][i], output, re.I):
                    fail_flag = True
                    fail_log += " %s" % driver
                    fail_log += "(%s) is not right; " % i
                    error_log.write("inf:\t%s\n" % check_str[driver][i])
                    error_log.write("sys: \t%s\n" % output)
        else:
            output = session.cmd(check_cmds[driver])
            if not re.findall(check_str[driver], output, re.I):
                fail_falg = True
                fail_log += " %s" % driver
                error_log.write("Check command output: %s\n" % output)

    if fail_flag:
        raise error.TestFail(fail_log)

    if check_cmds:
        error.context("Do more operates in guest to check the driver",
                      logging.info)
        for driver in drivers_install:
            if isinstance(check_cmds[driver], str):
                session.cmd(cmd)
            else:
                for cmd in check_cmds[driver]:
                    session.cmd(cmd)
Beispiel #4
0
def run_win_virtio_update(test, params, env):
    """
    Update virtio driver:
    1) Boot up guest with default devices and virtio_win iso
    2) Install virtio driver
    3) Check dirver info

    @param test: QEMU test object
    @param params: Dictionary with the test parameters
    @param env: Dictionary with test environment.
    """
    def check_cdrom(timeout):
        cdrom_chk_cmd = "echo list volume > cmd && echo exit >>"
        cdrom_chk_cmd += " cmd && diskpart /s cmd"
        vols = []
        start_time = time.time()
        while time.time() - start_time < timeout:
            vols_str = session.cmd(cdrom_chk_cmd)

            if len(re.findall("CDFS.*Healthy", vols_str)) >= cdrom_num:
                vols = re.findall(".*CDFS.*?Healthy.*\n", vols_str)
                break
        return vols

    if params.get("case_type") == "driver_install":
        error.context("Update the device type to default.", logging.info)
        default_drive_format = params.get("default_drive_format", "ide")
        default_nic_model = params.get("default_nic_model", "rtl8139")
        images = params.get("images")
        main_image = re.split("\s+", images)[0]
        nics = params.get("nics")
        main_nic = re.split("\s+", nics)[0]
        default_display = params.get("default_display", "vnc")

        default_parameters = {"default_drive_format": default_drive_format,
                              "default_nic_model": default_nic_model,
                              "default_display": default_display}

        for key in default_parameters:
            params[key[8:]] = default_parameters[key]

    if params.get("prewhql_install") == "yes":
        error.context("Prepare the prewhql virtio_win driver iso")
        url_virtio_win = params.get("url_virtio_win")
        if os.path.isdir("/tmp/virtio_win"):
            utils.system("rm -rf /tmp/virtio_win")
        utils.system("mkdir /tmp/virtio_win")

        virtio_win = utils.unmap_url("/tmp/virtio_win", url_virtio_win,
                              "/tmp/virtio_win")
        if re.findall("zip$", url_virtio_win):
            utils.system("cd /tmp/virtio_win; unzip *; rm -f *.zip")

        virtio_version = params.get("virtio_version", "unkown")
        virtio_iso = params.get("cdrom_virtio", "/tmp/prewhql.iso")
        utils.system("mkisofs -J -o %s /tmp/virtio_win" % virtio_iso)

    drivers_install = re.split(";", params.get("drivers_install"))

    timeout = float(params.get("login_timeout", 240))

    install_cmds = {}
    check_str = {}
    check_cmds = {}
    op_cmds = {}
    setup_ps = False

    error.context("Fill up driver install command line", logging.info)
    for driver in drivers_install:
        params_driver = params.object_params(driver)
        mount_point = params_driver.get("mount_point")
        storage_path = params_driver.get("cdrom_virtio")
        re_hw_id = params_driver.get("re_hw_id", "(PCI.{14,50})\r\n")
        driver_install_cmd = params_driver.get("driver_install_cmd")
        if "hwidcmd" in driver_install_cmd:
            pattern_drive = params.get("pattern_drive",
                                       "\s+\w:(.[^\s]+)\s+hwidcmd")
            driver_path = re.findall(pattern_drive, driver_install_cmd)[0]
            driver_path = "/".join(driver_path.split("\\\\")[1:])
            storage_path = utils_misc.get_path(data_dir.get_data_dir(), storage_path)
            hw_id = utils_test.get_driver_hardware_id(driver_path,
                                                   mount_point=mount_point,
                                                 storage_path=storage_path,
                                                         re_hw_id=re_hw_id)
            install_cmds[driver] = re.sub("hwidcmd", hw_id,
                                          driver_install_cmd)
        else:
            install_cmds[driver] = driver_install_cmd

        check_str[driver] = params_driver.get("check_str")
        check_cmds[driver] = params_driver.get("check_cmd")
        op_cmds[driver] = params_driver.get("op_cmd")

        if "pecheck.py" in check_cmds[driver]:
            setup_ps = True

        if params.get("check_info") == "yes":
            mount_point = params.get("virtio_mount_point",
                                     "/tmp/virtio_win")
            iso_path = utils_misc.get_path(data_dir.get_data_dir(),
                                           params.get("cdrom_virtio"))
            utils.system("mount -o loop %s %s" % (iso_path, mount_point))
            pattern_driver = params_driver.get("pattern_driver")
            driver_path = re.findall(pattern_driver,
                                     driver_install_cmd)[0]
            driver_path = "/".join(driver_path.split("\\\\")[1:])
            storage_path = utils_misc.get_path(mount_point, driver_path)
            storage_path = os.path.dirname(storage_path)
            files = " ".join(os.listdir(storage_path))
            file_name = re.findall("\s+(.*?\.inf)", files)
            if file_name:
                file_name = utils_misc.get_path(storage_path,
                                                file_name[0])
            else:
                raise error.TestError("Can not find .inf file.")
            inf = open(file_name)
            inf_context = inf.read()
            inf.close()
            utils.system("umount %s" % mount_point)
            patterns_check_str = params_driver.get("check_str")
            check_str[driver] = {}
            for i in patterns_check_str.split(";"):
                check_n, check_p = i.split("::")
                check_str[driver][check_n] = re.findall(check_p,
                                                        inf_context)[0]
            check_cmds[driver] = {}
            for i in params_driver.get("check_cmd").split(";"):
                cmd_n, cmd_c = i.split("::")
                cmd_c = re.sub("DRIVER_PATH",
                               params_driver.get("sys_file_path", ""),
                               cmd_c)
                cmd_c = re.sub("DRIVER_PATTERN_%s" % cmd_n,
                              params_driver.get("info_pattern_%s" % cmd_n,
                                               ""),
                               cmd_c)
                check_cmds[driver][cmd_n] = cmd_c

    error.context("Boot up guest with setup parameters", logging.info)
    vm = "vm1"
    vm_params = params.copy()
    env_process.preprocess_vm(test, vm_params, env, vm)
    vm = env.get_vm(vm)
    session = vm.wait_for_login(timeout=timeout)

    cdroms = params.get("cdroms")
    cdrom_num = len(re.split("\s+", cdroms.strip()))
    init_timeout = int(params.get("init_timeout", "60"))

    error.context("Check the cdrom is available")
    volumes = check_cdrom(init_timeout)
    vol_info = []
    for volume in volumes:
        vol_info += re.findall("Volume\s+\d+\s+(\w).*?(\d+)\s+\w+", volume)
    if len(volumes) > 1:
        if int(vol_info[0][1]) > int(vol_info[1][1]):
            vol_utils = vol_info[0][0]
            vol_virtio = vol_info[1][0]
        else:
            vol_utils = vol_info[1][0]
            vol_virtio = vol_info[0][0]
    else:
        vol_utils = vol_info[0][0]

    error.context("Install drivers", logging.info)
    for driver in drivers_install:
        if params.get("kill_rundll", "no") == "yes":
            kill_cmd = 'tasklist | find /I "rundll32"'
            status, tasks = session.cmd_status_output(kill_cmd)
            if status == 0:
                for i in re.findall("rundll32.*?(\d+)", tasks):
                    session.cmd('taskkill /PID %s' % i)
        if install_cmds:
            cmd = re.sub("WIN_UTILS", vol_utils, install_cmds[driver])
            cmd = re.sub("WIN_VIRTIO", vol_virtio, cmd)
            session.cmd(cmd)
            session = vm.reboot(session)

    if params.get("check_info") == "yes":
        fail_log = "Details check failed in guest."
        fail_log += " Please check the error_log. "
    else:
        fail_log = "Failed to install:"
    error_log = open("%s/error_log" % test.resultsdir, "w")
    fail_flag = False
    error.context("Check driver available in guest", logging.info)
    if setup_ps:
        setup_cmd = params.get("python_scripts")
        session.cmd(setup_cmd)

    for driver in drivers_install:
        error_log.write("For driver %s:\n" % driver)
        if isinstance(check_str[driver], dict):
            for i in check_str[driver]:
                output = session.cmd(check_cmds[driver][i])
                if not re.findall(check_str[driver][i], output, re.I):
                    fail_flag = True
                    fail_log += " %s" % driver
                    fail_log += "(%s) is not right; " % i
                    error_log.write("inf:\t%s\n" % check_str[driver][i])
                    error_log.write("sys: \t%s\n" % output)
        else:
            output = session.cmd(check_cmds[driver])
            if not re.findall(check_str[driver], output, re.I):
                fail_falg = True
                fail_log += " %s" % driver
                error_log.write("Check command output: %s\n" % output)

    if fail_flag:
        raise error.TestFail(fail_log)

    if check_cmds:
        error.context("Do more operates in guest to check the driver",
                      logging.info)
        for driver in drivers_install:
            if isinstance(check_cmds[driver], str):
                session.cmd(cmd)
            else:
                for cmd in check_cmds[driver]:
                    session.cmd(cmd)
Beispiel #5
0
        """
        flags_re = re.compile(r'^flags\s*:(.*)$', re.MULTILINE)
        out = vm_session.cmd_output("cat /proc/cpuinfo")
        try:
            flags = flags_re.search(out).groups()[0].split()
            return set(map(utils_misc.Flag, flags))
        except Exception, e:
            logging.error("Failed to get guest cpu flags %s" % e)

    utils_misc.Flag.aliases = utils_misc.kvm_map_flags_aliases

    # Get all models' info from dump file
    dump_file = params.get("dump_file")
    default_dump_path = os.path.join(data_dir.get_deps_dir(), "cpuid")
    dump_path = params.get("dump_path", default_dump_path)
    cpuinfo_file = utils.unmap_url(dump_path, dump_file, dump_path)
    host_flags = utils_misc.get_cpu_flags()

    vm = env.get_vm(params["main_vm"])
    guest_cpumodel = vm.cpuinfo.model
    extra_flags = params.get("cpu_model_flags", " ")

    error.context("Boot guest with -cpu %s,%s" %
                  (guest_cpumodel, extra_flags), logging.info)
    vm.verify_alive()
    timeout = float(params.get("login_timeout", 240))
    session = vm.wait_for_login(timeout=timeout)

    # Get qemu model
    host_cpumodel = utils_misc.get_host_cpu_models()
    if guest_cpumodel not in host_cpumodel:
Beispiel #6
0
def run(test, params, env):
    """
    flag_check test:
    steps:
    1. boot guest with -cpu model,+extra_flags (extra_flags is optional)
       a. no defined model_name in cfg file
          guest_model = host_model
       b. model_name defined in cfg file
          guest_model = params.get("cpu_model")
    2. get guest flags
    3. get expected model flags from dump file
       a. -cpu host: qemu_model = host_model
       b. guest_model > host_model --> expected_model = host_model
          e.g guest_model = Haswell, host_model = Sandybridge
              expected_model = Sandybridge
       c. guest_model < host_model --> expected_model = guest_model
    4. get extra flags
       a. add_flags = +flag
          1). flag is exposed to guest if it's supported in host
          2). flag is not supported to guest if it's unknown in host
          3). ignore "check", "enforce" which are params not flag
       b. del_flags = -flag
          flag is removed if it's supported in guest
       c. params check: check lack flag in host include unknow flag
    5. compare expected flag with flags in guest
       a. out_flags: not supported with some conf, this kinds of flag
          will be displayed in dump file, but not in guest.
          e.g tsc-dedline is not supported with -M rhel6.3.0
       b. option_flags: some flag is generated by kernel which is not
          defined in dump file. it's acceptable when display in guest.
          e.g rep_good
       expected_flags = expected_model_flags + add_flags - del_flags
                        - out_flags
       miss_flag = expected_flags - guest_flags
       unexpect_flag = guest_flags - expected_flags - option_flags

    :param test: Kvm test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """

    def qemu_model_info(models_list, cpumodel):
        """
        Get cpumodel info from models_list
        :param models_list: all models info
        :param cpumodel: model name
        :return: model info of cpumodel
        """
        for model in models_list:
            if cpumodel in model:
                return model
        return None

    def qemu_support_flag(model_info, reg):
        """
        Get register's supported flags from model_info
        :param model_info: model_info get from dump file
        :param reg: reg name, e.g feature_edx
        """
        reg_re = re.compile(r".*%s.*\((.*)\)\n" % reg)
        flag = reg_re.search(model_info)
        try:
            if flag:
                return flag.groups()[0]
        except Exception as e:
            logging.error("Failed to get support flag %s" % e)

    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 get_extra_flag(extra_flags, symbol, lack_check=False):
        """
        Get added/removed flags
        :param extra_flags: exposed/removed flags. e.g "+sse4.1,+sse4.2"
        :param symbol: "+","-"
        :return: return all extra_flags if lack_check is true
                 return host supported flags if lack_check is false
        """
        flags = []
        re_flags = [_[1:] for _ in extra_flags.split(",") if _ and symbol == _[0]]
        for flag in re_flags:
            if lack_check:
                flags.append(flag)
            elif flag in host_flags:
                flags.append(flag)
        return set(map(utils_misc.Flag, flags))

    def get_guest_cpuflags(vm_session):
        """
        Get guest system cpuflags.

        :param vm_session: session to checked vm.
        :return: [corespond flags]
        """
        flags_re = re.compile(r"^flags\s*:(.*)$", re.MULTILINE)
        out = vm_session.cmd_output("cat /proc/cpuinfo")
        try:
            flags = flags_re.search(out).groups()[0].split()
            return set(map(utils_misc.Flag, flags))
        except Exception as e:
            logging.error("Failed to get guest cpu flags %s" % e)

    utils_misc.Flag.aliases = utils_misc.kvm_map_flags_aliases

    # Get all models' info from dump file
    dump_file = params.get("dump_file")
    default_dump_path = os.path.join(data_dir.get_deps_dir(), "cpuid")
    dump_path = params.get("dump_path", default_dump_path)
    cpuinfo_file = utils.unmap_url(dump_path, dump_file, dump_path)
    host_flags = utils_misc.get_cpu_flags()

    vm = env.get_vm(params["main_vm"])
    guest_cpumodel = vm.cpuinfo.model
    extra_flags = params.get("cpu_model_flags", " ")

    error.context("Boot guest with -cpu %s,%s" % (guest_cpumodel, extra_flags), logging.info)

    if params.get("start_vm") == "no" and "unknown,check" in extra_flags:
        params["start_vm"] = "yes"
        try:
            vm.create(params=params)
            vm.verify_alive()
            output = vm.process.get_output()
            vm.destroy()
        except virt_vm.VMCreateError as detail:
            output = str(detail)
        if params["qemu_output"] not in output:
            raise error.TestFail("no qemu output: %s" % params["qemu_output"])
    else:
        vm.verify_alive()
        timeout = float(params.get("login_timeout", 240))
        session = vm.wait_for_login(timeout=timeout)

        # Get qemu model
        host_cpumodel = utils_misc.get_host_cpu_models()
        if guest_cpumodel not in host_cpumodel:
            qemu_model = host_cpumodel[0]
        else:
            qemu_model = guest_cpumodel
        error.context("Get model %s support flags" % qemu_model, logging.info)

        # Get flags for every reg from model's info
        models_info = utils.system_output("cat %s" % cpuinfo_file).split("x86")
        model_info = qemu_model_info(models_info, qemu_model)
        reg_list = params.get("reg_list", "feature_edx ").split()
        model_support_flags = " "
        if model_info:
            for reg in reg_list:
                reg_flags = qemu_support_flag(model_info, reg)
                if reg_flags:
                    model_support_flags += " %s" % reg_flags
        model_support_flags = set(map(utils_misc.Flag, model_support_flags.split()))

        error.context("Get guest flags", logging.info)
        guest_flags = get_guest_cpuflags(session)

        error.context("Get expected flag list", logging.info)

        # out_flags is definded in dump file, but not in guest
        out_flags = params.get("out_flags", " ").split()
        out_flags = set(map(utils_misc.Flag, out_flags))
        # no_check_flags is definded in all_support_flags, but not in guest and host
        no_check_flags = params.get("no_check_flags", " ").split()
        no_check_flags = set(map(utils_misc.Flag, no_check_flags))
        # option_flags are generated by kernel or kvm, which are not definded in
        # dump file, but can be displayed in guest
        option_flags = params.get("option_flags", " ").split()
        if params["smp"] == "1" and "up" not in option_flags:
            option_flags.append("up")
        option_flags = set(map(utils_misc.Flag, option_flags))
        # add_flags are exposed by +flag
        add_flags = get_extra_flag(extra_flags, "+")
        # del_flags are disabled by -flag
        del_flags = get_extra_flag(extra_flags, "-", lack_check=True)
        expected_flags = (model_support_flags | add_flags) - del_flags - out_flags
        # get all flags for host lack flag checking
        check_flags = get_extra_flag(extra_flags, "+", lack_check=True)
        check_flags = check_flags - no_check_flags
        host_flags = set(map(utils_misc.Flag, host_flags))
        lack_flags = set(expected_flags | check_flags) - host_flags

        if "check" in extra_flags and "unknown" not in extra_flags:
            error.context("Check lack flag in host", logging.info)
            process_output = vm.process.get_output()
            miss_warn = []
            if lack_flags:
                for flag in lack_flags:
                    if flag not in process_output:
                        miss_warn.extend(flag.split())
            if miss_warn:
                raise error.TestFail("no warning for lack flag %s" % miss_warn)

        error.context("Compare guest flags with expected flags", logging.info)
        all_support_flags = get_all_support_flags()
        missing_flags = expected_flags - guest_flags
        unexpect_flags = guest_flags - expected_flags - all_support_flags - option_flags
        if missing_flags or unexpect_flags:
            raise error.TestFail(
                "missing flags:\n %s\n"
                "more flags than expected:\n %s\n"
                "expected flags:\n %s\n"
                "guest flags:\n %s\n" % (missing_flags, unexpect_flags, expected_flags, guest_flags)
            )