Blob Blame History Raw
From d4a1a398ec871c803f37a70efe3d2e8d083bd2fe Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Fri, 15 May 2015 10:02:28 +0100
Subject: [PATCH] customize: Give a clear error message if host_cpu not
 compatible with guest arch.

In cases where we are asked to run commands in the guest (eg.  options
such as --run-command or --install), give a clear error in the cases
where the guest arch is not compatible with the host arch.

Similar code existed in virt-builder, but I have removed that.  Users
will still get an error message, it will just happen a bit later on.

There is a slight change in semantics here, in that architectures are
no longer normalized when matching, but that's probably fine since
`virt-builder -l' prints the exact arch string that people should use.

(cherry picked from commit 8864c47b94cf44ac2d0d8d4b5019073ad1da121b)
---
 builder/Makefile.am        |  9 +-------
 builder/architecture.ml    | 41 ----------------------------------
 builder/builder.ml         |  2 +-
 builder/cmdline.ml         | 21 ++----------------
 builder/uname-c.c          | 55 ----------------------------------------------
 builder/uname.ml           | 27 -----------------------
 builder/uname.mli          | 28 -----------------------
 customize/Makefile.am      |  1 +
 customize/customize_run.ml | 10 +++++++++
 mllib/Makefile.am          |  4 ++--
 mllib/common_utils.ml      |  9 ++++++++
 mllib/common_utils.mli     |  4 ++++
 po/POTFILES                |  1 -
 po/POTFILES-ml             |  2 --
 resize/Makefile.am         |  1 +
 sparsify/Makefile.am       |  2 +-
 sysprep/Makefile.am        |  2 +-
 v2v/Makefile.am            |  2 +-
 18 files changed, 34 insertions(+), 187 deletions(-)
 delete mode 100644 builder/architecture.ml
 delete mode 100644 builder/uname-c.c
 delete mode 100644 builder/uname.ml
 delete mode 100644 builder/uname.mli

diff --git a/builder/Makefile.am b/builder/Makefile.am
index dd96533..1698160 100644
--- a/builder/Makefile.am
+++ b/builder/Makefile.am
@@ -44,7 +44,6 @@ CLEANFILES = *~ *.cmi *.cmo *.cmx *.cmxa *.o virt-builder
 
 # Alphabetical order.
 SOURCES = \
-	architecture.ml \
 	builder.ml \
 	cache.mli \
 	cache.ml \
@@ -72,10 +71,7 @@ SOURCES = \
 	sigchecker.mli \
 	sigchecker.ml \
 	sources.mli \
-	sources.ml \
-	uname.ml \
-	uname.mli \
-	uname-c.c
+	sources.ml
 
 man_MANS =
 noinst_DATA =
@@ -121,9 +117,6 @@ deps = \
 	pxzcat.cmx \
 	setlocale-c.o \
 	setlocale.cmx \
-	uname-c.o \
-	uname.cmx \
-	architecture.cmx \
 	ini_reader.cmx \
 	paths.cmx \
 	languages.cmx \
diff --git a/builder/architecture.ml b/builder/architecture.ml
deleted file mode 100644
index 59c1cf6..0000000
--- a/builder/architecture.ml
+++ /dev/null
@@ -1,41 +0,0 @@
-(* virt-builder
- * Copyright (C) 2014 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.
- *)
-
-open Common_gettext.Gettext
-open Common_utils
-
-open Unix
-
-let filter_arch = function
-  | "amd64" | "x86_64" | "x64" -> "x86_64"
-  | "powerpc" | "ppc" -> "ppc"
-  | arch -> arch
-
-let arch_is_compatible nativearch otherarch =
-  let nativearch = filter_arch nativearch in
-  let otherarch = filter_arch otherarch in
-  match nativearch, otherarch with
-  | a, b when a = b -> true
-  | "x86_64", "i386" -> true
-  | "ppc64", "ppc" -> true
-  | "sparc64", "sparc" -> true
-  | a, b -> false
-
-let current_arch =
-  try filter_arch ((Uname.uname ()).Uname.machine)
-  with Unix_error _ -> "unknown"
diff --git a/builder/builder.ml b/builder/builder.ml
index 121c5fb..1ab580b 100644
--- a/builder/builder.ml
+++ b/builder/builder.ml
@@ -243,7 +243,7 @@ let main () =
   let item =
     try List.find (
       fun (name, { Index_parser.arch = a }) ->
-        name = arg && arch = Architecture.filter_arch a
+        name = arg && arch = a
     ) index
     with Not_found ->
       eprintf (f_"%s: cannot find os-version '%s' with architecture '%s'.\nUse --list to list available guest types.\n")
diff --git a/builder/cmdline.ml b/builder/cmdline.ml
index 1242aaa..56447e7 100644
--- a/builder/cmdline.ml
+++ b/builder/cmdline.ml
@@ -312,25 +312,8 @@ read the man page virt-builder(1).
   (* Check the architecture. *)
   let arch =
     match arch with
-    | "" -> Architecture.current_arch
-    | arch ->
-      let target_arch = Architecture.filter_arch arch in
-      if Architecture.arch_is_compatible Architecture.current_arch target_arch <> true then (
-        let requires_execute_on_guest = List.exists (
-          function
-          | `Command _ | `InstallPackages _ | `Script _ | `Update -> true
-          | `Delete _ | `Edit _ | `FirstbootCommand _ | `FirstbootPackages _
-          | `FirstbootScript _ | `Hostname _ | `Link _ | `Mkdir _
-          | `Password _ | `RootPassword _ | `Scrub _ | `Timezone _ | `Upload _
-          | `Write _ | `Chmod _ -> false
-        ) ops.ops in
-        if requires_execute_on_guest then (
-          eprintf (f_"%s: sorry, cannot run commands on a guest with a different architecture\n")
-            prog;
-          exit 1
-        );
-      );
-      target_arch in
+    | "" -> Config.host_cpu
+    | arch -> arch in
 
   (* If user didn't elect any root password, that means we set a random
    * root password.
diff --git a/builder/uname-c.c b/builder/uname-c.c
deleted file mode 100644
index b8a6ef7..0000000
--- a/builder/uname-c.c
+++ /dev/null
@@ -1,55 +0,0 @@
-/* virt-builder
- * Copyright (C) 2014 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 <errno.h>
-#include <sys/utsname.h>
-
-#include <caml/alloc.h>
-#include <caml/fail.h>
-#include <caml/memory.h>
-#include <caml/mlvalues.h>
-
-#ifdef HAVE_CAML_UNIXSUPPORT_H
-#include <caml/unixsupport.h>
-#else
-#define Nothing ((value) 0)
-extern void unix_error (int errcode, char * cmdname, value arg) Noreturn;
-#endif
-
-value
-virt_builder_uname (value unit)
-{
-  CAMLparam0 ();
-  CAMLlocal1 (rv);
-  struct utsname u;
-
-  if (uname (&u) < 0)
-    unix_error (errno, (char *) "uname", Val_int (0));
-
-  rv = caml_alloc (5, 0);
-
-  Store_field (rv, 0, caml_copy_string (u.sysname));
-  Store_field (rv, 1, caml_copy_string (u.nodename));
-  Store_field (rv, 2, caml_copy_string (u.release));
-  Store_field (rv, 3, caml_copy_string (u.version));
-  Store_field (rv, 4, caml_copy_string (u.machine));
-
-  CAMLreturn (rv);
-}
diff --git a/builder/uname.ml b/builder/uname.ml
deleted file mode 100644
index c370c2c..0000000
--- a/builder/uname.ml
+++ /dev/null
@@ -1,27 +0,0 @@
-(* virt-builder
- * Copyright (C) 2014 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.
- *)
-
-type uname_struct = {
-  sysname : string;
-  nodename : string;
-  release : string;
-  version : string;
-  machine : string;
-}
-
-external uname : unit -> uname_struct = "virt_builder_uname"
diff --git a/builder/uname.mli b/builder/uname.mli
deleted file mode 100644
index aea441b..0000000
--- a/builder/uname.mli
+++ /dev/null
@@ -1,28 +0,0 @@
-(* virt-builder
- * Copyright (C) 2014 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.
- *)
-
-type uname_struct = {
-  sysname : string;
-  nodename : string;
-  release : string;
-  version : string;
-  machine : string;
-}
-
-val uname : unit -> uname_struct
-(** [uname] Tiny wrapper to the C [uname]. *)
diff --git a/customize/Makefile.am b/customize/Makefile.am
index 0760476..ebdc2d4 100644
--- a/customize/Makefile.am
+++ b/customize/Makefile.am
@@ -64,6 +64,7 @@ if HAVE_OCAML
 deps = \
 	$(top_builddir)/fish/guestfish-uri.o \
 	$(top_builddir)/fish/guestfish-file-edit.o \
+	$(top_builddir)/mllib/config.cmx \
 	$(top_builddir)/mllib/common_gettext.cmx \
 	$(top_builddir)/mllib/tty-c.o \
 	$(top_builddir)/mllib/tTY.cmx \
diff --git a/customize/customize_run.ml b/customize/customize_run.ml
index af513f0..6b861d5 100644
--- a/customize/customize_run.ml
+++ b/customize/customize_run.ml
@@ -31,6 +31,12 @@ let run ~prog ~verbose ~quiet (g : Guestfs.guestfs) root (ops : ops) =
   (* Timestamped messages in ordinary, non-debug non-quiet mode. *)
   let msg fs = make_message_function ~quiet fs in
 
+  (* Is the host_cpu compatible with the guest arch?  ie. Can we
+   * run commands in this guest?
+   *)
+  let guest_arch = g#inspect_get_arch root in
+  let guest_arch_compatible = guest_arch_compatible guest_arch in
+
   (* Based on the guest type, choose a log file location. *)
   let logfile =
     match g#inspect_get_type root with
@@ -55,6 +61,10 @@ let run ~prog ~verbose ~quiet (g : Guestfs.guestfs) root (ops : ops) =
 
   (* Useful wrapper for scripts. *)
   let do_run ~display cmd =
+    if not guest_arch_compatible then
+      error ~prog (f_"host cpu (%s) and guest arch (%s) are not compatible, so you cannot use command line options that involve running commands in the guest.  Use --firstboot scripts instead.")
+            Config.host_cpu guest_arch;
+
     (* Add a prologue to the scripts:
      * - Pass environment variables through from the host.
      * - Send stdout and stderr to a log file so we capture all output
diff --git a/mllib/Makefile.am b/mllib/Makefile.am
index 653b8aa..9916f7a 100644
--- a/mllib/Makefile.am
+++ b/mllib/Makefile.am
@@ -151,11 +151,11 @@ DEFAULT_INCLUDES = \
 check_SCRIPTS = common_utils_tests
 
 if HAVE_OCAMLOPT
-common_utils_tests: common_gettext.cmx tty-c.o tTY.cmx common_utils.cmx common_utils_tests.cmx
+common_utils_tests: config.cmx common_gettext.cmx tty-c.o tTY.cmx common_utils.cmx common_utils_tests.cmx
 	$(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) \
 	  mlguestfs.cmxa -linkpkg $^ -cclib -lncurses -o $@
 else
-common_utils_tests: common_gettext.cmo tty-c.o tTY.cmo common_utils.cmo common_utils_tests.cmo
+common_utils_tests: config.cmo common_gettext.cmo tty-c.o tTY.cmo common_utils.cmo common_utils_tests.cmo
 	$(OCAMLFIND) ocamlc $(OCAMLCFLAGS) \
 	  mlguestfs.cma -linkpkg $^ -cclib -lncurses -custom -o $@
 endif
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 7c64ad0..a2513ea 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -593,3 +593,12 @@ let is_directory path =
 let absolute_path path =
   if not (Filename.is_relative path) then path
   else Sys.getcwd () // path
+
+(* Are guest arch and host_cpu compatible, in terms of being able
+ * to run commands in the libguestfs appliance?
+ *)
+let guest_arch_compatible guest_arch =
+  match Config.host_cpu, guest_arch with
+  | x, y when x = y -> true
+  | "x86_64", ("i386"|"i486"|"i586"|"i686") -> true
+  | _ -> false
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index 6d0a0fc..4cf9259 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -129,3 +129,7 @@ val is_directory : string -> bool
 
 val absolute_path : string -> string
 (** Convert any path to an absolute path. *)
+
+val guest_arch_compatible : string -> bool
+(** Are guest arch and host_cpu compatible, in terms of being able
+    to run commands in the libguestfs appliance? *)
diff --git a/po/POTFILES b/po/POTFILES
index c64afda..320710f 100644
--- a/po/POTFILES
+++ b/po/POTFILES
@@ -6,7 +6,6 @@ builder/index-struct.c
 builder/index-validate.c
 builder/pxzcat-c.c
 builder/setlocale-c.c
-builder/uname-c.c
 cat/cat.c
 cat/filesystems.c
 cat/log.c
diff --git a/po/POTFILES-ml b/po/POTFILES-ml
index 7403497..915c611 100644
--- a/po/POTFILES-ml
+++ b/po/POTFILES-ml
@@ -1,4 +1,3 @@
-builder/architecture.ml
 builder/builder.ml
 builder/cache.ml
 builder/cmdline.ml
@@ -13,7 +12,6 @@ builder/pxzcat.ml
 builder/setlocale.ml
 builder/sigchecker.ml
 builder/sources.ml
-builder/uname.ml
 customize/crypt.ml
 customize/customize_cmdline.ml
 customize/customize_main.ml
diff --git a/resize/Makefile.am b/resize/Makefile.am
index eb4ded3..26a6587 100644
--- a/resize/Makefile.am
+++ b/resize/Makefile.am
@@ -32,6 +32,7 @@ if HAVE_OCAML
 
 # Note this list must be in dependency order.
 deps = \
+	$(top_builddir)/mllib/config.cmx \
 	$(top_builddir)/mllib/tty-c.o \
 	$(top_builddir)/mllib/tTY.cmx \
 	$(top_builddir)/mllib/fsync-c.o \
diff --git a/sparsify/Makefile.am b/sparsify/Makefile.am
index 48cd5f0..6717eb0 100644
--- a/sparsify/Makefile.am
+++ b/sparsify/Makefile.am
@@ -52,11 +52,11 @@ virt_sparsify_CFLAGS = \
 	$(WARN_CFLAGS) $(WERROR_CFLAGS)
 
 BOBJECTS = \
+	$(top_builddir)/mllib/config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
 	$(top_builddir)/mllib/tTY.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/progress.cmo \
-	$(top_builddir)/mllib/config.cmo \
 	$(SOURCES_ML:.ml=.cmo)
 XOBJECTS = $(BOBJECTS:.cmo=.cmx)
 
diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am
index b1cebc2..17991c9 100644
--- a/sysprep/Makefile.am
+++ b/sysprep/Makefile.am
@@ -81,13 +81,13 @@ if HAVE_OCAML
 
 # Note this list must be in dependency order.
 deps = \
+	$(top_builddir)/mllib/config.cmx \
 	$(top_builddir)/mllib/common_gettext.cmx \
 	$(top_builddir)/mllib/tty-c.o \
 	$(top_builddir)/mllib/tTY.cmx \
 	$(top_builddir)/mllib/common_utils.cmx \
 	$(top_builddir)/mllib/uri-c.o \
 	$(top_builddir)/mllib/uRI.cmx \
-	$(top_builddir)/mllib/config.cmx \
 	$(top_builddir)/mllib/mkdtemp-c.o \
 	$(top_builddir)/mllib/mkdtemp.cmx \
 	$(top_builddir)/mllib/regedit.cmx \
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
index 242dd49..26955aa 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -123,12 +123,12 @@ virt_v2v_CFLAGS = \
 	$(LIBVIRT_CFLAGS)
 
 BOBJECTS = \
+	$(top_builddir)/mllib/config.cmo \
 	$(top_builddir)/mllib/common_gettext.cmo \
 	$(top_builddir)/mllib/tTY.cmo \
 	$(top_builddir)/mllib/common_utils.cmo \
 	$(top_builddir)/mllib/regedit.cmo \
 	$(top_builddir)/mllib/progress.cmo \
-	$(top_builddir)/mllib/config.cmo \
 	$(top_builddir)/mllib/mkdtemp.cmo \
 	$(top_builddir)/mllib/JSON.cmo \
 	$(top_builddir)/customize/urandom.cmo \
-- 
1.8.3.1