canonical / canonical/cloud-init

In DataSourceCloudStack the primary_nic is not Always Assigned a Value but it may Always be Read

Open
#6,838 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
3.8k
Forks
1.1k
Avg merge
2d 23h
Merged PRs (30d)
18

Description

# Bug report
When using a NetworkManager backend on Ubuntu in CloudStack a strange situation can arise where there is no primary_nic and we try to obtain a DHCP lease. Most likely this is because Ubuntu's service ordering currently doesn't block on NetworkManager-wait-online.service and so the interfaces have not yet been brought up. Attempting to obtain a DHCP lease on the undefined nic essentially becomes a no-op and works fine. However, the error below is thrown when attempting to log the situation. Something in the boot process has probably gone wrong for the nic to be unassigned but given that the traceback is just occuring on the log statement rather than anything else it may be clearer to set a default value of None for primary_nic so that the logs will show "Unable to obtain a DHCP lease on None" rather than throwing an error.

```
2026-03-31 17:50:51,633 - log_util.py[DEBUG]: Getting data from failed
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/cloudinit/net/dhcp.py", line 864, in get_newest_lease
subp.subp(
File "/usr/lib/python3/dist-packages/cloudinit/subp.py", line 291, in subp
raise ProcessExecutionError(
cloudinit.subp.ProcessExecutionError: Unexpected error while running command.
Command: ['dhcpcd', '--dumplease', '--ipv4only', 'ens3']
Exit code: 1
Reason: -
Stdout:
Stderr: dhcpcd is not running

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceCloudStack.py", line 214, in _get_data
vr_addr = get_vr_address(self.distro)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceCloudStack.py", line 342, in get_vr_address
latest_lease = distro.dhcp_client.get_newest_lease(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/cloudinit/net/dhcp.py", line 882, in get_newest_lease
raise NoDHCPLeaseError from error
cloudinit.net.dhcp.NoDHCPLeaseError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/cloudinit/sources/__init__.py", line 1091, in find_source
if s.update_metadata_if_supported(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/cloudinit/sources/__init__.py", line 959, in update_metadata_if_supported
result = self.get_data()
^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/cloudinit/performance.py", line 103, in decorator
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/cloudinit/sources/__init__.py", line 505, in get_data
return_value = self._check_and_get_data()
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/cloudinit/sources/__init__.py", line 436, in _check_and_get_data
return self._get_data()
^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceCloudStack.py", line 229, in _get_data
LOG.warning("Unable to obtain a DHCP lease on %s", primary_nic)
^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'primary_nic' where it is not associated with a value
```

## Steps to reproduce the problem
Boot an Ubuntu Noble VM that uses NetworkManager in a CloudStack environment

## Environment details
- Cloud-init version: `25.3-0ubuntu1~24.04` and also the daily PPA: `100.0.1-daily-202603021632-a2209a9e1~ubuntu24.04.1`
- Operating System Distribution: Ubuntu Noble
- Cloud provider, platform or installer type: CloudStack

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.