RHEL Engineering is moving the tracking of its product development work on RHEL 6 through RHEL 9 to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "RHEL project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs in the statuses "NEW", "ASSIGNED", and "POST" are being migrated throughout September 2023. Bugs of Red Hat partners with an assigned Engineering Partner Manager (EPM) are migrated in late September as per pre-agreed dates. Bugs against components "kernel", "kernel-rt", and "kpatch" are only migrated if still in "NEW" or "ASSIGNED". If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "RHEL project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/RHEL-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.
Bug 2182407 - cloud-init strips new line from "/etc/hostname" when processing "/var/lib/cloud/data/previous-hostname"
Summary: cloud-init strips new line from "/etc/hostname" when processing "/var/lib/clo...
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Red Hat Enterprise Linux 8
Classification: Red Hat
Component: cloud-init
Version: 8.7
Hardware: Unspecified
OS: Unspecified
unspecified
medium
Target Milestone: rc
: ---
Assignee: Ani Sinha
QA Contact: xiachen
URL:
Whiteboard:
Depends On:
Blocks: 2184608
TreeView+ depends on / blocked
 
Reported: 2023-03-28 14:58 UTC by Marian Jankular
Modified: 2024-11-20 07:49 UTC (History)
11 users (show)

Fixed In Version: cloud-init-23.1.1-3.el8
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
: 2184608 (view as bug list)
Environment:
Last Closed: 2023-11-14 15:31:09 UTC
Type: Bug
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Gitlab redhat/rhel/src cloud-init merge_requests 97 0 None None None 2023-04-04 14:59:13 UTC
Red Hat Issue Tracker RHELPLAN-153288 0 None None None 2023-03-28 15:00:10 UTC
Red Hat Product Errata RHSA-2023:6943 0 None None None 2023-11-14 15:31:57 UTC

Description Marian Jankular 2023-03-28 14:58:57 UTC
Description of problem:
cloud-init strips new line from "/etc/hostname" when processing "/var/lib/cloud/data/previous-hostname"

Version-Release number of selected component (if applicable):
redhat-release-8.7-0.3.el8.x86_64
cloud-init-22.1-5.el8.noarch


How reproducible:
everytime

Steps to Reproduce:
1, enable cloud-init for rhel8

Actual results:
[root@mj-rhel87 ~]# cat /etc/hostname
mj-rhel87
[root@mj-rhel87 ~]# cat /var/lib/cloud/data/previous-hostname 
mj-rhel87[root@mj-rhel87 ~]# 


2023-03-28 14:47:45,733 - util.py[DEBUG]: Reading from /var/lib/cloud/data/previous-hostname (quiet=False)
2023-03-28 14:47:45,734 - util.py[DEBUG]: Read 9 bytes from /var/lib/cloud/data/previous-hostname
2023-03-28 14:47:45,734 - subp.py[DEBUG]: Running command ['hostname'] with allowed return codes [0] (shell=False, capture=True)
2023-03-28 14:47:45,738 - __init__.py[INFO]: /var/lib/cloud/data/previous-hostname differs from /etc/hostname, assuming user maintained hostname.


Expected results:
file /var/lib/cloud/data/previous-hostname will be terminated with new line as well



Additional info:
same behaviour is observed for rhel7 and rhel9 as well

Comment 1 xiachen 2023-03-31 03:06:29 UTC
Although the problem doesn't have a serious influence, it is there. Set QA_ACK+.

I added some test codes to /usr/lib/python3.9/site-packages/cloudinit/distros/__init__.py

        if sys_hostname and prev_hostname and sys_hostname != prev_hostname:
            LOG.info(sys_hostname)   #test code
            LOG.info(prev_hostname)  #test code
            LOG.info(
                "%s differs from %s, assuming user maintained hostname.",
                prev_hostname_fn,
                sys_fn,
            )
            return

and then reboot the instance, we can see that LOG.info(sys_hostname) output with newline, but LOG.info(prev_hostname) does not contain newline.

2023-03-30 08:55:29,843 - subp.py[DEBUG]: Running command ['hostname'] with allowed return codes [0] (shell=False, capture=True)
2023-03-30 08:55:29,845 - __init__.py[INFO]: xiachen-test-vm

2023-03-30 08:55:29,845 - __init__.py[INFO]: xiachen-test-vm
2023-03-30 08:55:29,845 - __init__.py[INFO]: /var/lib/cloud/data/previous-hostname differs from /etc/hostname, assuming user maintained hostname.
------------------------------------------------------

And I tested ubuntu, it doesn't have this problem. 
-------------
ubuntu@xiachen-test-ubuntu:~$ cat /etc/hostname
xiachen-test-ubuntu
ubuntu@xiachen-test-ubuntu:~$ cat /var/lib/cloud/data/previous-hostname
xiachen-test-ubuntu
ubuntu@xiachen-test-ubuntu:~$ 
-------------
Comparing to other distros, like ubuntu or debian, the differences are functions of read/write_hostname in /usr/lib/python3.9/site-packages/cloudinit/distros/rhel.py,
when read_hostname using command 'hostname' which contains newline, but it doesn't write newline to /var/lib/cloud/data/previous-hostname.

There is a Parser 'cloudinit.distros.parsers.hostname' that deals with how to work with /etc/hostname format, maybe we could also import and use it.

Comment 6 Ani Sinha 2023-04-03 11:06:11 UTC
Upstream ticket: https://bugs.launchpad.net/cloud-init/+bug/2015043

Comment 7 Ani Sinha 2023-04-03 14:33:20 UTC
reply to Ani Sinha from comment #5)
> (In reply to Marian Jankular from comment #4)
> > Created attachment 1955398 [details]
> > cloud-init log
> > 
> > not sure if the patch fixes the issue as i have installed new rpm on my
> > reproducer (rhel8.7)
> > 
> > 1, initial hostname - mj-rhel87
> > 2, reboot
> > 3, cloud init reported that hostname and previous hostname differs
> > ~~~
> > [root@mj-rhel87 ~]# cat /var/lib/cloud/data/previous-hostname 
> > mj-rhel87[root@mj-rhel87 ~]# hostnamectl set-hostname mj-rhel87-new
> > ~~~
> > 
> > 4, changed hostname to mj-rhel87-new
> > 5, reboot
> > 6, cloud-init reported that hostname and previous hostname differs
> > ~~~
> > [root@mj-rhel87-new ~]# cat /var/lib/cloud/data/previous-hostname 
> > mj-rhel87[root@mj-rhel87hostnamectl set-hostname mj-rhel87-new-new
> > ~~~
> > 
> > 7, reboot
> > 8, cloud-init reported that hostname and previous hostname differs
> > ~~~
> > [root@mj-rhel87-new-new ~]# cat /var/lib/cloud/data/previous-hostname 
> > mj-rhel87[root@mj-rhel87-new-new ~]# rpm -qa | grep cloud-init
> > cloud-init-22.1-6.el8.2.anisinha202303311339.noarch
> > ~~~
> > 
> > i would say the /var/lib/cloud/data/previous-hostname should change in my
> > case right?
> 
> I had to delete previous-hostname for it to get regenerated. In your case,
> it seems cloud-init is not updating the file at all, so you don't know if it
> works or not. Delete "previous-hostname" and then install the updated rpm
> and check.

I see this code in update_hostname() and this explains it:

       # If something else has changed the hostname after we set it                                                                                                             
        # initially, we should not overwrite those changes (we should                                                                                                            
        # only be setting the hostname once per instance)                                                                                                                        
        if sys_hostname and prev_hostname and sys_hostname != prev_hostname:                                                                                                     
            LOG.info(                                                                                                                                                            
                "%s differs from %s, assuming user maintained hostname.",                                                                                                        
                prev_hostname_fn,                                                                                                                                                
                sys_fn,                                                                                                                                                          
            )                                                                                                                                                                    
            return  

This means subsequent changes to hostname would not update previous-hostname :(  However, if you use cloud-init config to change the hostname, I think the file will be updated:

https://cloudinit.readthedocs.io/en/latest/reference/modules.html#set-hostname

Comment 27 errata-xmlrpc 2023-11-14 15:31:09 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (Moderate: cloud-init security, bug fix, and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHSA-2023:6943


Note You need to log in before you can comment on or make changes to this bug.