[Libguestfs] [PATCH v2] New API: btrfs-image

Chen Hanxiao chenhanxiao at cn.fujitsu.com
Tue Mar 17 06:53:35 UTC 2015


Signed-off-by: Chen Hanxiao <chenhanxiao at cn.fujitsu.com>
---
v2: add optargs_bitmask check

 daemon/btrfs.c       | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 generator/actions.ml | 22 ++++++++++++++++++++++
 src/MAX_PROC_NR      |  2 +-
 3 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/daemon/btrfs.c b/daemon/btrfs.c
index d4b3207..340b548 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,54 @@ 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 ((optargs_bitmask & GUESTFS_BTRFS_IMAGE_COMPRESSLEVEL_BITMASK)
+    && compresslevel >= 0) {
+    snprintf (compresslevel_s, sizeof compresslevel_s, "%d", compresslevel);
+    ADD_ARG (argv, i, "-c");
+    ADD_ARG (argv, i, compresslevel_s);
+  }
+
+  /* btrfs-progs will valid numtheads and choose the right one for us */
+  if ((optargs_bitmask & GUESTFS_BTRFS_IMAGE_NUMTHREADS_BITMASK)
+    && 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 fb971d3..8effc0e 100644
--- a/generator/actions.ml
+++ b/generator/actions.ml
@@ -12522,6 +12522,28 @@ 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 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"];
+         ["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 is 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 @@
-453
+454
-- 
2.1.0




More information about the Libguestfs mailing list