From patchwork Wed May 18 11:36:54 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukas Czerner X-Patchwork-Id: 96155 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id BAFF5B6F65 for ; Wed, 18 May 2011 21:37:23 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932648Ab1ERLhU (ORCPT ); Wed, 18 May 2011 07:37:20 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55188 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932811Ab1ERLhS (ORCPT ); Wed, 18 May 2011 07:37:18 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p4IBbHIW013461 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 18 May 2011 07:37:17 -0400 Received: from dhcp-27-109.brq.redhat.com (dhcp-1-233.brq.redhat.com [10.34.1.233]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p4IBbBaL026513; Wed, 18 May 2011 07:37:15 -0400 From: Lukas Czerner To: linux-ext4@vger.kernel.org Cc: tytso@mit.edu, sandeen@redhat.com, adilger@dilger.ca, Lukas Czerner Subject: [PATCH 3/4 v3] e2image: Support for conversion QCOW2 image into raw Date: Wed, 18 May 2011 13:36:54 +0200 Message-Id: <1305718615-5991-3-git-send-email-lczerner@redhat.com> In-Reply-To: <1305718615-5991-1-git-send-email-lczerner@redhat.com> References: <1305718615-5991-1-git-send-email-lczerner@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org This commit adds support for converting QCOW2 image created previously with e2image into raw image. The QCOW2 image is detected automatically, so there is not new option. Just use following command: e2image -r image.qcow image.raw No that this tool is aimed to quickly convert qcow2 image created with e2image into raw image. In order to improve speed we are doing some assumption I believe might not be true for regular qcow2 images. So it was not tested with regular QCOW2 images and it might not work with them. The intention of this tool is only convert images previously created by e2image. Note that there is nothing special with QCOW2 images created by e2images and it can be used with tools like qemu-img, or qemu-nbd without any problems. Signed-off-by: Lukas Czerner --- misc/e2image.8.in | 4 ++++ misc/e2image.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletions(-) diff --git a/misc/e2image.8.in b/misc/e2image.8.in index 4a28580..6f31dd1 100644 --- a/misc/e2image.8.in +++ b/misc/e2image.8.in @@ -131,6 +131,10 @@ the option will prevent analysis of problems related to hash-tree indexed directories. .PP +Note that this will work even if you substitute "/dev/hda1" for another raw +disk image, or QCOW2 image previously created by +.BR e2image . +.PP .SH QCOW2 IMAGE FILES The .B \-Q diff --git a/misc/e2image.c b/misc/e2image.c index dbf4421..0a1b1f5 100644 --- a/misc/e2image.c +++ b/misc/e2image.c @@ -1213,16 +1213,33 @@ static void install_image(char *device, char *image_fn, int type) exit (0); } +static struct ext2_qcow2_hdr *check_qcow2_image(int *fd, char *name) +{ + +#ifdef HAVE_OPEN64 + *fd = open64(name, O_RDONLY, 0600); +#else + *fd = open(name, O_RDONLY, 0600); +#endif + if (*fd < 0) + return NULL; + + return qcow2_read_header(*fd); +} + int main (int argc, char ** argv) { int c; errcode_t retval; ext2_filsys fs; char *image_fn; + struct ext2_qcow2_hdr *header = NULL; int open_flag = EXT2_FLAG_64BITS; int img_type = 0; int flags = 0; + int qcow2_fd = 0; int fd = 0; + int ret = 0; #ifdef ENABLE_NLS setlocale(LC_MESSAGES, ""); @@ -1266,6 +1283,14 @@ int main (int argc, char ** argv) exit (0); } + if (img_type & E2IMAGE_RAW) { + header = check_qcow2_image(&qcow2_fd, device_name); + if (header) { + flags |= E2IMAGE_IS_QCOW2_FLAG; + goto skip_device; + } + } + retval = ext2fs_open (device_name, open_flag, 0, 0, unix_io_manager, &fs); if (retval) { @@ -1275,6 +1300,7 @@ int main (int argc, char ** argv) exit(1); } +skip_device: if (strcmp(image_fn, "-") == 0) fd = 1; else { @@ -1296,12 +1322,32 @@ int main (int argc, char ** argv) exit(1); } + if (flags & E2IMAGE_IS_QCOW2_FLAG) { + ret = qcow2_write_raw_image(qcow2_fd, fd, header); + if (ret) + if (ret == -QCOW_COMPRESSED) + fprintf(stderr, "Image (%s) is compressed\n"); + if (ret == -QCOW_ENCRYPTED) + fprintf(stderr, "Image (%s) is encrypted\n"); + com_err(program_name, ret, + _("while trying to convert qcow2 image" + " (%s) into raw image (%s)"), + device_name, image_fn); + goto out; + } + + if (img_type) write_raw_image_file(fs, fd, img_type, flags); else write_image_file(fs, fd); ext2fs_close (fs); +out: + if (header) + free(header); + if (qcow2_fd) + close(qcow2_fd); remove_error_table(&et_ext2_error_table); - exit (0); + return ret; }