mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone
Blob Blame History Raw
From 44afd4c1f26379b32f62690cf19e06022cafa994 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Thu, 2 Jun 2016 14:51:12 +0100
Subject: [PATCH] builder: Warn if --output is a host partition (RHBZ#1342337).

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

(cherry picked from commit 7274f11ad408bcd4299ba82b3b26f46e62930d03)
---
 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 a6f92c7..ac4c748 100644
--- a/builder/builder.ml
+++ b/builder/builder.ml
@@ -268,6 +268,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 4893cbf..5205fe6 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 = push_front 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 (
@@ -323,4 +328,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 545b134..94ba430 100644
--- a/builder/virt-builder.pod
+++ b/builder/virt-builder.pod
@@ -495,6 +495,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>
-- 
1.8.3.1