Esempio n. 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()
Esempio n. 2
0
def run(test, params, env):
    """
    qemu-img supports convert images with unaligned size.

    1. create source image via truncate, and  write 10k "1" into
       the source image via qemu-io
    2. convert the source image to target, check whether there is error

    :param test: Qemu test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def _qemu_io(img, cmd):
        """Run qemu-io cmd to a given img."""
        logging.info("Run qemu-io %s", img.image_filename)
        try:
            QemuIOSystem(test, params, img.image_filename).cmd_output(cmd, 120)
        except process.CmdError:
            test.fail("qemu-io to '%s' failed." % img.image_filename)

    src_image = params["images"]
    tgt_image = params["convert_target"]
    img_dir = data_dir.get_data_dir()

    source = QemuImg(params.object_params(src_image), img_dir, src_image)
    with open(source.image_filename, mode='wb') as fd:
        fd.truncate(int(params["truncate_size"]))
    _qemu_io(source, 'write -P 1 0 %s' % params["write_size"])

    fail_on((process.CmdError, ))(source.convert)(source.params,
                                                  img_dir,
                                                  cache_mode="none",
                                                  source_cache_mode="none")

    params["images"] += " " + tgt_image
def run(test, params, env):
    """
    1. convert image with both default cache mode.
    2. check strace output that `O_DIRECT` is off for `open`.
    3. convert image with cache mode `none` for both source and dest images.
    4. check strace output that `O_DIRECT` is on for `open`.
    """

    find_strace()

    root_dir = data_dir.get_data_dir()
    strace_events = params["strace_event"].split()
    image = params["images"]
    image_params = params.object_params(image)
    image = qemu_storage.QemuImg(image_params, root_dir, image)
    convert_target1, convert_target2 = params["convert_target"].split()

    strace_output_file = os.path.join(test.debugdir,
                                      "convert_to_%s.log" % convert_target1)
    image_params["convert_target"] = convert_target1
    logging.debug("Convert image from %s to %s, strace log: %s", image.tag,
                  convert_target1, strace_output_file)

    with strace(image, strace_events, strace_output_file):
        fail_on((process.CmdError, ))(image.convert)(image_params, root_dir)

    convert_target1_filename = storage.get_image_filename(
        params.object_params(convert_target1), root_dir)
    fail_msg = "'O_DIRECT' is presented in system calls %s" % strace_events
    if check_flag(strace_output_file, image.image_filename, "O_DIRECT"):
        test.fail(fail_msg)
    if check_flag(strace_output_file, convert_target1_filename, "O_DIRECT"):
        test.fail(fail_msg)

    strace_output_file = os.path.join(test.debugdir,
                                      "convert_to_%s.log" % convert_target2)
    image_params["convert_target"] = convert_target2
    logging.debug(("Convert image from %s to %s with cache mode "
                   "'none', strace log: %s"), image.tag, convert_target2,
                  strace_output_file)

    with strace(image, strace_events, strace_output_file):
        fail_on((process.CmdError, ))(image.convert)(image_params,
                                                     root_dir,
                                                     cache_mode="none",
                                                     source_cache_mode="none")

    convert_target2_filename = storage.get_image_filename(
        params.object_params(convert_target2), root_dir)
    fail_msg = "'O_DIRECT' is not presented in system calls %s" % strace_events
    if not check_flag(strace_output_file, image.image_filename, "O_DIRECT"):
        test.fail(fail_msg)
    if not check_flag(strace_output_file, convert_target2_filename,
                      "O_DIRECT"):
        test.fail(fail_msg)
    params["images"] += params["convert_target"]
def run(test, params, env):
    """
    qemu-img supports 'discard' for raw block target images.

    1. Create source image via dd with all zero.
    2. Modprobe a 1G scsi_debug disk with writesame_16 mode.
    3. Trace the system calls while converting the zero image to the
       scsi_debug block device, then check whether 'fallocate' system call
       results work in the log.
    :param test: Qemu test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    def _check_output(strace_event, strace_output, match_str):
        """Check whether the value is good in the output file."""
        logging.debug("Check the output file '%s'.", strace_output)
        with open(strace_output) as fd:
            m = re.findall(match_str + r', \d+, \d+', fd.read())
            if not m:
                test.fail("The result of system call '%s' is not right, "
                          "check '%s' for more details." %
                          (strace_event, strace_output))
            last_lst = m[-1].split(',')
            sum_size = int(last_lst[-1]) + int(last_lst[-2])
            # get the source image size in byte unit
            byte_image_size = int(
                utils_numeric.normalize_data_size(image_size, "B"))
            if sum_size != byte_image_size:
                test.fail(
                    "The target allocated size '%s' is different from the source image size, "
                    "check '%s' for more details." %
                    (str(sum_size), strace_output))

    src_image = params["images"]
    image_size = params["image_size_test"]
    root_dir = data_dir.get_data_dir()
    source = QemuImg(params.object_params(src_image), root_dir, src_image)
    strace_event = params["strace_event"]
    strace_events = strace_event.split()
    strace_output_file = os.path.join(test.debugdir, "convert_to_block.log")

    source.create(source.params)
    # Generate the target scsi block file.
    tgt_disk = process.system_output("lsscsi | grep '%s' | awk '{print $NF}'" %
                                     params["scsi_mod"],
                                     shell=True).decode()
    params["image_name_target"] = tgt_disk

    logging.debug(
        "Convert from %s to %s with cache mode none, strace log: %s.",
        source.image_filename, tgt_disk, strace_output_file)
    with strace(source, strace_events, strace_output_file, trace_child=True):
        fail_on((process.CmdError, ))(source.convert)(
            params.object_params(src_image), root_dir, cache_mode="none")

    _check_output(strace_event, strace_output_file, "FALLOC_FL_PUNCH_HOLE")
Esempio n. 5
0
    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)
Esempio n. 6
0
 def create_snapshot(target):
     """Create snapshot."""
     while True:
         snapshot = yield
         logging.debug("create image %s", snapshot)
         snapshot_params = params.object_params(snapshot)
         snapshot = qemu_storage.QemuImg(snapshot_params, root_dir, snapshot)
         fail_on((process.CmdError,))(snapshot.create)(snapshot.params)
         snapshots.append(snapshot)
         target.send(snapshot)
Esempio n. 7
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()
Esempio n. 8
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()
Esempio n. 9
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)
Esempio n. 10
0
 def check_fallocate_syscall(trace_event):
     """
     check whether invoke fallocate system call
     when creating an image with preallocation=falloc
     """
     strace_log = os.path.join(test.debugdir, "fallocate.log")
     with img_utils.strace(img_stg, trace_event.split(), strace_log, True):
         fail_on((process.CmdError,))(img_stg.create)(image_stg_params)
     with open(strace_log) as fd:
         if trace_event not in fd.read():
             test.fail("Not invoked fallocate system call when "
                       "creating an image with preallocation=falloc")
Esempio n. 11
0
def run(test, params, env):
    """
    Convert a image to a virtual block device.
    1. Create source and middle images.
    2. Setup loop device with the middle image.
    3. Convert the source image to the loop device.
    :param test: Qemu test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    def setup_loop_dev(image_path):
        """
        Setup a loop device with a file image.
        :param image_path: The path to the image used to setup loop device
        :return: The loop device under /dev
        """
        cmd_result = process.run("losetup -f %s --show" % image_path)
        return cmd_result.stdout_text.strip()

    def free_loop_dev(loop_dev):
        """
        Free a loop device.
        :param loop_dev: The loop device will be free
        """
        process.run("losetup -d %s" % loop_dev)

    src_img = params["images"].split()[0]
    mid_img = params["images"].split()[-1]
    root_dir = data_dir.get_data_dir()
    source = QemuImg(params.object_params(src_img), root_dir, src_img)
    middle = QemuImg(params.object_params(mid_img), root_dir, mid_img)
    mid_filename = middle.image_filename

    logging.info("Create the test image files.")
    source.create(source.params)
    middle.create(middle.params)

    logging.info("Setup target loop device via 'losetup'.")
    target = setup_loop_dev(mid_filename)
    params["image_name_target"] = target

    logging.debug("Convert from %s to %s with cache mode none.",
                  source.image_filename, mid_filename)
    try:
        fail_on((process.CmdError,))(source.convert)(
            params.object_params(src_img), root_dir, cache_mode="none")
    finally:
        logging.info("Clean the loop device.")
        free_loop_dev(target)
Esempio n. 12
0
 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 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()
def run(test, params, env):
    """
    qemu-img supports 'discard' for raw block target images.

    1. Create source image via dd with all zero.
    2. Modprobe a 1G scsi_debug disk with writesame_16 mode.
    3. Trace the system calls while converting the zero image to the
       scsi_debug block device, then check whether 'fallocate' system call
       works in the log.
    :param test: Qemu test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    def check_output(strace_event, strace_output, match_str):
        """Check whether the value is good in the output file."""
        logging.debug("Check the output file '%s'.", strace_output)
        with open(strace_output) as fd:
            if match_str not in fd.read():
                test.fail("The system call of '%s' is not right, "
                          "check '%s' for more details." %
                          (strace_event, strace_output))

    src_image = params["images"]
    root_dir = data_dir.get_data_dir()
    source = QemuImg(params.object_params(src_image), root_dir, src_image)
    strace_event = params["strace_event"]
    strace_events = strace_event.split()
    strace_output_file = os.path.join(test.debugdir, "convert_to_block.log")

    source.create(source.params)
    # Generate the target scsi block file.
    tgt_disk = process.system_output("lsscsi | grep '%s' | awk '{print $NF}'" %
                                     params["scsi_mod"],
                                     shell=True).decode()
    params["image_name_target"] = tgt_disk

    logging.debug(
        "Convert from %s to %s with cache mode none, strace log: %s.",
        source.image_filename, tgt_disk, strace_output_file)
    with strace(source, strace_events, strace_output_file, trace_child=True):
        fail_on((process.CmdError, ))(source.convert)(
            params.object_params(src_image), root_dir, cache_mode="none")

    check_output(strace_event, strace_output_file,
                 "FALLOC_FL_PUNCH_HOLE, 0, 1073741824")
Esempio n. 15
0
def run(test, params, env):
    """
    Check the data after resizing short overlay over longer backing files.
    1. Create a qcow2 base image.
    2. Create a middle snapshot file with smaller size.
    3. Create a top snapshot with the size as the same as the one of base.
    4. Write '1' to the base image file.
    5. Check the data of the top image file.
    6. Commit top image file.
    7. Check the data in the middle image file.
    :param test: VT test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    def _qemu_io(img, cmd):
        """Run qemu-io cmd to a given img."""
        logging.info("Run qemu-io %s" % img.image_filename)
        try:
            QemuIOSystem(test, params, img.image_filename).cmd_output(cmd, 120)
        except process.CmdError as err:
            test.fail("qemu-io to '%s' failed: %s." %
                      (img.image_filename, err))

    images = params["image_chain"].split()
    root_dir = data_dir.get_data_dir()
    base = QemuImg(params.object_params(images[0]), root_dir, images[0])
    mid = QemuImg(params.object_params(images[1]), root_dir, images[1])
    top = QemuImg(params.object_params(images[-1]), root_dir, images[-1])

    logging.info("Create base and snapshot files")
    for image in (base, mid, top):
        image.create(image.params)

    _qemu_io(base, params["base_cmd"])

    top_cmd = params["top_cmd"]
    _qemu_io(top, top_cmd)

    logging.info("Commit %s image file." % top.image_filename)
    fail_on((process.CmdError, ))(top.commit)()

    _qemu_io(mid, top_cmd)
Esempio n. 16
0
class UpgradeTomcat(Test):
    """
    This test want to upgrade tomcat by zypper installation.
    Default repo is the latest tomcat from tumbleweed
    """
    # use avocado decorator for avoiding try/except.
    avocado.fail_on(subprocess.CalledProcessError)

    def test_upgrade(self):
        tumbleweed = "FIMXMe"
        repo = self.params.get('repo', default=tumbleweed)
        subprocess.check_output("systemctl restart tomcat", shell=True)
Esempio n. 17
0
def run(test, params, env):
    """
    qemu-img uses larger output buffer for "none" cache mode.
    1. Create 100M source image with random data via 'dd'.
    2. Trace the system calls while converting the source image to the
       a qcow2 target image, and check the maxim result of pwrite/pwrite64
       should be 2M.
    :param test: Qemu test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    src_image = params["images"]
    tgt_image = params["convert_target"]
    root_dir = data_dir.get_data_dir()
    source = QemuImg(params.object_params(src_image), root_dir, src_image)
    strace_event = params["strace_event"]
    strace_events = strace_event.split()
    strace_output_file = os.path.join(test.debugdir, "convert_with_none.log")
    src_filename = source.image_filename
    process.run("dd if=/dev/urandom of=%s bs=1M count=100" % src_filename)
    logging.debug(
        "Convert from %s to %s with cache mode none, strace log: "
        "%s.", src_filename, tgt_image, strace_output_file)
    with strace(source, strace_events, strace_output_file, trace_child=True):
        fail_on((process.CmdError, ))(source.convert)(
            params.object_params(src_image), root_dir, cache_mode="none")

    logging.debug("Check whether the max size of %s syscall is 2M in %s.",
                  strace_event, strace_output_file)
    with open(strace_output_file) as fd:
        for line in fd.readlines():
            if int(line.split()[-1]) == 2097152:
                break
        else:
            test.fail("The max size of '%s' is not 2M, check '%s' please.",
                      strace_event, strace_output_file)

    params["images"] += " " + tgt_image
Esempio n. 18
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()
Esempio n. 19
0
def run(test, params, env):
    """
    check if qemu-img rebase could bypass host cache.
    1) create snapshot chain image1 -> sn1 -> sn2
    2) rebase sn2 to image1 and check the open syscall that no flag O_DIRECT
    3) create snapshot chain image1 -> sn1 -> sn2
    4) rebase sn2 to image1 with cache mode 'none' and check flag O_DIRECT
    is on.
    """
    def remove_snapshots():
        """Remove snapshots created."""
        while snapshots:
            snapshot = snapshots.pop()
            snapshot.remove()

    def parse_snapshot_chain(target):
        """Parse snapshot chain."""
        image_chain = params["image_chain"].split()
        for snapshot in image_chain[1:]:
            target.send(snapshot)

    @coroutine
    def create_snapshot(target):
        """Create snapshot."""
        while True:
            snapshot = yield
            logging.debug("create image %s", snapshot)
            snapshot_params = params.object_params(snapshot)
            snapshot = qemu_storage.QemuImg(snapshot_params, root_dir, snapshot)
            fail_on((process.CmdError,))(snapshot.create)(snapshot.params)
            snapshots.append(snapshot)
            target.send(snapshot)

    @coroutine
    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()

    img_utils.find_strace()
    base = params["image_chain"].split()[0]
    params["image_name_%s" % base] = params["image_name"]
    params["image_format_%s" % base] = params["image_format"]
    root_dir = data_dir.get_data_dir()
    base = qemu_storage.QemuImg(params.object_params(base), root_dir, base)
    trace_events = params["trace_event"].split()

    snapshots = []
    parse_snapshot_chain(create_snapshot(save_file_to_snapshot()))

    strace_log = os.path.join(test.debugdir, "rebase.log")
    top = snapshots[-1]
    logging.debug("rebase snapshot %s to %s", top.tag, base.tag)
    with img_utils.strace(top, trace_events, strace_log):
        top.base_tag = base.tag
        fail_on((process.CmdError))(top.rebase)(params)

    fail_msg = "'O_DIRECT' is presented in %s with file %s"
    for image in [base] + snapshots:
        if img_utils.check_flag(strace_log, image.image_filename, "O_DIRECT"):
            test.fail(fail_msg % (trace_events, image.image_filename))

    remove_snapshots()
    parse_snapshot_chain(create_snapshot(save_file_to_snapshot()))

    strace_log = os.path.join(test.debugdir, "rebase_bypass.log")
    top = snapshots[-1]
    logging.debug("rebase snapshot %s to %s in cache mode 'none'",
                  top.tag, base.tag)
    with img_utils.strace(top, trace_events, strace_log):
        top.base_tag = base.tag
        fail_on((process.CmdError))(top.rebase)(params,
                                                cache_mode="none",
                                                source_cache_mode="none")

    fail_msg = "'O_DIRECT' is missing in %s with file %s"
    for image in [base] + snapshots:
        if not img_utils.check_flag(strace_log,
                                    image.image_filename, "O_DIRECT"):
            test.fail(fail_msg % (trace_events, image.image_filename))

    remove_snapshots()
Esempio n. 20
0
def run(test, params, env):
    """
    Verification that image lock has no effect on the read operation from
    different image chain.
    Steps:
        1. create the first snapshot chain: image1 -> sn01 -> sn02
        2. boot first vm from sn02
        3. create the second snapshot chain: image1 -> sn11 -> sn12 ->sn13
        4. boot second vm frm sn13 and create a temporary file
        5. commit sn13
        6. boot second vm from sn12 and verify the temporary file is presented.
    """

    params.update({
        "image_name_image1": params["image_name"],
        "image_format_image1": params["image_format"]
    })

    error_context.context("boot first vm from first image chain", logging.info)
    env_process.process(test, params, env, env_process.preprocess_image,
                        env_process.preprocess_vm)
    vm1 = env.get_vm(params["main_vm"])
    vm1.verify_alive()

    params["images"] = params["image_chain"] = params["image_chain_second"]
    params["main_vm"] = params["vms"].split()[-1]
    sn_tags = params["image_chain"].split()[1:]
    images = [QemuImgTest(test, params, env, image) for image in sn_tags]

    error_context.context("create the second snapshot chain", logging.info)
    for image in images:
        logging.debug("create snapshot %s based on %s", image.image_filename,
                      image.base_image_filename)
        image.create_snapshot()
        logging.debug("boot from snapshot %s", image.image_filename)
        try:
            # ensure vm only boot with this snapshot
            image.start_vm({"boot_drive_%s" % image.tag: "yes"})
        except virt_vm.VMCreateError:
            # add images in second chain to images so they could be deleted
            # in postprocess
            params["images"] += " %s" % image
            test.fail("fail to start vm from snapshot %s" %
                      image.image_filename)
        else:
            if image is not images[-1]:
                image.destroy_vm()

    tmpfile = params.get("guest_tmp_filename")
    error_context.context(
        "create a temporary file: %s in %s" % (tmpfile, image.image_filename),
        logging.info)
    hash_val = image.save_file(tmpfile)
    logging.debug("The hash of temporary file:\n%s", hash_val)
    image.destroy_vm()

    error_context.context("commit image %s" % image.image_filename,
                          logging.info)
    fail_on()(image.commit)()

    error_context.context("check temporary file after commit", logging.info)
    image = images[-2]
    logging.debug("boot vm from %s", image.image_filename)
    image.start_vm({"boot_drive_%s" % image.tag: "yes"})
    if not image.check_file(tmpfile, hash_val):
        test.fail("File %s's hash is different after commit" % tmpfile)
Esempio n. 21
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 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"