[systemd-devel] [PATCH] hostnamectl: correct IDs for remote hosts
Rico Sagner
sagner at b1-systems.de
Sat May 31 09:21:22 PDT 2014
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-hostnamed offer the IDs via dbus
and hostnamectl retrieve it this way.
---
src/hostname/hostnamectl.c | 18 ++++++++++--------
src/hostname/hostnamed.c | 18 ++++++++++++++++++
2 files changed, 28 insertions(+), 8 deletions(-)
diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c
index 267cd74..c6fdb9a 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;
+ char *machineid;
+ char *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(!isempty(i->machineid))
+ printf(" Machine ID: %s \n",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(!isempty(i->bootid))
+ printf(" Boot ID: %s \n",i->bootid);
if (!isempty(i->virtualization))
printf(" Virtualization: %s\n", i->virtualization);
@@ -161,6 +159,8 @@ static int show_all_names(sd_bus *bus) {
{ "KernelRelease", "s", NULL, offsetof(StatusInfo, kernel_release) },
{ "OperatingSystemPrettyName", "s", NULL, offsetof(StatusInfo, os_pretty_name) },
{ "OperatingSystemCPEName", "s", NULL, offsetof(StatusInfo, os_cpe_name) },
+ { "MachineID", "s" , NULL, offsetof(StatusInfo, machineid) },
+ { "BootID", "s" , NULL, offsetof(StatusInfo, bootid) },
{}
};
@@ -200,6 +200,8 @@ fail:
free(info.os_cpe_name);
free(info.virtualization);
free(info.architecture);
+ free(info.machineid);
+ free(info.bootid);
return r;
}
diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c
index a350490..b4f86be 100644
--- a/src/hostname/hostnamed.c
+++ b/src/hostname/hostnamed.c
@@ -46,6 +46,8 @@ enum {
PROP_KERNEL_VERSION,
PROP_OS_PRETTY_NAME,
PROP_OS_CPE_NAME,
+ PROP_MACHINE_ID,
+ PROP_BOOT_ID,
_PROP_MAX
};
@@ -74,6 +76,7 @@ static void context_free(Context *c, sd_bus *bus) {
static int context_read_data(Context *c) {
int r;
+ sd_id128_t mid = {}, bid = {};
struct utsname u;
assert(c);
@@ -111,6 +114,19 @@ static int context_read_data(Context *c) {
if (r < 0 && r != -ENOENT)
return r;
+
+ c->data[PROP_MACHINE_ID] = (char *) malloc(sizeof(char)* SD_ID128_STRING_MAX);
+ r = sd_id128_get_machine(&mid);
+ if(r < 0 )
+ return r;
+ snprintf(c->data[PROP_MACHINE_ID],SD_ID128_STRING_MAX,SD_ID128_FORMAT_STR,SD_ID128_FORMAT_VAL(mid));
+
+ c->data[PROP_BOOT_ID] = (char *) malloc(sizeof(char)* SD_ID128_STRING_MAX);
+ r = sd_id128_get_boot(&bid);
+ if(r < 0 )
+ return r;
+ snprintf(c->data[PROP_BOOT_ID],SD_ID128_STRING_MAX,SD_ID128_FORMAT_STR,SD_ID128_FORMAT_VAL(bid));
+
return 0;
}
@@ -598,6 +614,8 @@ static const sd_bus_vtable hostname_vtable[] = {
SD_BUS_PROPERTY("KernelVersion", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_VERSION, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_OS_PRETTY_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OperatingSystemCPEName", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_OS_CPE_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
+ SD_BUS_PROPERTY("MachineID","s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_MACHINE_ID, SD_BUS_VTABLE_PROPERTY_CONST),
+ SD_BUS_PROPERTY("BootID","s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_BOOT_ID, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_METHOD("SetHostname", "sb", NULL, method_set_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("SetStaticHostname", "sb", NULL, method_set_static_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("SetPrettyHostname", "sb", NULL, method_set_pretty_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
--
1.9.3
More information about the systemd-devel
mailing list