]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dissect-image.c
Merge pull request #11770 from yuwata/fix-9955
[thirdparty/systemd.git] / src / shared / dissect-image.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <sys/mount.h>
4 #include <sys/prctl.h>
5 #include <sys/wait.h>
6
7 #include "sd-device.h"
8 #include "sd-id128.h"
9
10 #include "architecture.h"
11 #include "ask-password-api.h"
12 #include "blkid-util.h"
13 #include "blockdev-util.h"
14 #include "copy.h"
15 #include "crypt-util.h"
16 #include "def.h"
17 #include "device-nodes.h"
18 #include "device-util.h"
19 #include "dissect-image.h"
20 #include "env-file.h"
21 #include "fd-util.h"
22 #include "fileio.h"
23 #include "fs-util.h"
24 #include "gpt.h"
25 #include "hexdecoct.h"
26 #include "hostname-util.h"
27 #include "id128-util.h"
28 #include "linux-3.13/dm-ioctl.h"
29 #include "missing.h"
30 #include "mount-util.h"
31 #include "mountpoint-util.h"
32 #include "os-util.h"
33 #include "path-util.h"
34 #include "process-util.h"
35 #include "raw-clone.h"
36 #include "signal-util.h"
37 #include "stat-util.h"
38 #include "stdio-util.h"
39 #include "string-table.h"
40 #include "string-util.h"
41 #include "strv.h"
42 #include "tmpfile-util.h"
43 #include "udev-util.h"
44 #include "user-util.h"
45 #include "xattr-util.h"
46
47 int probe_filesystem(const char *node, char **ret_fstype) {
48 /* Try to find device content type and return it in *ret_fstype. If nothing is found,
49 * 0/NULL will be returned. -EUCLEAN will be returned for ambigous results, and an
50 * different error otherwise. */
51
52 #if HAVE_BLKID
53 _cleanup_(blkid_free_probep) blkid_probe b = NULL;
54 const char *fstype;
55 int r;
56
57 errno = 0;
58 b = blkid_new_probe_from_filename(node);
59 if (!b)
60 return -errno ?: -ENOMEM;
61
62 blkid_probe_enable_superblocks(b, 1);
63 blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
64
65 errno = 0;
66 r = blkid_do_safeprobe(b);
67 if (r == 1) {
68 log_debug("No type detected on partition %s", node);
69 goto not_found;
70 }
71 if (r == -2) {
72 log_debug("Results ambiguous for partition %s", node);
73 return -EUCLEAN;
74 }
75 if (r != 0)
76 return -errno ?: -EIO;
77
78 (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
79
80 if (fstype) {
81 char *t;
82
83 t = strdup(fstype);
84 if (!t)
85 return -ENOMEM;
86
87 *ret_fstype = t;
88 return 1;
89 }
90
91 not_found:
92 *ret_fstype = NULL;
93 return 0;
94 #else
95 return -EOPNOTSUPP;
96 #endif
97 }
98
99 #if HAVE_BLKID
100 /* Detect RPMB and Boot partitions, which are not listed by blkid.
101 * See https://github.com/systemd/systemd/issues/5806. */
102 static bool device_is_mmc_special_partition(sd_device *d) {
103 const char *sysname;
104
105 assert(d);
106
107 if (sd_device_get_sysname(d, &sysname) < 0)
108 return false;
109
110 return startswith(sysname, "mmcblk") &&
111 (endswith(sysname, "rpmb") || endswith(sysname, "boot0") || endswith(sysname, "boot1"));
112 }
113
114 static bool device_is_block(sd_device *d) {
115 const char *ss;
116
117 assert(d);
118
119 if (sd_device_get_subsystem(d, &ss) < 0)
120 return false;
121
122 return streq(ss, "block");
123 }
124
125 static int enumerator_for_parent(sd_device *d, sd_device_enumerator **ret) {
126 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
127 int r;
128
129 assert(d);
130 assert(ret);
131
132 r = sd_device_enumerator_new(&e);
133 if (r < 0)
134 return r;
135
136 r = sd_device_enumerator_allow_uninitialized(e);
137 if (r < 0)
138 return r;
139
140 r = sd_device_enumerator_add_match_parent(e, d);
141 if (r < 0)
142 return r;
143
144 *ret = TAKE_PTR(e);
145 return 0;
146 }
147
148 /* how many times to wait for the device nodes to appear */
149 #define N_DEVICE_NODE_LIST_ATTEMPTS 10
150
151 static int wait_for_partitions_to_appear(
152 int fd,
153 sd_device *d,
154 unsigned num_partitions,
155 DissectImageFlags flags,
156 sd_device_enumerator **ret_enumerator) {
157
158 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
159 sd_device *q;
160 unsigned n;
161 int r;
162
163 assert(fd >= 0);
164 assert(d);
165 assert(ret_enumerator);
166
167 r = enumerator_for_parent(d, &e);
168 if (r < 0)
169 return r;
170
171 /* Count the partitions enumerated by the kernel */
172 n = 0;
173 FOREACH_DEVICE(e, q) {
174 if (sd_device_get_devnum(q, NULL) < 0)
175 continue;
176 if (!device_is_block(q))
177 continue;
178 if (device_is_mmc_special_partition(q))
179 continue;
180
181 if (!FLAGS_SET(flags, DISSECT_IMAGE_NO_UDEV)) {
182 r = device_wait_for_initialization(q, "block", NULL);
183 if (r < 0)
184 return r;
185 }
186
187 n++;
188 }
189
190 if (n == num_partitions + 1) {
191 *ret_enumerator = TAKE_PTR(e);
192 return 0; /* success! */
193 }
194 if (n > num_partitions + 1)
195 return log_debug_errno(SYNTHETIC_ERRNO(EIO),
196 "blkid and kernel partition lists do not match.");
197
198 /* The kernel has probed fewer partitions than blkid? Maybe the kernel prober is still running or it
199 * got EBUSY because udev already opened the device. Let's reprobe the device, which is a synchronous
200 * call that waits until probing is complete. */
201
202 for (unsigned j = 0; ; j++) {
203 if (j++ > 20)
204 return -EBUSY;
205
206 if (ioctl(fd, BLKRRPART, 0) >= 0)
207 break;
208 r = -errno;
209 if (r == -EINVAL) {
210 struct loop_info64 info;
211
212 /* If we are running on a loop device that has partition scanning off, return
213 * an explicit recognizable error about this, so that callers can generate a
214 * proper message explaining the situation. */
215
216 if (ioctl(fd, LOOP_GET_STATUS64, &info) >= 0 && (info.lo_flags & LO_FLAGS_PARTSCAN) == 0) {
217 log_debug("Device is a loop device and partition scanning is off!");
218 return -EPROTONOSUPPORT;
219 }
220 }
221 if (r != -EBUSY)
222 return r;
223
224 /* If something else has the device open, such as an udev rule, the ioctl will return
225 * EBUSY. Since there's no way to wait until it isn't busy anymore, let's just wait a bit,
226 * and try again.
227 *
228 * This is really something they should fix in the kernel! */
229 (void) usleep(50 * USEC_PER_MSEC);
230
231 }
232
233 return -EAGAIN; /* no success yet, try again */
234 }
235
236 static int loop_wait_for_partitions_to_appear(
237 int fd,
238 sd_device *d,
239 unsigned num_partitions,
240 DissectImageFlags flags,
241 sd_device_enumerator **ret_enumerator) {
242 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
243 int r;
244
245 assert(fd >= 0);
246 assert(d);
247 assert(ret_enumerator);
248
249 log_debug("Waiting for device (parent + %d partitions) to appear...", num_partitions);
250
251 if (!FLAGS_SET(flags, DISSECT_IMAGE_NO_UDEV)) {
252 r = device_wait_for_initialization(d, "block", &device);
253 if (r < 0)
254 return r;
255 } else
256 device = sd_device_ref(d);
257
258 for (unsigned i = 0; i < N_DEVICE_NODE_LIST_ATTEMPTS; i++) {
259 r = wait_for_partitions_to_appear(fd, device, num_partitions, flags, ret_enumerator);
260 if (r != -EAGAIN)
261 return r;
262 }
263
264 return log_debug_errno(SYNTHETIC_ERRNO(ENXIO),
265 "Kernel partitions dit not appear within %d attempts",
266 N_DEVICE_NODE_LIST_ATTEMPTS);
267 }
268
269 #endif
270
271 int dissect_image(
272 int fd,
273 const void *root_hash,
274 size_t root_hash_size,
275 DissectImageFlags flags,
276 DissectedImage **ret) {
277
278 #if HAVE_BLKID
279 sd_id128_t root_uuid = SD_ID128_NULL, verity_uuid = SD_ID128_NULL;
280 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
281 bool is_gpt, is_mbr, generic_rw, multiple_generic = false;
282 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
283 _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
284 _cleanup_(blkid_free_probep) blkid_probe b = NULL;
285 _cleanup_free_ char *generic_node = NULL;
286 sd_id128_t generic_uuid = SD_ID128_NULL;
287 const char *pttype = NULL;
288 blkid_partlist pl;
289 int r, generic_nr;
290 struct stat st;
291 sd_device *q;
292 unsigned i;
293
294 assert(fd >= 0);
295 assert(ret);
296 assert(root_hash || root_hash_size == 0);
297
298 /* Probes a disk image, and returns information about what it found in *ret.
299 *
300 * Returns -ENOPKG if no suitable partition table or file system could be found.
301 * Returns -EADDRNOTAVAIL if a root hash was specified but no matching root/verity partitions found. */
302
303 if (root_hash) {
304 /* If a root hash is supplied, then we use the root partition that has a UUID that match the first
305 * 128bit of the root hash. And we use the verity partition that has a UUID that match the final
306 * 128bit. */
307
308 if (root_hash_size < sizeof(sd_id128_t))
309 return -EINVAL;
310
311 memcpy(&root_uuid, root_hash, sizeof(sd_id128_t));
312 memcpy(&verity_uuid, (const uint8_t*) root_hash + root_hash_size - sizeof(sd_id128_t), sizeof(sd_id128_t));
313
314 if (sd_id128_is_null(root_uuid))
315 return -EINVAL;
316 if (sd_id128_is_null(verity_uuid))
317 return -EINVAL;
318 }
319
320 if (fstat(fd, &st) < 0)
321 return -errno;
322
323 if (!S_ISBLK(st.st_mode))
324 return -ENOTBLK;
325
326 b = blkid_new_probe();
327 if (!b)
328 return -ENOMEM;
329
330 errno = 0;
331 r = blkid_probe_set_device(b, fd, 0, 0);
332 if (r != 0)
333 return -errno ?: -ENOMEM;
334
335 if ((flags & DISSECT_IMAGE_GPT_ONLY) == 0) {
336 /* Look for file system superblocks, unless we only shall look for GPT partition tables */
337 blkid_probe_enable_superblocks(b, 1);
338 blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_USAGE);
339 }
340
341 blkid_probe_enable_partitions(b, 1);
342 blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
343
344 errno = 0;
345 r = blkid_do_safeprobe(b);
346 if (IN_SET(r, -2, 1))
347 return log_debug_errno(SYNTHETIC_ERRNO(ENOPKG), "Failed to identify any partition table.");
348 if (r != 0)
349 return -errno ?: -EIO;
350
351 m = new0(DissectedImage, 1);
352 if (!m)
353 return -ENOMEM;
354
355 r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
356 if (r < 0)
357 return r;
358
359 if (!(flags & DISSECT_IMAGE_GPT_ONLY) &&
360 (flags & DISSECT_IMAGE_REQUIRE_ROOT)) {
361 const char *usage = NULL;
362
363 (void) blkid_probe_lookup_value(b, "USAGE", &usage, NULL);
364 if (STRPTR_IN_SET(usage, "filesystem", "crypto")) {
365 _cleanup_free_ char *t = NULL, *n = NULL;
366 const char *fstype = NULL;
367
368 /* OK, we have found a file system, that's our root partition then. */
369 (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
370
371 if (fstype) {
372 t = strdup(fstype);
373 if (!t)
374 return -ENOMEM;
375 }
376
377 r = device_path_make_major_minor(st.st_mode, st.st_rdev, &n);
378 if (r < 0)
379 return r;
380
381 m->partitions[PARTITION_ROOT] = (DissectedPartition) {
382 .found = true,
383 .rw = true,
384 .partno = -1,
385 .architecture = _ARCHITECTURE_INVALID,
386 .fstype = TAKE_PTR(t),
387 .node = TAKE_PTR(n),
388 };
389
390 m->encrypted = streq_ptr(fstype, "crypto_LUKS");
391
392 r = loop_wait_for_partitions_to_appear(fd, d, 0, flags, &e);
393 if (r < 0)
394 return r;
395
396 *ret = TAKE_PTR(m);
397
398 return 0;
399 }
400 }
401
402 (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL);
403 if (!pttype)
404 return -ENOPKG;
405
406 is_gpt = streq_ptr(pttype, "gpt");
407 is_mbr = streq_ptr(pttype, "dos");
408
409 if (!is_gpt && ((flags & DISSECT_IMAGE_GPT_ONLY) || !is_mbr))
410 return -ENOPKG;
411
412 errno = 0;
413 pl = blkid_probe_get_partitions(b);
414 if (!pl)
415 return -errno ?: -ENOMEM;
416
417 r = loop_wait_for_partitions_to_appear(fd, d, blkid_partlist_numof_partitions(pl), flags, &e);
418 if (r < 0)
419 return r;
420
421 FOREACH_DEVICE(e, q) {
422 unsigned long long pflags;
423 blkid_partition pp;
424 const char *node;
425 dev_t qn;
426 int nr;
427
428 r = sd_device_get_devnum(q, &qn);
429 if (r < 0)
430 continue;
431
432 if (st.st_rdev == qn)
433 continue;
434
435 if (!device_is_block(q))
436 continue;
437
438 if (device_is_mmc_special_partition(q))
439 continue;
440
441 r = sd_device_get_devname(q, &node);
442 if (r < 0)
443 continue;
444
445 pp = blkid_partlist_devno_to_partition(pl, qn);
446 if (!pp)
447 continue;
448
449 pflags = blkid_partition_get_flags(pp);
450
451 nr = blkid_partition_get_partno(pp);
452 if (nr < 0)
453 continue;
454
455 if (is_gpt) {
456 int designator = _PARTITION_DESIGNATOR_INVALID, architecture = _ARCHITECTURE_INVALID;
457 const char *stype, *sid, *fstype = NULL;
458 sd_id128_t type_id, id;
459 bool rw = true;
460
461 sid = blkid_partition_get_uuid(pp);
462 if (!sid)
463 continue;
464 if (sd_id128_from_string(sid, &id) < 0)
465 continue;
466
467 stype = blkid_partition_get_type_string(pp);
468 if (!stype)
469 continue;
470 if (sd_id128_from_string(stype, &type_id) < 0)
471 continue;
472
473 if (sd_id128_equal(type_id, GPT_HOME)) {
474
475 if (pflags & GPT_FLAG_NO_AUTO)
476 continue;
477
478 designator = PARTITION_HOME;
479 rw = !(pflags & GPT_FLAG_READ_ONLY);
480 } else if (sd_id128_equal(type_id, GPT_SRV)) {
481
482 if (pflags & GPT_FLAG_NO_AUTO)
483 continue;
484
485 designator = PARTITION_SRV;
486 rw = !(pflags & GPT_FLAG_READ_ONLY);
487 } else if (sd_id128_equal(type_id, GPT_ESP)) {
488
489 /* Note that we don't check the GPT_FLAG_NO_AUTO flag for the ESP, as it is not defined
490 * there. We instead check the GPT_FLAG_NO_BLOCK_IO_PROTOCOL, as recommended by the
491 * UEFI spec (See "12.3.3 Number and Location of System Partitions"). */
492
493 if (pflags & GPT_FLAG_NO_BLOCK_IO_PROTOCOL)
494 continue;
495
496 designator = PARTITION_ESP;
497 fstype = "vfat";
498
499 } else if (sd_id128_equal(type_id, GPT_XBOOTLDR)) {
500
501 if (pflags & GPT_FLAG_NO_AUTO)
502 continue;
503
504 designator = PARTITION_XBOOTLDR;
505 rw = !(pflags & GPT_FLAG_READ_ONLY);
506 }
507 #ifdef GPT_ROOT_NATIVE
508 else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE)) {
509
510 if (pflags & GPT_FLAG_NO_AUTO)
511 continue;
512
513 /* If a root ID is specified, ignore everything but the root id */
514 if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id))
515 continue;
516
517 designator = PARTITION_ROOT;
518 architecture = native_architecture();
519 rw = !(pflags & GPT_FLAG_READ_ONLY);
520 } else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE_VERITY)) {
521
522 if (pflags & GPT_FLAG_NO_AUTO)
523 continue;
524
525 m->can_verity = true;
526
527 /* Ignore verity unless a root hash is specified */
528 if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id))
529 continue;
530
531 designator = PARTITION_ROOT_VERITY;
532 fstype = "DM_verity_hash";
533 architecture = native_architecture();
534 rw = false;
535 }
536 #endif
537 #ifdef GPT_ROOT_SECONDARY
538 else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY)) {
539
540 if (pflags & GPT_FLAG_NO_AUTO)
541 continue;
542
543 /* If a root ID is specified, ignore everything but the root id */
544 if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id))
545 continue;
546
547 designator = PARTITION_ROOT_SECONDARY;
548 architecture = SECONDARY_ARCHITECTURE;
549 rw = !(pflags & GPT_FLAG_READ_ONLY);
550 } else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY_VERITY)) {
551
552 if (pflags & GPT_FLAG_NO_AUTO)
553 continue;
554
555 m->can_verity = true;
556
557 /* Ignore verity unless root has is specified */
558 if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id))
559 continue;
560
561 designator = PARTITION_ROOT_SECONDARY_VERITY;
562 fstype = "DM_verity_hash";
563 architecture = SECONDARY_ARCHITECTURE;
564 rw = false;
565 }
566 #endif
567 else if (sd_id128_equal(type_id, GPT_SWAP)) {
568
569 if (pflags & GPT_FLAG_NO_AUTO)
570 continue;
571
572 designator = PARTITION_SWAP;
573 fstype = "swap";
574 } else if (sd_id128_equal(type_id, GPT_LINUX_GENERIC)) {
575
576 if (pflags & GPT_FLAG_NO_AUTO)
577 continue;
578
579 if (generic_node)
580 multiple_generic = true;
581 else {
582 generic_nr = nr;
583 generic_rw = !(pflags & GPT_FLAG_READ_ONLY);
584 generic_uuid = id;
585 generic_node = strdup(node);
586 if (!generic_node)
587 return -ENOMEM;
588 }
589 }
590
591 if (designator != _PARTITION_DESIGNATOR_INVALID) {
592 _cleanup_free_ char *t = NULL, *n = NULL;
593
594 /* First one wins */
595 if (m->partitions[designator].found)
596 continue;
597
598 if (fstype) {
599 t = strdup(fstype);
600 if (!t)
601 return -ENOMEM;
602 }
603
604 n = strdup(node);
605 if (!n)
606 return -ENOMEM;
607
608 m->partitions[designator] = (DissectedPartition) {
609 .found = true,
610 .partno = nr,
611 .rw = rw,
612 .architecture = architecture,
613 .node = TAKE_PTR(n),
614 .fstype = TAKE_PTR(t),
615 .uuid = id,
616 };
617 }
618
619 } else if (is_mbr) {
620
621 switch (blkid_partition_get_type(pp)) {
622
623 case 0x83: /* Linux partition */
624
625 if (pflags != 0x80) /* Bootable flag */
626 continue;
627
628 if (generic_node)
629 multiple_generic = true;
630 else {
631 generic_nr = nr;
632 generic_rw = true;
633 generic_node = strdup(node);
634 if (!generic_node)
635 return -ENOMEM;
636 }
637
638 break;
639
640 case 0xEA: { /* Boot Loader Spec extended $BOOT partition */
641 _cleanup_free_ char *n = NULL;
642 sd_id128_t id = SD_ID128_NULL;
643 const char *sid;
644
645 /* First one wins */
646 if (m->partitions[PARTITION_XBOOTLDR].found)
647 continue;
648
649 sid = blkid_partition_get_uuid(pp);
650 if (sid)
651 (void) sd_id128_from_string(sid, &id);
652
653 n = strdup(node);
654 if (!n)
655 return -ENOMEM;
656
657 m->partitions[PARTITION_XBOOTLDR] = (DissectedPartition) {
658 .found = true,
659 .partno = nr,
660 .rw = true,
661 .architecture = _ARCHITECTURE_INVALID,
662 .node = TAKE_PTR(n),
663 .uuid = id,
664 };
665
666 break;
667 }}
668 }
669 }
670
671 if (!m->partitions[PARTITION_ROOT].found) {
672 /* No root partition found? Then let's see if ther's one for the secondary architecture. And if not
673 * either, then check if there's a single generic one, and use that. */
674
675 if (m->partitions[PARTITION_ROOT_VERITY].found)
676 return -EADDRNOTAVAIL;
677
678 if (m->partitions[PARTITION_ROOT_SECONDARY].found) {
679 m->partitions[PARTITION_ROOT] = m->partitions[PARTITION_ROOT_SECONDARY];
680 zero(m->partitions[PARTITION_ROOT_SECONDARY]);
681
682 m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY];
683 zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY]);
684
685 } else if (flags & DISSECT_IMAGE_REQUIRE_ROOT) {
686
687 /* If the root has was set, then we won't fallback to a generic node, because the root hash
688 * decides */
689 if (root_hash)
690 return -EADDRNOTAVAIL;
691
692 /* If we didn't find a generic node, then we can't fix this up either */
693 if (!generic_node)
694 return -ENXIO;
695
696 /* If we didn't find a properly marked root partition, but we did find a single suitable
697 * generic Linux partition, then use this as root partition, if the caller asked for it. */
698 if (multiple_generic)
699 return -ENOTUNIQ;
700
701 m->partitions[PARTITION_ROOT] = (DissectedPartition) {
702 .found = true,
703 .rw = generic_rw,
704 .partno = generic_nr,
705 .architecture = _ARCHITECTURE_INVALID,
706 .node = TAKE_PTR(generic_node),
707 .uuid = generic_uuid,
708 };
709 }
710 }
711
712 if (root_hash) {
713 if (!m->partitions[PARTITION_ROOT_VERITY].found || !m->partitions[PARTITION_ROOT].found)
714 return -EADDRNOTAVAIL;
715
716 /* If we found the primary root with the hash, then we definitely want to suppress any secondary root
717 * (which would be weird, after all the root hash should only be assigned to one pair of
718 * partitions... */
719 m->partitions[PARTITION_ROOT_SECONDARY].found = false;
720 m->partitions[PARTITION_ROOT_SECONDARY_VERITY].found = false;
721
722 /* If we found a verity setup, then the root partition is necessarily read-only. */
723 m->partitions[PARTITION_ROOT].rw = false;
724
725 m->verity = true;
726 }
727
728 blkid_free_probe(b);
729 b = NULL;
730
731 /* Fill in file system types if we don't know them yet. */
732 for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
733 DissectedPartition *p = m->partitions + i;
734
735 if (!p->found)
736 continue;
737
738 if (!p->fstype && p->node) {
739 r = probe_filesystem(p->node, &p->fstype);
740 if (r < 0 && r != -EUCLEAN)
741 return r;
742 }
743
744 if (streq_ptr(p->fstype, "crypto_LUKS"))
745 m->encrypted = true;
746
747 if (p->fstype && fstype_is_ro(p->fstype))
748 p->rw = false;
749 }
750
751 *ret = TAKE_PTR(m);
752
753 return 0;
754 #else
755 return -EOPNOTSUPP;
756 #endif
757 }
758
759 DissectedImage* dissected_image_unref(DissectedImage *m) {
760 unsigned i;
761
762 if (!m)
763 return NULL;
764
765 for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
766 free(m->partitions[i].fstype);
767 free(m->partitions[i].node);
768 free(m->partitions[i].decrypted_fstype);
769 free(m->partitions[i].decrypted_node);
770 }
771
772 free(m->hostname);
773 strv_free(m->machine_info);
774 strv_free(m->os_release);
775
776 return mfree(m);
777 }
778
779 static int is_loop_device(const char *path) {
780 char s[SYS_BLOCK_PATH_MAX("/../loop/")];
781 struct stat st;
782
783 assert(path);
784
785 if (stat(path, &st) < 0)
786 return -errno;
787
788 if (!S_ISBLK(st.st_mode))
789 return -ENOTBLK;
790
791 xsprintf_sys_block_path(s, "/loop/", st.st_dev);
792 if (access(s, F_OK) < 0) {
793 if (errno != ENOENT)
794 return -errno;
795
796 /* The device itself isn't a loop device, but maybe it's a partition and its parent is? */
797 xsprintf_sys_block_path(s, "/../loop/", st.st_dev);
798 if (access(s, F_OK) < 0)
799 return errno == ENOENT ? false : -errno;
800 }
801
802 return true;
803 }
804
805 static int mount_partition(
806 DissectedPartition *m,
807 const char *where,
808 const char *directory,
809 uid_t uid_shift,
810 DissectImageFlags flags) {
811
812 _cleanup_free_ char *chased = NULL, *options = NULL;
813 const char *p, *node, *fstype;
814 bool rw;
815 int r;
816
817 assert(m);
818 assert(where);
819
820 node = m->decrypted_node ?: m->node;
821 fstype = m->decrypted_fstype ?: m->fstype;
822
823 if (!m->found || !node || !fstype)
824 return 0;
825
826 /* Stacked encryption? Yuck */
827 if (streq_ptr(fstype, "crypto_LUKS"))
828 return -ELOOP;
829
830 rw = m->rw && !(flags & DISSECT_IMAGE_READ_ONLY);
831
832 if (directory) {
833 r = chase_symlinks(directory, where, CHASE_PREFIX_ROOT, &chased);
834 if (r < 0)
835 return r;
836
837 p = chased;
838 } else
839 p = where;
840
841 /* If requested, turn on discard support. */
842 if (fstype_can_discard(fstype) &&
843 ((flags & DISSECT_IMAGE_DISCARD) ||
844 ((flags & DISSECT_IMAGE_DISCARD_ON_LOOP) && is_loop_device(m->node)))) {
845 options = strdup("discard");
846 if (!options)
847 return -ENOMEM;
848 }
849
850 if (uid_is_valid(uid_shift) && uid_shift != 0 && fstype_can_uid_gid(fstype)) {
851 _cleanup_free_ char *uid_option = NULL;
852
853 if (asprintf(&uid_option, "uid=" UID_FMT ",gid=" GID_FMT, uid_shift, (gid_t) uid_shift) < 0)
854 return -ENOMEM;
855
856 if (!strextend_with_separator(&options, ",", uid_option, NULL))
857 return -ENOMEM;
858 }
859
860 r = mount_verbose(LOG_DEBUG, node, p, fstype, MS_NODEV|(rw ? 0 : MS_RDONLY), options);
861 if (r < 0)
862 return r;
863
864 return 1;
865 }
866
867 int dissected_image_mount(DissectedImage *m, const char *where, uid_t uid_shift, DissectImageFlags flags) {
868 int r, boot_mounted;
869
870 assert(m);
871 assert(where);
872
873 if (!m->partitions[PARTITION_ROOT].found)
874 return -ENXIO;
875
876 if ((flags & DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY) == 0) {
877 r = mount_partition(m->partitions + PARTITION_ROOT, where, NULL, uid_shift, flags);
878 if (r < 0)
879 return r;
880
881 if (flags & DISSECT_IMAGE_VALIDATE_OS) {
882 r = path_is_os_tree(where);
883 if (r < 0)
884 return r;
885 if (r == 0)
886 return -EMEDIUMTYPE;
887 }
888 }
889
890 if (flags & DISSECT_IMAGE_MOUNT_ROOT_ONLY)
891 return 0;
892
893 r = mount_partition(m->partitions + PARTITION_HOME, where, "/home", uid_shift, flags);
894 if (r < 0)
895 return r;
896
897 r = mount_partition(m->partitions + PARTITION_SRV, where, "/srv", uid_shift, flags);
898 if (r < 0)
899 return r;
900
901 boot_mounted = mount_partition(m->partitions + PARTITION_XBOOTLDR, where, "/boot", uid_shift, flags);
902 if (boot_mounted < 0)
903 return boot_mounted;
904
905 if (m->partitions[PARTITION_ESP].found) {
906 /* Mount the ESP to /efi if it exists. If it doesn't exist, use /boot instead, but only if it
907 * exists and is empty, and we didn't already mount the XBOOTLDR partition into it. */
908
909 r = chase_symlinks("/efi", where, CHASE_PREFIX_ROOT, NULL);
910 if (r >= 0) {
911 r = mount_partition(m->partitions + PARTITION_ESP, where, "/efi", uid_shift, flags);
912 if (r < 0)
913 return r;
914
915 } else if (boot_mounted <= 0) {
916 _cleanup_free_ char *p = NULL;
917
918 r = chase_symlinks("/boot", where, CHASE_PREFIX_ROOT, &p);
919 if (r >= 0 && dir_is_empty(p) > 0) {
920 r = mount_partition(m->partitions + PARTITION_ESP, where, "/boot", uid_shift, flags);
921 if (r < 0)
922 return r;
923 }
924 }
925 }
926
927 return 0;
928 }
929
930 #if HAVE_LIBCRYPTSETUP
931 typedef struct DecryptedPartition {
932 struct crypt_device *device;
933 char *name;
934 bool relinquished;
935 } DecryptedPartition;
936
937 struct DecryptedImage {
938 DecryptedPartition *decrypted;
939 size_t n_decrypted;
940 size_t n_allocated;
941 };
942 #endif
943
944 DecryptedImage* decrypted_image_unref(DecryptedImage* d) {
945 #if HAVE_LIBCRYPTSETUP
946 size_t i;
947 int r;
948
949 if (!d)
950 return NULL;
951
952 for (i = 0; i < d->n_decrypted; i++) {
953 DecryptedPartition *p = d->decrypted + i;
954
955 if (p->device && p->name && !p->relinquished) {
956 r = crypt_deactivate(p->device, p->name);
957 if (r < 0)
958 log_debug_errno(r, "Failed to deactivate encrypted partition %s", p->name);
959 }
960
961 if (p->device)
962 crypt_free(p->device);
963 free(p->name);
964 }
965
966 free(d);
967 #endif
968 return NULL;
969 }
970
971 #if HAVE_LIBCRYPTSETUP
972
973 static int make_dm_name_and_node(const void *original_node, const char *suffix, char **ret_name, char **ret_node) {
974 _cleanup_free_ char *name = NULL, *node = NULL;
975 const char *base;
976
977 assert(original_node);
978 assert(suffix);
979 assert(ret_name);
980 assert(ret_node);
981
982 base = strrchr(original_node, '/');
983 if (!base)
984 return -EINVAL;
985 base++;
986 if (isempty(base))
987 return -EINVAL;
988
989 name = strjoin(base, suffix);
990 if (!name)
991 return -ENOMEM;
992 if (!filename_is_valid(name))
993 return -EINVAL;
994
995 node = strjoin(crypt_get_dir(), "/", name);
996 if (!node)
997 return -ENOMEM;
998
999 *ret_name = TAKE_PTR(name);
1000 *ret_node = TAKE_PTR(node);
1001
1002 return 0;
1003 }
1004
1005 static int decrypt_partition(
1006 DissectedPartition *m,
1007 const char *passphrase,
1008 DissectImageFlags flags,
1009 DecryptedImage *d) {
1010
1011 _cleanup_free_ char *node = NULL, *name = NULL;
1012 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
1013 int r;
1014
1015 assert(m);
1016 assert(d);
1017
1018 if (!m->found || !m->node || !m->fstype)
1019 return 0;
1020
1021 if (!streq(m->fstype, "crypto_LUKS"))
1022 return 0;
1023
1024 if (!passphrase)
1025 return -ENOKEY;
1026
1027 r = make_dm_name_and_node(m->node, "-decrypted", &name, &node);
1028 if (r < 0)
1029 return r;
1030
1031 if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1))
1032 return -ENOMEM;
1033
1034 r = crypt_init(&cd, m->node);
1035 if (r < 0)
1036 return log_debug_errno(r, "Failed to initialize dm-crypt: %m");
1037
1038 r = crypt_load(cd, CRYPT_LUKS, NULL);
1039 if (r < 0)
1040 return log_debug_errno(r, "Failed to load LUKS metadata: %m");
1041
1042 r = crypt_activate_by_passphrase(cd, name, CRYPT_ANY_SLOT, passphrase, strlen(passphrase),
1043 ((flags & DISSECT_IMAGE_READ_ONLY) ? CRYPT_ACTIVATE_READONLY : 0) |
1044 ((flags & DISSECT_IMAGE_DISCARD_ON_CRYPTO) ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0));
1045 if (r < 0) {
1046 log_debug_errno(r, "Failed to activate LUKS device: %m");
1047 return r == -EPERM ? -EKEYREJECTED : r;
1048 }
1049
1050 d->decrypted[d->n_decrypted].name = TAKE_PTR(name);
1051 d->decrypted[d->n_decrypted].device = TAKE_PTR(cd);
1052 d->n_decrypted++;
1053
1054 m->decrypted_node = TAKE_PTR(node);
1055
1056 return 0;
1057 }
1058
1059 static int verity_partition(
1060 DissectedPartition *m,
1061 DissectedPartition *v,
1062 const void *root_hash,
1063 size_t root_hash_size,
1064 DissectImageFlags flags,
1065 DecryptedImage *d) {
1066
1067 _cleanup_free_ char *node = NULL, *name = NULL;
1068 _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
1069 int r;
1070
1071 assert(m);
1072 assert(v);
1073
1074 if (!root_hash)
1075 return 0;
1076
1077 if (!m->found || !m->node || !m->fstype)
1078 return 0;
1079 if (!v->found || !v->node || !v->fstype)
1080 return 0;
1081
1082 if (!streq(v->fstype, "DM_verity_hash"))
1083 return 0;
1084
1085 r = make_dm_name_and_node(m->node, "-verity", &name, &node);
1086 if (r < 0)
1087 return r;
1088
1089 if (!GREEDY_REALLOC0(d->decrypted, d->n_allocated, d->n_decrypted + 1))
1090 return -ENOMEM;
1091
1092 r = crypt_init(&cd, v->node);
1093 if (r < 0)
1094 return r;
1095
1096 r = crypt_load(cd, CRYPT_VERITY, NULL);
1097 if (r < 0)
1098 return r;
1099
1100 r = crypt_set_data_device(cd, m->node);
1101 if (r < 0)
1102 return r;
1103
1104 r = crypt_activate_by_volume_key(cd, name, root_hash, root_hash_size, CRYPT_ACTIVATE_READONLY);
1105 if (r < 0)
1106 return r;
1107
1108 d->decrypted[d->n_decrypted].name = TAKE_PTR(name);
1109 d->decrypted[d->n_decrypted].device = TAKE_PTR(cd);
1110 d->n_decrypted++;
1111
1112 m->decrypted_node = TAKE_PTR(node);
1113
1114 return 0;
1115 }
1116 #endif
1117
1118 int dissected_image_decrypt(
1119 DissectedImage *m,
1120 const char *passphrase,
1121 const void *root_hash,
1122 size_t root_hash_size,
1123 DissectImageFlags flags,
1124 DecryptedImage **ret) {
1125
1126 #if HAVE_LIBCRYPTSETUP
1127 _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL;
1128 unsigned i;
1129 int r;
1130 #endif
1131
1132 assert(m);
1133 assert(root_hash || root_hash_size == 0);
1134
1135 /* Returns:
1136 *
1137 * = 0 → There was nothing to decrypt
1138 * > 0 → Decrypted successfully
1139 * -ENOKEY → There's something to decrypt but no key was supplied
1140 * -EKEYREJECTED → Passed key was not correct
1141 */
1142
1143 if (root_hash && root_hash_size < sizeof(sd_id128_t))
1144 return -EINVAL;
1145
1146 if (!m->encrypted && !m->verity) {
1147 *ret = NULL;
1148 return 0;
1149 }
1150
1151 #if HAVE_LIBCRYPTSETUP
1152 d = new0(DecryptedImage, 1);
1153 if (!d)
1154 return -ENOMEM;
1155
1156 for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
1157 DissectedPartition *p = m->partitions + i;
1158 int k;
1159
1160 if (!p->found)
1161 continue;
1162
1163 r = decrypt_partition(p, passphrase, flags, d);
1164 if (r < 0)
1165 return r;
1166
1167 k = PARTITION_VERITY_OF(i);
1168 if (k >= 0) {
1169 r = verity_partition(p, m->partitions + k, root_hash, root_hash_size, flags, d);
1170 if (r < 0)
1171 return r;
1172 }
1173
1174 if (!p->decrypted_fstype && p->decrypted_node) {
1175 r = probe_filesystem(p->decrypted_node, &p->decrypted_fstype);
1176 if (r < 0 && r != -EUCLEAN)
1177 return r;
1178 }
1179 }
1180
1181 *ret = TAKE_PTR(d);
1182
1183 return 1;
1184 #else
1185 return -EOPNOTSUPP;
1186 #endif
1187 }
1188
1189 int dissected_image_decrypt_interactively(
1190 DissectedImage *m,
1191 const char *passphrase,
1192 const void *root_hash,
1193 size_t root_hash_size,
1194 DissectImageFlags flags,
1195 DecryptedImage **ret) {
1196
1197 _cleanup_strv_free_erase_ char **z = NULL;
1198 int n = 3, r;
1199
1200 if (passphrase)
1201 n--;
1202
1203 for (;;) {
1204 r = dissected_image_decrypt(m, passphrase, root_hash, root_hash_size, flags, ret);
1205 if (r >= 0)
1206 return r;
1207 if (r == -EKEYREJECTED)
1208 log_error_errno(r, "Incorrect passphrase, try again!");
1209 else if (r != -ENOKEY)
1210 return log_error_errno(r, "Failed to decrypt image: %m");
1211
1212 if (--n < 0)
1213 return log_error_errno(SYNTHETIC_ERRNO(EKEYREJECTED),
1214 "Too many retries.");
1215
1216 z = strv_free(z);
1217
1218 r = ask_password_auto("Please enter image passphrase:", NULL, "dissect", "dissect", USEC_INFINITY, 0, &z);
1219 if (r < 0)
1220 return log_error_errno(r, "Failed to query for passphrase: %m");
1221
1222 passphrase = z[0];
1223 }
1224 }
1225
1226 #if HAVE_LIBCRYPTSETUP
1227 static int deferred_remove(DecryptedPartition *p) {
1228 struct dm_ioctl dm = {
1229 .version = {
1230 DM_VERSION_MAJOR,
1231 DM_VERSION_MINOR,
1232 DM_VERSION_PATCHLEVEL
1233 },
1234 .data_size = sizeof(dm),
1235 .flags = DM_DEFERRED_REMOVE,
1236 };
1237
1238 _cleanup_close_ int fd = -1;
1239
1240 assert(p);
1241
1242 /* Unfortunately, libcryptsetup doesn't provide a proper API for this, hence call the ioctl() directly. */
1243
1244 fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC);
1245 if (fd < 0)
1246 return -errno;
1247
1248 if (strlen(p->name) > sizeof(dm.name))
1249 return -ENAMETOOLONG;
1250
1251 strncpy(dm.name, p->name, sizeof(dm.name));
1252
1253 if (ioctl(fd, DM_DEV_REMOVE, &dm))
1254 return -errno;
1255
1256 return 0;
1257 }
1258 #endif
1259
1260 int decrypted_image_relinquish(DecryptedImage *d) {
1261
1262 #if HAVE_LIBCRYPTSETUP
1263 size_t i;
1264 int r;
1265 #endif
1266
1267 assert(d);
1268
1269 /* Turns on automatic removal after the last use ended for all DM devices of this image, and sets a boolean so
1270 * that we don't clean it up ourselves either anymore */
1271
1272 #if HAVE_LIBCRYPTSETUP
1273 for (i = 0; i < d->n_decrypted; i++) {
1274 DecryptedPartition *p = d->decrypted + i;
1275
1276 if (p->relinquished)
1277 continue;
1278
1279 r = deferred_remove(p);
1280 if (r < 0)
1281 return log_debug_errno(r, "Failed to mark %s for auto-removal: %m", p->name);
1282
1283 p->relinquished = true;
1284 }
1285 #endif
1286
1287 return 0;
1288 }
1289
1290 int root_hash_load(const char *image, void **ret, size_t *ret_size) {
1291 _cleanup_free_ char *text = NULL;
1292 _cleanup_free_ void *k = NULL;
1293 size_t l;
1294 int r;
1295
1296 assert(image);
1297 assert(ret);
1298 assert(ret_size);
1299
1300 if (is_device_path(image)) {
1301 /* If we are asked to load the root hash for a device node, exit early */
1302 *ret = NULL;
1303 *ret_size = 0;
1304 return 0;
1305 }
1306
1307 r = getxattr_malloc(image, "user.verity.roothash", &text, true);
1308 if (r < 0) {
1309 char *fn, *e, *n;
1310
1311 if (!IN_SET(r, -ENODATA, -EOPNOTSUPP, -ENOENT))
1312 return r;
1313
1314 fn = newa(char, strlen(image) + STRLEN(".roothash") + 1);
1315 n = stpcpy(fn, image);
1316 e = endswith(fn, ".raw");
1317 if (e)
1318 n = e;
1319
1320 strcpy(n, ".roothash");
1321
1322 r = read_one_line_file(fn, &text);
1323 if (r == -ENOENT) {
1324 *ret = NULL;
1325 *ret_size = 0;
1326 return 0;
1327 }
1328 if (r < 0)
1329 return r;
1330 }
1331
1332 r = unhexmem(text, strlen(text), &k, &l);
1333 if (r < 0)
1334 return r;
1335 if (l < sizeof(sd_id128_t))
1336 return -EINVAL;
1337
1338 *ret = TAKE_PTR(k);
1339 *ret_size = l;
1340
1341 return 1;
1342 }
1343
1344 int dissected_image_acquire_metadata(DissectedImage *m) {
1345
1346 enum {
1347 META_HOSTNAME,
1348 META_MACHINE_ID,
1349 META_MACHINE_INFO,
1350 META_OS_RELEASE,
1351 _META_MAX,
1352 };
1353
1354 static const char *const paths[_META_MAX] = {
1355 [META_HOSTNAME] = "/etc/hostname\0",
1356 [META_MACHINE_ID] = "/etc/machine-id\0",
1357 [META_MACHINE_INFO] = "/etc/machine-info\0",
1358 [META_OS_RELEASE] = "/etc/os-release\0/usr/lib/os-release\0",
1359 };
1360
1361 _cleanup_strv_free_ char **machine_info = NULL, **os_release = NULL;
1362 _cleanup_(rmdir_and_freep) char *t = NULL;
1363 _cleanup_(sigkill_waitp) pid_t child = 0;
1364 sd_id128_t machine_id = SD_ID128_NULL;
1365 _cleanup_free_ char *hostname = NULL;
1366 unsigned n_meta_initialized = 0, k;
1367 int fds[2 * _META_MAX], r;
1368
1369 BLOCK_SIGNALS(SIGCHLD);
1370
1371 assert(m);
1372
1373 for (; n_meta_initialized < _META_MAX; n_meta_initialized ++)
1374 if (pipe2(fds + 2*n_meta_initialized, O_CLOEXEC) < 0) {
1375 r = -errno;
1376 goto finish;
1377 }
1378
1379 r = mkdtemp_malloc("/tmp/dissect-XXXXXX", &t);
1380 if (r < 0)
1381 goto finish;
1382
1383 r = safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, &child);
1384 if (r < 0)
1385 goto finish;
1386 if (r == 0) {
1387 r = dissected_image_mount(m, t, UID_INVALID, DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_MOUNT_ROOT_ONLY|DISSECT_IMAGE_VALIDATE_OS);
1388 if (r < 0) {
1389 log_debug_errno(r, "Failed to mount dissected image: %m");
1390 _exit(EXIT_FAILURE);
1391 }
1392
1393 for (k = 0; k < _META_MAX; k++) {
1394 _cleanup_close_ int fd = -1;
1395 const char *p;
1396
1397 fds[2*k] = safe_close(fds[2*k]);
1398
1399 NULSTR_FOREACH(p, paths[k]) {
1400 fd = chase_symlinks_and_open(p, t, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL);
1401 if (fd >= 0)
1402 break;
1403 }
1404 if (fd < 0) {
1405 log_debug_errno(fd, "Failed to read %s file of image, ignoring: %m", paths[k]);
1406 continue;
1407 }
1408
1409 r = copy_bytes(fd, fds[2*k+1], (uint64_t) -1, 0);
1410 if (r < 0)
1411 _exit(EXIT_FAILURE);
1412
1413 fds[2*k+1] = safe_close(fds[2*k+1]);
1414 }
1415
1416 _exit(EXIT_SUCCESS);
1417 }
1418
1419 for (k = 0; k < _META_MAX; k++) {
1420 _cleanup_fclose_ FILE *f = NULL;
1421
1422 fds[2*k+1] = safe_close(fds[2*k+1]);
1423
1424 f = fdopen(fds[2*k], "r");
1425 if (!f) {
1426 r = -errno;
1427 goto finish;
1428 }
1429
1430 fds[2*k] = -1;
1431
1432 switch (k) {
1433
1434 case META_HOSTNAME:
1435 r = read_etc_hostname_stream(f, &hostname);
1436 if (r < 0)
1437 log_debug_errno(r, "Failed to read /etc/hostname: %m");
1438
1439 break;
1440
1441 case META_MACHINE_ID: {
1442 _cleanup_free_ char *line = NULL;
1443
1444 r = read_line(f, LONG_LINE_MAX, &line);
1445 if (r < 0)
1446 log_debug_errno(r, "Failed to read /etc/machine-id: %m");
1447 else if (r == 33) {
1448 r = sd_id128_from_string(line, &machine_id);
1449 if (r < 0)
1450 log_debug_errno(r, "Image contains invalid /etc/machine-id: %s", line);
1451 } else if (r == 0)
1452 log_debug("/etc/machine-id file is empty.");
1453 else
1454 log_debug("/etc/machine-id has unexpected length %i.", r);
1455
1456 break;
1457 }
1458
1459 case META_MACHINE_INFO:
1460 r = load_env_file_pairs(f, "machine-info", &machine_info);
1461 if (r < 0)
1462 log_debug_errno(r, "Failed to read /etc/machine-info: %m");
1463
1464 break;
1465
1466 case META_OS_RELEASE:
1467 r = load_env_file_pairs(f, "os-release", &os_release);
1468 if (r < 0)
1469 log_debug_errno(r, "Failed to read OS release file: %m");
1470
1471 break;
1472 }
1473 }
1474
1475 r = wait_for_terminate_and_check("(sd-dissect)", child, 0);
1476 child = 0;
1477 if (r < 0)
1478 goto finish;
1479 if (r != EXIT_SUCCESS)
1480 return -EPROTO;
1481
1482 free_and_replace(m->hostname, hostname);
1483 m->machine_id = machine_id;
1484 strv_free_and_replace(m->machine_info, machine_info);
1485 strv_free_and_replace(m->os_release, os_release);
1486
1487 finish:
1488 for (k = 0; k < n_meta_initialized; k++)
1489 safe_close_pair(fds + 2*k);
1490
1491 return r;
1492 }
1493
1494 int dissect_image_and_warn(
1495 int fd,
1496 const char *name,
1497 const void *root_hash,
1498 size_t root_hash_size,
1499 DissectImageFlags flags,
1500 DissectedImage **ret) {
1501
1502 _cleanup_free_ char *buffer = NULL;
1503 int r;
1504
1505 if (!name) {
1506 r = fd_get_path(fd, &buffer);
1507 if (r < 0)
1508 return r;
1509
1510 name = buffer;
1511 }
1512
1513 r = dissect_image(fd, root_hash, root_hash_size, flags, ret);
1514
1515 switch (r) {
1516
1517 case -EOPNOTSUPP:
1518 return log_error_errno(r, "Dissecting images is not supported, compiled without blkid support.");
1519
1520 case -ENOPKG:
1521 return log_error_errno(r, "Couldn't identify a suitable partition table or file system in '%s'.", name);
1522
1523 case -EADDRNOTAVAIL:
1524 return log_error_errno(r, "No root partition for specified root hash found in '%s'.", name);
1525
1526 case -ENOTUNIQ:
1527 return log_error_errno(r, "Multiple suitable root partitions found in image '%s'.", name);
1528
1529 case -ENXIO:
1530 return log_error_errno(r, "No suitable root partition found in image '%s'.", name);
1531
1532 case -EPROTONOSUPPORT:
1533 return log_error_errno(r, "Device '%s' is loopback block device with partition scanning turned off, please turn it on.", name);
1534
1535 default:
1536 if (r < 0)
1537 return log_error_errno(r, "Failed to dissect image '%s': %m", name);
1538
1539 return r;
1540 }
1541 }
1542
1543 static const char *const partition_designator_table[] = {
1544 [PARTITION_ROOT] = "root",
1545 [PARTITION_ROOT_SECONDARY] = "root-secondary",
1546 [PARTITION_HOME] = "home",
1547 [PARTITION_SRV] = "srv",
1548 [PARTITION_ESP] = "esp",
1549 [PARTITION_XBOOTLDR] = "xbootldr",
1550 [PARTITION_SWAP] = "swap",
1551 [PARTITION_ROOT_VERITY] = "root-verity",
1552 [PARTITION_ROOT_SECONDARY_VERITY] = "root-secondary-verity",
1553 };
1554
1555 DEFINE_STRING_TABLE_LOOKUP(partition_designator, int);