[Libguestfs] [PATCH 1/2] resize: add --unknown-filesystems

Pino Toscano ptoscano at redhat.com
Fri Oct 23 16:28:58 UTC 2015


Introduce a new option to control how virt-resize behaves when asking to
expand a filesystem, either unknown to libguestfs or that virt-resize
cannot expand. The default keeps the current behaviour, i.e. just warn.
---
 bash/virt-resize       |  3 +++
 resize/resize.ml       | 65 ++++++++++++++++++++++++++++++++++++++++++++++++--
 resize/virt-resize.pod | 28 +++++++++++++++++++++-
 3 files changed, 93 insertions(+), 3 deletions(-)

diff --git a/bash/virt-resize b/bash/virt-resize
index 4471f07..5627807 100644
--- a/bash/virt-resize
+++ b/bash/virt-resize
@@ -36,6 +36,9 @@ _guestfs_options_only ()
         --password-crypto)
             COMPREPLY=( $( compgen -W "md5 sha256 sha512" -- "$cur") )
             return ;;
+        --unknown-filesystems)
+            COMPREPLY=( $( compgen -W "ignore warn error" -- "$cur") )
+            return ;;
     esac
 
     case "$cur" in
diff --git a/resize/resize.ml b/resize/resize.ml
index 75bb2f7..ecb7cf0 100644
--- a/resize/resize.ml
+++ b/resize/resize.ml
@@ -149,13 +149,18 @@ let string_of_expand_content_method = function
   | BtrfsFilesystemResize -> s_"btrfs-filesystem-resize"
   | XFSGrowFS -> s_"xfs_growfs"
 
+type unknown_filesystems_mode =
+  | UnknownFsIgnore
+  | UnknownFsWarn
+  | UnknownFsError
+
 (* Main program. *)
 let main () =
   let infile, outfile, align_first, alignment, copy_boot_loader,
     deletes,
     dryrun, expand, expand_content, extra_partition, format, ignores,
     lv_expands, machine_readable, ntfsresize_force, output_format,
-    resizes, resizes_force, shrink, sparse =
+    resizes, resizes_force, shrink, sparse, unknown_fs_mode =
 
     let add xs s = xs := s :: !xs in
 
@@ -187,6 +192,7 @@ let main () =
       else shrink := s
     in
     let sparse = ref true in
+    let unknown_fs_mode = ref "warn" in
 
     let ditto = " -\"-" in
     let argspec = [
@@ -215,6 +221,8 @@ let main () =
       "--resize-force", Arg.String (add resizes_force), s_"part=size" ^ " " ^ s_"Forcefully resize partition";
       "--shrink",  Arg.String set_shrink,     s_"part" ^ " " ^ s_"Shrink partition";
       "--no-sparse", Arg.Clear sparse,        " " ^ s_"Turn off sparse copying";
+      "--unknown-filesystems", Arg.Set_string unknown_fs_mode,
+                                              s_"ignore|warn|error" ^ " " ^ s_"Behaviour on expand unknown filesystems (default: warn)";
     ] in
     let argspec = set_standard_options argspec in
     let disks = ref [] in
@@ -253,6 +261,7 @@ read the man page virt-resize(1).
     let resizes_force = List.rev !resizes_force in
     let shrink = match !shrink with "" -> None | str -> Some str in
     let sparse = !sparse in
+    let unknown_fs_mode = !unknown_fs_mode in
 
     if alignment < 1 then
       error (f_"alignment cannot be < 1");
@@ -266,6 +275,14 @@ read the man page virt-resize(1).
       | _ ->
         error (f_"unknown --align-first option: use never|always|auto") in
 
+    let unknown_fs_mode =
+      match unknown_fs_mode with
+      | "ignore" -> UnknownFsIgnore
+      | "warn" -> UnknownFsWarn
+      | "error" -> UnknownFsError
+      | _ ->
+        error (f_"unknown --unknown-filesystems: use ignore|warn|error") in
+
     (* No arguments and machine-readable mode?  Print out some facts
      * about what this binary supports.  We only need to print out new
      * things added since this option, or things which depend on features
@@ -315,7 +332,7 @@ read the man page virt-resize(1).
     deletes,
     dryrun, expand, expand_content, extra_partition, format, ignores,
     lv_expands, machine_readable, ntfsresize_force, output_format,
-    resizes, resizes_force, shrink, sparse in
+    resizes, resizes_force, shrink, sparse, unknown_fs_mode in
 
   (* Default to true, since NTFS/btrfs/XFS support are usually available. *)
   let ntfs_available = ref true in
@@ -821,6 +838,50 @@ read the man page virt-resize(1).
       lv.lv_operation <- LVOpExpand
   ) lv_expands;
 
+  (* In case we need to error out on unknown/unhandled filesystems,
+   * iterate on what we need to resize/expand.
+   *)
+  (match unknown_fs_mode with
+  | UnknownFsIgnore -> ()
+  | UnknownFsWarn -> ()
+  | UnknownFsError ->
+    List.iter (
+      fun p ->
+        match p.p_operation with
+        | OpCopy
+        | OpIgnore
+        | OpDelete -> ()
+        | OpResize _ ->
+          if not (can_expand_content p.p_type) then (
+            (match p.p_type with
+            | ContentUnknown
+            | ContentPV _
+            | ContentExtendedPartition -> ()
+            | ContentFS (fs, _) ->
+              error (f_"unknown/unavailable method for expanding the %s filesystem on %s")
+                fs p.p_name
+            );
+          )
+    ) partitions;
+
+    List.iter (
+      fun lv ->
+        match lv.lv_operation with
+        | LVOpNone -> ()
+        | LVOpExpand ->
+          if not (can_expand_content lv.lv_type) then (
+            (match lv.lv_type with
+            | ContentUnknown
+            | ContentPV _
+            | ContentExtendedPartition -> ()
+            | ContentFS (fs, _) ->
+              error (f_"unknown/unavailable method for expanding the %s filesystem on %s")
+                fs lv.lv_name;
+            );
+          )
+    ) lvs;
+  );
+
   (* Print a summary of what we will do. *)
   flush stderr;
 
diff --git a/resize/virt-resize.pod b/resize/virt-resize.pod
index db282b4..a0ab459 100644
--- a/resize/virt-resize.pod
+++ b/resize/virt-resize.pod
@@ -578,6 +578,28 @@ gigabyte of free space.
 
 Note that you cannot use I<--expand> and I<--shrink> together.
 
+=item B<--unknown-filesystems ignore>
+
+=item B<--unknown-filesystems warn>
+
+=item B<--unknown-filesystems error>
+
+Configure the behaviour of virt-resize when asking to expand a
+filesystem, and neither libguestfs has the support it, nor virt-resize
+knows how to expand the content of the filesystem.
+
+I<--unknown-filesystems ignore> will cause virt-resize to silently
+ignore such filesystems, and nothing is printed about them.
+
+I<--unknown-filesystems warn> (the default behaviour) will cause
+virt-resize to warn for each of the filesystem that cannot be
+expanded, but still continuing to resize the disk.
+
+I<--unknown-filesystems error> will cause virt-resize to error out
+at the first filesystem that cannot be expanded.
+
+See also L</"unknown/unavailable method for expanding the TYPE filesystem on DEVICE/LV">.
+
 =item B<-v>
 
 =item B<--verbose>
@@ -764,7 +786,11 @@ expand that type of filesystem.
 
 =back
 
-In both cases, virt-resize will not expand the mentioned filesystem.
+In both cases, virt-resize will not expand the mentioned filesystem;
+the result (unless I<--unknown-filesystems error> is specified)
+is that the partitions containing such filesystems will be actually
+bigger as requested, but the filesystems will still be usable at the
+their older sizes.
 
 =head1 ALTERNATIVE TOOLS
 
-- 
2.1.0




More information about the Libguestfs mailing list