Skip to contentSkip to conversation

e2fsprogs: Add memory allocation and zero-out helpers

PATCH Applied Lukas Czerner 3 participants 11 messages May 18, 2011 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 e2fsprogs: Add memory allocation and zero-out helpers

Add functions ext2fs_get_memzero() which will malloc() the memory using ext2fs_get_mem(), but it will zero the allocated memory afterwards with memset().

Add function ext2fs_get_arrayzero() which will use calloc() for allocating and zero-out the array.

---
 lib/ext2fs/ext2fs.h |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)
lib/ext2fs/ext2fs.h +24 -0
index d3eb31d..4aff26d 100644
@@ -1404,6 +1404,17 @@ _INLINE_ errcode_t ext2fs_get_memalign(unsigned long size,
 	return 0;
 }
 
+_INLINE_ errcode_t ext2fs_get_memzero(unsigned long size, void *ptr)
+{
+	errcode_t retval;
+
+	retval = ext2fs_get_mem(size, ptr);
+	if (retval)
+		return retval;
+	memset(ptr, 0, size);
+	return 0;
+}
+
 _INLINE_ errcode_t ext2fs_get_array(unsigned long count, unsigned long size, void *ptr)
 {
 	if (count && (-1UL)/count<size)
@@ -1411,6 +1422,19 @@ _INLINE_ errcode_t ext2fs_get_array(unsigned long count, unsigned long size, voi
 	return ext2fs_get_mem(count*size, ptr);
 }
 
+_INLINE_ errcode_t ext2fs_get_arrayzero(unsigned long count, unsigned long size, void *ptr)
+{
+	void *pp;
+
+	if (count && (-1UL)/count<size)
+		return EXT2_ET_NO_MEMORY; //maybe define EXT2_ET_OVERFLOW ?
+	pp = calloc(count, size);
+	if (!pp)
+		return EXT2_ET_NO_MEMORY;
+	memcpy(ptr, &pp, sizeof (pp));
+	return 0;
+}
+
 /*
  * Free memory
  */
Signature · 1 line
1.7.4.4
#1 lore raw

PATCH v3 2/4 e2image: Add support for qcow2 format

This commit adds support for exporting filesystem into QCOW2 image format. Like sparse format this saves space, by writing only necessary (metadata blocks) into image. Unlike sparse image, QCOW2 image is NOT sparse, hence does not change its size by copying with not-sparse-aware tools.

New options '-Q' has been added to tell the e2image to use QCOW2 as an output image format. QCOW2 supports encryption and compression, however e2image so far does no support such features, however you can still scramble filenames with '-s' option.

---
 lib/ext2fs/Makefile.in |    4 +-
 lib/ext2fs/bitops.h    |    4 +
 lib/ext2fs/e2image.h   |   21 +-
 lib/ext2fs/qcow2.c     |  249 ++++++++++++++++
 lib/ext2fs/qcow2.h     |  113 +++++++
 misc/e2image.8.in      |   47 +++-
 misc/e2image.c         |  769 ++++++++++++++++++++++++++++++++++++++++++------
 7 files changed, 1104 insertions(+), 103 deletions(-)
 create mode 100644 lib/ext2fs/qcow2.c
 create mode 100644 lib/ext2fs/qcow2.h
lib/ext2fs/Makefile.in +3 -1
index 9c1c273..7299779 100644
@@ -70,6 +70,7 @@ OBJS= $(DEBUGFS_LIB_OBJS) $(RESIZE_LIB_OBJS) $(E2IMAGE_LIB_OBJS) \
 	openfs.o \
 	progress.o \
 	punch.o \
+	qcow2.o \
 	read_bb.o \
 	read_bb_file.o \
 	res_gdt.o \
@@ -138,6 +139,7 @@ SRCS= ext2_err.c \
 	$(srcdir)/openfs.c \
 	$(srcdir)/progress.c \
 	$(srcdir)/punch.c \
+	$(srcdir)/qcow2.c \
 	$(srcdir)/read_bb.c \
 	$(srcdir)/read_bb_file.c \
 	$(srcdir)/res_gdt.c \
@@ -158,7 +160,7 @@ SRCS= ext2_err.c \
 	$(srcdir)/write_bb_file.c
 
 HFILES= bitops.h ext2fs.h ext2_io.h ext2_fs.h ext2_ext_attr.h ext3_extents.h \
-	tdb.h
+	tdb.h qcow2.h
 HFILES_IN=  ext2_err.h ext2_types.h
 
 LIBRARY= libext2fs
lib/ext2fs/bitops.h +4 -0
index 3ded002..83a01e4 100644
@@ -31,6 +31,8 @@ extern __u64 ext2fs_swab64(__u64 val);
 #define ext2fs_le32_to_cpu(x) ext2fs_swab32((x))
 #define ext2fs_cpu_to_le16(x) ext2fs_swab16((x))
 #define ext2fs_le16_to_cpu(x) ext2fs_swab16((x))
+#define ext2fs_cpu_to_be64(x) ((__u64)(x))
+#define ext2fs_be64_to_cpu(x) ((__u64)(x))
 #define ext2fs_cpu_to_be32(x) ((__u32)(x))
 #define ext2fs_be32_to_cpu(x) ((__u32)(x))
 #define ext2fs_cpu_to_be16(x) ((__u16)(x))
@@ -42,6 +44,8 @@ extern __u64 ext2fs_swab64(__u64 val);
 #define ext2fs_le32_to_cpu(x) ((__u32)(x))
 #define ext2fs_cpu_to_le16(x) ((__u16)(x))
 #define ext2fs_le16_to_cpu(x) ((__u16)(x))
+#define ext2fs_cpu_to_be64(x) ext2fs_swab64((x))
+#define ext2fs_be64_to_cpu(x) ext2fs_swab64((x))
 #define ext2fs_cpu_to_be32(x) ext2fs_swab32((x))
 #define ext2fs_be32_to_cpu(x) ext2fs_swab32((x))
 #define ext2fs_cpu_to_be16(x) ext2fs_swab16((x))
lib/ext2fs/e2image.h +8 -13
index 4de2c8d..c918529 100644
@@ -12,6 +12,14 @@
  * %End-Header%
  */
 
+/* Image types */
+#define E2IMAGE_RAW	1
+#define E2IMAGE_QCOW2	2
+
+/* Image flags */
+#define E2IMAGE_INSTALL_FLAG	1
+#define E2IMAGE_SCRAMBLE_FLAG	2
+#define E2IMAGE_IS_QCOW2_FLAG	3
 
 struct ext2_image_hdr {
 	__u32	magic_number;	/* This must be EXT2_ET_MAGIC_E2IMAGE */
@@ -36,16 +44,3 @@ struct ext2_image_hdr {
 	__u32	offset_blockmap; /* Byte offset of the inode bitmaps */
 	__u32	offset_reserved[8];
 };
-
-
-
-
-
-
-
-
-
-
-
-
-
lib/ext2fs/qcow2.c new file +249 -0

249 lines not on this page · raw message

lib/ext2fs/qcow2.h new file +113 -0

113 lines not on this page · raw message

misc/e2image.8.in +43 -4

69 lines not on this page · raw message

misc/e2image.c +684 -85

992 lines not on this page · raw message

Signature · 1 line
1.7.4.4
Applied
#2 lore raw

Re: [PATCH 2/4 v3] e2image: Add support for qcow2 format

Quoted 6 lines · Lukas Czerner

This commit adds support for exporting filesystem into QCOW2 image format. Like sparse format this saves space, by writing only necessary (metadata blocks) into image. Unlike sparse image, QCOW2 image is NOT sparse, hence does not change its size by copying with not-sparse-aware tools.

Quoted 6 lines

New options '-Q' has been added to tell the e2image to use QCOW2 as an output image format. QCOW2 supports encryption and compression, however e2image so far does no support such features, however you can still scramble filenames with '-s' option.

Applied to the e2fsprogs branch, with a lot of whitespace fixes.

- Ted

#3 lore raw

Re: [PATCH 2/4 v3] e2image: Add support for qcow2 format

Quoted 12 lines · Ted Ts'o
Quoted 11 lines · Lukas Czerner

This commit adds support for exporting filesystem into QCOW2 image format. Like sparse format this saves space, by writing only necessary (metadata blocks) into image. Unlike sparse image, QCOW2 image is NOT sparse, hence does not change its size by copying with not-sparse-aware tools.

New options '-Q' has been added to tell the e2image to use QCOW2 as an output image format. QCOW2 supports encryption and compression, however e2image so far does no support such features, however you can still scramble filenames with '-s' option.

Quoted 5 lines
Quoted 1 line

Applied to the e2fsprogs branch, with a lot of whitespace fixes.

- Ted

Thanks Ted! And sorry for the whitespace problems, will do better next time!

-Lukas

#4 lore raw

Re: [PATCH 2/4 v3] e2image: Add support for qcow2 format

In reply to Lukas Czerner
Quoted 9 lines · Lukas Czerner
lib/ext2fs/qcow2.c new file +3 -0
new file mode 100644
index 0000000..7de72ed
 
+int qcow2_write_raw_image(int qcow2_fd, int raw_fd,
+			      struct ext2_qcow2_hdr *hdr)
+{

lib/ext2fs/qcow2.c line 162

Quoted 10 lines
lib/ext2fs/qcow2.c new file +10 -0
+	struct ext2_qcow2_image img;
+	errcode_t ret = 0;
+	unsigned int l1_index, l2_index;
+	ext2_off64_t offset;
+	blk64_t *l1_table, *l2_table;
+	void *copy_buf = NULL;
+	size_t size;
+
+	if (hdr->crypt_method)
+		return -QCOW_ENCRYPTED;

Good.

lib/ext2fs/qcow2.c line 203

Quoted 9 lines
lib/ext2fs/qcow2.c new file +9 -0
+	for (l1_index = 0; l1_index < img.l1_size; l1_index++) {
+		ext2_off64_t off_out;
+
+		offset = ext2fs_be64_to_cpu(l1_table[l1_index]) &
+			 ~QCOW_OFLAG_COPIED;
+
+		if ((offset > img.image_size) ||
+		    (offset <= 0))
+			continue;

Should this be considered an error, instead of just silently continuing? At a minimum it should print an error to the user and return a non-zero value on exit, but I'm not sure whether it should abort because the user may care more about extracting _some_ data from the image instead of little or none.

Cheers, Andreas

#5 lore raw

PATCH v3 3/4 e2image: Support for conversion QCOW2 image into raw

In reply to Lukas Czerner

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.

---
 misc/e2image.8.in |    4 ++++
 misc/e2image.c    |   48 +++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 51 insertions(+), 1 deletions(-)
misc/e2image.8.in +4 -0
index 4a28580..6f31dd1 100644
@@ -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
misc/e2image.c +47 -1

87 lines not on this page · raw message

Signature · 1 line
1.7.4.4
#6 lore raw

PATCH v4 3/4 e2image: Support for conversion QCOW2 image into raw

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.

---
 misc/e2image.8.in |    4 ++++
 misc/e2image.c    |   49 ++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 52 insertions(+), 1 deletions(-)
misc/e2image.8.in +4 -0
index 4a28580..6f31dd1 100644
@@ -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
misc/e2image.c +48 -1

88 lines not on this page · raw message

Signature · 1 line
1.7.4.4
Applied
#7 lore raw

Re: [PATCH 3/4 v4] e2image: Support for conversion QCOW2 image into raw

Quoted 13 lines · Lukas Czerner

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.

Quoted 5 lines

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.

Applied to the e2fsprogs next branch.

- Ted

#8 lore raw

Re: [PATCH 3/4 v3] e2image: Support for conversion QCOW2 image into raw

In reply to Lukas Czerner
Quoted 104 lines · Lukas Czerner

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.

---
 misc/e2image.8.in |    4 ++++
 misc/e2image.c    |   48 +++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 51 insertions(+), 1 deletions(-)
misc/e2image.8.in +4 -0
index 4a28580..6f31dd1 100644
@@ -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
misc/e2image.c +27 -0
index dbf4421..0a1b1f5 100644
@@ -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) {

misc/e2image.c int main (int argc, char ** argv) · line 1335

Quoted 10 lines
misc/e2image.c +10 -0
+		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);

I have forgot braces. Fixed in v4.

Quoted 20 lines
misc/e2image.c +10 -1
+		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;
 }
#9 lore raw
In reply to Lukas Czerner
Quoted 6 lines · Lukas Czerner

Add functions ext2fs_get_memzero() which will malloc() the memory using ext2fs_get_mem(), but it will zero the allocated memory afterwards with memset().

Add function ext2fs_get_arrayzero() which will use calloc() for allocating and zero-out the array.

Sorry this is not right I forgot to commit my changes. Version 2 is out there.

Thanks!
-Lukas

Quoted 49 lines
---
 lib/ext2fs/ext2fs.h |   24 ++++++++++++++++++++++++
 1 files changed, 24 insertions(+), 0 deletions(-)
lib/ext2fs/ext2fs.h +24 -0
index d3eb31d..4aff26d 100644
@@ -1404,6 +1404,17 @@ _INLINE_ errcode_t ext2fs_get_memalign(unsigned long size,
 	return 0;
 }
 
+_INLINE_ errcode_t ext2fs_get_memzero(unsigned long size, void *ptr)
+{
+	errcode_t retval;
+
+	retval = ext2fs_get_mem(size, ptr);
+	if (retval)
+		return retval;
+	memset(ptr, 0, size);
+	return 0;
+}
+
 _INLINE_ errcode_t ext2fs_get_array(unsigned long count, unsigned long size, void *ptr)
 {
 	if (count && (-1UL)/count<size)
@@ -1411,6 +1422,19 @@ _INLINE_ errcode_t ext2fs_get_array(unsigned long count, unsigned long size, voi
 	return ext2fs_get_mem(count*size, ptr);
 }
 
+_INLINE_ errcode_t ext2fs_get_arrayzero(unsigned long count, unsigned long size, void *ptr)
+{
+	void *pp;
+
+	if (count && (-1UL)/count<size)
+		return EXT2_ET_NO_MEMORY; //maybe define EXT2_ET_OVERFLOW ?
+	pp = calloc(count, size);
+	if (!pp)
+		return EXT2_ET_NO_MEMORY;
+	memcpy(ptr, &pp, sizeof (pp));
+	return 0;
+}
+
 /*
  * Free memory
  */

A message that is not in the archive (sent off-list or not yet received): [email protected]

Applied
#11 lore raw

Re: [PATCH 4/4 v3] tests: New i_e2image test to validate image creation/conversion

Quoted 5 lines · Lukas Czerner

This commit adds new regression test called i_e2image which should validate expected behaviour of e2image raw and qcow2 image creation and qcow2 -> raw image conversion. You can run it with "make check" as the rest of regression tests.

Quoted 8 lines

Testing is performed on three filesystem images with different block sizes (1024, 2048, 4096). It creates raw and qcow2 images from the original and then convert qcow2 image back to raw image. The results are checksummed and compared with pre-prepared results. All md5sums should stay the same and raw image created from original and qcow2 image should be the same as well (just for the record).

Applied into the e2fsprogs next branch.

I had to fix up this commit so that it works when you are using a $VPATH build directory. Note that, temp files for tests should go in the current directory, and that files in the source file have to be referenced via $SRCDIR.

- Ted