summaryrefslogtreecommitdiff
path: root/cloudinit/cmd
diff options
context:
space:
mode:
authorChad Smith <chad.smith@canonical.com>2022-07-21 17:19:27 -0600
committerGitHub <noreply@github.com>2022-07-21 17:19:27 -0600
commit8f569d45111dce368f92883e35bfbb40bbb86091 (patch)
treedf2b56f653fa66b3d59260c2a1a1aa824640c1d9 /cloudinit/cmd
parent98ed2b9d1a8ccb354e5ad404314d68447aaeae88 (diff)
downloadcloud-init-git-8f569d45111dce368f92883e35bfbb40bbb86091.tar.gz
clean: add param to remove /etc/machine-id for golden image creation
Typically when a cloud-image boots /etc/machine-id should be auto-generated if /etc/machine-id does not exist. Some cloud images appropriately generate /etc/machine-id if the image is recognized as a clone, in most other cases it is a best practice to remove /etc/machine-id and let DBUS or systemd do the work of generating a new UUID on next boot so the cloned VMs are represented uniquely to any monitoring/management software.
Diffstat (limited to 'cloudinit/cmd')
-rwxr-xr-xcloudinit/cmd/clean.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/cloudinit/cmd/clean.py b/cloudinit/cmd/clean.py
index 0d7ca15a..65d3eece 100755
--- a/cloudinit/cmd/clean.py
+++ b/cloudinit/cmd/clean.py
@@ -22,6 +22,8 @@ from cloudinit.util import (
is_link,
)
+ETC_MACHINE_ID = "/etc/machine-id"
+
def get_parser(parser=None):
"""Build or extend an arg parser for clean utility.
@@ -49,6 +51,15 @@ def get_parser(parser=None):
help="Remove cloud-init logs.",
)
parser.add_argument(
+ "--machine-id",
+ action="store_true",
+ default=False,
+ help=(
+ "Remove /etc/machine-id for golden image creation."
+ " Next boot generates a new machine-id."
+ ),
+ )
+ parser.add_argument(
"-r",
"--reboot",
action="store_true",
@@ -108,6 +119,8 @@ def remove_artifacts(remove_logs, remove_seed=False):
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 exit_code == 0 and args.reboot:
cmd = ["shutdown", "-r", "now"]
try: