static int verb_boot_id(int argc, char **argv, void *userdata) { sd_id128_t id; int r; if (sd_id128_is_null(arg_app)) r = sd_id128_get_boot(&id); else r = sd_id128_get_boot_app_specific(arg_app, &id); if (r < 0) return log_error_errno(r, "Failed to get %sboot-ID: %m", sd_id128_is_null(arg_app) ? "" : "app-specific "); return id128_pretty_print(id, arg_pretty); }
static int print_efi_option(uint16_t id, bool in_order) { _cleanup_free_ char *title = NULL; _cleanup_free_ char *path = NULL; sd_id128_t partition; bool active; int r = 0; r = efi_get_boot_option(id, &title, &partition, &path, &active); if (r < 0) return r; /* print only configured entries with partition information */ if (!path || sd_id128_is_null(partition)) return 0; efi_tilt_backslashes(path); printf(" Title: %s\n", strna(title)); printf(" ID: 0x%04X\n", id); printf(" Status: %sactive%s\n", active ? "" : "in", in_order ? ", boot-order" : ""); printf(" Partition: /dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n", SD_ID128_FORMAT_VAL(partition)); printf(" File: %s%s\n", special_glyph(TREE_RIGHT), path); printf("\n"); return 0; }
static int status_binaries(const char *esp_path, sd_id128_t partition) { int r; printf("Boot Loader Binaries:\n"); if (!esp_path) { printf(" ESP: Cannot find or access mount point of ESP.\n\n"); return -ENOENT; } printf(" ESP: %s", esp_path); if (!sd_id128_is_null(partition)) printf(" (/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x)", SD_ID128_FORMAT_VAL(partition)); printf("\n"); r = enumerate_binaries(esp_path, "EFI/systemd", NULL); if (r == 0) log_error("systemd-boot not installed in ESP."); else if (r < 0) return r; r = enumerate_binaries(esp_path, "EFI/BOOT", "boot"); if (r == 0) log_error("No default/fallback boot loader installed in ESP."); else if (r < 0) return r; printf("\n"); return 0; }
_public_ int sd_id128_get_machine(sd_id128_t *ret) { static thread_local sd_id128_t saved_machine_id = {}; int r; assert_return(ret, -EINVAL); if (sd_id128_is_null(saved_machine_id)) { r = id128_read("/etc/machine-id", ID128_PLAIN, &saved_machine_id); if (r < 0) return r; if (sd_id128_is_null(saved_machine_id)) return -EINVAL; } *ret = saved_machine_id; return 0; }
static int context_read_data(Context *c) { int r; struct utsname u; assert(c); context_reset(c); assert_se(uname(&u) >= 0); c->data[PROP_KERNEL_NAME] = strdup(u.sysname); c->data[PROP_KERNEL_RELEASE] = strdup(u.release); c->data[PROP_KERNEL_VERSION] = strdup(u.version); if (!c->data[PROP_KERNEL_NAME] || !c->data[PROP_KERNEL_RELEASE] || !c->data[PROP_KERNEL_VERSION]) return -ENOMEM; c->data[PROP_HOSTNAME] = gethostname_malloc(); if (!c->data[PROP_HOSTNAME]) return -ENOMEM; r = read_etc_hostname(NULL, &c->data[PROP_STATIC_HOSTNAME]); if (r < 0 && r != -ENOENT) return r; r = parse_env_file(NULL, "/etc/machine-info", "PRETTY_HOSTNAME", &c->data[PROP_PRETTY_HOSTNAME], "ICON_NAME", &c->data[PROP_ICON_NAME], "CHASSIS", &c->data[PROP_CHASSIS], "DEPLOYMENT", &c->data[PROP_DEPLOYMENT], "LOCATION", &c->data[PROP_LOCATION]); if (r < 0 && r != -ENOENT) return r; r = parse_os_release(NULL, "PRETTY_NAME", &c->data[PROP_OS_PRETTY_NAME], "CPE_NAME", &c->data[PROP_OS_CPE_NAME], "HOME_URL", &c->data[PROP_HOME_URL], NULL); if (r < 0 && r != -ENOENT) return r; r = id128_read("/sys/class/dmi/id/product_uuid", ID128_UUID, &c->uuid); if (r < 0) log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, "Failed to read product UUID, ignoring: %m"); else if (sd_id128_is_null(c->uuid) || sd_id128_is_allf(c->uuid)) log_debug("DMI product UUID " SD_ID128_FORMAT_STR " is all 0x00 or all 0xFF, ignoring.", SD_ID128_FORMAT_VAL(c->uuid)); else c->has_uuid = true; return 0; }
static int verb_invocation_id(int argc, char **argv, void *userdata) { sd_id128_t id; int r; if (!sd_id128_is_null(arg_app)) return log_error_errno(EINVAL, "Verb \"invocation-id\" cannot be combined with --app-specific=."); r = sd_id128_get_invocation(&id); if (r < 0) return log_error_errno(r, "Failed to get invocation-ID: %m"); return id128_pretty_print(id, arg_pretty); }
_public_ int sd_id128_get_boot(sd_id128_t *ret) { static thread_local sd_id128_t saved_boot_id = {}; int r; assert_return(ret, -EINVAL); if (sd_id128_is_null(saved_boot_id)) { r = id128_read("/proc/sys/kernel/random/boot_id", ID128_UUID, &saved_boot_id); if (r < 0) return r; } *ret = saved_boot_id; return 0; }
/* Is this a GUID? Then be nice, and skip over * the dashes */ if (i == 8) is_guid = true; else if (i == 13 || i == 18 || i == 23) { if (!is_guid) return -EINVAL; } else return -EINVAL; i++; continue; } a = unhexchar(s[i++]); if (a < 0) return -EINVAL; b = unhexchar(s[i++]); if (b < 0) return -EINVAL; t.bytes[n++] = (a << 4) | b; } if (i != (is_guid ? 36 : 32)) return -EINVAL; if (s[i] != 0) return -EINVAL; if (ret) *ret = t; return 0; } _public_ int sd_id128_get_machine(sd_id128_t *ret) { static thread_local sd_id128_t saved_machine_id = {}; int r; assert_return(ret, -EINVAL); if (sd_id128_is_null(saved_machine_id)) { r = id128_read("/etc/machine-id", ID128_PLAIN, &saved_machine_id); if (r < 0) return r; if (sd_id128_is_null(saved_machine_id)) return -EINVAL; } *ret = saved_machine_id; return 0; } _public_ int sd_id128_get_boot(sd_id128_t *ret) { static thread_local sd_id128_t saved_boot_id = {}; int r; assert_return(ret, -EINVAL); if (sd_id128_is_null(saved_boot_id)) { r = id128_read("/proc/sys/kernel/random/boot_id", ID128_UUID, &saved_boot_id); if (r < 0) return r; } *ret = saved_boot_id; return 0; } #if 0 /* NM_IGNORED */ _public_ int sd_id128_get_invocation(sd_id128_t *ret) { static thread_local sd_id128_t saved_invocation_id = {}; int r; assert_return(ret, -EINVAL); if (sd_id128_is_null(saved_invocation_id)) { const char *e; e = secure_getenv("INVOCATION_ID"); if (!e) return -ENXIO; r = sd_id128_from_string(e, &saved_invocation_id); if (r < 0) return r; } *ret = saved_invocation_id; return 0; }
static int process_machine_id(void) { const char *etc_machine_id; char id[SD_ID128_STRING_MAX]; int r; etc_machine_id = prefix_roota(arg_root, "/etc/machine-id"); if (laccess(etc_machine_id, F_OK) >= 0) return 0; if (sd_id128_is_null(arg_machine_id)) return 0; mkdir_parents(etc_machine_id, 0755); r = write_string_file(etc_machine_id, sd_id128_to_string(arg_machine_id, id), WRITE_STRING_FILE_CREATE | WRITE_STRING_FILE_SYNC); if (r < 0) return log_error_errno(r, "Failed to write machine id: %m"); log_info("%s written.", etc_machine_id); return 0; }
int main(int argc, char *argv[]) { _cleanup_(loop_device_unrefp) LoopDevice *d = NULL; _cleanup_(decrypted_image_unrefp) DecryptedImage *di = NULL; _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; int r; log_parse_environment(); log_open(); r = parse_argv(argc, argv); if (r <= 0) goto finish; r = loop_device_make_by_path(arg_image, (arg_flags & DISSECT_IMAGE_READ_ONLY) ? O_RDONLY : O_RDWR, &d); if (r < 0) { log_error_errno(r, "Failed to set up loopback device: %m"); goto finish; } if (!arg_root_hash) { r = root_hash_load(arg_image, &arg_root_hash, &arg_root_hash_size); if (r < 0) { log_error_errno(r, "Failed to read root hash file for %s: %m", arg_image); goto finish; } } r = dissect_image(d->fd, arg_root_hash, arg_root_hash_size, arg_flags, &m); if (r == -ENOPKG) { log_error_errno(r, "Couldn't identify a suitable partition table or file system in %s.", arg_image); goto finish; } if (r == -EADDRNOTAVAIL) { log_error_errno(r, "No root partition for specified root hash found in %s.", arg_image); goto finish; } if (r == -ENOTUNIQ) { log_error_errno(r, "Multiple suitable root partitions found in image %s.", arg_image); goto finish; } if (r == -ENXIO) { log_error_errno(r, "No suitable root partition found in image %s.", arg_image); goto finish; } if (r == -EPROTONOSUPPORT) { log_error_errno(r, "Device %s is loopback block device with partition scanning turned off, please turn it on.", arg_image); goto finish; } if (r < 0) { log_error_errno(r, "Failed to dissect image: %m"); goto finish; } switch (arg_action) { case ACTION_DISSECT: { unsigned i; for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { DissectedPartition *p = m->partitions + i; int k; if (!p->found) continue; printf("Found %s '%s' partition", p->rw ? "writable" : "read-only", partition_designator_to_string(i)); if (!sd_id128_is_null(p->uuid)) printf(" (UUID " SD_ID128_FORMAT_STR ")", SD_ID128_FORMAT_VAL(p->uuid)); if (p->fstype) printf(" of type %s", p->fstype); if (p->architecture != _ARCHITECTURE_INVALID) printf(" for %s", architecture_to_string(p->architecture)); k = PARTITION_VERITY_OF(i); if (k >= 0) printf(" %s verity", m->partitions[k].found ? "with" : "without"); if (p->partno >= 0) printf(" on partition #%i", p->partno); if (p->node) printf(" (%s)", p->node); putchar('\n'); } r = dissected_image_acquire_metadata(m); if (r < 0) { log_error_errno(r, "Failed to acquire image metadata: %m"); goto finish; } if (m->hostname) printf(" Hostname: %s\n", m->hostname); if (!sd_id128_is_null(m->machine_id)) printf("Machine ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(m->machine_id)); if (!strv_isempty(m->machine_info)) { char **p, **q; STRV_FOREACH_PAIR(p, q, m->machine_info) printf("%s %s=%s\n", p == m->machine_info ? "Mach. Info:" : " ", *p, *q); } if (!strv_isempty(m->os_release)) { char **p, **q; STRV_FOREACH_PAIR(p, q, m->os_release) printf("%s %s=%s\n", p == m->os_release ? "OS Release:" : " ", *p, *q); } break; } case ACTION_MOUNT: r = dissected_image_decrypt_interactively(m, NULL, arg_root_hash, arg_root_hash_size, arg_flags, &di); if (r < 0) goto finish; r = dissected_image_mount(m, arg_path, UID_INVALID, arg_flags); if (r < 0) { log_error_errno(r, "Failed to mount image: %m"); goto finish; } if (di) { r = decrypted_image_relinquish(di); if (r < 0) { log_error_errno(r, "Failed to relinquish DM devices: %m"); goto finish; } } loop_device_relinquish(d); break; default: assert_not_reached("Unknown action."); } finish: free(arg_root_hash); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }
int dissect_image(int fd, const void *root_hash, size_t root_hash_size, DissectedImage **ret) { #ifdef HAVE_BLKID sd_id128_t root_uuid = SD_ID128_NULL, verity_uuid = SD_ID128_NULL; _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL; bool is_gpt, is_mbr, generic_rw, multiple_generic = false; _cleanup_udev_device_unref_ struct udev_device *d = NULL; _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; _cleanup_blkid_free_probe_ blkid_probe b = NULL; _cleanup_udev_unref_ struct udev *udev = NULL; _cleanup_free_ char *generic_node = NULL; const char *pttype = NULL, *usage = NULL; struct udev_list_entry *first, *item; blkid_partlist pl; int r, generic_nr; struct stat st; unsigned i; assert(fd >= 0); assert(ret); assert(root_hash || root_hash_size == 0); /* Probes a disk image, and returns information about what it found in *ret. * * Returns -ENOPKG if no suitable partition table or file system could be found. * Returns -EADDRNOTAVAIL if a root hash was specified but no matching root/verity partitions found. */ if (root_hash) { /* If a root hash is supplied, then we use the root partition that has a UUID that match the first * 128bit of the root hash. And we use the verity partition that has a UUID that match the final * 128bit. */ if (root_hash_size < sizeof(sd_id128_t)) return -EINVAL; memcpy(&root_uuid, root_hash, sizeof(sd_id128_t)); memcpy(&verity_uuid, (const uint8_t*) root_hash + root_hash_size - sizeof(sd_id128_t), sizeof(sd_id128_t)); if (sd_id128_is_null(root_uuid)) return -EINVAL; if (sd_id128_is_null(verity_uuid)) return -EINVAL; } if (fstat(fd, &st) < 0) return -errno; if (!S_ISBLK(st.st_mode)) return -ENOTBLK; b = blkid_new_probe(); if (!b) return -ENOMEM; errno = 0; r = blkid_probe_set_device(b, fd, 0, 0); if (r != 0) { if (errno == 0) return -ENOMEM; return -errno; } blkid_probe_enable_superblocks(b, 1); blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_USAGE); blkid_probe_enable_partitions(b, 1); blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS); errno = 0; r = blkid_do_safeprobe(b); if (r == -2 || r == 1) { log_debug("Failed to identify any partition table."); return -ENOPKG; } if (r != 0) { if (errno == 0) return -EIO; return -errno; } m = new0(DissectedImage, 1); if (!m) return -ENOMEM; (void) blkid_probe_lookup_value(b, "USAGE", &usage, NULL); if (STRPTR_IN_SET(usage, "filesystem", "crypto")) { _cleanup_free_ char *t = NULL, *n = NULL; const char *fstype = NULL; /* OK, we have found a file system, that's our root partition then. */ (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); if (fstype) { t = strdup(fstype); if (!t) return -ENOMEM; } if (asprintf(&n, "/dev/block/%u:%u", major(st.st_rdev), minor(st.st_rdev)) < 0) return -ENOMEM; m->partitions[PARTITION_ROOT] = (DissectedPartition) { .found = true, .rw = true, .partno = -1, .architecture = _ARCHITECTURE_INVALID, .fstype = t, .node = n, }; t = n = NULL; m->encrypted = streq(fstype, "crypto_LUKS"); *ret = m; m = NULL; return 0; } (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL); if (!pttype) return -ENOPKG; is_gpt = streq_ptr(pttype, "gpt"); is_mbr = streq_ptr(pttype, "dos"); if (!is_gpt && !is_mbr) return -ENOPKG; errno = 0; pl = blkid_probe_get_partitions(b); if (!pl) { if (errno == 0) return -ENOMEM; return -errno; } udev = udev_new(); if (!udev) return -errno; d = udev_device_new_from_devnum(udev, 'b', st.st_rdev); if (!d) return -ENOMEM; for (i = 0;; i++) { int n, z; if (i >= 10) { log_debug("Kernel partitions never appeared."); return -ENXIO; } e = udev_enumerate_new(udev); if (!e) return -errno; r = udev_enumerate_add_match_parent(e, d); if (r < 0) return r; r = udev_enumerate_scan_devices(e); if (r < 0) return r; /* Count the partitions enumerated by the kernel */ n = 0; first = udev_enumerate_get_list_entry(e); udev_list_entry_foreach(item, first) n++; /* Count the partitions enumerated by blkid */ z = blkid_partlist_numof_partitions(pl); if (n == z + 1) break; if (n > z + 1) { log_debug("blkid and kernel partition list do not match."); return -EIO; } if (n < z + 1) { unsigned j; /* The kernel has probed fewer partitions than blkid? Maybe the kernel prober is still running * or it got EBUSY because udev already opened the device. Let's reprobe the device, which is a * synchronous call that waits until probing is complete. */ for (j = 0; j < 20; j++) { r = ioctl(fd, BLKRRPART, 0); if (r < 0) r = -errno; if (r >= 0 || r != -EBUSY) break; /* If something else has the device open, such as an udev rule, the ioctl will return * EBUSY. Since there's no way to wait until it isn't busy anymore, let's just wait a * bit, and try again. * * This is really something they should fix in the kernel! */ usleep(50 * USEC_PER_MSEC); } if (r < 0) return r; } e = udev_enumerate_unref(e); } first = udev_enumerate_get_list_entry(e); udev_list_entry_foreach(item, first) { _cleanup_udev_device_unref_ struct udev_device *q; unsigned long long flags; blkid_partition pp; const char *node; dev_t qn; int nr; q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)); if (!q) return -errno; qn = udev_device_get_devnum(q); if (major(qn) == 0) continue; if (st.st_rdev == qn) continue; node = udev_device_get_devnode(q); if (!node) continue; pp = blkid_partlist_devno_to_partition(pl, qn); if (!pp) continue; flags = blkid_partition_get_flags(pp); nr = blkid_partition_get_partno(pp); if (nr < 0) continue; if (is_gpt) { int designator = _PARTITION_DESIGNATOR_INVALID, architecture = _ARCHITECTURE_INVALID; const char *stype, *sid, *fstype = NULL; sd_id128_t type_id, id; bool rw = true; if (flags & GPT_FLAG_NO_AUTO) continue; sid = blkid_partition_get_uuid(pp); if (!sid) continue; if (sd_id128_from_string(sid, &id) < 0) continue; stype = blkid_partition_get_type_string(pp); if (!stype) continue; if (sd_id128_from_string(stype, &type_id) < 0) continue; if (sd_id128_equal(type_id, GPT_HOME)) { designator = PARTITION_HOME; rw = !(flags & GPT_FLAG_READ_ONLY); } else if (sd_id128_equal(type_id, GPT_SRV)) { designator = PARTITION_SRV; rw = !(flags & GPT_FLAG_READ_ONLY); } else if (sd_id128_equal(type_id, GPT_ESP)) { designator = PARTITION_ESP; fstype = "vfat"; } #ifdef GPT_ROOT_NATIVE else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE)) { /* If a root ID is specified, ignore everything but the root id */ if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id)) continue; designator = PARTITION_ROOT; architecture = native_architecture(); rw = !(flags & GPT_FLAG_READ_ONLY); } else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE_VERITY)) { m->can_verity = true; /* Ignore verity unless a root hash is specified */ if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id)) continue; designator = PARTITION_ROOT_VERITY; fstype = "DM_verity_hash"; architecture = native_architecture(); rw = false; } #endif #ifdef GPT_ROOT_SECONDARY else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY)) { /* If a root ID is specified, ignore everything but the root id */ if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id)) continue; designator = PARTITION_ROOT_SECONDARY; architecture = SECONDARY_ARCHITECTURE; rw = !(flags & GPT_FLAG_READ_ONLY); } else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY_VERITY)) { m->can_verity = true; /* Ignore verity unless root has is specified */ if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id)) continue; designator = PARTITION_ROOT_SECONDARY_VERITY; fstype = "DM_verity_hash"; architecture = SECONDARY_ARCHITECTURE; rw = false; } #endif else if (sd_id128_equal(type_id, GPT_SWAP)) { designator = PARTITION_SWAP; fstype = "swap"; } else if (sd_id128_equal(type_id, GPT_LINUX_GENERIC)) { if (generic_node) multiple_generic = true; else { generic_nr = nr; generic_rw = !(flags & GPT_FLAG_READ_ONLY); generic_node = strdup(node); if (!generic_node) return -ENOMEM; } } if (designator != _PARTITION_DESIGNATOR_INVALID) { _cleanup_free_ char *t = NULL, *n = NULL; /* First one wins */ if (m->partitions[designator].found) continue; if (fstype) { t = strdup(fstype); if (!t) return -ENOMEM; } n = strdup(node); if (!n) return -ENOMEM; m->partitions[designator] = (DissectedPartition) { .found = true, .partno = nr, .rw = rw, .architecture = architecture, .node = n, .fstype = t, }; n = t = NULL; } } else if (is_mbr) {