[Pkg-libvirt-commits] [libguestfs] 18/29: New API: btrfs_replace

Hilko Bengen bengen at moszumanska.debian.org
Sun Nov 1 17:14:17 UTC 2015


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to annotated tag upstream/1.29.48
in repository libguestfs.

commit 36a197e2cc119f6d2691b110af5b292ba794162b
Author: Cao jin <caoj.fnst at cn.fujitsu.com>
Date:   Wed Jun 24 10:08:42 2015 +0800

    New API: btrfs_replace
    
    Signed-off-by: Cao jin <caoj.fnst at cn.fujitsu.com>
---
 daemon/btrfs.c                    | 36 +++++++++++++++++++++
 generator/actions.ml              | 16 ++++++++++
 src/MAX_PROC_NR                   |  2 +-
 tests/btrfs/Makefile.am           |  3 +-
 tests/btrfs/test-btrfs-replace.sh | 66 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 121 insertions(+), 2 deletions(-)

diff --git a/daemon/btrfs.c b/daemon/btrfs.c
index 69e306b..210212d 100644
--- a/daemon/btrfs.c
+++ b/daemon/btrfs.c
@@ -2060,3 +2060,39 @@ do_btrfs_image (char *const *sources, const char *image,
 
   return 0;
 }
+
+int
+do_btrfs_replace (const char *srcdev, const char *targetdev,
+                                const char* mntpoint)
+{
+  const size_t MAX_ARGS = 64;
+  const char *argv[MAX_ARGS];
+  size_t i = 0;
+  CLEANUP_FREE char *err = NULL;
+  CLEANUP_FREE char *path_buf = NULL;
+  int r;
+
+  path_buf = sysroot_path (mntpoint);
+  if (path_buf == NULL) {
+    reply_with_perror ("malloc");
+    return -1;
+  }
+
+  ADD_ARG (argv, i, str_btrfs);
+  ADD_ARG (argv, i, "replace");
+  ADD_ARG (argv, i, "start");
+  ADD_ARG (argv, i, srcdev);
+  ADD_ARG (argv, i, targetdev);
+  ADD_ARG (argv, i, path_buf);
+  ADD_ARG (argv, i, "-B");
+  ADD_ARG (argv, i, "-f");
+  ADD_ARG (argv, i, NULL);
+
+  r = commandv (NULL, &err, argv);
+  if (r == -1) {
+    reply_with_error ("%s: %s", mntpoint, err);
+    return -1;
+  }
+
+  return 0;
+}
diff --git a/generator/actions.ml b/generator/actions.ml
index 11b8652..6089afa 100644
--- a/generator/actions.ml
+++ b/generator/actions.ml
@@ -12606,6 +12606,22 @@ numbered C<partnum> on device C<device>.
 
 It returns C<primary>, C<logical>, or C<extended>." };
 
+  { defaults with
+    name = "btrfs_replace"; added = (1, 29, 48);
+    style = RErr, [Device "srcdev"; Device "targetdev"; Pathname "mntpoint"], [];
+    proc_nr = Some 455;
+    optional = Some "btrfs"; camel_name = "BTRFSReplace";
+    test_excuse = "put the test in 'tests/btrfs' directory";
+    shortdesc = "replace a btrfs managed device with another device";
+    longdesc = "\
+Replace device of a btrfs filesystem. On a live filesystem, duplicate the data
+to the target device which is currently stored on the source device.
+After completion of the operation, the source device is wiped out and
+removed from the filesystem.
+
+The C<targetdev> needs to be same size or larger than the C<srcdev>. Devices
+which are currently mounted are never allowed to be used as the C<targetdev>." };
+
 ]
 
 (* Non-API meta-commands available only in guestfish.
diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR
index 515f19a..4930863 100644
--- a/src/MAX_PROC_NR
+++ b/src/MAX_PROC_NR
@@ -1 +1 @@
-454
+455
diff --git a/tests/btrfs/Makefile.am b/tests/btrfs/Makefile.am
index bf4d7ae..b6ef794 100644
--- a/tests/btrfs/Makefile.am
+++ b/tests/btrfs/Makefile.am
@@ -20,7 +20,8 @@ include $(top_srcdir)/subdir-rules.mk
 TESTS = \
 	test-btrfs-misc.pl \
 	test-btrfs-devices.sh \
-	test-btrfs-subvolume-default.pl
+	test-btrfs-subvolume-default.pl \
+	test-btrfs-replace.sh
 
 TESTS_ENVIRONMENT = $(top_builddir)/run --test
 
diff --git a/tests/btrfs/test-btrfs-replace.sh b/tests/btrfs/test-btrfs-replace.sh
new file mode 100755
index 0000000..2bf6d6d
--- /dev/null
+++ b/tests/btrfs/test-btrfs-replace.sh
@@ -0,0 +1,66 @@
+#!/bin/bash -
+# libguestfs
+# Copyright (C) 2015 Fujitsu Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# Test btrfs replace devices.
+
+set -e
+
+# Allow the test to be skipped since btrfs is often broken.
+if [ -n "$SKIP_TEST_BTRFS_REPLACE_SH" ]; then
+    echo "$0: skipping test because environment variable is set."
+    exit 77
+fi
+
+# If btrfs is not available, bail.
+if ! guestfish -a /dev/null run : available btrfs; then
+    echo "$0: skipping test because btrfs is not available"
+    exit 77
+fi
+
+rm -f test-btrfs-devices-{1,2}.img replace.output
+
+guestfish  <<EOF > replace.output
+# Add 2 empty disks
+sparse test-btrfs-devices-1.img 1G
+sparse test-btrfs-devices-2.img 1G
+run
+
+mkfs-btrfs /dev/sda
+mount /dev/sda /
+
+mkdir /data
+copy-in $srcdir/../data/filesanddirs-10M.tar.xz /data
+
+# now, sda is btrfs while sdb is blank.
+btrfs-replace /dev/sda /dev/sdb /
+
+# after replace: sda is wiped out, while sdb has btrfs with data
+list-filesystems
+ls /data/
+
+EOF
+
+if [ "$(cat replace.output)" != "/dev/sda: unknown
+/dev/sdb: btrfs
+filesanddirs-10M.tar.xz" ]; then
+    echo "btrfs-repalce fail!"
+    cat replace.output
+    exit 1
+fi
+
+rm test-btrfs-devices-{1,2}.img replace.output

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git



More information about the Pkg-libvirt-commits mailing list