Chen Hanxiao
2015-Mar-03 08:48 UTC
[Libguestfs] [PATCH 0/2] btrfs: add support to btrfs-image
This series adds new APIs to support btrfstune. Chen Hanxiao (2): New API: btrfs-image New API: btrfs_image_restore daemon/btrfs.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ generator/actions.ml | 45 +++++++++++++++++++++++++++ src/MAX_PROC_NR | 2 +- 3 files changed, 133 insertions(+), 1 deletion(-) -- 2.1.0
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- daemon/btrfs.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ generator/actions.ml | 23 +++++++++++++++++++++++ src/MAX_PROC_NR | 2 +- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/daemon/btrfs.c b/daemon/btrfs.c index d4b3207..be648bc 100644 --- a/daemon/btrfs.c +++ b/daemon/btrfs.c @@ -37,6 +37,7 @@ GUESTFSD_EXT_CMD(str_btrfstune, btrfstune); GUESTFSD_EXT_CMD(str_btrfsck, btrfsck); GUESTFSD_EXT_CMD(str_mkfs_btrfs, mkfs.btrfs); GUESTFSD_EXT_CMD(str_umount, umount); +GUESTFSD_EXT_CMD(str_btrfsimage, btrfs-image); int optgroup_btrfs_available (void) @@ -2005,3 +2006,51 @@ do_btrfstune_enable_skinny_metadata_extent_refs (const char *device) return 0; } + +int +do_btrfs_image (char *const *sources, const char *image, + int compresslevel, int numthreads) +{ + size_t nr_sources = count_strings (sources); + const size_t MAX_ARGS = 64 + nr_sources; + const char *argv[MAX_ARGS]; + size_t i = 0, j; + CLEANUP_FREE char *err = NULL; + CLEANUP_FREE char *out = NULL; + char compresslevel_s[64]; + char numthreads_s[64]; + int r; + + if (nr_sources == 0) { + reply_with_error ("list of sources must be non-empty"); + return -1; + } + + ADD_ARG (argv, i, str_btrfsimage); + + if (compresslevel >= 0) { + snprintf (compresslevel_s, sizeof compresslevel_s, "%d", compresslevel); + ADD_ARG (argv, i, "-c"); + ADD_ARG (argv, i, compresslevel_s); + } + + if (numthreads) { + snprintf (numthreads_s, sizeof numthreads_s, "%d", numthreads); + ADD_ARG (argv, i, "-t"); + ADD_ARG (argv, i, numthreads_s); + } + + for (j = 0; j < nr_sources; ++j) + ADD_ARG (argv, i, sources[j]); + + ADD_ARG (argv, i, image); + ADD_ARG (argv, i, NULL); + + r = commandv (&out, &err, argv); + if (r == -1) { + reply_with_error ("%s %s: %s", sources[0], image, err); + return -1; + } + + return 0; +} diff --git a/generator/actions.ml b/generator/actions.ml index 448dc4f..090f797 100644 --- a/generator/actions.ml +++ b/generator/actions.ml @@ -12522,6 +12522,29 @@ This will Enable extended inode refs." }; longdesc = "\ This enable skinny metadata extent refs." }; + { defaults with + name = "btrfs_image"; + style = RErr, [DeviceList "source"; Pathname "image"], [OInt "compresslevel"; OInt "numthreads"]; + proc_nr = Some 453; + optional = Some "btrfs"; camel_name = "BTRFSImage"; + tests = [ + InitPartition, Always, TestRun ( + [["part_init"; "/dev/sda"; "mbr"]; + ["part_add"; "/dev/sda"; "p"; "64"; "204799"]; + ["part_add"; "/dev/sda"; "p"; "204800"; "409599"]; + ["mkfs_btrfs"; "/dev/sda1"; ""; ""; "NOARG"; ""; "NOARG"; "NOARG"; ""; ""]; + ["mkfs_btrfs"; "/dev/sda2"; ""; ""; "NOARG"; ""; "NOARG"; "NOARG"; ""; ""]; + ["mount"; "/dev/sda1"; "/"]; + ["btrfs_image"; "/dev/sda2"; "/1.img"; ""; ""]; + ["btrfs_image"; "/dev/sda2"; "/2.img"; "2"; "3"]]), [] + ]; + + shortdesc = "create an image of a btrfs filesystem"; + longdesc = "\ +This used to create an image of a btrfs filesystem. +All data will be zeroed, but metadata and the like is preserved." }; + + ] (* Non-API meta-commands available only in guestfish. diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index 8670c73..534b992 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -452 +453 -- 2.1.0
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> --- daemon/btrfs.c | 38 ++++++++++++++++++++++++++++++++++++++ generator/actions.ml | 22 ++++++++++++++++++++++ src/MAX_PROC_NR | 2 +- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/daemon/btrfs.c b/daemon/btrfs.c index be648bc..e036880 100644 --- a/daemon/btrfs.c +++ b/daemon/btrfs.c @@ -2054,3 +2054,41 @@ do_btrfs_image (char *const *sources, const char *image, return 0; } + +int +do_btrfs_image_restore (const char *image, char *const *sources) +{ + size_t nr_sources = count_strings (sources); + const size_t MAX_ARGS = 64 + nr_sources; + const char *argv[MAX_ARGS]; + size_t i = 0, j; + CLEANUP_FREE char *err = NULL; + CLEANUP_FREE char *out = NULL; + int r; + + if (nr_sources == 0) { + reply_with_error ("list of sources must be non-empty"); + return -1; + } + + ADD_ARG (argv, i, str_btrfsimage); + ADD_ARG (argv, i, "-r"); + + if (nr_sources > 1) + ADD_ARG (argv, i, "-m"); + + ADD_ARG (argv, i, image); + + for (j = 0; j < nr_sources; ++j) + ADD_ARG (argv, i, sources[j]); + + ADD_ARG (argv, i, NULL); + + r = commandv (&out, &err, argv); + if (r == -1) { + reply_with_error ("%s %s: %s", image, sources[0], err); + return -1; + } + + return 0; +} diff --git a/generator/actions.ml b/generator/actions.ml index 090f797..c4c882e 100644 --- a/generator/actions.ml +++ b/generator/actions.ml @@ -12545,6 +12545,28 @@ This used to create an image of a btrfs filesystem. All data will be zeroed, but metadata and the like is preserved." }; + { defaults with + name = "btrfs_image_restore"; + style = RErr, [Pathname "image"; DeviceList "source"], []; + proc_nr = Some 454; + optional = Some "btrfs"; camel_name = "BTRFSImage"; + tests = [ + InitPartition, Always, TestRun ( + [["part_init"; "/dev/sda"; "mbr"]; + ["part_add"; "/dev/sda"; "p"; "64"; "204799"]; + ["part_add"; "/dev/sda"; "p"; "204800"; "409599"]; + ["part_add"; "/dev/sda"; "p"; "409600"; "614399"]; + ["mkfs_btrfs"; "/dev/sda1"; ""; ""; "NOARG"; ""; "NOARG"; "NOARG"; ""; ""]; + ["mkfs_btrfs"; "/dev/sda2 /dev/sda3"; ""; ""; "NOARG"; ""; "NOARG"; "NOARG"; ""; ""]; + ["mount"; "/dev/sda1"; "/"]; + ["btrfs_image"; "/dev/sda2"; "/1.img"; ""; ""]; + ["btrfs_image_restore"; "/1.img"; "/dev/sda2 /dev/sda3"]]), [] + ]; + + shortdesc = "restore metadump image to btrfs filesystem"; + longdesc = "\ +This used to restore metadump image to btrfs filesystem."}; + ] (* Non-API meta-commands available only in guestfish. diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index 534b992..515f19a 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -453 +454 -- 2.1.0
Richard W.M. Jones
2015-Mar-05 12:47 UTC
Re: [Libguestfs] [PATCH 1/2] New API: btrfs-image
On Tue, Mar 03, 2015 at 03:48:02AM -0500, Chen Hanxiao wrote:> Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com> > + if (compresslevel >= 0) { > + snprintf (compresslevel_s, sizeof compresslevel_s, "%d", compresslevel); > + ADD_ARG (argv, i, "-c"); > + ADD_ARG (argv, i, compresslevel_s); > + }Because compresslevel is an optional parameter, you can't just use it directly like this. You have to check optargs_bitmask. The condition should be something like: if ((optargs_bitmask & GUESTFS_BTRFS_IMAGE_COMPRESSLEVEL_BITMASK) && compresslevel >= 0) { ... }> + if (numthreads) {I would prefer not to expose the -t parameter, since it's essentially an implementation detail. If btrfs-image doesn't do the right thing, then you could pass `sysconf (_SC_NPROCESSORS_ONLN)' here (see builder/pxzcat-c.c for an example).> +This used to create an image of a btrfs filesystem.^ This is used ...> +All data will be zeroed, but metadata and the like is preserved." };This is fine with the corrections above. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
Richard W.M. Jones
2015-Mar-05 12:48 UTC
Re: [Libguestfs] [PATCH 2/2] New API: btrfs_image_restore
I'm guessing that what users will actually want to do is to download and upload these images, rather than having to write them to another part of the disk image first. I wonder if btrfs-image can do streaming? Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/