def do_convert(args):
    volume_abs_path = args[1]
    raw_volume_abs_path = volume_abs_path + RAW_SUFFIX
    qcow2_volume_abs_path = volume_abs_path + QCOW2_SUFFIX
    check(volume_abs_path, raw_volume_abs_path)

    volume_size = lvm.get_lv_size(volume_abs_path)
    lvm.create_lv_from_absolute_path(raw_volume_abs_path, volume_size)
    with lvm.RecursiveOperateLv(volume_abs_path,
                                shared=False,
                                delete_when_exception=False):
        with lvm.OperateLv(raw_volume_abs_path,
                           shared=False,
                           delete_when_exception=False):
            lvm.add_lv_tag(volume_abs_path, START_TAG)
            r, o, e = qcow2_convert_to_raw(volume_abs_path,
                                           raw_volume_abs_path)
            if r != 0:
                logger.warn("convert failed: %s, removing tag and raw volume")
                lvm.clean_lv_tag(volume_abs_path, START_TAG)
                lvm.delete_lv(raw_volume_abs_path)
                return
            lvm.add_lv_tag(volume_abs_path, DONE_TAG)
            lvm.clean_lv_tag(volume_abs_path, START_TAG)
            lvm.lv_rename(volume_abs_path,
                          qcow2_volume_abs_path,
                          overwrite=False)
            lvm.lv_rename(raw_volume_abs_path,
                          volume_abs_path,
                          overwrite=False)
    def offline_merge_snapshots(self, req):
        cmd = jsonobject.loads(req[http.REQUEST_BODY])
        rsp = OfflineMergeSnapshotRsp()
        src_abs_path = translate_absolute_path_from_install_path(cmd.srcPath)
        dst_abs_path = translate_absolute_path_from_install_path(cmd.destPath)

        with lvm.RecursiveOperateLv(src_abs_path, shared=True):
            virtual_size = linux.qcow2_virtualsize(src_abs_path)
            if not lvm.lv_exists(dst_abs_path):
                lvm.create_lv_from_absolute_path(dst_abs_path, virtual_size,
                                                 "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time()))
            with lvm.RecursiveOperateLv(dst_abs_path, shared=False):
                if not cmd.fullRebase:
                    linux.qcow2_rebase(src_abs_path, dst_abs_path)
                else:
                    tmp_lv = 'tmp_%s' % uuidhelper.uuid()
                    tmp_abs_path = os.path.join(os.path.dirname(dst_abs_path), tmp_lv)
                    tmp_abs_path = os.path.join(os.path.dirname(dst_abs_path), tmp_lv)
                    logger.debug("creating temp lv %s" % tmp_abs_path)
                    lvm.create_lv_from_absolute_path(tmp_abs_path, virtual_size,
                                                     "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time()))
                    with lvm.OperateLv(tmp_abs_path, shared=False, delete_when_exception=True):
                        linux.create_template(dst_abs_path, tmp_abs_path)
                        lvm.lv_rename(tmp_abs_path, dst_abs_path, overwrite=True)

        rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid)
        return jsonobject.dumps(rsp)
    def offline_merge_snapshots(self, req):
        cmd = jsonobject.loads(req[http.REQUEST_BODY])
        rsp = OfflineMergeSnapshotRsp()
        src_abs_path = translate_absolute_path_from_install_path(cmd.srcPath)
        dst_abs_path = translate_absolute_path_from_install_path(cmd.destPath)

        with lvm.RecursiveOperateLv(src_abs_path, shared=True):
            virtual_size = linux.qcow2_virtualsize(src_abs_path)
            if not lvm.lv_exists(dst_abs_path):
                lvm.create_lv_from_absolute_path(
                    dst_abs_path, virtual_size,
                    "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time()))
            with lvm.RecursiveOperateLv(dst_abs_path, shared=False):
                if not cmd.fullRebase:
                    linux.qcow2_rebase(src_abs_path, dst_abs_path)
                else:
                    tmp_lv = 'tmp_%s' % uuidhelper.uuid()
                    tmp_abs_path = os.path.join(os.path.dirname(dst_abs_path),
                                                tmp_lv)
                    tmp_abs_path = os.path.join(os.path.dirname(dst_abs_path),
                                                tmp_lv)
                    logger.debug("creating temp lv %s" % tmp_abs_path)
                    lvm.create_lv_from_absolute_path(
                        tmp_abs_path, virtual_size,
                        "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time()))
                    with lvm.OperateLv(tmp_abs_path,
                                       shared=False,
                                       delete_when_exception=True):
                        linux.create_template(dst_abs_path, tmp_abs_path)
                        lvm.lv_rename(tmp_abs_path,
                                      dst_abs_path,
                                      overwrite=True)

        rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid)
        return jsonobject.dumps(rsp)
def do_convert(args):
    volume_abs_path = args[1]
    raw_volume_abs_path = volume_abs_path + RAW_SUFFIX
    qcow2_volume_abs_path = volume_abs_path + QCOW2_SUFFIX
    check(volume_abs_path, raw_volume_abs_path)

    volume_size = lvm.get_lv_size(volume_abs_path)
    lvm.create_lv_from_absolute_path(raw_volume_abs_path, volume_size)
    with lvm.RecursiveOperateLv(volume_abs_path, shared=False, delete_when_exception=False):
        with lvm.OperateLv(raw_volume_abs_path, shared=False, delete_when_exception=False):
            lvm.add_lv_tag(volume_abs_path, START_TAG)
            r, o, e = qcow2_convert_to_raw(volume_abs_path, raw_volume_abs_path)
            if r != 0:
                logger.warn("convert failed: %s, removing tag and raw volume")
                lvm.clean_lv_tag(volume_abs_path, START_TAG)
                lvm.delete_lv(raw_volume_abs_path)
                return
            lvm.add_lv_tag(volume_abs_path, DONE_TAG)
            lvm.clean_lv_tag(volume_abs_path, START_TAG)
            lvm.lv_rename(volume_abs_path, qcow2_volume_abs_path, overwrite=False)
            lvm.lv_rename(raw_volume_abs_path, volume_abs_path, overwrite=False)