Set NOCOW flag to newly created images to solve performance issues on btrfs.
Btrfs has terrible performance when hosting VM images, even more when the guest in those VM are also using btrfs as file system. One way to mitigate this bad performance is to turn off COW attributes on VM files (since having copy on write for this kind of data is not useful). Signed-off-by: Chunyan Liu <cy...@suse.com> --- qemu-img.c | 15 +++++++++++++++ 1 files changed, 15 insertions(+), 0 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index bf3fb4f..d43e8f1 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -34,11 +34,17 @@ #include <getopt.h> #include <stdio.h> #include <stdarg.h> +#include <linux/fs.h> +#include <sys/ioctl.h> #ifdef _WIN32 #include <windows.h> #endif +#ifndef FS_NOCOW_FL +#define FS_NOCOW_FL 0x00800000 /* Do not cow file */ +#endif + typedef struct img_cmd_t { const char *name; int (*handler)(int argc, char **argv); @@ -340,6 +346,7 @@ static int img_create(int argc, char **argv) char *options = NULL; Error *local_err = NULL; bool quiet = false; + int fd, attr; for(;;) { c = getopt(argc, argv, "F:b:f:he6o:q"); @@ -417,6 +424,14 @@ static int img_create(int argc, char **argv) return 1; } + /* set NOCOW by default to solve performance issue on btrfs */ + fd = qemu_open(filename, O_RDONLY|O_NONBLOCK); + if (fd >= 0) { + attr = FS_NOCOW_FL; + ioctl(fd, FS_IOC_SETFLAGS, &attr); + qemu_close(fd); + } + return 0; } -- 1.6.0.2