]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
8c1be37e | 2 | |
10c1b188 LP |
3 | #if HAVE_VALGRIND_MEMCHECK_H |
4 | #include <valgrind/memcheck.h> | |
5 | #endif | |
6 | ||
01234e1f YW |
7 | #include <linux/dm-ioctl.h> |
8 | #include <linux/loop.h> | |
8c1be37e | 9 | #include <sys/mount.h> |
3b925504 LP |
10 | #include <sys/prctl.h> |
11 | #include <sys/wait.h> | |
8c1be37e | 12 | |
3c1f2cee | 13 | #include "sd-device.h" |
dccca82b LP |
14 | #include "sd-id128.h" |
15 | ||
8c1be37e | 16 | #include "architecture.h" |
18b5886e | 17 | #include "ask-password-api.h" |
8c1be37e | 18 | #include "blkid-util.h" |
18c528e9 | 19 | #include "blockdev-util.h" |
3b925504 | 20 | #include "copy.h" |
294bd454 | 21 | #include "crypt-util.h" |
3b925504 | 22 | #include "def.h" |
553e15f2 | 23 | #include "device-nodes.h" |
8437c059 | 24 | #include "device-util.h" |
8c1be37e | 25 | #include "dissect-image.h" |
a709a315 | 26 | #include "dm-util.h" |
686d13b9 | 27 | #include "env-file.h" |
18b5886e | 28 | #include "fd-util.h" |
78ebe980 | 29 | #include "fileio.h" |
2eedfd2d | 30 | #include "fs-util.h" |
8c1be37e | 31 | #include "gpt.h" |
78ebe980 | 32 | #include "hexdecoct.h" |
3b925504 LP |
33 | #include "hostname-util.h" |
34 | #include "id128-util.h" | |
8c1be37e | 35 | #include "mount-util.h" |
e4de7287 | 36 | #include "mountpoint-util.h" |
d8b4d14d | 37 | #include "nulstr-util.h" |
d58ad743 | 38 | #include "os-util.h" |
8c1be37e | 39 | #include "path-util.h" |
3b925504 LP |
40 | #include "process-util.h" |
41 | #include "raw-clone.h" | |
42 | #include "signal-util.h" | |
8c1be37e | 43 | #include "stat-util.h" |
18b5886e | 44 | #include "stdio-util.h" |
8c1be37e LP |
45 | #include "string-table.h" |
46 | #include "string-util.h" | |
2eedfd2d | 47 | #include "strv.h" |
e4de7287 | 48 | #include "tmpfile-util.h" |
a8040b6d | 49 | #include "udev-util.h" |
2d3a5a73 | 50 | #include "user-util.h" |
41488e1f | 51 | #include "xattr-util.h" |
8c1be37e | 52 | |
c34b75a1 | 53 | int probe_filesystem(const char *node, char **ret_fstype) { |
7cc84b2c | 54 | /* Try to find device content type and return it in *ret_fstype. If nothing is found, |
5238e957 | 55 | * 0/NULL will be returned. -EUCLEAN will be returned for ambiguous results, and an |
7cc84b2c ZJS |
56 | * different error otherwise. */ |
57 | ||
349cc4a5 | 58 | #if HAVE_BLKID |
8e766630 | 59 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; |
18b5886e LP |
60 | const char *fstype; |
61 | int r; | |
62 | ||
995fa2e5 | 63 | errno = 0; |
18b5886e LP |
64 | b = blkid_new_probe_from_filename(node); |
65 | if (!b) | |
66855de7 | 66 | return errno_or_else(ENOMEM); |
18b5886e LP |
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); | |
7cc84b2c ZJS |
73 | if (r == 1) { |
74 | log_debug("No type detected on partition %s", node); | |
18b5886e LP |
75 | goto not_found; |
76 | } | |
7cc84b2c ZJS |
77 | if (r == -2) { |
78 | log_debug("Results ambiguous for partition %s", node); | |
79 | return -EUCLEAN; | |
80 | } | |
b382db9f | 81 | if (r != 0) |
66855de7 | 82 | return errno_or_else(EIO); |
18b5886e LP |
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; | |
d1c536f5 ZJS |
100 | #else |
101 | return -EOPNOTSUPP; | |
a75e27eb | 102 | #endif |
d1c536f5 | 103 | } |
18b5886e | 104 | |
40c10d3f | 105 | #if HAVE_BLKID |
cde942f6 JPRV |
106 | /* Detect RPMB and Boot partitions, which are not listed by blkid. |
107 | * See https://github.com/systemd/systemd/issues/5806. */ | |
3c1f2cee | 108 | static bool device_is_mmc_special_partition(sd_device *d) { |
aae22eb3 LP |
109 | const char *sysname; |
110 | ||
f70e7f70 LP |
111 | assert(d); |
112 | ||
3c1f2cee YW |
113 | if (sd_device_get_sysname(d, &sysname) < 0) |
114 | return false; | |
115 | ||
116 | return startswith(sysname, "mmcblk") && | |
0cfa78dd | 117 | (endswith(sysname, "rpmb") || endswith(sysname, "boot0") || endswith(sysname, "boot1")); |
cde942f6 JPRV |
118 | } |
119 | ||
3c1f2cee | 120 | static bool device_is_block(sd_device *d) { |
aae22eb3 LP |
121 | const char *ss; |
122 | ||
f70e7f70 LP |
123 | assert(d); |
124 | ||
3c1f2cee | 125 | if (sd_device_get_subsystem(d, &ss) < 0) |
aae22eb3 LP |
126 | return false; |
127 | ||
128 | return streq(ss, "block"); | |
129 | } | |
ea887be0 ZJS |
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 | ||
f70e7f70 LP |
135 | assert(d); |
136 | assert(ret); | |
137 | ||
ea887be0 ZJS |
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, | |
052eaf5c | 161 | DissectImageFlags flags, |
ea887be0 ZJS |
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 | ||
f70e7f70 LP |
169 | assert(fd >= 0); |
170 | assert(d); | |
171 | assert(ret_enumerator); | |
172 | ||
ea887be0 ZJS |
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 | ||
052eaf5c | 187 | if (!FLAGS_SET(flags, DISSECT_IMAGE_NO_UDEV)) { |
1b47436e | 188 | r = device_wait_for_initialization(q, "block", USEC_INFINITY, NULL); |
052eaf5c LP |
189 | if (r < 0) |
190 | return r; | |
191 | } | |
a8040b6d | 192 | |
ea887be0 ZJS |
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 | ||
10c1b188 LP |
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!"); | |
ea887be0 ZJS |
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, | |
052eaf5c | 252 | DissectImageFlags flags, |
ea887be0 | 253 | sd_device_enumerator **ret_enumerator) { |
a8040b6d | 254 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
ea887be0 ZJS |
255 | int r; |
256 | ||
f70e7f70 LP |
257 | assert(fd >= 0); |
258 | assert(d); | |
259 | assert(ret_enumerator); | |
260 | ||
b887c8b8 ZJS |
261 | log_debug("Waiting for device (parent + %d partitions) to appear...", num_partitions); |
262 | ||
052eaf5c | 263 | if (!FLAGS_SET(flags, DISSECT_IMAGE_NO_UDEV)) { |
1b47436e | 264 | r = device_wait_for_initialization(d, "block", USEC_INFINITY, &device); |
052eaf5c LP |
265 | if (r < 0) |
266 | return r; | |
267 | } else | |
268 | device = sd_device_ref(d); | |
a8040b6d | 269 | |
ea887be0 | 270 | for (unsigned i = 0; i < N_DEVICE_NODE_LIST_ATTEMPTS; i++) { |
052eaf5c | 271 | r = wait_for_partitions_to_appear(fd, device, num_partitions, flags, ret_enumerator); |
ea887be0 ZJS |
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 | ||
40c10d3f | 281 | #endif |
aae22eb3 | 282 | |
4526113f LP |
283 | int dissect_image( |
284 | int fd, | |
285 | const void *root_hash, | |
286 | size_t root_hash_size, | |
287 | DissectImageFlags flags, | |
288 | DissectedImage **ret) { | |
8c1be37e | 289 | |
349cc4a5 | 290 | #if HAVE_BLKID |
4623e8e6 | 291 | sd_id128_t root_uuid = SD_ID128_NULL, verity_uuid = SD_ID128_NULL; |
3c1f2cee | 292 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
8c1be37e | 293 | bool is_gpt, is_mbr, generic_rw, multiple_generic = false; |
3c1f2cee | 294 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
8c1be37e | 295 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; |
8e766630 | 296 | _cleanup_(blkid_free_probep) blkid_probe b = NULL; |
8c1be37e | 297 | _cleanup_free_ char *generic_node = NULL; |
be30ad41 | 298 | sd_id128_t generic_uuid = SD_ID128_NULL; |
9b6deb03 | 299 | const char *pttype = NULL; |
8c1be37e LP |
300 | blkid_partlist pl; |
301 | int r, generic_nr; | |
302 | struct stat st; | |
3c1f2cee | 303 | sd_device *q; |
8c1be37e LP |
304 | unsigned i; |
305 | ||
306 | assert(fd >= 0); | |
307 | assert(ret); | |
4623e8e6 | 308 | assert(root_hash || root_hash_size == 0); |
8c1be37e LP |
309 | |
310 | /* Probes a disk image, and returns information about what it found in *ret. | |
311 | * | |
4623e8e6 LP |
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 | } | |
8c1be37e LP |
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); | |
b382db9f | 344 | if (r != 0) |
66855de7 | 345 | return errno_or_else(ENOMEM); |
8c1be37e | 346 | |
9b6deb03 LP |
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 | ||
8c1be37e LP |
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); | |
59ba6d0c LP |
358 | if (IN_SET(r, -2, 1)) |
359 | return log_debug_errno(SYNTHETIC_ERRNO(ENOPKG), "Failed to identify any partition table."); | |
b382db9f | 360 | if (r != 0) |
66855de7 | 361 | return errno_or_else(EIO); |
8c1be37e LP |
362 | |
363 | m = new0(DissectedImage, 1); | |
364 | if (!m) | |
365 | return -ENOMEM; | |
366 | ||
b887c8b8 ZJS |
367 | r = sd_device_new_from_devnum(&d, 'b', st.st_rdev); |
368 | if (r < 0) | |
369 | return r; | |
370 | ||
e0f9e7bd LP |
371 | if (!(flags & DISSECT_IMAGE_GPT_ONLY) && |
372 | (flags & DISSECT_IMAGE_REQUIRE_ROOT)) { | |
9b6deb03 | 373 | const char *usage = NULL; |
8c1be37e | 374 | |
9b6deb03 LP |
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; | |
8c1be37e | 379 | |
9b6deb03 LP |
380 | /* OK, we have found a file system, that's our root partition then. */ |
381 | (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL); | |
8c1be37e | 382 | |
9b6deb03 LP |
383 | if (fstype) { |
384 | t = strdup(fstype); | |
385 | if (!t) | |
386 | return -ENOMEM; | |
387 | } | |
388 | ||
54b22b26 LP |
389 | r = device_path_make_major_minor(st.st_mode, st.st_rdev, &n); |
390 | if (r < 0) | |
391 | return r; | |
8c1be37e | 392 | |
9b6deb03 LP |
393 | m->partitions[PARTITION_ROOT] = (DissectedPartition) { |
394 | .found = true, | |
395 | .rw = true, | |
396 | .partno = -1, | |
397 | .architecture = _ARCHITECTURE_INVALID, | |
1cc6c93a YW |
398 | .fstype = TAKE_PTR(t), |
399 | .node = TAKE_PTR(n), | |
9b6deb03 | 400 | }; |
8c1be37e | 401 | |
4db1879a | 402 | m->encrypted = streq_ptr(fstype, "crypto_LUKS"); |
18b5886e | 403 | |
052eaf5c | 404 | r = loop_wait_for_partitions_to_appear(fd, d, 0, flags, &e); |
b887c8b8 ZJS |
405 | if (r < 0) |
406 | return r; | |
407 | ||
1cc6c93a | 408 | *ret = TAKE_PTR(m); |
8c1be37e | 409 | |
9b6deb03 LP |
410 | return 0; |
411 | } | |
8c1be37e LP |
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 | ||
9b6deb03 | 421 | if (!is_gpt && ((flags & DISSECT_IMAGE_GPT_ONLY) || !is_mbr)) |
8c1be37e LP |
422 | return -ENOPKG; |
423 | ||
424 | errno = 0; | |
425 | pl = blkid_probe_get_partitions(b); | |
b382db9f | 426 | if (!pl) |
66855de7 | 427 | return errno_or_else(ENOMEM); |
8c1be37e | 428 | |
052eaf5c | 429 | r = loop_wait_for_partitions_to_appear(fd, d, blkid_partlist_numof_partitions(pl), flags, &e); |
ea887be0 ZJS |
430 | if (r < 0) |
431 | return r; | |
8c1be37e | 432 | |
8437c059 | 433 | FOREACH_DEVICE(e, q) { |
9b6deb03 | 434 | unsigned long long pflags; |
8c1be37e | 435 | blkid_partition pp; |
cde942f6 | 436 | const char *node; |
8c1be37e LP |
437 | dev_t qn; |
438 | int nr; | |
439 | ||
3c1f2cee YW |
440 | r = sd_device_get_devnum(q, &qn); |
441 | if (r < 0) | |
8c1be37e LP |
442 | continue; |
443 | ||
444 | if (st.st_rdev == qn) | |
445 | continue; | |
446 | ||
aae22eb3 LP |
447 | if (!device_is_block(q)) |
448 | continue; | |
449 | ||
cde942f6 | 450 | if (device_is_mmc_special_partition(q)) |
7be1420f LP |
451 | continue; |
452 | ||
3c1f2cee YW |
453 | r = sd_device_get_devname(q, &node); |
454 | if (r < 0) | |
8c1be37e LP |
455 | continue; |
456 | ||
457 | pp = blkid_partlist_devno_to_partition(pl, qn); | |
458 | if (!pp) | |
459 | continue; | |
460 | ||
9b6deb03 | 461 | pflags = blkid_partition_get_flags(pp); |
8c1be37e LP |
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; | |
4623e8e6 LP |
469 | const char *stype, *sid, *fstype = NULL; |
470 | sd_id128_t type_id, id; | |
8c1be37e LP |
471 | bool rw = true; |
472 | ||
4623e8e6 LP |
473 | sid = blkid_partition_get_uuid(pp); |
474 | if (!sid) | |
475 | continue; | |
476 | if (sd_id128_from_string(sid, &id) < 0) | |
477 | continue; | |
478 | ||
8c1be37e LP |
479 | stype = blkid_partition_get_type_string(pp); |
480 | if (!stype) | |
481 | continue; | |
8c1be37e LP |
482 | if (sd_id128_from_string(stype, &type_id) < 0) |
483 | continue; | |
484 | ||
485 | if (sd_id128_equal(type_id, GPT_HOME)) { | |
a48dd347 LP |
486 | |
487 | if (pflags & GPT_FLAG_NO_AUTO) | |
488 | continue; | |
489 | ||
8c1be37e | 490 | designator = PARTITION_HOME; |
9b6deb03 | 491 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
8c1be37e | 492 | } else if (sd_id128_equal(type_id, GPT_SRV)) { |
a48dd347 LP |
493 | |
494 | if (pflags & GPT_FLAG_NO_AUTO) | |
495 | continue; | |
496 | ||
8c1be37e | 497 | designator = PARTITION_SRV; |
9b6deb03 | 498 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
8c1be37e | 499 | } else if (sd_id128_equal(type_id, GPT_ESP)) { |
a48dd347 LP |
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 | ||
8c1be37e LP |
508 | designator = PARTITION_ESP; |
509 | fstype = "vfat"; | |
a8c47660 LP |
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); | |
8c1be37e LP |
518 | } |
519 | #ifdef GPT_ROOT_NATIVE | |
520 | else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE)) { | |
4623e8e6 | 521 | |
a48dd347 LP |
522 | if (pflags & GPT_FLAG_NO_AUTO) |
523 | continue; | |
524 | ||
4623e8e6 LP |
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 | ||
8c1be37e LP |
529 | designator = PARTITION_ROOT; |
530 | architecture = native_architecture(); | |
9b6deb03 | 531 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
4f8b86e3 | 532 | } else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE_VERITY)) { |
4623e8e6 | 533 | |
a48dd347 LP |
534 | if (pflags & GPT_FLAG_NO_AUTO) |
535 | continue; | |
536 | ||
4623e8e6 LP |
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 | |
8c1be37e LP |
549 | #ifdef GPT_ROOT_SECONDARY |
550 | else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY)) { | |
4623e8e6 | 551 | |
a48dd347 LP |
552 | if (pflags & GPT_FLAG_NO_AUTO) |
553 | continue; | |
554 | ||
4623e8e6 LP |
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 | ||
8c1be37e LP |
559 | designator = PARTITION_ROOT_SECONDARY; |
560 | architecture = SECONDARY_ARCHITECTURE; | |
9b6deb03 | 561 | rw = !(pflags & GPT_FLAG_READ_ONLY); |
4f8b86e3 | 562 | } else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY_VERITY)) { |
a48dd347 LP |
563 | |
564 | if (pflags & GPT_FLAG_NO_AUTO) | |
565 | continue; | |
566 | ||
4623e8e6 LP |
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 | } | |
8c1be37e LP |
578 | #endif |
579 | else if (sd_id128_equal(type_id, GPT_SWAP)) { | |
a48dd347 LP |
580 | |
581 | if (pflags & GPT_FLAG_NO_AUTO) | |
582 | continue; | |
583 | ||
8c1be37e LP |
584 | designator = PARTITION_SWAP; |
585 | fstype = "swap"; | |
586 | } else if (sd_id128_equal(type_id, GPT_LINUX_GENERIC)) { | |
587 | ||
a48dd347 LP |
588 | if (pflags & GPT_FLAG_NO_AUTO) |
589 | continue; | |
590 | ||
8c1be37e LP |
591 | if (generic_node) |
592 | multiple_generic = true; | |
593 | else { | |
594 | generic_nr = nr; | |
9b6deb03 | 595 | generic_rw = !(pflags & GPT_FLAG_READ_ONLY); |
be30ad41 | 596 | generic_uuid = id; |
8c1be37e LP |
597 | generic_node = strdup(node); |
598 | if (!generic_node) | |
599 | return -ENOMEM; | |
600 | } | |
d4dffb85 LP |
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); | |
8c1be37e LP |
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, | |
1cc6c93a YW |
662 | .node = TAKE_PTR(n), |
663 | .fstype = TAKE_PTR(t), | |
be30ad41 | 664 | .uuid = id, |
8c1be37e | 665 | }; |
8c1be37e LP |
666 | } |
667 | ||
668 | } else if (is_mbr) { | |
669 | ||
a8c47660 | 670 | switch (blkid_partition_get_type(pp)) { |
8c1be37e | 671 | |
a8c47660 LP |
672 | case 0x83: /* Linux partition */ |
673 | ||
674 | if (pflags != 0x80) /* Bootable flag */ | |
675 | continue; | |
8c1be37e | 676 | |
a8c47660 LP |
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) | |
8c1be37e | 704 | return -ENOMEM; |
a8c47660 LP |
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 | }} | |
8c1be37e LP |
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 | ||
4623e8e6 | 724 | if (m->partitions[PARTITION_ROOT_VERITY].found) |
e0f9e7bd | 725 | return -EADDRNOTAVAIL; |
4623e8e6 | 726 | |
8c1be37e LP |
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]); | |
4623e8e6 LP |
730 | |
731 | m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY]; | |
732 | zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY]); | |
733 | ||
e0f9e7bd LP |
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; | |
8c1be37e | 740 | |
e0f9e7bd LP |
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. */ | |
8c1be37e LP |
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, | |
1cc6c93a | 755 | .node = TAKE_PTR(generic_node), |
be30ad41 | 756 | .uuid = generic_uuid, |
8c1be37e | 757 | }; |
e0f9e7bd | 758 | } |
8c1be37e LP |
759 | } |
760 | ||
4623e8e6 | 761 | if (root_hash) { |
e0f9e7bd | 762 | if (!m->partitions[PARTITION_ROOT_VERITY].found || !m->partitions[PARTITION_ROOT].found) |
4623e8e6 LP |
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 | ||
18b5886e LP |
777 | blkid_free_probe(b); |
778 | b = NULL; | |
779 | ||
8c1be37e LP |
780 | /* Fill in file system types if we don't know them yet. */ |
781 | for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { | |
18b5886e | 782 | DissectedPartition *p = m->partitions + i; |
8c1be37e | 783 | |
18b5886e | 784 | if (!p->found) |
8c1be37e LP |
785 | continue; |
786 | ||
18b5886e LP |
787 | if (!p->fstype && p->node) { |
788 | r = probe_filesystem(p->node, &p->fstype); | |
7cc84b2c | 789 | if (r < 0 && r != -EUCLEAN) |
18b5886e | 790 | return r; |
8c1be37e LP |
791 | } |
792 | ||
18b5886e LP |
793 | if (streq_ptr(p->fstype, "crypto_LUKS")) |
794 | m->encrypted = true; | |
896f937f LP |
795 | |
796 | if (p->fstype && fstype_is_ro(p->fstype)) | |
797 | p->rw = false; | |
8c1be37e LP |
798 | } |
799 | ||
1cc6c93a | 800 | *ret = TAKE_PTR(m); |
8c1be37e LP |
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); | |
18b5886e LP |
817 | free(m->partitions[i].decrypted_fstype); |
818 | free(m->partitions[i].decrypted_node); | |
8c1be37e LP |
819 | } |
820 | ||
3b925504 LP |
821 | free(m->hostname); |
822 | strv_free(m->machine_info); | |
823 | strv_free(m->os_release); | |
824 | ||
5fecf46d | 825 | return mfree(m); |
8c1be37e LP |
826 | } |
827 | ||
18b5886e | 828 | static int is_loop_device(const char *path) { |
553e15f2 | 829 | char s[SYS_BLOCK_PATH_MAX("/../loop/")]; |
18b5886e LP |
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 | ||
553e15f2 | 840 | xsprintf_sys_block_path(s, "/loop/", st.st_dev); |
18b5886e LP |
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? */ | |
553e15f2 | 846 | xsprintf_sys_block_path(s, "/../loop/", st.st_dev); |
18b5886e LP |
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, | |
2d3a5a73 | 858 | uid_t uid_shift, |
18b5886e LP |
859 | DissectImageFlags flags) { |
860 | ||
2d3a5a73 LP |
861 | _cleanup_free_ char *chased = NULL, *options = NULL; |
862 | const char *p, *node, *fstype; | |
8c1be37e | 863 | bool rw; |
2eedfd2d | 864 | int r; |
8c1be37e LP |
865 | |
866 | assert(m); | |
867 | assert(where); | |
868 | ||
18b5886e LP |
869 | node = m->decrypted_node ?: m->node; |
870 | fstype = m->decrypted_fstype ?: m->fstype; | |
871 | ||
872 | if (!m->found || !node || !fstype) | |
8c1be37e LP |
873 | return 0; |
874 | ||
18b5886e LP |
875 | /* Stacked encryption? Yuck */ |
876 | if (streq_ptr(fstype, "crypto_LUKS")) | |
877 | return -ELOOP; | |
878 | ||
879 | rw = m->rw && !(flags & DISSECT_IMAGE_READ_ONLY); | |
8c1be37e | 880 | |
2eedfd2d | 881 | if (directory) { |
a5648b80 | 882 | r = chase_symlinks(directory, where, CHASE_PREFIX_ROOT, &chased, NULL); |
2eedfd2d LP |
883 | if (r < 0) |
884 | return r; | |
885 | ||
886 | p = chased; | |
887 | } else | |
8c1be37e LP |
888 | p = where; |
889 | ||
18b5886e | 890 | /* If requested, turn on discard support. */ |
154d2269 | 891 | if (fstype_can_discard(fstype) && |
18b5886e | 892 | ((flags & DISSECT_IMAGE_DISCARD) || |
2d3a5a73 LP |
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 | } | |
8c1be37e | 908 | |
d9223c07 LP |
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; | |
8c1be37e LP |
914 | } |
915 | ||
2d3a5a73 | 916 | int dissected_image_mount(DissectedImage *m, const char *where, uid_t uid_shift, DissectImageFlags flags) { |
d9223c07 | 917 | int r, boot_mounted; |
8c1be37e LP |
918 | |
919 | assert(m); | |
920 | assert(where); | |
921 | ||
922 | if (!m->partitions[PARTITION_ROOT].found) | |
923 | return -ENXIO; | |
924 | ||
2d3a5a73 LP |
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; | |
03bcb6d4 LP |
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 | } | |
2d3a5a73 LP |
937 | } |
938 | ||
705727fd | 939 | if (flags & DISSECT_IMAGE_MOUNT_ROOT_ONLY) |
2d3a5a73 | 940 | return 0; |
8c1be37e | 941 | |
2d3a5a73 | 942 | r = mount_partition(m->partitions + PARTITION_HOME, where, "/home", uid_shift, flags); |
8c1be37e LP |
943 | if (r < 0) |
944 | return r; | |
945 | ||
2d3a5a73 | 946 | r = mount_partition(m->partitions + PARTITION_SRV, where, "/srv", uid_shift, flags); |
8c1be37e LP |
947 | if (r < 0) |
948 | return r; | |
949 | ||
d4dffb85 LP |
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 | ||
d9223c07 LP |
958 | boot_mounted = mount_partition(m->partitions + PARTITION_XBOOTLDR, where, "/boot", uid_shift, flags); |
959 | if (boot_mounted < 0) | |
960 | return boot_mounted; | |
961 | ||
8c1be37e | 962 | if (m->partitions[PARTITION_ESP].found) { |
d9223c07 LP |
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. */ | |
8c1be37e | 965 | |
a5648b80 | 966 | r = chase_symlinks("/efi", where, CHASE_PREFIX_ROOT, NULL, NULL); |
d9223c07 LP |
967 | if (r >= 0) { |
968 | r = mount_partition(m->partitions + PARTITION_ESP, where, "/efi", uid_shift, flags); | |
969 | if (r < 0) | |
970 | return r; | |
8c1be37e | 971 | |
d9223c07 | 972 | } else if (boot_mounted <= 0) { |
2eedfd2d LP |
973 | _cleanup_free_ char *p = NULL; |
974 | ||
a5648b80 | 975 | r = chase_symlinks("/boot", where, CHASE_PREFIX_ROOT, &p, NULL); |
d9223c07 LP |
976 | if (r >= 0 && dir_is_empty(p) > 0) { |
977 | r = mount_partition(m->partitions + PARTITION_ESP, where, "/boot", uid_shift, flags); | |
2eedfd2d LP |
978 | if (r < 0) |
979 | return r; | |
980 | } | |
8c1be37e LP |
981 | } |
982 | } | |
983 | ||
984 | return 0; | |
985 | } | |
986 | ||
349cc4a5 | 987 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
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) { | |
349cc4a5 | 1002 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
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 | ||
349cc4a5 | 1028 | #if HAVE_LIBCRYPTSETUP |
4623e8e6 LP |
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 | ||
657ee2d8 | 1052 | node = path_join(crypt_get_dir(), name); |
4623e8e6 LP |
1053 | if (!node) |
1054 | return -ENOMEM; | |
1055 | ||
1cc6c93a YW |
1056 | *ret_name = TAKE_PTR(name); |
1057 | *ret_node = TAKE_PTR(node); | |
4623e8e6 | 1058 | |
4623e8e6 LP |
1059 | return 0; |
1060 | } | |
1061 | ||
18b5886e LP |
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; | |
294bd454 | 1069 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
18b5886e LP |
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 | ||
bdd73ac5 ZJS |
1081 | if (!passphrase) |
1082 | return -ENOKEY; | |
1083 | ||
4623e8e6 LP |
1084 | r = make_dm_name_and_node(m->node, "-decrypted", &name, &node); |
1085 | if (r < 0) | |
1086 | return r; | |
18b5886e LP |
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) | |
715cbb81 | 1093 | return log_debug_errno(r, "Failed to initialize dm-crypt: %m"); |
18b5886e | 1094 | |
dd59868b | 1095 | r = crypt_load(cd, CRYPT_LUKS, NULL); |
294bd454 ZJS |
1096 | if (r < 0) |
1097 | return log_debug_errno(r, "Failed to load LUKS metadata: %m"); | |
18b5886e LP |
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)); | |
294bd454 | 1102 | if (r < 0) { |
715cbb81 | 1103 | log_debug_errno(r, "Failed to activate LUKS device: %m"); |
294bd454 | 1104 | return r == -EPERM ? -EKEYREJECTED : r; |
18b5886e | 1105 | } |
18b5886e | 1106 | |
1cc6c93a YW |
1107 | d->decrypted[d->n_decrypted].name = TAKE_PTR(name); |
1108 | d->decrypted[d->n_decrypted].device = TAKE_PTR(cd); | |
18b5886e LP |
1109 | d->n_decrypted++; |
1110 | ||
1cc6c93a | 1111 | m->decrypted_node = TAKE_PTR(node); |
18b5886e LP |
1112 | |
1113 | return 0; | |
4623e8e6 LP |
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; | |
294bd454 | 1125 | _cleanup_(crypt_freep) struct crypt_device *cd = NULL; |
4623e8e6 LP |
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) | |
294bd454 | 1155 | return r; |
4623e8e6 LP |
1156 | |
1157 | r = crypt_set_data_device(cd, m->node); | |
1158 | if (r < 0) | |
294bd454 | 1159 | return r; |
4623e8e6 LP |
1160 | |
1161 | r = crypt_activate_by_volume_key(cd, name, root_hash, root_hash_size, CRYPT_ACTIVATE_READONLY); | |
1162 | if (r < 0) | |
294bd454 | 1163 | return r; |
4623e8e6 | 1164 | |
1cc6c93a YW |
1165 | d->decrypted[d->n_decrypted].name = TAKE_PTR(name); |
1166 | d->decrypted[d->n_decrypted].device = TAKE_PTR(cd); | |
4623e8e6 LP |
1167 | d->n_decrypted++; |
1168 | ||
1cc6c93a | 1169 | m->decrypted_node = TAKE_PTR(node); |
4623e8e6 LP |
1170 | |
1171 | return 0; | |
18b5886e LP |
1172 | } |
1173 | #endif | |
1174 | ||
1175 | int dissected_image_decrypt( | |
1176 | DissectedImage *m, | |
1177 | const char *passphrase, | |
4623e8e6 LP |
1178 | const void *root_hash, |
1179 | size_t root_hash_size, | |
18b5886e LP |
1180 | DissectImageFlags flags, |
1181 | DecryptedImage **ret) { | |
1182 | ||
349cc4a5 | 1183 | #if HAVE_LIBCRYPTSETUP |
49b5b3b4 | 1184 | _cleanup_(decrypted_image_unrefp) DecryptedImage *d = NULL; |
18b5886e LP |
1185 | unsigned i; |
1186 | int r; | |
1187 | #endif | |
1188 | ||
1189 | assert(m); | |
4623e8e6 | 1190 | assert(root_hash || root_hash_size == 0); |
18b5886e LP |
1191 | |
1192 | /* Returns: | |
1193 | * | |
1194 | * = 0 → There was nothing to decrypt | |
1195 | * > 0 → Decrypted successfully | |
d1c536f5 | 1196 | * -ENOKEY → There's something to decrypt but no key was supplied |
18b5886e LP |
1197 | * -EKEYREJECTED → Passed key was not correct |
1198 | */ | |
1199 | ||
4623e8e6 LP |
1200 | if (root_hash && root_hash_size < sizeof(sd_id128_t)) |
1201 | return -EINVAL; | |
1202 | ||
1203 | if (!m->encrypted && !m->verity) { | |
18b5886e LP |
1204 | *ret = NULL; |
1205 | return 0; | |
1206 | } | |
1207 | ||
349cc4a5 | 1208 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
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; | |
4623e8e6 | 1215 | int k; |
18b5886e LP |
1216 | |
1217 | if (!p->found) | |
1218 | continue; | |
1219 | ||
1220 | r = decrypt_partition(p, passphrase, flags, d); | |
1221 | if (r < 0) | |
1222 | return r; | |
1223 | ||
4623e8e6 LP |
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 | ||
18b5886e LP |
1231 | if (!p->decrypted_fstype && p->decrypted_node) { |
1232 | r = probe_filesystem(p->decrypted_node, &p->decrypted_fstype); | |
7cc84b2c | 1233 | if (r < 0 && r != -EUCLEAN) |
18b5886e LP |
1234 | return r; |
1235 | } | |
1236 | } | |
1237 | ||
1cc6c93a | 1238 | *ret = TAKE_PTR(d); |
18b5886e LP |
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, | |
4623e8e6 LP |
1249 | const void *root_hash, |
1250 | size_t root_hash_size, | |
18b5886e LP |
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 (;;) { | |
4623e8e6 | 1261 | r = dissected_image_decrypt(m, passphrase, root_hash, root_hash_size, flags, ret); |
18b5886e LP |
1262 | if (r >= 0) |
1263 | return r; | |
1264 | if (r == -EKEYREJECTED) | |
1265 | log_error_errno(r, "Incorrect passphrase, try again!"); | |
fc95c359 YW |
1266 | else if (r != -ENOKEY) |
1267 | return log_error_errno(r, "Failed to decrypt image: %m"); | |
18b5886e | 1268 | |
baaa35ad ZJS |
1269 | if (--n < 0) |
1270 | return log_error_errno(SYNTHETIC_ERRNO(EKEYREJECTED), | |
1271 | "Too many retries."); | |
18b5886e LP |
1272 | |
1273 | z = strv_free(z); | |
1274 | ||
a1c111c2 | 1275 | r = ask_password_auto("Please enter image passphrase:", NULL, "dissect", "dissect", USEC_INFINITY, 0, &z); |
18b5886e LP |
1276 | if (r < 0) |
1277 | return log_error_errno(r, "Failed to query for passphrase: %m"); | |
1278 | ||
1279 | passphrase = z[0]; | |
1280 | } | |
1281 | } | |
1282 | ||
18b5886e LP |
1283 | int decrypted_image_relinquish(DecryptedImage *d) { |
1284 | ||
349cc4a5 | 1285 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
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 | ||
349cc4a5 | 1295 | #if HAVE_LIBCRYPTSETUP |
18b5886e LP |
1296 | for (i = 0; i < d->n_decrypted; i++) { |
1297 | DecryptedPartition *p = d->decrypted + i; | |
1298 | ||
1299 | if (p->relinquished) | |
1300 | continue; | |
1301 | ||
a709a315 | 1302 | r = dm_deferred_remove(p->name); |
18b5886e LP |
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 | ||
78ebe980 LP |
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; | |
78ebe980 LP |
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 | ||
41488e1f LP |
1330 | r = getxattr_malloc(image, "user.verity.roothash", &text, true); |
1331 | if (r < 0) { | |
1332 | char *fn, *e, *n; | |
78ebe980 | 1333 | |
41488e1f LP |
1334 | if (!IN_SET(r, -ENODATA, -EOPNOTSUPP, -ENOENT)) |
1335 | return r; | |
78ebe980 | 1336 | |
fbd0b64f | 1337 | fn = newa(char, strlen(image) + STRLEN(".roothash") + 1); |
41488e1f LP |
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; | |
78ebe980 | 1353 | } |
78ebe980 LP |
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 | ||
1cc6c93a | 1361 | *ret = TAKE_PTR(k); |
78ebe980 LP |
1362 | *ret_size = l; |
1363 | ||
78ebe980 LP |
1364 | return 1; |
1365 | } | |
1366 | ||
3b925504 LP |
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", | |
d4dffb85 LP |
1381 | [META_OS_RELEASE] = "/etc/os-release\0" |
1382 | "/usr/lib/os-release\0", | |
3b925504 LP |
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; | |
3b925504 LP |
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 | ||
e2047ba9 | 1407 | r = safe_fork("(sd-dissect)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, &child); |
be39f6ee | 1408 | if (r < 0) |
3b925504 | 1409 | goto finish; |
be39f6ee | 1410 | if (r == 0) { |
03bcb6d4 | 1411 | r = dissected_image_mount(m, t, UID_INVALID, DISSECT_IMAGE_READ_ONLY|DISSECT_IMAGE_MOUNT_ROOT_ONLY|DISSECT_IMAGE_VALIDATE_OS); |
429d4e41 LP |
1412 | if (r < 0) { |
1413 | log_debug_errno(r, "Failed to mount dissected image: %m"); | |
3b925504 | 1414 | _exit(EXIT_FAILURE); |
429d4e41 | 1415 | } |
3b925504 LP |
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]) { | |
36952d19 | 1424 | fd = chase_symlinks_and_open(p, t, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL); |
3b925504 LP |
1425 | if (fd >= 0) |
1426 | break; | |
1427 | } | |
36952d19 LP |
1428 | if (fd < 0) { |
1429 | log_debug_errno(fd, "Failed to read %s file of image, ignoring: %m", paths[k]); | |
3b925504 | 1430 | continue; |
36952d19 | 1431 | } |
3b925504 LP |
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 | ||
e92aaed3 | 1448 | f = fdopen(fds[2*k], "r"); |
3b925504 LP |
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: | |
aa8fbc74 | 1484 | r = load_env_file_pairs(f, "machine-info", &machine_info); |
3b925504 LP |
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: | |
aa8fbc74 | 1491 | r = load_env_file_pairs(f, "os-release", &os_release); |
3b925504 LP |
1492 | if (r < 0) |
1493 | log_debug_errno(r, "Failed to read OS release file: %m"); | |
1494 | ||
1495 | break; | |
1496 | } | |
1497 | } | |
1498 | ||
2e87a1fd | 1499 | r = wait_for_terminate_and_check("(sd-dissect)", child, 0); |
3b925504 | 1500 | child = 0; |
2e87a1fd | 1501 | if (r < 0) |
3b925504 | 1502 | goto finish; |
2e87a1fd LP |
1503 | if (r != EXIT_SUCCESS) |
1504 | return -EPROTO; | |
3b925504 LP |
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 | ||
4526113f LP |
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 | ||
8c1be37e LP |
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", | |
a8c47660 | 1573 | [PARTITION_XBOOTLDR] = "xbootldr", |
8c1be37e | 1574 | [PARTITION_SWAP] = "swap", |
4623e8e6 LP |
1575 | [PARTITION_ROOT_VERITY] = "root-verity", |
1576 | [PARTITION_ROOT_SECONDARY_VERITY] = "root-secondary-verity", | |
d4dffb85 LP |
1577 | [PARTITION_TMP] = "tmp", |
1578 | [PARTITION_VAR] = "var", | |
8c1be37e LP |
1579 | }; |
1580 | ||
1581 | DEFINE_STRING_TABLE_LOOKUP(partition_designator, int); |