From: Lennart Poettering Date: Fri, 4 Mar 2011 20:53:19 +0000 (+0100) Subject: main: introduce /etc/machine-id X-Git-Tag: v20~21 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d7ccca2e3f86feb81a48e243d8bad78814659a74;p=systemd main: introduce /etc/machine-id This is supposed to play the same roles /var/lib/dbus/machine-id, however fixes a couple of problems: - It is available during early boot since it is stored in /etc - Removes the ID from the D-Bus context and moves it into a system context, thus hopefully lowering hesitation by people to use it. - It is generated at installation time. If the file is empty at boot time it will be mounted over with a randomly generated ID, which is not saved to disk. This is useful to support state-less machines with no transient or writable /etc configuration. --- diff --git a/.gitignore b/.gitignore index adee97f6..c5bb5755 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +systemd-machine-id-setup systemd-detect-virt systemd-sysctl test-strv diff --git a/Makefile.am b/Makefile.am index 1841ad5c..3d32df2e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -98,7 +98,8 @@ rootbin_PROGRAMS = \ systemd-notify \ systemd-ask-password \ systemd-tty-ask-password-agent \ - systemd-tmpfiles + systemd-tmpfiles \ + systemd-machine-id-setup bin_PROGRAMS = \ systemd-cgls @@ -434,6 +435,7 @@ libsystemd_core_la_SOURCES = \ src/loopback-setup.c \ src/kmod-setup.c \ src/locale-setup.c \ + src/machine-id-setup.c \ src/specifier.c \ src/unit-name.c \ src/fdset.c \ @@ -523,6 +525,7 @@ MANPAGES = \ man/systemd.conf.5 \ man/tmpfiles.d.5 \ man/hostname.5 \ + man/machine-id.5 \ man/vconsole.conf.5 \ man/locale.conf.5 \ man/os-release.5 \ @@ -737,6 +740,16 @@ systemd_tmpfiles_CFLAGS = \ systemd_tmpfiles_LDADD = \ libsystemd-basic.la +systemd_machine_id_setup_SOURCES = \ + src/machine-id-setup.c \ + src/machine-id-main.c + +systemd_machine_id_setup_CFLAGS = \ + $(AM_CFLAGS) + +systemd_machine_id_setup_LDADD = \ + libsystemd-basic.la + systemd_sysctl_SOURCES = \ src/sysctl.c diff --git a/man/machine-id.xml b/man/machine-id.xml new file mode 100644 index 00000000..fefeb66f --- /dev/null +++ b/man/machine-id.xml @@ -0,0 +1,104 @@ + + + + + + + + + /etc/machine-id + systemd + + + + Developer + Lennart + Poettering + lennart@poettering.net + + + + + + machine-id + 5 + + + + machine-id + local machine ID configuration file + + + + /etc/machine-id + + + + Description + + The /etc/machine-id file + configures the unique machine id of the local system + that is set during installation. It should contain a + single newline-terminated, hexadecimal, lowercase 16 + character machine ID string. + + The machine ID is usually generated from a + random source during system installation and stays + constant for all subsequent boots. Optionally, for + stateless systems it is generated during runtime at + boot. + + The machine ID does not change based on user + configuration, or when hardware is replaced. + + This machine id follows the same format and + logic as the D-Bus machine ID. + + Programs may use this ID to identify the host + with a globally unique ID in the network, that does + not change even if the local network configuration + changes. Due to this and its greater length it is + a more useful replacement than the + gethostid3 + call POSIX specifies. + + + + History + + The simple configuration file format of + /etc/machine-id originates in the + /var/lib/dbus/machine-id file + introduced by D-Bus. In fact this latter file might be a + symlink to the + /etc/machine-id. + + + + See Also + + systemd1, + gethostid3, + hostname5 + + + + diff --git a/src/machine-id-main.c b/src/machine-id-main.c new file mode 100644 index 00000000..03970a2b --- /dev/null +++ b/src/machine-id-main.c @@ -0,0 +1,35 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see . +***/ + +#include +#include + +#include "machine-id-setup.h" +#include "log.h" + +int main(int argc, char *argv[]) { + + log_set_target(LOG_TARGET_AUTO); + log_parse_environment(); + log_open(); + + return machine_id_setup() < 0 ? EXIT_FAILURE : EXIT_SUCCESS; +} diff --git a/src/machine-id-setup.c b/src/machine-id-setup.c new file mode 100644 index 00000000..940670b3 --- /dev/null +++ b/src/machine-id-setup.c @@ -0,0 +1,166 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include +#include +#include +#include +#include + +#include "machine-id-setup.h" +#include "macro.h" +#include "util.h" +#include "log.h" + +static int generate(char id[34]) { + int fd; + char buf[16]; + char *p, *q; + ssize_t k; + + assert(id); + + /* First, try reading the D-Bus machine id, unless it is a symlink */ + if ((fd = open("/var/lib/dbus/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0) { + + k = loop_read(fd, id, 33, false); + close_nointr_nofail(fd); + + if (k >= 32) { + id[32] = '\n'; + id[33] = 0; + + log_info("Initializing machine ID from D-Bus machine ID."); + return 0; + } + } + + /* If that didn't work, generate a random machine id */ + if ((fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) { + log_error("Failed to open /dev/urandom: %m"); + return -errno; + } + + k = loop_read(fd, buf, sizeof(buf), false); + close_nointr_nofail(fd); + + if (k != sizeof(buf)) { + log_error("Failed to read /dev/urandom: %s", strerror(k < 0 ? -k : EIO)); + return k < 0 ? (int) k : -EIO; + } + + for (p = buf, q = id; p < buf + sizeof(buf); p++, q += 2) { + q[0] = hexchar(*p >> 4); + q[1] = hexchar(*p & 15); + } + + id[32] = '\n'; + id[33] = 0; + + log_info("Initializing machine ID from random generator."); + + return 0; +} + +int machine_id_setup(void) { + int fd, r; + bool writable; + struct stat st; + char id[34]; /* 32 + \n + \0 */ + mode_t m; + + m = umask(0000); + + if ((fd = open("/etc/machine-id", O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0644)) >= 0) + writable = true; + else { + if ((fd = open("/etc/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY)) < 0) { + umask(m); + log_error("Cannot open /etc/machine-id: %m"); + return -errno; + } + + writable = false; + } + + umask(m); + + if (fstat(fd, &st) < 0) { + log_error("fstat() failed: %m"); + r = -errno; + goto finish; + } + + if (S_ISREG(st.st_mode)) { + if (loop_read(fd, id, 32, false) >= 32) { + r = 0; + goto finish; + } + } + + /* Hmm, so, the id currently stored is not useful, then let's + * generate one */ + + if ((r = generate(id)) < 0) + goto finish; + + if (S_ISREG(st.st_mode) && writable) { + lseek(fd, 0, SEEK_SET); + + if (loop_write(fd, id, 33, false) == 33) { + r = 0; + goto finish; + } + } + + close_nointr_nofail(fd); + fd = -1; + + /* Hmm, we couldn't write it? So let's write it to + * /dev/.systemd/machine-id as a replacement */ + + mkdir_p("/dev/.systemd", 0755); + + if ((r = write_one_line_file("/dev/.systemd/machine-id", id)) < 0) { + log_error("Cannot write /dev/.systemd/machine-id: %s", strerror(-r)); + + unlink("/dev/.systemd/machine-id"); + goto finish; + } + + /* And now, let's mount it over */ + r = mount("/dev/.systemd/machine-id", "/etc/machine-id", "bind", MS_BIND|MS_RDONLY, NULL) < 0 ? -errno : 0; + unlink("/dev/.systemd/machine-id"); + + if (r < 0) + log_error("Failed to mount /etc/machine-id: %s", strerror(-r)); + else + log_info("Installed non-transient /etc/machine-id file."); + +finish: + + if (fd >= 0) + close_nointr_nofail(fd); + + return r; +} diff --git a/src/machine-id-setup.h b/src/machine-id-setup.h new file mode 100644 index 00000000..4d0a9cf3 --- /dev/null +++ b/src/machine-id-setup.h @@ -0,0 +1,27 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#ifndef foomachineidsetuphfoo +#define foomachineidsetuphfoo + +/*** + This file is part of systemd. + + Copyright 2010 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see . +***/ + +int machine_id_setup(void); + +#endif diff --git a/src/main.c b/src/main.c index ef6e6817..a041a22e 100644 --- a/src/main.c +++ b/src/main.c @@ -41,6 +41,7 @@ #include "kmod-setup.h" #include "locale-setup.h" #include "selinux-setup.h" +#include "machine-id-setup.h" #include "load-fragment.h" #include "fdset.h" #include "special.h" @@ -1127,6 +1128,7 @@ int main(int argc, char *argv[]) { kmod_setup(); hostname_setup(); + machine_id_setup(); loopback_setup(); mkdir_p("/dev/.systemd/ask-password/", 0755);