Skip to content

Commit

Permalink
hostnamectl: show image info in hostnamectl (systemd#36638)
Browse files Browse the repository at this point in the history
On image-based systems these properties are quite fundamental, hence
show them in the hostnamed output.
  • Loading branch information
bluca authored Mar 6, 2025
2 parents 10b8d65 + f37df1b commit cb26206
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 41 deletions.
16 changes: 15 additions & 1 deletion man/org.freedesktop.hostname1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ node /org/freedesktop/hostname1 {
@org.freedesktop.DBus.Property.EmitsChangedSignal("const")
readonly s HomeURL = '...';
@org.freedesktop.DBus.Property.EmitsChangedSignal("const")
readonly s OperatingSystemImageID = '...';
@org.freedesktop.DBus.Property.EmitsChangedSignal("const")
readonly s OperatingSystemImageVersion = '...';
@org.freedesktop.DBus.Property.EmitsChangedSignal("const")
readonly s HardwareVendor = '...';
@org.freedesktop.DBus.Property.EmitsChangedSignal("const")
readonly s HardwareModel = '...';
Expand Down Expand Up @@ -168,6 +172,10 @@ node /org/freedesktop/hostname1 {

<variablelist class="dbus-property" generated="True" extra-ref="HomeURL"/>

<variablelist class="dbus-property" generated="True" extra-ref="OperatingSystemImageID"/>

<variablelist class="dbus-property" generated="True" extra-ref="OperatingSystemImageVersion"/>

<variablelist class="dbus-property" generated="True" extra-ref="HardwareVendor"/>

<variablelist class="dbus-property" generated="True" extra-ref="HardwareModel"/>
Expand Down Expand Up @@ -293,6 +301,11 @@ node /org/freedesktop/hostname1 {
information is known. It's an unsigned 64bit value, in µs since the UNIX epoch, UTC. If this information
is not known carries the value 2^64-1, i.e. <constant>UINT64_MAX</constant>.</para>

<para><varname>OperatingSystemImageID</varname> and <varname>OperatingSystemImageVersion</varname> expose
the OS image name and version if available, or contain empty strings otherwise. This mostly corresponds
to the <varname>IMAGE_ID=</varname> and <varname>IMAGE_VERSION=</varname> fields of the
<filename>os-release</filename> file.</para>

<para><varname>HardwareVendor</varname> and <varname>HardwareModel</varname> expose information about the
vendor of the hardware of the system. If no such information can be determined these properties are set
to empty strings.</para>
Expand Down Expand Up @@ -460,7 +473,8 @@ node /org/freedesktop/hostname1 {
<varname>FirmwareDate</varname> were added in version 253.</para>
<para><varname>MachineID</varname>, <varname>BootID</varname> and
<varname>VSockCID</varname> were added in version 256.</para>
<para><varname>ChassisAssetTag</varname> was added in version 258.</para>
<para><varname>ChassisAssetTag</varname>, <varname>OperatingSystemImageID</varname> and
<varname>OperatingSystemImageVersion</varname> were added in version 258.</para>
</refsect2>
</refsect1>

Expand Down
78 changes: 50 additions & 28 deletions src/hostname/hostnamectl.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ typedef struct StatusInfo {
const char *os_pretty_name;
const char *os_cpe_name;
usec_t os_support_end;
const char *os_image_id;
const char *os_image_version;
const char *virtualization;
const char *architecture;
const char *home_url;
Expand Down Expand Up @@ -259,6 +261,22 @@ static int print_status_info(StatusInfo *i) {
return table_log_add_error(r);
}

if (!isempty(i->os_image_id)) {
r = table_add_many(table,
TABLE_FIELD, "OS Image",
TABLE_STRING, i->os_image_id);
if (r < 0)
return table_log_add_error(r);
}

if (!isempty(i->os_image_version)) {
r = table_add_many(table,
TABLE_FIELD, "OS Image Version",
TABLE_STRING, i->os_image_version);
if (r < 0)
return table_log_add_error(r);
}

if (!isempty(i->kernel_name) && !isempty(i->kernel_release)) {
const char *v;

Expand Down Expand Up @@ -377,31 +395,33 @@ static int show_all_names(sd_bus *bus) {
};

static const struct bus_properties_map hostname_map[] = {
{ "Hostname", "s", NULL, offsetof(StatusInfo, hostname) },
{ "StaticHostname", "s", NULL, offsetof(StatusInfo, static_hostname) },
{ "PrettyHostname", "s", NULL, offsetof(StatusInfo, pretty_hostname) },
{ "IconName", "s", NULL, offsetof(StatusInfo, icon_name) },
{ "Chassis", "s", NULL, offsetof(StatusInfo, chassis) },
{ "ChassisAssetTag", "s", NULL, offsetof(StatusInfo, chassis_asset_tag)},
{ "Deployment", "s", NULL, offsetof(StatusInfo, deployment) },
{ "Location", "s", NULL, offsetof(StatusInfo, location) },
{ "KernelName", "s", NULL, offsetof(StatusInfo, kernel_name) },
{ "KernelRelease", "s", NULL, offsetof(StatusInfo, kernel_release) },
{ "OperatingSystemPrettyName", "s", NULL, offsetof(StatusInfo, os_pretty_name) },
{ "OperatingSystemCPEName", "s", NULL, offsetof(StatusInfo, os_cpe_name) },
{ "OperatingSystemSupportEnd", "t", NULL, offsetof(StatusInfo, os_support_end) },
{ "HomeURL", "s", NULL, offsetof(StatusInfo, home_url) },
{ "HardwareVendor", "s", NULL, offsetof(StatusInfo, hardware_vendor) },
{ "HardwareModel", "s", NULL, offsetof(StatusInfo, hardware_model) },
{ "FirmwareVersion", "s", NULL, offsetof(StatusInfo, firmware_version) },
{ "FirmwareDate", "t", NULL, offsetof(StatusInfo, firmware_date) },
{ "MachineID", "ay", bus_map_id128, offsetof(StatusInfo, machine_id) },
{ "BootID", "ay", bus_map_id128, offsetof(StatusInfo, boot_id) },
{ "VSockCID", "u", NULL, offsetof(StatusInfo, vsock_cid) },
{ "Hostname", "s", NULL, offsetof(StatusInfo, hostname) },
{ "StaticHostname", "s", NULL, offsetof(StatusInfo, static_hostname) },
{ "PrettyHostname", "s", NULL, offsetof(StatusInfo, pretty_hostname) },
{ "IconName", "s", NULL, offsetof(StatusInfo, icon_name) },
{ "Chassis", "s", NULL, offsetof(StatusInfo, chassis) },
{ "ChassisAssetTag", "s", NULL, offsetof(StatusInfo, chassis_asset_tag)},
{ "Deployment", "s", NULL, offsetof(StatusInfo, deployment) },
{ "Location", "s", NULL, offsetof(StatusInfo, location) },
{ "KernelName", "s", NULL, offsetof(StatusInfo, kernel_name) },
{ "KernelRelease", "s", NULL, offsetof(StatusInfo, kernel_release) },
{ "OperatingSystemPrettyName", "s", NULL, offsetof(StatusInfo, os_pretty_name) },
{ "OperatingSystemCPEName", "s", NULL, offsetof(StatusInfo, os_cpe_name) },
{ "OperatingSystemSupportEnd", "t", NULL, offsetof(StatusInfo, os_support_end) },
{ "OperatingSystemImageID", "s", NULL, offsetof(StatusInfo, os_image_id) },
{ "OperatingSystemImageVersion", "s", NULL, offsetof(StatusInfo, os_image_version) },
{ "HomeURL", "s", NULL, offsetof(StatusInfo, home_url) },
{ "HardwareVendor", "s", NULL, offsetof(StatusInfo, hardware_vendor) },
{ "HardwareModel", "s", NULL, offsetof(StatusInfo, hardware_model) },
{ "FirmwareVersion", "s", NULL, offsetof(StatusInfo, firmware_version) },
{ "FirmwareDate", "t", NULL, offsetof(StatusInfo, firmware_date) },
{ "MachineID", "ay", bus_map_id128, offsetof(StatusInfo, machine_id) },
{ "BootID", "ay", bus_map_id128, offsetof(StatusInfo, boot_id) },
{ "VSockCID", "u", NULL, offsetof(StatusInfo, vsock_cid) },
{}
}, manager_map[] = {
{ "Virtualization", "s", NULL, offsetof(StatusInfo, virtualization) },
{ "Architecture", "s", NULL, offsetof(StatusInfo, architecture) },
{ "Virtualization", "s", NULL, offsetof(StatusInfo, virtualization) },
{ "Architecture", "s", NULL, offsetof(StatusInfo, architecture) },
{}
};

Expand Down Expand Up @@ -669,16 +689,16 @@ static int help(void) {
if (r < 0)
return log_oom();

printf("%s [OPTIONS...] COMMAND ...\n\n"
"%sQuery or change system hostname.%s\n"
"\nCommands:\n"
printf("%1$s [OPTIONS...] COMMAND ...\n\n"
"%2$sQuery or change system hostname.%3$s\n"
"\n%4$sCommands:%5$s\n"
" status Show current hostname settings\n"
" hostname [NAME] Get/set system hostname\n"
" icon-name [NAME] Get/set icon name for host\n"
" chassis [NAME] Get/set chassis type for host\n"
" deployment [NAME] Get/set deployment environment for host\n"
" location [NAME] Get/set location for host\n"
"\nOptions:\n"
"\n%4$sOptions:%5$s\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --no-ask-password Do not prompt for password\n"
Expand All @@ -690,10 +710,12 @@ static int help(void) {
" --json=pretty|short|off\n"
" Generate JSON output\n"
" -j Same as --json=pretty on tty, --json=short otherwise\n"
"\nSee the %s for details.\n",
"\nSee the %6$s for details.\n",
program_invocation_short_name,
ansi_highlight(),
ansi_normal(),
ansi_underline(),
ansi_normal(),
link);

return 0;
Expand Down
34 changes: 22 additions & 12 deletions src/hostname/hostnamed.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ typedef enum {
PROP_OS_CPE_NAME,
PROP_OS_HOME_URL,
PROP_OS_SUPPORT_END,
PROP_OS_IMAGE_ID,
PROP_OS_IMAGE_VERSION,
_PROP_MAX,
_PROP_INVALID = -EINVAL,
} HostProperty;
Expand Down Expand Up @@ -181,14 +183,18 @@ static void context_read_os_release(Context *c) {
(UINT64_C(1) << PROP_OS_PRETTY_NAME) |
(UINT64_C(1) << PROP_OS_CPE_NAME) |
(UINT64_C(1) << PROP_OS_HOME_URL) |
(UINT64_C(1) << PROP_OS_SUPPORT_END));
(UINT64_C(1) << PROP_OS_SUPPORT_END) |
(UINT64_C(1) << PROP_OS_IMAGE_ID) |
(UINT64_C(1) << PROP_OS_IMAGE_VERSION));

r = parse_os_release(NULL,
"PRETTY_NAME", &os_pretty_name,
"NAME", &os_name,
"CPE_NAME", &c->data[PROP_OS_CPE_NAME],
"HOME_URL", &c->data[PROP_OS_HOME_URL],
"SUPPORT_END", &c->data[PROP_OS_SUPPORT_END]);
"PRETTY_NAME", &os_pretty_name,
"NAME", &os_name,
"CPE_NAME", &c->data[PROP_OS_CPE_NAME],
"HOME_URL", &c->data[PROP_OS_HOME_URL],
"SUPPORT_END", &c->data[PROP_OS_SUPPORT_END],
"IMAGE_ID", &c->data[PROP_OS_IMAGE_ID],
"IMAGE_VERSION", &c->data[PROP_OS_IMAGE_VERSION]);
if (r < 0 && r != -ENOENT)
log_warning_errno(r, "Failed to read os-release file, ignoring: %m");

Expand Down Expand Up @@ -1571,6 +1577,8 @@ static int build_describe_response(Context *c, bool privileged, sd_json_variant
SD_JSON_BUILD_PAIR_STRING("OperatingSystemHomeURL", c->data[PROP_OS_HOME_URL]),
JSON_BUILD_PAIR_FINITE_USEC("OperatingSystemSupportEnd", eol),
SD_JSON_BUILD_PAIR("OperatingSystemReleaseData", JSON_BUILD_STRV_ENV_PAIR(os_release_pairs)),
SD_JSON_BUILD_PAIR_STRING("OperatingSystemImageID", c->data[PROP_OS_IMAGE_ID]),
SD_JSON_BUILD_PAIR_STRING("OperatingSystemImageVersion", c->data[PROP_OS_IMAGE_VERSION]),
SD_JSON_BUILD_PAIR("MachineInformationData", JSON_BUILD_STRV_ENV_PAIR(machine_info_pairs)),
SD_JSON_BUILD_PAIR_STRING("HardwareVendor", vendor ?: c->data[PROP_HARDWARE_VENDOR]),
SD_JSON_BUILD_PAIR_STRING("HardwareModel", model ?: c->data[PROP_HARDWARE_MODEL]),
Expand Down Expand Up @@ -1628,20 +1636,22 @@ static const sd_bus_vtable hostname_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("Hostname", "s", property_get_hostname, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("StaticHostname", "s", property_get_static_hostname, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("PrettyHostname", "s", property_get_machine_info_field, offsetof(Context, data) + sizeof(char*) * PROP_PRETTY_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("PrettyHostname", "s", property_get_machine_info_field, offsetof(Context, data[PROP_PRETTY_HOSTNAME]), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("DefaultHostname", "s", property_get_default_hostname, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HostnameSource", "s", property_get_hostname_source, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("IconName", "s", property_get_icon_name, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("Chassis", "s", property_get_chassis, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("Deployment", "s", property_get_machine_info_field, offsetof(Context, data) + sizeof(char*) * PROP_DEPLOYMENT, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("Location", "s", property_get_machine_info_field, offsetof(Context, data) + sizeof(char*) * PROP_LOCATION, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("Deployment", "s", property_get_machine_info_field, offsetof(Context, data[PROP_DEPLOYMENT]), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("Location", "s", property_get_machine_info_field, offsetof(Context, data[PROP_LOCATION]), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("KernelName", "s", property_get_uname_field, offsetof(struct utsname, sysname), SD_BUS_VTABLE_ABSOLUTE_OFFSET|SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KernelRelease", "s", property_get_uname_field, offsetof(struct utsname, release), SD_BUS_VTABLE_ABSOLUTE_OFFSET|SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KernelVersion", "s", property_get_uname_field, offsetof(struct utsname, version), SD_BUS_VTABLE_ABSOLUTE_OFFSET|SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_PRETTY_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OperatingSystemCPEName", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_CPE_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", property_get_os_release_field, offsetof(Context, data[PROP_OS_PRETTY_NAME]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OperatingSystemCPEName", "s", property_get_os_release_field, offsetof(Context, data[PROP_OS_CPE_NAME]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OperatingSystemSupportEnd", "t", property_get_os_support_end, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HomeURL", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_HOME_URL, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HomeURL", "s", property_get_os_release_field, offsetof(Context, data[PROP_OS_HOME_URL]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OperatingSystemImageID", "s", property_get_os_release_field, offsetof(Context, data[PROP_OS_IMAGE_ID]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OperatingSystemImageVersion", "s", property_get_os_release_field, offsetof(Context, data[PROP_OS_IMAGE_VERSION]), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HardwareVendor", "s", property_get_hardware_vendor, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("HardwareModel", "s", property_get_hardware_model, 0, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("FirmwareVersion", "s", property_get_firmware_version, 0, SD_BUS_VTABLE_PROPERTY_CONST),
Expand Down

0 comments on commit cb26206

Please sign in to comment.