Skip to contentSkip to conversation

Add guest-get-hostname to retrieve the guests current hostname

PATCH In review Guido Günther 5 participants 8 messages Aug 21, 2012 – Aug 24, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add guest-get-hostname to retrieve the guests current hostname

This allows to retrieve the guest's hostname via gethostname(2).

This can be useful to identify a VM e.g. one without network.

---
We have an API in libvirt for that (virDomainGetHostname). Cheers
-- Guido

 qapi-schema-guest.json |   12 ++++++++++++
 qga/commands-posix.c   |   12 ++++++++++++
 qga/commands-win32.c   |    6 ++++++
 3 files changed, 30 insertions(+)
qapi-schema-guest.json +12 -0
index d955cf1..8c7a4a5 100644
@@ -515,3 +515,15 @@
 ##
 { 'command': 'guest-network-get-interfaces',
   'returns': ['GuestNetworkInterface'] }
+
+##
+# @guest-get-hostname:
+#
+# Get the guest's hostname
+#
+# Returns: The guest's hostname
+#
+# Since: 1.2
+##
+{ 'command': 'guest-get-hostname',
+  'returns': 'str' }
qga/commands-posix.c +12 -0
index ce90421..9223f18 100644
@@ -15,6 +15,7 @@
 #include <sys/types.h>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
+#include <unistd.h>
 #include "qga/guest-agent-core.h"
 #include "qga-qmp-commands.h"
 #include "qerror.h"
@@ -993,6 +994,17 @@ void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
 }
 #endif
 
+char *qmp_guest_get_hostname(Error **err)
+{
+    char hostname[HOST_NAME_MAX];
+
+    if (gethostname(hostname, HOST_NAME_MAX)) {
+        error_set(err, QERR_QGA_COMMAND_FAILED, strerror(errno));
+        return NULL;
+    }
+    return g_strdup(hostname);
+}
+
 /* register init/cleanup routines for stateful command groups */
 void ga_command_state_init(GAState *s, GACommandState *cs)
 {
qga/commands-win32.c +6 -0
index 54bc546..55e8162 100644
@@ -280,6 +280,12 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **err)
     return NULL;
 }
 
+char *qmp_guest_get_hostname(Error **err)
+{
+    error_set(err, QERR_UNSUPPORTED);
+    return NULL;
+}
+
 /* register init/cleanup routines for stateful command groups */
 void ga_command_state_init(GAState *s, GACommandState *cs)
 {
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 70 lines · Guido Günther

This allows to retrieve the guest's hostname via gethostname(2).

This can be useful to identify a VM e.g. one without network.

---
We have an API in libvirt for that (virDomainGetHostname). Cheers
-- Guido

 qapi-schema-guest.json |   12 ++++++++++++
 qga/commands-posix.c   |   12 ++++++++++++
 qga/commands-win32.c   |    6 ++++++
 3 files changed, 30 insertions(+)
qapi-schema-guest.json +12 -0
index d955cf1..8c7a4a5 100644
@@ -515,3 +515,15 @@
 ##
 { 'command': 'guest-network-get-interfaces',
   'returns': ['GuestNetworkInterface'] }
+
+##
+# @guest-get-hostname:
+#
+# Get the guest's hostname
+#
+# Returns: The guest's hostname
+#
+# Since: 1.2
+##
+{ 'command': 'guest-get-hostname',
+  'returns': 'str' }
qga/commands-posix.c +12 -0
index ce90421..9223f18 100644
@@ -15,6 +15,7 @@
 #include <sys/types.h>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
+#include <unistd.h>
 #include "qga/guest-agent-core.h"
 #include "qga-qmp-commands.h"
 #include "qerror.h"
@@ -993,6 +994,17 @@ void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
 }
 #endif
 
+char *qmp_guest_get_hostname(Error **err)
+{
+    char hostname[HOST_NAME_MAX];
+
+    if (gethostname(hostname, HOST_NAME_MAX)) {
+        error_set(err, QERR_QGA_COMMAND_FAILED, strerror(errno));
+        return NULL;
+    }
+    return g_strdup(hostname);
+}
+
 /* register init/cleanup routines for stateful command groups */
 void ga_command_state_init(GAState *s, GACommandState *cs)
 {
qga/commands-win32.c +0 -0
index 54bc546..55e8162 100644

qga/commands-win32.c GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **err) · line 287

Quoted 8 lines
qga/commands-win32.c +5 -0
@@ -280,6 +280,12 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **err)
     return NULL;
 }
 
+char *qmp_guest_get_hostname(Error **err)
+{
+    error_set(err, QERR_UNSUPPORTED);
+    return NULL;
+}

Why no impl ? Winsock has the gethostname() API too

$ grep gethostname /usr/i686-w64-mingw32/sys-root/mingw/include/*.h
/usr/i686-w64-mingw32/sys-root/mingw/include/winsock2.h:  WINSOCK_API_LINKAGE int WSAAPI gethostname(char *name,int namelen);

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#5 lore raw
In reply to Guido Günther
Quoted 9 lines · Guido Günther

This allows to retrieve the guest's hostname via gethostname(2).

This can be useful to identify a VM e.g. one without network.

---
We have an API in libvirt for that (virDomainGetHostname). Cheers
-- Guido

Still, this feels like a new feature and not a bug fix, so

qapi-schema-guest.json line 526

Quoted 9 lines
qapi-schema-guest.json +9 -0
+
+##
+# @guest-get-hostname:
+#
+# Get the guest's hostname
+#
+# Returns: The guest's hostname
+#
+# Since: 1.2

...this should probably be 1.3.

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
In reply to Daniel P. Berrange
Quoted 1 line · Daniel P. Berrange

On Tue, Aug 21, 2012 at 01:57:54PM +0200, Guido Günther wrote:

[..snip..]

Quoted 5 lines

Why no impl ? Winsock has the gethostname() API too

$ grep gethostname /usr/i686-w64-mingw32/sys-root/mingw/include/*.h
/usr/i686-w64-mingw32/sys-root/mingw/include/winsock2.h:  WINSOCK_API_LINKAGE int WSAAPI gethostname(char *name,int namelen);

This was mostly due to the lack of a test system. Are there any pointers on how to cross compile qemu-qa for Windows?
Cheers,
-- Guido

Quoted 9 lines

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#3 lore raw
Quoted 11 lines · Guido Günther
Quoted 1 line · Daniel P. Berrange

On Tue, Aug 21, 2012 at 01:57:54PM +0200, Guido Günther wrote:

[..snip..]

Quoted 5 lines

Why no impl ? Winsock has the gethostname() API too

$ grep gethostname /usr/i686-w64-mingw32/sys-root/mingw/include/*.h
/usr/i686-w64-mingw32/sys-root/mingw/include/winsock2.h:  WINSOCK_API_LINKAGE int WSAAPI gethostname(char *name,int namelen);

This was mostly due to the lack of a test system. Are there any pointers on how to cross compile qemu-qa for Windows?

Assuming you have the Mingw64 toolchain installed, then compilation is just a case of passing the --cross-prefix arg to configure. eg on Fedora 17 I would do:

  ./configure --target-list=x86_64-softmmu --cross-prefix=i686-w64-mingw32-

which causes it to use i686-w64-mingw32-gcc as the compiler

Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#6 lore raw
In reply to Guido Günther
Quoted 22 lines · On Tue, 21 Aug 2012 13:57:54 +0200 Guido Günther <[email protected]> wrote:

This allows to retrieve the guest's hostname via gethostname(2).

This can be useful to identify a VM e.g. one without network.

---
We have an API in libvirt for that (virDomainGetHostname). Cheers
-- Guido

 qapi-schema-guest.json |   12 ++++++++++++
 qga/commands-posix.c   |   12 ++++++++++++
 qga/commands-win32.c   |    6 ++++++
 3 files changed, 30 insertions(+)
qapi-schema-guest.json +0 -0
index d955cf1..8c7a4a5 100644
@@ -515,3 +515,15 @@
 ##
 { 'command': 'guest-network-get-interfaces',

qapi-schema-guest.json line 526

Quoted 10 lines
qapi-schema-guest.json +9 -0
   'returns': ['GuestNetworkInterface'] }
+
+##
+# @guest-get-hostname:
+#
+# Get the guest's hostname
+#
+# Returns: The guest's hostname
+#
+# Since: 1.2

Won't make it for 1.2 because we're in hard-freeze.

Quoted 16 lines
qapi-schema-guest.json +3 -0
+##
+{ 'command': 'guest-get-hostname',
+  'returns': 'str' }
qga/commands-posix.c +1 -0
index ce90421..9223f18 100644
@@ -15,6 +15,7 @@
 #include <sys/types.h>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
+#include <unistd.h>
 #include "qga/guest-agent-core.h"
 #include "qga-qmp-commands.h"
 #include "qerror.h"

qga/commands-posix.c void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err) · line 1002

Quoted 9 lines
qga/commands-posix.c +6 -0
@@ -993,6 +994,17 @@ void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
 }
 #endif
 
+char *qmp_guest_get_hostname(Error **err)
+{
+    char hostname[HOST_NAME_MAX];
+
+    if (gethostname(hostname, HOST_NAME_MAX)) {
+        error_set(err, QERR_QGA_COMMAND_FAILED, strerror(errno));

You shouldn't use that macro, you can do something like this instead:

error_set(err, "can't get hostname: %s", strerror(errno));

Michael, do you think that's fine for new error messages or do you think it's worth it to add a "guest-agent: " prefix to all guest agent error messages?

Quoted 25 lines
qga/commands-posix.c +5 -0
+        return NULL;
+    }
+    return g_strdup(hostname);
+}
+
 /* register init/cleanup routines for stateful command groups */
 void ga_command_state_init(GAState *s, GACommandState *cs)
 {
qga/commands-win32.c +6 -0
index 54bc546..55e8162 100644
@@ -280,6 +280,12 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **err)
     return NULL;
 }
 
+char *qmp_guest_get_hostname(Error **err)
+{
+    error_set(err, QERR_UNSUPPORTED);
+    return NULL;
+}
+
 /* register init/cleanup routines for stateful command groups */
 void ga_command_state_init(GAState *s, GACommandState *cs)
 {
#7 lore raw
Quoted 66 lines · Luiz Capitulino
Quoted 22 lines · On Tue, 21 Aug 2012 13:57:54 +0200 Guido Günther <[email protected]> wrote:

This allows to retrieve the guest's hostname via gethostname(2).

This can be useful to identify a VM e.g. one without network.

---
We have an API in libvirt for that (virDomainGetHostname). Cheers
-- Guido

 qapi-schema-guest.json |   12 ++++++++++++
 qga/commands-posix.c   |   12 ++++++++++++
 qga/commands-win32.c   |    6 ++++++
 3 files changed, 30 insertions(+)
qapi-schema-guest.json +0 -0
index d955cf1..8c7a4a5 100644
@@ -515,3 +515,15 @@
 ##
 { 'command': 'guest-network-get-interfaces',

qapi-schema-guest.json line 526

Quoted 10 lines
qapi-schema-guest.json +9 -0
   'returns': ['GuestNetworkInterface'] }
+
+##
+# @guest-get-hostname:
+#
+# Get the guest's hostname
+#
+# Returns: The guest's hostname
+#
+# Since: 1.2

Won't make it for 1.2 because we're in hard-freeze.

Quoted 16 lines
qapi-schema-guest.json +3 -0
+##
+{ 'command': 'guest-get-hostname',
+  'returns': 'str' }
qga/commands-posix.c +1 -0
index ce90421..9223f18 100644
@@ -15,6 +15,7 @@
 #include <sys/types.h>
 #include <sys/ioctl.h>
 #include <sys/wait.h>
+#include <unistd.h>
 #include "qga/guest-agent-core.h"
 #include "qga-qmp-commands.h"
 #include "qerror.h"

qga/commands-posix.c void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err) · line 1002

Quoted 9 lines
qga/commands-posix.c +6 -0
@@ -993,6 +994,17 @@ void qmp_guest_fstrim(bool has_minimum, int64_t minimum, Error **err)
 }
 #endif
 
+char *qmp_guest_get_hostname(Error **err)
+{
+    char hostname[HOST_NAME_MAX];
+
+    if (gethostname(hostname, HOST_NAME_MAX)) {
+        error_set(err, QERR_QGA_COMMAND_FAILED, strerror(errno));

You shouldn't use that macro, you can do something like this instead:

Quoted 5 lines

error_set(err, "can't get hostname: %s", strerror(errno));

Michael, do you think that's fine for new error messages or do you think it's worth it to add a "guest-agent: " prefix to all guest agent error messages?

I think that's fine. libvirt or QMP (if we integrate the qemu-ga commands with QMP) can tack on a prefix for cases where we need to distinguish where in the stack the error occurred, but for direct communication with qemu-ga it's unambiguous, so might as well save the keystrokes.

Quoted 27 lines
Quoted 25 lines
qga/commands-posix.c +5 -0
+        return NULL;
+    }
+    return g_strdup(hostname);
+}
+
 /* register init/cleanup routines for stateful command groups */
 void ga_command_state_init(GAState *s, GACommandState *cs)
 {
qga/commands-win32.c +6 -0
index 54bc546..55e8162 100644
@@ -280,6 +280,12 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **err)
     return NULL;
 }
 
+char *qmp_guest_get_hostname(Error **err)
+{
+    error_set(err, QERR_UNSUPPORTED);
+    return NULL;
+}
+
 /* register init/cleanup routines for stateful command groups */
 void ga_command_state_init(GAState *s, GACommandState *cs)
 {
#4 lore raw
In reply to Daniel P. Berrange
Quoted 13 lines · Daniel P. Berrange
Quoted 11 lines · Guido Günther
Quoted 1 line · Daniel P. Berrange

On Tue, Aug 21, 2012 at 01:57:54PM +0200, Guido Günther wrote:

[..snip..]

Quoted 5 lines

Why no impl ? Winsock has the gethostname() API too

$ grep gethostname /usr/i686-w64-mingw32/sys-root/mingw/include/*.h
/usr/i686-w64-mingw32/sys-root/mingw/include/winsock2.h:  WINSOCK_API_LINKAGE int WSAAPI gethostname(char *name,int namelen);

This was mostly due to the lack of a test system. Are there any pointers on how to cross compile qemu-qa for Windows?

Quoted 7 lines

Assuming you have the Mingw64 toolchain installed, then compilation is just a case of passing the --cross-prefix arg to configure. eg on Fedora 17 I would do:

  ./configure --target-list=x86_64-softmmu --cross-prefix=i686-w64-mingw32-

which causes it to use i686-w64-mingw32-gcc as the compiler

That helped! I had to additionally install

mingw32-pkg-config mingw32-zlib mingw32-glib2

to cross build qemu-ga. Howver running this on a win7-pro evaluation version I get:

.\qemu-ga

<timestamp>: critical: error opening path <timestamp>: critical: error opening channel <timestamp>: critical: failed to create guest agent channel <timestamp>: critical: failed to initialize guest agent channel

qemu-ga is current git while the host is qemu-kvm 1.1.0. The virtio serial driver is installed in the guest but running the test program gives errors as well:

.\VIOSER-TEST
Running in non blocking mode.
Cannot find vioserial device \\?\.\{<uuid>}#vioserialport#... , error 5

Libvirt creates the guest agent channel via:

<channel type='unix'>

      <source mode='bind' path='/var/lib/libvirt/qemu/win7-32.agent'/>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <alias name='channel0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>

</channel>

While the same works well with linux guests. Any ideas what I'm missing on the Windows side?
Cheers,
-- Guido

Quoted 8 lines

Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|