azure: /run/cloud-init/cloud-id broken symlink across reboot.

Bug #1998998 reported by Chad Smith
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
cloud-init
Fix Released
High
Unassigned

Bug Description

cloud-init 22.4.2

On Azure, datasource details change across reboot cloud-init incorrectly removes /run/cloud-init/cloud-id-azure symlink.

2022-12-07 04:14:17,920 - util.py[DEBUG]: Creating symbolic link from '/run/cloud-init/cloud-id' => '/run/cloud-init/cloud-id-azure'
2022-12-07 04:14:17,920 - util.py[DEBUG]: Attempting to remove /run/cloud-init/cloud-id-azure

# FRESH LAUNCH
ubuntu@test-focal:~$ ls -l /run/cloud-init/cloud-id
lrwxrwxrwx 1 root root 30 Dec 7 04:10 /run/cloud-init/cloud-id -> /run/cloud-init/cloud-id-azure
ubuntu@test-focal:~$ grep cloud-id /var/log/cloud-init.log
2022-12-07 04:10:34,734 - util.py[DEBUG]: Writing to /run/cloud-init/cloud-id-azure - wb: [644] 6 bytes
2022-12-07 04:10:34,735 - util.py[DEBUG]: Creating symbolic link from '/run/cloud-init/cloud-id' => '/run/cloud-init/cloud-id-azure'

# ACROSS REBOOT

ubuntu@test-focal:~$ ls -l /run/cloud-init/cloud-id
lrwxrwxrwx 1 root root 30 Dec 7 04:14 /run/cloud-init/cloud-id -> /run/cloud-init/cloud-id-azure

2022-12-07 04:14:17,920 - util.py[DEBUG]: Writing to /run/cloud-init/cloud-id-azure - wb: [644] 6 bytes
2022-12-07 04:14:17,920 - util.py[DEBUG]: Creating symbolic link from '/run/cloud-init/cloud-id' => '/run/cloud-init/cloud-id-azure'
2022-12-07 04:14:17,920 - util.py[DEBUG]: Attempting to remove /run/cloud-init/cloud-id-azure

It looks like the symlink cleanup logic introduced in https://github.com/canonical/cloud-init/commit/217ef6ba6c52788f4363b998b6da08863fea5cd9 (which was released in cloud-init v. 21.4) doesn't do a proper comparison of previous-datasource to current datasource

Debug logs added to https://github.com/canonical/cloud-init/blob/main/cloudinit/sources/__init__.py#L443 shows that we are comparing the incorrect source file /root/cloud-init/cloud-id versus actually comparing the cloud-specific symlink name /run/cloud-init/cloud-id-<cloud>, resulting in a removal of the cloud-specific link name across reboot.

2022-12-07 04:25:58,162 - __init__.py[WARNING]: previous_cloud_id_file /run/cloud-init/cloud-id-azure != cloud_id_file /run/cloud-init/cloud-id

Likely all we need is a correction in logic like the following:
diff --git a/cloudinit/sources/__init__.py b/cloudinit/sources/__init__.py
index 85e094ac5..3fb08ead2 100644
--- a/cloudinit/sources/__init__.py
+++ b/cloudinit/sources/__init__.py
@@ -439,8 +439,9 @@ class DataSource(CloudInitPickleMixin, metaclass=abc.ABCMeta):
             prev_cloud_id_file = os.path.realpath(cloud_id_file)
         else:
             prev_cloud_id_file = cloud_id_file
- util.sym_link(f"{cloud_id_file}-{cloud_id}", cloud_id_file, force=True)
- if prev_cloud_id_file != cloud_id_file:
+ new_cloud_id_file = f"{cloud_id_file}-{cloud_id}"
+ util.sym_link(new_cloud_id_file, cloud_id_file, force=True)
+ if prev_cloud_id_file != new_cloud_id_file:
             util.del_file(prev_cloud_id_file)
         write_json(json_sensitive_file, processed_data, mode=0o600)
         json_file = self.paths.get_runpath("instance_data")

Tags: bitesize
Chad Smith (chad.smith)
Changed in cloud-init:
status: New → Triaged
importance: Undecided → High
tags: added: bitesize
Revision history for this message
Alberto Contreras (aciba) wrote : Fixed in cloud-init version 23.1.

This bug is believed to be fixed in cloud-init in version 23.1. If this is still a problem for you, please make a comment and set the state back to New

Thank you.

Changed in cloud-init:
status: Triaged → Fix Released
Revision history for this message
James Falcon (falcojr) wrote :
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.