Blob Blame History Raw
From a99e410a2dc450569bc3218005fbc38e60e2d4af Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Tue, 7 Jun 2016 10:24:54 +0100
Subject: [PATCH] customize: Add --uninstall operation (RHBZ#1343375).

(cherry picked from commit 6425a8824b13a60ddf5d04270808da00568b1b53)
---
 customize/customize_run.ml | 29 +++++++++++++++++++++++++++++
 generator/customize.ml     | 17 +++++++++++++++--
 2 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/customize/customize_run.ml b/customize/customize_run.ml
index 8c38d62..10647fd 100644
--- a/customize/customize_run.ml
+++ b/customize/customize_run.ml
@@ -154,6 +154,30 @@ exec >>%s 2>&1
     | pm ->
       error_unimplemented_package_manager (s_"--update") pm
 
+  and guest_uninstall_command packages =
+    let quoted_args = String.concat " " (List.map quote packages) in
+    match g#inspect_get_package_management root with
+    | "apk" -> sprintf "apk del %s" quoted_args
+    | "apt" ->
+      (* http://unix.stackexchange.com/questions/22820 *)
+      sprintf "
+        export DEBIAN_FRONTEND=noninteractive
+        apt_opts='-q -y -o Dpkg::Options::=--force-confnew'
+        apt-get $apt_opts remove %s
+      " quoted_args
+    | "dnf" ->    sprintf "dnf -y remove %s" quoted_args
+    | "pisi" ->   sprintf "pisi rm %s" quoted_args
+    | "pacman" -> sprintf "pacman -R %s" quoted_args
+    | "urpmi" ->  sprintf "urpme %s" quoted_args
+    | "xbps" ->   sprintf "xbps-remove -Sy %s" quoted_args
+    | "yum" ->    sprintf "yum -y remove %s" quoted_args
+    | "zypper" -> sprintf "zypper -n rm -l %s" quoted_args
+
+    | "unknown" ->
+      error_unknown_package_manager (s_"--uninstall")
+    | pm ->
+      error_unimplemented_package_manager (s_"--uninstall") pm
+
   (* Windows has package_management == "unknown". *)
   and error_unknown_package_manager flag =
     error (f_"cannot use '%s' because no package manager has been detected for this guest OS.\n\nIf this guest OS is a common one with ordinary package management then this may have been caused by a failure of libguestfs inspection.\n\nFor OSes such as Windows that lack package management, this is not possible.  Try using one of the '--firstboot*' flags instead (described in the manual).") flag
@@ -334,6 +358,11 @@ exec >>%s 2>&1
       message (f_"Running touch: %s") path;
       g#touch path
 
+    | `UninstallPackages pkgs ->
+      message (f_"Uninstalling packages: %s") (String.concat " " pkgs);
+      let cmd = guest_uninstall_command pkgs in
+      do_run ~display:cmd cmd
+
     | `Update ->
       message (f_"Updating packages");
       let cmd = guest_update_command () in
diff --git a/generator/customize.ml b/generator/customize.ml
index 403bb89..8caf2b5 100644
--- a/generator/customize.ml
+++ b/generator/customize.ml
@@ -214,7 +214,7 @@ installed during the image build using the guest's package manager
 For an overview on the different ways to install packages, see
 L<virt-builder(1)/INSTALLING PACKAGES>.
 
-See also I<--update>.";
+See also I<--update>, I<--uninstall>.";
   };
 
   { op_name = "link";
@@ -424,6 +424,19 @@ string like C<Europe/London>";
 This command performs a L<touch(1)>-like operation on C<FILE>.";
   };
 
+  { op_name = "uninstall";
+    op_type = StringList "PKG,PKG..";
+    op_discrim = "`UninstallPackages";
+    op_shortdesc = "Uninstall package(s)";
+    op_pod_longdesc = "\
+Uninstall the named packages (a comma-separated list).  These are
+removed during the image build using the guest's package manager
+(eg. apt, yum, etc.).  Dependent packages may also need to be
+uninstalled to satisfy the request.
+
+See also I<--install>, I<--update>.";
+  };
+
   { op_name = "update";
     op_type = Unit;
     op_discrim = "`Update";
@@ -433,7 +446,7 @@ Do the equivalent of C<yum update>, C<apt-get upgrade>, or whatever
 command is required to update the packages already installed in the
 template to their latest versions.
 
-See also I<--install>.";
+See also I<--install>, I<--uninstall>.";
   };
 
   { op_name = "upload";
-- 
2.7.4