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