machine-id-setup.c revision 671c34195896e89f935d842b13f52a748bd8a5b4
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser 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 <fcntl.h>
#include "systemd/sd-id128.h"
#include "machine-id-setup.h"
#include "macro.h"
#include "util.h"
#include "mkdir.h"
#include "log.h"
#include "virt.h"
#include "fileio.h"
#include "path-util.h"
#include "process-util.h"
unsigned i, j;
/* Converts a UUID into a machine ID, by lowercasing it and
* removing dashes. Validates everything. */
for (i = 0, j = 0; i < 36 && j < 32; i++) {
int t;
if (t < 0)
continue;
destination[j++] = hexchar(t);
}
if (i != 36 || j != 32)
return -EINVAL;
destination[33] = 0;
return 0;
}
char id_to_validate[34];
int r;
/* Reads a machine ID from a file, validates it, and returns
* it. The returned ID ends in a newline. */
if (r < 0)
return r;
return -EINVAL;
id_to_validate[32] = 0;
if (!id128_is_valid(id_to_validate))
return -EINVAL;
id[33] = 0;
return 0;
}
return -errno;
}
int fd, r;
unsigned char *p;
char *q;
const char *vm_id, *dbus_machine_id;
dbus_machine_id = "/var/lib/dbus/machine-id";
else
/* First, try reading the D-Bus machine id, unless it is a symlink */
if (fd >= 0) {
safe_close(fd);
if (r >= 0) {
log_info("Initializing machine ID from D-Bus machine ID.");
return 0;
}
}
/* If that didn't work, see if we are running in a container,
* and a machine ID was passed in via $container_uuid the way
r = detect_container(NULL);
if (r > 0) {
_cleanup_free_ char *e = NULL;
if (r > 0) {
r = shorten_uuid(id, e);
if (r >= 0) {
log_info("Initializing machine ID from container UUID.");
return 0;
}
}
} else {
/* If we are not running in a container, see if we are
char uuid[36];
if (fd >= 0) {
safe_close(fd);
if (r >= 0) {
if (r >= 0) {
log_info("Initializing machine ID from KVM UUID.");
return 0;
}
}
}
}
}
}
/* If that didn't work, generate a random machine id */
r = sd_id128_randomize(&buf);
if (r < 0)
return log_error_errno(r, "Failed to open /dev/urandom: %m");
q[0] = hexchar(*p >> 4);
}
id[33] = 0;
log_info("Initializing machine ID from random generator.");
return 0;
}
int machine_id_setup(const char *root) {
const char *etc_machine_id, *run_machine_id;
bool writable = true;
int r;
etc_machine_id = "/etc/machine-id";
run_machine_id = "/run/machine-id";
} else {
char *x;
}
RUN_WITH_UMASK(0000) {
/* We create this 0444, to indicate that this isn't really
* something you should ever modify. Of course, since the file
* will be owned by root it doesn't matter much, but maybe
* people look. */
if (fd < 0) {
if (fd < 0) {
"System cannot boot: Missing /etc/machine-id and /etc is mounted read-only.\n"
"Booting up is supported only when:\n"
"1) /etc/machine-id exists and is populated.\n"
"2) /etc/machine-id exists and is empty.\n"
"3) /etc/machine-id is missing and /etc is writable.\n");
else
return -errno;
}
writable = false;
}
}
return 0;
/* Hmm, so, the id currently stored is not useful, then let's
* generate one */
if (r < 0)
return r;
if (writable)
return 0;
/* Hmm, we couldn't write it? So let's write it to
* /run/machine-id as a replacement */
RUN_WITH_UMASK(0022) {
}
if (r < 0) {
(void) unlink(run_machine_id);
}
/* And now, let's mount it over */
(void) unlink_noerrno(run_machine_id);
}
/* Mark the mount read-only */
return 0;
}
int machine_id_commit(const char *root) {
const char *etc_machine_id;
int r;
etc_machine_id = "/etc/machine-id";
else {
char *x;
}
r = path_is_mount_point(etc_machine_id, 0);
if (r < 0)
if (r == 0) {
return 0;
}
/* Read existing machine-id */
if (fd < 0)
if (r < 0)
r = fd_is_temporary_fs(fd);
if (r < 0)
return log_error_errno(r, "Failed to determine whether %s is on a temporary file system: %m", etc_machine_id);
if (r == 0) {
return -EROFS;
}
/* Store current mount namespace */
if (r < 0)
return log_error_errno(r, "Can't fetch current mount namespace: %m");
/* Switch to a new mount namespace, isolate ourself and unmount etc_machine_id in our new namespace */
if (unshare(CLONE_NEWNS) < 0)
if (umount(etc_machine_id) < 0)
return log_error_errno(errno, "Failed to unmount transient %s file in our private namespace: %m", etc_machine_id);
/* Update a persistent version of etc_machine_id */
if (fd < 0)
return log_error_errno(errno, "Cannot open for writing %s. This is mandatory to get a persistent machine-id: %m", etc_machine_id);
if (r < 0)
/* Return to initial namespace and proceed a lazy tmpfs unmount */
if (r < 0)
return log_warning_errno(r, "Failed to switch back to initial mount namespace: %m.\nWe'll keep transient %s file until next reboot.", etc_machine_id);
return log_warning_errno(errno, "Failed to unmount transient %s file: %m.\nWe keep that mount until next reboot.", etc_machine_id);
return 0;
}