[systemd-devel] [PATCH v2 ] hostnamectl: correct IDs for remote hosts

Rico Sagner sagner at b1-systems.de
Sun Jun 1 11:53:36 PDT 2014


Hello

On 01.06.2014 08:01, Lennart Poettering wrote:
> On Sat, 31.05.14 18:21, Rico Sagner (sagner at b1-systems.de) wrote:
> 
> Heya!
> 
> I think that the two ids would probably be better exposed by PID 1
> istelf, instead of hostnamed. It's a bit difficult to come up with a
> rule which props should be exposed from hostnamed and which ones from
> PID1, but I think the rule should be something along the lines of "if
> it's more an automatically determined property than a user or vendor
> chosen one, and if PID 1 knows/caches it anyway then it belongs into PID
> 1"...
> 
Sounds plausible

> So, could you please move the machine/boot IDs into PID 1 instead of
> hostnamed, next to where the Virtualization/Architecture properties are
> defined?
> 
> Also, please use "ay" instead of "s" as type for the property (i.e. a
> byte-array of the 16 raw uuid bytes instead of an ascii-formatted
> string). This is how we encoded UUIDs so far as bus properties, see
> machined's machine uuid property (src/machine/machine-dbus.c) for example.
> 

I think that should fulfill the requested changes.
Let me know if there is something to improve.

 - Rico




If hostnamectl is used with the --host option it does not
show the correct machine and boot IDs of the remote host.
The IDs are read by hostnamectl locally instead of querying
dbus on the remote host.

This patch makes systemd offer the IDs via dbus
and hostnamectl retrieve it this way.
---
 src/core/dbus-manager.c    | 38 ++++++++++++++++++++++++++++++++++++++
 src/core/manager.c         |  8 ++++++++
 src/core/manager.h         |  4 ++++
 src/hostname/hostnamectl.c | 16 ++++++++--------
 4 files changed, 58 insertions(+), 8 deletions(-)

diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c
index 333c1d4..d28588a 100644
--- a/src/core/dbus-manager.c
+++ b/src/core/dbus-manager.c
@@ -317,6 +317,42 @@ static int property_get_system_state(
         return sd_bus_message_append(reply, "s", manager_state_to_string(manager_state(m)));
 }
 
+static int property_get_machineid(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        Manager *m = userdata;
+
+        assert(bus);
+        assert(reply);
+        assert(m);
+
+        return sd_bus_message_append_array(reply, 'y', &m->machineid, 16);
+}
+
+static int property_get_bootid(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        Manager *m = userdata;
+
+        assert(bus);
+        assert(reply);
+        assert(m);
+
+        return sd_bus_message_append_array(reply, 'y', &m->bootid, 16);
+}
+
 static int property_set_runtime_watchdog(
                 sd_bus *bus,
                 const char *path,
@@ -1670,6 +1706,8 @@ const sd_bus_vtable bus_manager_vtable[] = {
         SD_BUS_WRITABLE_PROPERTY("ShutdownWatchdogUSec", "t", bus_property_get_usec, bus_property_set_usec, offsetof(Manager, shutdown_watchdog), SD_BUS_VTABLE_PROPERTY_CONST),
         SD_BUS_PROPERTY("ControlGroup", "s", NULL, offsetof(Manager, cgroup_root), 0),
         SD_BUS_PROPERTY("SystemState", "s", property_get_system_state, 0, 0),
+        SD_BUS_PROPERTY("MachineId", "ay", property_get_machineid, 0, SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("BootId", "ay", property_get_bootid, 0, SD_BUS_VTABLE_PROPERTY_CONST),
 
         SD_BUS_METHOD("GetUnit", "s", "o", method_get_unit, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_METHOD("GetUnitByPID", "u", "o", method_get_unit_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
diff --git a/src/core/manager.c b/src/core/manager.c
index 0cb2044..d33776b 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -491,6 +491,14 @@ int manager_new(SystemdRunningAs running_as, Manager **_m) {
         if (r < 0)
                 goto fail;
 
+        r = sd_id128_get_machine(&m->machineid);
+        if(r < 0 )
+                goto fail;
+
+        r = sd_id128_get_boot(&m->bootid);
+        if(r < 0 )
+                goto fail;
+
         m->udev = udev_new();
         if (!m->udev) {
                 r = -ENOMEM;
diff --git a/src/core/manager.h b/src/core/manager.h
index f2c1b0d..9c74c57 100644
--- a/src/core/manager.h
+++ b/src/core/manager.h
@@ -273,6 +273,10 @@ struct Manager {
 
         /* Reference to the kdbus bus control fd */
         int kdbus_fd;
+
+        sd_id128_t machineid;
+
+        sd_id128_t bootid;
 };
 
 int manager_new(SystemdRunningAs running_as, Manager **m);
diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c
index 267cd74..2c875f2 100644
--- a/src/hostname/hostnamectl.c
+++ b/src/hostname/hostnamectl.c
@@ -73,11 +73,11 @@ typedef struct StatusInfo {
         char *os_cpe_name;
         char *virtualization;
         char *architecture;
+        sd_id128_t machineid;
+        sd_id128_t bootid;
 } StatusInfo;
 
 static void print_status_info(StatusInfo *i) {
-        sd_id128_t mid = {}, bid = {};
-        int r;
 
         assert(i);
 
@@ -96,13 +96,11 @@ static void print_status_info(StatusInfo *i) {
                strna(i->icon_name),
                strna(i->chassis));
 
-        r = sd_id128_get_machine(&mid);
-        if (r >= 0)
-                printf("        Machine ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(mid));
+        if (!sd_id128_equal(i->machineid, SD_ID128_NULL))
+                printf("        Machine ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(i->machineid));
 
-        r = sd_id128_get_boot(&bid);
-        if (r >= 0)
-                printf("           Boot ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(bid));
+        if (!sd_id128_equal(i->bootid, SD_ID128_NULL))
+                printf("           Boot ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(i->bootid));
 
         if (!isempty(i->virtualization))
                 printf("    Virtualization: %s\n", i->virtualization);
@@ -167,6 +165,8 @@ static int show_all_names(sd_bus *bus) {
         static const struct bus_properties_map manager_map[] = {
                 { "Virtualization", "s", NULL, offsetof(StatusInfo, virtualization) },
                 { "Architecture",   "s", NULL, offsetof(StatusInfo, architecture) },
+                { "MachineId", "ay", bus_map_id128, offsetof(StatusInfo, machineid) },
+                { "BootId", "ay", bus_map_id128, offsetof(StatusInfo, bootid) },
                 {}
         };
 
-- 
1.9.3




More information about the systemd-devel mailing list