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:
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"}}
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).
Patch proposed on the upstream list: https://lists.nongnu.org/archive/html/qemu-devel/2020-06/msg04457.html
(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).
(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.
(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"}}
v2: https://lists.nongnu.org/archive/html/qemu-devel/2020-06/msg06913.html v3: https://lists.nongnu.org/archive/html/qemu-devel/2020-06/msg06933.html
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
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
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.
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
(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.
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.
*** Bug 1904109 has been marked as a duplicate of this bug. ***