libguestfs / libguestfs

library and tools for accessing and modifying virtual machine disk images. PLEASE DO NOT USE GITHUB FOR ISSUES OR PULL REQUESTS. See the website for how to file a bug or contact us.
http://libguestfs.org
GNU General Public License v2.0
631 stars 175 forks source link

[virt-sysprep] RFE: Add an operation to clean cloud-init data #119

Open eskultety opened 1 year ago

eskultety commented 1 year ago

When cloud-init runs in an instance, it'll populate /var/lib/cloud with various data. If such an instance is then re-configured to later become an enhanced template image, one has to make sure --delete /var/lib/cloud is passed to virt-sysprep, otherwise the next instance boot provisioned out of such an image may behave unexpectedly, because of the presence of some data in the aforementioned directory - many cloud-init modules have the execution policy set to _once_perboot, e.g. the ssh module which means that the host keys will be generated exactly once, i.e. when an instance is booted from the original vendor cloud image, which in turn means that if one enhances the vendor provided cloud-init image and then runs sysprep, the SSH keys won't be regenerated in the new instance during boot because ssh-keygen service is disabled when cloud-init service is enabled which runs ssh-keygen on demand.

rwmjones commented 1 year ago

Here's an example of a virt-sysprep operation that just rm -rf 's a directory: https://github.com/rwmjones/guestfs-tools/blob/master/sysprep/sysprep_operation_mail_spool.ml

rwmjones commented 1 year ago

I'm a little confused by the initial report. Is it sufficient to simply rm -rf /var/lib/cloud or are there other actions that have to be taken?

eskultety commented 1 year ago

I'm a little confused by the initial report. Is it sufficient to simply rm -rf /var/lib/cloud or are there other actions that have to be taken?

@rwmjones well, yes, /etc/machine-id needs to be reset on systemd platforms if you're not cleaning that one yet.

A few notes on the proposed approach itself:

To summarize the above, removing the /var/lib/cloud directory on Fedora, OpenSUSE, Debian, and FreeBSD is perfectly sufficient in order to reset cloud-init to its desired default behaviour for next boot. However, despite having been doing the above for some time myself, I went over the cloud-init docs and I found this: https://cloudinit.readthedocs.io/en/latest/reference/cli.html#clean. So, in order to be 100% compliant with the docs, which is probably what we actually want to do, we should use the clean subcommand.

eskultety commented 1 year ago

I just stumbled on this: https://gitlab.alpinelinux.org/alpine/cloud/tiny-cloud#main-phase. TL;DR Alpine added experimental implementation of some low memory footprint cloud-init implementation. The difference is that because this is a custom implementation with limited functionality and limited compatibility with cloud-init, there isn't any cloud-init command available. What I'm trying to say is that removal of the /var/lib/cloud directory should be ultimately considered as well when this RFE gets implemented because it'll get us more platform coverage.