예제 #1
0
def run(test, params, env):
    """Convert from/to luks image."""
    vm = img_utils.boot_vm_with_images(test, params, env)
    session = vm.wait_for_login()
    guest_temp_file = params["guest_temp_file"]
    md5sum_bin = params.get("md5sum_bin", "md5sum")
    sync_bin = params.get("sync_bin", "sync")

    logging.debug("Create temporary file on guest: %s", guest_temp_file)
    img_utils.save_random_file_to_vm(vm, guest_temp_file, 2048 * 512, sync_bin)

    logging.debug("Get md5 value of the temporary file")
    md5_value = img_utils.check_md5sum(guest_temp_file, md5sum_bin, session)
    session.close()
    vm.destroy()

    root_dir = data_dir.get_data_dir()
    convert_source = params["convert_source"]
    convert_target = params["convert_target"]
    source_params = params.object_params(convert_source)
    source = qemu_storage.QemuImg(source_params, root_dir, convert_source)
    logging.debug("Convert from %s to %s", convert_source, convert_target)
    fail_on((process.CmdError,))(source.convert)(source_params, root_dir)

    logging.debug("Compare images: %s and %s", convert_source, convert_target)
    img_utils.qemu_img_compare(params, convert_source, convert_target)

    vm = img_utils.boot_vm_with_images(test, params, env, (convert_target,))
    session = vm.wait_for_login()
    logging.debug("Verify md5 value of the temporary file")
    img_utils.check_md5sum(guest_temp_file, md5sum_bin, session,
                           md5_value_to_check=md5_value)
    session.close()
    vm.destroy()
예제 #2
0
def run(test, params, env):
    """
    Convert image with parameter --target-is-zero
    1. Boot a guest
    2. Create temporary file in the guest
    3. Get md5 value of the temporary file
    4. Destroy the guest
    5. Convert image to raw/qcow2 with parameter --target-is-zero -n
    6. Boot the target image, check the md5 value of the temporary file
       Make sure the values are the same
    7. remove the target image

    :param test: VT test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    vm = img_utils.boot_vm_with_images(test, params, env)
    session = vm.wait_for_login()
    guest_temp_file = params["guest_temp_file"]
    md5sum_bin = params.get("md5sum_bin", "md5sum")
    sync_bin = params.get("sync_bin", "sync")
    logging.info("Create temporary file on guest: %s", guest_temp_file)
    img_utils.save_random_file_to_vm(vm, guest_temp_file, 2048 * 512, sync_bin)
    logging.info("Get md5 value of the temporary file")
    md5_value = img_utils.check_md5sum(guest_temp_file, md5sum_bin, session)
    session.close()
    vm.destroy()

    root_dir = data_dir.get_data_dir()
    convert_source = params["convert_source"]
    convert_target = params["convert_target"]
    source_params = params.object_params(convert_source)
    target_params = params.object_params(convert_target)
    source = qemu_storage.QemuImg(source_params, root_dir, convert_source)
    target = qemu_storage.QemuImg(target_params, root_dir, convert_target)
    target.create(target_params)
    skip_target_creation = target_params.get_boolean("skip_target_creation")
    cache_mode = params.get("cache_mode")
    source_cache_mode = params.get("source_cache_mode")

    logging.info("Convert from %s to %s", convert_source, convert_target)
    fail_on(
        (process.CmdError, ))(source.convert)(source_params, root_dir,
                                              cache_mode, source_cache_mode,
                                              skip_target_creation)

    vm = img_utils.boot_vm_with_images(test, params, env, (convert_target, ))
    session = vm.wait_for_login()
    logging.info("Verify md5 value of the temporary file")
    img_utils.check_md5sum(guest_temp_file,
                           md5sum_bin,
                           session,
                           md5_value_to_check=md5_value)
    session.close()
    target.remove()
예제 #3
0
def run(test, params, env):
    """Convert from/to luks image."""
    tmp_file_check = params.get("tmp_file_check", "yes") == "yes"
    if tmp_file_check:
        vm = img_utils.boot_vm_with_images(test, params, env)
        session = vm.wait_for_login()
        guest_temp_file = params["guest_temp_file"]
        md5sum_bin = params.get("md5sum_bin", "md5sum")
        sync_bin = params.get("sync_bin", "sync")

        logging.debug("Create temporary file on guest: %s", guest_temp_file)
        img_utils.save_random_file_to_vm(vm, guest_temp_file, 2048 * 512,
                                         sync_bin)

        logging.debug("Get md5 value of the temporary file")
        md5_value = img_utils.check_md5sum(guest_temp_file, md5sum_bin,
                                           session)
        session.close()
        vm.destroy()

    root_dir = data_dir.get_data_dir()
    convert_source = params["convert_source"]
    convert_target = params["convert_target"]
    source_params = params.object_params(convert_source)
    target_params = params.object_params(convert_target)
    source = qemu_storage.QemuImg(source_params, root_dir, convert_source)
    target = qemu_storage.QemuImg(target_params, root_dir, convert_target)
    cache_mode = params.get("cache_mode")
    logging.debug("Convert from %s to %s", convert_source, convert_target)
    fail_on((process.CmdError, ))(source.convert)(source_params,
                                                  root_dir,
                                                  cache_mode=cache_mode)

    logging.debug("Compare images: %s and %s", convert_source, convert_target)
    compare_cache_mode = params.get("compare_cache_mode")
    compare_ret = source.compare_to(target,
                                    source_cache_mode=compare_cache_mode)
    if compare_ret.exit_status != 0:
        logging.error(compare_ret.stdout_text)
        if compare_ret.exit_status == 1:
            test.fail(compare_ret.stdout_text)
        test.error(compare_ret.stdout_text)

    if tmp_file_check:
        vm = img_utils.boot_vm_with_images(test, params, env,
                                           (convert_target, ))
        session = vm.wait_for_login()
        logging.debug("Verify md5 value of the temporary file")
        img_utils.check_md5sum(guest_temp_file,
                               md5sum_bin,
                               session,
                               md5_value_to_check=md5_value)
        session.close()
        vm.destroy()
    target.remove()
def run(test, params, env):
    """
    qemu-img create a snapshot on a running base image.

    1. boot a guest up from a base image
    2. create a file on the base image disk, calculate its md5sum
    3. create a snapshot on the running base image
    4. shut the guest down and boot a guest from the snapshot
    5. check whether the file's md5sum stays same

    :param test: Qemu test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def generate_images_from_image_chain(image_chain):
        root_dir = data_dir.get_data_dir()
        return [qemu_storage.QemuImg(
            params.object_params(image), root_dir, image)
                for image in image_chain.split()]

    params["image_name_image1"] = params["image_name"]
    params["image_format_image1"] = params["image_format"]

    images = generate_images_from_image_chain(params["image_chain"])
    base, snapshot = images[0], images[1]
    guest_temp_file = params["guest_temp_file"]
    md5sum_bin = params.get("md5sum_bin", "md5sum")
    sync_bin = params.get("sync_bin", "sync")

    logging.info("Boot a guest up from base image: %s, and create a"
                 " file %s on the disk.", base.tag, guest_temp_file)
    vm = img_utils.boot_vm_with_images(test, params, env)
    session = vm.wait_for_login()
    img_utils.save_random_file_to_vm(vm, guest_temp_file, 1024 * 512, sync_bin)
    md5_value = img_utils.check_md5sum(guest_temp_file, md5sum_bin, session)
    session.close()

    logging.info("Create a snapshot %s on the running base image.",
                 snapshot.tag)
    snapshot.create(snapshot.params)

    vm.destroy()
    logging.info("Boot the guest up from snapshot image: %s, and verify the"
                 " file %s's md5 on the disk.", snapshot.tag, guest_temp_file)
    vm = img_utils.boot_vm_with_images(test, params, env,
                                       images=(snapshot.tag,))
    session = vm.wait_for_login()
    img_utils.check_md5sum(guest_temp_file, md5sum_bin, session,
                           md5_value_to_check=md5_value)
    session.close()
    vm.destroy()
def run(test, params, env):
    """
    1) Convert remote readonly system image to the local image
    2) Start VM from the local image,
       with the remote iso image as its cdrom
    3) Log into VM
    4) Check readable cdrom
    """
    def _convert_image():
        source = params['images'].split()[0]
        target = params['convert_target']
        source_params = params.object_params(source)
        target_params = params.object_params(target)
        source_image = qemu_storage.QemuImg(source_params, None, source)

        # Convert source to target
        fail_on((process.CmdError, ))(source_image.convert)(
            target_params, data_dir.get_data_dir())

    _convert_image()
    vm = img_utils.boot_vm_with_images(test, params, env,
                                       (params['convert_target'], ))
    session = vm.wait_for_login(
        timeout=params.get_numeric("login_timeout", 360))
    cdroms = utils_misc.wait_for(
        lambda: (utils_test.get_readable_cdroms(params, session)),
        timeout=params.get_numeric("timeout", 10))
    session.close()
    if not cdroms:
        test.fail("None readable cdrom found in vm.")
예제 #6
0
def run(test, params, env):
    """
    Cache sizes test for a guest.

    1. Boot a guest up with different cache sizes.
    2. Check writing data to the guest works fine.
    3. Shut the guest down.

    :param test: Qemu test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment
    """
    file = params["guest_file_name"]
    initial_tag = params["images"]
    cache_sizes = params["cache_sizes"].split()

    logging.info("Boot a guest up from initial image: %s, and create a"
                 " file %s on the disk.", initial_tag, file)
    for cache_size in cache_sizes:
        params["drv_extra_params_image1"] = "cache-size=%s" % cache_size
        vm = img_utils.boot_vm_with_images(test, params, env)
        session = vm.wait_for_login()
        guest_temp_file = params["guest_file_name"]
        sync_bin = params.get("sync_bin", "sync")

        logging.debug("Create temporary file on guest: %s", guest_temp_file)
        img_utils.save_random_file_to_vm(vm, guest_temp_file, 2048 * 512,
                                         sync_bin)

        session.close()
        vm.destroy()
def run(test, params, env):
    """
    Convert image with parameter -r, rate limit: the unit is bytes per second.
    1. Boot a guest
    2. Create temporary file in the guest
    3. Get md5 value of the temporary file
    3. Destroy the guest
    4. Convert image to raw/qcow2/luks with parameter -r
    4. Boot the target image, check the md5 value of the temporary file
       Make sure the values are the same

    :param test: VT test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    vm = img_utils.boot_vm_with_images(test, params, env)
    session = vm.wait_for_login()
    guest_temp_file = params["guest_temp_file"]
    md5sum_bin = params.get("md5sum_bin", "md5sum")
    sync_bin = params.get("sync_bin", "sync")
    logging.debug("Create temporary file on guest: %s", guest_temp_file)
    img_utils.save_random_file_to_vm(vm, guest_temp_file, 2048 * 512, sync_bin)
    logging.debug("Get md5 value of the temporary file")
    md5_value = img_utils.check_md5sum(guest_temp_file, md5sum_bin, session)
    session.close()
    vm.destroy()

    root_dir = data_dir.get_data_dir()
    convert_source = params["convert_source"]
    convert_target = params["convert_target"]
    source_params = params.object_params(convert_source)
    target_params = params.object_params(convert_target)
    source = qemu_storage.QemuImg(source_params, root_dir, convert_source)
    target = qemu_storage.QemuImg(target_params, root_dir, convert_target)
    logging.debug("Convert from %s to %s", convert_source, convert_target)
    fail_on((process.CmdError, ))(source.convert)(source_params, root_dir)

    vm = img_utils.boot_vm_with_images(test, params, env, (convert_target, ))
    session = vm.wait_for_login()
    logging.debug("Verify md5 value of the temporary file")
    img_utils.check_md5sum(guest_temp_file,
                           md5sum_bin,
                           session,
                           md5_value_to_check=md5_value)
    session.close()
    target.remove()
예제 #8
0
 def _start_vm_without_image():
     params['images'] = ''
     vm = None
     try:
         vm = img_utils.boot_vm_with_images(test, params, env)
         vm.verify_alive()
     finally:
         # let VT remove it
         params['images'] = ' %s' % params['local_image_tag']
     return vm
예제 #9
0
 def _check_file(boot_image, md5_value):
     logging.debug("Check md5sum.")
     vm = img_utils.boot_vm_with_images(test, params, env, (boot_image, ))
     session = vm.wait_for_login()
     guest_temp_file = params["guest_temp_file"]
     md5sum_bin = params.get("md5sum_bin", "md5sum")
     img_utils.check_md5sum(guest_temp_file,
                            md5sum_bin,
                            session,
                            md5_value_to_check=md5_value)
     session.close()
     vm.destroy()
예제 #10
0
def run(test, params, env):
    """
    1. create snapshot base->sn
    2. write to snapshot sn
    3. commit sn to base with default cache mode.
    4. check strace output that `O_DIRECT` is off for `open`.
    5. write to snapshot sn
    6. commit sn to base with cache=none.
    7. check strace output that `O_DIRECT` is on.
    """
    img_utils.find_strace()

    root_dir = data_dir.get_data_dir()
    trace_events = params["trace_events"].split()
    sync_bin = params.get("sync_bin", "sync")
    images = params["images"].split()
    params["image_name_%s" % images[0]] = params["image_name"]
    params["image_format_%s" % images[0]] = params["image_format"]

    base, sn = (qemu_storage.QemuImg(params.object_params(tag), root_dir, tag)
                for tag in images)
    try:
        sn.create(sn.params)
        vm = img_utils.boot_vm_with_images(test, params, env, (sn.tag, ))
    except (process.CmdError, virt_vm.VMCreateError) as detail:
        test.fail(str(detail))

    guest_file = params["guest_tmp_filename"]
    logging.debug("Create tmp file %s in image %s", guest_file,
                  sn.image_filename)
    img_utils.save_random_file_to_vm(vm, guest_file, 2048 * 100, sync_bin)
    vm.destroy()

    strace_log = os.path.join(test.debugdir, "commit.log")
    logging.debug("commit snapshot, strace log %s", strace_log)
    with img_utils.strace(sn, trace_events, strace_log):
        fail_on((process.CmdError, ))(sn.commit)()
    fail_msg = "'O_DIRECT' is presented in system calls %s" % trace_events
    if img_utils.check_flag(strace_log, base.image_filename, "O_DIRECT"):
        test.fail(fail_msg)
    if img_utils.check_flag(strace_log, sn.image_filename, "O_DIRECT"):
        test.fail(fail_msg)

    strace_log = os.path.join(test.debugdir, "commit_bypass.log")
    logging.debug("commit snapshot with cache 'none', strace log: %s",
                  strace_log)
    with img_utils.strace(sn, trace_events, strace_log):
        fail_on((process.CmdError, ))(sn.commit)(cache_mode="none")
    fail_msg = "'O_DIRECT' is missing in system calls %s" % trace_events
    if not img_utils.check_flag(strace_log, base.image_filename, "O_DIRECT"):
        test.fail(fail_msg)
    if not img_utils.check_flag(strace_log, sn.image_filename, "O_DIRECT"):
        test.fail(fail_msg)
예제 #11
0
 def save_file_to_snapshot():
     """Save temporary file to snapshot."""
     sync_bin = params.get("sync_bin", "sync")
     while True:
         snapshot = yield
         logging.debug("boot vm from image %s", snapshot.tag)
         vm = img_utils.boot_vm_with_images(test, params, env,
                                            images=(snapshot.tag,),
                                            vm_name="VM_%s" % snapshot.tag)
         guest_file = params["guest_tmp_filename"] % snapshot.tag
         logging.debug("create tmp file %s in %s", guest_file, snapshot.tag)
         img_utils.save_random_file_to_vm(vm, guest_file, 2048, sync_bin)
         vm.destroy()
def run(test, params, env):
    """
    1) Clone the system image1 with qemu-img
    2) Export the cloned image with qemu-nbd(type=unix)
    3) Start VM from the exported image

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def _prepare():
        logging.info("Clone system image with qemu-img")
        result = qemu_storage.QemuImg(
            params, None,
            params['images'].split()[0]).dd(output=storage.get_image_filename(
                params.object_params(params["local_image_tag"]),
                data_dir.get_data_dir()),
                                            bs=1024 * 1024)
        if result.exit_status != 0:
            test.fail('Failed to clone the system image, error: %s' %
                      result.stderr.decode())

        # Remove the image after test by avocado-vt
        # params['images'] += ' %s' % params["local_image_tag"]

    _prepare()

    # local image to be exported
    nbd_export = QemuNBDExportImage(params, params["local_image_tag"])
    nbd_export.export_image()

    session = None
    logging.info("Start VM from the exported image")

    try:
        # Start VM from the nbd exported image
        vm = img_utils.boot_vm_with_images(test, params, env,
                                           (params["nbd_image_tag"], ))
        session = vm.wait_for_login(
            timeout=int(params.get("login_timeout", 360)))
        if not session:
            test.fail('Failed to log into VM')
    finally:
        if session:
            session.close()
        vm.destroy()
        nbd_export.stop_export()
예제 #13
0
def run(test, params, env):
    """
    1) Create a local raw/qcow2/luks image
    2) Export it with qemu-nbd
    3) Convert remote system image to the exported nbd image
    4) Start VM from the exported image
    5) Log into VM
    """
    def _convert_image():
        source = params['images'].split()[0]
        target = params['convert_target']
        source_params = params.object_params(source)
        target_params = params.object_params(target)
        source_image = qemu_storage.QemuImg(source_params, None, source)

        # Convert source to target
        fail_on((process.CmdError,))(source_image.convert)(
            target_params, None, skip_target_creation=True)

    nbd_export = QemuNBDExportImage(params, params["local_image_tag"])
    nbd_export.create_image()
    nbd_export.export_image()

    # we only export image with local nbd server
    localhost = socket.gethostname()
    params['nbd_server_%s' % params['convert_target']
           ] = localhost if localhost else 'localhost'

    vm = None
    try:
        _convert_image()
        vm = img_utils.boot_vm_with_images(test, params, env,
                                           (params['convert_target'],))
        session = vm.wait_for_login(
            timeout=params.get_numeric("login_timeout", 480))
        session.close()
    finally:
        if vm:
            vm.destroy()
        nbd_export.stop_export()
예제 #14
0
def run(test, params, env):
    """
    1) Clone system image with qemu-img
    2) Export the image with qemu internal NBD server
    3) ncate ip -p port or ncat -U /socket/path
    4) Boot from the exported nbd image
    5) Log into VM

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def _create_image():
        result = qemu_storage.QemuImg(
            params, None, params['images'].split()[0]).dd(
                output=storage.get_image_filename(
                    params.object_params(params["local_image_tag"]),
                    data_dir.get_data_dir()
                ),
                bs=1024*1024
            )

        if result.exit_status != 0:
            test.fail('Failed to clone the system image, error: %s'
                      % result.stderr.decode())

    def _start_vm_without_image():
        params['images'] = ''
        vm = None
        try:
            vm = img_utils.boot_vm_with_images(test, params, env)
            vm.verify_alive()
        finally:
            # let VT remove it
            params['images'] = ' %s' % params['local_image_tag']
        return vm

    def _make_ncat_cmd():
        ncat = ''
        if params.get('nbd_unix_socket_%s' % params['nbd_image_tag']):
            ncat = params['ncat_cmd']
        else:
            localhost = socket.gethostname()
            params['nbd_server'] = localhost if localhost else 'localhost'
            ncat = params['ncat_cmd'].format(localhost=params['nbd_server'])
        return ncat

    _create_image()
    vm = _start_vm_without_image()

    nbd_export = InternalNBDExportImage(vm, params, params['local_image_tag'])
    nbd_export.hotplug_tls()
    nbd_export.hotplug_image()
    nbd_export.export_image()
    params['nbd_export_name'] = nbd_export.get_export_name()

    ncat_cmd = _make_ncat_cmd()
    result = process.run(ncat_cmd, ignore_status=True, shell=True)
    if params['errmsg_check'] not in result.stderr.decode().strip():
        test.fail('Failed to read message(%s) from output(%s)'
                  % (params['errmsg_check'], result.stderr.decode()))

    vm2 = None
    try:
        # Start another VM from the nbd exported image
        vm2 = img_utils.boot_vm_with_images(test, params, env,
                                            (params["nbd_image_tag"],),
                                            'vm2')
        session = vm2.wait_for_login(
            timeout=params.get_numeric("login_timeout", 480))
        session.close()
    finally:
        if vm2:
            vm2.destroy()
예제 #15
0
def run(test, params, env):
    """
    Convert remote image.

    1) Start VM and create a tmp file, record its md5sum, shutdown VM
    2) Convert image
    3) Start VM by the converted image and then check the md5sum
    """
    def _check_file(boot_image, md5_value):
        logging.debug("Check md5sum.")
        vm = img_utils.boot_vm_with_images(test, params, env, (boot_image, ))
        session = vm.wait_for_login()
        guest_temp_file = params["guest_temp_file"]
        md5sum_bin = params.get("md5sum_bin", "md5sum")
        img_utils.check_md5sum(guest_temp_file,
                               md5sum_bin,
                               session,
                               md5_value_to_check=md5_value)
        session.close()
        vm.destroy()

    vm = img_utils.boot_vm_with_images(test, params, env)
    session = vm.wait_for_login()
    guest_temp_file = params["guest_temp_file"]
    md5sum_bin = params.get("md5sum_bin", "md5sum")
    sync_bin = params.get("sync_bin", "sync")

    logging.info("Create temporary file on guest: %s", guest_temp_file)
    img_utils.save_random_file_to_vm(vm, guest_temp_file, 2048 * 512, sync_bin)

    md5_value = img_utils.check_md5sum(guest_temp_file, md5sum_bin, session)
    logging.info("Get md5 value of the temporary file: %s", md5_value)

    session.close()
    vm.destroy()

    root_dir = data_dir.get_data_dir()

    # Make a list of all source and target image pairs
    img_pairs = [(params["convert_source"], params["convert_target"])]
    if params.get("convert_target_remote"):
        # local -> remote
        img_pairs.append(
            (params["convert_target"], params["convert_target_remote"]))

    # Convert images
    for source, target in img_pairs:
        params["convert_source"] = source
        params["convert_target"] = target

        source_params = params.object_params(source)
        target_params = params.object_params(target)

        source_image = qemu_storage.QemuImg(source_params, root_dir, source)
        target_image = qemu_storage.QemuImg(target_params, root_dir, target)

        # remove the target
        target_filename = storage.get_image_filename(target_params, root_dir)
        storage.file_remove(target_params, target_filename)

        # Convert source to target
        cache_mode = params.get("cache_mode")
        source_cache_mode = params.get("source_cache_mode")
        logging.info("Convert %s to %s", source, target)
        fail_on((process.CmdError, ))(source_image.convert)(
            params,
            root_dir,
            cache_mode=cache_mode,
            source_cache_mode=source_cache_mode)

        _check_file(target, md5_value)

    # Remove images converted
    for _, target in img_pairs:
        target_params = params.object_params(target)
        target_image = qemu_storage.QemuImg(target_params, root_dir, target)
        target_image.remove()
예제 #16
0
def run(test, params, env):
    """
    Change the backing file from luks/raw to qcow2.
    1) create snapshot image1 -> sn1
    2) boot from each images in the snapshot chain and create tmp files
    3) create a new base qcow2 image
    4) rebase to the new base qcow2 image
    5) check if the tmp files create in step 2) persist
    """
    def verify_backing_file(image):
        """Verify image backing file."""
        info_output = json.loads(image.info(output="json"))
        backing_params = image.params.object_params(image.base_tag)
        backing_file = qemu_storage.get_image_repr(image.base_tag,
                                                   backing_params, root_dir)
        backing_file_info = info_output["backing-filename"]
        if backing_file != backing_file_info:
            err_msg = "backing file mismatch, got %s, expected %s." % \
                (backing_file_info, backing_file)
            raise ValueError(err_msg)

    timeout = int(params.get("timeout", 360))
    root_dir = data_dir.get_data_dir()
    images = params["image_chain"].split()
    params["image_name_%s" % images[0]] = params["image_name"]
    params["image_format_%s" % images[0]] = params["image_format"]
    images = [
        qemu_storage.QemuImg(params.object_params(tag), root_dir, tag)
        for tag in images
    ]

    for image in images[1:]:
        logging.debug("create snapshot %s based on %s", image.image_filename,
                      image.base_image_filename)
        image.create(image.params)

    md5sum_bin = params.get("md5sum_bin", "md5sum")
    sync_bin = params.get("sync_bin", "sync")
    hashes = {}
    for image in images:
        vm = img_utils.boot_vm_with_images(test, params, env, (image.tag, ))
        guest_file = params["guest_tmp_filename"] % image.tag
        logging.debug("save tmp file %s in image %s", guest_file,
                      image.image_filename)
        img_utils.save_random_file_to_vm(vm, guest_file, 2048 * 100, sync_bin)
        session = vm.wait_for_login(timeout=timeout)
        hashes[guest_file] = img_utils.check_md5sum(guest_file, md5sum_bin,
                                                    session)
        session.close()
        vm.destroy()

    snapshot = images[-1]
    rebase_target = params["rebase_target"]
    # ensure size equals to the base
    params["image_size_%s" % rebase_target] = images[0].size
    rebase_target = qemu_storage.QemuImg(params.object_params(rebase_target),
                                         root_dir, rebase_target)
    rebase_target.create(rebase_target.params)
    logging.debug("rebase snapshot")
    snapshot.base_tag = rebase_target.tag
    fail_on((process.CmdError, ))(snapshot.rebase)(snapshot.params)
    fail_on((ValueError, ))(verify_backing_file)(snapshot)

    logging.debug("boot from snapshot %s", snapshot.image_filename)
    vm = img_utils.boot_vm_with_images(test, params, env, (snapshot.tag, ))
    session = vm.wait_for_login(timeout=timeout)
    for guest_file, hashval in hashes.items():
        img_utils.check_md5sum(guest_file,
                               md5sum_bin,
                               session,
                               md5_value_to_check=hashval)
    session.close()
    vm.destroy()

    # if nothing goes wrong, remove newly created images
    params["remove_image_%s" % snapshot.tag] = "yes"
    params["images"] += " %s" % rebase_target.tag
def run(test, params, env):
    """
    Rebase a second qcow2 snapshot to a raw base file.

    1. create a qcow2 snapshot base -> sn1
    2. boot the guest from the sn1
    3. create a file in the snapshot disk,  calculate its md5sum
    4. shut the guest down
    5. create a qcow2 snapshot sn1 -> sn2
    6. rebase the sn2 to the base
    7. remove the sn1, optional
    8. boot the guest from the sn2 and check whether the
       file's md5sum stays same

    :param test: Qemu test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment
    """
    def get_img_objs(images, params):
        return [
            qemu_storage.QemuImg(params.object_params(tag), root_dir, tag)
            for tag in images
        ]

    @fail_on((AssertionError, ))
    def verify_qemu_img_info_backing_chain(output):
        """Verify qemu-img info output for this case."""
        def _get_compat_version():
            """Get compat version from params."""
            return params.get("qcow2_compatible", "1.1")

        logging.info("Verify snapshot's backing file information.")
        for image, img_info in zip(images, reversed(output)):
            # skip base layer
            if not image.base_tag:
                continue
            base_params = params.object_params(image.base_tag)
            base_image = qemu_storage.get_image_repr(image.base_tag,
                                                     base_params, root_dir)
            base_format = image.base_format
            compat = _get_compat_version()
            base_image_info = img_info.get("backing-filename")
            assert base_image == base_image_info, "backing image mismatches"
            if base_image_info and not base_image_info.startswith("json"):
                base_format_info = img_info.get("backing-filename-format")
                assert base_format == base_format_info, \
                    "backing format mismatches"
            compat_info = img_info["format-specific"]["data"]["compat"]
            assert compat == compat_info, "compat mode mismatches"

    timeout = int(params.get("timeout", 240))
    images = params["image_chain"].split()
    params["image_name_%s" % images[0]] = params["image_name"]
    params["image_format_%s" % images[0]] = params["image_format"]
    root_dir = data_dir.get_data_dir()
    images = get_img_objs(images, params)
    base, active_layer = images[0], images[-1]

    md5sum_bin = params.get("md5sum_bin", "md5sum")
    sync_bin = params.get("sync_bin", "sync")
    for image in images[1:]:
        logging.debug("Create snapshot %s based on %s", image.image_filename,
                      image.base_image_filename)
        image.create(image.params)
        info_output = json.loads(image.info(output="json"))
        verify_qemu_img_info_backing_chain(info_output)

    hashes = {}
    rebase_mode = params.get("rebase_mode", "safe")
    for image in images:
        # do not create temp file in intermediate images in unsafe rebase
        if rebase_mode == "safe" or image in (base, active_layer):
            vm = img_utils.boot_vm_with_images(test, params, env,
                                               (image.tag, ))
            guest_file = params["guest_tmp_filename"] % image.tag
            logging.debug("Create tmp file %s in image %s", guest_file,
                          image.image_filename)
            img_utils.save_random_file_to_vm(vm, guest_file, 2048 * 100,
                                             sync_bin)
            session = vm.wait_for_login(timeout=timeout)
            logging.debug("Get md5 value fo the temporary file")
            hashes[guest_file] = img_utils.check_md5sum(
                guest_file, md5sum_bin, session)
            session.close()
            vm.destroy()

    # remove intermediate images before unsafe rebase
    if rebase_mode == "unsafe":
        for image in images:
            if image not in (base, active_layer):
                logging.debug("Remove the snapshot %s before rebase.",
                              image.image_filename)
                image.remove()

    cache_mode = params.get("cache_mode")
    msg = "Rebase the snapshot %s to %s"
    msg += "with cache %s." % cache_mode if cache_mode else "."
    logging.info(msg)
    active_layer.base_tag = base.tag
    active_layer.rebase(active_layer.params, cache_mode)
    info_output = json.loads(active_layer.info(output="json"))
    verify_qemu_img_info_backing_chain(info_output)

    vm = img_utils.boot_vm_with_images(test, params, env, (active_layer.tag, ))
    session = vm.wait_for_login(timeout=timeout)
    for guest_file, hash_val in hashes.items():
        img_utils.check_md5sum(guest_file,
                               md5sum_bin,
                               session,
                               md5_value_to_check=hash_val)
    session.close()
    vm.destroy()
    for image in images:
        if image is not base:
            image.remove()
예제 #18
0
def run(test, params, env):
    """
    Commit with explicit backing file specification.

    1. create snapshot chain as image1->sn1->sn2
    2. commit sn2 to base
    3. check to see that sn2 is not emptied and the temp file in corresponding
    snapshot remains intact.
    """

    def prepare_images_from_params(images, params):
        """Parse params to initialize a QImage list."""
        return [qemu_storage.QemuImg(params.object_params(tag), root_dir, tag)
                for tag in images]

    def verify_backing_chain(info):
        """Verify image's backing chain."""
        for image, img_info in zip(images, reversed(info)):
            base_image = None
            if image.base_tag:
                base_params = params.object_params(image.base_tag)
                base_image = qemu_storage.get_image_repr(image.base_tag,
                                                         base_params, root_dir)
            base_image_from_info = img_info.get("full-backing-filename")
            if base_image != base_image_from_info:
                test.fail(("backing chain check for image %s failed, backing"
                           " file from info is %s, which should be %s.") %
                          (image.image_filename, base_image_from_info,
                           base_image))

    images = params.get("image_chain", "").split()
    if len(images) < 3:
        test.cancel("Snapshot chain must at least contains three images")
    params["image_name_%s" % images[0]] = params["image_name"]
    params["image_format_%s" % images[0]] = params["image_format"]
    root_dir = data_dir.get_data_dir()
    images = prepare_images_from_params(images, params)
    base, active_layer = images[0], images[-1]

    md5sum_bin = params.get("md5sum_bin", "md5sum")
    sync_bin = params.get("sync_bin", "sync")
    hashes = {}
    for image in images:
        if image is not base:
            logging.debug("Create snapshot %s based on %s",
                          image.image_filename, image.base_image_filename)
            image.create(image.params)
        vm = img_utils.boot_vm_with_images(test, params, env, (image.tag,))
        guest_file = params["guest_tmp_filename"] % image.tag
        logging.debug("Create tmp file %s in image %s", guest_file,
                      image.image_filename)
        img_utils.save_random_file_to_vm(vm, guest_file, 2048 * 100, sync_bin)

        session = vm.wait_for_login()
        logging.debug("Get md5 value fo the temporary file")
        hashes[guest_file] = img_utils.check_md5sum(guest_file,
                                                    md5sum_bin, session)
        session.close()
        vm.destroy()

    logging.debug("Hashes of temporary files:\n%s", hashes)

    logging.debug("Verify the snapshot chain")
    info = json.loads(active_layer.info(output="json"))
    active_layer_size_before = info[0]["actual-size"]
    verify_backing_chain(info)

    logging.debug("Commit image")
    active_layer.commit(base=base.tag)

    logging.debug("Verify the snapshot chain after commit")
    info = json.loads(active_layer.info(output="json"))
    active_layer_size_after = info[0]["actual-size"]
    logging.debug("%s file size before commit: %s, after commit: %s",
                  active_layer.image_filename, active_layer_size_before,
                  active_layer_size_after)
    if active_layer_size_after < active_layer_size_before:
        test.fail("image %s is emptied after commit with explicit base" %
                  active_layer.image_filename)
    verify_backing_chain(info)

    logging.debug("Verify hashes of temporary files")
    vm = img_utils.boot_vm_with_images(test, params, env, (base.tag,))
    session = vm.wait_for_login()
    for tmpfile, hashval in hashes.items():
        img_utils.check_md5sum(tmpfile, md5sum_bin, session,
                               md5_value_to_check=hashval)
def run(test, params, env):
    """
    Rebase a qcow2 snapshot onto no backing file.

    1. create an external qcow2v2/qcow2v3 snapshot
       based on a raw image
    2. boot the guest from the base
    3. create a file in the base disk, calculate its md5sum
    4. shut the guest down
    5. rebase the snapshot onto no backing file
    6. check the snapshot
    7. boot the guest from the snapshot and check whether the
    file's md5sum stays same

    :param test: Qemu test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment
    """
    def _verify_image_backing_file(info_output, base):
        """Verify backing image filename and format."""
        backing_filename = info_output["backing-filename"]
        backing_format = info_output.get("backing-filename-format")
        backing_filename_desired = qemu_storage.get_image_repr(
            base.tag, params, root_dir)
        if backing_filename != backing_filename_desired:
            test.fail("backing image name mismatch, got %s, expect %s" %
                      (backing_filename, backing_filename_desired))
        if backing_format:
            backing_format_desired = base.image_format
            if backing_format != backing_format_desired:
                test.fail("backing image format mismatch, got %s, expect %s" %
                          (backing_format, backing_format_desired))

    def _verify_qcow2_compatible(info_output, image):
        """Verify qcow2 compat version."""
        compat = info_output["format-specific"]["data"]["compat"]
        compat_desired = image.params.get("qcow2_compatible", "1.1")
        if compat != compat_desired:
            test.fail("%s image compat version mismatch, got %s, expect %s" %
                      (image.tag, compat, compat_desired))

    def _verify_no_backing_file(info_output):
        """Verify snapshot has no backing file for this case."""
        logging.info("Verify snapshot has no backing file after rebase.")
        for key in info_output:
            if "backing" in key:
                test.fail("the snapshot has backing file after rebase.")

    images = params["image_chain"].split()
    params["image_name_%s" % images[0]] = params["image_name"]
    params["image_format_%s" % images[0]] = params["image_format"]
    root_dir = data_dir.get_data_dir()
    base, sn = (qemu_storage.QemuImg(params.object_params(tag), root_dir, tag)
                for tag in images)

    md5sum_bin = params.get("md5sum_bin", "md5sum")
    sync_bin = params.get("sync_bin", "sync")

    logging.info("boot guest from base image %s", base.image_filename)
    vm = img_utils.boot_vm_with_images(test, params, env, (base.tag, ))

    guest_file = params["guest_tmp_filename"]
    logging.info("save tmp file %s in guest", guest_file)
    img_utils.save_random_file_to_vm(vm, guest_file, 2048 * 100, sync_bin)

    logging.info("get md5 value of tmp file %s", guest_file)
    session = vm.wait_for_login()
    hashval = img_utils.check_md5sum(guest_file, md5sum_bin, session)
    logging.info("tmp file %s md5: %s", guest_file, hashval)
    session.close()
    vm.destroy()

    logging.info("create a snapshot %s based on %s", sn.tag, base.tag)
    sn.create(sn.params)

    logging.info("verify backing chain")
    info_output = json.loads(sn.info(output="json"))
    _verify_image_backing_file(info_output, base)

    logging.info("verify snapshot %s qcow2 compat version", sn.tag)
    _verify_qcow2_compatible(info_output, sn)

    logging.info("rebase snapshot %s to none", sn.tag)
    sn.base_tag = "null"
    fail_on((process.CmdError, ))(sn.rebase)(sn.params)

    logging.info("verify backing chain after rebase")
    info_output = json.loads(sn.info(output="json"))
    _verify_no_backing_file(info_output)

    logging.info("check image %s after rebase", sn.tag)
    sn.check_image(sn.params, root_dir)

    logging.info("boot guest from snapshot %s", sn.tag)
    vm = img_utils.boot_vm_with_images(test, params, env, (sn.tag, ))

    logging.info("check the md5 value of tmp file %s after rebase", guest_file)
    session = vm.wait_for_login()
    img_utils.check_md5sum(guest_file,
                           md5sum_bin,
                           session,
                           md5_value_to_check=hashval)
    session.close()
    vm.destroy()

    # if nothing goes wrong, remove snapshot
    params["remove_image_%s" % sn.tag] = "yes"
def run(test, params, env):
    """
    qemu-img commit test.
    1. create images image1->sn
    2. boot from sn and create temporary files.
    3. calculate MD5 value of the temp file.
    4. commit sn.
    5. check snapshot is emptied.
    6. boot from image1 and check the existence of the temp file and its md5
    value.
    """
    # add missing params for image1
    images = params["images"].split()
    params["image_name_%s" % images[0]] = params["image_name"]
    params["image_format_%s" % images[0]] = params["image_format"]

    root_dir = data_dir.get_data_dir()
    image_chain = params["image_chain"].split()
    base, sn = (qemu_storage.QemuImg(params.object_params(tag), root_dir, tag)
                for tag in image_chain)

    error_context.context("create snapshot %s" % sn.tag, logging.info)
    sn.create(sn.params)

    error_context.context("boot vm from snapshot %s" % sn.tag, logging.info)
    vm = img_utils.boot_vm_with_images(test, params, env, (sn.tag, ))

    md5sum_bin = params.get("md5sum_bin", "md5sum")
    sync_bin = params.get("sync_bin", "sync")
    guest_file = params["guest_tmp_filename"]
    dd_blkcnt = int(params["dd_blkcnt"])
    error_context.context("save random file %s" % guest_file, logging.info)
    img_utils.save_random_file_to_vm(vm, guest_file, dd_blkcnt, sync_bin)
    session = vm.wait_for_login()
    md5val = img_utils.check_md5sum(guest_file, md5sum_bin, session)
    logging.debug("random file %s md5sum value: %s", guest_file, md5val)
    session.close()
    vm.destroy()

    error_context.context("commit snapshot %s" % sn.tag, logging.info)
    size_before_commit = json.loads(sn.info(output="json"))["actual-size"]
    logging.debug("%s size before commit: %s", sn.tag, size_before_commit)
    cache_mode = params.get("cache_mode")
    sn.commit(cache_mode=cache_mode)
    logging.debug("sync host cache after commit")
    process.system("sync")

    error_context.context("verify snapshot is emptied after commit",
                          logging.info)
    size_after_commit = json.loads(sn.info(output="json"))["actual-size"]
    logging.debug("%s size after commit: %s", sn.tag, size_after_commit)
    guest_file_size = dd_blkcnt * 512  # tmp file size in bytes
    if size_before_commit - size_after_commit >= guest_file_size:
        logging.debug("the snapshot file was emptied.")
    else:
        test.fail("snapshot was not emptied")

    error_context.context("boot vm from base %s" % base.tag, logging.info)
    vm = img_utils.boot_vm_with_images(test, params, env, (base.tag, ))
    session = vm.wait_for_login()
    img_utils.check_md5sum(guest_file,
                           md5sum_bin,
                           session,
                           md5_value_to_check=md5val)
    vm.destroy()
    # remove snapshot
    params["remove_image_%s" % sn.tag] = "yes"
예제 #21
0
def run(test, params, env):
    """
    Check data integrity after qemu unexpectedly quit
    1. Boot a guest
    2. If tmp_file_check == yes
    2.1 Create temporary file in the guest
    2.2 Get md5 value of the temporary file
    3. Kill qemu process after finishing writing data in the guest
    4. Boot the guest again, check the md5 value of the temporary file
       Make sure the values are the same
    5. If tmp_file_check == no
    5.1 Kill qemu process during writing data in the guest
    6. Boot the guest again, make sure it could boot successfully

    :param test: VT test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    def kill_vm_process(vm):
        """kill vm process

        :param vm: vm object
        """
        pid = vm.process.get_pid()
        logging.debug("Ending VM %s process (killing PID %s)", vm.name, pid)
        try:
            utils_misc.kill_process_tree(pid, 9, timeout=60)
            logging.debug("VM %s down (process killed)", vm.name)
        except RuntimeError:
            test.error("VM %s (PID %s) is a zombie!" %
                       (vm.name, vm.process.get_pid()))

    def run_iozone_background(vm):
        """
        run iozone in guest

        :param vm: vm object
        """
        logging.debug("Start iozone in background.")
        iozone = generate_instance(params, vm, 'iozone')
        args = (params['iozone_cmd_opitons'], int(params['iozone_timeout']))
        iozone_thread = utils_misc.InterruptedThread(iozone.run, args)
        iozone_thread.start()
        if not utils_misc.wait_for(lambda: iozone_thread.is_alive, 60):
            test.error("Failed to start iozone thread.")
        return iozone_thread

    vm = img_utils.boot_vm_with_images(test, params, env)
    tmp_file_check = params.get_boolean("tmp_file_check")
    if tmp_file_check:
        session = vm.wait_for_login()
        guest_temp_file = params["guest_temp_file"]
        md5sum_bin = params.get("md5sum_bin", "md5sum")
        sync_bin = params.get("sync_bin", "sync")
        logging.debug("Create temporary file on guest: %s", guest_temp_file)
        img_utils.save_random_file_to_vm(vm, guest_temp_file, 2048 * 512,
                                         sync_bin)
        logging.debug("Get md5 value of the temporary file")
        md5_value = img_utils.check_md5sum(guest_temp_file, md5sum_bin,
                                           session)
        session.close()
        kill_vm_process(vm)
        vm = img_utils.boot_vm_with_images(test, params, env)
        session = vm.wait_for_login()
        logging.debug("Verify md5 value of the temporary file")
        img_utils.check_md5sum(guest_temp_file,
                               md5sum_bin,
                               session,
                               md5_value_to_check=md5_value)
        session.cmd(params["rm_testfile_cmd"] % guest_temp_file)
    else:
        iozone_testfile = params["iozone_testfile"]
        iozone_thread = run_iozone_background(vm)
        running_time = params.get_numeric("running_time", 15)
        time.sleep(running_time)
        if iozone_thread.is_alive:
            kill_vm_process(vm)
        else:
            test.error("Iozone thread is not running.")
        vm = img_utils.boot_vm_with_images(test, params, env)
        session = vm.wait_for_login()
        session.cmd(params["rm_testfile_cmd"] % iozone_testfile)
    session.close()
예제 #22
0
def run(test, params, env):
    """
    Convert image with parameter -B -n
    1. Boot a guest with image1
    2. Create temporary file in the guest
    3. Get md5 value of the temporary file
    4. Destroy the guest
    5. Create a snapshot, base1 -> sn1
    6. Create a image and its snapshot, base2 -> sn2
    7. Convert base1 to base2 with parameter -n
    8. Convert sn1 to sn2 with parameter -B -n, set backing file to base2
    9. Boot sn2, check the md5 value of the temporary file
       Make sure the values are the same
    10. Destroy the guest
    11. Check sn1 is not allocated the entire image after the convert
    12. Check sn2 is not allocated the entire image after the convert
    13. Check the backing file of sn2, it should be base2
    14. remove base2, sn1 and sn2

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

    def prepare_images_from_params(image_chain, params):
        """Parse params to initialize a QImage."""
        params["image_chain"] = image_chain
        image_chain = params["image_chain"].split()
        base, sn = (qemu_storage.QemuImg(params.object_params(tag), root_dir, tag)
                    for tag in image_chain)
        return base, sn

    def convert_images_from_params(convert_source, convert_target,
                                   backing_file=None):
        """Convert images with specified parameters"""
        source_params = convert_source.params
        target_params = convert_target.params
        skip_target_creation = target_params.get_boolean("skip_target_creation")
        cache_mode = params.get("cache_mode")
        source_cache_mode = params.get("source_cache_mode")
        source_params["convert_target"] = convert_target.tag
        source_params["convert_backing_file"] = backing_file
        logging.info("Convert from %s to %s",
                     convert_source.tag, convert_target.tag)
        fail_on((process.CmdError,))(convert_source.convert)(
                source_params, root_dir, cache_mode,
                source_cache_mode, skip_target_creation)

    def check_image_size(image):
        """Check image is not fully allocated"""
        logging.info("Verify qemu-img does not allocate the "
                     "entire image after image convert")
        info = json.loads(image.info(output="json"))
        virtual_size = info["virtual-size"]
        actual_size = info["actual-size"]
        if actual_size >= virtual_size:
            test.fail("qemu-img wrongly allocates to %s the entire image",
                      image.tag)

    images = params["images"].split()
    params["image_name_%s" % images[0]] = params["image_name"]
    params["image_format_%s" % images[0]] = params["image_format"]
    vm = img_utils.boot_vm_with_images(test, params, env, (images[0],))
    session = vm.wait_for_login()
    guest_temp_file = params["guest_temp_file"]
    md5sum_bin = params.get("md5sum_bin", "md5sum")
    sync_bin = params.get("sync_bin", "sync")
    logging.info("Create temporary file on guest: %s", guest_temp_file)
    img_utils.save_random_file_to_vm(vm, guest_temp_file, 2048 * 512, sync_bin)
    logging.info("Get md5 value of the temporary file")
    md5_value = img_utils.check_md5sum(guest_temp_file, md5sum_bin, session)
    session.close()
    vm.destroy()

    root_dir = data_dir.get_data_dir()
    base1, sn1 = prepare_images_from_params(params["image_chain1"], params)
    logging.info("Create snapshot %s", sn1.tag)
    sn1.create(sn1.params)
    base2, sn2 = prepare_images_from_params(params["image_chain2"], params)
    logging.info("Create snapshot %s", sn2.tag)
    base2.create(base2.params)
    sn2.create(sn2.params)

    convert_images_from_params(base1, base2)
    convert_images_from_params(sn1, sn2, backing_file=base2.image_filename)

    vm = img_utils.boot_vm_with_images(test, params, env, (sn2.tag,))
    session = vm.wait_for_login()
    logging.info("Verify md5 value of the temporary file")
    img_utils.check_md5sum(guest_temp_file, md5sum_bin, session,
                           md5_value_to_check=md5_value)
    session.close()
    vm.destroy()

    check_image_size(sn1)
    check_image_size(sn2)

    logging.info("Verify the snapshot chain of %s", sn2.tag)
    info = json.loads(sn2.info(output="json"))
    full_backing_filename = info["full-backing-filename"]
    if full_backing_filename != base2.image_filename:
        test.fail("The full-backing-filename of %s is incorrect."
                  "It should be %s, but it is %s.",
                  sn2.tag, base2.image_filename, full_backing_filename)
    base2.remove()
    sn1.remove()
    sn2.remove()