Blob Blame History Raw
From 6f8c270b3fb7b5b3109d2553fef38f128b6137ac Mon Sep 17 00:00:00 2001
From: Laszlo Ersek <lersek@redhat.com>
Date: Thu, 2 Dec 2021 14:10:06 +0100
Subject: [PATCH] sysprep: remove system-local NetworkManager connection
 profiles (keyfiles)

Add a simple (default) operation to remove

  /etc/NetworkManager/system-connections/*.nmconnection

which arguably carry stale information after the initial creation of the
system disk image.

Note: no side effect callback is invoked. Before commit 576f1541a20c
("sysprep: Use customize module for customizing the guest after
sysprepping.", 2014-03-25), the "delete" operation had been native to
virt-sysprep ("sysprep/sysprep_operation_delete.ml"), and it didn't invoke
side effects. In said commit, "delete" was delegated to virt-customize,
and that was when "side_effects#created_file" was introduced (most likely)
as a catch-all. (We still have the "XXX Did we?" comment today.)

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1980922
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20211202131006.12774-1-lersek@redhat.com>
Acked-by: Richard W.M. Jones <rjones@redhat.com>
(cherry picked from commit 903819ecf480bcefec108bdbd7e9bdec1b3b5a49)
---
 sysprep/Makefile.am                     |  1 +
 sysprep/sysprep_operation_net_nmconn.ml | 43 +++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 sysprep/sysprep_operation_net_nmconn.ml

diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am
index d32ab20e..561a71ae 100644
--- a/sysprep/Makefile.am
+++ b/sysprep/Makefile.am
@@ -52,6 +52,7 @@ operations = \
 	mail_spool \
 	net_hostname \
 	net_hwaddr \
+	net_nmconn \
 	pacct_log \
 	package_manager_cache \
 	pam_data \
diff --git a/sysprep/sysprep_operation_net_nmconn.ml b/sysprep/sysprep_operation_net_nmconn.ml
new file mode 100644
index 00000000..2d8667f1
--- /dev/null
+++ b/sysprep/sysprep_operation_net_nmconn.ml
@@ -0,0 +1,43 @@
+(* virt-sysprep
+ * Copyright (C) 2012-2021 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 Sysprep_operation
+
+let glob = "/etc/NetworkManager/system-connections/*.nmconnection"
+
+let net_nmconn_perform (g : Guestfs.guestfs) root side_effects =
+  let typ = g#inspect_get_type root in
+  let distro = g#inspect_get_distro root in
+  match typ, distro with
+  | "linux", ("fedora"|"rhel"|"centos"|"scientificlinux"|"oraclelinux"|
+              "redhat-based") -> Array.iter g#rm_f (g#glob_expand glob)
+  | _ -> ()
+
+let op = {
+  defaults with
+    name = "net-nmconn";
+    enabled_by_default = true;
+    heading = s_"Remove system-local NetworkManager connection profiles \
+      (keyfiles)";
+    pod_description = Some (s_"On Fedora and Red Hat Enterprise Linux, remove \
+      the C<" ^ glob ^ "> files.");
+    perform_on_filesystems = Some net_nmconn_perform;
+}
+
+let () = register_operation op
-- 
2.31.1