Bug 1845127 - Incorrect hostname returned by virsh guest-get-host-name command
Summary: Incorrect hostname returned by virsh guest-get-host-name command
Keywords:
Status: CLOSED CURRENTRELEASE
Alias: None
Product: Red Hat Enterprise Linux Advanced Virtualization
Classification: Red Hat
Component: qemu-kvm
Version: ---
Hardware: Unspecified
OS: Unspecified
medium
medium
Target Milestone: rc
: 8.3
Assignee: Michal Privoznik
QA Contact: dehanmeng
URL:
Whiteboard:
: 1904109 (view as bug list)
Depends On:
Blocks: 1840500 1891923 1903942 1910326
TreeView+ depends on / blocked
 
Reported: 2020-06-08 14:00 UTC by vsibirsk
Modified: 2021-01-19 14:56 UTC (History)
8 users (show)

Fixed In Version: v5.1.0
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
: 1910326 (view as bug list)
Environment:
Last Closed: 2021-01-08 17:00:12 UTC
Type: Bug
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)

Description vsibirsk 2020-06-08 14:00:51 UTC
Description of problem:
Incorrect hostname returned by virsh command

Version-Release number of selected component (if applicable):
fedora:
qemu-guest-agent.x86_64                  2:4.2.0-7.fc32

rhel:
qemu-guest-agent.x86_64  15:2.12.0-63.module+el8+2833+c7d6d092

How reproducible:
100%

Steps to Reproduce:
1.Get VM hostname via virsh qemu-agent-command <domain> '{"execute": "guest-get-host-name"}'
2.Get hostname directly from guest os
3.

Actual results:
hostname in libvirt:
virsh qemu-agent-command fedora-vm '{"execute": "guest-get-host-name"}'
{"return":{"host-name":"ibm-p8-kvm-03-guest-02.virt.pnr.lab.eng.rdu2.redhat.com"}}

actual hostname:
[fedora@fedora31 ~]$ hostname
fedora31

Expected results:
guest-get-host-name returns correct hostname

Additional info:

Comment 1 xiagao 2020-06-09 07:10:50 UTC
Couldn't reproduce with qemu-kvm-2.12.0-99.module+el8.2.0+6870+55b789b4.2 and qemu-guest-agent-2.12.0-63.module+el8+2833+c7d6d092.x86_64.rpm

{"execute": "guest-get-host-name"}
{"return": {"host-name": "bootp-73-75-79.lab.eng.pek2.redhat.com"}}
[root@bootp-73-75-79 ~]# hostname
[root@bootp-73-75-79 ~]# bootp-73-75-79.lab.eng.pek2.redhat.com



And install a fedora31 vm, still can't reproduce.
{"execute": "guest-get-host-name"}
{"return": {"host-name": "vm-74-248.lab.eng.pek2.redhat.com"}}

After change hostname in vm and reboot,then get the hostname via guest-agent.
{"execute": "guest-get-host-name"}
{"return": {"host-name": "testname"}}

Comment 2 Michal Privoznik 2020-06-09 12:55:44 UTC
The problem here is that guest agent uses g_get_host_name() which uses gethostname() and caches the output. Therefore, if the hostname changes at any point, this is not reflected by guest agent, until restarted (when it repeats the while process and caches the hostname again). This is terrible API to use, and I will post a patch to both qemu-ga and libvirt to use the good old gethostname() (yeah, found out libvirt uses the same glib API unfortunately).

Comment 3 Michal Privoznik 2020-06-16 08:55:49 UTC
Patch proposed on the upstream list:

https://lists.nongnu.org/archive/html/qemu-devel/2020-06/msg04457.html

Comment 4 xiagao 2020-06-16 09:01:45 UTC
(In reply to Michal Privoznik from comment #2)
> The problem here is that guest agent uses g_get_host_name() which uses
> gethostname() and caches the output. Therefore, if the hostname changes at
> any point, this is not reflected by guest agent, until restarted (when it
> repeats the while process and caches the hostname again).

Hi, I want to make it clear, you mean when user modify host name in guest but does't not reboot guest os,
then the new host name couldn't get by guest agent, right?

> This is terrible
> API to use, and I will post a patch to both qemu-ga and libvirt to use the
> good old gethostname() (yeah, found out libvirt uses the same glib API
> unfortunately).

Comment 5 Michal Privoznik 2020-06-16 09:54:07 UTC
(In reply to xiagao from comment #4)
> (In reply to Michal Privoznik from comment #2)
> > The problem here is that guest agent uses g_get_host_name() which uses
> > gethostname() and caches the output. Therefore, if the hostname changes at
> > any point, this is not reflected by guest agent, until restarted (when it
> > repeats the while process and caches the hostname again).
> 
> Hi, I want to make it clear, you mean when user modify host name in guest
> but does't not reboot guest os,
> then the new host name couldn't get by guest agent, right?

Either that, or if the new hostname is set during DHCP. The glib API that qemu-ga uses caches the hostname on the first query. Therefore if the hostname is queried before DHCP, or before user changes it, the qemu-ga will always report the old one. If the guest agent is restarted, then the new hostname is cached, but again - if it's changed any point later, it won't be reflected in the guest agent output.

Reboot of the guest does not play important role, it's only a way of restarting qemu-ga. That's all.

Comment 6 xiagao 2020-06-17 01:34:11 UTC
(In reply to Michal Privoznik from comment #5)
> (In reply to xiagao from comment #4)
> > (In reply to Michal Privoznik from comment #2)
> > > The problem here is that guest agent uses g_get_host_name() which uses
> > > gethostname() and caches the output. Therefore, if the hostname changes at
> > > any point, this is not reflected by guest agent, until restarted (when it
> > > repeats the while process and caches the hostname again).
> > 
> > Hi, I want to make it clear, you mean when user modify host name in guest
> > but does't not reboot guest os,
> > then the new host name couldn't get by guest agent, right?
> 
> Either that, or if the new hostname is set during DHCP. The glib API that
> qemu-ga uses caches the hostname on the first query. Therefore if the
> hostname is queried before DHCP, or before user changes it, the qemu-ga will
> always report the old one. If the guest agent is restarted, then the new
> hostname is cached, but again - if it's changed any point later, it won't be
> reflected in the guest agent output.
> 
> Reboot of the guest does not play important role, it's only a way of
> restarting qemu-ga. That's all.

Get it,thank you.

Can reproduce with the following steps.
1. Change host name in guest.
[root@testname ~]# hostnamectl set-hostname  newtestname
[root@testname ~]# hostname
newtestname

2. Get host name via qga from host.
{"execute": "guest-get-host-name"}
{"return": {"host-name": "testname"}}

3. Restart qga service in guest.
[root@testname ~]# service qemu-guest-agent restart
Redirecting to /bin/systemctl restart qemu-guest-agent.service

4. repeate step2.
{"execute": "guest-get-host-name"}
{"return": {"host-name": "newtestname"}}

Comment 8 John Ferlan 2020-07-06 18:59:04 UTC
Reassigned to Michal since he sent the patches upstream. Hopefully they make the qemu-5.1 release and thus we won't need a backport for RHEL AV 8.3.0

Comment 9 Michal Privoznik 2020-07-16 12:45:01 UTC
Patches were merged upstream:

0d3a8f32b1 qga: Use qemu_get_host_name() instead of g_get_host_name()
e47f4765af util: Introduce qemu_get_host_name()

v5.1.0-rc0~5^2

Comment 10 dehanmeng 2020-08-17 08:36:03 UTC
Reproduce with version:
qemu-guest-agent-2.12.0-63.module+el8+2833+c7d6d092.x86_64

Steps to Reproduce same as comment0

Actual result:

[root@dell-per440-06 Guest_boot_script]# nc -U /tmp/qga.sock
{"execute":"guest-get-host-name"}
{"return": {"host-name": "oldtestname"}}

Expected result:

{"execute":"guest-get-host-name"}
{"return": {"host-name": "newtestname"}}


Verified with version:
qemu-kvm-5.1.0-2.module+el8.3.0+7652+b30e6901

steps to verified same as comment0 

Actual result:

[root@dell-per440-06 Guest_boot_script]# nc -U /tmp/qga.sock
{"execute":"guest-get-host-name"}
{"return": {"host-name": "vm-74-124.lab.eng.pek2.redhat.com"}}
{"execute":"guest-get-host-name"}
{"return": {"host-name": "newtestname"}}

Same as expected result.

Comment 12 dehanmeng 2020-12-23 02:58:11 UTC
Hit this issue on guest rhel8.4.0  with pkg qemu-kvm-4.2.0-38.module+el8.4.0+9133+5346b06d. should this bz be cloned? 
thanks

Comment 13 Michal Privoznik 2021-01-04 10:09:13 UTC
(In reply to dehanmeng from comment #12)
> Hit this issue on guest rhel8.4.0  with pkg
> qemu-kvm-4.2.0-38.module+el8.4.0+9133+5346b06d. should this bz be cloned? 
> thanks

It already is: bug 1910326.

Comment 14 Jeff Nelson 2021-01-08 17:00:12 UTC
This BZ was not attached to an advisory and therefore was not closed when RHEL AV was shipped. Correcting this now by marking the BZ CLOSED CURRENTRELEASE.

Comment 15 Tomáš Golembiovský 2021-01-19 14:56:00 UTC
*** Bug 1904109 has been marked as a duplicate of this bug. ***


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