diff options
author | Chad Smith <chad.smith@canonical.com> | 2023-01-17 19:34:31 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-17 19:34:31 -0700 |
commit | abfdf1d83995cc20eb929d98485c321afb3a73b9 (patch) | |
tree | a21efa0e6d5dfdcd5b95fd449176e7c58a0dca11 /cloudinit/cmd | |
parent | dcd8413c8874b615fc7e3c54da15f6ed5d8dedc7 (diff) | |
download | cloud-init-git-abfdf1d83995cc20eb929d98485c321afb3a73b9.tar.gz |
machine-id: set to uninitialized to trigger regeneration on clones
With systemd-networkd, the removal of /etc/machine-id can
result in DHCP errors such as:
DHCP4 CLIENT: Failed to set IAID+DUID: No such file or directory
Adapt cloud-init clean --machine-id to be systemd-aware.
systemd v. 237 and later will trigger /etc/machine-id regeneration
on next boot when the value of /etc/machine-id file is
'uninitialized\n'.
On systems without systemd, continue to remove /etc/machine-id when
cloud-init clean --machine-id is provided as there are triggers in
place to regenerate on absence of this file.
LP: #1999680
Diffstat (limited to 'cloudinit/cmd')
-rwxr-xr-x | cloudinit/cmd/clean.py | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/cloudinit/cmd/clean.py b/cloudinit/cmd/clean.py index 65d3eece..5a61eac5 100755 --- a/cloudinit/cmd/clean.py +++ b/cloudinit/cmd/clean.py @@ -12,6 +12,7 @@ import os import sys from cloudinit import settings +from cloudinit.distros import uses_systemd from cloudinit.stages import Init from cloudinit.subp import ProcessExecutionError, runparts, subp from cloudinit.util import ( @@ -20,6 +21,7 @@ from cloudinit.util import ( error, get_config_logfiles, is_link, + write_file, ) ETC_MACHINE_ID = "/etc/machine-id" @@ -55,8 +57,9 @@ def get_parser(parser=None): action="store_true", default=False, help=( - "Remove /etc/machine-id for golden image creation." - " Next boot generates a new machine-id." + "Set /etc/machine-id to 'uninitialized\n' for golden image" + "creation. On next boot, systemd generates a new machine-id." + " Remove /etc/machine-id on non-systemd environments." ), ) parser.add_argument( @@ -120,7 +123,12 @@ def handle_clean_args(name, args): """Handle calls to 'cloud-init clean' as a subcommand.""" exit_code = remove_artifacts(args.remove_logs, args.remove_seed) if args.machine_id: - del_file(ETC_MACHINE_ID) + if uses_systemd(): + # Systemd v237 and later will create a new machine-id on next boot + write_file(ETC_MACHINE_ID, "uninitialized\n", mode=0o444) + else: + # Non-systemd like FreeBSD regen machine-id when file is absent + del_file(ETC_MACHINE_ID) if exit_code == 0 and args.reboot: cmd = ["shutdown", "-r", "now"] try: |