]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/partition/repart.c
tpm2-util: extend TPM2 policies to optionally check PCR values against signed values
[thirdparty/systemd.git] / src / partition / repart.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
e594a3b1
LP
2
3#if HAVE_VALGRIND_MEMCHECK_H
4#include <valgrind/memcheck.h>
5#endif
6
7#include <fcntl.h>
8#include <getopt.h>
e594a3b1
LP
9#include <linux/fs.h>
10#include <linux/loop.h>
11#include <sys/file.h>
12#include <sys/ioctl.h>
13#include <sys/stat.h>
14
1a037ba2 15#include "sd-device.h"
e594a3b1
LP
16#include "sd-id128.h"
17
18#include "alloc-util.h"
19#include "blkid-util.h"
20#include "blockdev-util.h"
21#include "btrfs-util.h"
f4351959 22#include "chase-symlinks.h"
e594a3b1
LP
23#include "conf-files.h"
24#include "conf-parser.h"
1e2f3230 25#include "cryptsetup-util.h"
e594a3b1 26#include "def.h"
ca822829 27#include "device-util.h"
7176f06c 28#include "devnum-util.h"
5c08da58 29#include "dirent-util.h"
e594a3b1
LP
30#include "efivars.h"
31#include "errno-util.h"
32#include "fd-util.h"
4dc07c3a 33#include "fdisk-util.h"
b9df3536 34#include "fileio.h"
e594a3b1
LP
35#include "format-table.h"
36#include "format-util.h"
37#include "fs-util.h"
d8e32c47 38#include "glyph-util.h"
e594a3b1 39#include "gpt.h"
889914ef 40#include "hexdecoct.h"
ade99252 41#include "hmac.h"
e594a3b1 42#include "id128-util.h"
a015fbe7 43#include "json.h"
e594a3b1 44#include "list.h"
53171c04 45#include "loop-util.h"
e594a3b1 46#include "main-func.h"
8a794850 47#include "mkdir.h"
53171c04 48#include "mkfs-util.h"
8a794850 49#include "mount-util.h"
5c08da58 50#include "mountpoint-util.h"
614b022c 51#include "parse-argument.h"
c3eaba2d 52#include "parse-helpers.h"
e594a3b1
LP
53#include "pretty-print.h"
54#include "proc-cmdline.h"
8a794850 55#include "process-util.h"
b9df3536 56#include "random-util.h"
170c9823 57#include "resize-fs.h"
e594a3b1 58#include "sort-util.h"
e031166e 59#include "specifier.h"
e594a3b1 60#include "stdio-util.h"
889914ef 61#include "string-table.h"
e594a3b1
LP
62#include "string-util.h"
63#include "strv.h"
bf819d3a 64#include "sync-util.h"
e594a3b1 65#include "terminal-util.h"
889914ef 66#include "tpm2-util.h"
8a794850 67#include "user-util.h"
e594a3b1
LP
68#include "utf8.h"
69
fb08381c
LP
70/* If not configured otherwise use a minimal partition size of 10M */
71#define DEFAULT_MIN_SIZE (10*1024*1024)
72
73/* Hard lower limit for new partition sizes */
74#define HARD_MIN_SIZE 4096
75
69e3234d 76/* libfdisk takes off slightly more than 1M of the disk size when creating a GPT disk label */
170c9823
LP
77#define GPT_METADATA_SIZE (1044*1024)
78
79/* LUKS2 takes off 16M of the partition size with its metadata by default */
80#define LUKS2_METADATA_SIZE (16*1024*1024)
81
e594a3b1
LP
82/* Note: When growing and placing new partitions we always align to 4K sector size. It's how newer hard disks
83 * are designed, and if everything is aligned to that performance is best. And for older hard disks with 512B
84 * sector size devices were generally assumed to have an even number of sectors, hence at the worst we'll
85 * waste 3K per partition, which is probably fine. */
86
87static enum {
88 EMPTY_REFUSE, /* refuse empty disks, never create a partition table */
89 EMPTY_ALLOW, /* allow empty disks, create partition table if necessary */
90 EMPTY_REQUIRE, /* require an empty disk, create a partition table */
91 EMPTY_FORCE, /* make disk empty, erase everything, create a partition table always */
a26f4a49 92 EMPTY_CREATE, /* create disk as loopback file, create a partition table always */
e594a3b1
LP
93} arg_empty = EMPTY_REFUSE;
94
95static bool arg_dry_run = true;
96static const char *arg_node = NULL;
97static char *arg_root = NULL;
252d6267 98static char *arg_image = NULL;
224c853f 99static char **arg_definitions = NULL;
e594a3b1
LP
100static bool arg_discard = true;
101static bool arg_can_factory_reset = false;
102static int arg_factory_reset = -1;
103static sd_id128_t arg_seed = SD_ID128_NULL;
104static bool arg_randomize = false;
105static int arg_pretty = -1;
a26f4a49 106static uint64_t arg_size = UINT64_MAX;
170c9823 107static bool arg_size_auto = false;
6a01ea4a 108static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
896e678b
LP
109static PagerFlags arg_pager_flags = 0;
110static bool arg_legend = true;
b9df3536
LP
111static void *arg_key = NULL;
112static size_t arg_key_size = 0;
889914ef
LP
113static char *arg_tpm2_device = NULL;
114static uint32_t arg_tpm2_pcr_mask = UINT32_MAX;
e594a3b1
LP
115
116STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
252d6267 117STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
224c853f 118STATIC_DESTRUCTOR_REGISTER(arg_definitions, strv_freep);
b9df3536 119STATIC_DESTRUCTOR_REGISTER(arg_key, erase_and_freep);
889914ef 120STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep);
e594a3b1
LP
121
122typedef struct Partition Partition;
123typedef struct FreeArea FreeArea;
124typedef struct Context Context;
125
889914ef
LP
126typedef enum EncryptMode {
127 ENCRYPT_OFF,
128 ENCRYPT_KEY_FILE,
129 ENCRYPT_TPM2,
130 ENCRYPT_KEY_FILE_TPM2,
131 _ENCRYPT_MODE_MAX,
2d93c20e 132 _ENCRYPT_MODE_INVALID = -EINVAL,
889914ef
LP
133} EncryptMode;
134
b5b7879a
DDM
135typedef enum VerityMode {
136 VERITY_OFF,
137 VERITY_DATA,
138 VERITY_HASH,
139 _VERITY_MODE_MAX,
140 _VERITY_MODE_INVALID = -EINVAL,
141} VerityMode;
142
e594a3b1
LP
143struct Partition {
144 char *definition_path;
39fc0174 145 char **drop_in_files;
e594a3b1
LP
146
147 sd_id128_t type_uuid;
148 sd_id128_t current_uuid, new_uuid;
11749b61 149 bool new_uuid_is_set;
e594a3b1
LP
150 char *current_label, *new_label;
151
152 bool dropped;
153 bool factory_reset;
154 int32_t priority;
155
156 uint32_t weight, padding_weight;
157
158 uint64_t current_size, new_size;
159 uint64_t size_min, size_max;
160
161 uint64_t current_padding, new_padding;
162 uint64_t padding_min, padding_max;
163
164 uint64_t partno;
165 uint64_t offset;
166
167 struct fdisk_partition *current_partition;
168 struct fdisk_partition *new_partition;
169 FreeArea *padding_area;
170 FreeArea *allocated_to_area;
171
757bc2e4 172 char *copy_blocks_path;
5c08da58 173 bool copy_blocks_auto;
757bc2e4
LP
174 int copy_blocks_fd;
175 uint64_t copy_blocks_size;
176
53171c04 177 char *format;
8a794850 178 char **copy_files;
d83d8048 179 char **make_directories;
889914ef 180 EncryptMode encrypt;
b5b7879a
DDM
181 VerityMode verity;
182 char *verity_match_key;
53171c04 183
e73309c5 184 uint64_t gpt_flags;
ff0771bf 185 int no_auto;
e73309c5 186 int read_only;
1c41c1dc 187 int growfs;
e73309c5 188
b5b7879a
DDM
189 uint8_t *roothash;
190 size_t roothash_size;
191
192 Partition *siblings[_VERITY_MODE_MAX];
193
e594a3b1
LP
194 LIST_FIELDS(Partition, partitions);
195};
196
197#define PARTITION_IS_FOREIGN(p) (!(p)->definition_path)
198#define PARTITION_EXISTS(p) (!!(p)->current_partition)
199
200struct FreeArea {
201 Partition *after;
202 uint64_t size;
203 uint64_t allocated;
204};
205
206struct Context {
207 LIST_HEAD(Partition, partitions);
208 size_t n_partitions;
209
210 FreeArea **free_areas;
319a4f4b 211 size_t n_free_areas;
e594a3b1
LP
212
213 uint64_t start, end, total;
214
215 struct fdisk_context *fdisk_context;
994b3031
LP
216 uint64_t sector_size;
217 uint64_t grain_size;
e594a3b1
LP
218
219 sd_id128_t seed;
220};
221
889914ef
LP
222static const char *encrypt_mode_table[_ENCRYPT_MODE_MAX] = {
223 [ENCRYPT_OFF] = "off",
224 [ENCRYPT_KEY_FILE] = "key-file",
225 [ENCRYPT_TPM2] = "tpm2",
226 [ENCRYPT_KEY_FILE_TPM2] = "key-file+tpm2",
227};
228
b5b7879a
DDM
229static const char *verity_mode_table[_VERITY_MODE_MAX] = {
230 [VERITY_OFF] = "off",
231 [VERITY_DATA] = "data",
232 [VERITY_HASH] = "hash",
233};
234
2709d029 235#if HAVE_LIBCRYPTSETUP
889914ef 236DEFINE_PRIVATE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(encrypt_mode, EncryptMode, ENCRYPT_KEY_FILE);
b5b7879a 237DEFINE_PRIVATE_STRING_TABLE_LOOKUP(verity_mode, VerityMode);
2709d029
MH
238#else
239DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(encrypt_mode, EncryptMode, ENCRYPT_KEY_FILE);
b5b7879a 240DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(verity_mode, VerityMode);
2709d029
MH
241#endif
242
889914ef 243
e594a3b1
LP
244static uint64_t round_down_size(uint64_t v, uint64_t p) {
245 return (v / p) * p;
246}
247
248static uint64_t round_up_size(uint64_t v, uint64_t p) {
249
250 v = DIV_ROUND_UP(v, p);
251
252 if (v > UINT64_MAX / p)
253 return UINT64_MAX; /* overflow */
254
255 return v * p;
256}
257
258static Partition *partition_new(void) {
259 Partition *p;
260
261 p = new(Partition, 1);
262 if (!p)
263 return NULL;
264
265 *p = (Partition) {
266 .weight = 1000,
267 .padding_weight = 0,
268 .current_size = UINT64_MAX,
269 .new_size = UINT64_MAX,
270 .size_min = UINT64_MAX,
271 .size_max = UINT64_MAX,
272 .current_padding = UINT64_MAX,
273 .new_padding = UINT64_MAX,
274 .padding_min = UINT64_MAX,
275 .padding_max = UINT64_MAX,
276 .partno = UINT64_MAX,
277 .offset = UINT64_MAX,
757bc2e4
LP
278 .copy_blocks_fd = -1,
279 .copy_blocks_size = UINT64_MAX,
ff0771bf 280 .no_auto = -1,
e73309c5 281 .read_only = -1,
1c41c1dc 282 .growfs = -1,
e594a3b1
LP
283 };
284
285 return p;
286}
287
288static Partition* partition_free(Partition *p) {
289 if (!p)
290 return NULL;
291
292 free(p->current_label);
293 free(p->new_label);
294 free(p->definition_path);
39fc0174 295 strv_free(p->drop_in_files);
e594a3b1
LP
296
297 if (p->current_partition)
298 fdisk_unref_partition(p->current_partition);
299 if (p->new_partition)
300 fdisk_unref_partition(p->new_partition);
301
757bc2e4
LP
302 free(p->copy_blocks_path);
303 safe_close(p->copy_blocks_fd);
304
53171c04 305 free(p->format);
8a794850 306 strv_free(p->copy_files);
d83d8048 307 strv_free(p->make_directories);
b5b7879a
DDM
308 free(p->verity_match_key);
309
310 free(p->roothash);
53171c04 311
e594a3b1
LP
312 return mfree(p);
313}
314
315static Partition* partition_unlink_and_free(Context *context, Partition *p) {
316 if (!p)
317 return NULL;
318
319 LIST_REMOVE(partitions, context->partitions, p);
320
321 assert(context->n_partitions > 0);
322 context->n_partitions--;
323
324 return partition_free(p);
325}
326
327DEFINE_TRIVIAL_CLEANUP_FUNC(Partition*, partition_free);
328
329static Context *context_new(sd_id128_t seed) {
330 Context *context;
331
332 context = new(Context, 1);
333 if (!context)
334 return NULL;
335
336 *context = (Context) {
337 .start = UINT64_MAX,
338 .end = UINT64_MAX,
339 .total = UINT64_MAX,
340 .seed = seed,
341 };
342
343 return context;
344}
345
346static void context_free_free_areas(Context *context) {
347 assert(context);
348
349 for (size_t i = 0; i < context->n_free_areas; i++)
350 free(context->free_areas[i]);
351
352 context->free_areas = mfree(context->free_areas);
353 context->n_free_areas = 0;
e594a3b1
LP
354}
355
356static Context *context_free(Context *context) {
357 if (!context)
358 return NULL;
359
360 while (context->partitions)
361 partition_unlink_and_free(context, context->partitions);
362 assert(context->n_partitions == 0);
363
364 context_free_free_areas(context);
365
366 if (context->fdisk_context)
367 fdisk_unref_context(context->fdisk_context);
368
369 return mfree(context);
370}
371
372DEFINE_TRIVIAL_CLEANUP_FUNC(Context*, context_free);
373
374static int context_add_free_area(
375 Context *context,
376 uint64_t size,
377 Partition *after) {
378
379 FreeArea *a;
380
381 assert(context);
382 assert(!after || !after->padding_area);
383
319a4f4b 384 if (!GREEDY_REALLOC(context->free_areas, context->n_free_areas + 1))
e594a3b1
LP
385 return -ENOMEM;
386
387 a = new(FreeArea, 1);
388 if (!a)
389 return -ENOMEM;
390
391 *a = (FreeArea) {
392 .size = size,
393 .after = after,
394 };
395
396 context->free_areas[context->n_free_areas++] = a;
397
398 if (after)
399 after->padding_area = a;
400
401 return 0;
402}
403
404static bool context_drop_one_priority(Context *context) {
405 int32_t priority = 0;
e594a3b1
LP
406 bool exists = false;
407
408 LIST_FOREACH(partitions, p, context->partitions) {
409 if (p->dropped)
410 continue;
411 if (p->priority < priority)
412 continue;
413 if (p->priority == priority) {
414 exists = exists || PARTITION_EXISTS(p);
415 continue;
416 }
417
418 priority = p->priority;
419 exists = PARTITION_EXISTS(p);
420 }
421
422 /* Refuse to drop partitions with 0 or negative priorities or partitions of priorities that have at
423 * least one existing priority */
424 if (priority <= 0 || exists)
425 return false;
426
427 LIST_FOREACH(partitions, p, context->partitions) {
428 if (p->priority < priority)
429 continue;
430
431 if (p->dropped)
432 continue;
433
434 p->dropped = true;
435 log_info("Can't fit partition %s of priority %" PRIi32 ", dropping.", p->definition_path, p->priority);
b5b7879a
DDM
436
437 /* We ensure that all verity sibling partitions have the same priority, so it's safe
438 * to drop all siblings here as well. */
439
440 for (VerityMode mode = VERITY_OFF + 1; mode < _VERITY_MODE_MAX; mode++) {
441 if (!p->siblings[mode])
442 continue;
443
444 if (p->siblings[mode]->dropped)
445 continue;
446
447 p->siblings[mode]->dropped = true;
448 log_info("Also dropping sibling verity %s partition %s",
449 verity_mode_to_string(mode), p->siblings[mode]->definition_path);
450 }
e594a3b1
LP
451 }
452
453 return true;
454}
455
994b3031 456static uint64_t partition_min_size(Context *context, const Partition *p) {
e594a3b1
LP
457 uint64_t sz;
458
994b3031
LP
459 assert(context);
460 assert(p);
461
e594a3b1
LP
462 /* Calculate the disk space we really need at minimum for this partition. If the partition already
463 * exists the current size is what we really need. If it doesn't exist yet refuse to allocate less
fb08381c
LP
464 * than 4K.
465 *
466 * DEFAULT_MIN_SIZE is the default SizeMin= we configure if nothing else is specified. */
e594a3b1
LP
467
468 if (PARTITION_IS_FOREIGN(p)) {
469 /* Don't allow changing size of partitions not managed by us */
470 assert(p->current_size != UINT64_MAX);
471 return p->current_size;
472 }
473
fb08381c 474 sz = p->current_size != UINT64_MAX ? p->current_size : HARD_MIN_SIZE;
757bc2e4 475
170c9823
LP
476 if (!PARTITION_EXISTS(p)) {
477 uint64_t d = 0;
478
889914ef 479 if (p->encrypt != ENCRYPT_OFF)
994b3031 480 d += round_up_size(LUKS2_METADATA_SIZE, context->grain_size);
170c9823
LP
481
482 if (p->copy_blocks_size != UINT64_MAX)
994b3031 483 d += round_up_size(p->copy_blocks_size, context->grain_size);
889914ef 484 else if (p->format || p->encrypt != ENCRYPT_OFF) {
170c9823
LP
485 uint64_t f;
486
487 /* If we shall synthesize a file system, take minimal fs size into account (assumed to be 4K if not known) */
994b3031
LP
488 f = p->format ? round_up_size(minimal_size_by_fs_name(p->format), context->grain_size) : UINT64_MAX;
489 d += f == UINT64_MAX ? context->grain_size : f;
170c9823
LP
490 }
491
492 if (d > sz)
493 sz = d;
494 }
757bc2e4 495
994b3031 496 return MAX(round_up_size(p->size_min != UINT64_MAX ? p->size_min : DEFAULT_MIN_SIZE, context->grain_size), sz);
e594a3b1
LP
497}
498
994b3031
LP
499static uint64_t partition_max_size(const Context *context, const Partition *p) {
500 uint64_t sm;
501
e594a3b1
LP
502 /* Calculate how large the partition may become at max. This is generally the configured maximum
503 * size, except when it already exists and is larger than that. In that case it's the existing size,
504 * since we never want to shrink partitions. */
505
994b3031
LP
506 assert(context);
507 assert(p);
508
e594a3b1
LP
509 if (PARTITION_IS_FOREIGN(p)) {
510 /* Don't allow changing size of partitions not managed by us */
511 assert(p->current_size != UINT64_MAX);
512 return p->current_size;
513 }
514
994b3031
LP
515 sm = round_down_size(p->size_max, context->grain_size);
516
e594a3b1 517 if (p->current_size != UINT64_MAX)
994b3031 518 return MAX(p->current_size, sm);
e594a3b1 519
994b3031 520 return sm;
e594a3b1
LP
521}
522
994b3031 523static uint64_t partition_min_size_with_padding(Context *context, const Partition *p) {
e594a3b1
LP
524 uint64_t sz;
525
526 /* Calculate the disk space we need for this partition plus any free space coming after it. This
527 * takes user configured padding into account as well as any additional whitespace needed to align
528 * the next partition to 4K again. */
529
994b3031
LP
530 assert(context);
531 assert(p);
532
533 sz = partition_min_size(context, p);
e594a3b1
LP
534
535 if (p->padding_min != UINT64_MAX)
536 sz += p->padding_min;
537
538 if (PARTITION_EXISTS(p)) {
539 /* If the partition wasn't aligned, add extra space so that any we might add will be aligned */
540 assert(p->offset != UINT64_MAX);
994b3031 541 return round_up_size(p->offset + sz, context->grain_size) - p->offset;
e594a3b1
LP
542 }
543
544 /* If this is a new partition we'll place it aligned, hence we just need to round up the required size here */
994b3031 545 return round_up_size(sz, context->grain_size);
e594a3b1
LP
546}
547
548static uint64_t free_area_available(const FreeArea *a) {
549 assert(a);
550
551 /* Determines how much of this free area is not allocated yet */
552
553 assert(a->size >= a->allocated);
554 return a->size - a->allocated;
555}
556
994b3031 557static uint64_t free_area_available_for_new_partitions(Context *context, const FreeArea *a) {
e594a3b1
LP
558 uint64_t avail;
559
994b3031
LP
560 assert(context);
561 assert(a);
562
e594a3b1 563 /* Similar to free_area_available(), but takes into account that the required size and padding of the
162392b7 564 * preceding partition is honoured. */
e594a3b1
LP
565
566 avail = free_area_available(a);
567 if (a->after) {
1052a114 568 uint64_t need, space_end, new_end;
e594a3b1 569
994b3031 570 need = partition_min_size_with_padding(context, a->after);
e594a3b1
LP
571
572 assert(a->after->offset != UINT64_MAX);
573 assert(a->after->current_size != UINT64_MAX);
574
1052a114 575 /* Calculate where the free area ends, based on the offset of the partition preceding it */
994b3031 576 space_end = round_up_size(a->after->offset + a->after->current_size, context->grain_size) + avail;
e594a3b1 577
1052a114 578 /* Calculate where the partition would end when we give it as much as it needs */
994b3031 579 new_end = round_up_size(a->after->offset + need, context->grain_size);
1052a114
LP
580
581 /* Calculate saturated difference of the two: that's how much we have free for other partitions */
582 return LESS_BY(space_end, new_end);
e594a3b1
LP
583 }
584
585 return avail;
586}
587
994b3031
LP
588static int free_area_compare(FreeArea *const *a, FreeArea *const*b, Context *context) {
589 assert(context);
590
591 return CMP(free_area_available_for_new_partitions(context, *a),
592 free_area_available_for_new_partitions(context, *b));
e594a3b1
LP
593}
594
994b3031
LP
595static uint64_t charge_size(Context *context, uint64_t total, uint64_t amount) {
596 assert(context);
e594a3b1 597 /* Subtract the specified amount from total, rounding up to multiple of 4K if there's room */
184cf99a 598 assert(amount <= total);
994b3031 599 return LESS_BY(total, round_up_size(amount, context->grain_size));
e594a3b1
LP
600}
601
602static uint64_t charge_weight(uint64_t total, uint64_t amount) {
603 assert(amount <= total);
604 return total - amount;
605}
606
14a4c4ed 607static bool context_allocate_partitions(Context *context, uint64_t *ret_largest_free_area) {
e594a3b1
LP
608 assert(context);
609
14a4c4ed 610 /* Sort free areas by size, putting smallest first */
994b3031 611 typesafe_qsort_r(context->free_areas, context->n_free_areas, free_area_compare, context);
e594a3b1 612
14a4c4ed
LP
613 /* In any case return size of the largest free area (i.e. not the size of all free areas
614 * combined!) */
615 if (ret_largest_free_area)
616 *ret_largest_free_area =
617 context->n_free_areas == 0 ? 0 :
994b3031 618 free_area_available_for_new_partitions(context, context->free_areas[context->n_free_areas-1]);
14a4c4ed
LP
619
620 /* A simple first-fit algorithm. We return true if we can fit the partitions in, otherwise false. */
e594a3b1
LP
621 LIST_FOREACH(partitions, p, context->partitions) {
622 bool fits = false;
623 uint64_t required;
624 FreeArea *a = NULL;
625
626 /* Skip partitions we already dropped or that already exist */
627 if (p->dropped || PARTITION_EXISTS(p))
628 continue;
629
e594a3b1 630 /* How much do we need to fit? */
994b3031
LP
631 required = partition_min_size_with_padding(context, p);
632 assert(required % context->grain_size == 0);
e594a3b1
LP
633
634 for (size_t i = 0; i < context->n_free_areas; i++) {
635 a = context->free_areas[i];
636
994b3031 637 if (free_area_available_for_new_partitions(context, a) >= required) {
e594a3b1
LP
638 fits = true;
639 break;
640 }
641 }
642
643 if (!fits)
644 return false; /* 😢 Oh no! We can't fit this partition into any free area! */
645
646 /* Assign the partition to this free area */
647 p->allocated_to_area = a;
648
649 /* Budget the minimal partition size */
650 a->allocated += required;
651 }
652
653 return true;
654}
655
656static int context_sum_weights(Context *context, FreeArea *a, uint64_t *ret) {
657 uint64_t weight_sum = 0;
e594a3b1
LP
658
659 assert(context);
660 assert(a);
661 assert(ret);
662
663 /* Determine the sum of the weights of all partitions placed in or before the specified free area */
664
665 LIST_FOREACH(partitions, p, context->partitions) {
666 if (p->padding_area != a && p->allocated_to_area != a)
667 continue;
668
669 if (p->weight > UINT64_MAX - weight_sum)
670 goto overflow_sum;
671 weight_sum += p->weight;
672
673 if (p->padding_weight > UINT64_MAX - weight_sum)
674 goto overflow_sum;
675 weight_sum += p->padding_weight;
676 }
677
678 *ret = weight_sum;
679 return 0;
680
681overflow_sum:
682 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Combined weight of partition exceeds unsigned 64bit range, refusing.");
683}
684
685static int scale_by_weight(uint64_t value, uint64_t weight, uint64_t weight_sum, uint64_t *ret) {
686 assert(weight_sum >= weight);
687 assert(ret);
688
689 if (weight == 0) {
690 *ret = 0;
691 return 0;
692 }
693
694 if (value > UINT64_MAX / weight)
695 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Scaling by weight of partition exceeds unsigned 64bit range, refusing.");
696
697 *ret = value * weight / weight_sum;
698 return 0;
699}
700
701typedef enum GrowPartitionPhase {
702 /* The first phase: we charge partitions which need more (according to constraints) than their weight-based share. */
703 PHASE_OVERCHARGE,
704
705 /* The second phase: we charge partitions which need less (according to constraints) than their weight-based share. */
706 PHASE_UNDERCHARGE,
707
708 /* The third phase: we distribute what remains among the remaining partitions, according to the weights */
709 PHASE_DISTRIBUTE,
ae0613c6
LP
710
711 _GROW_PARTITION_PHASE_MAX,
e594a3b1
LP
712} GrowPartitionPhase;
713
714static int context_grow_partitions_phase(
715 Context *context,
716 FreeArea *a,
717 GrowPartitionPhase phase,
718 uint64_t *span,
719 uint64_t *weight_sum) {
720
e594a3b1
LP
721 int r;
722
723 assert(context);
724 assert(a);
725
726 /* Now let's look at the intended weights and adjust them taking the minimum space assignments into
727 * account. i.e. if a partition has a small weight but a high minimum space value set it should not
728 * get any additional room from the left-overs. Similar, if two partitions have the same weight they
729 * should get the same space if possible, even if one has a smaller minimum size than the other. */
730 LIST_FOREACH(partitions, p, context->partitions) {
731
732 /* Look only at partitions associated with this free area, i.e. immediately
162392b7 733 * preceding it, or allocated into it */
e594a3b1
LP
734 if (p->allocated_to_area != a && p->padding_area != a)
735 continue;
736
737 if (p->new_size == UINT64_MAX) {
738 bool charge = false, try_again = false;
739 uint64_t share, rsz, xsz;
740
741 /* Calculate how much this space this partition needs if everyone would get
742 * the weight based share */
743 r = scale_by_weight(*span, p->weight, *weight_sum, &share);
744 if (r < 0)
745 return r;
746
994b3031
LP
747 rsz = partition_min_size(context, p);
748 xsz = partition_max_size(context, p);
e594a3b1
LP
749
750 if (phase == PHASE_OVERCHARGE && rsz > share) {
751 /* This partition needs more than its calculated share. Let's assign
752 * it that, and take this partition out of all calculations and start
753 * again. */
754
755 p->new_size = rsz;
756 charge = try_again = true;
757
758 } else if (phase == PHASE_UNDERCHARGE && xsz != UINT64_MAX && xsz < share) {
759 /* This partition accepts less than its calculated
760 * share. Let's assign it that, and take this partition out
761 * of all calculations and start again. */
762
763 p->new_size = xsz;
764 charge = try_again = true;
765
766 } else if (phase == PHASE_DISTRIBUTE) {
767 /* This partition can accept its calculated share. Let's
768 * assign it. There's no need to restart things here since
769 * assigning this shouldn't impact the shares of the other
770 * partitions. */
771
772 if (PARTITION_IS_FOREIGN(p))
773 /* Never change of foreign partitions (i.e. those we don't manage) */
774 p->new_size = p->current_size;
775 else
994b3031 776 p->new_size = MAX(round_down_size(share, context->grain_size), rsz);
e594a3b1
LP
777
778 charge = true;
779 }
780
781 if (charge) {
994b3031 782 *span = charge_size(context, *span, p->new_size);
e594a3b1
LP
783 *weight_sum = charge_weight(*weight_sum, p->weight);
784 }
785
786 if (try_again)
787 return 0; /* try again */
788 }
789
790 if (p->new_padding == UINT64_MAX) {
791 bool charge = false, try_again = false;
792 uint64_t share;
793
794 r = scale_by_weight(*span, p->padding_weight, *weight_sum, &share);
795 if (r < 0)
796 return r;
797
798 if (phase == PHASE_OVERCHARGE && p->padding_min != UINT64_MAX && p->padding_min > share) {
799 p->new_padding = p->padding_min;
800 charge = try_again = true;
801 } else if (phase == PHASE_UNDERCHARGE && p->padding_max != UINT64_MAX && p->padding_max < share) {
802 p->new_padding = p->padding_max;
803 charge = try_again = true;
804 } else if (phase == PHASE_DISTRIBUTE) {
805
994b3031 806 p->new_padding = round_down_size(share, context->grain_size);
e594a3b1
LP
807 if (p->padding_min != UINT64_MAX && p->new_padding < p->padding_min)
808 p->new_padding = p->padding_min;
809
810 charge = true;
811 }
812
813 if (charge) {
994b3031 814 *span = charge_size(context, *span, p->new_padding);
e594a3b1
LP
815 *weight_sum = charge_weight(*weight_sum, p->padding_weight);
816 }
817
818 if (try_again)
819 return 0; /* try again */
820 }
821 }
822
823 return 1; /* done */
824}
825
826static int context_grow_partitions_on_free_area(Context *context, FreeArea *a) {
827 uint64_t weight_sum = 0, span;
828 int r;
829
830 assert(context);
831 assert(a);
832
833 r = context_sum_weights(context, a, &weight_sum);
834 if (r < 0)
835 return r;
836
837 /* Let's calculate the total area covered by this free area and the partition before it */
838 span = a->size;
839 if (a->after) {
840 assert(a->after->offset != UINT64_MAX);
841 assert(a->after->current_size != UINT64_MAX);
842
994b3031 843 span += round_up_size(a->after->offset + a->after->current_size, context->grain_size) - a->after->offset;
e594a3b1
LP
844 }
845
ae0613c6 846 for (GrowPartitionPhase phase = 0; phase < _GROW_PARTITION_PHASE_MAX;) {
e594a3b1
LP
847 r = context_grow_partitions_phase(context, a, phase, &span, &weight_sum);
848 if (r < 0)
849 return r;
850 if (r == 0) /* not done yet, re-run this phase */
851 continue;
852
ae0613c6 853 phase++; /* got to next phase */
e594a3b1
LP
854 }
855
162392b7 856 /* We still have space left over? Donate to preceding partition if we have one */
e594a3b1
LP
857 if (span > 0 && a->after && !PARTITION_IS_FOREIGN(a->after)) {
858 uint64_t m, xsz;
859
860 assert(a->after->new_size != UINT64_MAX);
0b7f574f
LP
861
862 /* Calculate new size and align (but ensure this doesn't shrink the size) */
994b3031 863 m = MAX(a->after->new_size, round_down_size(a->after->new_size + span, context->grain_size));
e594a3b1 864
994b3031 865 xsz = partition_max_size(context, a->after);
e594a3b1
LP
866 if (xsz != UINT64_MAX && m > xsz)
867 m = xsz;
868
994b3031 869 span = charge_size(context, span, m - a->after->new_size);
e594a3b1
LP
870 a->after->new_size = m;
871 }
872
162392b7 873 /* What? Even still some space left (maybe because there was no preceding partition, or it had a
e594a3b1 874 * size limit), then let's donate it to whoever wants it. */
03677889 875 if (span > 0)
e594a3b1
LP
876 LIST_FOREACH(partitions, p, context->partitions) {
877 uint64_t m, xsz;
878
879 if (p->allocated_to_area != a)
880 continue;
881
882 if (PARTITION_IS_FOREIGN(p))
883 continue;
884
885 assert(p->new_size != UINT64_MAX);
994b3031 886 m = MAX(p->new_size, round_down_size(p->new_size + span, context->grain_size));
e594a3b1 887
994b3031 888 xsz = partition_max_size(context, p);
e594a3b1
LP
889 if (xsz != UINT64_MAX && m > xsz)
890 m = xsz;
891
994b3031 892 span = charge_size(context, span, m - p->new_size);
e594a3b1
LP
893 p->new_size = m;
894
895 if (span == 0)
896 break;
897 }
e594a3b1 898
162392b7 899 /* Yuck, still no one? Then make it padding */
e594a3b1
LP
900 if (span > 0 && a->after) {
901 assert(a->after->new_padding != UINT64_MAX);
902 a->after->new_padding += span;
903 }
904
905 return 0;
906}
907
908static int context_grow_partitions(Context *context) {
e594a3b1
LP
909 int r;
910
911 assert(context);
912
913 for (size_t i = 0; i < context->n_free_areas; i++) {
914 r = context_grow_partitions_on_free_area(context, context->free_areas[i]);
915 if (r < 0)
916 return r;
917 }
918
919 /* All existing partitions that have no free space after them can't change size */
920 LIST_FOREACH(partitions, p, context->partitions) {
921 if (p->dropped)
922 continue;
923
924 if (!PARTITION_EXISTS(p) || p->padding_area) {
925 /* The algorithm above must have initialized this already */
926 assert(p->new_size != UINT64_MAX);
927 continue;
928 }
929
930 assert(p->new_size == UINT64_MAX);
931 p->new_size = p->current_size;
932
933 assert(p->new_padding == UINT64_MAX);
934 p->new_padding = p->current_padding;
935 }
936
937 return 0;
938}
939
940static void context_place_partitions(Context *context) {
941 uint64_t partno = 0;
e594a3b1
LP
942
943 assert(context);
944
945 /* Determine next partition number to assign */
946 LIST_FOREACH(partitions, p, context->partitions) {
947 if (!PARTITION_EXISTS(p))
948 continue;
949
950 assert(p->partno != UINT64_MAX);
951 if (p->partno >= partno)
952 partno = p->partno + 1;
953 }
954
955 for (size_t i = 0; i < context->n_free_areas; i++) {
956 FreeArea *a = context->free_areas[i];
2ea7eb00
FS
957 _unused_ uint64_t left;
958 uint64_t start;
e594a3b1
LP
959
960 if (a->after) {
961 assert(a->after->offset != UINT64_MAX);
962 assert(a->after->new_size != UINT64_MAX);
963 assert(a->after->new_padding != UINT64_MAX);
964
965 start = a->after->offset + a->after->new_size + a->after->new_padding;
966 } else
967 start = context->start;
968
994b3031 969 start = round_up_size(start, context->grain_size);
e594a3b1
LP
970 left = a->size;
971
972 LIST_FOREACH(partitions, p, context->partitions) {
973 if (p->allocated_to_area != a)
974 continue;
975
976 p->offset = start;
977 p->partno = partno++;
978
979 assert(left >= p->new_size);
980 start += p->new_size;
981 left -= p->new_size;
982
983 assert(left >= p->new_padding);
984 start += p->new_padding;
985 left -= p->new_padding;
986 }
987 }
988}
989
e594a3b1
LP
990static int config_parse_type(
991 const char *unit,
992 const char *filename,
993 unsigned line,
994 const char *section,
995 unsigned section_line,
996 const char *lvalue,
997 int ltype,
998 const char *rvalue,
999 void *data,
1000 void *userdata) {
1001
1002 sd_id128_t *type_uuid = data;
1003 int r;
1004
1005 assert(rvalue);
1006 assert(type_uuid);
1007
1008 r = gpt_partition_type_uuid_from_string(rvalue, type_uuid);
1009 if (r < 0)
1010 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse partition type: %s", rvalue);
1011
1012 return 0;
1013}
1014
1015static int config_parse_label(
1016 const char *unit,
1017 const char *filename,
1018 unsigned line,
1019 const char *section,
1020 unsigned section_line,
1021 const char *lvalue,
1022 int ltype,
1023 const char *rvalue,
1024 void *data,
1025 void *userdata) {
1026
e031166e 1027 _cleanup_free_ char *resolved = NULL;
e594a3b1
LP
1028 char **label = data;
1029 int r;
1030
1031 assert(rvalue);
1032 assert(label);
1033
be9ce018
LP
1034 /* Nota bene: the empty label is a totally valid one. Let's hence not follow our usual rule of
1035 * assigning the empty string to reset to default here, but really accept it as label to set. */
1036
de61a04b 1037 r = specifier_printf(rvalue, GPT_LABEL_MAX, system_and_tmp_specifier_table, arg_root, NULL, &resolved);
e031166e 1038 if (r < 0) {
e459258f 1039 log_syntax(unit, LOG_WARNING, filename, line, r,
e031166e
LP
1040 "Failed to expand specifiers in Label=, ignoring: %s", rvalue);
1041 return 0;
1042 }
1043
1044 if (!utf8_is_valid(resolved)) {
e594a3b1
LP
1045 log_syntax(unit, LOG_WARNING, filename, line, 0,
1046 "Partition label not valid UTF-8, ignoring: %s", rvalue);
1047 return 0;
1048 }
1049
22a0a36e
LP
1050 r = gpt_partition_label_valid(resolved);
1051 if (r < 0) {
1052 log_syntax(unit, LOG_WARNING, filename, line, r,
1053 "Failed to check if string is valid as GPT partition label, ignoring: \"%s\" (from \"%s\")",
1054 resolved, rvalue);
1055 return 0;
1056 }
1057 if (!r) {
e594a3b1 1058 log_syntax(unit, LOG_WARNING, filename, line, 0,
46072ae3
ZJS
1059 "Partition label too long for GPT table, ignoring: \"%s\" (from \"%s\")",
1060 resolved, rvalue);
e594a3b1
LP
1061 return 0;
1062 }
1063
e031166e 1064 free_and_replace(*label, resolved);
e594a3b1
LP
1065 return 0;
1066}
1067
1068static int config_parse_weight(
1069 const char *unit,
1070 const char *filename,
1071 unsigned line,
1072 const char *section,
1073 unsigned section_line,
1074 const char *lvalue,
1075 int ltype,
1076 const char *rvalue,
1077 void *data,
1078 void *userdata) {
1079
1080 uint32_t *priority = data, v;
1081 int r;
1082
1083 assert(rvalue);
1084 assert(priority);
1085
1086 r = safe_atou32(rvalue, &v);
1087 if (r < 0) {
1088 log_syntax(unit, LOG_WARNING, filename, line, r,
1089 "Failed to parse weight value, ignoring: %s", rvalue);
1090 return 0;
1091 }
1092
1093 if (v > 1000U*1000U) {
c8f3d767 1094 log_syntax(unit, LOG_WARNING, filename, line, 0,
e594a3b1
LP
1095 "Weight needs to be in range 0…10000000, ignoring: %" PRIu32, v);
1096 return 0;
1097 }
1098
1099 *priority = v;
1100 return 0;
1101}
1102
1103static int config_parse_size4096(
1104 const char *unit,
1105 const char *filename,
1106 unsigned line,
1107 const char *section,
1108 unsigned section_line,
1109 const char *lvalue,
1110 int ltype,
1111 const char *rvalue,
1112 void *data,
1113 void *userdata) {
1114
1115 uint64_t *sz = data, parsed;
1116 int r;
1117
1118 assert(rvalue);
1119 assert(data);
1120
1121 r = parse_size(rvalue, 1024, &parsed);
1122 if (r < 0)
c8f3d767 1123 return log_syntax(unit, LOG_ERR, filename, line, r,
e594a3b1
LP
1124 "Failed to parse size value: %s", rvalue);
1125
1126 if (ltype > 0)
1127 *sz = round_up_size(parsed, 4096);
1128 else if (ltype < 0)
1129 *sz = round_down_size(parsed, 4096);
1130 else
1131 *sz = parsed;
1132
1133 if (*sz != parsed)
e2341b6b
DT
1134 log_syntax(unit, LOG_NOTICE, filename, line, r, "Rounded %s= size %" PRIu64 " %s %" PRIu64 ", a multiple of 4096.",
1135 lvalue, parsed, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), *sz);
e594a3b1
LP
1136
1137 return 0;
1138}
1139
53171c04
LP
1140static int config_parse_fstype(
1141 const char *unit,
1142 const char *filename,
1143 unsigned line,
1144 const char *section,
1145 unsigned section_line,
1146 const char *lvalue,
1147 int ltype,
1148 const char *rvalue,
1149 void *data,
1150 void *userdata) {
1151
1152 char **fstype = data;
1153
1154 assert(rvalue);
1155 assert(data);
1156
1157 if (!filename_is_valid(rvalue))
1158 return log_syntax(unit, LOG_ERR, filename, line, 0,
1159 "File system type is not valid, refusing: %s", rvalue);
1160
1161 return free_and_strdup_warn(fstype, rvalue);
1162}
1163
8a794850
LP
1164static int config_parse_copy_files(
1165 const char *unit,
1166 const char *filename,
1167 unsigned line,
1168 const char *section,
1169 unsigned section_line,
1170 const char *lvalue,
1171 int ltype,
1172 const char *rvalue,
1173 void *data,
1174 void *userdata) {
1175
1176 _cleanup_free_ char *source = NULL, *buffer = NULL, *resolved_source = NULL, *resolved_target = NULL;
1177 const char *p = rvalue, *target;
1178 Partition *partition = data;
1179 int r;
1180
1181 assert(rvalue);
1182 assert(partition);
1183
1184 r = extract_first_word(&p, &source, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS);
1185 if (r < 0)
1186 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract source path: %s", rvalue);
1187 if (r == 0) {
1188 log_syntax(unit, LOG_WARNING, filename, line, 0, "No argument specified: %s", rvalue);
1189 return 0;
1190 }
1191
1192 r = extract_first_word(&p, &buffer, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS);
1193 if (r < 0)
1194 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract target path: %s", rvalue);
1195 if (r == 0)
1196 target = source; /* No target, then it's the same as the source */
1197 else
1198 target = buffer;
1199
1200 if (!isempty(p))
1201 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), "Too many arguments: %s", rvalue);
1202
de61a04b 1203 r = specifier_printf(source, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_source);
8a794850
LP
1204 if (r < 0) {
1205 log_syntax(unit, LOG_WARNING, filename, line, r,
1206 "Failed to expand specifiers in CopyFiles= source, ignoring: %s", rvalue);
1207 return 0;
1208 }
1209
0ade2213
LP
1210 r = path_simplify_and_warn(resolved_source, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1211 if (r < 0)
8a794850 1212 return 0;
8a794850 1213
de61a04b 1214 r = specifier_printf(target, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_target);
8a794850
LP
1215 if (r < 0) {
1216 log_syntax(unit, LOG_WARNING, filename, line, r,
1217 "Failed to expand specifiers in CopyFiles= target, ignoring: %s", resolved_target);
1218 return 0;
1219 }
1220
0ade2213
LP
1221 r = path_simplify_and_warn(resolved_target, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1222 if (r < 0)
8a794850 1223 return 0;
8a794850
LP
1224
1225 r = strv_consume_pair(&partition->copy_files, TAKE_PTR(resolved_source), TAKE_PTR(resolved_target));
1226 if (r < 0)
1227 return log_oom();
1228
1229 return 0;
1230}
1231
5c08da58
LP
1232static int config_parse_copy_blocks(
1233 const char *unit,
1234 const char *filename,
1235 unsigned line,
1236 const char *section,
1237 unsigned section_line,
1238 const char *lvalue,
1239 int ltype,
1240 const char *rvalue,
1241 void *data,
1242 void *userdata) {
1243
1244 _cleanup_free_ char *d = NULL;
1245 Partition *partition = data;
1246 int r;
1247
1248 assert(rvalue);
1249 assert(partition);
1250
1251 if (isempty(rvalue)) {
1252 partition->copy_blocks_path = mfree(partition->copy_blocks_path);
1253 partition->copy_blocks_auto = false;
1254 return 0;
1255 }
1256
1257 if (streq(rvalue, "auto")) {
1258 partition->copy_blocks_path = mfree(partition->copy_blocks_path);
1259 partition->copy_blocks_auto = true;
1260 return 0;
1261 }
1262
de61a04b 1263 r = specifier_printf(rvalue, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &d);
5c08da58
LP
1264 if (r < 0) {
1265 log_syntax(unit, LOG_WARNING, filename, line, r,
1266 "Failed to expand specifiers in CopyBlocks= source path, ignoring: %s", rvalue);
1267 return 0;
1268 }
1269
1270 r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1271 if (r < 0)
1272 return 0;
1273
1274 free_and_replace(partition->copy_blocks_path, d);
1275 partition->copy_blocks_auto = false;
1276 return 0;
1277}
1278
d83d8048
LP
1279static int config_parse_make_dirs(
1280 const char *unit,
1281 const char *filename,
1282 unsigned line,
1283 const char *section,
1284 unsigned section_line,
1285 const char *lvalue,
1286 int ltype,
1287 const char *rvalue,
1288 void *data,
1289 void *userdata) {
1290
1291 Partition *partition = data;
1292 const char *p = rvalue;
1293 int r;
1294
1295 assert(rvalue);
1296 assert(partition);
1297
1298 for (;;) {
1299 _cleanup_free_ char *word = NULL, *d = NULL;
1300
1301 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
1302 if (r == -ENOMEM)
1303 return log_oom();
1304 if (r < 0) {
1305 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
1306 return 0;
1307 }
1308 if (r == 0)
1309 return 0;
1310
de61a04b 1311 r = specifier_printf(word, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &d);
d83d8048
LP
1312 if (r < 0) {
1313 log_syntax(unit, LOG_WARNING, filename, line, r,
1314 "Failed to expand specifiers in MakeDirectories= parameter, ignoring: %s", word);
1315 continue;
1316 }
1317
1318 r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1319 if (r < 0)
1320 continue;
1321
1322 r = strv_consume(&partition->make_directories, TAKE_PTR(d));
1323 if (r < 0)
1324 return log_oom();
1325 }
1326}
1327
889914ef
LP
1328static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_encrypt, encrypt_mode, EncryptMode, ENCRYPT_OFF, "Invalid encryption mode");
1329
e73309c5
LP
1330static int config_parse_gpt_flags(
1331 const char *unit,
1332 const char *filename,
1333 unsigned line,
1334 const char *section,
1335 unsigned section_line,
1336 const char *lvalue,
1337 int ltype,
1338 const char *rvalue,
1339 void *data,
1340 void *userdata) {
1341
1342 uint64_t *gpt_flags = data;
1343 int r;
1344
1345 assert(rvalue);
1346 assert(gpt_flags);
1347
1348 r = safe_atou64(rvalue, gpt_flags);
1349 if (r < 0) {
1350 log_syntax(unit, LOG_WARNING, filename, line, r,
1351 "Failed to parse Flags= value, ignoring: %s", rvalue);
1352 return 0;
1353 }
1354
1355 return 0;
1356}
1357
11749b61
DDM
1358static int config_parse_uuid(
1359 const char *unit,
1360 const char *filename,
1361 unsigned line,
1362 const char *section,
1363 unsigned section_line,
1364 const char *lvalue,
1365 int ltype,
1366 const char *rvalue,
1367 void *data,
1368 void *userdata) {
1369
1370 Partition *partition = ASSERT_PTR(data);
1371 int r;
1372
1373 if (isempty(rvalue)) {
1374 partition->new_uuid = SD_ID128_NULL;
1375 partition->new_uuid_is_set = false;
1376 return 0;
1377 }
1378
1379 if (streq(rvalue, "null")) {
1380 partition->new_uuid = SD_ID128_NULL;
1381 partition->new_uuid_is_set = true;
1382 return 0;
1383 }
1384
1385 r = sd_id128_from_string(rvalue, &partition->new_uuid);
1386 if (r < 0) {
1387 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse 128bit ID/UUID, ignoring: %s", rvalue);
1388 return 0;
1389 }
1390
1391 partition->new_uuid_is_set = true;
1392
1393 return 0;
1394}
1395
b5b7879a
DDM
1396static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_verity, verity_mode, VerityMode, VERITY_OFF, "Invalid verity mode");
1397
39fc0174 1398static int partition_read_definition(Partition *p, const char *path, const char *const *conf_file_dirs) {
e594a3b1
LP
1399
1400 ConfigTableItem table[] = {
5c08da58
LP
1401 { "Partition", "Type", config_parse_type, 0, &p->type_uuid },
1402 { "Partition", "Label", config_parse_label, 0, &p->new_label },
11749b61 1403 { "Partition", "UUID", config_parse_uuid, 0, p },
5c08da58
LP
1404 { "Partition", "Priority", config_parse_int32, 0, &p->priority },
1405 { "Partition", "Weight", config_parse_weight, 0, &p->weight },
1406 { "Partition", "PaddingWeight", config_parse_weight, 0, &p->padding_weight },
1407 { "Partition", "SizeMinBytes", config_parse_size4096, 1, &p->size_min },
1408 { "Partition", "SizeMaxBytes", config_parse_size4096, -1, &p->size_max },
1409 { "Partition", "PaddingMinBytes", config_parse_size4096, 1, &p->padding_min },
1410 { "Partition", "PaddingMaxBytes", config_parse_size4096, -1, &p->padding_max },
1411 { "Partition", "FactoryReset", config_parse_bool, 0, &p->factory_reset },
1412 { "Partition", "CopyBlocks", config_parse_copy_blocks, 0, p },
1413 { "Partition", "Format", config_parse_fstype, 0, &p->format },
1414 { "Partition", "CopyFiles", config_parse_copy_files, 0, p },
1415 { "Partition", "MakeDirectories", config_parse_make_dirs, 0, p },
1416 { "Partition", "Encrypt", config_parse_encrypt, 0, &p->encrypt },
b5b7879a
DDM
1417 { "Partition", "Verity", config_parse_verity, 0, &p->verity },
1418 { "Partition", "VerityMatchKey", config_parse_string, 0, &p->verity_match_key },
e73309c5
LP
1419 { "Partition", "Flags", config_parse_gpt_flags, 0, &p->gpt_flags },
1420 { "Partition", "ReadOnly", config_parse_tristate, 0, &p->read_only },
ff0771bf 1421 { "Partition", "NoAuto", config_parse_tristate, 0, &p->no_auto },
1c41c1dc 1422 { "Partition", "GrowFileSystem", config_parse_tristate, 0, &p->growfs },
e594a3b1
LP
1423 {}
1424 };
1425 int r;
39fc0174
RP
1426 _cleanup_free_ char *filename = NULL;
1427 const char* dropin_dirname;
e594a3b1 1428
39fc0174
RP
1429 r = path_extract_filename(path, &filename);
1430 if (r < 0)
1431 return log_error_errno(r, "Failed to extract filename from path '%s': %m", path);;
1432
1433 dropin_dirname = strjoina(filename, ".d");
1434
1435 r = config_parse_many(
1436 STRV_MAKE_CONST(path),
1437 conf_file_dirs,
1438 dropin_dirname,
1439 "Partition\0",
1440 config_item_table_lookup, table,
1441 CONFIG_PARSE_WARN,
1442 p,
1443 NULL,
1444 &p->drop_in_files);
e594a3b1
LP
1445 if (r < 0)
1446 return r;
1447
1448 if (p->size_min != UINT64_MAX && p->size_max != UINT64_MAX && p->size_min > p->size_max)
1449 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1450 "SizeMinBytes= larger than SizeMaxBytes=, refusing.");
1451
1452 if (p->padding_min != UINT64_MAX && p->padding_max != UINT64_MAX && p->padding_min > p->padding_max)
1453 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1454 "PaddingMinBytes= larger than PaddingMaxBytes=, refusing.");
1455
1456 if (sd_id128_is_null(p->type_uuid))
1457 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1458 "Type= not defined, refusing.");
1459
5c08da58
LP
1460 if ((p->copy_blocks_path || p->copy_blocks_auto) &&
1461 (p->format || !strv_isempty(p->copy_files) || !strv_isempty(p->make_directories)))
53171c04 1462 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
5c08da58 1463 "Format=/CopyFiles=/MakeDirectories= and CopyBlocks= cannot be combined, refusing.");
53171c04 1464
d83d8048 1465 if ((!strv_isempty(p->copy_files) || !strv_isempty(p->make_directories)) && streq_ptr(p->format, "swap"))
8a794850
LP
1466 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1467 "Format=swap and CopyFiles= cannot be combined, refusing.");
1468
5c08da58 1469 if (!p->format && (!strv_isempty(p->copy_files) || !strv_isempty(p->make_directories) || (p->encrypt != ENCRYPT_OFF && !(p->copy_blocks_path || p->copy_blocks_auto)))) {
b9df3536 1470 /* Pick "ext4" as file system if we are configured to copy files or encrypt the device */
8a794850
LP
1471 p->format = strdup("ext4");
1472 if (!p->format)
1473 return log_oom();
1474 }
1475
b5b7879a
DDM
1476 if (p->verity != VERITY_OFF || p->encrypt != ENCRYPT_OFF) {
1477 r = dlopen_cryptsetup();
1478 if (r < 0)
1479 return log_syntax(NULL, LOG_ERR, path, 1, r,
1480 "libcryptsetup not found, Verity=/Encrypt= are not supported: %m");
1481 }
1482
1483 if (p->verity != VERITY_OFF && !p->verity_match_key)
1484 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1485 "VerityMatchKey= must be set if Verity=%s", verity_mode_to_string(p->verity));
1486
1487 if (p->verity == VERITY_OFF && p->verity_match_key)
1488 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1489 "VerityMatchKey= can only be set if Verity= is not \"%s\"",
1490 verity_mode_to_string(p->verity));
1491
1492 if (p->verity == VERITY_HASH && (p->copy_files || p->copy_blocks_path || p->copy_blocks_auto || p->format || p->make_directories))
1493 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1494 "CopyBlocks=/CopyFiles=/Format=/MakeDirectories= cannot be used with Verity=%s",
1495 verity_mode_to_string(p->verity));
1496
1497 if (p->verity != VERITY_OFF && p->encrypt != ENCRYPT_OFF)
1498 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1499 "Encrypting verity hash/data partitions is not supported");
1500
e73309c5
LP
1501 /* Verity partitions are read only, let's imply the RO flag hence, unless explicitly configured otherwise. */
1502 if ((gpt_partition_type_is_root_verity(p->type_uuid) ||
1503 gpt_partition_type_is_usr_verity(p->type_uuid)) &&
1504 p->read_only < 0)
1505 p->read_only = true;
1506
1c41c1dc
LP
1507 /* Default to "growfs" on, unless read-only */
1508 if (gpt_partition_type_knows_growfs(p->type_uuid) &&
1509 p->read_only <= 0)
1510 p->growfs = true;
1511
e594a3b1
LP
1512 return 0;
1513}
1514
b5b7879a
DDM
1515static int find_verity_sibling(Context *context, Partition *p, VerityMode mode, Partition **ret) {
1516 Partition *s = NULL;
1517
1518 assert(p);
1519 assert(p->verity != VERITY_OFF);
1520 assert(p->verity_match_key);
1521 assert(mode != VERITY_OFF);
1522 assert(p->verity != mode);
1523 assert(ret);
1524
1525 /* Try to find the matching sibling partition of the given type for a verity partition. For a data
1526 * partition, this is the corresponding hash partiton with the same verity name (and vice versa for
1527 * the hash partition).
1528 */
1529
1530 LIST_FOREACH(partitions, q, context->partitions) {
1531 if (p == q)
1532 continue;
1533
1534 if (q->verity != mode)
1535 continue;
1536
1537 assert(q->verity_match_key);
1538
1539 if (!streq(p->verity_match_key, q->verity_match_key))
1540 continue;
1541
1542 if (s)
1543 return -ENOTUNIQ;
1544
1545 s = q;
1546 }
1547
1548 if (!s)
1549 return -ENXIO;
1550
1551 *ret = s;
1552
1553 return 0;
1554}
1555
e594a3b1
LP
1556static int context_read_definitions(
1557 Context *context,
224c853f 1558 char **directories,
e594a3b1
LP
1559 const char *root) {
1560
1561 _cleanup_strv_free_ char **files = NULL;
1562 Partition *last = NULL;
e594a3b1 1563 int r;
39fc0174 1564 const char *const *dirs;
e594a3b1
LP
1565
1566 assert(context);
1567
224c853f 1568 dirs = (const char* const*) (directories ?: CONF_PATHS_STRV("repart.d"));
39fc0174 1569
224c853f 1570 r = conf_files_list_strv(&files, ".conf", directories ? NULL : root, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, dirs);
e594a3b1
LP
1571 if (r < 0)
1572 return log_error_errno(r, "Failed to enumerate *.conf files: %m");
1573
1574 STRV_FOREACH(f, files) {
1575 _cleanup_(partition_freep) Partition *p = NULL;
1576
1577 p = partition_new();
1578 if (!p)
1579 return log_oom();
1580
1581 p->definition_path = strdup(*f);
1582 if (!p->definition_path)
1583 return log_oom();
1584
39fc0174 1585 r = partition_read_definition(p, *f, dirs);
e594a3b1
LP
1586 if (r < 0)
1587 return r;
1588
1589 LIST_INSERT_AFTER(partitions, context->partitions, last, p);
1590 last = TAKE_PTR(p);
1591 context->n_partitions++;
1592 }
1593
b5b7879a
DDM
1594 /* Check that each configured verity hash/data partition has a matching verity data/hash partition. */
1595
1596 LIST_FOREACH(partitions, p, context->partitions) {
1597 if (p->verity == VERITY_OFF)
1598 continue;
1599
1600 for (VerityMode mode = VERITY_OFF + 1; mode < _VERITY_MODE_MAX; mode++) {
1601 Partition *q;
1602
1603 if (p->verity == mode)
1604 continue;
1605
1606 if (p->siblings[mode])
1607 continue;
1608
1609 r = find_verity_sibling(context, p, mode, &q);
1610 if (r == -ENXIO)
1611 return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL),
1612 "Missing verity %s partition for verity %s partition with VerityMatchKey=%s",
1613 verity_mode_to_string(mode), verity_mode_to_string(p->verity), p->verity_match_key);
1614 if (r == -ENOTUNIQ)
1615 return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL),
1616 "Multiple verity %s partitions found for verity %s partition with VerityMatchKey=%s",
1617 verity_mode_to_string(mode), verity_mode_to_string(p->verity), p->verity_match_key);
1618 if (r < 0)
1619 return r;
1620
1621 if (q->priority != p->priority)
1622 return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL),
1623 "Priority mismatch (%i != %i) for verity sibling partitions with VerityMatchKey=%s",
1624 p->priority, q->priority, p->verity_match_key);
1625
1626 p->siblings[mode] = q;
1627 }
1628 }
1629
e594a3b1
LP
1630 return 0;
1631}
1632
e594a3b1
LP
1633static int determine_current_padding(
1634 struct fdisk_context *c,
1635 struct fdisk_table *t,
1636 struct fdisk_partition *p,
994b3031
LP
1637 uint64_t secsz,
1638 uint64_t grainsz,
e594a3b1
LP
1639 uint64_t *ret) {
1640
1641 size_t n_partitions;
1642 uint64_t offset, next = UINT64_MAX;
1643
1644 assert(c);
1645 assert(t);
1646 assert(p);
1647
1648 if (!fdisk_partition_has_end(p))
1649 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition has no end!");
1650
1651 offset = fdisk_partition_get_end(p);
994b3031
LP
1652 assert(offset < UINT64_MAX / secsz);
1653 offset *= secsz;
e594a3b1
LP
1654
1655 n_partitions = fdisk_table_get_nents(t);
695cfd53 1656 for (size_t i = 0; i < n_partitions; i++) {
e594a3b1
LP
1657 struct fdisk_partition *q;
1658 uint64_t start;
1659
1660 q = fdisk_table_get_partition(t, i);
1661 if (!q)
1662 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
1663
1664 if (fdisk_partition_is_used(q) <= 0)
1665 continue;
1666
1667 if (!fdisk_partition_has_start(q))
1668 continue;
1669
1670 start = fdisk_partition_get_start(q);
994b3031
LP
1671 assert(start < UINT64_MAX / secsz);
1672 start *= secsz;
e594a3b1
LP
1673
1674 if (start >= offset && (next == UINT64_MAX || next > start))
1675 next = start;
1676 }
1677
1678 if (next == UINT64_MAX) {
1679 /* No later partition? In that case check the end of the usable area */
1680 next = fdisk_get_last_lba(c);
1681 assert(next < UINT64_MAX);
1682 next++; /* The last LBA is one sector before the end */
1683
994b3031
LP
1684 assert(next < UINT64_MAX / secsz);
1685 next *= secsz;
e594a3b1
LP
1686
1687 if (offset > next)
1688 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end.");
1689 }
1690
1691 assert(next >= offset);
994b3031
LP
1692 offset = round_up_size(offset, grainsz);
1693 next = round_down_size(next, grainsz);
e594a3b1 1694
a6f44d61 1695 *ret = LESS_BY(next, offset); /* Saturated subtraction, rounding might have fucked things up */
e594a3b1
LP
1696 return 0;
1697}
1698
1699static int fdisk_ask_cb(struct fdisk_context *c, struct fdisk_ask *ask, void *data) {
1700 _cleanup_free_ char *ids = NULL;
1701 int r;
1702
1703 if (fdisk_ask_get_type(ask) != FDISK_ASKTYPE_STRING)
1704 return -EINVAL;
1705
b7416360 1706 ids = new(char, SD_ID128_UUID_STRING_MAX);
e594a3b1
LP
1707 if (!ids)
1708 return -ENOMEM;
1709
b7416360 1710 r = fdisk_ask_string_set_result(ask, sd_id128_to_uuid_string(*(sd_id128_t*) data, ids));
e594a3b1
LP
1711 if (r < 0)
1712 return r;
1713
1714 TAKE_PTR(ids);
1715 return 0;
1716}
1717
1718static int fdisk_set_disklabel_id_by_uuid(struct fdisk_context *c, sd_id128_t id) {
1719 int r;
1720
1721 r = fdisk_set_ask(c, fdisk_ask_cb, &id);
1722 if (r < 0)
1723 return r;
1724
1725 r = fdisk_set_disklabel_id(c);
1726 if (r < 0)
1727 return r;
1728
1729 return fdisk_set_ask(c, NULL, NULL);
1730}
1731
53171c04 1732static int derive_uuid(sd_id128_t base, const char *token, sd_id128_t *ret) {
e594a3b1 1733 union {
ade99252 1734 uint8_t md[SHA256_DIGEST_SIZE];
e594a3b1
LP
1735 sd_id128_t id;
1736 } result;
1737
53171c04 1738 assert(token);
e594a3b1
LP
1739 assert(ret);
1740
53171c04
LP
1741 /* Derive a new UUID from the specified UUID in a stable and reasonably safe way. Specifically, we
1742 * calculate the HMAC-SHA256 of the specified token string, keyed by the supplied base (typically the
1743 * machine ID). We use the machine ID as key (and not as cleartext!) of the HMAC operation since it's
1744 * the machine ID we don't want to leak. */
e594a3b1 1745
ade99252 1746 hmac_sha256(base.bytes, sizeof(base.bytes), token, strlen(token), result.md);
e594a3b1
LP
1747
1748 /* Take the first half, mark it as v4 UUID */
1749 assert_cc(sizeof(result.md) == sizeof(result.id) * 2);
1750 *ret = id128_make_v4_uuid(result.id);
1751 return 0;
1752}
1753
a26f4a49
LP
1754static int context_load_partition_table(
1755 Context *context,
1756 const char *node,
1757 int *backing_fd) {
1758
e594a3b1
LP
1759 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
1760 _cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL;
1761 uint64_t left_boundary = UINT64_MAX, first_lba, last_lba, nsectors;
1762 _cleanup_free_ char *disk_uuid_string = NULL;
1763 bool from_scratch = false;
1764 sd_id128_t disk_uuid;
1765 size_t n_partitions;
994b3031
LP
1766 unsigned long secsz;
1767 uint64_t grainsz;
e594a3b1
LP
1768 int r;
1769
1770 assert(context);
1771 assert(node);
a26f4a49 1772 assert(backing_fd);
170c9823
LP
1773 assert(!context->fdisk_context);
1774 assert(!context->free_areas);
1775 assert(context->start == UINT64_MAX);
1776 assert(context->end == UINT64_MAX);
1777 assert(context->total == UINT64_MAX);
e594a3b1
LP
1778
1779 c = fdisk_new_context();
1780 if (!c)
1781 return log_oom();
1782
a26f4a49
LP
1783 /* libfdisk doesn't have an API to operate on arbitrary fds, hence reopen the fd going via the
1784 * /proc/self/fd/ magic path if we have an existing fd. Open the original file otherwise. */
1785 if (*backing_fd < 0)
1786 r = fdisk_assign_device(c, node, arg_dry_run);
ddb6eeaf
LP
1787 else
1788 r = fdisk_assign_device(c, FORMAT_PROC_FD_PATH(*backing_fd), arg_dry_run);
170c9823
LP
1789 if (r == -EINVAL && arg_size_auto) {
1790 struct stat st;
1791
1792 /* libfdisk returns EINVAL if opening a file of size zero. Let's check for that, and accept
1793 * it if automatic sizing is requested. */
1794
1795 if (*backing_fd < 0)
1796 r = stat(node, &st);
1797 else
1798 r = fstat(*backing_fd, &st);
1799 if (r < 0)
1800 return log_error_errno(errno, "Failed to stat block device '%s': %m", node);
1801
994b3031
LP
1802 if (S_ISREG(st.st_mode) && st.st_size == 0) {
1803 /* User the fallback values if we have no better idea */
1804 context->sector_size = 512;
1805 context->grain_size = 4096;
170c9823 1806 return /* from_scratch = */ true;
994b3031 1807 }
170c9823
LP
1808
1809 r = -EINVAL;
1810 }
e594a3b1 1811 if (r < 0)
a26f4a49
LP
1812 return log_error_errno(r, "Failed to open device '%s': %m", node);
1813
1814 if (*backing_fd < 0) {
1815 /* If we have no fd referencing the device yet, make a copy of the fd now, so that we have one */
38f81e93 1816 *backing_fd = fd_reopen(fdisk_get_devfd(c), O_RDONLY|O_CLOEXEC);
a26f4a49 1817 if (*backing_fd < 0)
38f81e93 1818 return log_error_errno(*backing_fd, "Failed to duplicate fdisk fd: %m");
e594a3b1 1819
25baae50
DDM
1820 /* Tell udev not to interfere while we are processing the device */
1821 if (flock(*backing_fd, arg_dry_run ? LOCK_SH : LOCK_EX) < 0)
1822 return log_error_errno(errno, "Failed to lock block device: %m");
1823 }
e594a3b1 1824
994b3031
LP
1825 /* The offsets/sizes libfdisk returns to us will be in multiple of the sector size of the
1826 * device. This is typically 512, and sometimes 4096. Let's query libfdisk once for it, and then use
1827 * it for all our needs. Note that the values we use ourselves always are in bytes though, thus mean
1828 * the same thing universally. Also note that regardless what kind of sector size is in use we'll
1829 * place partitions at multiples of 4K. */
1830 secsz = fdisk_get_sector_size(c);
1831
1832 /* Insist on a power of two, and that it's a multiple of 512, i.e. the traditional sector size. */
983ce0b5
LP
1833 if (secsz < 512 || !ISPOWEROF2(secsz))
1834 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Sector size %lu is not a power of two larger than 512? Refusing.", secsz);
994b3031
LP
1835
1836 /* Use at least 4K, and ensure it's a multiple of the sector size, regardless if that is smaller or
1837 * larger */
1838 grainsz = secsz < 4096 ? 4096 : secsz;
1839
1840 log_debug("Sector size of device is %lu bytes. Using grain size of %" PRIu64 ".", secsz, grainsz);
1841
e594a3b1
LP
1842 switch (arg_empty) {
1843
1844 case EMPTY_REFUSE:
1845 /* Refuse empty disks, insist on an existing GPT partition table */
1846 if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
1847 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not repartitioning.", node);
1848
1849 break;
1850
1851 case EMPTY_REQUIRE:
1852 /* Require an empty disk, refuse any existing partition table */
1853 r = fdisk_has_label(c);
1854 if (r < 0)
1855 return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", node);
1856 if (r > 0)
1857 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s already has a disk label, refusing.", node);
1858
1859 from_scratch = true;
1860 break;
1861
1862 case EMPTY_ALLOW:
1863 /* Allow both an empty disk and an existing partition table, but only GPT */
1864 r = fdisk_has_label(c);
1865 if (r < 0)
1866 return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", node);
1867 if (r > 0) {
1868 if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
1869 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has non-GPT disk label, not repartitioning.", node);
1870 } else
1871 from_scratch = true;
1872
1873 break;
1874
1875 case EMPTY_FORCE:
a26f4a49 1876 case EMPTY_CREATE:
e594a3b1
LP
1877 /* Always reinitiaize the disk, don't consider what there was on the disk before */
1878 from_scratch = true;
1879 break;
1880 }
1881
1882 if (from_scratch) {
e594a3b1
LP
1883 r = fdisk_create_disklabel(c, "gpt");
1884 if (r < 0)
1885 return log_error_errno(r, "Failed to create GPT disk label: %m");
1886
53171c04 1887 r = derive_uuid(context->seed, "disk-uuid", &disk_uuid);
e594a3b1
LP
1888 if (r < 0)
1889 return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
1890
1891 r = fdisk_set_disklabel_id_by_uuid(c, disk_uuid);
1892 if (r < 0)
1893 return log_error_errno(r, "Failed to set GPT disk label: %m");
1894
1895 goto add_initial_free_area;
1896 }
1897
1898 r = fdisk_get_disklabel_id(c, &disk_uuid_string);
1899 if (r < 0)
1900 return log_error_errno(r, "Failed to get current GPT disk label UUID: %m");
1901
1902 r = sd_id128_from_string(disk_uuid_string, &disk_uuid);
1903 if (r < 0)
1904 return log_error_errno(r, "Failed to parse current GPT disk label UUID: %m");
1905
1906 if (sd_id128_is_null(disk_uuid)) {
53171c04 1907 r = derive_uuid(context->seed, "disk-uuid", &disk_uuid);
e594a3b1
LP
1908 if (r < 0)
1909 return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
1910
1911 r = fdisk_set_disklabel_id(c);
1912 if (r < 0)
1913 return log_error_errno(r, "Failed to set GPT disk label: %m");
1914 }
1915
1916 r = fdisk_get_partitions(c, &t);
1917 if (r < 0)
1918 return log_error_errno(r, "Failed to acquire partition table: %m");
1919
1920 n_partitions = fdisk_table_get_nents(t);
695cfd53 1921 for (size_t i = 0; i < n_partitions; i++) {
e594a3b1 1922 _cleanup_free_ char *label_copy = NULL;
03677889 1923 Partition *last = NULL;
e594a3b1
LP
1924 struct fdisk_partition *p;
1925 struct fdisk_parttype *pt;
1926 const char *pts, *ids, *label;
1927 uint64_t sz, start;
1928 bool found = false;
1929 sd_id128_t ptid, id;
1930 size_t partno;
1931
1932 p = fdisk_table_get_partition(t, i);
1933 if (!p)
1934 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
1935
1936 if (fdisk_partition_is_used(p) <= 0)
1937 continue;
1938
1939 if (fdisk_partition_has_start(p) <= 0 ||
1940 fdisk_partition_has_size(p) <= 0 ||
1941 fdisk_partition_has_partno(p) <= 0)
1942 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a position, size or number.");
1943
1944 pt = fdisk_partition_get_type(p);
1945 if (!pt)
1946 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition: %m");
1947
1948 pts = fdisk_parttype_get_string(pt);
1949 if (!pts)
1950 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition as string: %m");
1951
1952 r = sd_id128_from_string(pts, &ptid);
1953 if (r < 0)
1954 return log_error_errno(r, "Failed to parse partition type UUID %s: %m", pts);
1955
1956 ids = fdisk_partition_get_uuid(p);
1957 if (!ids)
1958 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a UUID.");
1959
1960 r = sd_id128_from_string(ids, &id);
1961 if (r < 0)
1962 return log_error_errno(r, "Failed to parse partition UUID %s: %m", ids);
1963
1964 label = fdisk_partition_get_name(p);
1965 if (!isempty(label)) {
1966 label_copy = strdup(label);
1967 if (!label_copy)
1968 return log_oom();
1969 }
1970
1971 sz = fdisk_partition_get_size(p);
ac33e147 1972 assert(sz <= UINT64_MAX/secsz);
994b3031 1973 sz *= secsz;
e594a3b1
LP
1974
1975 start = fdisk_partition_get_start(p);
ac33e147 1976 assert(start <= UINT64_MAX/secsz);
994b3031 1977 start *= secsz;
e594a3b1
LP
1978
1979 partno = fdisk_partition_get_partno(p);
1980
1981 if (left_boundary == UINT64_MAX || left_boundary > start)
1982 left_boundary = start;
1983
1984 /* Assign this existing partition to the first partition of the right type that doesn't have
1985 * an existing one assigned yet. */
1986 LIST_FOREACH(partitions, pp, context->partitions) {
1987 last = pp;
1988
1989 if (!sd_id128_equal(pp->type_uuid, ptid))
1990 continue;
1991
1992 if (!pp->current_partition) {
1993 pp->current_uuid = id;
1994 pp->current_size = sz;
1995 pp->offset = start;
1996 pp->partno = partno;
1997 pp->current_label = TAKE_PTR(label_copy);
1998
1999 pp->current_partition = p;
2000 fdisk_ref_partition(p);
2001
994b3031 2002 r = determine_current_padding(c, t, p, secsz, grainsz, &pp->current_padding);
e594a3b1
LP
2003 if (r < 0)
2004 return r;
2005
2006 if (pp->current_padding > 0) {
2007 r = context_add_free_area(context, pp->current_padding, pp);
2008 if (r < 0)
2009 return r;
2010 }
2011
2012 found = true;
2013 break;
2014 }
2015 }
2016
2017 /* If we have no matching definition, create a new one. */
2018 if (!found) {
2019 _cleanup_(partition_freep) Partition *np = NULL;
2020
2021 np = partition_new();
2022 if (!np)
2023 return log_oom();
2024
2025 np->current_uuid = id;
2026 np->type_uuid = ptid;
2027 np->current_size = sz;
2028 np->offset = start;
2029 np->partno = partno;
2030 np->current_label = TAKE_PTR(label_copy);
2031
2032 np->current_partition = p;
2033 fdisk_ref_partition(p);
2034
994b3031 2035 r = determine_current_padding(c, t, p, secsz, grainsz, &np->current_padding);
e594a3b1
LP
2036 if (r < 0)
2037 return r;
2038
2039 if (np->current_padding > 0) {
2040 r = context_add_free_area(context, np->current_padding, np);
2041 if (r < 0)
2042 return r;
2043 }
2044
2045 LIST_INSERT_AFTER(partitions, context->partitions, last, TAKE_PTR(np));
2046 context->n_partitions++;
2047 }
2048 }
2049
2050add_initial_free_area:
2051 nsectors = fdisk_get_nsectors(c);
994b3031
LP
2052 assert(nsectors <= UINT64_MAX/secsz);
2053 nsectors *= secsz;
e594a3b1
LP
2054
2055 first_lba = fdisk_get_first_lba(c);
994b3031
LP
2056 assert(first_lba <= UINT64_MAX/secsz);
2057 first_lba *= secsz;
e594a3b1
LP
2058
2059 last_lba = fdisk_get_last_lba(c);
2060 assert(last_lba < UINT64_MAX);
2061 last_lba++;
994b3031
LP
2062 assert(last_lba <= UINT64_MAX/secsz);
2063 last_lba *= secsz;
e594a3b1
LP
2064
2065 assert(last_lba >= first_lba);
2066
2067 if (left_boundary == UINT64_MAX) {
2068 /* No partitions at all? Then the whole disk is up for grabs. */
2069
994b3031
LP
2070 first_lba = round_up_size(first_lba, grainsz);
2071 last_lba = round_down_size(last_lba, grainsz);
e594a3b1
LP
2072
2073 if (last_lba > first_lba) {
2074 r = context_add_free_area(context, last_lba - first_lba, NULL);
2075 if (r < 0)
2076 return r;
2077 }
2078 } else {
2079 /* Add space left of first partition */
2080 assert(left_boundary >= first_lba);
2081
994b3031
LP
2082 first_lba = round_up_size(first_lba, grainsz);
2083 left_boundary = round_down_size(left_boundary, grainsz);
2084 last_lba = round_down_size(last_lba, grainsz);
e594a3b1
LP
2085
2086 if (left_boundary > first_lba) {
2087 r = context_add_free_area(context, left_boundary - first_lba, NULL);
2088 if (r < 0)
2089 return r;
2090 }
2091 }
2092
2093 context->start = first_lba;
2094 context->end = last_lba;
2095 context->total = nsectors;
994b3031
LP
2096 context->sector_size = secsz;
2097 context->grain_size = grainsz;
e594a3b1
LP
2098 context->fdisk_context = TAKE_PTR(c);
2099
2100 return from_scratch;
2101}
2102
2103static void context_unload_partition_table(Context *context) {
e594a3b1
LP
2104 assert(context);
2105
80a226b2 2106 LIST_FOREACH(partitions, p, context->partitions) {
e594a3b1
LP
2107
2108 /* Entirely remove partitions that have no configuration */
2109 if (PARTITION_IS_FOREIGN(p)) {
2110 partition_unlink_and_free(context, p);
2111 continue;
2112 }
2113
2114 /* Otherwise drop all data we read off the block device and everything we might have
2115 * calculated based on it */
2116
2117 p->dropped = false;
2118 p->current_size = UINT64_MAX;
2119 p->new_size = UINT64_MAX;
2120 p->current_padding = UINT64_MAX;
2121 p->new_padding = UINT64_MAX;
2122 p->partno = UINT64_MAX;
2123 p->offset = UINT64_MAX;
2124
2125 if (p->current_partition) {
2126 fdisk_unref_partition(p->current_partition);
2127 p->current_partition = NULL;
2128 }
2129
2130 if (p->new_partition) {
2131 fdisk_unref_partition(p->new_partition);
2132 p->new_partition = NULL;
2133 }
2134
2135 p->padding_area = NULL;
2136 p->allocated_to_area = NULL;
2137
15d43e30
LP
2138 p->current_uuid = SD_ID128_NULL;
2139 p->current_label = mfree(p->current_label);
e594a3b1
LP
2140 }
2141
2142 context->start = UINT64_MAX;
2143 context->end = UINT64_MAX;
2144 context->total = UINT64_MAX;
2145
2146 if (context->fdisk_context) {
2147 fdisk_unref_context(context->fdisk_context);
2148 context->fdisk_context = NULL;
2149 }
2150
2151 context_free_free_areas(context);
2152}
2153
2154static int format_size_change(uint64_t from, uint64_t to, char **ret) {
2b59bf51 2155 char *t;
e594a3b1
LP
2156
2157 if (from != UINT64_MAX) {
2158 if (from == to || to == UINT64_MAX)
2b59bf51 2159 t = strdup(FORMAT_BYTES(from));
e594a3b1 2160 else
fc03e80c 2161 t = strjoin(FORMAT_BYTES(from), " ", special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), " ", FORMAT_BYTES(to));
e594a3b1 2162 } else if (to != UINT64_MAX)
fc03e80c 2163 t = strjoin(special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), " ", FORMAT_BYTES(to));
e594a3b1
LP
2164 else {
2165 *ret = NULL;
2166 return 0;
2167 }
2168
2b59bf51 2169 if (!t)
e594a3b1
LP
2170 return log_oom();
2171
2b59bf51 2172 *ret = t;
e594a3b1
LP
2173 return 1;
2174}
2175
2176static const char *partition_label(const Partition *p) {
2177 assert(p);
2178
2179 if (p->new_label)
2180 return p->new_label;
2181
2182 if (p->current_label)
2183 return p->current_label;
2184
2185 return gpt_partition_type_uuid_to_string(p->type_uuid);
2186}
2187
2188static int context_dump_partitions(Context *context, const char *node) {
2189 _cleanup_(table_unrefp) Table *t = NULL;
2190 uint64_t sum_padding = 0, sum_size = 0;
e594a3b1 2191 int r;
b5b7879a
DDM
2192 const size_t roothash_col = 13, dropin_files_col = 14;
2193 bool has_roothash = false, has_dropin_files = false;
e594a3b1 2194
6a01ea4a 2195 if ((arg_json_format_flags & JSON_FORMAT_OFF) && context->n_partitions == 0) {
a015fbe7
TH
2196 log_info("Empty partition table.");
2197 return 0;
2198 }
2199
b5b7879a 2200 t = table_new("type", "label", "uuid", "file", "node", "offset", "old size", "raw size", "size", "old padding", "raw padding", "padding", "activity", "roothash", "drop-in files");
e594a3b1
LP
2201 if (!t)
2202 return log_oom();
2203
a015fbe7 2204 if (!DEBUG_LOGGING) {
6a01ea4a 2205 if (arg_json_format_flags & JSON_FORMAT_OFF)
a015fbe7 2206 (void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
b5b7879a 2207 (size_t) 8, (size_t) 11, roothash_col, dropin_files_col);
a015fbe7
TH
2208 else
2209 (void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
b5b7879a
DDM
2210 (size_t) 5, (size_t) 6, (size_t) 7, (size_t) 9, (size_t) 10,
2211 (size_t) 12, roothash_col, dropin_files_col);
a015fbe7 2212 }
e594a3b1 2213
e594a3b1 2214 (void) table_set_align_percent(t, table_get_cell(t, 0, 5), 100);
9c07c9ec
LP
2215 (void) table_set_align_percent(t, table_get_cell(t, 0, 6), 100);
2216 (void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
2217 (void) table_set_align_percent(t, table_get_cell(t, 0, 8), 100);
2218 (void) table_set_align_percent(t, table_get_cell(t, 0, 9), 100);
2219 (void) table_set_align_percent(t, table_get_cell(t, 0, 10), 100);
2220 (void) table_set_align_percent(t, table_get_cell(t, 0, 11), 100);
e594a3b1
LP
2221
2222 LIST_FOREACH(partitions, p, context->partitions) {
b5b7879a 2223 _cleanup_free_ char *size_change = NULL, *padding_change = NULL, *partname = NULL, *rh = NULL;
b7416360 2224 char uuid_buffer[SD_ID128_UUID_STRING_MAX];
a015fbe7 2225 const char *label, *activity = NULL;
e594a3b1
LP
2226
2227 if (p->dropped)
2228 continue;
2229
a015fbe7
TH
2230 if (p->current_size == UINT64_MAX)
2231 activity = "create";
2232 else if (p->current_size != p->new_size)
2233 activity = "resize";
2234
e594a3b1
LP
2235 label = partition_label(p);
2236 partname = p->partno != UINT64_MAX ? fdisk_partname(node, p->partno+1) : NULL;
2237
2238 r = format_size_change(p->current_size, p->new_size, &size_change);
2239 if (r < 0)
2240 return r;
2241
2242 r = format_size_change(p->current_padding, p->new_padding, &padding_change);
2243 if (r < 0)
2244 return r;
2245
2246 if (p->new_size != UINT64_MAX)
2247 sum_size += p->new_size;
2248 if (p->new_padding != UINT64_MAX)
2249 sum_padding += p->new_padding;
2250
b5b7879a
DDM
2251 if (p->verity == VERITY_HASH) {
2252 rh = p->roothash ? hexmem(p->roothash, p->roothash_size) : strdup("TBD");
2253 if (!rh)
2254 return log_oom();
2255 }
2256
e594a3b1
LP
2257 r = table_add_many(
2258 t,
2259 TABLE_STRING, gpt_partition_type_uuid_to_string_harder(p->type_uuid, uuid_buffer),
be9ce018 2260 TABLE_STRING, empty_to_null(label) ?: "-", TABLE_SET_COLOR, empty_to_null(label) ? NULL : ansi_grey(),
11749b61 2261 TABLE_UUID, p->new_uuid_is_set ? p->new_uuid : p->current_uuid,
e594a3b1 2262 TABLE_STRING, p->definition_path ? basename(p->definition_path) : "-", TABLE_SET_COLOR, p->definition_path ? NULL : ansi_grey(),
a015fbe7 2263 TABLE_STRING, partname ?: "-", TABLE_SET_COLOR, partname ? NULL : ansi_highlight(),
e594a3b1 2264 TABLE_UINT64, p->offset,
a015fbe7 2265 TABLE_UINT64, p->current_size == UINT64_MAX ? 0 : p->current_size,
e594a3b1
LP
2266 TABLE_UINT64, p->new_size,
2267 TABLE_STRING, size_change, TABLE_SET_COLOR, !p->partitions_next && sum_size > 0 ? ansi_underline() : NULL,
a015fbe7 2268 TABLE_UINT64, p->current_padding == UINT64_MAX ? 0 : p->current_padding,
e594a3b1 2269 TABLE_UINT64, p->new_padding,
a015fbe7 2270 TABLE_STRING, padding_change, TABLE_SET_COLOR, !p->partitions_next && sum_padding > 0 ? ansi_underline() : NULL,
39fc0174 2271 TABLE_STRING, activity ?: "unchanged",
b5b7879a 2272 TABLE_STRING, rh,
39fc0174 2273 TABLE_STRV, p->drop_in_files);
e594a3b1 2274 if (r < 0)
f987a261 2275 return table_log_add_error(r);
39fc0174 2276
b5b7879a 2277 has_roothash = has_roothash || !isempty(rh);
3ab44dbd 2278 has_dropin_files = has_dropin_files || !strv_isempty(p->drop_in_files);
e594a3b1
LP
2279 }
2280
6a01ea4a 2281 if ((arg_json_format_flags & JSON_FORMAT_OFF) && (sum_padding > 0 || sum_size > 0)) {
e594a3b1
LP
2282 const char *a, *b;
2283
2b59bf51
ZJS
2284 a = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_size));
2285 b = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_padding));
e594a3b1
LP
2286
2287 r = table_add_many(
2288 t,
2289 TABLE_EMPTY,
2290 TABLE_EMPTY,
2291 TABLE_EMPTY,
2292 TABLE_EMPTY,
2293 TABLE_EMPTY,
2294 TABLE_EMPTY,
2295 TABLE_EMPTY,
a015fbe7 2296 TABLE_EMPTY,
e594a3b1
LP
2297 TABLE_STRING, a,
2298 TABLE_EMPTY,
a015fbe7
TH
2299 TABLE_EMPTY,
2300 TABLE_STRING, b,
39fc0174 2301 TABLE_EMPTY,
b5b7879a 2302 TABLE_EMPTY,
a015fbe7 2303 TABLE_EMPTY);
e594a3b1 2304 if (r < 0)
f987a261 2305 return table_log_add_error(r);
e594a3b1
LP
2306 }
2307
b5b7879a
DDM
2308 if (!has_roothash) {
2309 r = table_hide_column_from_display(t, roothash_col);
2310 if (r < 0)
2311 return log_error_errno(r, "Failed to set columns to display: %m");
2312 }
2313
3ab44dbd 2314 if (!has_dropin_files) {
39fc0174
RP
2315 r = table_hide_column_from_display(t, dropin_files_col);
2316 if (r < 0)
2317 return log_error_errno(r, "Failed to set columns to display: %m");
2318 }
2319
896e678b 2320 return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
e594a3b1
LP
2321}
2322
2323static void context_bar_char_process_partition(
2324 Context *context,
2325 Partition *bar[],
2326 size_t n,
2327 Partition *p,
2328 size_t *ret_start) {
2329
2330 uint64_t from, to, total;
2331 size_t x, y;
2332
2333 assert(context);
2334 assert(bar);
2335 assert(n > 0);
2336 assert(p);
2337
2338 if (p->dropped)
2339 return;
2340
2341 assert(p->offset != UINT64_MAX);
2342 assert(p->new_size != UINT64_MAX);
2343
2344 from = p->offset;
2345 to = from + p->new_size;
2346
d8daed09
TY
2347 assert(context->total > 0);
2348 total = context->total;
e594a3b1 2349
d8daed09
TY
2350 assert(from <= total);
2351 x = from * n / total;
e594a3b1 2352
d8daed09
TY
2353 assert(to <= total);
2354 y = to * n / total;
e594a3b1
LP
2355
2356 assert(x <= y);
2357 assert(y <= n);
2358
2359 for (size_t i = x; i < y; i++)
2360 bar[i] = p;
2361
2362 *ret_start = x;
2363}
2364
2365static int partition_hint(const Partition *p, const char *node, char **ret) {
2366 _cleanup_free_ char *buf = NULL;
e594a3b1
LP
2367 const char *label;
2368 sd_id128_t id;
2369
2370 /* Tries really hard to find a suitable description for this partition */
2371
2372 if (p->definition_path) {
2373 buf = strdup(basename(p->definition_path));
2374 goto done;
2375 }
2376
2377 label = partition_label(p);
2378 if (!isempty(label)) {
2379 buf = strdup(label);
2380 goto done;
2381 }
2382
2383 if (p->partno != UINT64_MAX) {
2384 buf = fdisk_partname(node, p->partno+1);
2385 goto done;
2386 }
2387
11749b61 2388 if (p->new_uuid_is_set)
e594a3b1
LP
2389 id = p->new_uuid;
2390 else if (!sd_id128_is_null(p->current_uuid))
2391 id = p->current_uuid;
2392 else
2393 id = p->type_uuid;
2394
b7416360 2395 buf = strdup(SD_ID128_TO_UUID_STRING(id));
e594a3b1
LP
2396
2397done:
2398 if (!buf)
2399 return -ENOMEM;
2400
2401 *ret = TAKE_PTR(buf);
2402 return 0;
2403}
2404
2405static int context_dump_partition_bar(Context *context, const char *node) {
2406 _cleanup_free_ Partition **bar = NULL;
2407 _cleanup_free_ size_t *start_array = NULL;
03677889 2408 Partition *last = NULL;
e594a3b1
LP
2409 bool z = false;
2410 size_t c, j = 0;
2411
f391597c 2412 assert_se((c = columns()) >= 2);
e594a3b1
LP
2413 c -= 2; /* We do not use the leftmost and rightmost character cell */
2414
2415 bar = new0(Partition*, c);
2416 if (!bar)
2417 return log_oom();
2418
2419 start_array = new(size_t, context->n_partitions);
2420 if (!start_array)
2421 return log_oom();
2422
2423 LIST_FOREACH(partitions, p, context->partitions)
2424 context_bar_char_process_partition(context, bar, c, p, start_array + j++);
2425
2426 putc(' ', stdout);
2427
2428 for (size_t i = 0; i < c; i++) {
2429 if (bar[i]) {
2430 if (last != bar[i])
2431 z = !z;
2432
2433 fputs(z ? ansi_green() : ansi_yellow(), stdout);
2434 fputs(special_glyph(SPECIAL_GLYPH_DARK_SHADE), stdout);
2435 } else {
2436 fputs(ansi_normal(), stdout);
2437 fputs(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE), stdout);
2438 }
2439
2440 last = bar[i];
2441 }
2442
2443 fputs(ansi_normal(), stdout);
2444 putc('\n', stdout);
2445
2446 for (size_t i = 0; i < context->n_partitions; i++) {
2447 _cleanup_free_ char **line = NULL;
2448
2449 line = new0(char*, c);
2450 if (!line)
2451 return log_oom();
2452
2453 j = 0;
2454 LIST_FOREACH(partitions, p, context->partitions) {
2455 _cleanup_free_ char *d = NULL;
2456 j++;
2457
2458 if (i < context->n_partitions - j) {
2459
2460 if (line[start_array[j-1]]) {
2461 const char *e;
2462
2463 /* Upgrade final corner to the right with a branch to the right */
2464 e = startswith(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_RIGHT));
2465 if (e) {
2466 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), e);
2467 if (!d)
2468 return log_oom();
2469 }
2470 }
2471
2472 if (!d) {
2473 d = strdup(special_glyph(SPECIAL_GLYPH_TREE_VERTICAL));
2474 if (!d)
2475 return log_oom();
2476 }
2477
2478 } else if (i == context->n_partitions - j) {
2479 _cleanup_free_ char *hint = NULL;
2480
2481 (void) partition_hint(p, node, &hint);
2482
2483 if (streq_ptr(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_VERTICAL)))
2484 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), " ", strna(hint));
2485 else
2486 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_RIGHT), " ", strna(hint));
2487
2488 if (!d)
2489 return log_oom();
2490 }
2491
2492 if (d)
2493 free_and_replace(line[start_array[j-1]], d);
2494 }
2495
2496 putc(' ', stdout);
2497
2498 j = 0;
2499 while (j < c) {
2500 if (line[j]) {
2501 fputs(line[j], stdout);
2502 j += utf8_console_width(line[j]);
2503 } else {
2504 putc(' ', stdout);
2505 j++;
2506 }
2507 }
2508
2509 putc('\n', stdout);
2510
2511 for (j = 0; j < c; j++)
2512 free(line[j]);
2513 }
2514
2515 return 0;
2516}
2517
b5b7879a
DDM
2518static bool context_has_roothash(Context *context) {
2519 LIST_FOREACH(partitions, p, context->partitions)
2520 if (p->roothash)
2521 return true;
2522
2523 return false;
2524}
2525
2526static int context_dump(Context *context, const char *node, bool late) {
a26d463d
DDM
2527 int r;
2528
2529 assert(context);
2530 assert(node);
2531
2532 if (arg_pretty == 0 && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
2533 return 0;
2534
b5b7879a
DDM
2535 /* If we're outputting JSON, only dump after doing all operations so we can include the roothashes
2536 * in the output. */
2537 if (!late && !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
2538 return 0;
2539
2540 /* If we're not outputting JSON, only dump again after doing all operations if there are any
2541 * roothashes that we need to communicate to the user. */
2542 if (late && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && !context_has_roothash(context))
2543 return 0;
2544
a26d463d
DDM
2545 r = context_dump_partitions(context, node);
2546 if (r < 0)
2547 return r;
2548
b5b7879a
DDM
2549 /* Make sure we only write the partition bar once, even if we're writing the partition table twice to
2550 * communicate roothashes. */
2551 if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && !late) {
a26d463d
DDM
2552 putc('\n', stdout);
2553
2554 r = context_dump_partition_bar(context, node);
2555 if (r < 0)
2556 return r;
2557
2558 putc('\n', stdout);
2559 }
2560
2561 fflush(stdout);
2562
2563 return 0;
2564}
2565
2566
e594a3b1 2567static bool context_changed(const Context *context) {
03677889 2568 assert(context);
e594a3b1
LP
2569
2570 LIST_FOREACH(partitions, p, context->partitions) {
2571 if (p->dropped)
2572 continue;
2573
2574 if (p->allocated_to_area)
2575 return true;
2576
2577 if (p->new_size != p->current_size)
2578 return true;
2579 }
2580
2581 return false;
2582}
2583
81873a6b 2584static int context_wipe_range(Context *context, uint64_t offset, uint64_t size) {
e594a3b1
LP
2585 _cleanup_(blkid_free_probep) blkid_probe probe = NULL;
2586 int r;
2587
2588 assert(context);
81873a6b
LP
2589 assert(offset != UINT64_MAX);
2590 assert(size != UINT64_MAX);
e594a3b1
LP
2591
2592 probe = blkid_new_probe();
2593 if (!probe)
2594 return log_oom();
2595
e594a3b1 2596 errno = 0;
81873a6b 2597 r = blkid_probe_set_device(probe, fdisk_get_devfd(context->fdisk_context), offset, size);
e594a3b1 2598 if (r < 0)
81873a6b 2599 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to allocate device probe for wiping.");
e594a3b1
LP
2600
2601 errno = 0;
2602 if (blkid_probe_enable_superblocks(probe, true) < 0 ||
2603 blkid_probe_set_superblocks_flags(probe, BLKID_SUBLKS_MAGIC|BLKID_SUBLKS_BADCSUM) < 0 ||
2604 blkid_probe_enable_partitions(probe, true) < 0 ||
2605 blkid_probe_set_partitions_flags(probe, BLKID_PARTS_MAGIC) < 0)
81873a6b 2606 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to enable superblock and partition probing.");
e594a3b1
LP
2607
2608 for (;;) {
2609 errno = 0;
2610 r = blkid_do_probe(probe);
2611 if (r < 0)
2612 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe for file systems.");
2613 if (r > 0)
2614 break;
2615
2616 errno = 0;
2617 if (blkid_do_wipe(probe, false) < 0)
2618 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to wipe file system signature.");
2619 }
2620
e594a3b1
LP
2621 return 0;
2622}
2623
81873a6b
LP
2624static int context_wipe_partition(Context *context, Partition *p) {
2625 int r;
2626
2627 assert(context);
2628 assert(p);
2629 assert(!PARTITION_EXISTS(p)); /* Safety check: never wipe existing partitions */
2630
2631 assert(p->offset != UINT64_MAX);
2632 assert(p->new_size != UINT64_MAX);
2633
2634 r = context_wipe_range(context, p->offset, p->new_size);
2635 if (r < 0)
2636 return r;
2637
2638 log_info("Successfully wiped file system signatures from future partition %" PRIu64 ".", p->partno);
2639 return 0;
2640}
2641
2642static int context_discard_range(
2643 Context *context,
2644 uint64_t offset,
2645 uint64_t size) {
2646
e594a3b1
LP
2647 struct stat st;
2648 int fd;
2649
2650 assert(context);
2651 assert(offset != UINT64_MAX);
2652 assert(size != UINT64_MAX);
2653
2654 if (size <= 0)
2655 return 0;
2656
a26f4a49 2657 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
e594a3b1
LP
2658
2659 if (fstat(fd, &st) < 0)
2660 return -errno;
2661
2662 if (S_ISREG(st.st_mode)) {
2663 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, offset, size) < 0) {
2664 if (ERRNO_IS_NOT_SUPPORTED(errno))
2665 return -EOPNOTSUPP;
2666
2667 return -errno;
2668 }
2669
2670 return 1;
2671 }
2672
2673 if (S_ISBLK(st.st_mode)) {
2674 uint64_t range[2], end;
2675
994b3031 2676 range[0] = round_up_size(offset, context->sector_size);
e594a3b1 2677
55d38014
LP
2678 if (offset > UINT64_MAX - size)
2679 return -ERANGE;
2680
e594a3b1
LP
2681 end = offset + size;
2682 if (end <= range[0])
2683 return 0;
2684
994b3031 2685 range[1] = round_down_size(end - range[0], context->sector_size);
e594a3b1
LP
2686 if (range[1] <= 0)
2687 return 0;
2688
2689 if (ioctl(fd, BLKDISCARD, range) < 0) {
2690 if (ERRNO_IS_NOT_SUPPORTED(errno))
2691 return -EOPNOTSUPP;
2692
2693 return -errno;
2694 }
2695
2696 return 1;
2697 }
2698
2699 return -EOPNOTSUPP;
2700}
2701
2702static int context_discard_partition(Context *context, Partition *p) {
2703 int r;
2704
2705 assert(context);
2706 assert(p);
2707
2708 assert(p->offset != UINT64_MAX);
2709 assert(p->new_size != UINT64_MAX);
2710 assert(!PARTITION_EXISTS(p)); /* Safety check: never discard existing partitions */
2711
2712 if (!arg_discard)
2713 return 0;
2714
2715 r = context_discard_range(context, p->offset, p->new_size);
2716 if (r == -EOPNOTSUPP) {
5b5109e2 2717 log_info("Storage does not support discard, not discarding data in future partition %" PRIu64 ".", p->partno);
e594a3b1
LP
2718 return 0;
2719 }
22163eb5
LP
2720 if (r == -EBUSY) {
2721 /* Let's handle this gracefully: https://bugzilla.kernel.org/show_bug.cgi?id=211167 */
2722 log_info("Block device is busy, not discarding partition %" PRIu64 " because it probably is mounted.", p->partno);
2723 return 0;
2724 }
e594a3b1
LP
2725 if (r == 0) {
2726 log_info("Partition %" PRIu64 " too short for discard, skipping.", p->partno);
2727 return 0;
2728 }
2729 if (r < 0)
5b5109e2 2730 return log_error_errno(r, "Failed to discard data for future partition %" PRIu64 ".", p->partno);
e594a3b1 2731
5b5109e2 2732 log_info("Successfully discarded data from future partition %" PRIu64 ".", p->partno);
e594a3b1
LP
2733 return 1;
2734}
2735
2736static int context_discard_gap_after(Context *context, Partition *p) {
2737 uint64_t gap, next = UINT64_MAX;
e594a3b1
LP
2738 int r;
2739
2740 assert(context);
2741 assert(!p || (p->offset != UINT64_MAX && p->new_size != UINT64_MAX));
2742
2743 if (p)
2744 gap = p->offset + p->new_size;
2745 else
2746 gap = context->start;
2747
2748 LIST_FOREACH(partitions, q, context->partitions) {
2749 if (q->dropped)
2750 continue;
2751
2752 assert(q->offset != UINT64_MAX);
2753 assert(q->new_size != UINT64_MAX);
2754
2755 if (q->offset < gap)
2756 continue;
2757
2758 if (next == UINT64_MAX || q->offset < next)
2759 next = q->offset;
2760 }
2761
2762 if (next == UINT64_MAX) {
2763 next = context->end;
2764 if (gap > next)
2765 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end.");
2766 }
2767
2768 assert(next >= gap);
2769 r = context_discard_range(context, gap, next - gap);
2770 if (r == -EOPNOTSUPP) {
2771 if (p)
5b5109e2 2772 log_info("Storage does not support discard, not discarding gap after partition %" PRIu64 ".", p->partno);
e594a3b1 2773 else
5b5109e2 2774 log_info("Storage does not support discard, not discarding gap at beginning of disk.");
e594a3b1
LP
2775 return 0;
2776 }
2777 if (r == 0) /* Too short */
2778 return 0;
2779 if (r < 0) {
2780 if (p)
2781 return log_error_errno(r, "Failed to discard gap after partition %" PRIu64 ".", p->partno);
2782 else
2783 return log_error_errno(r, "Failed to discard gap at beginning of disk.");
2784 }
2785
2786 if (p)
2787 log_info("Successfully discarded gap after partition %" PRIu64 ".", p->partno);
2788 else
2789 log_info("Successfully discarded gap at beginning of disk.");
2790
2791 return 0;
2792}
2793
2794static int context_wipe_and_discard(Context *context, bool from_scratch) {
e594a3b1
LP
2795 int r;
2796
2797 assert(context);
2798
2799 /* Wipe and discard the contents of all partitions we are about to create. We skip the discarding if
2800 * we were supposed to start from scratch anyway, as in that case we just discard the whole block
2801 * device in one go early on. */
2802
2803 LIST_FOREACH(partitions, p, context->partitions) {
2804
2805 if (!p->allocated_to_area)
2806 continue;
2807
e594a3b1
LP
2808 r = context_wipe_partition(context, p);
2809 if (r < 0)
2810 return r;
2811
2812 if (!from_scratch) {
f0cb1b95
LP
2813 r = context_discard_partition(context, p);
2814 if (r < 0)
2815 return r;
2816
e594a3b1
LP
2817 r = context_discard_gap_after(context, p);
2818 if (r < 0)
2819 return r;
2820 }
2821 }
2822
2823 if (!from_scratch) {
2824 r = context_discard_gap_after(context, NULL);
2825 if (r < 0)
2826 return r;
2827 }
2828
2829 return 0;
2830}
2831
b9df3536 2832static int partition_encrypt(
994b3031 2833 Context *context,
b9df3536
LP
2834 Partition *p,
2835 const char *node,
2836 struct crypt_device **ret_cd,
2837 char **ret_volume,
2838 int *ret_fd) {
3dd8ae5c 2839#if HAVE_LIBCRYPTSETUP
0d12936d 2840 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
b9df3536
LP
2841 _cleanup_(erase_and_freep) void *volume_key = NULL;
2842 _cleanup_free_ char *dm_name = NULL, *vol = NULL;
b9df3536
LP
2843 size_t volume_key_size = 256 / 8;
2844 sd_id128_t uuid;
2845 int r;
2846
994b3031 2847 assert(context);
b9df3536 2848 assert(p);
889914ef
LP
2849 assert(p->encrypt != ENCRYPT_OFF);
2850
2851 log_debug("Encryption mode for partition %" PRIu64 ": %s", p->partno, encrypt_mode_to_string(p->encrypt));
b9df3536 2852
0d12936d
LP
2853 r = dlopen_cryptsetup();
2854 if (r < 0)
2855 return log_error_errno(r, "libcryptsetup not found, cannot encrypt: %m");
2856
b9df3536
LP
2857 if (asprintf(&dm_name, "luks-repart-%08" PRIx64, random_u64()) < 0)
2858 return log_oom();
2859
2860 if (ret_volume) {
2861 vol = path_join("/dev/mapper/", dm_name);
2862 if (!vol)
2863 return log_oom();
2864 }
2865
2866 r = derive_uuid(p->new_uuid, "luks-uuid", &uuid);
2867 if (r < 0)
2868 return r;
2869
2870 log_info("Encrypting future partition %" PRIu64 "...", p->partno);
2871
2872 volume_key = malloc(volume_key_size);
2873 if (!volume_key)
2874 return log_oom();
2875
87cb1ab6 2876 r = crypto_random_bytes(volume_key, volume_key_size);
b9df3536
LP
2877 if (r < 0)
2878 return log_error_errno(r, "Failed to generate volume key: %m");
2879
0d12936d 2880 r = sym_crypt_init(&cd, node);
b9df3536
LP
2881 if (r < 0)
2882 return log_error_errno(r, "Failed to allocate libcryptsetup context: %m");
2883
2884 cryptsetup_enable_logging(cd);
2885
0d12936d 2886 r = sym_crypt_format(cd,
b9df3536
LP
2887 CRYPT_LUKS2,
2888 "aes",
2889 "xts-plain64",
b7416360 2890 SD_ID128_TO_UUID_STRING(uuid),
b9df3536
LP
2891 volume_key,
2892 volume_key_size,
2893 &(struct crypt_params_luks2) {
be9ce018 2894 .label = strempty(p->new_label),
994b3031 2895 .sector_size = context->sector_size,
b9df3536
LP
2896 });
2897 if (r < 0)
2898 return log_error_errno(r, "Failed to LUKS2 format future partition: %m");
2899
889914ef
LP
2900 if (IN_SET(p->encrypt, ENCRYPT_KEY_FILE, ENCRYPT_KEY_FILE_TPM2)) {
2901 r = sym_crypt_keyslot_add_by_volume_key(
2902 cd,
2903 CRYPT_ANY_SLOT,
2904 volume_key,
2905 volume_key_size,
2906 strempty(arg_key),
2907 arg_key_size);
2908 if (r < 0)
2909 return log_error_errno(r, "Failed to add LUKS2 key: %m");
2910 }
2911
2912 if (IN_SET(p->encrypt, ENCRYPT_TPM2, ENCRYPT_KEY_FILE_TPM2)) {
2913#if HAVE_TPM2
2914 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
2915 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
2916 _cleanup_(erase_and_freep) void *secret = NULL;
2917 _cleanup_free_ void *blob = NULL, *hash = NULL;
2918 size_t secret_size, blob_size, hash_size;
2b92a672 2919 uint16_t pcr_bank, primary_alg;
889914ef
LP
2920 int keyslot;
2921
d9b5841d
LP
2922 r = tpm2_seal(arg_tpm2_device,
2923 arg_tpm2_pcr_mask,
2924 /* pubkey= */ NULL, /* pubkey_size= */ 0,
2925 /* pubkey_pcr_mask= */ 0,
2926 /* pin= */ NULL,
2927 &secret, &secret_size,
2928 &blob, &blob_size,
2929 &hash, &hash_size,
2930 &pcr_bank,
2931 &primary_alg);
889914ef
LP
2932 if (r < 0)
2933 return log_error_errno(r, "Failed to seal to TPM2: %m");
2934
2935 r = base64mem(secret, secret_size, &base64_encoded);
2936 if (r < 0)
2937 return log_error_errno(r, "Failed to base64 encode secret key: %m");
2938
2939 r = cryptsetup_set_minimal_pbkdf(cd);
2940 if (r < 0)
2941 return log_error_errno(r, "Failed to set minimal PBKDF: %m");
2942
2943 keyslot = sym_crypt_keyslot_add_by_volume_key(
2944 cd,
2945 CRYPT_ANY_SLOT,
2946 volume_key,
2947 volume_key_size,
2948 base64_encoded,
2949 strlen(base64_encoded));
2950 if (keyslot < 0)
2951 return log_error_errno(keyslot, "Failed to add new TPM2 key to %s: %m", node);
2952
6c7a1681 2953 r = tpm2_make_luks2_json(keyslot, arg_tpm2_pcr_mask, pcr_bank, primary_alg, blob, blob_size, hash, hash_size, 0, &v);
889914ef
LP
2954 if (r < 0)
2955 return log_error_errno(r, "Failed to prepare TPM2 JSON token object: %m");
2956
2957 r = cryptsetup_add_token_json(cd, v);
2958 if (r < 0)
2959 return log_error_errno(r, "Failed to add TPM2 JSON token to LUKS2 header: %m");
2960#else
2961 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
2962 "Support for TPM2 enrollment not enabled.");
2963#endif
2964 }
b9df3536 2965
0d12936d 2966 r = sym_crypt_activate_by_volume_key(
b9df3536
LP
2967 cd,
2968 dm_name,
2969 volume_key,
2970 volume_key_size,
2971 arg_discard ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0);
2972 if (r < 0)
2973 return log_error_errno(r, "Failed to activate LUKS superblock: %m");
2974
2975 log_info("Successfully encrypted future partition %" PRIu64 ".", p->partno);
2976
2977 if (ret_fd) {
2978 _cleanup_close_ int dev_fd = -1;
2979
2980 dev_fd = open(vol, O_RDWR|O_CLOEXEC|O_NOCTTY);
2981 if (dev_fd < 0)
2982 return log_error_errno(errno, "Failed to open LUKS volume '%s': %m", vol);
2983
2984 *ret_fd = TAKE_FD(dev_fd);
2985 }
2986
2987 if (ret_cd)
2988 *ret_cd = TAKE_PTR(cd);
2989 if (ret_volume)
2990 *ret_volume = TAKE_PTR(vol);
2991
2992 return 0;
3dd8ae5c 2993#else
2994 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libcryptsetup is not supported, cannot encrypt: %m");
2995#endif
b9df3536
LP
2996}
2997
2998static int deactivate_luks(struct crypt_device *cd, const char *node) {
3dd8ae5c 2999#if HAVE_LIBCRYPTSETUP
b9df3536
LP
3000 int r;
3001
3002 if (!cd)
3003 return 0;
3004
3005 assert(node);
3006
3007 /* udev or so might access out block device in the background while we are done. Let's hence force
3008 * detach the volume. We sync'ed before, hence this should be safe. */
3009
0d12936d 3010 r = sym_crypt_deactivate_by_name(cd, basename(node), CRYPT_DEACTIVATE_FORCE);
b9df3536
LP
3011 if (r < 0)
3012 return log_error_errno(r, "Failed to deactivate LUKS device: %m");
3013
3014 return 1;
3dd8ae5c 3015#else
3016 return 0;
3017#endif
b9df3536
LP
3018}
3019
757bc2e4 3020static int context_copy_blocks(Context *context) {
b9df3536 3021 int whole_fd = -1, r;
757bc2e4
LP
3022
3023 assert(context);
3024
3025 /* Copy in file systems on the block level */
3026
3027 LIST_FOREACH(partitions, p, context->partitions) {
0d12936d 3028 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
b9df3536
LP
3029 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
3030 _cleanup_free_ char *encrypted = NULL;
3031 _cleanup_close_ int encrypted_dev_fd = -1;
b9df3536 3032 int target_fd;
757bc2e4
LP
3033
3034 if (p->copy_blocks_fd < 0)
3035 continue;
3036
3037 if (p->dropped)
3038 continue;
3039
3040 if (PARTITION_EXISTS(p)) /* Never copy over existing partitions */
3041 continue;
3042
3043 assert(p->new_size != UINT64_MAX);
3044 assert(p->copy_blocks_size != UINT64_MAX);
3045 assert(p->new_size >= p->copy_blocks_size);
3046
b9df3536
LP
3047 if (whole_fd < 0)
3048 assert_se((whole_fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
3049
889914ef 3050 if (p->encrypt != ENCRYPT_OFF) {
7f52206a 3051 r = loop_device_make(whole_fd, O_RDWR, p->offset, p->new_size, 0, LOCK_EX, &d);
b9df3536
LP
3052 if (r < 0)
3053 return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno);
3054
994b3031 3055 r = partition_encrypt(context, p, d->node, &cd, &encrypted, &encrypted_dev_fd);
b9df3536
LP
3056 if (r < 0)
3057 return log_error_errno(r, "Failed to encrypt device: %m");
757bc2e4 3058
b9df3536
LP
3059 if (flock(encrypted_dev_fd, LOCK_EX) < 0)
3060 return log_error_errno(errno, "Failed to lock LUKS device: %m");
3061
3062 target_fd = encrypted_dev_fd;
695cfd53 3063 } else {
b9df3536
LP
3064 if (lseek(whole_fd, p->offset, SEEK_SET) == (off_t) -1)
3065 return log_error_errno(errno, "Failed to seek to partition offset: %m");
3066
3067 target_fd = whole_fd;
3068 }
757bc2e4 3069
2b59bf51
ZJS
3070 log_info("Copying in '%s' (%s) on block level into future partition %" PRIu64 ".",
3071 p->copy_blocks_path, FORMAT_BYTES(p->copy_blocks_size), p->partno);
757bc2e4 3072
b9df3536 3073 r = copy_bytes_full(p->copy_blocks_fd, target_fd, p->copy_blocks_size, 0, NULL, NULL, NULL, NULL);
757bc2e4
LP
3074 if (r < 0)
3075 return log_error_errno(r, "Failed to copy in data from '%s': %m", p->copy_blocks_path);
3076
b9df3536 3077 if (fsync(target_fd) < 0)
8ac04a65 3078 return log_error_errno(errno, "Failed to synchronize copied data blocks: %m");
b9df3536 3079
889914ef 3080 if (p->encrypt != ENCRYPT_OFF) {
b9df3536
LP
3081 encrypted_dev_fd = safe_close(encrypted_dev_fd);
3082
3083 r = deactivate_luks(cd, encrypted);
3084 if (r < 0)
3085 return r;
3086
0d12936d 3087 sym_crypt_free(cd);
b9df3536
LP
3088 cd = NULL;
3089
3090 r = loop_device_sync(d);
3091 if (r < 0)
3092 return log_error_errno(r, "Failed to sync loopback device: %m");
3093 }
3094
757bc2e4
LP
3095 log_info("Copying in of '%s' on block level completed.", p->copy_blocks_path);
3096 }
3097
3098 return 0;
3099}
3100
8a794850 3101static int do_copy_files(Partition *p, const char *fs) {
8a794850
LP
3102 int r;
3103
3104 assert(p);
3105 assert(fs);
3106
3107 STRV_FOREACH_PAIR(source, target, p->copy_files) {
3108 _cleanup_close_ int sfd = -1, pfd = -1, tfd = -1;
8a794850
LP
3109
3110 sfd = chase_symlinks_and_open(*source, arg_root, CHASE_PREFIX_ROOT|CHASE_WARN, O_CLOEXEC|O_NOCTTY, NULL);
3111 if (sfd < 0)
3112 return log_error_errno(sfd, "Failed to open source file '%s%s': %m", strempty(arg_root), *source);
3113
3114 r = fd_verify_regular(sfd);
3115 if (r < 0) {
3116 if (r != -EISDIR)
3117 return log_error_errno(r, "Failed to check type of source file '%s': %m", *source);
3118
3119 /* We are looking at a directory */
3120 tfd = chase_symlinks_and_open(*target, fs, CHASE_PREFIX_ROOT|CHASE_WARN, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
3121 if (tfd < 0) {
f21a3a82
LP
3122 _cleanup_free_ char *dn = NULL, *fn = NULL;
3123
8a794850
LP
3124 if (tfd != -ENOENT)
3125 return log_error_errno(tfd, "Failed to open target directory '%s': %m", *target);
3126
f21a3a82
LP
3127 r = path_extract_filename(*target, &fn);
3128 if (r < 0)
3129 return log_error_errno(r, "Failed to extract filename from '%s': %m", *target);
3130
3131 r = path_extract_directory(*target, &dn);
3132 if (r < 0)
3133 return log_error_errno(r, "Failed to extract directory from '%s': %m", *target);
3134
8a794850
LP
3135 r = mkdir_p_root(fs, dn, UID_INVALID, GID_INVALID, 0755);
3136 if (r < 0)
3137 return log_error_errno(r, "Failed to create parent directory '%s': %m", dn);
3138
3139 pfd = chase_symlinks_and_open(dn, fs, CHASE_PREFIX_ROOT|CHASE_WARN, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
3140 if (pfd < 0)
3141 return log_error_errno(pfd, "Failed to open parent directory of target: %m");
3142
652d9040
LP
3143 r = copy_tree_at(
3144 sfd, ".",
6020d00d 3145 pfd, fn,
652d9040 3146 UID_INVALID, GID_INVALID,
23e026de 3147 COPY_REFLINK|COPY_MERGE|COPY_REPLACE|COPY_SIGINT|COPY_HARDLINKS|COPY_ALL_XATTRS);
8a794850 3148 } else
652d9040
LP
3149 r = copy_tree_at(
3150 sfd, ".",
3151 tfd, ".",
3152 UID_INVALID, GID_INVALID,
23e026de 3153 COPY_REFLINK|COPY_MERGE|COPY_REPLACE|COPY_SIGINT|COPY_HARDLINKS|COPY_ALL_XATTRS);
8a794850 3154 if (r < 0)
554a2b64 3155 return log_error_errno(r, "Failed to copy '%s' to '%s%s': %m", *source, strempty(arg_root), *target);
8a794850 3156 } else {
f21a3a82
LP
3157 _cleanup_free_ char *dn = NULL, *fn = NULL;
3158
8a794850
LP
3159 /* We are looking at a regular file */
3160
f21a3a82
LP
3161 r = path_extract_filename(*target, &fn);
3162 if (r == -EADDRNOTAVAIL || r == O_DIRECTORY)
3163 return log_error_errno(SYNTHETIC_ERRNO(EISDIR),
3164 "Target path '%s' refers to a directory, but source path '%s' refers to regular file, can't copy.", *target, *source);
3165 if (r < 0)
3166 return log_error_errno(r, "Failed to extract filename from '%s': %m", *target);
3167
3168 r = path_extract_directory(*target, &dn);
3169 if (r < 0)
3170 return log_error_errno(r, "Failed to extract directory from '%s': %m", *target);
3171
8a794850
LP
3172 r = mkdir_p_root(fs, dn, UID_INVALID, GID_INVALID, 0755);
3173 if (r < 0)
3174 return log_error_errno(r, "Failed to create parent directory: %m");
3175
3176 pfd = chase_symlinks_and_open(dn, fs, CHASE_PREFIX_ROOT|CHASE_WARN, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
3177 if (pfd < 0)
a0ff9971 3178 return log_error_errno(pfd, "Failed to open parent directory of target: %m");
8a794850 3179
e2819067 3180 tfd = openat(pfd, fn, O_CREAT|O_EXCL|O_WRONLY|O_CLOEXEC, 0700);
8a794850
LP
3181 if (tfd < 0)
3182 return log_error_errno(errno, "Failed to create target file '%s': %m", *target);
3183
3184 r = copy_bytes(sfd, tfd, UINT64_MAX, COPY_REFLINK|COPY_SIGINT);
3185 if (r < 0)
554a2b64 3186 return log_error_errno(r, "Failed to copy '%s' to '%s%s': %m", *source, strempty(arg_root), *target);
8a794850 3187
23e026de 3188 (void) copy_xattr(sfd, tfd, COPY_ALL_XATTRS);
8a794850
LP
3189 (void) copy_access(sfd, tfd);
3190 (void) copy_times(sfd, tfd, 0);
3191 }
3192 }
3193
3194 return 0;
3195}
3196
d83d8048 3197static int do_make_directories(Partition *p, const char *fs) {
d83d8048
LP
3198 int r;
3199
3200 assert(p);
3201 assert(fs);
3202
3203 STRV_FOREACH(d, p->make_directories) {
3204
3205 r = mkdir_p_root(fs, *d, UID_INVALID, GID_INVALID, 0755);
3206 if (r < 0)
3207 return log_error_errno(r, "Failed to create directory '%s' in file system: %m", *d);
3208 }
3209
3210 return 0;
3211}
3212
3213static int partition_populate(Partition *p, const char *node) {
8a794850
LP
3214 int r;
3215
3216 assert(p);
3217 assert(node);
3218
d83d8048 3219 if (strv_isempty(p->copy_files) && strv_isempty(p->make_directories))
8a794850
LP
3220 return 0;
3221
3222 log_info("Populating partition %" PRIu64 " with files.", p->partno);
3223
3224 /* We copy in a child process, since we have to mount the fs for that, and we don't want that fs to
3225 * appear in the host namespace. Hence we fork a child that has its own file system namespace and
3226 * detached mount propagation. */
3227
3228 r = safe_fork("(sd-copy)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, NULL);
3229 if (r < 0)
3230 return r;
3231 if (r == 0) {
3232 static const char fs[] = "/run/systemd/mount-root";
3233 /* This is a child process with its own mount namespace and propagation to host turned off */
3234
3235 r = mkdir_p(fs, 0700);
3236 if (r < 0) {
3237 log_error_errno(r, "Failed to create mount point: %m");
3238 _exit(EXIT_FAILURE);
3239 }
3240
511a8cfe 3241 if (mount_nofollow_verbose(LOG_ERR, node, fs, p->format, MS_NOATIME|MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL) < 0)
8a794850
LP
3242 _exit(EXIT_FAILURE);
3243
3244 if (do_copy_files(p, fs) < 0)
3245 _exit(EXIT_FAILURE);
3246
d83d8048
LP
3247 if (do_make_directories(p, fs) < 0)
3248 _exit(EXIT_FAILURE);
3249
8a794850
LP
3250 r = syncfs_path(AT_FDCWD, fs);
3251 if (r < 0) {
3252 log_error_errno(r, "Failed to synchronize written files: %m");
3253 _exit(EXIT_FAILURE);
3254 }
3255
3256 _exit(EXIT_SUCCESS);
3257 }
3258
3259 log_info("Successfully populated partition %" PRIu64 " with files.", p->partno);
3260 return 0;
3261}
3262
53171c04 3263static int context_mkfs(Context *context) {
53171c04
LP
3264 int fd = -1, r;
3265
3266 assert(context);
3267
3268 /* Make a file system */
3269
3270 LIST_FOREACH(partitions, p, context->partitions) {
0d12936d 3271 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
53171c04 3272 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
b9df3536
LP
3273 _cleanup_free_ char *encrypted = NULL;
3274 _cleanup_close_ int encrypted_dev_fd = -1;
3275 const char *fsdev;
53171c04
LP
3276 sd_id128_t fs_uuid;
3277
3278 if (p->dropped)
3279 continue;
3280
3281 if (PARTITION_EXISTS(p)) /* Never format existing partitions */
3282 continue;
3283
3284 if (!p->format)
3285 continue;
3286
3287 assert(p->offset != UINT64_MAX);
3288 assert(p->new_size != UINT64_MAX);
3289
3290 if (fd < 0)
3291 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
3292
3293 /* Loopback block devices are not only useful to turn regular files into block devices, but
3294 * also to cut out sections of block devices into new block devices. */
3295
7f52206a 3296 r = loop_device_make(fd, O_RDWR, p->offset, p->new_size, 0, LOCK_EX, &d);
53171c04 3297 if (r < 0)
5b5109e2 3298 return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno);
53171c04 3299
889914ef 3300 if (p->encrypt != ENCRYPT_OFF) {
994b3031 3301 r = partition_encrypt(context, p, d->node, &cd, &encrypted, &encrypted_dev_fd);
b9df3536
LP
3302 if (r < 0)
3303 return log_error_errno(r, "Failed to encrypt device: %m");
3304
3305 if (flock(encrypted_dev_fd, LOCK_EX) < 0)
3306 return log_error_errno(errno, "Failed to lock LUKS device: %m");
3307
3308 fsdev = encrypted;
3309 } else
3310 fsdev = d->node;
3311
53171c04
LP
3312 log_info("Formatting future partition %" PRIu64 ".", p->partno);
3313
3314 /* Calculate the UUID for the file system as HMAC-SHA256 of the string "file-system-uuid",
3315 * keyed off the partition UUID. */
3316 r = derive_uuid(p->new_uuid, "file-system-uuid", &fs_uuid);
3317 if (r < 0)
3318 return r;
3319
be9ce018 3320 r = make_filesystem(fsdev, p->format, strempty(p->new_label), fs_uuid, arg_discard);
b9df3536
LP
3321 if (r < 0) {
3322 encrypted_dev_fd = safe_close(encrypted_dev_fd);
3323 (void) deactivate_luks(cd, encrypted);
53171c04 3324 return r;
b9df3536 3325 }
53171c04
LP
3326
3327 log_info("Successfully formatted future partition %" PRIu64 ".", p->partno);
3328
b9df3536 3329 /* The file system is now created, no need to delay udev further */
889914ef 3330 if (p->encrypt != ENCRYPT_OFF)
b9df3536
LP
3331 if (flock(encrypted_dev_fd, LOCK_UN) < 0)
3332 return log_error_errno(errno, "Failed to unlock LUKS device: %m");
3333
d83d8048 3334 r = partition_populate(p, fsdev);
b9df3536
LP
3335 if (r < 0) {
3336 encrypted_dev_fd = safe_close(encrypted_dev_fd);
3337 (void) deactivate_luks(cd, encrypted);
8a794850 3338 return r;
b9df3536
LP
3339 }
3340
3341 /* Note that we always sync explicitly here, since mkfs.fat doesn't do that on its own, and
3342 * if we don't sync before detaching a block device the in-flight sectors possibly won't hit
3343 * the disk. */
3344
889914ef 3345 if (p->encrypt != ENCRYPT_OFF) {
b9df3536 3346 if (fsync(encrypted_dev_fd) < 0)
8ac04a65 3347 return log_error_errno(errno, "Failed to synchronize LUKS volume: %m");
b9df3536
LP
3348 encrypted_dev_fd = safe_close(encrypted_dev_fd);
3349
3350 r = deactivate_luks(cd, encrypted);
3351 if (r < 0)
3352 return r;
3353
0d12936d 3354 sym_crypt_free(cd);
b9df3536
LP
3355 cd = NULL;
3356 }
8a794850 3357
53171c04
LP
3358 r = loop_device_sync(d);
3359 if (r < 0)
3360 return log_error_errno(r, "Failed to sync loopback device: %m");
3361 }
3362
3363 return 0;
3364}
3365
b5b7879a
DDM
3366static int do_verity_format(
3367 LoopDevice *data_device,
3368 LoopDevice *hash_device,
3369 uint64_t sector_size,
3370 uint8_t **ret_roothash,
3371 size_t *ret_roothash_size) {
3372
3373#if HAVE_LIBCRYPTSETUP
3374 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
3375 _cleanup_free_ uint8_t *rh = NULL;
3376 size_t rhs;
3377 int r;
3378
3379 assert(data_device);
3380 assert(hash_device);
3381 assert(sector_size > 0);
3382 assert(ret_roothash);
3383 assert(ret_roothash_size);
3384
3385 r = dlopen_cryptsetup();
3386 if (r < 0)
3387 return log_error_errno(r, "libcryptsetup not found, cannot setup verity: %m");
3388
3389 r = sym_crypt_init(&cd, hash_device->node);
3390 if (r < 0)
3391 return log_error_errno(r, "Failed to allocate libcryptsetup context: %m");
3392
3393 r = sym_crypt_format(
3394 cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0,
3395 &(struct crypt_params_verity){
3396 .data_device = data_device->node,
3397 .flags = CRYPT_VERITY_CREATE_HASH,
3398 .hash_name = "sha256",
3399 .hash_type = 1,
3400 .data_block_size = sector_size,
3401 .hash_block_size = sector_size,
3402 .salt_size = 32,
3403 });
3404 if (r < 0)
3405 return log_error_errno(r, "Failed to setup verity hash data: %m");
3406
3407 r = sym_crypt_get_volume_key_size(cd);
3408 if (r < 0)
3409 return log_error_errno(r, "Failed to determine verity root hash size: %m");
3410 rhs = (size_t) r;
3411
3412 rh = malloc(rhs);
3413 if (!rh)
3414 return log_oom();
3415
3416 r = sym_crypt_volume_key_get(cd, CRYPT_ANY_SLOT, (char *) rh, &rhs, NULL, 0);
3417 if (r < 0)
3418 return log_error_errno(r, "Failed to get verity root hash: %m");
3419
3420 *ret_roothash = TAKE_PTR(rh);
3421 *ret_roothash_size = rhs;
3422
3423 return 0;
3424#else
3425 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libcryptsetup is not supported, cannot setup verity: %m");
3426#endif
3427}
3428
3429static int context_verity(Context *context) {
3430 int fd = -1, r;
3431
3432 assert(context);
3433
3434 LIST_FOREACH(partitions, p, context->partitions) {
3435 Partition *dp;
3436 _cleanup_(loop_device_unrefp) LoopDevice *hash_device = NULL, *data_device = NULL;
3437 _cleanup_free_ uint8_t *rh = NULL;
3438 size_t rhs = 0; /* Initialize to work around for GCC false positive. */
3439
3440 if (p->dropped)
3441 continue;
3442
3443 if (PARTITION_EXISTS(p)) /* Never format existing partitions */
3444 continue;
3445
3446 if (p->verity != VERITY_HASH)
3447 continue;
3448
3449 assert_se(dp = p->siblings[VERITY_DATA]);
3450 assert(!dp->dropped);
3451
3452 if (fd < 0)
3453 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
3454
3455 r = loop_device_make(fd, O_RDONLY, dp->offset, dp->new_size, 0, LOCK_EX, &data_device);
3456 if (r < 0)
3457 return log_error_errno(r,
3458 "Failed to make loopback device of verity data partition %" PRIu64 ": %m",
3459 p->partno);
3460
3461 r = loop_device_make(fd, O_RDWR, p->offset, p->new_size, 0, LOCK_EX, &hash_device);
3462 if (r < 0)
3463 return log_error_errno(r,
3464 "Failed to make loopback device of verity hash partition %" PRIu64 ": %m",
3465 p->partno);
3466
3467 r = do_verity_format(data_device, hash_device, context->sector_size, &rh, &rhs);
3468 if (r < 0)
3469 return r;
3470
3471 assert(rhs >= sizeof(sd_id128_t) * 2);
3472
3473 if (!dp->new_uuid_is_set) {
3474 memcpy_safe(dp->new_uuid.bytes, rh, sizeof(sd_id128_t));
3475 dp->new_uuid_is_set = true;
3476 }
3477
3478 if (!p->new_uuid_is_set) {
3479 memcpy_safe(p->new_uuid.bytes, rh + rhs - sizeof(sd_id128_t), sizeof(sd_id128_t));
3480 p->new_uuid_is_set = true;
3481 }
3482
3483 p->roothash = TAKE_PTR(rh);
3484 p->roothash_size = rhs;
3485 }
3486
3487 return 0;
3488}
3489
e594a3b1
LP
3490static int partition_acquire_uuid(Context *context, Partition *p, sd_id128_t *ret) {
3491 struct {
3492 sd_id128_t type_uuid;
3493 uint64_t counter;
695cfd53 3494 } _packed_ plaintext = {};
e594a3b1 3495 union {
ade99252 3496 uint8_t md[SHA256_DIGEST_SIZE];
e594a3b1
LP
3497 sd_id128_t id;
3498 } result;
3499
3500 uint64_t k = 0;
e594a3b1
LP
3501 int r;
3502
3503 assert(context);
3504 assert(p);
3505 assert(ret);
3506
3507 /* Calculate a good UUID for the indicated partition. We want a certain degree of reproducibility,
3508 * hence we won't generate the UUIDs randomly. Instead we use a cryptographic hash (precisely:
3509 * HMAC-SHA256) to derive them from a single seed. The seed is generally the machine ID of the
3510 * installation we are processing, but if random behaviour is desired can be random, too. We use the
3511 * seed value as key for the HMAC (since the machine ID is something we generally don't want to leak)
3512 * and the partition type as plaintext. The partition type is suffixed with a counter (only for the
3513 * second and later partition of the same type) if we have more than one partition of the same
3514 * time. Or in other words:
3515 *
3516 * With:
3517 * SEED := /etc/machine-id
3518 *
3519 * If first partition instance of type TYPE_UUID:
3520 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID)
3521 *
3522 * For all later partition instances of type TYPE_UUID with INSTANCE being the LE64 encoded instance number:
3523 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID || INSTANCE)
3524 */
3525
3526 LIST_FOREACH(partitions, q, context->partitions) {
3527 if (p == q)
3528 break;
3529
3530 if (!sd_id128_equal(p->type_uuid, q->type_uuid))
3531 continue;
3532
3533 k++;
3534 }
3535
3536 plaintext.type_uuid = p->type_uuid;
3537 plaintext.counter = htole64(k);
3538
ade99252
KK
3539 hmac_sha256(context->seed.bytes, sizeof(context->seed.bytes),
3540 &plaintext,
3541 k == 0 ? sizeof(sd_id128_t) : sizeof(plaintext),
3542 result.md);
e594a3b1
LP
3543
3544 /* Take the first half, mark it as v4 UUID */
3545 assert_cc(sizeof(result.md) == sizeof(result.id) * 2);
3546 result.id = id128_make_v4_uuid(result.id);
3547
3548 /* Ensure this partition UUID is actually unique, and there's no remaining partition from an earlier run? */
3549 LIST_FOREACH(partitions, q, context->partitions) {
3550 if (p == q)
3551 continue;
3552
580f48cc 3553 if (sd_id128_in_set(result.id, q->current_uuid, q->new_uuid)) {
da1af43d 3554 log_warning("Partition UUID calculated from seed for partition %" PRIu64 " already used, reverting to randomized UUID.", p->partno);
e594a3b1
LP
3555
3556 r = sd_id128_randomize(&result.id);
3557 if (r < 0)
3558 return log_error_errno(r, "Failed to generate randomized UUID: %m");
3559
3560 break;
3561 }
3562 }
3563
3564 *ret = result.id;
3565 return 0;
3566}
3567
3568static int partition_acquire_label(Context *context, Partition *p, char **ret) {
3569 _cleanup_free_ char *label = NULL;
3570 const char *prefix;
3571 unsigned k = 1;
3572
3573 assert(context);
3574 assert(p);
3575 assert(ret);
3576
3577 prefix = gpt_partition_type_uuid_to_string(p->type_uuid);
3578 if (!prefix)
3579 prefix = "linux";
3580
3581 for (;;) {
3582 const char *ll = label ?: prefix;
3583 bool retry = false;
e594a3b1
LP
3584
3585 LIST_FOREACH(partitions, q, context->partitions) {
3586 if (p == q)
3587 break;
3588
3589 if (streq_ptr(ll, q->current_label) ||
3590 streq_ptr(ll, q->new_label)) {
3591 retry = true;
3592 break;
3593 }
3594 }
3595
3596 if (!retry)
3597 break;
3598
3599 label = mfree(label);
e594a3b1
LP
3600 if (asprintf(&label, "%s-%u", prefix, ++k) < 0)
3601 return log_oom();
3602 }
3603
3604 if (!label) {
3605 label = strdup(prefix);
3606 if (!label)
3607 return log_oom();
3608 }
3609
3610 *ret = TAKE_PTR(label);
3611 return 0;
3612}
3613
3614static int context_acquire_partition_uuids_and_labels(Context *context) {
e594a3b1
LP
3615 int r;
3616
3617 assert(context);
3618
3619 LIST_FOREACH(partitions, p, context->partitions) {
e594a3b1
LP
3620 /* Never touch foreign partitions */
3621 if (PARTITION_IS_FOREIGN(p)) {
3622 p->new_uuid = p->current_uuid;
3623
3624 if (p->current_label) {
78eee6ce
LP
3625 r = free_and_strdup_warn(&p->new_label, strempty(p->current_label));
3626 if (r < 0)
3627 return r;
e594a3b1
LP
3628 }
3629
3630 continue;
3631 }
3632
3633 if (!sd_id128_is_null(p->current_uuid))
3634 p->new_uuid = p->current_uuid; /* Never change initialized UUIDs */
b5b7879a 3635 else if (!p->new_uuid_is_set && p->verity == VERITY_OFF) {
12963533 3636 /* Not explicitly set by user! */
e594a3b1
LP
3637 r = partition_acquire_uuid(context, p, &p->new_uuid);
3638 if (r < 0)
3639 return r;
11749b61
DDM
3640
3641 p->new_uuid_is_set = true;
e594a3b1
LP
3642 }
3643
3644 if (!isempty(p->current_label)) {
78eee6ce
LP
3645 /* never change initialized labels */
3646 r = free_and_strdup_warn(&p->new_label, p->current_label);
3647 if (r < 0)
3648 return r;
12963533
TH
3649 } else if (!p->new_label) {
3650 /* Not explicitly set by user! */
3651
e594a3b1
LP
3652 r = partition_acquire_label(context, p, &p->new_label);
3653 if (r < 0)
3654 return r;
3655 }
3656 }
3657
3658 return 0;
3659}
3660
e73309c5
LP
3661static int set_gpt_flags(struct fdisk_partition *q, uint64_t flags) {
3662 _cleanup_free_ char *a = NULL;
3663
3664 for (unsigned i = 0; i < sizeof(flags) * 8; i++) {
3665 uint64_t bit = UINT64_C(1) << i;
3666 char buf[DECIMAL_STR_MAX(unsigned)+1];
3667
3668 if (!FLAGS_SET(flags, bit))
3669 continue;
3670
3671 xsprintf(buf, "%u", i);
3672 if (!strextend_with_separator(&a, ",", buf))
3673 return -ENOMEM;
3674 }
3675
3676 return fdisk_partition_set_attrs(q, a);
3677}
3678
1c41c1dc
LP
3679static uint64_t partition_merge_flags(Partition *p) {
3680 uint64_t f;
3681
3682 assert(p);
3683
3684 f = p->gpt_flags;
3685
ff0771bf
LP
3686 if (p->no_auto >= 0) {
3687 if (gpt_partition_type_knows_no_auto(p->type_uuid))
3688 SET_FLAG(f, GPT_FLAG_NO_AUTO, p->no_auto);
3689 else {
b7416360 3690 char buffer[SD_ID128_UUID_STRING_MAX];
ff0771bf
LP
3691 log_warning("Configured NoAuto=%s for partition type '%s' that doesn't support it, ignoring.",
3692 yes_no(p->no_auto),
3693 gpt_partition_type_uuid_to_string_harder(p->type_uuid, buffer));
3694 }
3695 }
3696
1c41c1dc
LP
3697 if (p->read_only >= 0) {
3698 if (gpt_partition_type_knows_read_only(p->type_uuid))
3699 SET_FLAG(f, GPT_FLAG_READ_ONLY, p->read_only);
3700 else {
b7416360 3701 char buffer[SD_ID128_UUID_STRING_MAX];
1c41c1dc
LP
3702 log_warning("Configured ReadOnly=%s for partition type '%s' that doesn't support it, ignoring.",
3703 yes_no(p->read_only),
3704 gpt_partition_type_uuid_to_string_harder(p->type_uuid, buffer));
3705 }
3706 }
3707
3708 if (p->growfs >= 0) {
3709 if (gpt_partition_type_knows_growfs(p->type_uuid))
3710 SET_FLAG(f, GPT_FLAG_GROWFS, p->growfs);
3711 else {
b7416360 3712 char buffer[SD_ID128_UUID_STRING_MAX];
1c41c1dc
LP
3713 log_warning("Configured GrowFileSystem=%s for partition type '%s' that doesn't support it, ignoring.",
3714 yes_no(p->growfs),
3715 gpt_partition_type_uuid_to_string_harder(p->type_uuid, buffer));
3716 }
3717 }
3718
3719 return f;
3720}
3721
f28d4f42 3722static int context_mangle_partitions(Context *context) {
f28d4f42 3723 int r;
e594a3b1
LP
3724
3725 assert(context);
3726
e594a3b1
LP
3727 LIST_FOREACH(partitions, p, context->partitions) {
3728 if (p->dropped)
3729 continue;
3730
3731 assert(p->new_size != UINT64_MAX);
3732 assert(p->offset != UINT64_MAX);
3733 assert(p->partno != UINT64_MAX);
3734
3735 if (PARTITION_EXISTS(p)) {
3736 bool changed = false;
3737
3738 assert(p->current_partition);
3739
3740 if (p->new_size != p->current_size) {
3741 assert(p->new_size >= p->current_size);
994b3031 3742 assert(p->new_size % context->sector_size == 0);
e594a3b1
LP
3743
3744 r = fdisk_partition_size_explicit(p->current_partition, true);
3745 if (r < 0)
3746 return log_error_errno(r, "Failed to enable explicit sizing: %m");
3747
994b3031 3748 r = fdisk_partition_set_size(p->current_partition, p->new_size / context->sector_size);
e594a3b1
LP
3749 if (r < 0)
3750 return log_error_errno(r, "Failed to grow partition: %m");
3751
3752 log_info("Growing existing partition %" PRIu64 ".", p->partno);
3753 changed = true;
3754 }
3755
3756 if (!sd_id128_equal(p->new_uuid, p->current_uuid)) {
b7416360 3757 r = fdisk_partition_set_uuid(p->current_partition, SD_ID128_TO_UUID_STRING(p->new_uuid));
e594a3b1
LP
3758 if (r < 0)
3759 return log_error_errno(r, "Failed to set partition UUID: %m");
3760
3761 log_info("Initializing UUID of existing partition %" PRIu64 ".", p->partno);
3762 changed = true;
3763 }
3764
3765 if (!streq_ptr(p->new_label, p->current_label)) {
be9ce018 3766 r = fdisk_partition_set_name(p->current_partition, strempty(p->new_label));
e594a3b1
LP
3767 if (r < 0)
3768 return log_error_errno(r, "Failed to set partition label: %m");
3769
3770 log_info("Setting partition label of existing partition %" PRIu64 ".", p->partno);
3771 changed = true;
3772 }
3773
3774 if (changed) {
3775 assert(!PARTITION_IS_FOREIGN(p)); /* never touch foreign partitions */
3776
3777 r = fdisk_set_partition(context->fdisk_context, p->partno, p->current_partition);
3778 if (r < 0)
3779 return log_error_errno(r, "Failed to update partition: %m");
3780 }
3781 } else {
3782 _cleanup_(fdisk_unref_partitionp) struct fdisk_partition *q = NULL;
3783 _cleanup_(fdisk_unref_parttypep) struct fdisk_parttype *t = NULL;
e594a3b1
LP
3784
3785 assert(!p->new_partition);
994b3031
LP
3786 assert(p->offset % context->sector_size == 0);
3787 assert(p->new_size % context->sector_size == 0);
be9ce018 3788 assert(p->new_label);
e594a3b1
LP
3789
3790 t = fdisk_new_parttype();
3791 if (!t)
3792 return log_oom();
3793
b7416360 3794 r = fdisk_parttype_set_typestr(t, SD_ID128_TO_UUID_STRING(p->type_uuid));
e594a3b1
LP
3795 if (r < 0)
3796 return log_error_errno(r, "Failed to initialize partition type: %m");
3797
3798 q = fdisk_new_partition();
3799 if (!q)
3800 return log_oom();
3801
3802 r = fdisk_partition_set_type(q, t);
3803 if (r < 0)
3804 return log_error_errno(r, "Failed to set partition type: %m");
3805
3806 r = fdisk_partition_size_explicit(q, true);
3807 if (r < 0)
3808 return log_error_errno(r, "Failed to enable explicit sizing: %m");
3809
994b3031 3810 r = fdisk_partition_set_start(q, p->offset / context->sector_size);
e594a3b1
LP
3811 if (r < 0)
3812 return log_error_errno(r, "Failed to position partition: %m");
3813
994b3031 3814 r = fdisk_partition_set_size(q, p->new_size / context->sector_size);
e594a3b1
LP
3815 if (r < 0)
3816 return log_error_errno(r, "Failed to grow partition: %m");
3817
3818 r = fdisk_partition_set_partno(q, p->partno);
3819 if (r < 0)
3820 return log_error_errno(r, "Failed to set partition number: %m");
3821
b7416360 3822 r = fdisk_partition_set_uuid(q, SD_ID128_TO_UUID_STRING(p->new_uuid));
e594a3b1
LP
3823 if (r < 0)
3824 return log_error_errno(r, "Failed to set partition UUID: %m");
3825
be9ce018 3826 r = fdisk_partition_set_name(q, strempty(p->new_label));
e594a3b1
LP
3827 if (r < 0)
3828 return log_error_errno(r, "Failed to set partition label: %m");
3829
ff0771bf 3830 /* Merge the no auto + read only + growfs setting with the literal flags, and set them for the partition */
1c41c1dc 3831 r = set_gpt_flags(q, partition_merge_flags(p));
e73309c5
LP
3832 if (r < 0)
3833 return log_error_errno(r, "Failed to set GPT partition flags: %m");
3834
5b5109e2 3835 log_info("Adding new partition %" PRIu64 " to partition table.", p->partno);
e594a3b1
LP
3836
3837 r = fdisk_add_partition(context->fdisk_context, q, NULL);
3838 if (r < 0)
3839 return log_error_errno(r, "Failed to add partition: %m");
3840
3841 assert(!p->new_partition);
3842 p->new_partition = TAKE_PTR(q);
3843 }
3844 }
3845
f28d4f42
LP
3846 return 0;
3847}
3848
3849static int context_write_partition_table(
3850 Context *context,
3851 const char *node,
3852 bool from_scratch) {
3853
3854 _cleanup_(fdisk_unref_tablep) struct fdisk_table *original_table = NULL;
3855 int capable, r;
3856
3857 assert(context);
3858
f28d4f42
LP
3859 if (!from_scratch && !context_changed(context)) {
3860 log_info("No changes.");
3861 return 0;
3862 }
3863
3864 if (arg_dry_run) {
3865 log_notice("Refusing to repartition, please re-run with --dry-run=no.");
3866 return 0;
3867 }
3868
3869 log_info("Applying changes.");
3870
3871 if (from_scratch) {
81873a6b
LP
3872 r = context_wipe_range(context, 0, context->total);
3873 if (r < 0)
3874 return r;
3875
3876 log_info("Wiped block device.");
3877
f28d4f42
LP
3878 r = context_discard_range(context, 0, context->total);
3879 if (r == -EOPNOTSUPP)
5b5109e2 3880 log_info("Storage does not support discard, not discarding entire block device data.");
f28d4f42
LP
3881 else if (r < 0)
3882 return log_error_errno(r, "Failed to discard entire block device: %m");
3883 else if (r > 0)
3884 log_info("Discarded entire block device.");
3885 }
3886
3887 r = fdisk_get_partitions(context->fdisk_context, &original_table);
3888 if (r < 0)
3889 return log_error_errno(r, "Failed to acquire partition table: %m");
3890
3891 /* Wipe fs signatures and discard sectors where the new partitions are going to be placed and in the
3892 * gaps between partitions, just to be sure. */
3893 r = context_wipe_and_discard(context, from_scratch);
3894 if (r < 0)
3895 return r;
3896
3897 r = context_copy_blocks(context);
3898 if (r < 0)
3899 return r;
3900
3901 r = context_mkfs(context);
3902 if (r < 0)
3903 return r;
3904
b5b7879a
DDM
3905 r = context_verity(context);
3906 if (r < 0)
3907 return r;
3908
f28d4f42
LP
3909 r = context_mangle_partitions(context);
3910 if (r < 0)
3911 return r;
3912
e594a3b1
LP
3913 log_info("Writing new partition table.");
3914
3915 r = fdisk_write_disklabel(context->fdisk_context);
3916 if (r < 0)
3917 return log_error_errno(r, "Failed to write partition table: %m");
3918
911ba624 3919 capable = blockdev_partscan_enabled(fdisk_get_devfd(context->fdisk_context));
9a1deb85
LP
3920 if (capable == -ENOTBLK)
3921 log_debug("Not telling kernel to reread partition table, since we are not operating on a block device.");
3922 else if (capable < 0)
911ba624 3923 return log_error_errno(capable, "Failed to check if block device supports partition scanning: %m");
9a1deb85 3924 else if (capable > 0) {
e594a3b1
LP
3925 log_info("Telling kernel to reread partition table.");
3926
3927 if (from_scratch)
3928 r = fdisk_reread_partition_table(context->fdisk_context);
3929 else
3930 r = fdisk_reread_changes(context->fdisk_context, original_table);
3931 if (r < 0)
3932 return log_error_errno(r, "Failed to reread partition table: %m");
3933 } else
3934 log_notice("Not telling kernel to reread partition table, because selected image does not support kernel partition block devices.");
3935
3936 log_info("All done.");
3937
3938 return 0;
3939}
3940
3941static int context_read_seed(Context *context, const char *root) {
3942 int r;
3943
3944 assert(context);
3945
3946 if (!sd_id128_is_null(context->seed))
3947 return 0;
3948
3949 if (!arg_randomize) {
3950 _cleanup_close_ int fd = -1;
3951
3952 fd = chase_symlinks_and_open("/etc/machine-id", root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC, NULL);
3953 if (fd == -ENOENT)
3954 log_info("No machine ID set, using randomized partition UUIDs.");
3955 else if (fd < 0)
3956 return log_error_errno(fd, "Failed to determine machine ID of image: %m");
3957 else {
448b782c 3958 r = id128_read_fd(fd, ID128_PLAIN_OR_UNINIT, &context->seed);
e594a3b1
LP
3959 if (r == -ENOMEDIUM)
3960 log_info("No machine ID set, using randomized partition UUIDs.");
3961 else if (r < 0)
3962 return log_error_errno(r, "Failed to parse machine ID of image: %m");
3963
3964 return 0;
3965 }
3966 }
3967
3968 r = sd_id128_randomize(&context->seed);
3969 if (r < 0)
3970 return log_error_errno(r, "Failed to generate randomized seed: %m");
3971
3972 return 0;
3973}
3974
3975static int context_factory_reset(Context *context, bool from_scratch) {
e594a3b1
LP
3976 size_t n = 0;
3977 int r;
3978
3979 assert(context);
3980
3981 if (arg_factory_reset <= 0)
3982 return 0;
3983
3984 if (from_scratch) /* Nothing to reset if we start from scratch */
3985 return 0;
3986
3987 if (arg_dry_run) {
3988 log_notice("Refusing to factory reset, please re-run with --dry-run=no.");
3989 return 0;
3990 }
3991
3992 log_info("Applying factory reset.");
3993
3994 LIST_FOREACH(partitions, p, context->partitions) {
3995
3996 if (!p->factory_reset || !PARTITION_EXISTS(p))
3997 continue;
3998
3999 assert(p->partno != UINT64_MAX);
4000
4001 log_info("Removing partition %" PRIu64 " for factory reset.", p->partno);
4002
4003 r = fdisk_delete_partition(context->fdisk_context, p->partno);
4004 if (r < 0)
4005 return log_error_errno(r, "Failed to remove partition %" PRIu64 ": %m", p->partno);
4006
4007 n++;
4008 }
4009
4010 if (n == 0) {
4011 log_info("Factory reset requested, but no partitions to delete found.");
4012 return 0;
4013 }
4014
4015 r = fdisk_write_disklabel(context->fdisk_context);
4016 if (r < 0)
4017 return log_error_errno(r, "Failed to write disk label: %m");
4018
4019 log_info("Successfully deleted %zu partitions.", n);
4020 return 1;
4021}
4022
4023static int context_can_factory_reset(Context *context) {
e594a3b1
LP
4024 assert(context);
4025
4026 LIST_FOREACH(partitions, p, context->partitions)
4027 if (p->factory_reset && PARTITION_EXISTS(p))
4028 return true;
4029
4030 return false;
4031}
4032
5c08da58
LP
4033static int resolve_copy_blocks_auto_candidate(
4034 dev_t partition_devno,
4035 sd_id128_t partition_type_uuid,
4036 dev_t restrict_devno,
4037 sd_id128_t *ret_uuid) {
4038
4039 _cleanup_(blkid_free_probep) blkid_probe b = NULL;
5c08da58 4040 _cleanup_close_ int fd = -1;
ca822829
YW
4041 _cleanup_free_ char *p = NULL;
4042 const char *pttype, *t;
5c08da58
LP
4043 sd_id128_t pt_parsed, u;
4044 blkid_partition pp;
4045 dev_t whole_devno;
4046 blkid_partlist pl;
5c08da58
LP
4047 int r;
4048
4049 /* Checks if the specified partition has the specified GPT type UUID, and is located on the specified
4050 * 'restrict_devno' device. The type check is particularly relevant if we have Verity volume which is
4051 * backed by two separate partitions: the data and the hash partitions, and we need to find the right
4052 * one of the two. */
4053
4054 r = block_get_whole_disk(partition_devno, &whole_devno);
4055 if (r < 0)
4056 return log_error_errno(
4057 r,
4058 "Unable to determine containing block device of partition %u:%u: %m",
4059 major(partition_devno), minor(partition_devno));
4060
4061 if (restrict_devno != (dev_t) -1 &&
4062 restrict_devno != whole_devno)
4063 return log_error_errno(
4064 SYNTHETIC_ERRNO(EPERM),
4065 "Partition %u:%u is located outside of block device %u:%u, refusing.",
4066 major(partition_devno), minor(partition_devno),
4067 major(restrict_devno), minor(restrict_devno));
4068
ca822829 4069 fd = r = device_open_from_devnum(S_IFBLK, whole_devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &p);
5c08da58 4070 if (r < 0)
ca822829
YW
4071 return log_error_errno(r, "Failed to open block device " DEVNUM_FORMAT_STR ": %m",
4072 DEVNUM_FORMAT_VAL(whole_devno));
5c08da58
LP
4073
4074 b = blkid_new_probe();
4075 if (!b)
4076 return log_oom();
4077
4078 errno = 0;
4079 r = blkid_probe_set_device(b, fd, 0, 0);
4080 if (r != 0)
4081 return log_error_errno(errno_or_else(ENOMEM), "Failed to open block device '%s': %m", p);
4082
4083 (void) blkid_probe_enable_partitions(b, 1);
4084 (void) blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
4085
4086 errno = 0;
4087 r = blkid_do_safeprobe(b);
4088 if (IN_SET(r, -2, 1)) { /* nothing found or ambiguous result */
4089 log_debug("Didn't find partition table on block device '%s'.", p);
4090 return false;
4091 }
4092 if (r != 0)
4093 return log_error_errno(errno_or_else(EIO), "Unable to probe for partition table of '%s': %m", p);
4094
4095 (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL);
4096 if (!streq_ptr(pttype, "gpt")) {
4097 log_debug("Didn't find a GPT partition table on '%s'.", p);
4098 return false;
4099 }
4100
4101 errno = 0;
4102 pl = blkid_probe_get_partitions(b);
4103 if (!pl)
4104 return log_error_errno(errno_or_else(EIO), "Unable read partition table of '%s': %m", p);
4105 errno = 0;
4106
4107 pp = blkid_partlist_devno_to_partition(pl, partition_devno);
4108 if (!pp) {
4109 log_debug("Partition %u:%u has no matching partition table entry on '%s'.",
4110 major(partition_devno), minor(partition_devno), p);
4111 return false;
4112 }
4113
4114 t = blkid_partition_get_type_string(pp);
4115 if (isempty(t)) {
4116 log_debug("Partition %u:%u has no type on '%s'.",
4117 major(partition_devno), minor(partition_devno), p);
4118 return false;
4119 }
4120
4121 r = sd_id128_from_string(t, &pt_parsed);
4122 if (r < 0) {
4123 log_debug_errno(r, "Failed to parse partition type \"%s\": %m", t);
4124 return false;
4125 }
4126
4127 if (!sd_id128_equal(pt_parsed, partition_type_uuid)) {
4128 log_debug("Partition %u:%u has non-matching partition type " SD_ID128_FORMAT_STR " (needed: " SD_ID128_FORMAT_STR "), ignoring.",
4129 major(partition_devno), minor(partition_devno),
4130 SD_ID128_FORMAT_VAL(pt_parsed), SD_ID128_FORMAT_VAL(partition_type_uuid));
4131 return false;
4132 }
4133
4134 t = blkid_partition_get_uuid(pp);
4135 if (isempty(t)) {
4136 log_debug("Partition %u:%u has no UUID.",
4137 major(partition_devno), minor(partition_devno));
4138 return false;
4139 }
4140
4141 r = sd_id128_from_string(t, &u);
4142 if (r < 0) {
4143 log_debug_errno(r, "Failed to parse partition UUID \"%s\": %m", t);
4144 return false;
4145 }
4146
4147 log_debug("Automatically found partition %u:%u of right type " SD_ID128_FORMAT_STR ".",
4148 major(partition_devno), minor(partition_devno),
4149 SD_ID128_FORMAT_VAL(pt_parsed));
4150
4151 if (ret_uuid)
4152 *ret_uuid = u;
4153
4154 return true;
4155}
4156
4157static int find_backing_devno(
4158 const char *path,
4159 const char *root,
4160 dev_t *ret) {
4161
4162 _cleanup_free_ char *resolved = NULL;
4163 int r;
4164
4165 assert(path);
4166
4167 r = chase_symlinks(path, root, CHASE_PREFIX_ROOT, &resolved, NULL);
4168 if (r < 0)
4169 return r;
4170
4171 r = path_is_mount_point(resolved, NULL, 0);
4172 if (r < 0)
4173 return r;
4174 if (r == 0) /* Not a mount point, then it's not a partition of its own, let's not automatically use it. */
4175 return -ENOENT;
4176
4177 r = get_block_device(resolved, ret);
4178 if (r < 0)
4179 return r;
4180 if (r == 0) /* Not backed by physical file system, we can't use this */
4181 return -ENOENT;
4182
4183 return 0;
4184}
4185
4186static int resolve_copy_blocks_auto(
4187 sd_id128_t type_uuid,
4188 const char *root,
4189 dev_t restrict_devno,
1a037ba2 4190 dev_t *ret_devno,
5c08da58
LP
4191 sd_id128_t *ret_uuid) {
4192
4193 const char *try1 = NULL, *try2 = NULL;
4194 char p[SYS_BLOCK_PATH_MAX("/slaves")];
4195 _cleanup_(closedirp) DIR *d = NULL;
4196 sd_id128_t found_uuid = SD_ID128_NULL;
4197 dev_t devno, found = 0;
4198 int r;
4199
5c08da58
LP
4200 /* Enforce some security restrictions: CopyBlocks=auto should not be an avenue to get outside of the
4201 * --root=/--image= confinement. Specifically, refuse CopyBlocks= in combination with --root= at all,
4202 * and restrict block device references in the --image= case to loopback block device we set up.
4203 *
4204 * restrict_devno contain the dev_t of the loop back device we operate on in case of --image=, and
4205 * thus declares which device (and its partition subdevices) we shall limit access to. If
4206 * restrict_devno is zero no device probing access shall be allowed at all (used for --root=) and if
4207 * it is (dev_t) -1 then free access shall be allowed (if neither switch is used). */
4208
4209 if (restrict_devno == 0)
4210 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
4211 "Automatic discovery of backing block devices not permitted in --root= mode, refusing.");
4212
4213 /* Handles CopyBlocks=auto, and finds the right source partition to copy from. We look for matching
4214 * partitions in the host, using the appropriate directory as key and ensuring that the partition
4215 * type matches. */
4216
4217 if (gpt_partition_type_is_root(type_uuid))
4218 try1 = "/";
4219 else if (gpt_partition_type_is_usr(type_uuid))
4220 try1 = "/usr/";
4221 else if (gpt_partition_type_is_root_verity(type_uuid))
4222 try1 = "/";
4223 else if (gpt_partition_type_is_usr_verity(type_uuid))
4224 try1 = "/usr/";
4225 else if (sd_id128_equal(type_uuid, GPT_ESP)) {
4226 try1 = "/efi/";
4227 try2 = "/boot/";
4228 } else if (sd_id128_equal(type_uuid, GPT_XBOOTLDR))
4229 try1 = "/boot/";
4230 else
4231 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
4232 "Partition type " SD_ID128_FORMAT_STR " not supported from automatic source block device discovery.",
4233 SD_ID128_FORMAT_VAL(type_uuid));
4234
4235 r = find_backing_devno(try1, root, &devno);
4236 if (r == -ENOENT && try2)
4237 r = find_backing_devno(try2, root, &devno);
4238 if (r < 0)
4239 return log_error_errno(r, "Failed to resolve automatic CopyBlocks= path for partition type " SD_ID128_FORMAT_STR ", sorry: %m",
4240 SD_ID128_FORMAT_VAL(type_uuid));
4241
4242 xsprintf_sys_block_path(p, "/slaves", devno);
4243 d = opendir(p);
4244 if (d) {
4245 struct dirent *de;
4246
4247 for (;;) {
4248 _cleanup_free_ char *q = NULL, *t = NULL;
4249 sd_id128_t u;
4250 dev_t sl;
4251
4252 errno = 0;
4253 de = readdir_no_dot(d);
4254 if (!de) {
4255 if (errno != 0)
4256 return log_error_errno(errno, "Failed to read directory '%s': %m", p);
4257
4258 break;
4259 }
4260
4261 if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
4262 continue;
4263
4264 q = path_join(p, de->d_name, "/dev");
4265 if (!q)
4266 return log_oom();
4267
4268 r = read_one_line_file(q, &t);
4269 if (r < 0)
4270 return log_error_errno(r, "Failed to read %s: %m", q);
4271
7176f06c 4272 r = parse_devnum(t, &sl);
5c08da58
LP
4273 if (r < 0) {
4274 log_debug_errno(r, "Failed to parse %s, ignoring: %m", q);
4275 continue;
4276 }
4277 if (major(sl) == 0) {
4278 log_debug_errno(r, "Device backing %s is special, ignoring: %m", q);
4279 continue;
4280 }
4281
4282 r = resolve_copy_blocks_auto_candidate(sl, type_uuid, restrict_devno, &u);
4283 if (r < 0)
4284 return r;
4285 if (r > 0) {
4286 /* We found a matching one! */
4287 if (found != 0)
4288 return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ),
4289 "Multiple matching partitions found, refusing.");
4290
4291 found = sl;
4292 found_uuid = u;
4293 }
4294 }
4295 } else if (errno != ENOENT)
4296 return log_error_errno(errno, "Failed open %s: %m", p);
4297 else {
4298 r = resolve_copy_blocks_auto_candidate(devno, type_uuid, restrict_devno, &found_uuid);
4299 if (r < 0)
4300 return r;
4301 if (r > 0)
4302 found = devno;
4303 }
4304
4305 if (found == 0)
4306 return log_error_errno(SYNTHETIC_ERRNO(ENXIO),
4307 "Unable to automatically discover suitable partition to copy blocks from.");
4308
1a037ba2
YW
4309 if (ret_devno)
4310 *ret_devno = found;
5c08da58
LP
4311
4312 if (ret_uuid)
4313 *ret_uuid = found_uuid;
4314
4315 return 0;
4316}
4317
4318static int context_open_copy_block_paths(
4319 Context *context,
4320 const char *root,
4321 dev_t restrict_devno) {
4322
757bc2e4
LP
4323 int r;
4324
4325 assert(context);
4326
4327 LIST_FOREACH(partitions, p, context->partitions) {
4328 _cleanup_close_ int source_fd = -1;
5c08da58
LP
4329 _cleanup_free_ char *opened = NULL;
4330 sd_id128_t uuid = SD_ID128_NULL;
757bc2e4
LP
4331 uint64_t size;
4332 struct stat st;
4333
4334 assert(p->copy_blocks_fd < 0);
4335 assert(p->copy_blocks_size == UINT64_MAX);
4336
4337 if (PARTITION_EXISTS(p)) /* Never copy over partitions that already exist! */
4338 continue;
4339
5c08da58 4340 if (p->copy_blocks_path) {
757bc2e4 4341
5c08da58
LP
4342 source_fd = chase_symlinks_and_open(p->copy_blocks_path, root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened);
4343 if (source_fd < 0)
4344 return log_error_errno(source_fd, "Failed to open '%s': %m", p->copy_blocks_path);
757bc2e4 4345
5c08da58
LP
4346 if (fstat(source_fd, &st) < 0)
4347 return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened);
4348
4349 if (!S_ISREG(st.st_mode) && restrict_devno != (dev_t) -1)
4350 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
4351 "Copying from block device node is not permitted in --image=/--root= mode, refusing.");
4352
4353 } else if (p->copy_blocks_auto) {
1a037ba2 4354 dev_t devno;
5c08da58 4355
1a037ba2 4356 r = resolve_copy_blocks_auto(p->type_uuid, root, restrict_devno, &devno, &uuid);
5c08da58
LP
4357 if (r < 0)
4358 return r;
4359
ca822829 4360 source_fd = r = device_open_from_devnum(S_IFBLK, devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened);
1a037ba2 4361 if (r < 0)
ca822829
YW
4362 return log_error_errno(r, "Failed to open automatically determined source block copy device " DEVNUM_FORMAT_STR ": %m",
4363 DEVNUM_FORMAT_VAL(devno));
5c08da58
LP
4364
4365 if (fstat(source_fd, &st) < 0)
4366 return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened);
1a037ba2 4367 } else
5c08da58 4368 continue;
757bc2e4
LP
4369
4370 if (S_ISDIR(st.st_mode)) {
ca822829
YW
4371 _cleanup_free_ char *bdev = NULL;
4372 dev_t devt;
757bc2e4
LP
4373
4374 /* If the file is a directory, automatically find the backing block device */
4375
4376 if (major(st.st_dev) != 0)
ca822829 4377 devt = st.st_dev;
757bc2e4 4378 else {
757bc2e4 4379 /* Special support for btrfs */
757bc2e4 4380 r = btrfs_get_block_device_fd(source_fd, &devt);
67f0ac8c 4381 if (r == -EUCLEAN)
5c08da58 4382 return btrfs_log_dev_root(LOG_ERR, r, opened);
757bc2e4 4383 if (r < 0)
5c08da58 4384 return log_error_errno(r, "Unable to determine backing block device of '%s': %m", opened);
757bc2e4 4385 }
757bc2e4
LP
4386
4387 safe_close(source_fd);
4388
ca822829
YW
4389 source_fd = r = device_open_from_devnum(S_IFBLK, devt, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &bdev);
4390 if (r < 0)
4391 return log_error_errno(r, "Failed to open block device backing '%s': %m", opened);
757bc2e4
LP
4392
4393 if (fstat(source_fd, &st) < 0)
4394 return log_error_errno(errno, "Failed to stat block device '%s': %m", bdev);
757bc2e4
LP
4395 }
4396
4397 if (S_ISREG(st.st_mode))
4398 size = st.st_size;
4399 else if (S_ISBLK(st.st_mode)) {
4400 if (ioctl(source_fd, BLKGETSIZE64, &size) != 0)
4401 return log_error_errno(errno, "Failed to determine size of block device to copy from: %m");
4402 } else
5c08da58 4403 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified path to copy blocks from '%s' is not a regular file, block device or directory, refusing: %m", opened);
757bc2e4
LP
4404
4405 if (size <= 0)
5c08da58 4406 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File to copy bytes from '%s' has zero size, refusing.", opened);
757bc2e4 4407 if (size % 512 != 0)
5c08da58 4408 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File to copy bytes from '%s' has size that is not multiple of 512, refusing.", opened);
757bc2e4
LP
4409
4410 p->copy_blocks_fd = TAKE_FD(source_fd);
4411 p->copy_blocks_size = size;
5c08da58
LP
4412
4413 free_and_replace(p->copy_blocks_path, opened);
4414
4415 /* When copying from an existing partition copy that partitions UUID if none is configured explicitly */
11749b61 4416 if (!p->new_uuid_is_set && !sd_id128_is_null(uuid)) {
5c08da58 4417 p->new_uuid = uuid;
11749b61
DDM
4418 p->new_uuid_is_set = true;
4419 }
757bc2e4
LP
4420 }
4421
4422 return 0;
4423}
4424
e594a3b1
LP
4425static int help(void) {
4426 _cleanup_free_ char *link = NULL;
4427 int r;
4428
4429 r = terminal_urlify_man("systemd-repart", "1", &link);
4430 if (r < 0)
4431 return log_oom();
4432
4433 printf("%s [OPTIONS...] [DEVICE]\n"
4434 "\n%sGrow and add partitions to partition table.%s\n\n"
4435 " -h --help Show this help\n"
4436 " --version Show package version\n"
896e678b
LP
4437 " --no-pager Do not pipe output into a pager\n"
4438 " --no-legend Do not show the headers and footers\n"
e594a3b1 4439 " --dry-run=BOOL Whether to run dry-run operation\n"
a26f4a49
LP
4440 " --empty=MODE One of refuse, allow, require, force, create; controls\n"
4441 " how to handle empty disks lacking partition tables\n"
e594a3b1 4442 " --discard=BOOL Whether to discard backing blocks for new partitions\n"
2d2d0a57 4443 " --pretty=BOOL Whether to show pretty summary before doing changes\n"
e594a3b1
LP
4444 " --factory-reset=BOOL Whether to remove data partitions before recreating\n"
4445 " them\n"
4446 " --can-factory-reset Test whether factory reset is defined\n"
4447 " --root=PATH Operate relative to root path\n"
252d6267 4448 " --image=PATH Operate relative to image file\n"
9d252fbb 4449 " --definitions=DIR Find partition definitions in specified directory\n"
b9df3536 4450 " --key-file=PATH Key to use when encrypting partitions\n"
889914ef 4451 " --tpm2-device=PATH Path to TPM2 device node to use\n"
a1788a69 4452 " --tpm2-pcrs=PCR1+PCR2+PCR3+…\n"
889914ef 4453 " TPM2 PCR indexes to use for TPM2 enrollment\n"
e594a3b1 4454 " --seed=UUID 128bit seed UUID to derive all UUIDs from\n"
a26f4a49 4455 " --size=BYTES Grow loopback file to specified size\n"
2d2d0a57 4456 " --json=pretty|short|off\n"
de8231b0 4457 " Generate JSON output\n"
bc556335
DDM
4458 "\nSee the %s for details.\n",
4459 program_invocation_short_name,
4460 ansi_highlight(),
4461 ansi_normal(),
4462 link);
e594a3b1
LP
4463
4464 return 0;
4465}
4466
4467static int parse_argv(int argc, char *argv[]) {
4468
4469 enum {
4470 ARG_VERSION = 0x100,
896e678b
LP
4471 ARG_NO_PAGER,
4472 ARG_NO_LEGEND,
e594a3b1
LP
4473 ARG_DRY_RUN,
4474 ARG_EMPTY,
4475 ARG_DISCARD,
4476 ARG_FACTORY_RESET,
4477 ARG_CAN_FACTORY_RESET,
4478 ARG_ROOT,
252d6267 4479 ARG_IMAGE,
e594a3b1
LP
4480 ARG_SEED,
4481 ARG_PRETTY,
4482 ARG_DEFINITIONS,
a26f4a49 4483 ARG_SIZE,
a015fbe7 4484 ARG_JSON,
b9df3536 4485 ARG_KEY_FILE,
889914ef
LP
4486 ARG_TPM2_DEVICE,
4487 ARG_TPM2_PCRS,
e594a3b1
LP
4488 };
4489
4490 static const struct option options[] = {
4491 { "help", no_argument, NULL, 'h' },
4492 { "version", no_argument, NULL, ARG_VERSION },
896e678b
LP
4493 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4494 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
e594a3b1
LP
4495 { "dry-run", required_argument, NULL, ARG_DRY_RUN },
4496 { "empty", required_argument, NULL, ARG_EMPTY },
4497 { "discard", required_argument, NULL, ARG_DISCARD },
4498 { "factory-reset", required_argument, NULL, ARG_FACTORY_RESET },
4499 { "can-factory-reset", no_argument, NULL, ARG_CAN_FACTORY_RESET },
4500 { "root", required_argument, NULL, ARG_ROOT },
252d6267 4501 { "image", required_argument, NULL, ARG_IMAGE },
e594a3b1
LP
4502 { "seed", required_argument, NULL, ARG_SEED },
4503 { "pretty", required_argument, NULL, ARG_PRETTY },
4504 { "definitions", required_argument, NULL, ARG_DEFINITIONS },
a26f4a49 4505 { "size", required_argument, NULL, ARG_SIZE },
a015fbe7 4506 { "json", required_argument, NULL, ARG_JSON },
b9df3536 4507 { "key-file", required_argument, NULL, ARG_KEY_FILE },
889914ef
LP
4508 { "tpm2-device", required_argument, NULL, ARG_TPM2_DEVICE },
4509 { "tpm2-pcrs", required_argument, NULL, ARG_TPM2_PCRS },
e594a3b1
LP
4510 {}
4511 };
4512
a26f4a49 4513 int c, r, dry_run = -1;
e594a3b1
LP
4514
4515 assert(argc >= 0);
4516 assert(argv);
4517
4518 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
4519
4520 switch (c) {
4521
4522 case 'h':
4523 return help();
4524
4525 case ARG_VERSION:
4526 return version();
4527
896e678b
LP
4528 case ARG_NO_PAGER:
4529 arg_pager_flags |= PAGER_DISABLE;
4530 break;
4531
4532 case ARG_NO_LEGEND:
4533 arg_legend = false;
4534 break;
4535
e594a3b1 4536 case ARG_DRY_RUN:
599c7c54 4537 r = parse_boolean_argument("--dry-run=", optarg, &arg_dry_run);
e594a3b1 4538 if (r < 0)
599c7c54 4539 return r;
e594a3b1
LP
4540 break;
4541
4542 case ARG_EMPTY:
4543 if (isempty(optarg) || streq(optarg, "refuse"))
4544 arg_empty = EMPTY_REFUSE;
4545 else if (streq(optarg, "allow"))
4546 arg_empty = EMPTY_ALLOW;
4547 else if (streq(optarg, "require"))
4548 arg_empty = EMPTY_REQUIRE;
4549 else if (streq(optarg, "force"))
4550 arg_empty = EMPTY_FORCE;
a26f4a49
LP
4551 else if (streq(optarg, "create")) {
4552 arg_empty = EMPTY_CREATE;
4553
4554 if (dry_run < 0)
4555 dry_run = false; /* Imply --dry-run=no if we create the loopback file
4556 * anew. After all we cannot really break anyone's
4557 * partition tables that way. */
4558 } else
e594a3b1
LP
4559 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
4560 "Failed to parse --empty= parameter: %s", optarg);
4561 break;
4562
4563 case ARG_DISCARD:
599c7c54 4564 r = parse_boolean_argument("--discard=", optarg, &arg_discard);
e594a3b1 4565 if (r < 0)
599c7c54 4566 return r;
e594a3b1
LP
4567 break;
4568
4569 case ARG_FACTORY_RESET:
c3470872 4570 r = parse_boolean_argument("--factory-reset=", optarg, NULL);
e594a3b1 4571 if (r < 0)
c3470872 4572 return r;
e594a3b1
LP
4573 arg_factory_reset = r;
4574 break;
4575
4576 case ARG_CAN_FACTORY_RESET:
4577 arg_can_factory_reset = true;
4578 break;
4579
4580 case ARG_ROOT:
252d6267
LP
4581 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root);
4582 if (r < 0)
4583 return r;
4584 break;
4585
4586 case ARG_IMAGE:
4587 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
e594a3b1
LP
4588 if (r < 0)
4589 return r;
4590 break;
4591
4592 case ARG_SEED:
4593 if (isempty(optarg)) {
4594 arg_seed = SD_ID128_NULL;
4595 arg_randomize = false;
4596 } else if (streq(optarg, "random"))
4597 arg_randomize = true;
4598 else {
4599 r = sd_id128_from_string(optarg, &arg_seed);
4600 if (r < 0)
4601 return log_error_errno(r, "Failed to parse seed: %s", optarg);
4602
4603 arg_randomize = false;
4604 }
4605
4606 break;
4607
4608 case ARG_PRETTY:
c3470872 4609 r = parse_boolean_argument("--pretty=", optarg, NULL);
e594a3b1 4610 if (r < 0)
c3470872 4611 return r;
e594a3b1
LP
4612 arg_pretty = r;
4613 break;
4614
224c853f
RP
4615 case ARG_DEFINITIONS: {
4616 _cleanup_free_ char *path = NULL;
4617 r = parse_path_argument(optarg, false, &path);
e594a3b1
LP
4618 if (r < 0)
4619 return r;
224c853f
RP
4620 if (strv_consume(&arg_definitions, TAKE_PTR(path)) < 0)
4621 return log_oom();
e594a3b1 4622 break;
224c853f 4623 }
e594a3b1 4624
a26f4a49
LP
4625 case ARG_SIZE: {
4626 uint64_t parsed, rounded;
4627
170c9823
LP
4628 if (streq(optarg, "auto")) {
4629 arg_size = UINT64_MAX;
4630 arg_size_auto = true;
4631 break;
4632 }
4633
a26f4a49
LP
4634 r = parse_size(optarg, 1024, &parsed);
4635 if (r < 0)
4636 return log_error_errno(r, "Failed to parse --size= parameter: %s", optarg);
4637
4638 rounded = round_up_size(parsed, 4096);
4639 if (rounded == 0)
4640 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too small, refusing.");
4641 if (rounded == UINT64_MAX)
4642 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too large, refusing.");
4643
4644 if (rounded != parsed)
e2341b6b
DT
4645 log_warning("Specified size is not a multiple of 4096, rounding up automatically. (%" PRIu64 " %s %" PRIu64 ")",
4646 parsed, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), rounded);
a26f4a49
LP
4647
4648 arg_size = rounded;
170c9823 4649 arg_size_auto = false;
a26f4a49
LP
4650 break;
4651 }
b9df3536 4652
a015fbe7 4653 case ARG_JSON:
b1e8f46c 4654 r = parse_json_argument(optarg, &arg_json_format_flags);
6a01ea4a
LP
4655 if (r <= 0)
4656 return r;
a015fbe7
TH
4657
4658 break;
4659
b9df3536
LP
4660 case ARG_KEY_FILE: {
4661 _cleanup_(erase_and_freep) char *k = NULL;
4662 size_t n = 0;
4663
8b3c3a49 4664 r = read_full_file_full(
986311c2 4665 AT_FDCWD, optarg, UINT64_MAX, SIZE_MAX,
8b3c3a49
LP
4666 READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
4667 NULL,
4668 &k, &n);
b9df3536
LP
4669 if (r < 0)
4670 return log_error_errno(r, "Failed to read key file '%s': %m", optarg);
4671
4672 erase_and_free(arg_key);
4673 arg_key = TAKE_PTR(k);
4674 arg_key_size = n;
4675 break;
4676 }
a26f4a49 4677
889914ef
LP
4678 case ARG_TPM2_DEVICE: {
4679 _cleanup_free_ char *device = NULL;
4680
4681 if (streq(optarg, "list"))
4682 return tpm2_list_devices();
4683
4684 if (!streq(optarg, "auto")) {
4685 device = strdup(optarg);
4686 if (!device)
4687 return log_oom();
4688 }
4689
4690 free(arg_tpm2_device);
4691 arg_tpm2_device = TAKE_PTR(device);
4692 break;
4693 }
4694
222a951f
LP
4695 case ARG_TPM2_PCRS:
4696 r = tpm2_parse_pcr_argument(optarg, &arg_tpm2_pcr_mask);
889914ef
LP
4697 if (r < 0)
4698 return r;
4699
889914ef 4700 break;
889914ef 4701
e594a3b1
LP
4702 case '?':
4703 return -EINVAL;
4704
4705 default:
04499a70 4706 assert_not_reached();
e594a3b1
LP
4707 }
4708
4709 if (argc - optind > 1)
4710 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
4711 "Expected at most one argument, the path to the block device.");
4712
a26f4a49 4713 if (arg_factory_reset > 0 && IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE))
e594a3b1 4714 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
a26f4a49 4715 "Combination of --factory-reset=yes and --empty=force/--empty=require/--empty=create is invalid.");
e594a3b1
LP
4716
4717 if (arg_can_factory_reset)
a26f4a49
LP
4718 arg_dry_run = true; /* When --can-factory-reset is specified we don't make changes, hence
4719 * non-dry-run mode makes no sense. Thus, imply dry run mode so that we
4720 * open things strictly read-only. */
4721 else if (dry_run >= 0)
4722 arg_dry_run = dry_run;
4723
170c9823 4724 if (arg_empty == EMPTY_CREATE && (arg_size == UINT64_MAX && !arg_size_auto))
a26f4a49
LP
4725 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
4726 "If --empty=create is specified, --size= must be specified, too.");
e594a3b1 4727
252d6267
LP
4728 if (arg_image && arg_root)
4729 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
4730 else if (!arg_image && !arg_root && in_initrd()) {
8f47e32a
LP
4731
4732 /* By default operate on /sysusr/ or /sysroot/ when invoked in the initrd. We prefer the
4733 * former, if it is mounted, so that we have deterministic behaviour on systems where /usr/
4734 * is vendor-supplied but the root fs formatted on first boot. */
4735 r = path_is_mount_point("/sysusr/usr", NULL, 0);
4736 if (r <= 0) {
4737 if (r < 0 && r != -ENOENT)
4738 log_debug_errno(r, "Unable to determine whether /sysusr/usr is a mount point, assuming it is not: %m");
4739
4740 arg_root = strdup("/sysroot");
4741 } else
4742 arg_root = strdup("/sysusr");
252d6267
LP
4743 if (!arg_root)
4744 return log_oom();
4745 }
4746
e594a3b1 4747 arg_node = argc > optind ? argv[optind] : NULL;
a26f4a49 4748
252d6267 4749 if (IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE) && !arg_node && !arg_image)
a26f4a49
LP
4750 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
4751 "A path to a device node or loopback file must be specified when --empty=force, --empty=require or --empty=create are used.");
4752
889914ef
LP
4753 if (arg_tpm2_pcr_mask == UINT32_MAX)
4754 arg_tpm2_pcr_mask = TPM2_PCR_MASK_DEFAULT;
4755
a26d463d
DDM
4756 if (arg_pretty < 0 && isatty(STDOUT_FILENO))
4757 arg_pretty = true;
4758
e594a3b1
LP
4759 return 1;
4760}
4761
4762static int parse_proc_cmdline_factory_reset(void) {
4763 bool b;
4764 int r;
4765
4766 if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */
4767 return 0;
4768
4769 if (!in_initrd()) /* Never honour kernel command line factory reset request outside of the initrd */
4770 return 0;
4771
4772 r = proc_cmdline_get_bool("systemd.factory_reset", &b);
4773 if (r < 0)
4774 return log_error_errno(r, "Failed to parse systemd.factory_reset kernel command line argument: %m");
4775 if (r > 0) {
4776 arg_factory_reset = b;
4777
4778 if (b)
4779 log_notice("Honouring factory reset requested via kernel command line.");
4780 }
4781
4782 return 0;
4783}
4784
4785static int parse_efi_variable_factory_reset(void) {
4786 _cleanup_free_ char *value = NULL;
4787 int r;
4788
4789 if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */
4790 return 0;
4791
4792 if (!in_initrd()) /* Never honour EFI variable factory reset request outside of the initrd */
4793 return 0;
4794
e6f055cb 4795 r = efi_get_variable_string(EFI_SYSTEMD_VARIABLE(FactoryReset), &value);
e594a3b1
LP
4796 if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
4797 return 0;
4798 if (r < 0)
4799 return log_error_errno(r, "Failed to read EFI variable FactoryReset: %m");
4800
4801 r = parse_boolean(value);
4802 if (r < 0)
4803 return log_error_errno(r, "Failed to parse EFI variable FactoryReset: %m");
4804
4805 arg_factory_reset = r;
4806 if (r)
111a3aae 4807 log_notice("Factory reset requested via EFI variable FactoryReset.");
e594a3b1
LP
4808
4809 return 0;
4810}
4811
4812static int remove_efi_variable_factory_reset(void) {
4813 int r;
4814
e6f055cb 4815 r = efi_set_variable(EFI_SYSTEMD_VARIABLE(FactoryReset), NULL, 0);
e594a3b1
LP
4816 if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
4817 return 0;
4818 if (r < 0)
4819 return log_error_errno(r, "Failed to remove EFI variable FactoryReset: %m");
4820
4821 log_info("Successfully unset EFI variable FactoryReset.");
4822 return 0;
4823}
4824
252d6267
LP
4825static int acquire_root_devno(
4826 const char *p,
4827 const char *root,
4828 int mode,
4829 char **ret,
4830 int *ret_fd) {
4831
4832 _cleanup_free_ char *found_path = NULL;
4833 dev_t devno, fd_devno = MODE_INVALID;
e594a3b1
LP
4834 _cleanup_close_ int fd = -1;
4835 struct stat st;
e594a3b1
LP
4836 int r;
4837
a26f4a49
LP
4838 assert(p);
4839 assert(ret);
4840 assert(ret_fd);
4841
252d6267 4842 fd = chase_symlinks_and_open(p, root, CHASE_PREFIX_ROOT, mode, &found_path);
e594a3b1 4843 if (fd < 0)
252d6267 4844 return fd;
e594a3b1
LP
4845
4846 if (fstat(fd, &st) < 0)
4847 return -errno;
4848
4849 if (S_ISREG(st.st_mode)) {
252d6267 4850 *ret = TAKE_PTR(found_path);
a26f4a49 4851 *ret_fd = TAKE_FD(fd);
e594a3b1
LP
4852 return 0;
4853 }
4854
252d6267
LP
4855 if (S_ISBLK(st.st_mode)) {
4856 /* Refuse referencing explicit block devices if a root dir is specified, after all we should
5c08da58 4857 * not be able to leave the image the root path constrains us to. */
252d6267
LP
4858 if (root)
4859 return -EPERM;
4860
a26f4a49 4861 fd_devno = devno = st.st_rdev;
252d6267 4862 } else if (S_ISDIR(st.st_mode)) {
e594a3b1
LP
4863
4864 devno = st.st_dev;
a26f4a49 4865 if (major(devno) == 0) {
e594a3b1
LP
4866 r = btrfs_get_block_device_fd(fd, &devno);
4867 if (r == -ENOTTY) /* not btrfs */
4868 return -ENODEV;
4869 if (r < 0)
4870 return r;
4871 }
e594a3b1
LP
4872 } else
4873 return -ENOTBLK;
4874
4875 /* From dm-crypt to backing partition */
4876 r = block_get_originating(devno, &devno);
8e5f3cec
LP
4877 if (r == -ENOENT)
4878 log_debug_errno(r, "Device '%s' has no dm-crypt/dm-verity device, no need to look for underlying block device.", p);
4879 else if (r < 0)
e594a3b1
LP
4880 log_debug_errno(r, "Failed to find underlying block device for '%s', ignoring: %m", p);
4881
4882 /* From partition to whole disk containing it */
4883 r = block_get_whole_disk(devno, &devno);
4884 if (r < 0)
162392b7 4885 log_debug_errno(r, "Failed to find whole disk block device for '%s', ignoring: %m", p);
e594a3b1 4886
4fe46c34 4887 r = devname_from_devnum(S_IFBLK, devno, ret);
a26f4a49
LP
4888 if (r < 0)
4889 return log_debug_errno(r, "Failed to determine canonical path for '%s': %m", p);
4890
6bbae9f8 4891 /* Only if we still look at the same block device we can reuse the fd. Otherwise return an
a26f4a49 4892 * invalidated fd. */
f5fbe71d 4893 *ret_fd = fd_devno != MODE_INVALID && fd_devno == devno ? TAKE_FD(fd) : -1;
a26f4a49 4894 return 0;
e594a3b1
LP
4895}
4896
a26f4a49 4897static int find_root(char **ret, int *ret_fd) {
54632d2e 4898 _cleanup_free_ char *device = NULL;
5980d463 4899 int r;
e594a3b1 4900
a26f4a49
LP
4901 assert(ret);
4902 assert(ret_fd);
4903
e594a3b1 4904 if (arg_node) {
a26f4a49
LP
4905 if (arg_empty == EMPTY_CREATE) {
4906 _cleanup_close_ int fd = -1;
4907 _cleanup_free_ char *s = NULL;
4908
4909 s = strdup(arg_node);
4910 if (!s)
4911 return log_oom();
4912
5332d7c6 4913 fd = open(arg_node, O_RDONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOFOLLOW, 0666);
a26f4a49
LP
4914 if (fd < 0)
4915 return log_error_errno(errno, "Failed to create '%s': %m", arg_node);
4916
4917 *ret = TAKE_PTR(s);
4918 *ret_fd = TAKE_FD(fd);
4919 return 0;
4920 }
4921
252d6267
LP
4922 /* Note that we don't specify a root argument here: if the user explicitly configured a node
4923 * we'll take it relative to the host, not the image */
4924 r = acquire_root_devno(arg_node, NULL, O_RDONLY|O_CLOEXEC, ret, ret_fd);
67f0ac8c
LP
4925 if (r == -EUCLEAN)
4926 return btrfs_log_dev_root(LOG_ERR, r, arg_node);
e594a3b1 4927 if (r < 0)
aa2a74ad 4928 return log_error_errno(r, "Failed to open file or determine backing device of %s: %m", arg_node);
e594a3b1
LP
4929
4930 return 0;
4931 }
4932
a26f4a49
LP
4933 assert(IN_SET(arg_empty, EMPTY_REFUSE, EMPTY_ALLOW));
4934
54632d2e
KK
4935 /* If the root mount has been replaced by some form of volatile file system (overlayfs), the
4936 * original root block device node is symlinked in /run/systemd/volatile-root. Let's read that
4937 * here. */
4938 r = readlink_malloc("/run/systemd/volatile-root", &device);
4939 if (r == -ENOENT) { /* volatile-root not found */
4940 /* Let's search for the root device. We look for two cases here: first in /, and then in /usr. The
4941 * latter we check for cases where / is a tmpfs and only /usr is an actual persistent block device
4942 * (think: volatile setups) */
e594a3b1 4943
54632d2e 4944 FOREACH_STRING(p, "/", "/usr") {
e594a3b1 4945
54632d2e
KK
4946 r = acquire_root_devno(p, arg_root, O_RDONLY|O_DIRECTORY|O_CLOEXEC, ret, ret_fd);
4947 if (r < 0) {
4948 if (r == -EUCLEAN)
4949 return btrfs_log_dev_root(LOG_ERR, r, p);
4950 if (r != -ENODEV)
4951 return log_error_errno(r, "Failed to determine backing device of %s: %m", p);
4952 } else
4953 return 0;
4954 }
4955 } else if (r < 0)
4956 return log_error_errno(r, "Failed to read symlink /run/systemd/volatile-root: %m");
4957 else {
4958 r = acquire_root_devno(device, NULL, O_RDONLY|O_CLOEXEC, ret, ret_fd);
4959 if (r == -EUCLEAN)
4960 return btrfs_log_dev_root(LOG_ERR, r, device);
4961 if (r < 0)
4962 return log_error_errno(r, "Failed to open file or determine backing device of %s: %m", device);
4963
4964 return 0;
e594a3b1
LP
4965 }
4966
4967 return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "Failed to discover root block device.");
4968}
4969
f9b3afae 4970static int resize_pt(int fd) {
f9b3afae
LP
4971 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
4972 int r;
4973
4974 /* After resizing the backing file we need to resize the partition table itself too, so that it takes
4975 * possession of the enlarged backing file. For this it suffices to open the device with libfdisk and
4976 * immediately write it again, with no changes. */
4977
4978 c = fdisk_new_context();
4979 if (!c)
4980 return log_oom();
4981
ddb6eeaf 4982 r = fdisk_assign_device(c, FORMAT_PROC_FD_PATH(fd), 0);
f9b3afae 4983 if (r < 0)
ddb6eeaf 4984 return log_error_errno(r, "Failed to open device '%s': %m", FORMAT_PROC_FD_PATH(fd));
f9b3afae
LP
4985
4986 r = fdisk_has_label(c);
4987 if (r < 0)
ddb6eeaf 4988 return log_error_errno(r, "Failed to determine whether disk '%s' has a disk label: %m", FORMAT_PROC_FD_PATH(fd));
f9b3afae
LP
4989 if (r == 0) {
4990 log_debug("Not resizing partition table, as there currently is none.");
4991 return 0;
4992 }
4993
4994 r = fdisk_write_disklabel(c);
4995 if (r < 0)
4996 return log_error_errno(r, "Failed to write resized partition table: %m");
4997
4998 log_info("Resized partition table.");
4999 return 1;
5000}
5001
252d6267
LP
5002static int resize_backing_fd(
5003 const char *node, /* The primary way we access the disk image to operate on */
5004 int *fd, /* An O_RDONLY fd referring to that inode */
5005 const char *backing_file, /* If the above refers to a loopback device, the backing regular file for that, which we can grow */
5006 LoopDevice *loop_device) {
5007
a26f4a49 5008 _cleanup_close_ int writable_fd = -1;
252d6267 5009 uint64_t current_size;
a26f4a49
LP
5010 struct stat st;
5011 int r;
5012
5013 assert(node);
5014 assert(fd);
5015
5016 if (arg_size == UINT64_MAX) /* Nothing to do */
5017 return 0;
5018
5019 if (*fd < 0) {
5020 /* Open the file if we haven't opened it yet. Note that we open it read-only here, just to
5021 * keep a reference to the file we can pass around. */
5022 *fd = open(node, O_RDONLY|O_CLOEXEC);
5023 if (*fd < 0)
5024 return log_error_errno(errno, "Failed to open '%s' in order to adjust size: %m", node);
5025 }
5026
5027 if (fstat(*fd, &st) < 0)
5028 return log_error_errno(errno, "Failed to stat '%s': %m", node);
5029
252d6267
LP
5030 if (S_ISBLK(st.st_mode)) {
5031 if (!backing_file)
5032 return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Cannot resize block device '%s'.", node);
5033
5034 assert(loop_device);
a26f4a49 5035
252d6267
LP
5036 if (ioctl(*fd, BLKGETSIZE64, &current_size) < 0)
5037 return log_error_errno(errno, "Failed to determine size of block device %s: %m", node);
5038 } else {
5039 r = stat_verify_regular(&st);
5040 if (r < 0)
5041 return log_error_errno(r, "Specified path '%s' is not a regular file or loopback block device, cannot resize: %m", node);
5042
5043 assert(!backing_file);
5044 assert(!loop_device);
5045 current_size = st.st_size;
5046 }
5047
252d6267 5048 if (current_size >= arg_size) {
2b59bf51
ZJS
5049 log_info("File '%s' already is of requested size or larger, not growing. (%s >= %s)",
5050 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49
LP
5051 return 0;
5052 }
5053
252d6267
LP
5054 if (S_ISBLK(st.st_mode)) {
5055 assert(backing_file);
5056
5057 /* This is a loopback device. We can't really grow those directly, but we can grow the
5058 * backing file, hence let's do that. */
5059
5060 writable_fd = open(backing_file, O_WRONLY|O_CLOEXEC|O_NONBLOCK);
5061 if (writable_fd < 0)
5062 return log_error_errno(errno, "Failed to open backing file '%s': %m", backing_file);
5063
5064 if (fstat(writable_fd, &st) < 0)
5065 return log_error_errno(errno, "Failed to stat() backing file '%s': %m", backing_file);
5066
5067 r = stat_verify_regular(&st);
5068 if (r < 0)
5069 return log_error_errno(r, "Backing file '%s' of block device is not a regular file: %m", backing_file);
5070
5071 if ((uint64_t) st.st_size != current_size)
5072 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2b59bf51
ZJS
5073 "Size of backing file '%s' of loopback block device '%s' don't match, refusing.",
5074 node, backing_file);
252d6267
LP
5075 } else {
5076 assert(S_ISREG(st.st_mode));
5077 assert(!backing_file);
a26f4a49 5078
252d6267
LP
5079 /* The file descriptor is read-only. In order to grow the file we need to have a writable fd. We
5080 * reopen the file for that temporarily. We keep the writable fd only open for this operation though,
5081 * as fdisk can't accept it anyway. */
5082
5083 writable_fd = fd_reopen(*fd, O_WRONLY|O_CLOEXEC);
5084 if (writable_fd < 0)
5085 return log_error_errno(writable_fd, "Failed to reopen backing file '%s' writable: %m", node);
5086 }
a26f4a49
LP
5087
5088 if (!arg_discard) {
5089 if (fallocate(writable_fd, 0, 0, arg_size) < 0) {
5090 if (!ERRNO_IS_NOT_SUPPORTED(errno))
5091 return log_error_errno(errno, "Failed to grow '%s' from %s to %s by allocation: %m",
2b59bf51 5092 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49
LP
5093
5094 /* Fallback to truncation, if fallocate() is not supported. */
5095 log_debug("Backing file system does not support fallocate(), falling back to ftruncate().");
5096 } else {
252d6267 5097 if (current_size == 0) /* Likely regular file just created by us */
2b59bf51 5098 log_info("Allocated %s for '%s'.", FORMAT_BYTES(arg_size), node);
a26f4a49 5099 else
2b59bf51
ZJS
5100 log_info("File '%s' grown from %s to %s by allocation.",
5101 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49 5102
252d6267 5103 goto done;
a26f4a49
LP
5104 }
5105 }
5106
5107 if (ftruncate(writable_fd, arg_size) < 0)
5108 return log_error_errno(errno, "Failed to grow '%s' from %s to %s by truncation: %m",
2b59bf51 5109 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49 5110
252d6267 5111 if (current_size == 0) /* Likely regular file just created by us */
2b59bf51 5112 log_info("Sized '%s' to %s.", node, FORMAT_BYTES(arg_size));
252d6267 5113 else
2b59bf51
ZJS
5114 log_info("File '%s' grown from %s to %s by truncation.",
5115 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
252d6267
LP
5116
5117done:
f9b3afae
LP
5118 r = resize_pt(writable_fd);
5119 if (r < 0)
5120 return r;
5121
252d6267
LP
5122 if (loop_device) {
5123 r = loop_device_refresh_size(loop_device, UINT64_MAX, arg_size);
5124 if (r < 0)
5125 return log_error_errno(r, "Failed to update loop device size: %m");
5126 }
a26f4a49
LP
5127
5128 return 1;
5129}
5130
170c9823 5131static int determine_auto_size(Context *c) {
994b3031 5132 uint64_t sum;
170c9823 5133
ac33e147 5134 assert(c);
170c9823 5135
994b3031
LP
5136 sum = round_up_size(GPT_METADATA_SIZE, 4096);
5137
170c9823
LP
5138 LIST_FOREACH(partitions, p, c->partitions) {
5139 uint64_t m;
5140
5141 if (p->dropped)
5142 continue;
5143
994b3031 5144 m = partition_min_size_with_padding(c, p);
170c9823
LP
5145 if (m > UINT64_MAX - sum)
5146 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Image would grow too large, refusing.");
5147
5148 sum += m;
5149 }
5150
2b59bf51
ZJS
5151 if (c->total != UINT64_MAX)
5152 /* Image already allocated? Then show its size. */
5153 log_info("Automatically determined minimal disk image size as %s, current image size is %s.",
5154 FORMAT_BYTES(sum), FORMAT_BYTES(c->total));
5155 else
5156 /* If the image is being created right now, then it has no previous size, suppress any comment about it hence. */
5157 log_info("Automatically determined minimal disk image size as %s.",
5158 FORMAT_BYTES(sum));
170c9823
LP
5159
5160 arg_size = sum;
5161 return 0;
5162}
5163
e594a3b1 5164static int run(int argc, char *argv[]) {
252d6267
LP
5165 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
5166 _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL;
5167 _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
e594a3b1
LP
5168 _cleanup_(context_freep) Context* context = NULL;
5169 _cleanup_free_ char *node = NULL;
a26f4a49 5170 _cleanup_close_ int backing_fd = -1;
252d6267 5171 bool from_scratch, node_is_our_loop = false;
e594a3b1
LP
5172 int r;
5173
5174 log_show_color(true);
5175 log_parse_environment();
5176 log_open();
5177
e594a3b1
LP
5178 r = parse_argv(argc, argv);
5179 if (r <= 0)
5180 return r;
5181
5182 r = parse_proc_cmdline_factory_reset();
5183 if (r < 0)
5184 return r;
5185
5186 r = parse_efi_variable_factory_reset();
5187 if (r < 0)
5188 return r;
5189
30f19400
LP
5190#if HAVE_LIBCRYPTSETUP
5191 cryptsetup_enable_logging(NULL);
5192#endif
5193
252d6267
LP
5194 if (arg_image) {
5195 assert(!arg_root);
5196
5197 /* Mount this strictly read-only: we shall modify the partition table, not the file
5198 * systems */
5199 r = mount_image_privately_interactively(
5200 arg_image,
5201 DISSECT_IMAGE_MOUNT_READ_ONLY |
5202 (arg_node ? DISSECT_IMAGE_DEVICE_READ_ONLY : 0) | /* If a different node to make changes to is specified let's open the device in read-only mode) */
5203 DISSECT_IMAGE_GPT_ONLY |
5204 DISSECT_IMAGE_RELAX_VAR_CHECK |
5205 DISSECT_IMAGE_USR_NO_ROOT |
5206 DISSECT_IMAGE_REQUIRE_ROOT,
5207 &mounted_dir,
5208 &loop_device,
5209 &decrypted_image);
5210 if (r < 0)
5211 return r;
5212
5213 arg_root = strdup(mounted_dir);
5214 if (!arg_root)
5215 return log_oom();
5216
5217 if (!arg_node) {
5218 arg_node = strdup(loop_device->node);
5219 if (!arg_node)
5220 return log_oom();
5221
3d62af7d 5222 /* Remember that the device we are about to manipulate is actually the one we
252d6267
LP
5223 * allocated here, and thus to increase its backing file we know what to do */
5224 node_is_our_loop = true;
5225 }
5226 }
5227
e594a3b1
LP
5228 context = context_new(arg_seed);
5229 if (!context)
5230 return log_oom();
5231
224c853f
RP
5232 strv_uniq(arg_definitions);
5233
e594a3b1
LP
5234 r = context_read_definitions(context, arg_definitions, arg_root);
5235 if (r < 0)
5236 return r;
5237
a26f4a49 5238 if (context->n_partitions <= 0 && arg_empty == EMPTY_REFUSE) {
e2d65cd2 5239 log_info("Didn't find any partition definition files, nothing to do.");
0ae5ffe0 5240 return 0;
e2d65cd2 5241 }
0ae5ffe0 5242
a26f4a49 5243 r = find_root(&node, &backing_fd);
0ae5ffe0
YW
5244 if (r < 0)
5245 return r;
5246
a26f4a49 5247 if (arg_size != UINT64_MAX) {
252d6267
LP
5248 r = resize_backing_fd(
5249 node,
5250 &backing_fd,
5251 node_is_our_loop ? arg_image : NULL,
5252 node_is_our_loop ? loop_device : NULL);
a26f4a49
LP
5253 if (r < 0)
5254 return r;
5255 }
5256
5257 r = context_load_partition_table(context, node, &backing_fd);
e594a3b1
LP
5258 if (r == -EHWPOISON)
5259 return 77; /* Special return value which means "Not GPT, so not doing anything". This isn't
5260 * really an error when called at boot. */
5261 if (r < 0)
5262 return r;
5263 from_scratch = r > 0; /* Starting from scratch */
5264
5265 if (arg_can_factory_reset) {
5266 r = context_can_factory_reset(context);
5267 if (r < 0)
5268 return r;
5269 if (r == 0)
5270 return EXIT_FAILURE;
5271
5272 return 0;
5273 }
5274
5275 r = context_factory_reset(context, from_scratch);
5276 if (r < 0)
5277 return r;
5278 if (r > 0) {
5279 /* We actually did a factory reset! */
5280 r = remove_efi_variable_factory_reset();
5281 if (r < 0)
5282 return r;
5283
5284 /* Reload the reduced partition table */
5285 context_unload_partition_table(context);
a26f4a49 5286 r = context_load_partition_table(context, node, &backing_fd);
e594a3b1
LP
5287 if (r < 0)
5288 return r;
5289 }
5290
5291#if 0
5292 (void) context_dump_partitions(context, node);
5293 putchar('\n');
5294#endif
5295
5296 r = context_read_seed(context, arg_root);
5297 if (r < 0)
5298 return r;
5299
757bc2e4 5300 /* Open all files to copy blocks from now, since we want to take their size into consideration */
5c08da58
LP
5301 r = context_open_copy_block_paths(
5302 context,
5303 arg_root,
7802194a 5304 loop_device ? loop_device->devno : /* if --image= is specified, only allow partitions on the loopback device */
5c08da58
LP
5305 arg_root && !arg_image ? 0 : /* if --root= is specified, don't accept any block device */
5306 (dev_t) -1); /* if neither is specified, make no restrictions */
757bc2e4
LP
5307 if (r < 0)
5308 return r;
5309
170c9823
LP
5310 if (arg_size_auto) {
5311 r = determine_auto_size(context);
5312 if (r < 0)
5313 return r;
5314
5315 /* Flush out everything again, and let's grow the file first, then start fresh */
5316 context_unload_partition_table(context);
5317
ac33e147 5318 assert(arg_size != UINT64_MAX);
252d6267
LP
5319 r = resize_backing_fd(
5320 node,
5321 &backing_fd,
5322 node_is_our_loop ? arg_image : NULL,
5323 node_is_our_loop ? loop_device : NULL);
170c9823
LP
5324 if (r < 0)
5325 return r;
5326
5327 r = context_load_partition_table(context, node, &backing_fd);
5328 if (r < 0)
5329 return r;
5330 }
5331
e594a3b1
LP
5332 /* First try to fit new partitions in, dropping by priority until it fits */
5333 for (;;) {
14a4c4ed
LP
5334 uint64_t largest_free_area;
5335
5336 if (context_allocate_partitions(context, &largest_free_area))
e594a3b1
LP
5337 break; /* Success! */
5338
d17db7b2
LP
5339 if (!context_drop_one_priority(context)) {
5340 r = log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
14a4c4ed 5341 "Can't fit requested partitions into available free space (%s), refusing.",
2b59bf51 5342 FORMAT_BYTES(largest_free_area));
d17db7b2
LP
5343 determine_auto_size(context);
5344 return r;
5345 }
e594a3b1
LP
5346 }
5347
5348 /* Now assign free space according to the weight logic */
5349 r = context_grow_partitions(context);
5350 if (r < 0)
5351 return r;
5352
0b7f574f 5353 /* Now calculate where each new partition gets placed */
e594a3b1
LP
5354 context_place_partitions(context);
5355
5356 /* Make sure each partition has a unique UUID and unique label */
5357 r = context_acquire_partition_uuids_and_labels(context);
5358 if (r < 0)
5359 return r;
5360
b5b7879a 5361 (void) context_dump(context, node, /*late=*/ false);
a26d463d 5362
e594a3b1
LP
5363 r = context_write_partition_table(context, node, from_scratch);
5364 if (r < 0)
5365 return r;
5366
b5b7879a
DDM
5367 (void) context_dump(context, node, /*late=*/ true);
5368
e594a3b1
LP
5369 return 0;
5370}
5371
5372DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);