[systemd-devel] [systemd-commits] .gitignore Makefile.am man/machine-id.xml src/machine-id-main.c src/machine-id-setup.c src/machine-id-setup.h src/main.c

Gustavo Sverzut Barbieri barbieri at profusion.mobi
Sat Mar 5 10:52:14 PST 2011


Oh man, XML?

Ok, this one is pretty parseable with sed or custom code, but couldn't
u keep it as an ini file as systemd units?

On Friday, March 4, 2011, Lennart Poettering
<lennart at kemper.freedesktop.org> wrote:
>  .gitignore             |    1
>  Makefile.am            |   15 ++++
>  man/machine-id.xml     |  104 ++++++++++++++++++++++++++++++
>  src/machine-id-main.c  |   35 ++++++++++
>  src/machine-id-setup.c |  166 +++++++++++++++++++++++++++++++++++++++++++++++++
>  src/machine-id-setup.h |   27 +++++++
>  src/main.c             |    2
>  7 files changed, 349 insertions(+), 1 deletion(-)
>
> New commits:
> commit d7ccca2e3f86feb81a48e243d8bad78814659a74
> Author: Lennart Poettering <lennart at poettering.net>
> Date:   Fri Mar 4 21:53:19 2011 +0100
>
>     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 adee97f..c5bb575 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 1841ad5..3d32df2 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 0000000..fefeb66
> --- /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 at 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 0000000..03970a2
> --- /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 0000000..940670b
> --- /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);
> +

-- 
Gustavo Sverzut Barbieri
http://profusion.mobi embedded systems
--------------------------------------
MSN: barbieri at gmail.com
Skype: gsbarbieri
Mobile: +55 (19) 9225-2202


More information about the systemd-devel mailing list