From patchwork Fri Apr 29 01:42:26 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brad Campbell X-Patchwork-Id: 93372 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id F4031B6EDF for ; Fri, 29 Apr 2011 11:42:44 +1000 (EST) Received: from localhost ([::1]:44867 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QFcjB-0005x3-3o for incoming@patchwork.ozlabs.org; Thu, 28 Apr 2011 21:42:41 -0400 Received: from eggs.gnu.org ([140.186.70.92]:37183) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QFcj3-0005wm-8i for qemu-devel@nongnu.org; Thu, 28 Apr 2011 21:42:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QFcj2-00078S-6G for qemu-devel@nongnu.org; Thu, 28 Apr 2011 21:42:33 -0400 Received: from [93.93.131.224] (port=39402 helo=fnarfbargle.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QFcj1-00078N-UM for qemu-devel@nongnu.org; Thu, 28 Apr 2011 21:42:32 -0400 Received: from srv.homea ([10.8.0.1] helo=localhost ident=heh22544) by fnarfbargle.com with esmtp (Exim 4.69) (envelope-from ) id 1QFciz-0000TQ-Kq for qemu-devel@nongnu.org; Fri, 29 Apr 2011 02:42:30 +0100 Received: from bkmac.home ([192.168.2.87]) by localhost with esmtp (Exim 4.72) (envelope-from ) id 1QFciw-0005rX-Sd for qemu-devel@nongnu.org; Fri, 29 Apr 2011 09:42:27 +0800 Message-ID: <4DBA1782.7050600@fnarfbargle.com> Date: Fri, 29 Apr 2011 09:42:26 +0800 From: Brad Campbell User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.14) Gecko/20110223 Lightning/1.0b2 Thunderbird/3.1.8 MIME-Version: 1.0 To: qemu-devel@nongnu.org X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 93.93.131.224 Subject: [Qemu-devel] [PATCH] Make qemu-img convert properly consider backing file contents when used with -o backing_file X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org G'day all, This patch makes qemu-img properly consider the contents of the output backing file when performing a convert operation. All things considered it would also perform similar to rebase, where you could specify a completely different backing file and it would just de-dup. I've poked this in as an attachment as apparently my last attempt at an in-line patch munged the formatting. Comments, pokes or flames welcome. Regards, Brad diff --git a/qemu-img.c b/qemu-img.c index d9c2c12..02455af 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -571,11 +571,12 @@ static int img_convert(int argc, char **argv) int progress = 0; const char *fmt, *out_fmt, *out_baseimg, *out_filename; BlockDriver *drv, *proto_drv; - BlockDriverState **bs = NULL, *out_bs = NULL; + BlockDriverState **bs = NULL, *out_bs = NULL, *out_bf = NULL; int64_t total_sectors, nb_sectors, sector_num, bs_offset; uint64_t bs_sectors; uint8_t * buf = NULL; const uint8_t *buf1; + uint8_t * buf3 = NULL; BlockDriverInfo bdi; QEMUOptionParameter *param = NULL, *create_options = NULL; QEMUOptionParameter *out_baseimg_param; @@ -719,6 +720,16 @@ static int img_convert(int argc, char **argv) out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); if (out_baseimg_param) { out_baseimg = out_baseimg_param->value.s; + + /* out_baseimg_parm != NULL even if there is no base img specified! */ + if (out_baseimg) { + out_bf = bdrv_new_open(out_baseimg, NULL, BDRV_O_FLAGS); + if (!out_bf) { + error_report("Could not open backing file '%s'", out_baseimg); + ret = -1; + goto out; + } + } } /* Check if compression is supported */ @@ -767,6 +778,9 @@ static int img_convert(int argc, char **argv) bs_offset = 0; bdrv_get_geometry(bs[0], &bs_sectors); buf = qemu_malloc(IO_BUF_SIZE); + if (out_baseimg) { + buf3 = qemu_malloc(IO_BUF_SIZE); + } if (compress) { ret = bdrv_get_info(out_bs, &bdi); @@ -889,8 +903,15 @@ static int img_convert(int argc, char **argv) are present in both the output's and input's base images (no need to copy them). */ if (out_baseimg) { - if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset, - n, &n1)) { + if (bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n) < 0) { + error_report("error while reading input file"); + goto out; + } + if (bdrv_read(out_bf, sector_num - bs_offset, buf3, n) < 0) { + error_report("error while reading backing file"); + goto out; + } + if (!compare_sectors(buf, buf3, n, &n1)) { sector_num += n1; continue; } @@ -939,9 +960,13 @@ out: free_option_parameters(create_options); free_option_parameters(param); qemu_free(buf); + qemu_free(buf3); if (out_bs) { bdrv_delete(out_bs); } + if (out_bf) { + bdrv_delete(out_bf); + } if (bs) { for (bs_i = 0; bs_i < bs_n; bs_i++) { if (bs[bs_i]) {