diff mbox

qemu-img: add the 'dd' subcommand

Message ID 20160714041047.14282-1-fullmanet@gmail.com
State New
Headers show

Commit Message

Reda Sallahi July 14, 2016, 4:10 a.m. UTC
This patch adds a basic dd subcommand analogous to dd(1) to qemu-img.

For the start, this implements the bs, if, of and count options and requires
both if and of to be specified (no stdin/stdout if not specified) and doesn't
support tty, pipes, etc.

The image format must be specified with -O for the output if the raw format
is not the intended one.

get_size() was needed for the size syntax dd(1) supports which is different
from qemu_strtosz_suffix().

Two tests are added to test qemu-img dd.

Signed-off-by: Reda Sallahi <fullmanet@gmail.com>
---
 qemu-img-cmds.hx           |   6 +
 qemu-img.c                 | 645 ++++++++++++++++++++++++++++++++++++++++++++-
 tests/qemu-iotests/158     |  53 ++++
 tests/qemu-iotests/158.out |  15 ++
 tests/qemu-iotests/159     |  56 ++++
 tests/qemu-iotests/159.out |  87 ++++++
 tests/qemu-iotests/group   |   2 +
 7 files changed, 863 insertions(+), 1 deletion(-)
 create mode 100755 tests/qemu-iotests/158
 create mode 100644 tests/qemu-iotests/158.out
 create mode 100755 tests/qemu-iotests/159
 create mode 100644 tests/qemu-iotests/159.out

Comments

Stefan Hajnoczi July 14, 2016, 12:19 p.m. UTC | #1
On Thu, Jul 14, 2016 at 06:10:47AM +0200, Reda Sallahi wrote:
> This patch adds a basic dd subcommand analogous to dd(1) to qemu-img.
> 
> For the start, this implements the bs, if, of and count options and requires
> both if and of to be specified (no stdin/stdout if not specified) and doesn't
> support tty, pipes, etc.
> 
> The image format must be specified with -O for the output if the raw format
> is not the intended one.
> 
> get_size() was needed for the size syntax dd(1) supports which is different
> from qemu_strtosz_suffix().

Please put this as a comment above the get_size() function definition in
the code.  This way people modifying the source code will not be tempted
to replace the function with something that breaks dd(1) syntax
compatibility.

> 
> Two tests are added to test qemu-img dd.
> 
> Signed-off-by: Reda Sallahi <fullmanet@gmail.com>
> ---
>  qemu-img-cmds.hx           |   6 +
>  qemu-img.c                 | 645 ++++++++++++++++++++++++++++++++++++++++++++-
>  tests/qemu-iotests/158     |  53 ++++
>  tests/qemu-iotests/158.out |  15 ++
>  tests/qemu-iotests/159     |  56 ++++
>  tests/qemu-iotests/159.out |  87 ++++++
>  tests/qemu-iotests/group   |   2 +
>  7 files changed, 863 insertions(+), 1 deletion(-)
>  create mode 100755 tests/qemu-iotests/158
>  create mode 100644 tests/qemu-iotests/158.out
>  create mode 100755 tests/qemu-iotests/159
>  create mode 100644 tests/qemu-iotests/159.out
> 
> diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
> index 7e95b2d..03bdd7a 100644
> --- a/qemu-img-cmds.hx
> +++ b/qemu-img-cmds.hx
> @@ -45,6 +45,12 @@ STEXI
>  @item convert [--object @var{objectdef}] [--image-opts] [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_id_or_name}] [-l @var{snapshot_param}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
>  ETEXI
>  
> +DEF("dd", img_dd,
> +    "dd [--image-opts] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] if=input of=output")
> +STEXI
> +@item dd [--image-opts] [-f @var{fmt}] [-O @var{output_fmt}] [bs=@var{block_size}] [count=@var{blocks}] if=@var{input} of=@var{output}
> +ETEXI
> +
>  DEF("info", img_info,
>      "info [--object objectdef] [--image-opts] [-f fmt] [--output=ofmt] [--backing-chain] filename")
>  STEXI
> diff --git a/qemu-img.c b/qemu-img.c
> index ea5970b..d7f134d 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -166,7 +166,14 @@ static void QEMU_NORETURN help(void)
>             "Parameters to compare subcommand:\n"
>             "  '-f' first image format\n"
>             "  '-F' second image format\n"
> -           "  '-s' run in Strict mode - fail on different image size or sector allocation\n";
> +           "  '-s' run in Strict mode - fail on different image size or sector allocation\n"
> +           "\n"
> +           "Parameters to dd subcommand:\n"
> +           "  'bs=BYTES' read and write up to BYTES bytes at a time "
> +           "(default: 512)\n"
> +           "  'count=N' copy only N input blocks\n"
> +           "  'if=FILE' read from FILE instead of stdin\n"
> +           "  'of=FILE' write to FILE instead of stdout\n";

This help text does not make sense since stdin/stdout is not supported.

>  
>      printf("%s\nSupported formats:", help_msg);
>      bdrv_iterate_format(format_print, NULL);
> @@ -3794,6 +3801,642 @@ out:
>      return 0;
>  }
>  
> +#define C_BS      01
> +#define C_CBS     02
> +#define C_CONV    04
> +#define C_COUNT   010
> +#define C_IBS     020
> +#define C_IF      040
> +#define C_IFLAG   0100
> +#define C_OBS     0200
> +#define C_OF      0400
> +#define C_OFLAG   01000
> +#define C_SEEK    02000
> +#define C_SKIP    04000
> +#define C_STATUS  010000

Please remove dead code from this patch.  This includes unused options,
fields, function, etc.  For example, cbs= is not implemented yet so
C_CBS, DdEss.cbsz, img_dd_cbs(), etc should be removed.

Patches must not add dead code because it is a review & maintenance
burden.  There is no guarantee that the dead code will ever be used.

> +
> +struct DdEss {

What does Ess mean?  Please choose a clearer name.

> +    unsigned int flags;
> +    unsigned int status;
> +    unsigned int conv;
> +    size_t count;
> +    size_t cbsz; /* Conversion block size */
> +};
> +
> +struct DdIo {
> +    size_t bsz;    /* Block size */
> +    off_t offset;
> +    const char *filename;
> +    unsigned int flags;
> +    uint8_t *buf;
> +};
> +
> +struct DdOpts {
> +    const char *name;
> +    int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdEss *);
> +    unsigned int flag;
> +};
> +
> +static size_t get_size(const char *str)
> +{
> +    /* XXX: handle {k,m,g}B notations */
> +    unsigned long num;
> +    size_t res = 0;
> +    const char *buf;
> +    int ret;
> +
> +    errno = 0;
> +    if (strchr(str, '-')) {
> +        error_report("invalid number: '%s'", str);
> +        errno = EINVAL;
> +        return res;
> +    }
> +    ret = qemu_strtoul(str, &buf, 0, &num);
> +
> +    if (ret < 0) {
> +        error_report("invalid number: '%s'", str);
> +        return res;
> +    }
> +
> +    switch (*buf) {
> +    case '\0':
> +    case 'c':
> +        res = num;
> +        break;
> +    case 'w':
> +        res = num * 2;
> +        break;
> +    case 'b':
> +        res = num * 512;
> +        break;
> +    case 'K':
> +        res = num * 1024;
> +        break;
> +    case 'M':
> +        res = num * 1024 * 1024;
> +        break;
> +    case 'G':
> +        res = num * 1024 * 1024 * 1024;
> +        break;
> +    case 'T':
> +        res = num * 1024 * 1024 * 1024 * 1024;
> +        break;
> +    case 'P':
> +        res = num * 1024 * 1024 * 1024 * 1024 * 1024;
> +        break;
> +    case 'E':
> +        res = num * 1024 * 1024 * 1024 * 1024 * 1024 * 1024;
> +        break;
> +    case 'Z':
> +        res = num * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024;
> +        break;
> +    case 'Y':
> +        res = num * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024;
> +        break;
> +    default:
> +        error_report("invalid number: '%s'", str);
> +        errno = EINVAL;
> +    }
> +
> +    return res;
> +}
> +
> +static int img_dd_bs(const char *arg,
> +                     struct DdIo *in, struct DdIo *out,
> +                     struct DdEss *dd)
> +{
> +    in->bsz = out->bsz = get_size(arg);
> +
> +    if (in->bsz == 0 && (errno == EINVAL || errno == ERANGE)) {
> +        return 1;
> +    }
> +    if (in->bsz == 0) {
> +        error_report("invalid number: '%s'", arg);
> +        return 1;
> +    }
> +
> +    return 0;
> +}
> +
> +static int img_dd_cbs(const char *arg,
> +                      struct DdIo *in, struct DdIo *out,
> +                      struct DdEss *dd)
> +{
> +    dd->cbsz = get_size(arg);
> +
> +    if (dd->cbsz == 0 && (errno == EINVAL || errno == ERANGE)) {
> +        return 1;
> +    }
> +    if (dd->cbsz == 0) {
> +        error_report("invalid number: '%s'", arg);
> +        return 1;
> +    }
> +
> +    return 0;
> +}
> +
> +struct DdSymbols {
> +    const char *name;
> +    unsigned int value;
> +};
> +
> +#define C_ASCII    01
> +#define C_EBCDIC   02
> +#define C_IBM      04
> +#define C_BLOCK    010
> +#define C_UNBLOCK  020
> +#define C_LCASE    040
> +#define C_UCASE    0100
> +#define C_SPARSE   0200
> +#define C_SWAB     0400
> +#define C_SYNC     01000
> +#define C_EXCL     02000
> +#define C_NOCREAT  04000
> +#define C_NOTRUNC  010000
> +#define C_NOERROR  020000
> +#define C_FDATASYNC 040000
> +#define C_FSYNC     0100000
> +
> +static int img_dd_conv(const char *arg,
> +                       struct DdIo *in, struct DdIo *out,
> +                       struct DdEss *dd)
> +{
> +    const char *tok;
> +    char *str, *tmp;
> +    int ret = 0;
> +    const struct DdSymbols conv[] = {
> +        { "ascii", C_ASCII },
> +        { "ebcdic", C_EBCDIC },
> +        { "ibm", C_IBM },
> +        { "block", C_BLOCK },
> +        { "unblock", C_UNBLOCK },
> +        { "lcase", C_LCASE },
> +        { "ucase", C_UCASE },
> +        { "sparse", C_SPARSE },
> +        { "sync", C_SYNC },
> +        { "excl", C_EXCL },
> +        { "nocreat", C_NOCREAT },
> +        { "notrunc", C_NOTRUNC },
> +        { "noerror", C_NOERROR },
> +        { "fdatasync", C_FDATASYNC },
> +        { "fsync", C_FSYNC },
> +        { NULL, 0 }
> +    };
> +
> +    tmp = str = g_strdup(arg);
> +
> +    while (tmp != NULL && !ret) {
> +        tok = qemu_strsep(&tmp, ",");
> +        int j;
> +        for (j = 0; conv[j].name != NULL; j++) {
> +            if (!strcmp(tok, conv[j].name)) {
> +                dd->conv |= conv[j].value;
> +                break;
> +            }
> +        }
> +        if (conv[j].name == NULL) {
> +            error_report("invalid conversion: '%s'", tok);
> +            ret = 1;
> +        }
> +    }
> +
> +    g_free(str);
> +    return ret;
> +}
> +
> +static int img_dd_count(const char *arg,
> +                        struct DdIo *in, struct DdIo *out,
> +                        struct DdEss *dd)
> +{
> +    dd->count = get_size(arg);
> +
> +    if (dd->count == 0 && (errno == EINVAL || errno == ERANGE)) {
> +        return 1;
> +    }
> +
> +    return 0;
> +}
> +
> +static int img_dd_ibs(const char *arg,
> +                      struct DdIo *in, struct DdIo *out,
> +                      struct DdEss *dd)
> +{
> +    if (dd->flags & C_BS) { /* POSIX says bs supersedes ibs and obs */
> +        return 0;
> +    }
> +
> +    in->bsz = get_size(arg);
> +
> +    if (in->bsz == 0 && (errno == EINVAL || errno == ERANGE)) {
> +        return 1;
> +    }
> +    if (in->bsz == 0) {
> +        error_report("invalid number: '%s'", arg);
> +        return 1;
> +    }
> +
> +    return 0;
> +}
> +
> +static int img_dd_if(const char *arg,
> +                     struct DdIo *in, struct DdIo *out,
> +                     struct DdEss *dd)
> +{
> +    in->filename = arg;
> +
> +    return 0;
> +}
> +
> +#define C_APPEND      01
> +#define C_DIRECT      02
> +#define C_DIRECTORY   04
> +#define C_DSYNC       010
> +#define C_SYNC_FLAG   020
> +#define C_FULLBLOCK   040
> +#define C_NONBLOCK    0100
> +#define C_NOATIME     0200
> +#define C_NOCACHE     0400
> +#define C_NOCTTY      01000
> +#define C_NOFOLLOW    02000
> +#define C_COUNT_BYTES 04000
> +#define C_SKIP_BYTES  010000
> +#define C_SEEK_BYTES  020000
> +
> +static int img_dd_iflag(const char *arg,
> +                        struct DdIo *in, struct DdIo *out,
> +                        struct DdEss *dd)
> +{
> +    const struct DdSymbols flags[] = {
> +        { "direct", C_DIRECT },
> +        { "directory", C_DIRECTORY },
> +        { "dsync", C_DSYNC },
> +        { "sync", C_SYNC_FLAG },
> +        { "fullblock", C_FULLBLOCK },
> +        { "nonblock", C_NONBLOCK },
> +        { "noatime", C_NOATIME },
> +        { "nocache", C_NOCACHE },
> +        { "noctty", C_NOCTTY },
> +        { "nofollow", C_NOFOLLOW },
> +        { "count_bytes", C_COUNT_BYTES },
> +        { "skip_bytes", C_SKIP_BYTES },
> +        { NULL, 0}
> +    };
> +
> +    for (int j = 0; flags[j].name != NULL; j++) {
> +        if (!strcmp(arg, flags[j].name)) {
> +            in->flags = flags[j].value;
> +            return 0;
> +        }
> +    }
> +
> +    error_report("invalid input flag: '%s'", arg);
> +    return 1;
> +}
> +
> +static int img_dd_obs(const char *arg,
> +                      struct DdIo *in, struct DdIo *out,
> +                      struct DdEss *dd)
> +{
> +    if (dd->flags & C_BS) { /* POSIX says bs supersedes ibs and obs */
> +        return 0;
> +    }
> +
> +    out->bsz = get_size(arg);
> +
> +    if (out->bsz == 0 && (errno == EINVAL || errno == ERANGE)) {
> +        return 1;
> +    }
> +    if (out->bsz == 0) {
> +        error_report("invalid number: '%s'", arg);
> +        return 1;
> +    }
> +
> +    return 0;
> +}
> +
> +static int img_dd_of(const char *arg,
> +                     struct DdIo *in, struct DdIo *out,
> +                     struct DdEss *dd)
> +{
> +    out->filename = arg;
> +
> +    return 0;
> +}
> +
> +static int img_dd_oflag(const char *arg,
> +                        struct DdIo *in, struct DdIo *out,
> +                        struct DdEss *dd)
> +{
> +    const struct DdSymbols flags[] = {
> +        { "append", C_APPEND },
> +        { "direct", C_DIRECT },
> +        { "directory", C_DIRECTORY },
> +        { "dsync", C_DSYNC },
> +        { "sync", C_SYNC_FLAG },
> +        { "nonblock", C_NONBLOCK },
> +        { "noatime", C_NOATIME },
> +        { "nocache", C_NOCACHE },
> +        { "noctty", C_NOCTTY },
> +        { "nofollow", C_NOFOLLOW },
> +        { "seek_bytes", C_SEEK_BYTES },
> +        { NULL, 0 }
> +    };
> +
> +    for (int j = 0; flags[j].name != NULL; j++) {
> +        if (!strcmp(arg, flags[j].name)) {
> +            out->flags = flags[j].value;
> +            return 0;
> +        }
> +    }
> +
> +    error_report("invalid output flag: '%s'", arg);
> +    return 1;
> +}
> +
> +static int img_dd_seek(const char *arg,
> +                       struct DdIo *in, struct DdIo *out,
> +                       struct DdEss *dd)
> +{
> +    out->offset = get_size(arg);
> +
> +    if (out->offset == 0 && (errno == EINVAL || errno == ERANGE)) {
> +        return 1;
> +    }
> +
> +    return 0;
> +}
> +
> +static int img_dd_skip(const char *arg,
> +                       struct DdIo *in, struct DdIo *out,
> +                       struct DdEss *dd)
> +{
> +    in->offset = get_size(arg);
> +
> +    if (in->offset == 0 && (errno == EINVAL || errno == ERANGE)) {
> +        return 1;
> +    }
> +
> +    return 0;
> +}
> +
> +#define C_STATUS_DEFAULT  00
> +#define C_STATUS_NONE     01
> +#define C_STATUS_NOXFER   02
> +#define C_STATUS_PROGRESS 04
> +
> +static int img_dd_status(const char *arg,
> +                         struct DdIo *in, struct DdIo *out,
> +                         struct DdEss *dd)
> +{
> +    const struct DdSymbols dd_status[] = {
> +        { "none", C_STATUS_NONE },
> +        { "noxfer", C_STATUS_NOXFER },
> +        { "progress", C_STATUS_PROGRESS },
> +        { "default", C_STATUS_DEFAULT },
> +        { NULL, 0 }
> +    };
> +
> +    for (int j = 0; dd_status[j].name != NULL; j++) {
> +        if (!strcmp(arg, dd_status[j].name)) {
> +            dd->status = dd_status[j].value;
> +            return 0;
> +        }
> +    }
> +
> +    error_report("invalid status level: '%s'", arg);
> +    return 1;
> +}
> +
> +static int img_dd(int argc, char **argv)
> +{
> +    int ret = 0;
> +    char *arg = NULL;
> +    char *tmp;
> +    BlockDriver *drv = NULL, *proto_drv = NULL;
> +    BlockBackend *blk1 = NULL, *blk2 = NULL;
> +    QemuOpts *opts = NULL;
> +    QemuOptsList *create_opts = NULL;
> +    Error *local_err = NULL;
> +    bool image_opts = false;
> +    int c;
> +    const char *out_fmt = "raw";
> +    const char *fmt = NULL;
> +    int64_t size = 0;
> +    int64_t block_count = 0, incount = 0, outcount = 0;
> +    struct DdEss dd = {
> +        .flags = 0,
> +        .status = C_STATUS_DEFAULT,
> +        .conv = 0,
> +        .count = 0,
> +        .cbsz = 512
> +    };
> +    struct DdIo in = {
> +        .bsz = 512, /* Block size is by default 512 bytes */
> +        .offset = 0,
> +        .filename = NULL,
> +        .flags = 0,
> +        .buf = NULL
> +    };
> +    struct DdIo out = {
> +        .bsz = 512,
> +        .offset = 0,
> +        .filename = NULL,
> +        .flags = 0,
> +        .buf = NULL
> +    };
> +
> +    const struct DdOpts options[] = {
> +        { "bs", img_dd_bs, C_BS },
> +        { "cbs", img_dd_cbs, C_CBS },
> +        { "conv", img_dd_conv, C_CONV },
> +        { "count", img_dd_count, C_COUNT },
> +        { "ibs", img_dd_ibs, C_IBS },
> +        { "if", img_dd_if, C_IF },
> +        { "iflag", img_dd_iflag, C_IFLAG },
> +        { "obs", img_dd_obs, C_OBS },
> +        { "of", img_dd_of, C_OF },
> +        { "oflag", img_dd_oflag, C_OFLAG },
> +        { "seek", img_dd_seek, C_SEEK },
> +        { "skip", img_dd_skip, C_SKIP },
> +        { "status", img_dd_status, C_STATUS },
> +        { NULL, NULL, 0 }
> +    };
> +    const struct option long_options[] = {
> +        { "help", no_argument, 0, 'h'},
> +        { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
> +        { 0, 0, 0, 0 }
> +    };
> +
> +    while ((c = getopt_long(argc, argv, "hf:O:", long_options, NULL))) {
> +        if (c == EOF) {
> +            break;
> +        }
> +        switch (c) {
> +        case 'O':
> +            out_fmt = optarg;
> +            break;
> +        case 'f':
> +            fmt = optarg;
> +            break;
> +        case '?':
> +            error_report("Try 'qemu-img --help' for more information.");
> +            ret = -1;
> +            goto out;
> +            break;
> +        case 'h':
> +            help();
> +            break;
> +        case OPTION_IMAGE_OPTS:
> +            image_opts = true;
> +            break;
> +        }
> +    }
> +
> +    for (int i = optind; i < argc; i++) {
> +        int j;
> +        arg = g_strdup(argv[i]);
> +
> +        tmp = strchr(arg, '=');
> +        if (tmp == NULL) {
> +            error_report("unrecognized operand %s", arg);
> +            ret = -1;
> +            goto out;
> +        }
> +
> +        *tmp++ = '\0';
> +
> +        for (j = 0; options[j].name != NULL; j++) {
> +            if (!strcmp(arg, options[j].name)) {
> +                break;
> +            }
> +        }
> +        if (options[j].name == NULL) {
> +            error_report("unrecognized operand %s", arg);
> +            ret = -1;
> +            goto out;
> +        }
> +
> +        if (options[j].f(tmp, &in, &out, &dd) != 0) {
> +            ret = -1;
> +            goto out;
> +        }
> +        dd.flags |= options[j].flag;

arg is leaked.

> +    }
> +
> +    if (dd.flags & C_IF && dd.flags & C_OF) {
> +        blk1 = img_open(image_opts, in.filename, fmt, 0, false, true);
> +
> +        if (!blk1) {
> +            ret = -1;
> +            goto out;
> +        }
> +
> +        drv = bdrv_find_format(out_fmt);
> +        if (!drv) {
> +            error_report("Unknown file format");
> +            ret = -1;
> +            goto out;
> +        }
> +        proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
> +
> +        if (!proto_drv) {
> +            error_report_err(local_err);
> +            ret = -1;
> +            goto out;
> +        }
> +        if (!drv->create_opts) {
> +            error_report("Format driver '%s' does not support image creation",
> +                         drv->format_name);
> +            ret = -1;
> +            goto out;
> +        }
> +        if (!proto_drv->create_opts) {
> +            error_report("Protocol driver '%s' does not support image creation",
> +                       proto_drv->format_name);
> +            ret = -1;
> +            goto out;
> +        }
> +        create_opts = qemu_opts_append(create_opts, drv->create_opts);
> +        create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
> +
> +        opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
> +
> +        size =  blk_getlength(blk1);
> +
> +        if (dd.flags & C_COUNT && dd.count * in.bsz < size) {
> +            size = dd.count * in.bsz;
> +        }
> +
> +        qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
> +
> +        ret = bdrv_create(drv, out.filename, opts, &local_err);
> +        if (ret < 0) {
> +            error_reportf_err(local_err,
> +                              "%s: error while copying and converting: ",
> +                              out.filename);
> +            ret = -1;
> +            goto out;
> +        }
> +
> +        blk2 = img_open(image_opts, out.filename, out_fmt, BDRV_O_RDWR,
> +                        false, true);
> +
> +        if (!blk2) {
> +            ret = -1;
> +            goto out;
> +        }
> +
> +        in.buf = g_new(uint8_t, in.bsz);
> +
> +        for (; incount < size; incount += ret, block_count++) {
> +            int out_ret;
> +
> +            /* If the count option is specified. */
> +            if (dd.flags & C_COUNT && block_count >= dd.count) {
> +                break;
> +            }

Why is this necessary when size was already set based on C_COUNT above?

> +
> +            if (in.bsz + incount > size) {
> +                ret = blk_pread(blk1, incount, in.buf, size - incount);
> +            } else {
> +                ret = blk_pread(blk1, incount, in.buf, in.bsz);
> +            }
> +            if (ret < 0) {
> +                error_report("error while reading from input image file: %s",
> +                             strerror(-ret));
> +                ret = -1;
> +                goto out;
> +            }
> +            out_ret = blk_pwrite(blk2, outcount, in.buf, ret, 0);
> +
> +            if (out_ret < 0) {
> +                error_report("error while writing to output image file: %s",
> +                             strerror(-out_ret));
> +                ret = -1;
> +                goto out;
> +            }
> +            outcount += out_ret;
> +        }
> +    } else {
> +        error_report("Must specify both input and output files");
> +        ret = -1;
> +        goto out;
> +    }

Indentation can be avoided with:

  if (!(dd.flags & C_IF && dd.flags & C_OF)) {
      error_report("Must specify both input and output files");
      ret = -1;
      goto out;
  }

  ...code for if=/of=...

> +out:
> +    g_free(arg);
> +    qemu_opts_del(opts);
> +    qemu_opts_free(create_opts);
> +    blk_unref(blk1);
> +    blk_unref(blk2);
> +    g_free(in.buf);
> +    g_free(out.buf);
> +
> +    if (ret) {
> +        return 1;
> +    }
> +    return 0;
> +}
> +
>  
>  static const img_cmd_t img_cmds[] = {
>  #define DEF(option, callback, arg_string)        \
> diff --git a/tests/qemu-iotests/158 b/tests/qemu-iotests/158
> new file mode 100755
> index 0000000..bb5e715
> --- /dev/null
> +++ b/tests/qemu-iotests/158
> @@ -0,0 +1,53 @@
> +#! /bin/bash
> +#
> +# qemu-img dd test
> +
> +owner=fullmanet@gmail.com
> +
> +seq="$(basename $0)"
> +echo "QA output created by $seq"
> +
> +here="$PWD"
> +status=1
> +
> +_cleanup()
> +{
> +    _cleanup_test_img
> +}

$TEST_IMG.out also needs to be deleted.

> +trap "_cleanup; exit \$status" 0 1 2 3 15
> +
> +. ./common.rc
> +. ./common.filter
> +. ./common.pattern
> +
> +_supported_fmt generic
> +_supported_proto file
> +_supported_os Linux
> +
> +echo
> +echo "== Creating image =="
> +
> +size=1M
> +_make_test_img $size
> +_check_test_img
> +
> +$QEMU_IO -c "write -P 0xa 0 $size" "$TEST_IMG" | _filter_qemu_io
> +
> +echo
> +echo "== Converting the image with dd =="
> +
> +$QEMU_IMG dd if="$TEST_IMG" of="$TEST_IMG.out" -O "$IMGFMT"
> +$QEMU_IMG check "$TEST_IMG.out" -f "$IMGFMT" 2>&1  | _filter_testdir | \
> +    sed -e '/allocated.*fragmented.*compressed clusters/d' \
> +        -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
> +        -e '/Image end offset: [0-9]\+/d'
> +
> +echo
> +echo "== Compare the images with qemu-img compare =="
> +
> +$QEMU_IMG compare "$TEST_IMG" "$TEST_IMG.out"
> +
> +echo
> +echo "*** done"
> +rm -f "$seq.full"
> +status=0
> diff --git a/tests/qemu-iotests/158.out b/tests/qemu-iotests/158.out
> new file mode 100644
> index 0000000..58bfd85
> --- /dev/null
> +++ b/tests/qemu-iotests/158.out
> @@ -0,0 +1,15 @@
> +QA output created by 158
> +
> +== Creating image ==
> +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
> +No errors were found on the image.
> +wrote 1048576/1048576 bytes at offset 0
> +1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +
> +== Converting the image with dd ==
> +No errors were found on the image.
> +
> +== Compare the images with qemu-img compare ==
> +Images are identical.
> +
> +*** done
> diff --git a/tests/qemu-iotests/159 b/tests/qemu-iotests/159
> new file mode 100755
> index 0000000..4c9e4ec
> --- /dev/null
> +++ b/tests/qemu-iotests/159
> @@ -0,0 +1,56 @@
> +#! /bin/bash
> +#
> +# qemu-img dd test with different block sizes 
> +
> +owner=fullmanet@gmail.com
> +
> +seq="$(basename $0)"
> +echo "QA output created by $seq"
> +
> +here="$PWD"
> +status=1
> +
> +_cleanup()
> +{
> +    _cleanup_test_img
> +}
> +trap "_cleanup; exit \$status" 0 1 2 3 15
> +
> +. ./common.rc
> +. ./common.filter
> +. ./common.pattern
> +
> +_supported_fmt generic
> +_supported_proto file
> +_supported_os Linux
> +
> +TEST_SIZES="5 512 1024 1999 1K 64K 1M"
> +
> +for bs in $TEST_SIZES; do
> +    echo
> +    echo "== Creating image =="
> +
> +    size=1M
> +    _make_test_img $size
> +    _check_test_img
> +    $QEMU_IO -c "write -P 0xa 0 $size" "$TEST_IMG" | _filter_qemu_io
> +
> +    echo
> +    echo "== Converting the image with dd with a block size of $bs =="
> +
> +    $QEMU_IMG dd if="$TEST_IMG" of="$TEST_IMG.out" bs=$bs -O "$IMGFMT"
> +    $QEMU_IMG check "$TEST_IMG.out" -f "$IMGFMT" 2>&1  | _filter_testdir | \
> +	sed -e '/allocated.*fragmented.*compressed clusters/d' \
> +            -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
> +            -e '/Image end offset: [0-9]\+/d'
> +
> +    echo
> +    echo "== Compare the images with qemu-img compare =="
> +
> +    $QEMU_IMG compare "$TEST_IMG" "$TEST_IMG.out"
> +done
> +
> +echo
> +echo "*** done"
> +rm -f "$seq.full"
> +status=0
> diff --git a/tests/qemu-iotests/159.out b/tests/qemu-iotests/159.out
> new file mode 100644
> index 0000000..b86b63a
> --- /dev/null
> +++ b/tests/qemu-iotests/159.out
> @@ -0,0 +1,87 @@
> +QA output created by 159
> +
> +== Creating image ==
> +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
> +No errors were found on the image.
> +wrote 1048576/1048576 bytes at offset 0
> +1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +
> +== Converting the image with dd with a block size of 5 ==
> +No errors were found on the image.
> +
> +== Compare the images with qemu-img compare ==
> +Images are identical.
> +
> +== Creating image ==
> +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
> +No errors were found on the image.
> +wrote 1048576/1048576 bytes at offset 0
> +1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +
> +== Converting the image with dd with a block size of 512 ==
> +No errors were found on the image.
> +
> +== Compare the images with qemu-img compare ==
> +Images are identical.
> +
> +== Creating image ==
> +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
> +No errors were found on the image.
> +wrote 1048576/1048576 bytes at offset 0
> +1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +
> +== Converting the image with dd with a block size of 1024 ==
> +No errors were found on the image.
> +
> +== Compare the images with qemu-img compare ==
> +Images are identical.
> +
> +== Creating image ==
> +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
> +No errors were found on the image.
> +wrote 1048576/1048576 bytes at offset 0
> +1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +
> +== Converting the image with dd with a block size of 1999 ==
> +No errors were found on the image.
> +
> +== Compare the images with qemu-img compare ==
> +Images are identical.
> +
> +== Creating image ==
> +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
> +No errors were found on the image.
> +wrote 1048576/1048576 bytes at offset 0
> +1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +
> +== Converting the image with dd with a block size of 1K ==
> +No errors were found on the image.
> +
> +== Compare the images with qemu-img compare ==
> +Images are identical.
> +
> +== Creating image ==
> +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
> +No errors were found on the image.
> +wrote 1048576/1048576 bytes at offset 0
> +1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +
> +== Converting the image with dd with a block size of 64K ==
> +No errors were found on the image.
> +
> +== Compare the images with qemu-img compare ==
> +Images are identical.
> +
> +== Creating image ==
> +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
> +No errors were found on the image.
> +wrote 1048576/1048576 bytes at offset 0
> +1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +
> +== Converting the image with dd with a block size of 1M ==
> +No errors were found on the image.
> +
> +== Compare the images with qemu-img compare ==
> +Images are identical.
> +
> +*** done
> diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group
> index 1c6fcb6..6c3d6fe 100644
> --- a/tests/qemu-iotests/group
> +++ b/tests/qemu-iotests/group
> @@ -156,3 +156,5 @@
>  154 rw auto backing quick
>  155 rw auto
>  156 rw auto quick
> +158 rw auto quick
> +159 rw auto quick
> -- 
> 2.9.0
> 
>
diff mbox

Patch

diff --git a/qemu-img-cmds.hx b/qemu-img-cmds.hx
index 7e95b2d..03bdd7a 100644
--- a/qemu-img-cmds.hx
+++ b/qemu-img-cmds.hx
@@ -45,6 +45,12 @@  STEXI
 @item convert [--object @var{objectdef}] [--image-opts] [-c] [-p] [-q] [-n] [-f @var{fmt}] [-t @var{cache}] [-T @var{src_cache}] [-O @var{output_fmt}] [-o @var{options}] [-s @var{snapshot_id_or_name}] [-l @var{snapshot_param}] [-S @var{sparse_size}] @var{filename} [@var{filename2} [...]] @var{output_filename}
 ETEXI
 
+DEF("dd", img_dd,
+    "dd [--image-opts] [-f fmt] [-O output_fmt] [bs=block_size] [count=blocks] if=input of=output")
+STEXI
+@item dd [--image-opts] [-f @var{fmt}] [-O @var{output_fmt}] [bs=@var{block_size}] [count=@var{blocks}] if=@var{input} of=@var{output}
+ETEXI
+
 DEF("info", img_info,
     "info [--object objectdef] [--image-opts] [-f fmt] [--output=ofmt] [--backing-chain] filename")
 STEXI
diff --git a/qemu-img.c b/qemu-img.c
index ea5970b..d7f134d 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -166,7 +166,14 @@  static void QEMU_NORETURN help(void)
            "Parameters to compare subcommand:\n"
            "  '-f' first image format\n"
            "  '-F' second image format\n"
-           "  '-s' run in Strict mode - fail on different image size or sector allocation\n";
+           "  '-s' run in Strict mode - fail on different image size or sector allocation\n"
+           "\n"
+           "Parameters to dd subcommand:\n"
+           "  'bs=BYTES' read and write up to BYTES bytes at a time "
+           "(default: 512)\n"
+           "  'count=N' copy only N input blocks\n"
+           "  'if=FILE' read from FILE instead of stdin\n"
+           "  'of=FILE' write to FILE instead of stdout\n";
 
     printf("%s\nSupported formats:", help_msg);
     bdrv_iterate_format(format_print, NULL);
@@ -3794,6 +3801,642 @@  out:
     return 0;
 }
 
+#define C_BS      01
+#define C_CBS     02
+#define C_CONV    04
+#define C_COUNT   010
+#define C_IBS     020
+#define C_IF      040
+#define C_IFLAG   0100
+#define C_OBS     0200
+#define C_OF      0400
+#define C_OFLAG   01000
+#define C_SEEK    02000
+#define C_SKIP    04000
+#define C_STATUS  010000
+
+struct DdEss {
+    unsigned int flags;
+    unsigned int status;
+    unsigned int conv;
+    size_t count;
+    size_t cbsz; /* Conversion block size */
+};
+
+struct DdIo {
+    size_t bsz;    /* Block size */
+    off_t offset;
+    const char *filename;
+    unsigned int flags;
+    uint8_t *buf;
+};
+
+struct DdOpts {
+    const char *name;
+    int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdEss *);
+    unsigned int flag;
+};
+
+static size_t get_size(const char *str)
+{
+    /* XXX: handle {k,m,g}B notations */
+    unsigned long num;
+    size_t res = 0;
+    const char *buf;
+    int ret;
+
+    errno = 0;
+    if (strchr(str, '-')) {
+        error_report("invalid number: '%s'", str);
+        errno = EINVAL;
+        return res;
+    }
+    ret = qemu_strtoul(str, &buf, 0, &num);
+
+    if (ret < 0) {
+        error_report("invalid number: '%s'", str);
+        return res;
+    }
+
+    switch (*buf) {
+    case '\0':
+    case 'c':
+        res = num;
+        break;
+    case 'w':
+        res = num * 2;
+        break;
+    case 'b':
+        res = num * 512;
+        break;
+    case 'K':
+        res = num * 1024;
+        break;
+    case 'M':
+        res = num * 1024 * 1024;
+        break;
+    case 'G':
+        res = num * 1024 * 1024 * 1024;
+        break;
+    case 'T':
+        res = num * 1024 * 1024 * 1024 * 1024;
+        break;
+    case 'P':
+        res = num * 1024 * 1024 * 1024 * 1024 * 1024;
+        break;
+    case 'E':
+        res = num * 1024 * 1024 * 1024 * 1024 * 1024 * 1024;
+        break;
+    case 'Z':
+        res = num * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024;
+        break;
+    case 'Y':
+        res = num * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024;
+        break;
+    default:
+        error_report("invalid number: '%s'", str);
+        errno = EINVAL;
+    }
+
+    return res;
+}
+
+static int img_dd_bs(const char *arg,
+                     struct DdIo *in, struct DdIo *out,
+                     struct DdEss *dd)
+{
+    in->bsz = out->bsz = get_size(arg);
+
+    if (in->bsz == 0 && (errno == EINVAL || errno == ERANGE)) {
+        return 1;
+    }
+    if (in->bsz == 0) {
+        error_report("invalid number: '%s'", arg);
+        return 1;
+    }
+
+    return 0;
+}
+
+static int img_dd_cbs(const char *arg,
+                      struct DdIo *in, struct DdIo *out,
+                      struct DdEss *dd)
+{
+    dd->cbsz = get_size(arg);
+
+    if (dd->cbsz == 0 && (errno == EINVAL || errno == ERANGE)) {
+        return 1;
+    }
+    if (dd->cbsz == 0) {
+        error_report("invalid number: '%s'", arg);
+        return 1;
+    }
+
+    return 0;
+}
+
+struct DdSymbols {
+    const char *name;
+    unsigned int value;
+};
+
+#define C_ASCII    01
+#define C_EBCDIC   02
+#define C_IBM      04
+#define C_BLOCK    010
+#define C_UNBLOCK  020
+#define C_LCASE    040
+#define C_UCASE    0100
+#define C_SPARSE   0200
+#define C_SWAB     0400
+#define C_SYNC     01000
+#define C_EXCL     02000
+#define C_NOCREAT  04000
+#define C_NOTRUNC  010000
+#define C_NOERROR  020000
+#define C_FDATASYNC 040000
+#define C_FSYNC     0100000
+
+static int img_dd_conv(const char *arg,
+                       struct DdIo *in, struct DdIo *out,
+                       struct DdEss *dd)
+{
+    const char *tok;
+    char *str, *tmp;
+    int ret = 0;
+    const struct DdSymbols conv[] = {
+        { "ascii", C_ASCII },
+        { "ebcdic", C_EBCDIC },
+        { "ibm", C_IBM },
+        { "block", C_BLOCK },
+        { "unblock", C_UNBLOCK },
+        { "lcase", C_LCASE },
+        { "ucase", C_UCASE },
+        { "sparse", C_SPARSE },
+        { "sync", C_SYNC },
+        { "excl", C_EXCL },
+        { "nocreat", C_NOCREAT },
+        { "notrunc", C_NOTRUNC },
+        { "noerror", C_NOERROR },
+        { "fdatasync", C_FDATASYNC },
+        { "fsync", C_FSYNC },
+        { NULL, 0 }
+    };
+
+    tmp = str = g_strdup(arg);
+
+    while (tmp != NULL && !ret) {
+        tok = qemu_strsep(&tmp, ",");
+        int j;
+        for (j = 0; conv[j].name != NULL; j++) {
+            if (!strcmp(tok, conv[j].name)) {
+                dd->conv |= conv[j].value;
+                break;
+            }
+        }
+        if (conv[j].name == NULL) {
+            error_report("invalid conversion: '%s'", tok);
+            ret = 1;
+        }
+    }
+
+    g_free(str);
+    return ret;
+}
+
+static int img_dd_count(const char *arg,
+                        struct DdIo *in, struct DdIo *out,
+                        struct DdEss *dd)
+{
+    dd->count = get_size(arg);
+
+    if (dd->count == 0 && (errno == EINVAL || errno == ERANGE)) {
+        return 1;
+    }
+
+    return 0;
+}
+
+static int img_dd_ibs(const char *arg,
+                      struct DdIo *in, struct DdIo *out,
+                      struct DdEss *dd)
+{
+    if (dd->flags & C_BS) { /* POSIX says bs supersedes ibs and obs */
+        return 0;
+    }
+
+    in->bsz = get_size(arg);
+
+    if (in->bsz == 0 && (errno == EINVAL || errno == ERANGE)) {
+        return 1;
+    }
+    if (in->bsz == 0) {
+        error_report("invalid number: '%s'", arg);
+        return 1;
+    }
+
+    return 0;
+}
+
+static int img_dd_if(const char *arg,
+                     struct DdIo *in, struct DdIo *out,
+                     struct DdEss *dd)
+{
+    in->filename = arg;
+
+    return 0;
+}
+
+#define C_APPEND      01
+#define C_DIRECT      02
+#define C_DIRECTORY   04
+#define C_DSYNC       010
+#define C_SYNC_FLAG   020
+#define C_FULLBLOCK   040
+#define C_NONBLOCK    0100
+#define C_NOATIME     0200
+#define C_NOCACHE     0400
+#define C_NOCTTY      01000
+#define C_NOFOLLOW    02000
+#define C_COUNT_BYTES 04000
+#define C_SKIP_BYTES  010000
+#define C_SEEK_BYTES  020000
+
+static int img_dd_iflag(const char *arg,
+                        struct DdIo *in, struct DdIo *out,
+                        struct DdEss *dd)
+{
+    const struct DdSymbols flags[] = {
+        { "direct", C_DIRECT },
+        { "directory", C_DIRECTORY },
+        { "dsync", C_DSYNC },
+        { "sync", C_SYNC_FLAG },
+        { "fullblock", C_FULLBLOCK },
+        { "nonblock", C_NONBLOCK },
+        { "noatime", C_NOATIME },
+        { "nocache", C_NOCACHE },
+        { "noctty", C_NOCTTY },
+        { "nofollow", C_NOFOLLOW },
+        { "count_bytes", C_COUNT_BYTES },
+        { "skip_bytes", C_SKIP_BYTES },
+        { NULL, 0}
+    };
+
+    for (int j = 0; flags[j].name != NULL; j++) {
+        if (!strcmp(arg, flags[j].name)) {
+            in->flags = flags[j].value;
+            return 0;
+        }
+    }
+
+    error_report("invalid input flag: '%s'", arg);
+    return 1;
+}
+
+static int img_dd_obs(const char *arg,
+                      struct DdIo *in, struct DdIo *out,
+                      struct DdEss *dd)
+{
+    if (dd->flags & C_BS) { /* POSIX says bs supersedes ibs and obs */
+        return 0;
+    }
+
+    out->bsz = get_size(arg);
+
+    if (out->bsz == 0 && (errno == EINVAL || errno == ERANGE)) {
+        return 1;
+    }
+    if (out->bsz == 0) {
+        error_report("invalid number: '%s'", arg);
+        return 1;
+    }
+
+    return 0;
+}
+
+static int img_dd_of(const char *arg,
+                     struct DdIo *in, struct DdIo *out,
+                     struct DdEss *dd)
+{
+    out->filename = arg;
+
+    return 0;
+}
+
+static int img_dd_oflag(const char *arg,
+                        struct DdIo *in, struct DdIo *out,
+                        struct DdEss *dd)
+{
+    const struct DdSymbols flags[] = {
+        { "append", C_APPEND },
+        { "direct", C_DIRECT },
+        { "directory", C_DIRECTORY },
+        { "dsync", C_DSYNC },
+        { "sync", C_SYNC_FLAG },
+        { "nonblock", C_NONBLOCK },
+        { "noatime", C_NOATIME },
+        { "nocache", C_NOCACHE },
+        { "noctty", C_NOCTTY },
+        { "nofollow", C_NOFOLLOW },
+        { "seek_bytes", C_SEEK_BYTES },
+        { NULL, 0 }
+    };
+
+    for (int j = 0; flags[j].name != NULL; j++) {
+        if (!strcmp(arg, flags[j].name)) {
+            out->flags = flags[j].value;
+            return 0;
+        }
+    }
+
+    error_report("invalid output flag: '%s'", arg);
+    return 1;
+}
+
+static int img_dd_seek(const char *arg,
+                       struct DdIo *in, struct DdIo *out,
+                       struct DdEss *dd)
+{
+    out->offset = get_size(arg);
+
+    if (out->offset == 0 && (errno == EINVAL || errno == ERANGE)) {
+        return 1;
+    }
+
+    return 0;
+}
+
+static int img_dd_skip(const char *arg,
+                       struct DdIo *in, struct DdIo *out,
+                       struct DdEss *dd)
+{
+    in->offset = get_size(arg);
+
+    if (in->offset == 0 && (errno == EINVAL || errno == ERANGE)) {
+        return 1;
+    }
+
+    return 0;
+}
+
+#define C_STATUS_DEFAULT  00
+#define C_STATUS_NONE     01
+#define C_STATUS_NOXFER   02
+#define C_STATUS_PROGRESS 04
+
+static int img_dd_status(const char *arg,
+                         struct DdIo *in, struct DdIo *out,
+                         struct DdEss *dd)
+{
+    const struct DdSymbols dd_status[] = {
+        { "none", C_STATUS_NONE },
+        { "noxfer", C_STATUS_NOXFER },
+        { "progress", C_STATUS_PROGRESS },
+        { "default", C_STATUS_DEFAULT },
+        { NULL, 0 }
+    };
+
+    for (int j = 0; dd_status[j].name != NULL; j++) {
+        if (!strcmp(arg, dd_status[j].name)) {
+            dd->status = dd_status[j].value;
+            return 0;
+        }
+    }
+
+    error_report("invalid status level: '%s'", arg);
+    return 1;
+}
+
+static int img_dd(int argc, char **argv)
+{
+    int ret = 0;
+    char *arg = NULL;
+    char *tmp;
+    BlockDriver *drv = NULL, *proto_drv = NULL;
+    BlockBackend *blk1 = NULL, *blk2 = NULL;
+    QemuOpts *opts = NULL;
+    QemuOptsList *create_opts = NULL;
+    Error *local_err = NULL;
+    bool image_opts = false;
+    int c;
+    const char *out_fmt = "raw";
+    const char *fmt = NULL;
+    int64_t size = 0;
+    int64_t block_count = 0, incount = 0, outcount = 0;
+    struct DdEss dd = {
+        .flags = 0,
+        .status = C_STATUS_DEFAULT,
+        .conv = 0,
+        .count = 0,
+        .cbsz = 512
+    };
+    struct DdIo in = {
+        .bsz = 512, /* Block size is by default 512 bytes */
+        .offset = 0,
+        .filename = NULL,
+        .flags = 0,
+        .buf = NULL
+    };
+    struct DdIo out = {
+        .bsz = 512,
+        .offset = 0,
+        .filename = NULL,
+        .flags = 0,
+        .buf = NULL
+    };
+
+    const struct DdOpts options[] = {
+        { "bs", img_dd_bs, C_BS },
+        { "cbs", img_dd_cbs, C_CBS },
+        { "conv", img_dd_conv, C_CONV },
+        { "count", img_dd_count, C_COUNT },
+        { "ibs", img_dd_ibs, C_IBS },
+        { "if", img_dd_if, C_IF },
+        { "iflag", img_dd_iflag, C_IFLAG },
+        { "obs", img_dd_obs, C_OBS },
+        { "of", img_dd_of, C_OF },
+        { "oflag", img_dd_oflag, C_OFLAG },
+        { "seek", img_dd_seek, C_SEEK },
+        { "skip", img_dd_skip, C_SKIP },
+        { "status", img_dd_status, C_STATUS },
+        { NULL, NULL, 0 }
+    };
+    const struct option long_options[] = {
+        { "help", no_argument, 0, 'h'},
+        { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS},
+        { 0, 0, 0, 0 }
+    };
+
+    while ((c = getopt_long(argc, argv, "hf:O:", long_options, NULL))) {
+        if (c == EOF) {
+            break;
+        }
+        switch (c) {
+        case 'O':
+            out_fmt = optarg;
+            break;
+        case 'f':
+            fmt = optarg;
+            break;
+        case '?':
+            error_report("Try 'qemu-img --help' for more information.");
+            ret = -1;
+            goto out;
+            break;
+        case 'h':
+            help();
+            break;
+        case OPTION_IMAGE_OPTS:
+            image_opts = true;
+            break;
+        }
+    }
+
+    for (int i = optind; i < argc; i++) {
+        int j;
+        arg = g_strdup(argv[i]);
+
+        tmp = strchr(arg, '=');
+        if (tmp == NULL) {
+            error_report("unrecognized operand %s", arg);
+            ret = -1;
+            goto out;
+        }
+
+        *tmp++ = '\0';
+
+        for (j = 0; options[j].name != NULL; j++) {
+            if (!strcmp(arg, options[j].name)) {
+                break;
+            }
+        }
+        if (options[j].name == NULL) {
+            error_report("unrecognized operand %s", arg);
+            ret = -1;
+            goto out;
+        }
+
+        if (options[j].f(tmp, &in, &out, &dd) != 0) {
+            ret = -1;
+            goto out;
+        }
+        dd.flags |= options[j].flag;
+    }
+
+    if (dd.flags & C_IF && dd.flags & C_OF) {
+        blk1 = img_open(image_opts, in.filename, fmt, 0, false, true);
+
+        if (!blk1) {
+            ret = -1;
+            goto out;
+        }
+
+        drv = bdrv_find_format(out_fmt);
+        if (!drv) {
+            error_report("Unknown file format");
+            ret = -1;
+            goto out;
+        }
+        proto_drv = bdrv_find_protocol(out.filename, true, &local_err);
+
+        if (!proto_drv) {
+            error_report_err(local_err);
+            ret = -1;
+            goto out;
+        }
+        if (!drv->create_opts) {
+            error_report("Format driver '%s' does not support image creation",
+                         drv->format_name);
+            ret = -1;
+            goto out;
+        }
+        if (!proto_drv->create_opts) {
+            error_report("Protocol driver '%s' does not support image creation",
+                       proto_drv->format_name);
+            ret = -1;
+            goto out;
+        }
+        create_opts = qemu_opts_append(create_opts, drv->create_opts);
+        create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
+
+        opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
+
+        size =  blk_getlength(blk1);
+
+        if (dd.flags & C_COUNT && dd.count * in.bsz < size) {
+            size = dd.count * in.bsz;
+        }
+
+        qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size, &error_abort);
+
+        ret = bdrv_create(drv, out.filename, opts, &local_err);
+        if (ret < 0) {
+            error_reportf_err(local_err,
+                              "%s: error while copying and converting: ",
+                              out.filename);
+            ret = -1;
+            goto out;
+        }
+
+        blk2 = img_open(image_opts, out.filename, out_fmt, BDRV_O_RDWR,
+                        false, true);
+
+        if (!blk2) {
+            ret = -1;
+            goto out;
+        }
+
+        in.buf = g_new(uint8_t, in.bsz);
+
+        for (; incount < size; incount += ret, block_count++) {
+            int out_ret;
+
+            /* If the count option is specified. */
+            if (dd.flags & C_COUNT && block_count >= dd.count) {
+                break;
+            }
+
+            if (in.bsz + incount > size) {
+                ret = blk_pread(blk1, incount, in.buf, size - incount);
+            } else {
+                ret = blk_pread(blk1, incount, in.buf, in.bsz);
+            }
+            if (ret < 0) {
+                error_report("error while reading from input image file: %s",
+                             strerror(-ret));
+                ret = -1;
+                goto out;
+            }
+            out_ret = blk_pwrite(blk2, outcount, in.buf, ret, 0);
+
+            if (out_ret < 0) {
+                error_report("error while writing to output image file: %s",
+                             strerror(-out_ret));
+                ret = -1;
+                goto out;
+            }
+            outcount += out_ret;
+        }
+    } else {
+        error_report("Must specify both input and output files");
+        ret = -1;
+        goto out;
+    }
+out:
+    g_free(arg);
+    qemu_opts_del(opts);
+    qemu_opts_free(create_opts);
+    blk_unref(blk1);
+    blk_unref(blk2);
+    g_free(in.buf);
+    g_free(out.buf);
+
+    if (ret) {
+        return 1;
+    }
+    return 0;
+}
+
 
 static const img_cmd_t img_cmds[] = {
 #define DEF(option, callback, arg_string)        \
diff --git a/tests/qemu-iotests/158 b/tests/qemu-iotests/158
new file mode 100755
index 0000000..bb5e715
--- /dev/null
+++ b/tests/qemu-iotests/158
@@ -0,0 +1,53 @@ 
+#! /bin/bash
+#
+# qemu-img dd test
+
+owner=fullmanet@gmail.com
+
+seq="$(basename $0)"
+echo "QA output created by $seq"
+
+here="$PWD"
+status=1
+
+_cleanup()
+{
+    _cleanup_test_img
+}
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+. ./common.rc
+. ./common.filter
+. ./common.pattern
+
+_supported_fmt generic
+_supported_proto file
+_supported_os Linux
+
+echo
+echo "== Creating image =="
+
+size=1M
+_make_test_img $size
+_check_test_img
+
+$QEMU_IO -c "write -P 0xa 0 $size" "$TEST_IMG" | _filter_qemu_io
+
+echo
+echo "== Converting the image with dd =="
+
+$QEMU_IMG dd if="$TEST_IMG" of="$TEST_IMG.out" -O "$IMGFMT"
+$QEMU_IMG check "$TEST_IMG.out" -f "$IMGFMT" 2>&1  | _filter_testdir | \
+    sed -e '/allocated.*fragmented.*compressed clusters/d' \
+        -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
+        -e '/Image end offset: [0-9]\+/d'
+
+echo
+echo "== Compare the images with qemu-img compare =="
+
+$QEMU_IMG compare "$TEST_IMG" "$TEST_IMG.out"
+
+echo
+echo "*** done"
+rm -f "$seq.full"
+status=0
diff --git a/tests/qemu-iotests/158.out b/tests/qemu-iotests/158.out
new file mode 100644
index 0000000..58bfd85
--- /dev/null
+++ b/tests/qemu-iotests/158.out
@@ -0,0 +1,15 @@ 
+QA output created by 158
+
+== Creating image ==
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
+No errors were found on the image.
+wrote 1048576/1048576 bytes at offset 0
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== Converting the image with dd ==
+No errors were found on the image.
+
+== Compare the images with qemu-img compare ==
+Images are identical.
+
+*** done
diff --git a/tests/qemu-iotests/159 b/tests/qemu-iotests/159
new file mode 100755
index 0000000..4c9e4ec
--- /dev/null
+++ b/tests/qemu-iotests/159
@@ -0,0 +1,56 @@ 
+#! /bin/bash
+#
+# qemu-img dd test with different block sizes 
+
+owner=fullmanet@gmail.com
+
+seq="$(basename $0)"
+echo "QA output created by $seq"
+
+here="$PWD"
+status=1
+
+_cleanup()
+{
+    _cleanup_test_img
+}
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+. ./common.rc
+. ./common.filter
+. ./common.pattern
+
+_supported_fmt generic
+_supported_proto file
+_supported_os Linux
+
+TEST_SIZES="5 512 1024 1999 1K 64K 1M"
+
+for bs in $TEST_SIZES; do
+    echo
+    echo "== Creating image =="
+
+    size=1M
+    _make_test_img $size
+    _check_test_img
+    $QEMU_IO -c "write -P 0xa 0 $size" "$TEST_IMG" | _filter_qemu_io
+
+    echo
+    echo "== Converting the image with dd with a block size of $bs =="
+
+    $QEMU_IMG dd if="$TEST_IMG" of="$TEST_IMG.out" bs=$bs -O "$IMGFMT"
+    $QEMU_IMG check "$TEST_IMG.out" -f "$IMGFMT" 2>&1  | _filter_testdir | \
+	sed -e '/allocated.*fragmented.*compressed clusters/d' \
+            -e 's/qemu-img: This image format does not support checks/No errors were found on the image./' \
+            -e '/Image end offset: [0-9]\+/d'
+
+    echo
+    echo "== Compare the images with qemu-img compare =="
+
+    $QEMU_IMG compare "$TEST_IMG" "$TEST_IMG.out"
+done
+
+echo
+echo "*** done"
+rm -f "$seq.full"
+status=0
diff --git a/tests/qemu-iotests/159.out b/tests/qemu-iotests/159.out
new file mode 100644
index 0000000..b86b63a
--- /dev/null
+++ b/tests/qemu-iotests/159.out
@@ -0,0 +1,87 @@ 
+QA output created by 159
+
+== Creating image ==
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
+No errors were found on the image.
+wrote 1048576/1048576 bytes at offset 0
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== Converting the image with dd with a block size of 5 ==
+No errors were found on the image.
+
+== Compare the images with qemu-img compare ==
+Images are identical.
+
+== Creating image ==
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
+No errors were found on the image.
+wrote 1048576/1048576 bytes at offset 0
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== Converting the image with dd with a block size of 512 ==
+No errors were found on the image.
+
+== Compare the images with qemu-img compare ==
+Images are identical.
+
+== Creating image ==
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
+No errors were found on the image.
+wrote 1048576/1048576 bytes at offset 0
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== Converting the image with dd with a block size of 1024 ==
+No errors were found on the image.
+
+== Compare the images with qemu-img compare ==
+Images are identical.
+
+== Creating image ==
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
+No errors were found on the image.
+wrote 1048576/1048576 bytes at offset 0
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== Converting the image with dd with a block size of 1999 ==
+No errors were found on the image.
+
+== Compare the images with qemu-img compare ==
+Images are identical.
+
+== Creating image ==
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
+No errors were found on the image.
+wrote 1048576/1048576 bytes at offset 0
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== Converting the image with dd with a block size of 1K ==
+No errors were found on the image.
+
+== Compare the images with qemu-img compare ==
+Images are identical.
+
+== Creating image ==
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
+No errors were found on the image.
+wrote 1048576/1048576 bytes at offset 0
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== Converting the image with dd with a block size of 64K ==
+No errors were found on the image.
+
+== Compare the images with qemu-img compare ==
+Images are identical.
+
+== Creating image ==
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
+No errors were found on the image.
+wrote 1048576/1048576 bytes at offset 0
+1 MiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== Converting the image with dd with a block size of 1M ==
+No errors were found on the image.
+
+== Compare the images with qemu-img compare ==
+Images are identical.
+
+*** done
diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group
index 1c6fcb6..6c3d6fe 100644
--- a/tests/qemu-iotests/group
+++ b/tests/qemu-iotests/group
@@ -156,3 +156,5 @@ 
 154 rw auto backing quick
 155 rw auto
 156 rw auto quick
+158 rw auto quick
+159 rw auto quick