X Tutup
Skip to content

Commit 9bd6ee8

Browse files
authored
Merge pull request systemd#15531 from felipeborges/add-device-model-field-to-hostnamed
hostnamed: Add "Model" field
2 parents 16c89e6 + b9d8069 commit 9bd6ee8

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

hwdb.d/20-dmi-id.hwdb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This file is part of systemd
2+
3+
# Fix "Lenovo" capitalization in /sys/class/dmi/id/sys_vendor
4+
dmi:bvnLENOVO*
5+
ID_SYSFS_ATTRIBUTE_MODEL=product_version
6+
ID_VENDOR_FROM_DATABASE=Lenovo

hwdb.d/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# they are very long but quite repetitive and the parser is not very fast.
55
# So we don't "test" them.
66
hwdb_files_notest = files('''
7+
20-dmi-id.hwdb
78
20-pci-vendor-model.hwdb
89
20-pci-classes.hwdb
910
20-usb-vendor-model.hwdb

rules.d/50-udev-default.rules.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,7 @@ KERNEL=="udmabuf", GROUP="kvm"
8989

9090
SUBSYSTEM=="ptp", ATTR{clock_name}=="KVM virtual PTP", SYMLINK += "ptp_kvm"
9191

92+
SUBSYSTEM=="dmi", ENV{ID_SYSFS_ATTRIBUTE_MODEL}=="", ENV{ID_VENDOR}="$attr{sys_vendor}", ENV{ID_MODEL}="$attr{product_name}"
93+
SUBSYSTEM=="dmi", ENV{ID_SYSFS_ATTRIBUTE_MODEL}=="product_version", ENV{ID_VENDOR}="$attr{sys_vendor}", ENV{ID_MODEL}="$attr{product_version}"
94+
9295
LABEL="default_end"

src/hostname/hostnamectl.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ typedef struct StatusInfo {
4343
const char *virtualization;
4444
const char *architecture;
4545
const char *home_url;
46+
const char *hardware_vendor;
47+
const char *hardware_model;
4648
} StatusInfo;
4749

4850
static void print_status_info(StatusInfo *i) {
@@ -107,6 +109,11 @@ static void print_status_info(StatusInfo *i) {
107109
if (!isempty(i->architecture))
108110
printf(" Architecture: %s\n", i->architecture);
109111

112+
if (!isempty(i->hardware_vendor))
113+
printf(" Hardware Vendor: %s\n", i->hardware_vendor);
114+
115+
if (!isempty(i->hardware_model))
116+
printf(" Hardware Model: %s\n", i->hardware_model);
110117
}
111118

112119
static int show_one_name(sd_bus *bus, const char* attr) {
@@ -150,6 +157,8 @@ static int show_all_names(sd_bus *bus, sd_bus_error *error) {
150157
{ "OperatingSystemPrettyName", "s", NULL, offsetof(StatusInfo, os_pretty_name) },
151158
{ "OperatingSystemCPEName", "s", NULL, offsetof(StatusInfo, os_cpe_name) },
152159
{ "HomeURL", "s", NULL, offsetof(StatusInfo, home_url) },
160+
{ "HardwareVendor", "s", NULL, offsetof(StatusInfo, hardware_vendor) },
161+
{ "HardwareModel", "s", NULL, offsetof(StatusInfo, hardware_model) },
153162
{}
154163
};
155164

src/hostname/hostnamed.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
#include "hostname-setup.h"
2121
#include "hostname-util.h"
2222
#include "id128-util.h"
23+
#include "libudev.h"
2324
#include "main-func.h"
2425
#include "missing_capability.h"
2526
#include "nscd-flush.h"
2627
#include "nulstr-util.h"
2728
#include "os-util.h"
2829
#include "parse-util.h"
2930
#include "path-util.h"
31+
#include "sd-device.h"
3032
#include "selinux-util.h"
3133
#include "service-util.h"
3234
#include "signal-util.h"
@@ -50,6 +52,9 @@ enum {
5052
PROP_DEPLOYMENT,
5153
PROP_LOCATION,
5254

55+
PROP_HARDWARE_VENDOR,
56+
PROP_HARDWARE_MODEL,
57+
5358
/* Read from /etc/os-release (or /usr/lib/os-release) */
5459
PROP_OS_PRETTY_NAME,
5560
PROP_OS_CPE_NAME,
@@ -426,6 +431,54 @@ static int context_write_data_machine_info(Context *c) {
426431
return write_env_file_label("/etc/machine-info", l);
427432
}
428433

434+
static int property_get_hardware_vendor(
435+
sd_bus *bus,
436+
const char *path,
437+
const char *interface,
438+
const char *property,
439+
sd_bus_message *reply,
440+
void *userdata,
441+
sd_bus_error *error) {
442+
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
443+
const char *hardware_vendor = NULL;
444+
int r;
445+
446+
r = sd_device_new_from_syspath(&device, "/sys/class/dmi/id");
447+
if (r < 0) {
448+
log_warning_errno(r, "Failed to open /sys/class/dmi/id device, ignoring: %m");
449+
return sd_bus_message_append(reply, "s", NULL);
450+
}
451+
452+
if (sd_device_get_property_value(device, "ID_VENDOR_FROM_DATABASE", &hardware_vendor) < 0)
453+
(void) sd_device_get_property_value(device, "ID_VENDOR", &hardware_vendor);
454+
455+
return sd_bus_message_append(reply, "s", hardware_vendor);
456+
}
457+
458+
static int property_get_hardware_model(
459+
sd_bus *bus,
460+
const char *path,
461+
const char *interface,
462+
const char *property,
463+
sd_bus_message *reply,
464+
void *userdata,
465+
sd_bus_error *error) {
466+
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
467+
const char *hardware_model = NULL;
468+
int r;
469+
470+
r = sd_device_new_from_syspath(&device, "/sys/class/dmi/id");
471+
if (r < 0) {
472+
log_warning_errno(r, "Failed to open /sys/class/dmi/id device, ignoring: %m");
473+
return sd_bus_message_append(reply, "s", NULL);
474+
}
475+
476+
if (sd_device_get_property_value(device, "ID_MODEL_FROM_DATABASE", &hardware_model) < 0)
477+
(void) sd_device_get_property_value(device, "ID_MODEL", &hardware_model);
478+
479+
return sd_bus_message_append(reply, "s", hardware_model);
480+
}
481+
429482
static int property_get_hostname(
430483
sd_bus *bus,
431484
const char *path,
@@ -903,6 +956,8 @@ static const sd_bus_vtable hostname_vtable[] = {
903956
SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_PRETTY_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
904957
SD_BUS_PROPERTY("OperatingSystemCPEName", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_CPE_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
905958
SD_BUS_PROPERTY("HomeURL", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_HOME_URL, SD_BUS_VTABLE_PROPERTY_CONST),
959+
SD_BUS_PROPERTY("HardwareVendor", "s", property_get_hardware_vendor, 0, SD_BUS_VTABLE_PROPERTY_CONST),
960+
SD_BUS_PROPERTY("HardwareModel", "s", property_get_hardware_model, 0, SD_BUS_VTABLE_PROPERTY_CONST),
906961

907962
SD_BUS_METHOD_WITH_NAMES("SetHostname",
908963
"sb",

0 commit comments

Comments
 (0)
X Tutup