qemu-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to get the hostname and IP address of a VM using qemu-guest-agen


From: Tomáš Golembiovský
Subject: Re: How to get the hostname and IP address of a VM using qemu-guest-agent
Date: Tue, 25 Jan 2022 21:32:42 +0100

Hi,

On Mon, Jan 24, 2022 at 12:06:05AM +0600, Ahmad Ismail wrote:
> I am running a VM using
> 
> qemu-system-x86_64 -accel kvm,thread=multi -cpu host -smp 1 -m 2048 \
> -drive file=ubuntu-server1.qcow2,if=virtio -vga virtio -display none
> -daemonize \
> -nic bridge,model=virtio-net-pci,mac=DE:AD:BE:EF:E0:00
> 
> To get the IP address I have to use:
> 
> ps -ef | grep qemu
> arp | grep -i DE:AD:BE:EF:38:C4
> ip neigh | grep -i DE:AD:BE:EF:38:C4
> 
> After, reading the wiki, I have installed and enabled qemu-guest-agent in
> the guest.
> 
> sudo apt install qemu-guest-agent
> sudo systemctl start qemu-guest-agent
> sudo systemctl enable qemu-guest-agent
> 
> I have run the VM using
> 
> qemu-system-x86_64 -accel kvm,thread=multi -cpu host -smp 1 -m 2048 \
> -drive file=ubuntu-server1.qcow2,if=virtio -vga virtio -display none
> -daemonize \
> -nic bridge,model=virtio-net-pci,mac=DE:AD:BE:EF:E0:00 \
> -chardev socket,path=/tmp/qga.sock,server,nowait,id=qga0 \
> -device virtio-serial \
> -device virtserialport,chardev=qga0,name=org.qemu.guest_agent.0
> 
> So, the socket is in /tmp/qga.sock.
> 
> Now I am not understanding how to use this socket to get the hostname and
> IP of the guest.

To talk to the agent you need to connect to the socket and send
messages. You can either use some command line tool (e.g. socat) or
write something more robust program (e.g. in C or in Python using the
socket module). The agent uses a comunication protocol encapsulated in
JSON format. Commands and arguments are described in [1].

To answer your specific problem you can run the following to get the IPs
as well as other network info:

    echo '{"execute":"guest-network-get-interfaces"}' | socat -t5 
unix:/tmp/qga.0 stdio

To get an "interactive console" for experiments you can try:

    socat unix:/tmp/qga.0 readline

Hope this helps,

    Tomas

[1] https://github.com/qemu/qemu/blob/master/qga/qapi-schema.json

-- 
Tomáš Golembiovský <tgolembi@redhat.com>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]