Add qcow2 support
v2 cover Add qcow2 support
Hello,
it has been a while since I have posted this last time. Noone seems to care very much, but I think this is really useful feature for e2image so I give it a try once more.
These patches adds a qcow2 support to e2image. We all know how painful! is using e2image on huge filesystems, especially when you need to transfer the images. Qcow2 images are different, due to its format it is not sparse yet, it is space efficient because it packs all used file system metadata close together, hence avoiding sparseness. So it is really easy to generate, trasfer and convert qcow2 images.
Now in order to check the image, or use debugfs you have to convert it into the qcow2 image. Unlike targzipping it is *A LOT* faster, however you still have to have the space for the raw image, but I do not see that as a big problem and hey it is a HUGE improvement from what we have today! However it would be possible to create new iomanager which does understand qcow2 format directly, but I am not going to do that, since there is no library and it would mean copy/change and maintain part of qemu code and I do not think this is worth it. But anyone interested in this, feel free to look at it.
Also, remember that if you really do not want to convert the image because of file size limit, or whatever, you can always use qemu-nbd to attach qcow2 image into nbd block device and use that as regular device.
[ Note that the last patch (#3) might not make it into the list, because it contains new tests and file system images and it is 1.2MB big. If you would like to see it please let me know and I will send it to you directly ]
Now here are some numbers:
--------------------------
I have 6TB raid composed of four drives and I flooded it with lots and lots of files (copying /usr/share over and over again) and even created some big files (1M, 20M, 1G, 10G) so the number of used inodes on the filesystem is 10928139. I am using e2fsck form top of the master branch.
Before each step I run:
sync; echo 3 > /proc/sys/vm/drop_caches
exporting raw image:
time .//misc/e2image -r /dev/mapper/vg_raid-lv_stripe image.raw
real 12m3.798s
user 2m53.116s
sys 3m38.430s6,0G image.raw
exporting qcow2 image
time .//misc/e2image -Q /dev/mapper/vg_raid-lv_stripe image.qcow2 e2image 1.41.14 (22-Dec-2010)
real 11m55.574s
user 2m50.521s
sys 3m41.515s6,1G image.qcow2
So we can see that the running time is essentially the same, so there is no crazy overhead in creating qcow2 image. Note that qcow2 image is slightly bigger because of all the qcow2 related metadata and it's size really depends on the size of the device. Also I tried to see how long does it take to export bzipped2 raw image, but it is running almost one day now, so it is not even comparable.
e2fsck on the device:
time .//e2fsck/e2fsck -fn /dev/mapper/vg_raid-lv_stripe
real 3m9.400s
user 0m47.558s
sys 0m15.098se2fsck on the raw image:
time .//e2fsck/e2fsck -fn image.raw
real 2m36.767s
user 0m47.613s
sys 0m8.403sWe can see that e2fsck on the raw image is a bit faster, but that is obvious since the drive does not have to seek so much (right?).
Now converting qcow2 image into raw image:
time .//misc/e2image -r image.qcow2 image.qcow2.raw
real 1m23.486s
user 0m0.704s
sys 0m22.574sIt is hard to say if it is "quite fast" or not. But I would say it is not terribly slow either. Just out of curiosity, I have tried to convert raw->qcow2 with qemu-img convert tool:
time qemu-img convert -O raw image.qcow2 image.qemu.raw ..it is running almost an hour now, so it is not comparable as well :)
Please review!
Thanks!
-Lukas
Signature · 18 lines
[PATCH 1/3 v2] e2image: Add support for qcow2 format [PATCH 2/3 v2] e2image: Support for conversion QCOW2 image into raw [PATCH 3/3 v2] tests: New i_e2image test to validate image lib/ext2fs/Makefile.in | 4 +- lib/ext2fs/bitops.h | 4 + lib/ext2fs/e2image.h | 21 +- lib/ext2fs/qcow2.c | 228 +++++++++++ lib/ext2fs/qcow2.h | 94 +++++ misc/e2image.8.in | 51 +++- misc/e2image.c | 766 +++++++++++++++++++++++++++++++++--- tests/i_e2image/i_e2image.md5 | 15 + tests/i_e2image/image1024.orig.bz2 | Bin 0 -> 322312 bytes tests/i_e2image/image2048.orig.bz2 | Bin 0 -> 321388 bytes tests/i_e2image/image4096.orig.bz2 | Bin 0 -> 312342 bytes tests/i_e2image/script | 57 +++ tests/test_config | 1 + 13 files changed, 1168 insertions(+), 73 deletions(-)
PATCH v2 1/3 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 | 227 +++++++++++++++ lib/ext2fs/qcow2.h | 94 +++++++ misc/e2image.8.in | 47 +++- misc/e2image.c | 723 ++++++++++++++++++++++++++++++++++++++++++++---- 7 files changed, 1048 insertions(+), 72 deletions(-) create mode 100644 lib/ext2fs/qcow2.c create mode 100644 lib/ext2fs/qcow2.h
lib/ext2fs/Makefile.in +3 -1
@@ -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
@@ -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
@@ -12,6 +12,14 @@* %End-Header% */ +/* Image types */ +#define IMAGE_RAW 1 +#define IMAGE_QCOW2 2 + +/* Image flags */ +#define INSTALL_FLAG 1 +#define SCRAMBLE_FLAG 2 +#define IS_QCOW2_FLAG 3struct 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 +227 -0
227 lines not on this page · raw message
lib/ext2fs/qcow2.h new file +94 -0
94 lines not on this page · raw message
misc/e2image.8.in +43 -4
69 lines not on this page · raw message
misc/e2image.c +669 -54
916 lines not on this page · raw message
Signature · 1 line
1.7.4.4
PATCH v2 2/3 e2image: Support for conversion QCOW2 image into raw
In reply to Lukas CzernerThis 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.
--- lib/ext2fs/qcow2.c | 3 ++- misc/e2image.8.in | 4 ++++ misc/e2image.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 2 deletions(-)
lib/ext2fs/qcow2.c +2 -1
@@ -209,9 +209,10 @@ int qcow2_write_raw_image(int qcow2_fd, int raw_fd, }/* Resize the output image to the filesystem size */ -if (lseek (raw_fd, img.image_size,SEEK_SET ) <0 ) +if (lseek (raw_fd, img.image_size -1 ,SEEK_SET ) <0 )return errno; +memset (copy_buf,0 ,1 ); size =write (raw_fd, copy_buf,1 );if (size !=1 )return errno;
misc/e2image.8.in +4 -0
@@ -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 +42 -1
82 lines not on this page · raw message
Signature · 1 line
1.7.4.4
Quoted 5 lines · Lukas Czerner
Hello,
it has been a while since I have posted this last time. Noone seems to care very much, but I think this is really useful feature for e2image so I give it a try once more.
I care! I'm currently bunzip2'ing a multi-terabyte image.
Zipped, it is 14M. After about 6 hours, it has reached an offset of 3T, and it has written ...
... 131 Megabytes.
In 6 hours.
If ext4 dreams of actual large filesystem support, being able to handle metadata dumps at this scale will be critical.
So I should review this ;)
Thanks,
-Eric
Quoted 102 lines
These patches adds a qcow2 support to e2image. We all know how painful! is using e2image on huge filesystems, especially when you need to transfer the images. Qcow2 images are different, due to its format it is not sparse yet, it is space efficient because it packs all used file system metadata close together, hence avoiding sparseness. So it is really easy to generate, trasfer and convert qcow2 images.
Now in order to check the image, or use debugfs you have to convert it into the qcow2 image. Unlike targzipping it is *A LOT* faster, however you still have to have the space for the raw image, but I do not see that as a big problem and hey it is a HUGE improvement from what we have today! However it would be possible to create new iomanager which does understand qcow2 format directly, but I am not going to do that, since there is no library and it would mean copy/change and maintain part of qemu code and I do not think this is worth it. But anyone interested in this, feel free to look at it.
Also, remember that if you really do not want to convert the image because of file size limit, or whatever, you can always use qemu-nbd to attach qcow2 image into nbd block device and use that as regular device.
[ Note that the last patch (#3) might not make it into the list, because it contains new tests and file system images and it is 1.2MB big. If you would like to see it please let me know and I will send it to you directly ]
Now here are some numbers:
--------------------------I have 6TB raid composed of four drives and I flooded it with lots and lots of files (copying
/usr/shareover and over again) and even created some big files (1M, 20M, 1G, 10G) so the number of used inodes on the filesystem is 10928139. I am using e2fsck form top of the master branch.Before each step I run:
sync; echo 3 >/proc/sys/vm/drop_cachesexporting raw image:
time .//misc/e2image -r/dev/mapper/vg_raid-lv_stripeimage.rawreal 12m3.798s user 2m53.116s sys 3m38.430s6,0G image.raw
exporting qcow2 image
time .//misc/e2image -Q/dev/mapper/vg_raid-lv_stripeimage.qcow2 e2image 1.41.14 (22-Dec-2010)real 11m55.574s user 2m50.521s sys 3m41.515s6,1G image.qcow2So we can see that the running time is essentially the same, so there is no crazy overhead in creating qcow2 image. Note that qcow2 image is slightly bigger because of all the qcow2 related metadata and it's size really depends on the size of the device. Also I tried to see how long does it take to export bzipped2 raw image, but it is running almost one day now, so it is not even comparable.
e2fsck on the device:
time .//e2fsck/e2fsck -fn/dev/mapper/vg_raid-lv_stripereal 3m9.400s user 0m47.558s sys 0m15.098se2fsck on the raw image:
time .//e2fsck/e2fsck -fn image.rawreal 2m36.767s user 0m47.613s sys 0m8.403sWe can see that e2fsck on the raw image is a bit faster, but that is obvious since the drive does not have to seek so much (right?).
Now converting qcow2 image into raw image:
time .//misc/e2image -r image.qcow2 image.qcow2.rawreal 1m23.486s user 0m0.704s sys 0m22.574sIt is hard to say if it is "quite fast" or not. But I would say it is not terribly slow either. Just out of curiosity, I have tried to convert
raw->qcow2with qemu-img convert tool:time qemu-img convert -O raw image.qcow2 image.qemu.raw ..it is running almost an hour now, so it is not comparable as well :)
Please review!
Thanks!
-Lukas
Re: [PATCH 1/3 v2] e2image: Add support for qcow2 format
In reply to Lukas Czernerlib/ext2fs/e2image.h line 19
Quoted 13 lines · Lukas Czerner
lib/ext2fs/e2image.h +8
-0@@ -12,6 +12,14 @@ +/* Image types */ +#define IMAGE_RAW 1 +#define IMAGE_QCOW2 2 + +/* Image flags */ +#define INSTALL_FLAG 1 +#define SCRAMBLE_FLAG 2 +#define IS_QCOW2_FLAG 3
These should be given better names, with some prefix like "E2IMAGE_".
lib/ext2fs/qcow2.c line 2
Quoted 8 lines
lib/ext2fs/qcow2.c new file +2
-0@@ -0,0 +1,227 @@ +/* +* qcow2.c --- Set of qcow2 related functions
It would be nice to have a better comment here. I can see from the filename that this is related to "qcow2". How about something like:
* Functions to generate qcow2 formatted disk images. This format is used
* originally by QEMU for virtual machines, and stores the filesystem data
* on disk in a packed format to avoid creating sparse image files that need
* lots of seeking to read and write.
*
* The qcow2 format supports zlib compression, but that is not yet implemented. *
* It is possible to directly mount a qcow2 image using qemu-nbd:
*
* [root]# modprobe nbd max_part=63
* [root]# qemu-nbd -c /dev/nbd0 image.img
* [root]# mount /dev/nbd0p1 /mnt/qemu
*
* Format details at http://people.gnome.org/~markmc/qcow-image-format.html
lib/ext2fs/qcow2.c line 42
Quoted 9 lines
lib/ext2fs/qcow2.c new file +9
-0+/* Functions for converting qcow2 image into raw image */ + +struct ext2_qcow2_hdr *qcow2_read_header (int fd,char *fname) +{ +void *buffer =NULL ; +struct ext2_qcow2_hdr *hdr =NULL ; +size_t size; + + buffer =malloc (sizeof (struct ext2_qcow2_hdr ));
Because this will be linked into libext2fs, all of these functions should be converted to use the libext2 helper functions for portability reasons.
s/malloc(/,ext2fs_get_mem(/g
though in some places ext2fs_get_array() is preferable, since it does overflow checking.
lib/ext2fs/qcow2.c line 48
Quoted 6 lines
lib/ext2fs/qcow2.c new file +6
-0+if (!buffer) +return NULL ; +memset (buffer,0 ,sizeof (struct ext2_qcow2_hdr )); + +if (lseek (fd,0 ,SEEK_SET ) <0 ) +return NULL ;
This should instead use the built-in llseek:
s/lseek(/ext2fs_llseek(/g
lib/ext2fs/qcow2.c line 50
Quoted 1 line
lib/ext2fs/qcow2.c new file +1
-0+ size =read (fd, buffer,sizeof (struct ext2_qcow2_hdr ));
This should use io_channel_read_blk64()
lib/ext2fs/qcow2.c line 52
Quoted 2 lines
lib/ext2fs/qcow2.c new file +2
-0+if (size !=sizeof (struct ext2_qcow2_hdr )) { +free (buffer);
s/free(/ext2fs_free_mem(/g
Quoted 33 lines
lib/ext2fs/qcow2.c new file +31
-0+return NULL ; + } + + hdr = (struct ext2_qcow2_hdr *)(buffer); + +if ((ext2fs_be32_to_cpu (hdr->magic) !=QCOW_MAGIC ) || + (ext2fs_be32_to_cpu (hdr->version) !=2 )) { +free (hdr); +return NULL ; + } + +return hdr; +} + +static int qcow2_read_l1_table (struct ext2_qcow2_image *img) +{ +int fd = img->fd; +size_t size, l1_size = img->l1_size *sizeof (__u64); +__u64 *table; + + table =calloc (1 , l1_size); +if (!table) +return errno; + +if (lseek (fd, img->l1_offset,SEEK_SET ) <0 ) +return errno; + + size =read (fd, table, l1_size); +if (size != l1_size) { +free (table); +return errno;
lib/ext2fs/qcow2.c line 93
Quoted 10 lines
lib/ext2fs/qcow2.c new file +10
-0+ } + + img->l1_table = table; + +return 0 ; +} + +static int qcow2_read_l2_table (struct ext2_qcow2_image *img,off_t offset, +__u64 **l2_table) +{
Instead of "off_t" (which is not 64-bit clean) this should use "ext2_off64_t" for offset, and blk64_t for l2_table. See also my other email about using more consistent types within libext2fs itself.
s/off_t/ext2_off64_t/g
Quoted 141 lines
lib/ext2fs/qcow2.c new file +134
-0+int fd = img->fd; +size_t size; + +assert (*l2_table); + +if (lseek (fd, offset,SEEK_SET ) <0 ) +return errno; + + size =read (fd, *l2_table, img->cluster_size); +if (size != img->cluster_size) +return errno; + +return 0 ; +} + +static int qcow2_copy_data (int fdin,int fdout,off_t off_in,off_t off_out, +void *buf,size_t count) +{ +size_t size; + +assert (buf); + +if (lseek (fdout, off_out,SEEK_SET ) <0 ) +return errno; + +if (lseek (fdin, off_in,SEEK_SET ) <0 ) +return errno; + + size =read (fdin, buf, count); +if (size != count) +return errno; + + size =write (fdout, buf, count); +if (size != count) +return errno; + +return 0 ; +} + + +int qcow2_write_raw_image (int qcow2_fd,int raw_fd, +struct ext2_qcow2_hdr *hdr) +{ +struct ext2_qcow2_image img; +int ret =0 ; +unsigned int l1_index, l2_index; +off_t offset; +__u64 *l1_table, *l2_table; +void *copy_buf =NULL ; +size_t size; + + img.fd = qcow2_fd; + img.hdr = hdr; + img.l2_cache =NULL ; + img.l1_table =NULL ; + img.cluster_bits =ext2fs_be32_to_cpu (hdr->cluster_bits); + img.cluster_size =1 << img.cluster_bits; + img.l1_size =ext2fs_be32_to_cpu (hdr->l1_size); + img.l1_offset =ext2fs_be64_to_cpu (hdr->l1_table_offset); + img.l2_size =1 << (img.cluster_bits -3 ); + img.image_size =ext2fs_be64_to_cpu (hdr->size); + + l2_table =calloc (1 , img.cluster_size); +if (!l2_table) { + ret = errno; + goto out; + } + + copy_buf =calloc (1 ,1 << img.cluster_bits); +if (!copy_buf) { + ret = errno; + goto out; + } + +if (lseek (raw_fd,0 ,SEEK_SET ) <0 ) { + ret = errno; + goto out; + } + + ret =qcow2_read_l1_table (&img); +if (ret) + goto out; + + l1_table = img.l1_table; +/* Walk through l1 table */ +for (l1_index =0 ; l1_index < img.l1_size; l1_index++) { +off_t off_out; + + offset =ext2fs_be64_to_cpu (l1_table[l1_index]) & + ~QCOW_OFLAG_COPIED ; + +if ((offset > img.image_size) || + (offset <=0 )) +continue ; + + ret =qcow2_read_l2_table (&img, offset, &l2_table); +if (ret) +break ; + +/* Walk through l2 table and copy data blocks into raw image */ +for (l2_index =0 ; l2_index < img.l2_size; l2_index++) { + offset =ext2fs_be64_to_cpu (l2_table[l2_index]) & + ~QCOW_OFLAG_COPIED ; + +if (offset ==0 ) +continue ; + + off_out = (l1_index * img.l2_size) + + l2_index; + off_out <<= img.cluster_bits; + ret =qcow2_copy_data (qcow2_fd, raw_fd, offset, + off_out, copy_buf, img.cluster_size); +if (ret) + goto out; + } + } + +/* Resize the output image to the filesystem size */ +if (lseek (raw_fd, img.image_size,SEEK_SET ) <0 ) +return errno; + + size =write (raw_fd, copy_buf,1 ); +if (size !=1 ) +return errno; + +out: +if (copy_buf) +free (copy_buf); +if (img.l1_table) +free (img.l1_table); +if (l2_table) +free (l2_table); +return ret; +}lib/ext2fs/qcow2.h new file +0
-0
lib/ext2fs/qcow2.h line 4
Quoted 4 lines
lib/ext2fs/qcow2.h new file +4
-0@@ -0,0 +1,94 @@ +/* +* e2qcow.h --- +* +* Copyright
This copyright line should be completed.
Quoted 6 lines
misc/e2image.c +0
-0static void
generic_write(int fd, char *buf, int blocksize, blk64_t block)
This function should probably just take "void *buf" instead of "char *buf".
Quoted 11 lines
static void write_block(int fd, char *buf, int
sparse_offset,
int blocksize,blk64_tblock)
{
#ifdefHAVE_LSEEK64misc/e2image.c +1
-2-if (lseek64 (fd, sparse_offset,SEEK_CUR ) <0 ) -perror ("lseek" ); + ret =lseek64 (fd, sparse_offset,SEEK_CUR );#else
misc/e2image.c +1
-2-if (lseek (fd, sparse_offset,SEEK_CUR ) <0 ) -perror ("lseek" ); + ret =lseek (fd, sparse_offset,SEEK_CUR );
Quoted 1 line
#endif
This should just use ext2fs_llseek() as well.
Quoted 7 lines
misc/e2image.c +7
-0+static int initialize_qcow2_image (int fd,ext2_filsys fs, +struct ext2_qcow2_image *image) +{ +struct ext2_qcow2_hdr *header; +blk64_t total_size, offset; +int shift, l2_bits, header_size, l1_size, ret; +int cluster_bits =get_bits_from_size (fs->blocksize);
misc/e2image.c static void output_meta_data_blocks(ext2_filsys fs, int fd) · line 657
Quoted 10 lines
misc/e2image.c +10
-0+struct ext2_super_block *sb = fs->super; + +/* Allocate header */ + header =malloc (sizeof (struct ext2_qcow2_hdr )); +if (!header) +return errno; +memset (header,0 ,sizeof (struct ext2_qcow2_hdr )); + + total_size =ext2fs_blocks_count (sb) << cluster_bits; + image->cluster_size =1 << cluster_bits;
Isn't this just "fs->blocksize" again?
misc/e2image.c static void output_meta_data_blocks(ext2_filsys fs, int fd) · line 685
Quoted 11 lines
misc/e2image.c +11
-0+ image->l2_size =1 << (cluster_bits -3 ); + image->cluster_bits = cluster_bits; + image->fd = fd; + +/* Make space for L1 table */ + offset +=align_offset (l1_size *sizeof (blk64_t), image->cluster_size); + +/* Initialize refcounting */ + ret =init_refcount (image, offset); +if (ret) +return ret;
This will leak "header" on error.
misc/e2image.c static void output_meta_data_blocks(ext2_filsys fs, int fd) · line 767
Quoted 11 lines
misc/e2image.c +11
-0+static void flush_l2_cache (struct ext2_qcow2_image *image) +{ +blk64_t offset, seek =0 ; +struct ext2_qcow2_l2_cache *cache = image->l2_cache; +struct ext2_qcow2_l2_table *table = cache->used_head; +int fd = image->fd; + +/* Store current position */ +if ((offset =lseek (fd,0 ,SEEK_CUR )) <0 ) { +strerror (errno); +exit (1 );
Hmm, seems like a bit harsh for error handling? I guess there isn't much to be done if the seek fails, and that is pretty unlikely. However, exit(1) also makes it harder to ever use this code from within a program.
Also, the use of strerror(errno) in many places is just returning a pointer to a formatted string to the caller, and not actually printing anything that the user can see. You were probably thinking of "perror()", but it would be better to print a more meaningful error than what perror will provide
fprintf(stderr, "%s: unable to seek image file: %s\n", program_name, strerror(errno));
misc/e2image.c
Quoted 2 lines
misc/e2image.c +2
-0+while (cache->free < cache->count) { +assert (table);
This assert() doesn't need to be inside the while loop.
misc/e2image.c static void output_meta_data_blocks(ext2_filsys fs, int fd) · line 836
Quoted 7 lines
misc/e2image.c +7
-0+static int add_l2_item (struct ext2_qcow2_image *img,blk64_t blk, +blk64_t data,blk64_t next) +{ +/* +* Need to create new table if it does not exist, +* or if it is full +* */
Strange formatting of comment?
misc/e2image.c static void output_meta_data_blocks(ext2_filsys fs, int fd) · line 874
Quoted 6 lines
misc/e2image.c +6
-0+static int update_refcount (int fd,struct ext2_qcow2_image *img, +blk64_t offset,blk64_t rfblk_pos) +{ +generic_write (fd, (char *)ref->refcount_block, + img->cluster_size,0 ); +memset ((char *)ref->refcount_block,0 , img->cluster_size);
No need to cast to (char *) for memset.
misc/e2image.c static void output_meta_data_blocks(ext2_filsys fs, int fd) · line 927
Quoted 11 lines
misc/e2image.c +11
-0+static void output_qcow2_meta_data_blocks (ext2_filsys fs,int fd) +{ +errcode_t retval; +blk64_t blk, datablk, offset, size, actual, end; +char *buf; +int sparse =0 ; +struct ext2_qcow2_image *img; +unsigned int header_size, i; +blk64_t l1_index, l2_offset, l2_index; +char *buffer; +__u64 *l2_table;
It would be nice to align these variable declarations consistently.
misc/e2image.c static void output_meta_data_blocks(ext2_filsys fs, int fd) · line 933
Quoted 5 lines
misc/e2image.c +5
-0+/* allocate struct ext2_qcow2_image */ + img =malloc (sizeof (struct ext2_qcow2_image )); +if (!img) { +com_err (program_name,ENOMEM ,"while allocating " +"ext2_qcow2_image" );
Probably nicer to write:
com_err(program_name, ENOMEM, "while allocating ext2_qcow2_image");
misc/e2image.c static void output_meta_data_blocks(ext2_filsys fs, int fd) · line 940
Quoted 7 lines
misc/e2image.c +7
-0+exit (1 ); + } + + retval =initialize_qcow2_image (fd, fs, img); +if (retval) { +com_err (program_name, retval,"while allocating initializing " +"ext2_qcow2_image" );
Same.
misc/e2image.c static void output_meta_data_blocks(ext2_filsys fs, int fd) · line 982
Quoted 9 lines
misc/e2image.c +9
-0+/* Write qcow2 data blocks */ +for (blk =0 ; blk <ext2fs_blocks_count (fs->super); blk++) { +if ((blk >= fs->super->s_first_data_block) && +ext2fs_test_block_bitmap2 (meta_block_map, blk)) { + retval =io_channel_read_blk64 (fs->io, blk,1 , buf); +if (retval) { +com_err (program_name, retval, +"error reading block %llu" , blk); + }
What happens if this error is hit? It doesn't make sense to continue using "buf", and I don't think com_err() does anything other than printing an error.
misc/e2image.c static void output_meta_data_blocks(ext2_filsys fs, int fd) · line 998
Quoted 4 lines
misc/e2image.c +4
-0+if (update_refcount (fd, img, offset, offset)) { +/* +* We have created the new refcount block, this +* means that we need to refcount it as well.So
Move "So" to next line.
misc/e2image.c static void output_meta_data_blocks(ext2_filsys fs, int fd) · line 1005
Quoted 7 lines
misc/e2image.c +7
-0+* the prefious update_refcount refcounted the +* block itself and now we are going to create +* refcount for data. New refcount block should +* not be created! +*/ +if (update_refcount (fd, img, offset, offset)) { +fprintf (stderr,"Programming error\n" );
Would be good to have a better error message here.
misc/e2image.c static void output_meta_data_blocks(ext2_filsys fs, int fd) · line 1022
Quoted 5 lines
misc/e2image.c +5
-0+if (update_refcount (fd, img, offset, + offset)) { +fprintf (stderr,"Programming" +"error\n" ); +exit (1 );
Same.
Quoted 8 lines
misc/e2image.c +4
-0@@ -649,16 +1251,23 @@ int main (int argc, char ** argv) +while ((c =getopt (argc, argv,"rsIQ" )) !=EOF )switch (c) {case 'r' : +if (img_type) +usage (); + img_type |=IMAGE_RAW ;break ;
misc/e2image.c int main (int argc, char ** argv) · line 1267
Quoted 10 lines
misc/e2image.c +7
-0case 's' : + flags |=SCRAMBLE_FLAG ;break ;case 'I' : + flags |=INSTALL_FLAG ; +break ; +case 'Q' : +if (img_type) +usage (); + img_type |=IMAGE_QCOW2 ;
It's always nicer if the command-line options are in alphabetical order, so that it is easier to add in new options.
Cheers, Andreas
Re: [PATCH 2/3 v2] e2image: Support for conversion QCOW2 image into raw
In reply to Lukas CzernerQuoted 6 lines · Lukas Czerner
lib/ext2fs/qcow2.c +0
-0@@ -209,9 +209,10 @@ int qcow2_write_raw_image(int qcow2_fd, int raw_fd, }
lib/ext2fs/qcow2.c int qcow2_write_raw_image(int qcow2_fd, int raw_fd, · line 218
Quoted 10 lines
lib/ext2fs/qcow2.c +2
-1/* Resize the output image to the filesystem size */ -if (lseek (raw_fd, img.image_size,SEEK_SET ) <0 ) +if (lseek (raw_fd, img.image_size -1 ,SEEK_SET ) <0 )return errno; +memset (copy_buf,0 ,1 ); size =write (raw_fd, copy_buf,1 );if (size !=1 )return errno;
This should probably be part of the first patch. Probably memset() is overkill here, just "copy_buf[0] = 0;" is enough.
Quoted 10 lines
misc/e2image.c +2
-0@@ -1229,16 +1229,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) +{
misc/e2image.c static void install_image(char *device, char *image_fn, int type) · line 1243
Quoted 10 lines
misc/e2image.c +10
-0+ +#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, name);
It probably makes sense to check the header here that it is not using AES encryption, and return an error (NULL?) if it is. I don't think there is a flag in the header if compression is used, since that is only marked in each block.
misc/e2image.c int main (int argc, char ** argv) · line 1336
Quoted 2 lines
misc/e2image.c +2
-0+if (flags &IS_QCOW2_FLAG ) { + ret =qcow2_write_raw_image (qcow2_fd, fd, header);
This also reminds me that qcow2_write_raw_image() should check the block numbers read from disk whether they point to zlib-compressed blocks, and return an error if they do, instead of just writing out the compressed data to disk. Otherwise, some users might accidentally try to "restore" the qcow2 image created by some other tool and corrupt their filesystem.
Cheers, Andreas
Re: [PATCH 1/3 v2] e2image: Add support for qcow2 format
In reply to Andreas DilgerHi Andreas thanks for the review and useful comments.
Quoted 11 lines · Andreas Dilger
lib/ext2fs/qcow2.c line 2
Quoted 8 lines
lib/ext2fs/qcow2.c new file +2
-0@@ -0,0 +1,227 @@ +/* +* qcow2.c --- Set of qcow2 related functions It would be nice to have a better comment here. I can see from the filename that this is related to "qcow2". How about something like:
Quoted 14 lines
* Functions to generate qcow2 formatted disk images. This format is used
* originally by QEMU for virtual machines, and stores the filesystem data
* on disk in a packed format to avoid creating sparse image files that need
* lots of seeking to read and write.
*
* The qcow2 format supports zlib compression, but that is not yet implemented. *
* It is possible to directly mount a qcow2 image using qemu-nbd:
*
* [root]# modprobe nbd max_part=63
* [root]# qemu-nbd -c/dev/nbd0image.img
* [root]# mount/dev/nbd0p1/mnt/qemu
*
* Format details at http://people.gnome.org/~markmc/qcow-image-format.html
Very good, I will use it.
Quoted 13 lines
lib/ext2fs/qcow2.c line 42
Quoted 10 lines
lib/ext2fs/qcow2.c new file +9
-0+/* Functions for converting qcow2 image into raw image */ + +struct ext2_qcow2_hdr *qcow2_read_header (int fd,char *fname) +{ +void *buffer =NULL ; +struct ext2_qcow2_hdr *hdr =NULL ; +size_t size; + + buffer =malloc (sizeof (struct ext2_qcow2_hdr ));Because this will be linked into libext2fs, all of these functions should be converted to use the libext2 helper functions for portability reasons.
Quoted 3 lines
s/malloc(/,ext2fs_get_mem(/g
though in some places
ext2fs_get_array()is preferable, since it does overflow checking.
I will change malloc to ext2fs_get_mem, but leave calloc as it is since it will zero the memory as well unliki ext2fs_get_array().
Quoted 10 lines
lib/ext2fs/qcow2.c line 48
Quoted 7 lines
lib/ext2fs/qcow2.c new file +6
-0+if (!buffer) +return NULL ; +memset (buffer,0 ,sizeof (struct ext2_qcow2_hdr )); + +if (lseek (fd,0 ,SEEK_SET ) <0 ) +return NULL ;This should instead use the built-in llseek:
Quoted 5 lines
s/lseek(/ext2fs_llseek(/g
lib/ext2fs/qcow2.c line 50
Quoted 1 line
lib/ext2fs/qcow2.c new file +1
-0+ size =read (fd, buffer,sizeof (struct ext2_qcow2_hdr ));This should use
io_channel_read_blk64()
I do not think so, because there is no "real filesystem" on the qcow2 image, so there is no point of generating all the structures including io_channel. I think that simple read is more than enough, isn't it ? I do not see the benefit of it and also it reads block aligned data only, which header is not.
Quoted 14 lines
lib/ext2fs/qcow2.c line 29
Quoted 11 lines
lib/ext2fs/qcow2.c new file +11
-0+static void flush_l2_cache (struct ext2_qcow2_image *image) +{ +blk64_t offset, seek =0 ; +struct ext2_qcow2_l2_cache *cache = image->l2_cache; +struct ext2_qcow2_l2_table *table = cache->used_head; +int fd = image->fd; + +/* Store current position */ +if ((offset =lseek (fd,0 ,SEEK_CUR )) <0 ) { +strerror (errno); +exit (1 );Hmm, seems like a bit harsh for error handling? I guess there isn't much to be done if the seek fails, and that is pretty unlikely. However, exit(1) also makes it harder to ever use this code from within a program.
Quoted 1 line
Also, the use of strerror(errno) in many places is just returning a pointer to a formatted string to the caller, and not actually printing anything that the user can see. You were probably thinking of "
perror()", but it would be better to print a more meaningful error than what perror will provide
There is not anything I can do if lseek fails. Note that this kind of error handling is used in various places in e2image already. Also it is not a library so I do not see why it should be used by other programs.
However you're right about strerror(), I really meant perror() :) Thanks. But given that how unlikely will this happen on lseek() I'll just create new helper which will print perror() and exit(1). I think this is more than enough for this case, no need to write special err message for each lseek in the e2image (there are a lot of them).
Quoted 2 lines
Cheers, Andreas
I'll resend the updated version shortly.
Thanks!
-Lukas
Re: [PATCH 2/3 v2] e2image: Support for conversion QCOW2 image into raw
In reply to Andreas DilgerQuoted 13 lines · Andreas Dilger
Quoted 12 lines · Lukas Czerner
lib/ext2fs/qcow2.c +1
-1@@ -209,9 +209,10 @@ int qcow2_write_raw_image(int qcow2_fd, int raw_fd, }/* Resize the output image to the filesystem size */ -if (lseek (raw_fd, img.image_size,SEEK_SET ) <0 ) +if (lseek (raw_fd, img.image_size -1 ,SEEK_SET ) <0 )return errno;
Quoted 6 lines
lib/ext2fs/qcow2.c int qcow2_write_raw_image(int qcow2_fd, int raw_fd, · line 218
Quoted 4 lines
lib/ext2fs/qcow2.c +1
-0+memset (copy_buf,0 ,1 ); size =write (raw_fd, copy_buf,1 );if (size !=1 )return errno;This should probably be part of the first patch. Probably
memset()is overkill here, just "copy_buf[0] = 0;" is enough.
Right, thanks.
Quoted 24 lines
Quoted 11 lines
misc/e2image.c +2
-0@@ -1229,16 +1229,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) +{misc/e2image.c static void install_image(char *device, char *image_fn, int type) · line 1243
Quoted 10 lines
misc/e2image.c +10
-0+ +#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, name);It probably makes sense to check the header here that it is not using AES encryption, and return an error (NULL?) if it is. I don't think there is a flag in the header if compression is used, since that is only marked in each block.
Quoted 4 lines
misc/e2image.c int main (int argc, char ** argv) · line 1336
Quoted 2 lines
misc/e2image.c +2
-0+if (flags &IS_QCOW2_FLAG ) { + ret =qcow2_write_raw_image (qcow2_fd, fd, header);This also reminds me that
qcow2_write_raw_image()should check the block numbers read from disk whether they point to zlib-compressed blocks, and return an error if they do, instead of just writing out the compressed data to disk. Otherwise, some users might accidentally try to "restore" the qcow2 image created by some other tool and corrupt their filesystem.
I'll check for the encryption and compression in qcow2_write_raw_image() so it can return an corresponding error code (QCOW_COMPRESSED, QCOW_ENCRYPTED).
Quoted 2 lines
Cheers, Andreas
Once again thanks for the review!
-Lukas
Re: [PATCH 1/3 v2] e2image: Add support for qcow2 format
In reply to Lukas CzernerQuoted 10 lines · Lukas Czerner
Quoted 6 lines · Andreas Dilger
Because this will be linked into libext2fs, all of these functions should be converted to use the libext2 helper functions for portability reasons.
s/malloc(/,ext2fs_get_mem(/g
though in some places
ext2fs_get_array()is preferable, since it does overflow checking.I will change malloc to
ext2fs_get_mem, but leave calloc as it is since it will zero the memory as well unlikiext2fs_get_array().
Please use ext2fs_get_mem() and memset then. I've been thinking for a while to add proper memory accounting to libext2fs for large filesystems. The current use of the system memory accounting will overflow at 32-bit values, so cannot properly account for checking very large filesystems where we already know that e2fsck is allocating more that 4GB.
Keeping matching ext2_get_mem() and ext2fs_free_mem() is important for a number of reasons.
Cheers, Andreas