From: "Richard W.M. Jones" <rjones(a)redhat.com>
---
daemon/Makefile.am | 1 +
daemon/fstrim.c | 97 ++++++++++++++++++++++++++++++++++++++++
generator/generator_actions.ml | 27 ++++++++++-
gobject/Makefile.inc | 6 ++-
po/POTFILES | 2 +
src/MAX_PROC_NR | 2 +-
6 files changed, 130 insertions(+), 5 deletions(-)
create mode 100644 daemon/fstrim.c
diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index 02c21df..9e2a633 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -114,6 +114,7 @@ guestfsd_SOURCES = \
fill.c \
find.c \
fsck.c \
+ fstrim.c \
glob.c \
grep.c \
grub.c \
diff --git a/daemon/fstrim.c b/daemon/fstrim.c
new file mode 100644
index 0000000..e2daf6a
--- /dev/null
+++ b/daemon/fstrim.c
@@ -0,0 +1,97 @@
+/* libguestfs - the guestfsd daemon
+ * Copyright (C) 2012 Red Hat 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.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include "guestfs_protocol.h"
+#include "daemon.h"
+#include "actions.h"
+#include "optgroups.h"
+
+#define MAX_ARGS 64
+
+int
+optgroup_fstrim_available (void)
+{
+ return prog_exists ("fstrim");
+}
+
+/* Takes optional arguments, consult optargs_bitmask. */
+int
+do_fstrim (const char *path,
+ int64_t offset, int64_t length, int64_t minimumfreeextent)
+{
+ const char *argv[MAX_ARGS];
+ size_t i = 0;
+ char offset_s[64], length_s[64], mfe_s[64];
+ char *err;
+ int r;
+
+ ADD_ARG (argv, i, "fstrim");
+
+ if ((optargs_bitmask & GUESTFS_FSTRIM_OFFSET_BITMASK)) {
+ if (offset < 0) {
+ reply_with_error ("offset < 0");
+ return -1;
+ }
+
+ snprintf (offset_s, sizeof offset_s, "%" PRIi64, offset);
+ ADD_ARG (argv, i, "-o");
+ ADD_ARG (argv, i, offset_s);
+ }
+
+ if ((optargs_bitmask & GUESTFS_FSTRIM_LENGTH_BITMASK)) {
+ if (length <= 0) {
+ reply_with_error ("length <= 0");
+ return -1;
+ }
+
+ snprintf (length_s, sizeof length_s, "%" PRIi64, length);
+ ADD_ARG (argv, i, "-l");
+ ADD_ARG (argv, i, length_s);
+ }
+
+ if ((optargs_bitmask & GUESTFS_FSTRIM_MINIMUMFREEEXTENT_BITMASK)) {
+ if (minimumfreeextent <= 0) {
+ reply_with_error ("minimumfreeextent <= 0");
+ return -1;
+ }
+
+ snprintf (mfe_s, sizeof mfe_s, "%" PRIi64, minimumfreeextent);
+ ADD_ARG (argv, i, "-m");
+ ADD_ARG (argv, i, mfe_s);
+ }
+
+ ADD_ARG (argv, i, path);
+ ADD_ARG (argv, i, NULL);
+
+ r = commandv (NULL, &err, argv);
+ if (r == -1) {
+ reply_with_error ("%s", err);
+ free (err);
+ return -1;
+ }
+ free (err);
+
+ return 0;
+}
diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml
index cfbce00..120278a 100644
--- a/generator/generator_actions.ml
+++ b/generator/generator_actions.ml
@@ -6821,8 +6821,9 @@ The filesystem must be mounted read-write.
The filesystem contents are not affected, but any free space
in the filesystem is freed.
-In future (but not currently) these zeroed blocks will be
-\"sparsified\" - that is, given back to the host.");
+Free space is not \"trimmed\". You may want to call
+C<guestfs_fstrim> either as an alternative to this,
+or after calling this, depending on your requirements.");
("lvcreate_free", (RErr, [String "logvol"; String
"volgroup"; Int "percent"], []), 312, [Optional "lvm2"],
[InitEmpty, Always, TestOutputList (
@@ -7259,6 +7260,28 @@ a later version of the filesystem, or having incompatible
features.
See also C<guestfs_available>, L<guestfs(3)/AVAILABILITY>.");
+ ("fstrim", (RErr, [Pathname "mountpoint"], [OInt64
"offset"; OInt64 "length"; OInt64 "minimumfreeextent"]),
334, [Optional "fstrim"],
+ [],
+ "trim free space in a filesystem",
+ "\
+Trim the free space in the filesystem mounted on C<mountpoint>.
+The filesystem must be mounted read-write.
+
+The filesystem contents are not affected, but any free space
+in the filesystem is \"trimmed\", that is, given back to the host
+device, thus making disk images more sparse, allowing unused space
+in qcow2 files to be reused, etc.
+
+This operation requires support in libguestfs, the mounted
+filesystem, the host filesystem, qemu and the host kernel.
+If this support isn't present it may give an error or even
+appear to run but do nothing.
+
+See also C<guestfs_zero_free_space>. That is a slightly
+different operation that turns free space in the filesystem
+into zeroes. It is valid to call C<guestfs_fstrim> either
+instead of, or after calling C<guestfs_zero_free_space>.");
+
]
let all_functions = non_daemon_functions @ daemon_functions
diff --git a/gobject/Makefile.inc b/gobject/Makefile.inc
index d4db407..056c5f5 100644
--- a/gobject/Makefile.inc
+++ b/gobject/Makefile.inc
@@ -61,7 +61,8 @@ guestfs_gobject_headers= \
include/guestfs-gobject/optargs-ntfsclone_out.h \
include/guestfs-gobject/optargs-mkfs_btrfs.h \
include/guestfs-gobject/optargs-set_e2attrs.h \
- include/guestfs-gobject/optargs-btrfs_fsck.h
+ include/guestfs-gobject/optargs-btrfs_fsck.h \
+ include/guestfs-gobject/optargs-fstrim.h
guestfs_gobject_sources= \
src/session.c \
@@ -104,4 +105,5 @@ guestfs_gobject_sources= \
src/optargs-ntfsclone_out.c \
src/optargs-mkfs_btrfs.c \
src/optargs-set_e2attrs.c \
- src/optargs-btrfs_fsck.c
+ src/optargs-btrfs_fsck.c \
+ src/optargs-fstrim.c
diff --git a/po/POTFILES b/po/POTFILES
index 9d3282e..747b341 100644
--- a/po/POTFILES
+++ b/po/POTFILES
@@ -34,6 +34,7 @@ daemon/fill.c
daemon/find.c
daemon/findfs.c
daemon/fsck.c
+daemon/fstrim.c
daemon/glob.c
daemon/grep.c
daemon/grub.c
@@ -142,6 +143,7 @@ gobject/src/optargs-copy_device_to_file.c
gobject/src/optargs-copy_file_to_device.c
gobject/src/optargs-copy_file_to_file.c
gobject/src/optargs-e2fsck.c
+gobject/src/optargs-fstrim.c
gobject/src/optargs-inspect_get_icon.c
gobject/src/optargs-md_create.c
gobject/src/optargs-mkfs_btrfs.c
diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR
index 55bd0ac..0ae9d1e 100644
--- a/src/MAX_PROC_NR
+++ b/src/MAX_PROC_NR
@@ -1 +1 @@
-333
+334
--
1.7.10.1