Skip to content

Commit

Permalink
added machine_id_smbios variable
Browse files Browse the repository at this point in the history
  • Loading branch information
FML128 committed Apr 3, 2024
1 parent c099e8f commit c3882d8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions man/kernel-command-line.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<term><varname>systemd.default_standard_error=</varname></term>
<term><varname>systemd.setenv=</varname></term>
<term><varname>systemd.machine_id=</varname></term>
<term><varname>systemd.machine_id_smbios</varname></term>
<term><varname>systemd.set_credential=</varname></term>
<term><varname>systemd.set_credential_binary=</varname></term>
<term><varname>systemd.import_credentials=</varname></term>
Expand Down
2 changes: 2 additions & 0 deletions man/machine-id.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
(on KVM systems), the Xen hypervisor <filename>uuid</filename>, and finally a randomly
generated UUID.</para>

<para>The vm logic can be enabled manually by setting the <varname>systemd.machine_id_smbios</varname> kernel command line option.</para>

<para>After the machine ID is established,
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>
will attempt to save it to <filename>/etc/machine-id</filename>. If this fails, it
Expand Down
17 changes: 15 additions & 2 deletions src/shared/machine-id-setup.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <fcntl.h>
#include <proc-cmdline.h>
#include <sched.h>
#include <sys/mount.h>
#include <unistd.h>
Expand Down Expand Up @@ -48,6 +49,18 @@ static int acquire_machine_id_from_credential(sd_id128_t *ret) {
return 0;
}

static bool machine_id_smbios(void) {
bool b = false;
int r;

r = proc_cmdline_get_bool("systemd.machine_id_smbios", PROC_CMDLINE_VALUE_OPTIONAL, &b);
if (r < 0) {
log_warning_errno(r, "Failed to read systemd.machine_id_smbios from kernel command line, ignoring: %m");
return false;
}
return b;
}

static int generate_machine_id(const char *root, sd_id128_t *ret) {
_cleanup_close_ int fd = -EBADF;
int r;
Expand Down Expand Up @@ -80,14 +93,14 @@ static int generate_machine_id(const char *root, sd_id128_t *ret) {
return 0;
}

} else if (IN_SET(detect_vm(), VIRTUALIZATION_KVM, VIRTUALIZATION_AMAZON, VIRTUALIZATION_QEMU, VIRTUALIZATION_XEN)) {
} else if (IN_SET(detect_vm(), VIRTUALIZATION_KVM, VIRTUALIZATION_AMAZON, VIRTUALIZATION_QEMU, VIRTUALIZATION_XEN) || machine_id_smbios()) {

/* If we are not running in a container, see if we are running in a VM that provides
* a system UUID via the SMBIOS/DMI interfaces. Such environments include QEMU/KVM
* with the -uuid on the qemu command line or the Amazon EC2 Nitro hypervisor. */

if (id128_get_product(ret) >= 0) {
log_info("Initializing machine ID from VM UUID.");
log_info("Initializing machine ID from SMBIOS/DMI UUID.");
return 0;
}
}
Expand Down

0 comments on commit c3882d8

Please sign in to comment.