machine-id-setup.c revision 75f86906c52735c98dc0aa7e24b773edb42ee814
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"
return -EINVAL;
return -EINVAL;
return -EINVAL;
return -errno;
int fd, r;
const char *dbus_machine_id;
if (fd >= 0) {
if (detect_container() > 0) {
if (fd >= 0) {
bool writable = true;
RUN_WITH_UMASK(0000) {
if (fd < 0) {
if (fd < 0) {
"System cannot boot: Missing /etc/machine-id and /etc is mounted read-only.\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");
return -errno;
writable = false;
if (writable)
* /run/machine-id as a replacement */
const char *etc_machine_id;
if (fd < 0)
return log_error_errno(r, "Failed to determine whether %s is on a temporary file system: %m", etc_machine_id);
return -EROFS;
/* Switch to a new mount namespace, isolate ourself and unmount etc_machine_id in our new namespace */
return log_error_errno(errno, "Failed to unmount transient %s file in our private namespace: %m", 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);
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);