[Libguestfs] [PATCH 4/4] virt-sparsify --in-place: Exit with code 3 if discard is not supported.

Richard W.M. Jones rjones at redhat.com
Mon Mar 17 12:32:25 UTC 2014


Also skip the test in this case instead of failing.
---
 sparsify/cmdline.ml                     |  2 +-
 sparsify/in_place.ml                    | 13 ++++++++++++-
 sparsify/test-virt-sparsify-in-place.sh |  8 +++++++-
 sparsify/virt-sparsify.pod              | 19 +++++++++++++++++++
 4 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/sparsify/cmdline.ml b/sparsify/cmdline.ml
index d25e9e9..c96de57 100644
--- a/sparsify/cmdline.ml
+++ b/sparsify/cmdline.ml
@@ -24,7 +24,7 @@ open Common_gettext.Gettext
 open Common_utils
 
 let prog = Filename.basename Sys.executable_name
-let error fs = error ~prog fs
+let error ?exit_code fs = error ~prog ?exit_code fs
 
 type mode_t =
 | Mode_copying of string * check_t * bool * string option * string option
diff --git a/sparsify/in_place.ml b/sparsify/in_place.ml
index 9f46ad3..624f676 100644
--- a/sparsify/in_place.ml
+++ b/sparsify/in_place.ml
@@ -28,12 +28,23 @@ module G = Guestfs
 open Common_utils
 open Cmdline
 
-let run disk format ignores machine_readable quiet verbose trace zeroes =
+let rec run disk format ignores machine_readable quiet verbose trace zeroes =
   (* Connect to libguestfs. *)
   let g = new G.guestfs () in
   if trace then g#set_trace true;
   if verbose then g#set_verbose true;
 
+  try
+    perform g disk format ignores machine_readable quiet zeroes
+  with
+    G.Error msg as exn ->
+      if g#last_errno () = G.Errno.errno_ENOTSUP then (
+        (* for exit code 3, see man page *)
+        error ~exit_code:3 (f_"discard/trim is not supported: %s") msg;
+      )
+      else raise exn
+
+and perform g disk format ignores machine_readable quiet zeroes =
   (* XXX Current limitation of the API.  Can remove this hunk in future. *)
   let format =
     match format with
diff --git a/sparsify/test-virt-sparsify-in-place.sh b/sparsify/test-virt-sparsify-in-place.sh
index 56311a0..dd32963 100755
--- a/sparsify/test-virt-sparsify-in-place.sh
+++ b/sparsify/test-virt-sparsify-in-place.sh
@@ -49,7 +49,13 @@ EOF
 
 size_before=$(du -s test-virt-sparsify-in-place.img | awk '{print $1}')
 
-$VG ./virt-sparsify --debug-gc --in-place test-virt-sparsify-in-place.img
+$VG ./virt-sparsify --debug-gc --in-place test-virt-sparsify-in-place.img || {
+    if [ "$?" -eq 3 ]; then
+        echo "$0: discard not supported in virt-sparsify"
+        exit 77
+    fi
+    exit 1
+}
 
 size_after=$(du -s test-virt-sparsify-in-place.img | awk '{print $1}')
 
diff --git a/sparsify/virt-sparsify.pod b/sparsify/virt-sparsify.pod
index c12a15f..de07439 100644
--- a/sparsify/virt-sparsify.pod
+++ b/sparsify/virt-sparsify.pod
@@ -381,6 +381,25 @@ temporary space are B<not> required.
 
 For other environment variables, see L<guestfs(3)/ENVIRONMENT VARIABLES>.
 
+=head1 EXIT STATUS
+
+This program returns 0 if the operation completed without errors.
+(This doesn't necessarily mean that space could be freed up.)
+
+A non-zero exit code indicates an error.
+
+If the exit code is C<3> and the I<--in-place> option was used, that
+indicates that discard support is not available in libguestfs, so
+copying mode must be used instead.
+
+=over 4
+
+=item 0
+
+successful exit
+
+=back
+
 =head1 SEE ALSO
 
 L<virt-filesystems(1)>,
-- 
1.8.5.3




More information about the Libguestfs mailing list