diff options
author | Lennart Poettering <lennart@poettering.net> | 2011-03-04 21:53:19 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2011-03-04 21:53:19 +0100 |
commit | d7ccca2e3f86feb81a48e243d8bad78814659a74 (patch) | |
tree | 93f0063f961ec21b31546778add98c3b1cfe5c21 | |
parent | 63a723f3a7caf4eb1bc46a3849f59f27337924b7 (diff) |
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.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile.am | 15 | ||||
-rw-r--r-- | man/machine-id.xml | 104 | ||||
-rw-r--r-- | src/machine-id-main.c | 35 | ||||
-rw-r--r-- | src/machine-id-setup.c | 166 | ||||
-rw-r--r-- | src/machine-id-setup.h | 27 | ||||
-rw-r--r-- | src/main.c | 2 |
7 files changed, 349 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore index adee97f6ce..c5bb5755cc 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 1841ad5c5a..3d32df2e46 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 0000000000..fefeb66fd6 --- /dev/null +++ b/man/machine-id.xml @@ -0,0 +1,104 @@ +<?xml version='1.0'?> <!--*-nxml-*--> +<?xml-stylesheet type="text/xsl" href="http://docbook.sourceforge.net/release/xsl/current/xhtml/docbook.xsl"?> +<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" + "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> + +<!-- + 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 <http://www.gnu.org/licenses/>. +--> + +<refentry id="machine-id"> + <refentryinfo> + <title>/etc/machine-id</title> + <productname>systemd</productname> + + <authorgroup> + <author> + <contrib>Developer</contrib> + <firstname>Lennart</firstname> + <surname>Poettering</surname> + <email>lennart@poettering.net</email> + </author> + </authorgroup> + </refentryinfo> + + <refmeta> + <refentrytitle>machine-id</refentrytitle> + <manvolnum>5</manvolnum> + </refmeta> + + <refnamediv> + <refname>machine-id</refname> + <refpurpose>local machine ID configuration file</refpurpose> + </refnamediv> + + <refsynopsisdiv> + <para><filename>/etc/machine-id</filename></para> + </refsynopsisdiv> + + <refsect1> + <title>Description</title> + + <para>The <filename>/etc/machine-id</filename> 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.</para> + + <para>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.</para> + + <para>The machine ID does not change based on user + configuration, or when hardware is replaced.</para> + + <para>This machine id follows the same format and + logic as the D-Bus machine ID.</para> + + <para>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 + <citerefentry><refentrytitle>gethostid</refentrytitle><manvolnum>3</manvolnum></citerefentry> + call POSIX specifies.</para> + </refsect1> + + <refsect1> + <title>History</title> + + <para>The simple configuration file format of + <filename>/etc/machine-id</filename> originates in the + <filename>/var/lib/dbus/machine-id</filename> file + introduced by D-Bus. In fact this latter file might be a + symlink to the + <varname>/etc/machine-id</varname>.</para> + </refsect1> + + <refsect1> + <title>See Also</title> + <para> + <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>, + <citerefentry><refentrytitle>gethostid</refentrytitle><manvolnum>3</manvolnum></citerefentry>, + <citerefentry><refentrytitle>hostname</refentrytitle><manvolnum>5</manvolnum></citerefentry> + </para> + </refsect1> + +</refentry> diff --git a/src/machine-id-main.c b/src/machine-id-main.c new file mode 100644 index 0000000000..03970a2b06 --- /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 <http://www.gnu.org/licenses/>. +***/ + +#include <unistd.h> +#include <stdlib.h> + +#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 0000000000..940670b339 --- /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 <http://www.gnu.org/licenses/>. +***/ + +#include <unistd.h> +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <stdlib.h> +#include <fcntl.h> +#include <sys/mount.h> + +#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 0000000000..4d0a9cf331 --- /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 <http://www.gnu.org/licenses/>. +***/ + +int machine_id_setup(void); + +#endif diff --git a/src/main.c b/src/main.c index ef6e681771..a041a22eda 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); |