[Libguestfs] [guestfs-tools PATCH] sysprep: remove system-local NetworkManager connection profiles (keyfiles)

Richard W.M. Jones
2021-12-02T18:53:22Z

On Thu, Dec 02, 2021 at 02:10:06PM +0100, Laszlo Ersek
wrote:
> 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 at redhat.com>
> ---
>
> Notes:
>     Tested with:
>
>     $ virt-sysprep -v -x --enable=net-nmconn \
>           -a CentOS-Stream-GenericCloud-9-20211201.1.x86_64.qcow2
>
>     (Download directory:
>     <https://cloud.centos.org/centos/9-stream/x86_64/images/>.)
>
>     Log excerpt:
>
>     > libguestfs: trace: inspect_get_distro = "centos"
>     > libguestfs: trace: glob_expand
"/etc/NetworkManager/system-connections/*.nmconnection"
>     > [...]
>     > libguestfs: trace: glob_expand =
["/etc/NetworkManager/system-connections/ens3.nmconnection"]
>     > libguestfs: trace: rm_f
"/etc/NetworkManager/system-connections/ens3.nmconnection"
>     > [...]
>     > libguestfs: trace: rm_f = 0
>
>     And indeed the file got removed (checked with guestfish afterwards).
>
>  sysprep/Makefile.am                     |  1 +
>  sysprep/sysprep_operation_net_nmconn.ml | 43 ++++++++++++++++++++
>  2 files changed, 44 insertions(+)
>
> diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am
> index e213244bc17a..0e3afc8a01c7 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 000000000000..2d8667f19dfa
> --- /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
>
> base-commit: 05946b64f896b334a337f6982033e0e98545207b

Looks good.  I have checked the code in case you needed to call
side_effects#<some method> but that's not needed for deleting a file.

ACK

Rich.

--
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.