diff --git a/src/boot/bootctl-install.c b/src/boot/bootctl-install.c index 1dee592d77f..c382d2a84f9 100644 --- a/src/boot/bootctl-install.c +++ b/src/boot/bootctl-install.c @@ -6,30 +6,33 @@ #include "bootctl-util.h" #include "chase-symlinks.h" #include "copy.h" +#include "dirent-util.h" +#include "efi-api.h" #include "env-file.h" #include "fd-util.h" #include "fileio.h" #include "fs-util.h" #include "glyph-util.h" +#include "id128-util.h" #include "os-util.h" #include "path-util.h" +#include "rm-rf.h" #include "stat-util.h" #include "sync-util.h" #include "tmpfile-util.h" #include "umask-util.h" #include "utf8.h" -#include "dirent-util.h" -#include "efi-api.h" -#include "rm-rf.h" static int load_etc_machine_id(void) { int r; r = sd_id128_get_machine(&arg_machine_id); - if (IN_SET(r, -ENOENT, -ENOMEDIUM, -ENOPKG)) /* Not set or empty */ - return 0; - if (r < 0) + if (r < 0) { + if (ERRNO_IS_MACHINE_ID_UNSET(r)) /* Not set or empty */ + return 0; + return log_error_errno(r, "Failed to get machine-id: %m"); + } log_debug("Loaded machine ID %s from /etc/machine-id.", SD_ID128_TO_STRING(arg_machine_id)); return 0; diff --git a/src/libsystemd/sd-id128/id128-util.h b/src/libsystemd/sd-id128/id128-util.h index 887f443d693..e094de64419 100644 --- a/src/libsystemd/sd-id128/id128-util.h +++ b/src/libsystemd/sd-id128/id128-util.h @@ -32,3 +32,10 @@ extern const struct hash_ops id128_hash_ops_free; sd_id128_t id128_make_v4_uuid(sd_id128_t id); int id128_get_product(sd_id128_t *ret); + +/* A helper to check for the three relevant cases of "machine ID not initialized" */ +#define ERRNO_IS_MACHINE_ID_UNSET(r) \ + IN_SET(abs(r), \ + ENOENT, \ + ENOMEDIUM, \ + ENOPKG) diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index a2111b69671..2ead29548b3 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -20,6 +20,7 @@ #include "fd-util.h" #include "format-util.h" #include "fs-util.h" +#include "id128-util.h" #include "journal-authenticate.h" #include "journal-def.h" #include "journal-file.h" @@ -388,11 +389,13 @@ static int journal_file_refresh_header(JournalFile *f) { assert(f->header); r = sd_id128_get_machine(&f->header->machine_id); - if (IN_SET(r, -ENOENT, -ENOMEDIUM, -ENOPKG)) - /* We don't have a machine-id, let's continue without */ - zero(f->header->machine_id); - else if (r < 0) - return r; + if (r < 0) { + if (!ERRNO_IS_MACHINE_ID_UNSET(r)) + return r; + + /* don't have a machine-id, let's continue without */ + f->header->machine_id = SD_ID128_NULL; + } r = sd_id128_get_boot(&f->header->boot_id); if (r < 0) diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index f16d9506eb3..3567d034e7b 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -2841,7 +2841,7 @@ static int setup_machine_id(const char *directory) { r = id128_read(etc_machine_id, ID128_FORMAT_PLAIN, &id); if (r < 0) { - if (!IN_SET(r, -ENOENT, -ENOMEDIUM, -ENOPKG)) /* If the file is missing, empty, or uninitialized, we don't mind */ + if (!ERRNO_IS_MACHINE_ID_UNSET(r)) /* If the file is missing, empty, or uninitialized, we don't mind */ return log_error_errno(r, "Failed to read machine ID from container image: %m"); if (sd_id128_is_null(arg_uuid)) { diff --git a/src/partition/repart.c b/src/partition/repart.c index 920b442316a..73712a36eb6 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -4789,10 +4789,12 @@ static int context_read_seed(Context *context, const char *root) { return log_error_errno(fd, "Failed to determine machine ID of image: %m"); else { r = id128_read_fd(fd, ID128_FORMAT_PLAIN, &context->seed); - if (IN_SET(r, -ENOMEDIUM, -ENOPKG)) + if (r < 0) { + if (!ERRNO_IS_MACHINE_ID_UNSET(r)) + return log_error_errno(r, "Failed to parse machine ID of image: %m"); + log_info("No machine ID set, using randomized partition UUIDs."); - else if (r < 0) - return log_error_errno(r, "Failed to parse machine ID of image: %m"); + } return 0; } diff --git a/src/test/test-condition.c b/src/test/test-condition.c index 901d80eb3d5..8f52dab8ce8 100644 --- a/src/test/test-condition.c +++ b/src/test/test-condition.c @@ -250,7 +250,7 @@ TEST(condition_test_host) { int r; r = sd_id128_get_machine(&id); - if (IN_SET(r, -ENOENT, -ENOMEDIUM, -ENOPKG)) + if (r < 0 && ERRNO_IS_MACHINE_ID_UNSET(r)) return (void) log_tests_skipped("/etc/machine-id missing"); assert_se(r >= 0);