X Tutup
Skip to content

Commit 4fc7e4f

Browse files
committed
hostname: allow to override hardware vendor and model
Sometimes hardware vendor does not set DMI info correctly. Already there is a way that the dbus properties can be overriden by using hwdb. But that is not user friendly. This adds two new fields in /etc/machine-info. Closes systemd#22207.
1 parent 8c8b180 commit 4fc7e4f

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

man/machine-info.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@
139139
<citerefentry><refentrytitle>machine-id</refentrytitle><manvolnum>5</manvolnum></citerefentry>
140140
will be used.</para></listitem>
141141
</varlistentry>
142+
143+
<varlistentry>
144+
<term><varname>VENDOR=</varname></term>
145+
146+
<listitem><para>Specifies the hardware vendor. If unspecified, the hardware vendor set in DMI
147+
or hwdb will be used.</para></listitem>
148+
</varlistentry>
149+
150+
<varlistentry>
151+
<term><varname>MODEL=</varname></term>
152+
153+
<listitem><para>Specifies the hardware model. If unspecified, the hardware model set in DMI or
154+
hwdb will be used.</para></listitem>
155+
</varlistentry>
142156
</variablelist>
143157
</refsect1>
144158

src/hostname/hostnamed.c

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
/* Properties we cache are indexed by an enum, to make invalidation easy and systematic (as we can iterate
4545
* through them all, and they are uniformly strings). */
46-
enum {
46+
typedef enum {
4747
/* Read from /etc/hostname */
4848
PROP_STATIC_HOSTNAME,
4949

@@ -53,14 +53,16 @@ enum {
5353
PROP_CHASSIS,
5454
PROP_DEPLOYMENT,
5555
PROP_LOCATION,
56+
PROP_VENDOR,
57+
PROP_MODEL,
5658

5759
/* Read from /etc/os-release (or /usr/lib/os-release) */
5860
PROP_OS_PRETTY_NAME,
5961
PROP_OS_CPE_NAME,
6062
PROP_OS_HOME_URL,
6163
_PROP_MAX,
6264
_PROP_INVALID = -EINVAL,
63-
};
65+
} HostProperty;
6466

6567
typedef struct Context {
6668
char *data[_PROP_MAX];
@@ -133,7 +135,9 @@ static void context_read_machine_info(Context *c) {
133135
"ICON_NAME", &c->data[PROP_ICON_NAME],
134136
"CHASSIS", &c->data[PROP_CHASSIS],
135137
"DEPLOYMENT", &c->data[PROP_DEPLOYMENT],
136-
"LOCATION", &c->data[PROP_LOCATION]);
138+
"LOCATION", &c->data[PROP_LOCATION],
139+
"VENDOR", &c->data[PROP_VENDOR],
140+
"MODEL", &c->data[PROP_MODEL]);
137141
if (r < 0 && r != -ENOENT)
138142
log_warning_errno(r, "Failed to read /etc/machine-info, ignoring: %m");
139143

@@ -515,6 +519,27 @@ static int context_write_data_machine_info(Context *c) {
515519
return 0;
516520
}
517521

522+
static int property_get_hardware_property(
523+
sd_bus_message *reply,
524+
Context *c,
525+
HostProperty prop,
526+
int (*getter)(char **)) {
527+
528+
_cleanup_free_ char *from_dmi = NULL;
529+
530+
assert(reply);
531+
assert(c);
532+
assert(IN_SET(prop, PROP_VENDOR, PROP_MODEL));
533+
assert(getter);
534+
535+
context_read_machine_info(c);
536+
537+
if (isempty(c->data[prop]))
538+
(void) getter(&from_dmi);
539+
540+
return sd_bus_message_append(reply, "s", from_dmi ?: c->data[prop]);
541+
}
542+
518543
static int property_get_hardware_vendor(
519544
sd_bus *bus,
520545
const char *path,
@@ -524,10 +549,7 @@ static int property_get_hardware_vendor(
524549
void *userdata,
525550
sd_bus_error *error) {
526551

527-
_cleanup_free_ char *vendor = NULL;
528-
529-
(void) get_hardware_vendor(&vendor);
530-
return sd_bus_message_append(reply, "s", vendor);
552+
return property_get_hardware_property(reply, userdata, PROP_VENDOR, get_hardware_vendor);
531553
}
532554

533555
static int property_get_hardware_model(
@@ -539,10 +561,7 @@ static int property_get_hardware_model(
539561
void *userdata,
540562
sd_bus_error *error) {
541563

542-
_cleanup_free_ char *model = NULL;
543-
544-
(void) get_hardware_model(&model);
545-
return sd_bus_message_append(reply, "s", model);
564+
return property_get_hardware_property(reply, userdata, PROP_MODEL, get_hardware_model);
546565
}
547566

548567
static int property_get_hostname(
@@ -1087,8 +1106,10 @@ static int method_describe(sd_bus_message *m, void *userdata, sd_bus_error *erro
10871106

10881107
assert_se(uname(&u) >= 0);
10891108

1090-
(void) get_hardware_vendor(&vendor);
1091-
(void) get_hardware_model(&model);
1109+
if (isempty(c->data[PROP_VENDOR]))
1110+
(void) get_hardware_vendor(&vendor);
1111+
if (isempty(c->data[PROP_MODEL]))
1112+
(void) get_hardware_model(&model);
10921113

10931114
if (privileged) /* The product UUID is only available to privileged clients */
10941115
id128_get_product(&product_uuid);
@@ -1109,8 +1130,8 @@ static int method_describe(sd_bus_message *m, void *userdata, sd_bus_error *erro
11091130
JSON_BUILD_PAIR("OperatingSystemPrettyName", JSON_BUILD_STRING(c->data[PROP_OS_PRETTY_NAME])),
11101131
JSON_BUILD_PAIR("OperatingSystemCPEName", JSON_BUILD_STRING(c->data[PROP_OS_CPE_NAME])),
11111132
JSON_BUILD_PAIR("OperatingSystemHomeURL", JSON_BUILD_STRING(c->data[PROP_OS_HOME_URL])),
1112-
JSON_BUILD_PAIR("HardwareVendor", JSON_BUILD_STRING(vendor)),
1113-
JSON_BUILD_PAIR("HardwareModel", JSON_BUILD_STRING(model)),
1133+
JSON_BUILD_PAIR("HardwareVendor", JSON_BUILD_STRING(vendor ?: c->data[PROP_VENDOR])),
1134+
JSON_BUILD_PAIR("HardwareModel", JSON_BUILD_STRING(model ?: c->data[PROP_MODEL])),
11141135
JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(product_uuid), "ProductUUID", JSON_BUILD_ID128(product_uuid)),
11151136
JSON_BUILD_PAIR_CONDITION(sd_id128_is_null(product_uuid), "ProductUUID", JSON_BUILD_NULL)));
11161137

0 commit comments

Comments
 (0)
X Tutup