[Libguestfs] [PATCH 3/3] builder: Warn if --output is a host partition.

Richard W.M. Jones rjones at redhat.com
Thu Jun 2 15:11:37 UTC 2016


But allow the warning to be turned off using --no-warn-if-partition.

Ming Xie tried to create a bootable USB key using
'virt-p2v-make-disk -o /dev/sdX1'.  That works, but doesn't create a
bootable key because it puts everything into the first partition.

Emit a warning if someone tries to do this to try to prevent user
error:

  virt-builder: warning: output device (/dev/sdb1) is a partition.  If you
  are writing to a USB key or external drive then you probably need to write
  to the whole device, not to a partition.  If this warning is wrong then you
  can disable it with --no-warn-if-partition
---
 builder/builder.ml       | 9 +++++++++
 builder/cmdline.ml       | 6 ++++++
 builder/cmdline.mli      | 1 +
 builder/virt-builder.pod | 9 +++++++++
 4 files changed, 25 insertions(+)

diff --git a/builder/builder.ml b/builder/builder.ml
index 3c17ccd..affce10 100644
--- a/builder/builder.ml
+++ b/builder/builder.ml
@@ -280,6 +280,15 @@ let main () =
 
   (* --- If we get here, we want to create a guest. --- *)
 
+  (* Warn if the user might be writing to a partition on a USB key. *)
+  (match cmdline.output with
+   | Some device when is_partition device ->
+      if cmdline.warn_if_partition then
+        warning (f_"output device (%s) is a partition.  If you are writing to a USB key or external drive then you probably need to write to the whole device, not to a partition.  If this warning is wrong then you can disable it with --no-warn-if-partition")
+                device;
+   | Some _ | None -> ()
+  );
+
   (* Download the template, or it may be in the cache. *)
   let template =
     let template, delete_on_exit =
diff --git a/builder/cmdline.ml b/builder/cmdline.ml
index f872bd4..7b33013 100644
--- a/builder/cmdline.ml
+++ b/builder/cmdline.ml
@@ -51,6 +51,7 @@ type cmdline = {
   smp : int option;
   sources : (string * string) list;
   sync : bool;
+  warn_if_partition : bool;
 }
 
 let parse_cmdline () =
@@ -115,6 +116,7 @@ let parse_cmdline () =
   let add_source arg = sources := arg :: !sources in
 
   let sync = ref true in
+  let warn_if_partition = ref true in
 
   let argspec = [
     "--arch",    Arg.Set_string arch,       "arch" ^ " " ^ s_"Set the output architecture";
@@ -163,6 +165,8 @@ let parse_cmdline () =
     "--smp",     Arg.Int set_smp,           "vcpus" ^ " " ^ s_"Set number of vCPUs";
     "--source",  Arg.String add_source,     "URL" ^ " " ^ s_"Set source URL";
     "--no-sync", Arg.Clear sync,            " " ^ s_"Do not fsync output file on exit";
+    "--no-warn-if-partition", Arg.Clear warn_if_partition,
+                                            " " ^ s_"Do not warn if writing to a partition";
   ] in
   let customize_argspec, get_customize_ops = Customize_cmdline.argspec () in
   let customize_argspec =
@@ -212,6 +216,7 @@ read the man page virt-builder(1).
   let smp = !smp in
   let sources = List.rev !sources in
   let sync = !sync in
+  let warn_if_partition = !warn_if_partition in
 
   (* No arguments and machine-readable mode?  Print some facts. *)
   if args = [] && machine_readable then (
@@ -324,4 +329,5 @@ read the man page virt-builder(1).
     gpg = gpg; list_format = list_format; memsize = memsize;
     network = network; ops = ops; output = output;
     size = size; smp = smp; sources = sources; sync = sync;
+    warn_if_partition = warn_if_partition;
   }
diff --git a/builder/cmdline.mli b/builder/cmdline.mli
index 4c201dd..854db61 100644
--- a/builder/cmdline.mli
+++ b/builder/cmdline.mli
@@ -39,6 +39,7 @@ type cmdline = {
   smp : int option;
   sources : (string * string) list;
   sync : bool;
+  warn_if_partition : bool;
 }
 
 val parse_cmdline : unit -> cmdline
diff --git a/builder/virt-builder.pod b/builder/virt-builder.pod
index be5b568..cd3be9d 100644
--- a/builder/virt-builder.pod
+++ b/builder/virt-builder.pod
@@ -496,6 +496,15 @@ Note that you should not point I<--source> to sources that you don't
 trust (unless the source is signed by someone you do trust).  See also
 the I<--no-network> option.
 
+=item B<--no-warn-if-partition>
+
+Do not emit a warning if the output device is a partition.  This
+warning avoids a common user error when writing to a USB key or
+external drive, when you should normally write to the whole device
+(S<I<--output /dev/sdX>>), not to a partition on the device
+(S<I<--output /dev/sdX1>>).  Use this option to I<suppress> this
+warning.
+
 =item B<-v>
 
 =item B<--verbose>
-- 
2.7.4




More information about the Libguestfs mailing list