CentOS 7: update-hostname always overwrites hostname
| Affects | Status | Importance | Assigned to | Milestone | |
|---|---|---|---|---|---|
| | cloud-init |
Medium
|
Unassigned | ||
Bug Description
On CentOS 7 (systemd based), with cloud-init 0.7.5, cc_update_hostname will *always* overwrite the hostname, even if it shouldn't.
This is pretty easy to reproduce:
1) Add a 'hostname: metadata.
2) Run 'hostnamectl set-hostname test.example.com'
3) Reboot the server
4) Check the hostname, it'll still be 'metadata.
5) Repeat 2-4, hostname will still be set
Looking at the code, this is because rhel.py doesn't populate the previous-hostname file correctly:
def _write_
if self._dist_
else:
}
So, Distros:
The fix here seems pretty simple, we just need to update previous-hostname as well when running hostnamectl. I attached a patch that does this, though I don't know the best way to implement this.. my patch fails if cloud-init's data path is changed
Related branches
- Server Team CI bot: Needs Fixing (continuous-integration) on 2017-03-06
- Scott Moser: Needs Information on 2016-10-20
- Joshua Harlow: Approve on 2016-10-07
-
Diff: 17 lines (+3/-3)1 file modifiedcloudinit/distros/rhel.py (+3/-3)
| Brian Rak (brak) wrote : | #1 |
| Lennert den Teuling (lennert) wrote : | #2 |
| Brian Rak (brak) wrote : | #3 |
The upstream version of cloud-init (from centos-extras) contains this patch:
def _dist_uses_
...
return ((dist.
+ or (dist.startswit
or (dist.startswit
The EPEL version does not contain this, so cloud-init is not aware that CentOS 7 uses systemd. This works around the issue because it falls back to the old method of setting the hostname (which isn't broken).
So, the EPEL version works, but only accidentally.
| Lars Kellogg-Stedman (larsks) wrote : | #4 |
There is actually a bigger problem here.
The update_hostname method gets the previous hostname (prev_hostname) by reading a value of prev_hostname_
A fundamental problem with the code -- at least in 0.7.6 -- is that the base distros.Distro class doesn't provide a high enough level of abstraction. It assumes a file-based configuration for managing hostnames, and overloads functions like _read_hostname to perform multiple functions in ways that are pretty much guaranteed to fail.
I'm working on a band-aid that will resolve this problem, but the Distro class really needs to provide a high-level API that is well separated from the individual distribution drivers. In particular, the base class should probably *never* be reading or writing files outside of /var/lib/cloud.
| Brian Rak (brak) wrote : | #5 |
Yea, we had to patch _read_hostname and _write_hostname to explicitly look for the filename ending in /previous-hostname, and write that to an actual file (rather then relying on hostnamectl)
Seems to be working pretty well so far.
| Changed in cloud-init: | |
| status: | New → Confirmed |
| importance: | Undecided → Medium |


I can confirm this bug. If you place the previous-hostname file manually it stops changing the hostname on every reboot. As a workaround i installed cloud-init- 0.7.5-6. el7 (epel).