]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/partition/repart.c
repart: slightly improve error message if partition is not on dm-crypt/dm-verity
[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>
9#include <libfdisk.h>
10#include <linux/fs.h>
11#include <linux/loop.h>
12#include <sys/file.h>
13#include <sys/ioctl.h>
14#include <sys/stat.h>
15
16#include <openssl/hmac.h>
17#include <openssl/sha.h>
18
19#include "sd-id128.h"
20
21#include "alloc-util.h"
22#include "blkid-util.h"
23#include "blockdev-util.h"
24#include "btrfs-util.h"
25#include "conf-files.h"
26#include "conf-parser.h"
1e2f3230 27#include "cryptsetup-util.h"
e594a3b1
LP
28#include "def.h"
29#include "efivars.h"
30#include "errno-util.h"
31#include "fd-util.h"
b9df3536 32#include "fileio.h"
e594a3b1
LP
33#include "format-table.h"
34#include "format-util.h"
35#include "fs-util.h"
36#include "gpt.h"
889914ef 37#include "hexdecoct.h"
e594a3b1 38#include "id128-util.h"
a015fbe7 39#include "json.h"
e594a3b1
LP
40#include "list.h"
41#include "locale-util.h"
53171c04 42#include "loop-util.h"
e594a3b1 43#include "main-func.h"
8a794850 44#include "mkdir.h"
53171c04 45#include "mkfs-util.h"
8a794850 46#include "mount-util.h"
614b022c 47#include "parse-argument.h"
599c7c54 48#include "parse-util.h"
e594a3b1
LP
49#include "path-util.h"
50#include "pretty-print.h"
51#include "proc-cmdline.h"
8a794850 52#include "process-util.h"
b9df3536 53#include "random-util.h"
170c9823 54#include "resize-fs.h"
e594a3b1 55#include "sort-util.h"
e031166e 56#include "specifier.h"
e594a3b1
LP
57#include "stat-util.h"
58#include "stdio-util.h"
889914ef 59#include "string-table.h"
e594a3b1
LP
60#include "string-util.h"
61#include "strv.h"
62#include "terminal-util.h"
889914ef 63#include "tpm2-util.h"
8a794850 64#include "user-util.h"
e594a3b1
LP
65#include "utf8.h"
66
fb08381c
LP
67/* If not configured otherwise use a minimal partition size of 10M */
68#define DEFAULT_MIN_SIZE (10*1024*1024)
69
70/* Hard lower limit for new partition sizes */
71#define HARD_MIN_SIZE 4096
72
69e3234d 73/* libfdisk takes off slightly more than 1M of the disk size when creating a GPT disk label */
170c9823
LP
74#define GPT_METADATA_SIZE (1044*1024)
75
76/* LUKS2 takes off 16M of the partition size with its metadata by default */
77#define LUKS2_METADATA_SIZE (16*1024*1024)
78
e594a3b1
LP
79/* Note: When growing and placing new partitions we always align to 4K sector size. It's how newer hard disks
80 * are designed, and if everything is aligned to that performance is best. And for older hard disks with 512B
81 * sector size devices were generally assumed to have an even number of sectors, hence at the worst we'll
82 * waste 3K per partition, which is probably fine. */
83
84static enum {
85 EMPTY_REFUSE, /* refuse empty disks, never create a partition table */
86 EMPTY_ALLOW, /* allow empty disks, create partition table if necessary */
87 EMPTY_REQUIRE, /* require an empty disk, create a partition table */
88 EMPTY_FORCE, /* make disk empty, erase everything, create a partition table always */
a26f4a49 89 EMPTY_CREATE, /* create disk as loopback file, create a partition table always */
e594a3b1
LP
90} arg_empty = EMPTY_REFUSE;
91
92static bool arg_dry_run = true;
93static const char *arg_node = NULL;
94static char *arg_root = NULL;
95static char *arg_definitions = NULL;
96static bool arg_discard = true;
97static bool arg_can_factory_reset = false;
98static int arg_factory_reset = -1;
99static sd_id128_t arg_seed = SD_ID128_NULL;
100static bool arg_randomize = false;
101static int arg_pretty = -1;
a26f4a49 102static uint64_t arg_size = UINT64_MAX;
170c9823 103static bool arg_size_auto = false;
6a01ea4a 104static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
896e678b
LP
105static PagerFlags arg_pager_flags = 0;
106static bool arg_legend = true;
b9df3536
LP
107static void *arg_key = NULL;
108static size_t arg_key_size = 0;
889914ef
LP
109static char *arg_tpm2_device = NULL;
110static uint32_t arg_tpm2_pcr_mask = UINT32_MAX;
e594a3b1
LP
111
112STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
113STATIC_DESTRUCTOR_REGISTER(arg_definitions, freep);
b9df3536 114STATIC_DESTRUCTOR_REGISTER(arg_key, erase_and_freep);
889914ef 115STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep);
e594a3b1
LP
116
117typedef struct Partition Partition;
118typedef struct FreeArea FreeArea;
119typedef struct Context Context;
120
889914ef
LP
121typedef enum EncryptMode {
122 ENCRYPT_OFF,
123 ENCRYPT_KEY_FILE,
124 ENCRYPT_TPM2,
125 ENCRYPT_KEY_FILE_TPM2,
126 _ENCRYPT_MODE_MAX,
2d93c20e 127 _ENCRYPT_MODE_INVALID = -EINVAL,
889914ef
LP
128} EncryptMode;
129
e594a3b1
LP
130struct Partition {
131 char *definition_path;
132
133 sd_id128_t type_uuid;
134 sd_id128_t current_uuid, new_uuid;
135 char *current_label, *new_label;
136
137 bool dropped;
138 bool factory_reset;
139 int32_t priority;
140
141 uint32_t weight, padding_weight;
142
143 uint64_t current_size, new_size;
144 uint64_t size_min, size_max;
145
146 uint64_t current_padding, new_padding;
147 uint64_t padding_min, padding_max;
148
149 uint64_t partno;
150 uint64_t offset;
151
152 struct fdisk_partition *current_partition;
153 struct fdisk_partition *new_partition;
154 FreeArea *padding_area;
155 FreeArea *allocated_to_area;
156
757bc2e4
LP
157 char *copy_blocks_path;
158 int copy_blocks_fd;
159 uint64_t copy_blocks_size;
160
53171c04 161 char *format;
8a794850 162 char **copy_files;
889914ef 163 EncryptMode encrypt;
53171c04 164
e594a3b1
LP
165 LIST_FIELDS(Partition, partitions);
166};
167
168#define PARTITION_IS_FOREIGN(p) (!(p)->definition_path)
169#define PARTITION_EXISTS(p) (!!(p)->current_partition)
170
171struct FreeArea {
172 Partition *after;
173 uint64_t size;
174 uint64_t allocated;
175};
176
177struct Context {
178 LIST_HEAD(Partition, partitions);
179 size_t n_partitions;
180
181 FreeArea **free_areas;
182 size_t n_free_areas, n_allocated_free_areas;
183
184 uint64_t start, end, total;
185
186 struct fdisk_context *fdisk_context;
187
188 sd_id128_t seed;
189};
190
889914ef
LP
191static const char *encrypt_mode_table[_ENCRYPT_MODE_MAX] = {
192 [ENCRYPT_OFF] = "off",
193 [ENCRYPT_KEY_FILE] = "key-file",
194 [ENCRYPT_TPM2] = "tpm2",
195 [ENCRYPT_KEY_FILE_TPM2] = "key-file+tpm2",
196};
197
198DEFINE_PRIVATE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(encrypt_mode, EncryptMode, ENCRYPT_KEY_FILE);
199
e594a3b1
LP
200static uint64_t round_down_size(uint64_t v, uint64_t p) {
201 return (v / p) * p;
202}
203
204static uint64_t round_up_size(uint64_t v, uint64_t p) {
205
206 v = DIV_ROUND_UP(v, p);
207
208 if (v > UINT64_MAX / p)
209 return UINT64_MAX; /* overflow */
210
211 return v * p;
212}
213
214static Partition *partition_new(void) {
215 Partition *p;
216
217 p = new(Partition, 1);
218 if (!p)
219 return NULL;
220
221 *p = (Partition) {
222 .weight = 1000,
223 .padding_weight = 0,
224 .current_size = UINT64_MAX,
225 .new_size = UINT64_MAX,
226 .size_min = UINT64_MAX,
227 .size_max = UINT64_MAX,
228 .current_padding = UINT64_MAX,
229 .new_padding = UINT64_MAX,
230 .padding_min = UINT64_MAX,
231 .padding_max = UINT64_MAX,
232 .partno = UINT64_MAX,
233 .offset = UINT64_MAX,
757bc2e4
LP
234 .copy_blocks_fd = -1,
235 .copy_blocks_size = UINT64_MAX,
e594a3b1
LP
236 };
237
238 return p;
239}
240
241static Partition* partition_free(Partition *p) {
242 if (!p)
243 return NULL;
244
245 free(p->current_label);
246 free(p->new_label);
247 free(p->definition_path);
248
249 if (p->current_partition)
250 fdisk_unref_partition(p->current_partition);
251 if (p->new_partition)
252 fdisk_unref_partition(p->new_partition);
253
757bc2e4
LP
254 free(p->copy_blocks_path);
255 safe_close(p->copy_blocks_fd);
256
53171c04 257 free(p->format);
8a794850 258 strv_free(p->copy_files);
53171c04 259
e594a3b1
LP
260 return mfree(p);
261}
262
263static Partition* partition_unlink_and_free(Context *context, Partition *p) {
264 if (!p)
265 return NULL;
266
267 LIST_REMOVE(partitions, context->partitions, p);
268
269 assert(context->n_partitions > 0);
270 context->n_partitions--;
271
272 return partition_free(p);
273}
274
275DEFINE_TRIVIAL_CLEANUP_FUNC(Partition*, partition_free);
276
277static Context *context_new(sd_id128_t seed) {
278 Context *context;
279
280 context = new(Context, 1);
281 if (!context)
282 return NULL;
283
284 *context = (Context) {
285 .start = UINT64_MAX,
286 .end = UINT64_MAX,
287 .total = UINT64_MAX,
288 .seed = seed,
289 };
290
291 return context;
292}
293
294static void context_free_free_areas(Context *context) {
295 assert(context);
296
297 for (size_t i = 0; i < context->n_free_areas; i++)
298 free(context->free_areas[i]);
299
300 context->free_areas = mfree(context->free_areas);
301 context->n_free_areas = 0;
302 context->n_allocated_free_areas = 0;
303}
304
305static Context *context_free(Context *context) {
306 if (!context)
307 return NULL;
308
309 while (context->partitions)
310 partition_unlink_and_free(context, context->partitions);
311 assert(context->n_partitions == 0);
312
313 context_free_free_areas(context);
314
315 if (context->fdisk_context)
316 fdisk_unref_context(context->fdisk_context);
317
318 return mfree(context);
319}
320
321DEFINE_TRIVIAL_CLEANUP_FUNC(Context*, context_free);
322
323static int context_add_free_area(
324 Context *context,
325 uint64_t size,
326 Partition *after) {
327
328 FreeArea *a;
329
330 assert(context);
331 assert(!after || !after->padding_area);
332
333 if (!GREEDY_REALLOC(context->free_areas, context->n_allocated_free_areas, context->n_free_areas + 1))
334 return -ENOMEM;
335
336 a = new(FreeArea, 1);
337 if (!a)
338 return -ENOMEM;
339
340 *a = (FreeArea) {
341 .size = size,
342 .after = after,
343 };
344
345 context->free_areas[context->n_free_areas++] = a;
346
347 if (after)
348 after->padding_area = a;
349
350 return 0;
351}
352
353static bool context_drop_one_priority(Context *context) {
354 int32_t priority = 0;
355 Partition *p;
356 bool exists = false;
357
358 LIST_FOREACH(partitions, p, context->partitions) {
359 if (p->dropped)
360 continue;
361 if (p->priority < priority)
362 continue;
363 if (p->priority == priority) {
364 exists = exists || PARTITION_EXISTS(p);
365 continue;
366 }
367
368 priority = p->priority;
369 exists = PARTITION_EXISTS(p);
370 }
371
372 /* Refuse to drop partitions with 0 or negative priorities or partitions of priorities that have at
373 * least one existing priority */
374 if (priority <= 0 || exists)
375 return false;
376
377 LIST_FOREACH(partitions, p, context->partitions) {
378 if (p->priority < priority)
379 continue;
380
381 if (p->dropped)
382 continue;
383
384 p->dropped = true;
385 log_info("Can't fit partition %s of priority %" PRIi32 ", dropping.", p->definition_path, p->priority);
386 }
387
388 return true;
389}
390
391static uint64_t partition_min_size(const Partition *p) {
392 uint64_t sz;
393
394 /* Calculate the disk space we really need at minimum for this partition. If the partition already
395 * exists the current size is what we really need. If it doesn't exist yet refuse to allocate less
fb08381c
LP
396 * than 4K.
397 *
398 * DEFAULT_MIN_SIZE is the default SizeMin= we configure if nothing else is specified. */
e594a3b1
LP
399
400 if (PARTITION_IS_FOREIGN(p)) {
401 /* Don't allow changing size of partitions not managed by us */
402 assert(p->current_size != UINT64_MAX);
403 return p->current_size;
404 }
405
fb08381c 406 sz = p->current_size != UINT64_MAX ? p->current_size : HARD_MIN_SIZE;
757bc2e4 407
170c9823
LP
408 if (!PARTITION_EXISTS(p)) {
409 uint64_t d = 0;
410
889914ef 411 if (p->encrypt != ENCRYPT_OFF)
170c9823
LP
412 d += round_up_size(LUKS2_METADATA_SIZE, 4096);
413
414 if (p->copy_blocks_size != UINT64_MAX)
415 d += round_up_size(p->copy_blocks_size, 4096);
889914ef 416 else if (p->format || p->encrypt != ENCRYPT_OFF) {
170c9823
LP
417 uint64_t f;
418
419 /* If we shall synthesize a file system, take minimal fs size into account (assumed to be 4K if not known) */
420 f = p->format ? minimal_size_by_fs_name(p->format) : UINT64_MAX;
421 d += f == UINT64_MAX ? 4096 : f;
422 }
423
424 if (d > sz)
425 sz = d;
426 }
757bc2e4
LP
427
428 return MAX(p->size_min != UINT64_MAX ? p->size_min : DEFAULT_MIN_SIZE, sz);
e594a3b1
LP
429}
430
431static uint64_t partition_max_size(const Partition *p) {
432 /* Calculate how large the partition may become at max. This is generally the configured maximum
433 * size, except when it already exists and is larger than that. In that case it's the existing size,
434 * since we never want to shrink partitions. */
435
436 if (PARTITION_IS_FOREIGN(p)) {
437 /* Don't allow changing size of partitions not managed by us */
438 assert(p->current_size != UINT64_MAX);
439 return p->current_size;
440 }
441
442 if (p->current_size != UINT64_MAX)
443 return MAX(p->current_size, p->size_max);
444
445 return p->size_max;
446}
447
448static uint64_t partition_min_size_with_padding(const Partition *p) {
449 uint64_t sz;
450
451 /* Calculate the disk space we need for this partition plus any free space coming after it. This
452 * takes user configured padding into account as well as any additional whitespace needed to align
453 * the next partition to 4K again. */
454
455 sz = partition_min_size(p);
456
457 if (p->padding_min != UINT64_MAX)
458 sz += p->padding_min;
459
460 if (PARTITION_EXISTS(p)) {
461 /* If the partition wasn't aligned, add extra space so that any we might add will be aligned */
462 assert(p->offset != UINT64_MAX);
463 return round_up_size(p->offset + sz, 4096) - p->offset;
464 }
465
466 /* If this is a new partition we'll place it aligned, hence we just need to round up the required size here */
467 return round_up_size(sz, 4096);
468}
469
470static uint64_t free_area_available(const FreeArea *a) {
471 assert(a);
472
473 /* Determines how much of this free area is not allocated yet */
474
475 assert(a->size >= a->allocated);
476 return a->size - a->allocated;
477}
478
479static uint64_t free_area_available_for_new_partitions(const FreeArea *a) {
480 uint64_t avail;
481
482 /* Similar to free_area_available(), but takes into account that the required size and padding of the
162392b7 483 * preceding partition is honoured. */
e594a3b1
LP
484
485 avail = free_area_available(a);
486 if (a->after) {
487 uint64_t need, space;
488
489 need = partition_min_size_with_padding(a->after);
490
491 assert(a->after->offset != UINT64_MAX);
492 assert(a->after->current_size != UINT64_MAX);
493
494 space = round_up_size(a->after->offset + a->after->current_size, 4096) - a->after->offset + avail;
495 if (need >= space)
496 return 0;
497
498 return space - need;
499 }
500
501 return avail;
502}
503
504static int free_area_compare(FreeArea *const *a, FreeArea *const*b) {
505 return CMP(free_area_available_for_new_partitions(*a),
506 free_area_available_for_new_partitions(*b));
507}
508
509static uint64_t charge_size(uint64_t total, uint64_t amount) {
510 uint64_t rounded;
511
512 assert(amount <= total);
513
514 /* Subtract the specified amount from total, rounding up to multiple of 4K if there's room */
515 rounded = round_up_size(amount, 4096);
516 if (rounded >= total)
517 return 0;
518
519 return total - rounded;
520}
521
522static uint64_t charge_weight(uint64_t total, uint64_t amount) {
523 assert(amount <= total);
524 return total - amount;
525}
526
527static bool context_allocate_partitions(Context *context) {
528 Partition *p;
529
530 assert(context);
531
532 /* A simple first-fit algorithm, assuming the array of free areas is sorted by size in decreasing
533 * order. */
534
535 LIST_FOREACH(partitions, p, context->partitions) {
536 bool fits = false;
537 uint64_t required;
538 FreeArea *a = NULL;
539
540 /* Skip partitions we already dropped or that already exist */
541 if (p->dropped || PARTITION_EXISTS(p))
542 continue;
543
544 /* Sort by size */
545 typesafe_qsort(context->free_areas, context->n_free_areas, free_area_compare);
546
547 /* How much do we need to fit? */
548 required = partition_min_size_with_padding(p);
549 assert(required % 4096 == 0);
550
551 for (size_t i = 0; i < context->n_free_areas; i++) {
552 a = context->free_areas[i];
553
554 if (free_area_available_for_new_partitions(a) >= required) {
555 fits = true;
556 break;
557 }
558 }
559
560 if (!fits)
561 return false; /* 😢 Oh no! We can't fit this partition into any free area! */
562
563 /* Assign the partition to this free area */
564 p->allocated_to_area = a;
565
566 /* Budget the minimal partition size */
567 a->allocated += required;
568 }
569
570 return true;
571}
572
573static int context_sum_weights(Context *context, FreeArea *a, uint64_t *ret) {
574 uint64_t weight_sum = 0;
575 Partition *p;
576
577 assert(context);
578 assert(a);
579 assert(ret);
580
581 /* Determine the sum of the weights of all partitions placed in or before the specified free area */
582
583 LIST_FOREACH(partitions, p, context->partitions) {
584 if (p->padding_area != a && p->allocated_to_area != a)
585 continue;
586
587 if (p->weight > UINT64_MAX - weight_sum)
588 goto overflow_sum;
589 weight_sum += p->weight;
590
591 if (p->padding_weight > UINT64_MAX - weight_sum)
592 goto overflow_sum;
593 weight_sum += p->padding_weight;
594 }
595
596 *ret = weight_sum;
597 return 0;
598
599overflow_sum:
600 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Combined weight of partition exceeds unsigned 64bit range, refusing.");
601}
602
603static int scale_by_weight(uint64_t value, uint64_t weight, uint64_t weight_sum, uint64_t *ret) {
604 assert(weight_sum >= weight);
605 assert(ret);
606
607 if (weight == 0) {
608 *ret = 0;
609 return 0;
610 }
611
612 if (value > UINT64_MAX / weight)
613 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Scaling by weight of partition exceeds unsigned 64bit range, refusing.");
614
615 *ret = value * weight / weight_sum;
616 return 0;
617}
618
619typedef enum GrowPartitionPhase {
620 /* The first phase: we charge partitions which need more (according to constraints) than their weight-based share. */
621 PHASE_OVERCHARGE,
622
623 /* The second phase: we charge partitions which need less (according to constraints) than their weight-based share. */
624 PHASE_UNDERCHARGE,
625
626 /* The third phase: we distribute what remains among the remaining partitions, according to the weights */
627 PHASE_DISTRIBUTE,
628} GrowPartitionPhase;
629
630static int context_grow_partitions_phase(
631 Context *context,
632 FreeArea *a,
633 GrowPartitionPhase phase,
634 uint64_t *span,
635 uint64_t *weight_sum) {
636
637 Partition *p;
638 int r;
639
640 assert(context);
641 assert(a);
642
643 /* Now let's look at the intended weights and adjust them taking the minimum space assignments into
644 * account. i.e. if a partition has a small weight but a high minimum space value set it should not
645 * get any additional room from the left-overs. Similar, if two partitions have the same weight they
646 * should get the same space if possible, even if one has a smaller minimum size than the other. */
647 LIST_FOREACH(partitions, p, context->partitions) {
648
649 /* Look only at partitions associated with this free area, i.e. immediately
162392b7 650 * preceding it, or allocated into it */
e594a3b1
LP
651 if (p->allocated_to_area != a && p->padding_area != a)
652 continue;
653
654 if (p->new_size == UINT64_MAX) {
655 bool charge = false, try_again = false;
656 uint64_t share, rsz, xsz;
657
658 /* Calculate how much this space this partition needs if everyone would get
659 * the weight based share */
660 r = scale_by_weight(*span, p->weight, *weight_sum, &share);
661 if (r < 0)
662 return r;
663
664 rsz = partition_min_size(p);
665 xsz = partition_max_size(p);
666
667 if (phase == PHASE_OVERCHARGE && rsz > share) {
668 /* This partition needs more than its calculated share. Let's assign
669 * it that, and take this partition out of all calculations and start
670 * again. */
671
672 p->new_size = rsz;
673 charge = try_again = true;
674
675 } else if (phase == PHASE_UNDERCHARGE && xsz != UINT64_MAX && xsz < share) {
676 /* This partition accepts less than its calculated
677 * share. Let's assign it that, and take this partition out
678 * of all calculations and start again. */
679
680 p->new_size = xsz;
681 charge = try_again = true;
682
683 } else if (phase == PHASE_DISTRIBUTE) {
684 /* This partition can accept its calculated share. Let's
685 * assign it. There's no need to restart things here since
686 * assigning this shouldn't impact the shares of the other
687 * partitions. */
688
689 if (PARTITION_IS_FOREIGN(p))
690 /* Never change of foreign partitions (i.e. those we don't manage) */
691 p->new_size = p->current_size;
692 else
693 p->new_size = MAX(round_down_size(share, 4096), rsz);
694
695 charge = true;
696 }
697
698 if (charge) {
699 *span = charge_size(*span, p->new_size);
700 *weight_sum = charge_weight(*weight_sum, p->weight);
701 }
702
703 if (try_again)
704 return 0; /* try again */
705 }
706
707 if (p->new_padding == UINT64_MAX) {
708 bool charge = false, try_again = false;
709 uint64_t share;
710
711 r = scale_by_weight(*span, p->padding_weight, *weight_sum, &share);
712 if (r < 0)
713 return r;
714
715 if (phase == PHASE_OVERCHARGE && p->padding_min != UINT64_MAX && p->padding_min > share) {
716 p->new_padding = p->padding_min;
717 charge = try_again = true;
718 } else if (phase == PHASE_UNDERCHARGE && p->padding_max != UINT64_MAX && p->padding_max < share) {
719 p->new_padding = p->padding_max;
720 charge = try_again = true;
721 } else if (phase == PHASE_DISTRIBUTE) {
722
723 p->new_padding = round_down_size(share, 4096);
724 if (p->padding_min != UINT64_MAX && p->new_padding < p->padding_min)
725 p->new_padding = p->padding_min;
726
727 charge = true;
728 }
729
730 if (charge) {
731 *span = charge_size(*span, p->new_padding);
732 *weight_sum = charge_weight(*weight_sum, p->padding_weight);
733 }
734
735 if (try_again)
736 return 0; /* try again */
737 }
738 }
739
740 return 1; /* done */
741}
742
743static int context_grow_partitions_on_free_area(Context *context, FreeArea *a) {
744 uint64_t weight_sum = 0, span;
745 int r;
746
747 assert(context);
748 assert(a);
749
750 r = context_sum_weights(context, a, &weight_sum);
751 if (r < 0)
752 return r;
753
754 /* Let's calculate the total area covered by this free area and the partition before it */
755 span = a->size;
756 if (a->after) {
757 assert(a->after->offset != UINT64_MAX);
758 assert(a->after->current_size != UINT64_MAX);
759
760 span += round_up_size(a->after->offset + a->after->current_size, 4096) - a->after->offset;
761 }
762
763 GrowPartitionPhase phase = PHASE_OVERCHARGE;
764 for (;;) {
765 r = context_grow_partitions_phase(context, a, phase, &span, &weight_sum);
766 if (r < 0)
767 return r;
768 if (r == 0) /* not done yet, re-run this phase */
769 continue;
770
771 if (phase == PHASE_OVERCHARGE)
772 phase = PHASE_UNDERCHARGE;
773 else if (phase == PHASE_UNDERCHARGE)
774 phase = PHASE_DISTRIBUTE;
775 else if (phase == PHASE_DISTRIBUTE)
776 break;
777 }
778
162392b7 779 /* We still have space left over? Donate to preceding partition if we have one */
e594a3b1
LP
780 if (span > 0 && a->after && !PARTITION_IS_FOREIGN(a->after)) {
781 uint64_t m, xsz;
782
783 assert(a->after->new_size != UINT64_MAX);
784 m = a->after->new_size + span;
785
786 xsz = partition_max_size(a->after);
787 if (xsz != UINT64_MAX && m > xsz)
788 m = xsz;
789
790 span = charge_size(span, m - a->after->new_size);
791 a->after->new_size = m;
792 }
793
162392b7 794 /* What? Even still some space left (maybe because there was no preceding partition, or it had a
e594a3b1
LP
795 * size limit), then let's donate it to whoever wants it. */
796 if (span > 0) {
797 Partition *p;
798
799 LIST_FOREACH(partitions, p, context->partitions) {
800 uint64_t m, xsz;
801
802 if (p->allocated_to_area != a)
803 continue;
804
805 if (PARTITION_IS_FOREIGN(p))
806 continue;
807
808 assert(p->new_size != UINT64_MAX);
809 m = p->new_size + span;
810
db144226 811 xsz = partition_max_size(p);
e594a3b1
LP
812 if (xsz != UINT64_MAX && m > xsz)
813 m = xsz;
814
815 span = charge_size(span, m - p->new_size);
816 p->new_size = m;
817
818 if (span == 0)
819 break;
820 }
821 }
822
162392b7 823 /* Yuck, still no one? Then make it padding */
e594a3b1
LP
824 if (span > 0 && a->after) {
825 assert(a->after->new_padding != UINT64_MAX);
826 a->after->new_padding += span;
827 }
828
829 return 0;
830}
831
832static int context_grow_partitions(Context *context) {
833 Partition *p;
834 int r;
835
836 assert(context);
837
838 for (size_t i = 0; i < context->n_free_areas; i++) {
839 r = context_grow_partitions_on_free_area(context, context->free_areas[i]);
840 if (r < 0)
841 return r;
842 }
843
844 /* All existing partitions that have no free space after them can't change size */
845 LIST_FOREACH(partitions, p, context->partitions) {
846 if (p->dropped)
847 continue;
848
849 if (!PARTITION_EXISTS(p) || p->padding_area) {
850 /* The algorithm above must have initialized this already */
851 assert(p->new_size != UINT64_MAX);
852 continue;
853 }
854
855 assert(p->new_size == UINT64_MAX);
856 p->new_size = p->current_size;
857
858 assert(p->new_padding == UINT64_MAX);
859 p->new_padding = p->current_padding;
860 }
861
862 return 0;
863}
864
865static void context_place_partitions(Context *context) {
866 uint64_t partno = 0;
867 Partition *p;
868
869 assert(context);
870
871 /* Determine next partition number to assign */
872 LIST_FOREACH(partitions, p, context->partitions) {
873 if (!PARTITION_EXISTS(p))
874 continue;
875
876 assert(p->partno != UINT64_MAX);
877 if (p->partno >= partno)
878 partno = p->partno + 1;
879 }
880
881 for (size_t i = 0; i < context->n_free_areas; i++) {
882 FreeArea *a = context->free_areas[i];
883 uint64_t start, left;
884
885 if (a->after) {
886 assert(a->after->offset != UINT64_MAX);
887 assert(a->after->new_size != UINT64_MAX);
888 assert(a->after->new_padding != UINT64_MAX);
889
890 start = a->after->offset + a->after->new_size + a->after->new_padding;
891 } else
892 start = context->start;
893
894 start = round_up_size(start, 4096);
895 left = a->size;
896
897 LIST_FOREACH(partitions, p, context->partitions) {
898 if (p->allocated_to_area != a)
899 continue;
900
901 p->offset = start;
902 p->partno = partno++;
903
904 assert(left >= p->new_size);
905 start += p->new_size;
906 left -= p->new_size;
907
908 assert(left >= p->new_padding);
909 start += p->new_padding;
910 left -= p->new_padding;
911 }
912 }
913}
914
e594a3b1
LP
915static int config_parse_type(
916 const char *unit,
917 const char *filename,
918 unsigned line,
919 const char *section,
920 unsigned section_line,
921 const char *lvalue,
922 int ltype,
923 const char *rvalue,
924 void *data,
925 void *userdata) {
926
927 sd_id128_t *type_uuid = data;
928 int r;
929
930 assert(rvalue);
931 assert(type_uuid);
932
933 r = gpt_partition_type_uuid_from_string(rvalue, type_uuid);
934 if (r < 0)
935 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse partition type: %s", rvalue);
936
937 return 0;
938}
939
8a794850 940static const Specifier specifier_table[] = {
2824aa07 941 COMMON_SYSTEM_SPECIFIERS,
8a794850
LP
942 {}
943};
944
e594a3b1
LP
945static int config_parse_label(
946 const char *unit,
947 const char *filename,
948 unsigned line,
949 const char *section,
950 unsigned section_line,
951 const char *lvalue,
952 int ltype,
953 const char *rvalue,
954 void *data,
955 void *userdata) {
956
e031166e 957 _cleanup_free_ char *resolved = NULL;
e594a3b1
LP
958 char **label = data;
959 int r;
960
961 assert(rvalue);
962 assert(label);
963
e031166e
LP
964 r = specifier_printf(rvalue, specifier_table, NULL, &resolved);
965 if (r < 0) {
e459258f 966 log_syntax(unit, LOG_WARNING, filename, line, r,
e031166e
LP
967 "Failed to expand specifiers in Label=, ignoring: %s", rvalue);
968 return 0;
969 }
970
971 if (!utf8_is_valid(resolved)) {
e594a3b1
LP
972 log_syntax(unit, LOG_WARNING, filename, line, 0,
973 "Partition label not valid UTF-8, ignoring: %s", rvalue);
974 return 0;
975 }
976
22a0a36e
LP
977 r = gpt_partition_label_valid(resolved);
978 if (r < 0) {
979 log_syntax(unit, LOG_WARNING, filename, line, r,
980 "Failed to check if string is valid as GPT partition label, ignoring: \"%s\" (from \"%s\")",
981 resolved, rvalue);
982 return 0;
983 }
984 if (!r) {
e594a3b1 985 log_syntax(unit, LOG_WARNING, filename, line, 0,
46072ae3
ZJS
986 "Partition label too long for GPT table, ignoring: \"%s\" (from \"%s\")",
987 resolved, rvalue);
e594a3b1
LP
988 return 0;
989 }
990
e031166e 991 free_and_replace(*label, resolved);
e594a3b1
LP
992 return 0;
993}
994
995static int config_parse_weight(
996 const char *unit,
997 const char *filename,
998 unsigned line,
999 const char *section,
1000 unsigned section_line,
1001 const char *lvalue,
1002 int ltype,
1003 const char *rvalue,
1004 void *data,
1005 void *userdata) {
1006
1007 uint32_t *priority = data, v;
1008 int r;
1009
1010 assert(rvalue);
1011 assert(priority);
1012
1013 r = safe_atou32(rvalue, &v);
1014 if (r < 0) {
1015 log_syntax(unit, LOG_WARNING, filename, line, r,
1016 "Failed to parse weight value, ignoring: %s", rvalue);
1017 return 0;
1018 }
1019
1020 if (v > 1000U*1000U) {
c8f3d767 1021 log_syntax(unit, LOG_WARNING, filename, line, 0,
e594a3b1
LP
1022 "Weight needs to be in range 0…10000000, ignoring: %" PRIu32, v);
1023 return 0;
1024 }
1025
1026 *priority = v;
1027 return 0;
1028}
1029
1030static int config_parse_size4096(
1031 const char *unit,
1032 const char *filename,
1033 unsigned line,
1034 const char *section,
1035 unsigned section_line,
1036 const char *lvalue,
1037 int ltype,
1038 const char *rvalue,
1039 void *data,
1040 void *userdata) {
1041
1042 uint64_t *sz = data, parsed;
1043 int r;
1044
1045 assert(rvalue);
1046 assert(data);
1047
1048 r = parse_size(rvalue, 1024, &parsed);
1049 if (r < 0)
c8f3d767 1050 return log_syntax(unit, LOG_ERR, filename, line, r,
e594a3b1
LP
1051 "Failed to parse size value: %s", rvalue);
1052
1053 if (ltype > 0)
1054 *sz = round_up_size(parsed, 4096);
1055 else if (ltype < 0)
1056 *sz = round_down_size(parsed, 4096);
1057 else
1058 *sz = parsed;
1059
1060 if (*sz != parsed)
1061 log_syntax(unit, LOG_NOTICE, filename, line, r, "Rounded %s= size %" PRIu64 " → %" PRIu64 ", a multiple of 4096.", lvalue, parsed, *sz);
1062
1063 return 0;
1064}
1065
53171c04
LP
1066static int config_parse_fstype(
1067 const char *unit,
1068 const char *filename,
1069 unsigned line,
1070 const char *section,
1071 unsigned section_line,
1072 const char *lvalue,
1073 int ltype,
1074 const char *rvalue,
1075 void *data,
1076 void *userdata) {
1077
1078 char **fstype = data;
1079
1080 assert(rvalue);
1081 assert(data);
1082
1083 if (!filename_is_valid(rvalue))
1084 return log_syntax(unit, LOG_ERR, filename, line, 0,
1085 "File system type is not valid, refusing: %s", rvalue);
1086
1087 return free_and_strdup_warn(fstype, rvalue);
1088}
1089
8a794850
LP
1090static int config_parse_copy_files(
1091 const char *unit,
1092 const char *filename,
1093 unsigned line,
1094 const char *section,
1095 unsigned section_line,
1096 const char *lvalue,
1097 int ltype,
1098 const char *rvalue,
1099 void *data,
1100 void *userdata) {
1101
1102 _cleanup_free_ char *source = NULL, *buffer = NULL, *resolved_source = NULL, *resolved_target = NULL;
1103 const char *p = rvalue, *target;
1104 Partition *partition = data;
1105 int r;
1106
1107 assert(rvalue);
1108 assert(partition);
1109
1110 r = extract_first_word(&p, &source, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS);
1111 if (r < 0)
1112 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract source path: %s", rvalue);
1113 if (r == 0) {
1114 log_syntax(unit, LOG_WARNING, filename, line, 0, "No argument specified: %s", rvalue);
1115 return 0;
1116 }
1117
1118 r = extract_first_word(&p, &buffer, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS);
1119 if (r < 0)
1120 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract target path: %s", rvalue);
1121 if (r == 0)
1122 target = source; /* No target, then it's the same as the source */
1123 else
1124 target = buffer;
1125
1126 if (!isempty(p))
1127 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), "Too many arguments: %s", rvalue);
1128
1129 r = specifier_printf(source, specifier_table, NULL, &resolved_source);
1130 if (r < 0) {
1131 log_syntax(unit, LOG_WARNING, filename, line, r,
1132 "Failed to expand specifiers in CopyFiles= source, ignoring: %s", rvalue);
1133 return 0;
1134 }
1135
0ade2213
LP
1136 r = path_simplify_and_warn(resolved_source, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1137 if (r < 0)
8a794850 1138 return 0;
8a794850
LP
1139
1140 r = specifier_printf(target, specifier_table, NULL, &resolved_target);
1141 if (r < 0) {
1142 log_syntax(unit, LOG_WARNING, filename, line, r,
1143 "Failed to expand specifiers in CopyFiles= target, ignoring: %s", resolved_target);
1144 return 0;
1145 }
1146
0ade2213
LP
1147 r = path_simplify_and_warn(resolved_target, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1148 if (r < 0)
8a794850 1149 return 0;
8a794850
LP
1150
1151 r = strv_consume_pair(&partition->copy_files, TAKE_PTR(resolved_source), TAKE_PTR(resolved_target));
1152 if (r < 0)
1153 return log_oom();
1154
1155 return 0;
1156}
1157
889914ef
LP
1158static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_encrypt, encrypt_mode, EncryptMode, ENCRYPT_OFF, "Invalid encryption mode");
1159
e594a3b1
LP
1160static int partition_read_definition(Partition *p, const char *path) {
1161
1162 ConfigTableItem table[] = {
8a794850
LP
1163 { "Partition", "Type", config_parse_type, 0, &p->type_uuid },
1164 { "Partition", "Label", config_parse_label, 0, &p->new_label },
1165 { "Partition", "UUID", config_parse_id128, 0, &p->new_uuid },
1166 { "Partition", "Priority", config_parse_int32, 0, &p->priority },
1167 { "Partition", "Weight", config_parse_weight, 0, &p->weight },
1168 { "Partition", "PaddingWeight", config_parse_weight, 0, &p->padding_weight },
1169 { "Partition", "SizeMinBytes", config_parse_size4096, 1, &p->size_min },
1170 { "Partition", "SizeMaxBytes", config_parse_size4096, -1, &p->size_max },
1171 { "Partition", "PaddingMinBytes", config_parse_size4096, 1, &p->padding_min },
1172 { "Partition", "PaddingMaxBytes", config_parse_size4096, -1, &p->padding_max },
1173 { "Partition", "FactoryReset", config_parse_bool, 0, &p->factory_reset },
1174 { "Partition", "CopyBlocks", config_parse_path, 0, &p->copy_blocks_path },
1175 { "Partition", "Format", config_parse_fstype, 0, &p->format },
1176 { "Partition", "CopyFiles", config_parse_copy_files, 0, p },
889914ef 1177 { "Partition", "Encrypt", config_parse_encrypt, 0, &p->encrypt },
e594a3b1
LP
1178 {}
1179 };
1180 int r;
1181
4f9ff96a
LP
1182 r = config_parse(NULL, path, NULL,
1183 "Partition\0",
1184 config_item_table_lookup, table,
1185 CONFIG_PARSE_WARN,
1186 p,
1187 NULL);
e594a3b1
LP
1188 if (r < 0)
1189 return r;
1190
1191 if (p->size_min != UINT64_MAX && p->size_max != UINT64_MAX && p->size_min > p->size_max)
1192 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1193 "SizeMinBytes= larger than SizeMaxBytes=, refusing.");
1194
1195 if (p->padding_min != UINT64_MAX && p->padding_max != UINT64_MAX && p->padding_min > p->padding_max)
1196 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1197 "PaddingMinBytes= larger than PaddingMaxBytes=, refusing.");
1198
1199 if (sd_id128_is_null(p->type_uuid))
1200 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1201 "Type= not defined, refusing.");
1202
8a794850 1203 if (p->copy_blocks_path && (p->format || !strv_isempty(p->copy_files)))
53171c04
LP
1204 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1205 "Format= and CopyBlocks= cannot be combined, refusing.");
1206
8a794850
LP
1207 if (!strv_isempty(p->copy_files) && streq_ptr(p->format, "swap"))
1208 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1209 "Format=swap and CopyFiles= cannot be combined, refusing.");
1210
889914ef 1211 if (!p->format && (!strv_isempty(p->copy_files) || (p->encrypt != ENCRYPT_OFF && !p->copy_blocks_path))) {
b9df3536 1212 /* Pick "ext4" as file system if we are configured to copy files or encrypt the device */
8a794850
LP
1213 p->format = strdup("ext4");
1214 if (!p->format)
1215 return log_oom();
1216 }
1217
e594a3b1
LP
1218 return 0;
1219}
1220
1221static int context_read_definitions(
1222 Context *context,
1223 const char *directory,
1224 const char *root) {
1225
1226 _cleanup_strv_free_ char **files = NULL;
1227 Partition *last = NULL;
1228 char **f;
1229 int r;
1230
1231 assert(context);
1232
1233 if (directory)
1234 r = conf_files_list_strv(&files, ".conf", NULL, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, (const char**) STRV_MAKE(directory));
1235 else
1236 r = conf_files_list_strv(&files, ".conf", root, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, (const char**) CONF_PATHS_STRV("repart.d"));
1237 if (r < 0)
1238 return log_error_errno(r, "Failed to enumerate *.conf files: %m");
1239
1240 STRV_FOREACH(f, files) {
1241 _cleanup_(partition_freep) Partition *p = NULL;
1242
1243 p = partition_new();
1244 if (!p)
1245 return log_oom();
1246
1247 p->definition_path = strdup(*f);
1248 if (!p->definition_path)
1249 return log_oom();
1250
1251 r = partition_read_definition(p, *f);
1252 if (r < 0)
1253 return r;
1254
1255 LIST_INSERT_AFTER(partitions, context->partitions, last, p);
1256 last = TAKE_PTR(p);
1257 context->n_partitions++;
1258 }
1259
1260 return 0;
1261}
1262
fd421c4a
ZJS
1263DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_context*, fdisk_unref_context, NULL);
1264DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_partition*, fdisk_unref_partition, NULL);
1265DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_parttype*, fdisk_unref_parttype, NULL);
1266DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_table*, fdisk_unref_table, NULL);
e594a3b1
LP
1267
1268static int determine_current_padding(
1269 struct fdisk_context *c,
1270 struct fdisk_table *t,
1271 struct fdisk_partition *p,
1272 uint64_t *ret) {
1273
1274 size_t n_partitions;
1275 uint64_t offset, next = UINT64_MAX;
1276
1277 assert(c);
1278 assert(t);
1279 assert(p);
1280
1281 if (!fdisk_partition_has_end(p))
1282 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition has no end!");
1283
1284 offset = fdisk_partition_get_end(p);
1285 assert(offset < UINT64_MAX / 512);
1286 offset *= 512;
1287
1288 n_partitions = fdisk_table_get_nents(t);
1289 for (size_t i = 0; i < n_partitions; i++) {
1290 struct fdisk_partition *q;
1291 uint64_t start;
1292
1293 q = fdisk_table_get_partition(t, i);
1294 if (!q)
1295 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
1296
1297 if (fdisk_partition_is_used(q) <= 0)
1298 continue;
1299
1300 if (!fdisk_partition_has_start(q))
1301 continue;
1302
1303 start = fdisk_partition_get_start(q);
1304 assert(start < UINT64_MAX / 512);
1305 start *= 512;
1306
1307 if (start >= offset && (next == UINT64_MAX || next > start))
1308 next = start;
1309 }
1310
1311 if (next == UINT64_MAX) {
1312 /* No later partition? In that case check the end of the usable area */
1313 next = fdisk_get_last_lba(c);
1314 assert(next < UINT64_MAX);
1315 next++; /* The last LBA is one sector before the end */
1316
1317 assert(next < UINT64_MAX / 512);
1318 next *= 512;
1319
1320 if (offset > next)
1321 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end.");
1322 }
1323
1324 assert(next >= offset);
1325 offset = round_up_size(offset, 4096);
1326 next = round_down_size(next, 4096);
1327
1328 if (next >= offset) /* Check again, rounding might have fucked things up */
1329 *ret = next - offset;
1330 else
1331 *ret = 0;
1332
1333 return 0;
1334}
1335
1336static int fdisk_ask_cb(struct fdisk_context *c, struct fdisk_ask *ask, void *data) {
1337 _cleanup_free_ char *ids = NULL;
1338 int r;
1339
1340 if (fdisk_ask_get_type(ask) != FDISK_ASKTYPE_STRING)
1341 return -EINVAL;
1342
1343 ids = new(char, ID128_UUID_STRING_MAX);
1344 if (!ids)
1345 return -ENOMEM;
1346
1347 r = fdisk_ask_string_set_result(ask, id128_to_uuid_string(*(sd_id128_t*) data, ids));
1348 if (r < 0)
1349 return r;
1350
1351 TAKE_PTR(ids);
1352 return 0;
1353}
1354
1355static int fdisk_set_disklabel_id_by_uuid(struct fdisk_context *c, sd_id128_t id) {
1356 int r;
1357
1358 r = fdisk_set_ask(c, fdisk_ask_cb, &id);
1359 if (r < 0)
1360 return r;
1361
1362 r = fdisk_set_disklabel_id(c);
1363 if (r < 0)
1364 return r;
1365
1366 return fdisk_set_ask(c, NULL, NULL);
1367}
1368
53171c04 1369static int derive_uuid(sd_id128_t base, const char *token, sd_id128_t *ret) {
e594a3b1
LP
1370 union {
1371 unsigned char md[SHA256_DIGEST_LENGTH];
1372 sd_id128_t id;
1373 } result;
1374
53171c04 1375 assert(token);
e594a3b1
LP
1376 assert(ret);
1377
53171c04
LP
1378 /* Derive a new UUID from the specified UUID in a stable and reasonably safe way. Specifically, we
1379 * calculate the HMAC-SHA256 of the specified token string, keyed by the supplied base (typically the
1380 * machine ID). We use the machine ID as key (and not as cleartext!) of the HMAC operation since it's
1381 * the machine ID we don't want to leak. */
e594a3b1
LP
1382
1383 if (!HMAC(EVP_sha256(),
53171c04
LP
1384 &base, sizeof(base),
1385 (const unsigned char*) token, strlen(token),
e594a3b1
LP
1386 result.md, NULL))
1387 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "HMAC-SHA256 calculation failed.");
1388
1389 /* Take the first half, mark it as v4 UUID */
1390 assert_cc(sizeof(result.md) == sizeof(result.id) * 2);
1391 *ret = id128_make_v4_uuid(result.id);
1392 return 0;
1393}
1394
a26f4a49
LP
1395static int context_load_partition_table(
1396 Context *context,
1397 const char *node,
1398 int *backing_fd) {
1399
e594a3b1
LP
1400 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
1401 _cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL;
1402 uint64_t left_boundary = UINT64_MAX, first_lba, last_lba, nsectors;
1403 _cleanup_free_ char *disk_uuid_string = NULL;
1404 bool from_scratch = false;
1405 sd_id128_t disk_uuid;
1406 size_t n_partitions;
1407 int r;
1408
1409 assert(context);
1410 assert(node);
a26f4a49 1411 assert(backing_fd);
170c9823
LP
1412 assert(!context->fdisk_context);
1413 assert(!context->free_areas);
1414 assert(context->start == UINT64_MAX);
1415 assert(context->end == UINT64_MAX);
1416 assert(context->total == UINT64_MAX);
e594a3b1
LP
1417
1418 c = fdisk_new_context();
1419 if (!c)
1420 return log_oom();
1421
a26f4a49
LP
1422 /* libfdisk doesn't have an API to operate on arbitrary fds, hence reopen the fd going via the
1423 * /proc/self/fd/ magic path if we have an existing fd. Open the original file otherwise. */
1424 if (*backing_fd < 0)
1425 r = fdisk_assign_device(c, node, arg_dry_run);
1426 else {
1427 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
1428 xsprintf(procfs_path, "/proc/self/fd/%i", *backing_fd);
1429
1430 r = fdisk_assign_device(c, procfs_path, arg_dry_run);
1431 }
170c9823
LP
1432 if (r == -EINVAL && arg_size_auto) {
1433 struct stat st;
1434
1435 /* libfdisk returns EINVAL if opening a file of size zero. Let's check for that, and accept
1436 * it if automatic sizing is requested. */
1437
1438 if (*backing_fd < 0)
1439 r = stat(node, &st);
1440 else
1441 r = fstat(*backing_fd, &st);
1442 if (r < 0)
1443 return log_error_errno(errno, "Failed to stat block device '%s': %m", node);
1444
1445 if (S_ISREG(st.st_mode) && st.st_size == 0)
1446 return /* from_scratch = */ true;
1447
1448 r = -EINVAL;
1449 }
e594a3b1 1450 if (r < 0)
a26f4a49
LP
1451 return log_error_errno(r, "Failed to open device '%s': %m", node);
1452
1453 if (*backing_fd < 0) {
1454 /* If we have no fd referencing the device yet, make a copy of the fd now, so that we have one */
1455 *backing_fd = fcntl(fdisk_get_devfd(c), F_DUPFD_CLOEXEC, 3);
1456 if (*backing_fd < 0)
1457 return log_error_errno(errno, "Failed to duplicate fdisk fd: %m");
1458 }
e594a3b1
LP
1459
1460 /* Tell udev not to interfere while we are processing the device */
1461 if (flock(fdisk_get_devfd(c), arg_dry_run ? LOCK_SH : LOCK_EX) < 0)
1462 return log_error_errno(errno, "Failed to lock block device: %m");
1463
1464 switch (arg_empty) {
1465
1466 case EMPTY_REFUSE:
1467 /* Refuse empty disks, insist on an existing GPT partition table */
1468 if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
1469 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not repartitioning.", node);
1470
1471 break;
1472
1473 case EMPTY_REQUIRE:
1474 /* Require an empty disk, refuse any existing partition table */
1475 r = fdisk_has_label(c);
1476 if (r < 0)
1477 return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", node);
1478 if (r > 0)
1479 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s already has a disk label, refusing.", node);
1480
1481 from_scratch = true;
1482 break;
1483
1484 case EMPTY_ALLOW:
1485 /* Allow both an empty disk and an existing partition table, but only GPT */
1486 r = fdisk_has_label(c);
1487 if (r < 0)
1488 return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", node);
1489 if (r > 0) {
1490 if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
1491 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has non-GPT disk label, not repartitioning.", node);
1492 } else
1493 from_scratch = true;
1494
1495 break;
1496
1497 case EMPTY_FORCE:
a26f4a49 1498 case EMPTY_CREATE:
e594a3b1
LP
1499 /* Always reinitiaize the disk, don't consider what there was on the disk before */
1500 from_scratch = true;
1501 break;
1502 }
1503
1504 if (from_scratch) {
e594a3b1
LP
1505 r = fdisk_create_disklabel(c, "gpt");
1506 if (r < 0)
1507 return log_error_errno(r, "Failed to create GPT disk label: %m");
1508
53171c04 1509 r = derive_uuid(context->seed, "disk-uuid", &disk_uuid);
e594a3b1
LP
1510 if (r < 0)
1511 return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
1512
1513 r = fdisk_set_disklabel_id_by_uuid(c, disk_uuid);
1514 if (r < 0)
1515 return log_error_errno(r, "Failed to set GPT disk label: %m");
1516
1517 goto add_initial_free_area;
1518 }
1519
1520 r = fdisk_get_disklabel_id(c, &disk_uuid_string);
1521 if (r < 0)
1522 return log_error_errno(r, "Failed to get current GPT disk label UUID: %m");
1523
1524 r = sd_id128_from_string(disk_uuid_string, &disk_uuid);
1525 if (r < 0)
1526 return log_error_errno(r, "Failed to parse current GPT disk label UUID: %m");
1527
1528 if (sd_id128_is_null(disk_uuid)) {
53171c04 1529 r = derive_uuid(context->seed, "disk-uuid", &disk_uuid);
e594a3b1
LP
1530 if (r < 0)
1531 return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
1532
1533 r = fdisk_set_disklabel_id(c);
1534 if (r < 0)
1535 return log_error_errno(r, "Failed to set GPT disk label: %m");
1536 }
1537
1538 r = fdisk_get_partitions(c, &t);
1539 if (r < 0)
1540 return log_error_errno(r, "Failed to acquire partition table: %m");
1541
1542 n_partitions = fdisk_table_get_nents(t);
1543 for (size_t i = 0; i < n_partitions; i++) {
1544 _cleanup_free_ char *label_copy = NULL;
1545 Partition *pp, *last = NULL;
1546 struct fdisk_partition *p;
1547 struct fdisk_parttype *pt;
1548 const char *pts, *ids, *label;
1549 uint64_t sz, start;
1550 bool found = false;
1551 sd_id128_t ptid, id;
1552 size_t partno;
1553
1554 p = fdisk_table_get_partition(t, i);
1555 if (!p)
1556 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
1557
1558 if (fdisk_partition_is_used(p) <= 0)
1559 continue;
1560
1561 if (fdisk_partition_has_start(p) <= 0 ||
1562 fdisk_partition_has_size(p) <= 0 ||
1563 fdisk_partition_has_partno(p) <= 0)
1564 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a position, size or number.");
1565
1566 pt = fdisk_partition_get_type(p);
1567 if (!pt)
1568 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition: %m");
1569
1570 pts = fdisk_parttype_get_string(pt);
1571 if (!pts)
1572 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition as string: %m");
1573
1574 r = sd_id128_from_string(pts, &ptid);
1575 if (r < 0)
1576 return log_error_errno(r, "Failed to parse partition type UUID %s: %m", pts);
1577
1578 ids = fdisk_partition_get_uuid(p);
1579 if (!ids)
1580 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a UUID.");
1581
1582 r = sd_id128_from_string(ids, &id);
1583 if (r < 0)
1584 return log_error_errno(r, "Failed to parse partition UUID %s: %m", ids);
1585
1586 label = fdisk_partition_get_name(p);
1587 if (!isempty(label)) {
1588 label_copy = strdup(label);
1589 if (!label_copy)
1590 return log_oom();
1591 }
1592
1593 sz = fdisk_partition_get_size(p);
1594 assert_se(sz <= UINT64_MAX/512);
1595 sz *= 512;
1596
1597 start = fdisk_partition_get_start(p);
1598 assert_se(start <= UINT64_MAX/512);
1599 start *= 512;
1600
1601 partno = fdisk_partition_get_partno(p);
1602
1603 if (left_boundary == UINT64_MAX || left_boundary > start)
1604 left_boundary = start;
1605
1606 /* Assign this existing partition to the first partition of the right type that doesn't have
1607 * an existing one assigned yet. */
1608 LIST_FOREACH(partitions, pp, context->partitions) {
1609 last = pp;
1610
1611 if (!sd_id128_equal(pp->type_uuid, ptid))
1612 continue;
1613
1614 if (!pp->current_partition) {
1615 pp->current_uuid = id;
1616 pp->current_size = sz;
1617 pp->offset = start;
1618 pp->partno = partno;
1619 pp->current_label = TAKE_PTR(label_copy);
1620
1621 pp->current_partition = p;
1622 fdisk_ref_partition(p);
1623
1624 r = determine_current_padding(c, t, p, &pp->current_padding);
1625 if (r < 0)
1626 return r;
1627
1628 if (pp->current_padding > 0) {
1629 r = context_add_free_area(context, pp->current_padding, pp);
1630 if (r < 0)
1631 return r;
1632 }
1633
1634 found = true;
1635 break;
1636 }
1637 }
1638
1639 /* If we have no matching definition, create a new one. */
1640 if (!found) {
1641 _cleanup_(partition_freep) Partition *np = NULL;
1642
1643 np = partition_new();
1644 if (!np)
1645 return log_oom();
1646
1647 np->current_uuid = id;
1648 np->type_uuid = ptid;
1649 np->current_size = sz;
1650 np->offset = start;
1651 np->partno = partno;
1652 np->current_label = TAKE_PTR(label_copy);
1653
1654 np->current_partition = p;
1655 fdisk_ref_partition(p);
1656
1657 r = determine_current_padding(c, t, p, &np->current_padding);
1658 if (r < 0)
1659 return r;
1660
1661 if (np->current_padding > 0) {
1662 r = context_add_free_area(context, np->current_padding, np);
1663 if (r < 0)
1664 return r;
1665 }
1666
1667 LIST_INSERT_AFTER(partitions, context->partitions, last, TAKE_PTR(np));
1668 context->n_partitions++;
1669 }
1670 }
1671
1672add_initial_free_area:
1673 nsectors = fdisk_get_nsectors(c);
1674 assert(nsectors <= UINT64_MAX/512);
1675 nsectors *= 512;
1676
1677 first_lba = fdisk_get_first_lba(c);
1678 assert(first_lba <= UINT64_MAX/512);
1679 first_lba *= 512;
1680
1681 last_lba = fdisk_get_last_lba(c);
1682 assert(last_lba < UINT64_MAX);
1683 last_lba++;
1684 assert(last_lba <= UINT64_MAX/512);
1685 last_lba *= 512;
1686
1687 assert(last_lba >= first_lba);
1688
1689 if (left_boundary == UINT64_MAX) {
1690 /* No partitions at all? Then the whole disk is up for grabs. */
1691
1692 first_lba = round_up_size(first_lba, 4096);
1693 last_lba = round_down_size(last_lba, 4096);
1694
1695 if (last_lba > first_lba) {
1696 r = context_add_free_area(context, last_lba - first_lba, NULL);
1697 if (r < 0)
1698 return r;
1699 }
1700 } else {
1701 /* Add space left of first partition */
1702 assert(left_boundary >= first_lba);
1703
1704 first_lba = round_up_size(first_lba, 4096);
1705 left_boundary = round_down_size(left_boundary, 4096);
1706 last_lba = round_down_size(last_lba, 4096);
1707
1708 if (left_boundary > first_lba) {
1709 r = context_add_free_area(context, left_boundary - first_lba, NULL);
1710 if (r < 0)
1711 return r;
1712 }
1713 }
1714
1715 context->start = first_lba;
1716 context->end = last_lba;
1717 context->total = nsectors;
1718 context->fdisk_context = TAKE_PTR(c);
1719
1720 return from_scratch;
1721}
1722
1723static void context_unload_partition_table(Context *context) {
1724 Partition *p, *next;
1725
1726 assert(context);
1727
1728 LIST_FOREACH_SAFE(partitions, p, next, context->partitions) {
1729
1730 /* Entirely remove partitions that have no configuration */
1731 if (PARTITION_IS_FOREIGN(p)) {
1732 partition_unlink_and_free(context, p);
1733 continue;
1734 }
1735
1736 /* Otherwise drop all data we read off the block device and everything we might have
1737 * calculated based on it */
1738
1739 p->dropped = false;
1740 p->current_size = UINT64_MAX;
1741 p->new_size = UINT64_MAX;
1742 p->current_padding = UINT64_MAX;
1743 p->new_padding = UINT64_MAX;
1744 p->partno = UINT64_MAX;
1745 p->offset = UINT64_MAX;
1746
1747 if (p->current_partition) {
1748 fdisk_unref_partition(p->current_partition);
1749 p->current_partition = NULL;
1750 }
1751
1752 if (p->new_partition) {
1753 fdisk_unref_partition(p->new_partition);
1754 p->new_partition = NULL;
1755 }
1756
1757 p->padding_area = NULL;
1758 p->allocated_to_area = NULL;
1759
15d43e30
LP
1760 p->current_uuid = SD_ID128_NULL;
1761 p->current_label = mfree(p->current_label);
e594a3b1
LP
1762 }
1763
1764 context->start = UINT64_MAX;
1765 context->end = UINT64_MAX;
1766 context->total = UINT64_MAX;
1767
1768 if (context->fdisk_context) {
1769 fdisk_unref_context(context->fdisk_context);
1770 context->fdisk_context = NULL;
1771 }
1772
1773 context_free_free_areas(context);
1774}
1775
1776static int format_size_change(uint64_t from, uint64_t to, char **ret) {
1777 char format_buffer1[FORMAT_BYTES_MAX], format_buffer2[FORMAT_BYTES_MAX], *buf;
1778
1779 if (from != UINT64_MAX)
1780 format_bytes(format_buffer1, sizeof(format_buffer1), from);
1781 if (to != UINT64_MAX)
1782 format_bytes(format_buffer2, sizeof(format_buffer2), to);
1783
1784 if (from != UINT64_MAX) {
1785 if (from == to || to == UINT64_MAX)
1786 buf = strdup(format_buffer1);
1787 else
1788 buf = strjoin(format_buffer1, " ", special_glyph(SPECIAL_GLYPH_ARROW), " ", format_buffer2);
1789 } else if (to != UINT64_MAX)
1790 buf = strjoin(special_glyph(SPECIAL_GLYPH_ARROW), " ", format_buffer2);
1791 else {
1792 *ret = NULL;
1793 return 0;
1794 }
1795
1796 if (!buf)
1797 return log_oom();
1798
1799 *ret = TAKE_PTR(buf);
1800 return 1;
1801}
1802
1803static const char *partition_label(const Partition *p) {
1804 assert(p);
1805
1806 if (p->new_label)
1807 return p->new_label;
1808
1809 if (p->current_label)
1810 return p->current_label;
1811
1812 return gpt_partition_type_uuid_to_string(p->type_uuid);
1813}
1814
1815static int context_dump_partitions(Context *context, const char *node) {
1816 _cleanup_(table_unrefp) Table *t = NULL;
1817 uint64_t sum_padding = 0, sum_size = 0;
1818 Partition *p;
1819 int r;
1820
6a01ea4a 1821 if ((arg_json_format_flags & JSON_FORMAT_OFF) && context->n_partitions == 0) {
a015fbe7
TH
1822 log_info("Empty partition table.");
1823 return 0;
1824 }
1825
1826 t = table_new("type", "label", "uuid", "file", "node", "offset", "old size", "raw size", "size", "old padding", "raw padding", "padding", "activity");
e594a3b1
LP
1827 if (!t)
1828 return log_oom();
1829
a015fbe7 1830 if (!DEBUG_LOGGING) {
6a01ea4a 1831 if (arg_json_format_flags & JSON_FORMAT_OFF)
a015fbe7 1832 (void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
ef1e0b9a 1833 (size_t) 8, (size_t) 11);
a015fbe7
TH
1834 else
1835 (void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
ef1e0b9a 1836 (size_t) 5, (size_t) 6, (size_t) 7, (size_t) 9, (size_t) 10, (size_t) 12);
a015fbe7 1837 }
e594a3b1
LP
1838
1839 (void) table_set_align_percent(t, table_get_cell(t, 0, 4), 100);
1840 (void) table_set_align_percent(t, table_get_cell(t, 0, 5), 100);
1841
1842 LIST_FOREACH(partitions, p, context->partitions) {
1843 _cleanup_free_ char *size_change = NULL, *padding_change = NULL, *partname = NULL;
1844 char uuid_buffer[ID128_UUID_STRING_MAX];
a015fbe7 1845 const char *label, *activity = NULL;
e594a3b1
LP
1846
1847 if (p->dropped)
1848 continue;
1849
a015fbe7
TH
1850 if (p->current_size == UINT64_MAX)
1851 activity = "create";
1852 else if (p->current_size != p->new_size)
1853 activity = "resize";
1854
e594a3b1
LP
1855 label = partition_label(p);
1856 partname = p->partno != UINT64_MAX ? fdisk_partname(node, p->partno+1) : NULL;
1857
1858 r = format_size_change(p->current_size, p->new_size, &size_change);
1859 if (r < 0)
1860 return r;
1861
1862 r = format_size_change(p->current_padding, p->new_padding, &padding_change);
1863 if (r < 0)
1864 return r;
1865
1866 if (p->new_size != UINT64_MAX)
1867 sum_size += p->new_size;
1868 if (p->new_padding != UINT64_MAX)
1869 sum_padding += p->new_padding;
1870
1871 r = table_add_many(
1872 t,
1873 TABLE_STRING, gpt_partition_type_uuid_to_string_harder(p->type_uuid, uuid_buffer),
1874 TABLE_STRING, label ?: "-", TABLE_SET_COLOR, label ? NULL : ansi_grey(),
1875 TABLE_UUID, sd_id128_is_null(p->new_uuid) ? p->current_uuid : p->new_uuid,
1876 TABLE_STRING, p->definition_path ? basename(p->definition_path) : "-", TABLE_SET_COLOR, p->definition_path ? NULL : ansi_grey(),
a015fbe7 1877 TABLE_STRING, partname ?: "-", TABLE_SET_COLOR, partname ? NULL : ansi_highlight(),
e594a3b1 1878 TABLE_UINT64, p->offset,
a015fbe7 1879 TABLE_UINT64, p->current_size == UINT64_MAX ? 0 : p->current_size,
e594a3b1
LP
1880 TABLE_UINT64, p->new_size,
1881 TABLE_STRING, size_change, TABLE_SET_COLOR, !p->partitions_next && sum_size > 0 ? ansi_underline() : NULL,
a015fbe7 1882 TABLE_UINT64, p->current_padding == UINT64_MAX ? 0 : p->current_padding,
e594a3b1 1883 TABLE_UINT64, p->new_padding,
a015fbe7
TH
1884 TABLE_STRING, padding_change, TABLE_SET_COLOR, !p->partitions_next && sum_padding > 0 ? ansi_underline() : NULL,
1885 TABLE_STRING, activity ?: "unknown");
e594a3b1 1886 if (r < 0)
f987a261 1887 return table_log_add_error(r);
e594a3b1
LP
1888 }
1889
6a01ea4a 1890 if ((arg_json_format_flags & JSON_FORMAT_OFF) && (sum_padding > 0 || sum_size > 0)) {
e594a3b1
LP
1891 char s[FORMAT_BYTES_MAX];
1892 const char *a, *b;
1893
1894 a = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", format_bytes(s, sizeof(s), sum_size));
1895 b = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", format_bytes(s, sizeof(s), sum_padding));
1896
1897 r = table_add_many(
1898 t,
1899 TABLE_EMPTY,
1900 TABLE_EMPTY,
1901 TABLE_EMPTY,
1902 TABLE_EMPTY,
1903 TABLE_EMPTY,
1904 TABLE_EMPTY,
1905 TABLE_EMPTY,
a015fbe7 1906 TABLE_EMPTY,
e594a3b1
LP
1907 TABLE_STRING, a,
1908 TABLE_EMPTY,
a015fbe7
TH
1909 TABLE_EMPTY,
1910 TABLE_STRING, b,
1911 TABLE_EMPTY);
e594a3b1 1912 if (r < 0)
f987a261 1913 return table_log_add_error(r);
e594a3b1
LP
1914 }
1915
896e678b 1916 return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
e594a3b1
LP
1917}
1918
1919static void context_bar_char_process_partition(
1920 Context *context,
1921 Partition *bar[],
1922 size_t n,
1923 Partition *p,
1924 size_t *ret_start) {
1925
1926 uint64_t from, to, total;
1927 size_t x, y;
1928
1929 assert(context);
1930 assert(bar);
1931 assert(n > 0);
1932 assert(p);
1933
1934 if (p->dropped)
1935 return;
1936
1937 assert(p->offset != UINT64_MAX);
1938 assert(p->new_size != UINT64_MAX);
1939
1940 from = p->offset;
1941 to = from + p->new_size;
1942
1943 assert(context->end >= context->start);
1944 total = context->end - context->start;
1945
1946 assert(from >= context->start);
1947 assert(from <= context->end);
1948 x = (from - context->start) * n / total;
1949
1950 assert(to >= context->start);
1951 assert(to <= context->end);
1952 y = (to - context->start) * n / total;
1953
1954 assert(x <= y);
1955 assert(y <= n);
1956
1957 for (size_t i = x; i < y; i++)
1958 bar[i] = p;
1959
1960 *ret_start = x;
1961}
1962
1963static int partition_hint(const Partition *p, const char *node, char **ret) {
1964 _cleanup_free_ char *buf = NULL;
1965 char ids[ID128_UUID_STRING_MAX];
1966 const char *label;
1967 sd_id128_t id;
1968
1969 /* Tries really hard to find a suitable description for this partition */
1970
1971 if (p->definition_path) {
1972 buf = strdup(basename(p->definition_path));
1973 goto done;
1974 }
1975
1976 label = partition_label(p);
1977 if (!isempty(label)) {
1978 buf = strdup(label);
1979 goto done;
1980 }
1981
1982 if (p->partno != UINT64_MAX) {
1983 buf = fdisk_partname(node, p->partno+1);
1984 goto done;
1985 }
1986
1987 if (!sd_id128_is_null(p->new_uuid))
1988 id = p->new_uuid;
1989 else if (!sd_id128_is_null(p->current_uuid))
1990 id = p->current_uuid;
1991 else
1992 id = p->type_uuid;
1993
1994 buf = strdup(id128_to_uuid_string(id, ids));
1995
1996done:
1997 if (!buf)
1998 return -ENOMEM;
1999
2000 *ret = TAKE_PTR(buf);
2001 return 0;
2002}
2003
2004static int context_dump_partition_bar(Context *context, const char *node) {
2005 _cleanup_free_ Partition **bar = NULL;
2006 _cleanup_free_ size_t *start_array = NULL;
2007 Partition *p, *last = NULL;
2008 bool z = false;
2009 size_t c, j = 0;
2010
f391597c 2011 assert_se((c = columns()) >= 2);
e594a3b1
LP
2012 c -= 2; /* We do not use the leftmost and rightmost character cell */
2013
2014 bar = new0(Partition*, c);
2015 if (!bar)
2016 return log_oom();
2017
2018 start_array = new(size_t, context->n_partitions);
2019 if (!start_array)
2020 return log_oom();
2021
2022 LIST_FOREACH(partitions, p, context->partitions)
2023 context_bar_char_process_partition(context, bar, c, p, start_array + j++);
2024
2025 putc(' ', stdout);
2026
2027 for (size_t i = 0; i < c; i++) {
2028 if (bar[i]) {
2029 if (last != bar[i])
2030 z = !z;
2031
2032 fputs(z ? ansi_green() : ansi_yellow(), stdout);
2033 fputs(special_glyph(SPECIAL_GLYPH_DARK_SHADE), stdout);
2034 } else {
2035 fputs(ansi_normal(), stdout);
2036 fputs(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE), stdout);
2037 }
2038
2039 last = bar[i];
2040 }
2041
2042 fputs(ansi_normal(), stdout);
2043 putc('\n', stdout);
2044
2045 for (size_t i = 0; i < context->n_partitions; i++) {
2046 _cleanup_free_ char **line = NULL;
2047
2048 line = new0(char*, c);
2049 if (!line)
2050 return log_oom();
2051
2052 j = 0;
2053 LIST_FOREACH(partitions, p, context->partitions) {
2054 _cleanup_free_ char *d = NULL;
2055 j++;
2056
2057 if (i < context->n_partitions - j) {
2058
2059 if (line[start_array[j-1]]) {
2060 const char *e;
2061
2062 /* Upgrade final corner to the right with a branch to the right */
2063 e = startswith(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_RIGHT));
2064 if (e) {
2065 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), e);
2066 if (!d)
2067 return log_oom();
2068 }
2069 }
2070
2071 if (!d) {
2072 d = strdup(special_glyph(SPECIAL_GLYPH_TREE_VERTICAL));
2073 if (!d)
2074 return log_oom();
2075 }
2076
2077 } else if (i == context->n_partitions - j) {
2078 _cleanup_free_ char *hint = NULL;
2079
2080 (void) partition_hint(p, node, &hint);
2081
2082 if (streq_ptr(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_VERTICAL)))
2083 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), " ", strna(hint));
2084 else
2085 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_RIGHT), " ", strna(hint));
2086
2087 if (!d)
2088 return log_oom();
2089 }
2090
2091 if (d)
2092 free_and_replace(line[start_array[j-1]], d);
2093 }
2094
2095 putc(' ', stdout);
2096
2097 j = 0;
2098 while (j < c) {
2099 if (line[j]) {
2100 fputs(line[j], stdout);
2101 j += utf8_console_width(line[j]);
2102 } else {
2103 putc(' ', stdout);
2104 j++;
2105 }
2106 }
2107
2108 putc('\n', stdout);
2109
2110 for (j = 0; j < c; j++)
2111 free(line[j]);
2112 }
2113
2114 return 0;
2115}
2116
2117static bool context_changed(const Context *context) {
2118 Partition *p;
2119
2120 LIST_FOREACH(partitions, p, context->partitions) {
2121 if (p->dropped)
2122 continue;
2123
2124 if (p->allocated_to_area)
2125 return true;
2126
2127 if (p->new_size != p->current_size)
2128 return true;
2129 }
2130
2131 return false;
2132}
2133
81873a6b 2134static int context_wipe_range(Context *context, uint64_t offset, uint64_t size) {
e594a3b1
LP
2135 _cleanup_(blkid_free_probep) blkid_probe probe = NULL;
2136 int r;
2137
2138 assert(context);
81873a6b
LP
2139 assert(offset != UINT64_MAX);
2140 assert(size != UINT64_MAX);
e594a3b1
LP
2141
2142 probe = blkid_new_probe();
2143 if (!probe)
2144 return log_oom();
2145
e594a3b1 2146 errno = 0;
81873a6b 2147 r = blkid_probe_set_device(probe, fdisk_get_devfd(context->fdisk_context), offset, size);
e594a3b1 2148 if (r < 0)
81873a6b 2149 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to allocate device probe for wiping.");
e594a3b1
LP
2150
2151 errno = 0;
2152 if (blkid_probe_enable_superblocks(probe, true) < 0 ||
2153 blkid_probe_set_superblocks_flags(probe, BLKID_SUBLKS_MAGIC|BLKID_SUBLKS_BADCSUM) < 0 ||
2154 blkid_probe_enable_partitions(probe, true) < 0 ||
2155 blkid_probe_set_partitions_flags(probe, BLKID_PARTS_MAGIC) < 0)
81873a6b 2156 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to enable superblock and partition probing.");
e594a3b1
LP
2157
2158 for (;;) {
2159 errno = 0;
2160 r = blkid_do_probe(probe);
2161 if (r < 0)
2162 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe for file systems.");
2163 if (r > 0)
2164 break;
2165
2166 errno = 0;
2167 if (blkid_do_wipe(probe, false) < 0)
2168 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to wipe file system signature.");
2169 }
2170
e594a3b1
LP
2171 return 0;
2172}
2173
81873a6b
LP
2174static int context_wipe_partition(Context *context, Partition *p) {
2175 int r;
2176
2177 assert(context);
2178 assert(p);
2179 assert(!PARTITION_EXISTS(p)); /* Safety check: never wipe existing partitions */
2180
2181 assert(p->offset != UINT64_MAX);
2182 assert(p->new_size != UINT64_MAX);
2183
2184 r = context_wipe_range(context, p->offset, p->new_size);
2185 if (r < 0)
2186 return r;
2187
2188 log_info("Successfully wiped file system signatures from future partition %" PRIu64 ".", p->partno);
2189 return 0;
2190}
2191
2192static int context_discard_range(
2193 Context *context,
2194 uint64_t offset,
2195 uint64_t size) {
2196
e594a3b1
LP
2197 struct stat st;
2198 int fd;
2199
2200 assert(context);
2201 assert(offset != UINT64_MAX);
2202 assert(size != UINT64_MAX);
2203
2204 if (size <= 0)
2205 return 0;
2206
a26f4a49 2207 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
e594a3b1
LP
2208
2209 if (fstat(fd, &st) < 0)
2210 return -errno;
2211
2212 if (S_ISREG(st.st_mode)) {
2213 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, offset, size) < 0) {
2214 if (ERRNO_IS_NOT_SUPPORTED(errno))
2215 return -EOPNOTSUPP;
2216
2217 return -errno;
2218 }
2219
2220 return 1;
2221 }
2222
2223 if (S_ISBLK(st.st_mode)) {
2224 uint64_t range[2], end;
2225
2226 range[0] = round_up_size(offset, 512);
2227
2228 end = offset + size;
2229 if (end <= range[0])
2230 return 0;
2231
2232 range[1] = round_down_size(end - range[0], 512);
2233 if (range[1] <= 0)
2234 return 0;
2235
2236 if (ioctl(fd, BLKDISCARD, range) < 0) {
2237 if (ERRNO_IS_NOT_SUPPORTED(errno))
2238 return -EOPNOTSUPP;
2239
2240 return -errno;
2241 }
2242
2243 return 1;
2244 }
2245
2246 return -EOPNOTSUPP;
2247}
2248
2249static int context_discard_partition(Context *context, Partition *p) {
2250 int r;
2251
2252 assert(context);
2253 assert(p);
2254
2255 assert(p->offset != UINT64_MAX);
2256 assert(p->new_size != UINT64_MAX);
2257 assert(!PARTITION_EXISTS(p)); /* Safety check: never discard existing partitions */
2258
2259 if (!arg_discard)
2260 return 0;
2261
2262 r = context_discard_range(context, p->offset, p->new_size);
2263 if (r == -EOPNOTSUPP) {
5b5109e2 2264 log_info("Storage does not support discard, not discarding data in future partition %" PRIu64 ".", p->partno);
e594a3b1
LP
2265 return 0;
2266 }
2267 if (r == 0) {
2268 log_info("Partition %" PRIu64 " too short for discard, skipping.", p->partno);
2269 return 0;
2270 }
2271 if (r < 0)
5b5109e2 2272 return log_error_errno(r, "Failed to discard data for future partition %" PRIu64 ".", p->partno);
e594a3b1 2273
5b5109e2 2274 log_info("Successfully discarded data from future partition %" PRIu64 ".", p->partno);
e594a3b1
LP
2275 return 1;
2276}
2277
2278static int context_discard_gap_after(Context *context, Partition *p) {
2279 uint64_t gap, next = UINT64_MAX;
2280 Partition *q;
2281 int r;
2282
2283 assert(context);
2284 assert(!p || (p->offset != UINT64_MAX && p->new_size != UINT64_MAX));
2285
2286 if (p)
2287 gap = p->offset + p->new_size;
2288 else
2289 gap = context->start;
2290
2291 LIST_FOREACH(partitions, q, context->partitions) {
2292 if (q->dropped)
2293 continue;
2294
2295 assert(q->offset != UINT64_MAX);
2296 assert(q->new_size != UINT64_MAX);
2297
2298 if (q->offset < gap)
2299 continue;
2300
2301 if (next == UINT64_MAX || q->offset < next)
2302 next = q->offset;
2303 }
2304
2305 if (next == UINT64_MAX) {
2306 next = context->end;
2307 if (gap > next)
2308 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end.");
2309 }
2310
2311 assert(next >= gap);
2312 r = context_discard_range(context, gap, next - gap);
2313 if (r == -EOPNOTSUPP) {
2314 if (p)
5b5109e2 2315 log_info("Storage does not support discard, not discarding gap after partition %" PRIu64 ".", p->partno);
e594a3b1 2316 else
5b5109e2 2317 log_info("Storage does not support discard, not discarding gap at beginning of disk.");
e594a3b1
LP
2318 return 0;
2319 }
2320 if (r == 0) /* Too short */
2321 return 0;
2322 if (r < 0) {
2323 if (p)
2324 return log_error_errno(r, "Failed to discard gap after partition %" PRIu64 ".", p->partno);
2325 else
2326 return log_error_errno(r, "Failed to discard gap at beginning of disk.");
2327 }
2328
2329 if (p)
2330 log_info("Successfully discarded gap after partition %" PRIu64 ".", p->partno);
2331 else
2332 log_info("Successfully discarded gap at beginning of disk.");
2333
2334 return 0;
2335}
2336
2337static int context_wipe_and_discard(Context *context, bool from_scratch) {
2338 Partition *p;
2339 int r;
2340
2341 assert(context);
2342
2343 /* Wipe and discard the contents of all partitions we are about to create. We skip the discarding if
2344 * we were supposed to start from scratch anyway, as in that case we just discard the whole block
2345 * device in one go early on. */
2346
2347 LIST_FOREACH(partitions, p, context->partitions) {
2348
2349 if (!p->allocated_to_area)
2350 continue;
2351
e594a3b1
LP
2352 r = context_wipe_partition(context, p);
2353 if (r < 0)
2354 return r;
2355
2356 if (!from_scratch) {
f0cb1b95
LP
2357 r = context_discard_partition(context, p);
2358 if (r < 0)
2359 return r;
2360
e594a3b1
LP
2361 r = context_discard_gap_after(context, p);
2362 if (r < 0)
2363 return r;
2364 }
2365 }
2366
2367 if (!from_scratch) {
2368 r = context_discard_gap_after(context, NULL);
2369 if (r < 0)
2370 return r;
2371 }
2372
2373 return 0;
2374}
2375
b9df3536
LP
2376static int partition_encrypt(
2377 Partition *p,
2378 const char *node,
2379 struct crypt_device **ret_cd,
2380 char **ret_volume,
2381 int *ret_fd) {
3dd8ae5c 2382#if HAVE_LIBCRYPTSETUP
0d12936d 2383 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
b9df3536
LP
2384 _cleanup_(erase_and_freep) void *volume_key = NULL;
2385 _cleanup_free_ char *dm_name = NULL, *vol = NULL;
2386 char suuid[ID128_UUID_STRING_MAX];
2387 size_t volume_key_size = 256 / 8;
2388 sd_id128_t uuid;
2389 int r;
2390
2391 assert(p);
889914ef
LP
2392 assert(p->encrypt != ENCRYPT_OFF);
2393
2394 log_debug("Encryption mode for partition %" PRIu64 ": %s", p->partno, encrypt_mode_to_string(p->encrypt));
b9df3536 2395
0d12936d
LP
2396 r = dlopen_cryptsetup();
2397 if (r < 0)
2398 return log_error_errno(r, "libcryptsetup not found, cannot encrypt: %m");
2399
b9df3536
LP
2400 if (asprintf(&dm_name, "luks-repart-%08" PRIx64, random_u64()) < 0)
2401 return log_oom();
2402
2403 if (ret_volume) {
2404 vol = path_join("/dev/mapper/", dm_name);
2405 if (!vol)
2406 return log_oom();
2407 }
2408
2409 r = derive_uuid(p->new_uuid, "luks-uuid", &uuid);
2410 if (r < 0)
2411 return r;
2412
2413 log_info("Encrypting future partition %" PRIu64 "...", p->partno);
2414
2415 volume_key = malloc(volume_key_size);
2416 if (!volume_key)
2417 return log_oom();
2418
2419 r = genuine_random_bytes(volume_key, volume_key_size, RANDOM_BLOCK);
2420 if (r < 0)
2421 return log_error_errno(r, "Failed to generate volume key: %m");
2422
0d12936d 2423 r = sym_crypt_init(&cd, node);
b9df3536
LP
2424 if (r < 0)
2425 return log_error_errno(r, "Failed to allocate libcryptsetup context: %m");
2426
2427 cryptsetup_enable_logging(cd);
2428
0d12936d 2429 r = sym_crypt_format(cd,
b9df3536
LP
2430 CRYPT_LUKS2,
2431 "aes",
2432 "xts-plain64",
2433 id128_to_uuid_string(uuid, suuid),
2434 volume_key,
2435 volume_key_size,
2436 &(struct crypt_params_luks2) {
2437 .label = p->new_label,
2438 .sector_size = 512U,
2439 });
2440 if (r < 0)
2441 return log_error_errno(r, "Failed to LUKS2 format future partition: %m");
2442
889914ef
LP
2443 if (IN_SET(p->encrypt, ENCRYPT_KEY_FILE, ENCRYPT_KEY_FILE_TPM2)) {
2444 r = sym_crypt_keyslot_add_by_volume_key(
2445 cd,
2446 CRYPT_ANY_SLOT,
2447 volume_key,
2448 volume_key_size,
2449 strempty(arg_key),
2450 arg_key_size);
2451 if (r < 0)
2452 return log_error_errno(r, "Failed to add LUKS2 key: %m");
2453 }
2454
2455 if (IN_SET(p->encrypt, ENCRYPT_TPM2, ENCRYPT_KEY_FILE_TPM2)) {
2456#if HAVE_TPM2
2457 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
2458 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
2459 _cleanup_(erase_and_freep) void *secret = NULL;
2460 _cleanup_free_ void *blob = NULL, *hash = NULL;
2461 size_t secret_size, blob_size, hash_size;
2462 int keyslot;
2463
2464 r = tpm2_seal(arg_tpm2_device, arg_tpm2_pcr_mask, &secret, &secret_size, &blob, &blob_size, &hash, &hash_size);
2465 if (r < 0)
2466 return log_error_errno(r, "Failed to seal to TPM2: %m");
2467
2468 r = base64mem(secret, secret_size, &base64_encoded);
2469 if (r < 0)
2470 return log_error_errno(r, "Failed to base64 encode secret key: %m");
2471
2472 r = cryptsetup_set_minimal_pbkdf(cd);
2473 if (r < 0)
2474 return log_error_errno(r, "Failed to set minimal PBKDF: %m");
2475
2476 keyslot = sym_crypt_keyslot_add_by_volume_key(
2477 cd,
2478 CRYPT_ANY_SLOT,
2479 volume_key,
2480 volume_key_size,
2481 base64_encoded,
2482 strlen(base64_encoded));
2483 if (keyslot < 0)
2484 return log_error_errno(keyslot, "Failed to add new TPM2 key to %s: %m", node);
2485
2486 r = tpm2_make_luks2_json(keyslot, arg_tpm2_pcr_mask, blob, blob_size, hash, hash_size, &v);
2487 if (r < 0)
2488 return log_error_errno(r, "Failed to prepare TPM2 JSON token object: %m");
2489
2490 r = cryptsetup_add_token_json(cd, v);
2491 if (r < 0)
2492 return log_error_errno(r, "Failed to add TPM2 JSON token to LUKS2 header: %m");
2493#else
2494 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
2495 "Support for TPM2 enrollment not enabled.");
2496#endif
2497 }
b9df3536 2498
0d12936d 2499 r = sym_crypt_activate_by_volume_key(
b9df3536
LP
2500 cd,
2501 dm_name,
2502 volume_key,
2503 volume_key_size,
2504 arg_discard ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0);
2505 if (r < 0)
2506 return log_error_errno(r, "Failed to activate LUKS superblock: %m");
2507
2508 log_info("Successfully encrypted future partition %" PRIu64 ".", p->partno);
2509
2510 if (ret_fd) {
2511 _cleanup_close_ int dev_fd = -1;
2512
2513 dev_fd = open(vol, O_RDWR|O_CLOEXEC|O_NOCTTY);
2514 if (dev_fd < 0)
2515 return log_error_errno(errno, "Failed to open LUKS volume '%s': %m", vol);
2516
2517 *ret_fd = TAKE_FD(dev_fd);
2518 }
2519
2520 if (ret_cd)
2521 *ret_cd = TAKE_PTR(cd);
2522 if (ret_volume)
2523 *ret_volume = TAKE_PTR(vol);
2524
2525 return 0;
3dd8ae5c 2526#else
2527 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libcryptsetup is not supported, cannot encrypt: %m");
2528#endif
b9df3536
LP
2529}
2530
2531static int deactivate_luks(struct crypt_device *cd, const char *node) {
3dd8ae5c 2532#if HAVE_LIBCRYPTSETUP
b9df3536
LP
2533 int r;
2534
2535 if (!cd)
2536 return 0;
2537
2538 assert(node);
2539
2540 /* udev or so might access out block device in the background while we are done. Let's hence force
2541 * detach the volume. We sync'ed before, hence this should be safe. */
2542
0d12936d 2543 r = sym_crypt_deactivate_by_name(cd, basename(node), CRYPT_DEACTIVATE_FORCE);
b9df3536
LP
2544 if (r < 0)
2545 return log_error_errno(r, "Failed to deactivate LUKS device: %m");
2546
2547 return 1;
3dd8ae5c 2548#else
2549 return 0;
2550#endif
b9df3536
LP
2551}
2552
757bc2e4
LP
2553static int context_copy_blocks(Context *context) {
2554 Partition *p;
b9df3536 2555 int whole_fd = -1, r;
757bc2e4
LP
2556
2557 assert(context);
2558
2559 /* Copy in file systems on the block level */
2560
2561 LIST_FOREACH(partitions, p, context->partitions) {
0d12936d 2562 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
b9df3536
LP
2563 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
2564 _cleanup_free_ char *encrypted = NULL;
2565 _cleanup_close_ int encrypted_dev_fd = -1;
757bc2e4 2566 char buf[FORMAT_BYTES_MAX];
b9df3536 2567 int target_fd;
757bc2e4
LP
2568
2569 if (p->copy_blocks_fd < 0)
2570 continue;
2571
2572 if (p->dropped)
2573 continue;
2574
2575 if (PARTITION_EXISTS(p)) /* Never copy over existing partitions */
2576 continue;
2577
2578 assert(p->new_size != UINT64_MAX);
2579 assert(p->copy_blocks_size != UINT64_MAX);
2580 assert(p->new_size >= p->copy_blocks_size);
2581
b9df3536
LP
2582 if (whole_fd < 0)
2583 assert_se((whole_fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
2584
889914ef 2585 if (p->encrypt != ENCRYPT_OFF) {
b9df3536
LP
2586 r = loop_device_make(whole_fd, O_RDWR, p->offset, p->new_size, 0, &d);
2587 if (r < 0)
2588 return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno);
2589
2590 r = loop_device_flock(d, LOCK_EX);
2591 if (r < 0)
2592 return log_error_errno(r, "Failed to lock loopback device: %m");
2593
2594 r = partition_encrypt(p, d->node, &cd, &encrypted, &encrypted_dev_fd);
2595 if (r < 0)
2596 return log_error_errno(r, "Failed to encrypt device: %m");
757bc2e4 2597
b9df3536
LP
2598 if (flock(encrypted_dev_fd, LOCK_EX) < 0)
2599 return log_error_errno(errno, "Failed to lock LUKS device: %m");
2600
2601 target_fd = encrypted_dev_fd;
2602 } else {
2603 if (lseek(whole_fd, p->offset, SEEK_SET) == (off_t) -1)
2604 return log_error_errno(errno, "Failed to seek to partition offset: %m");
2605
2606 target_fd = whole_fd;
2607 }
757bc2e4 2608
5b5109e2 2609 log_info("Copying in '%s' (%s) on block level into future partition %" PRIu64 ".", p->copy_blocks_path, format_bytes(buf, sizeof(buf), p->copy_blocks_size), p->partno);
757bc2e4 2610
b9df3536 2611 r = copy_bytes_full(p->copy_blocks_fd, target_fd, p->copy_blocks_size, 0, NULL, NULL, NULL, NULL);
757bc2e4
LP
2612 if (r < 0)
2613 return log_error_errno(r, "Failed to copy in data from '%s': %m", p->copy_blocks_path);
2614
b9df3536
LP
2615 if (fsync(target_fd) < 0)
2616 return log_error_errno(r, "Failed to synchronize copied data blocks: %m");
2617
889914ef 2618 if (p->encrypt != ENCRYPT_OFF) {
b9df3536
LP
2619 encrypted_dev_fd = safe_close(encrypted_dev_fd);
2620
2621 r = deactivate_luks(cd, encrypted);
2622 if (r < 0)
2623 return r;
2624
0d12936d 2625 sym_crypt_free(cd);
b9df3536
LP
2626 cd = NULL;
2627
2628 r = loop_device_sync(d);
2629 if (r < 0)
2630 return log_error_errno(r, "Failed to sync loopback device: %m");
2631 }
2632
757bc2e4
LP
2633 log_info("Copying in of '%s' on block level completed.", p->copy_blocks_path);
2634 }
2635
2636 return 0;
2637}
2638
8a794850
LP
2639static int do_copy_files(Partition *p, const char *fs) {
2640 char **source, **target;
2641 int r;
2642
2643 assert(p);
2644 assert(fs);
2645
2646 STRV_FOREACH_PAIR(source, target, p->copy_files) {
2647 _cleanup_close_ int sfd = -1, pfd = -1, tfd = -1;
6020d00d 2648 _cleanup_free_ char *dn = NULL, *fn = NULL;
8a794850 2649
6020d00d
LP
2650 r = path_extract_directory(*target, &dn);
2651 if (r < 0)
2652 return log_error_errno(r, "Failed to extract directory from '%s': %m", *target);
2653
2654 r = path_extract_filename(*target, &fn);
2655 if (r < 0)
2656 return log_error_errno(r, "Failed to extract filename from '%s': %m", *target);
8a794850
LP
2657
2658 sfd = chase_symlinks_and_open(*source, arg_root, CHASE_PREFIX_ROOT|CHASE_WARN, O_CLOEXEC|O_NOCTTY, NULL);
2659 if (sfd < 0)
2660 return log_error_errno(sfd, "Failed to open source file '%s%s': %m", strempty(arg_root), *source);
2661
2662 r = fd_verify_regular(sfd);
2663 if (r < 0) {
2664 if (r != -EISDIR)
2665 return log_error_errno(r, "Failed to check type of source file '%s': %m", *source);
2666
2667 /* We are looking at a directory */
2668 tfd = chase_symlinks_and_open(*target, fs, CHASE_PREFIX_ROOT|CHASE_WARN, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
2669 if (tfd < 0) {
2670 if (tfd != -ENOENT)
2671 return log_error_errno(tfd, "Failed to open target directory '%s': %m", *target);
2672
2673 r = mkdir_p_root(fs, dn, UID_INVALID, GID_INVALID, 0755);
2674 if (r < 0)
2675 return log_error_errno(r, "Failed to create parent directory '%s': %m", dn);
2676
2677 pfd = chase_symlinks_and_open(dn, fs, CHASE_PREFIX_ROOT|CHASE_WARN, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
2678 if (pfd < 0)
2679 return log_error_errno(pfd, "Failed to open parent directory of target: %m");
2680
652d9040
LP
2681 r = copy_tree_at(
2682 sfd, ".",
6020d00d 2683 pfd, fn,
652d9040
LP
2684 UID_INVALID, GID_INVALID,
2685 COPY_REFLINK|COPY_MERGE|COPY_REPLACE|COPY_SIGINT|COPY_HARDLINKS);
8a794850 2686 } else
652d9040
LP
2687 r = copy_tree_at(
2688 sfd, ".",
2689 tfd, ".",
2690 UID_INVALID, GID_INVALID,
2691 COPY_REFLINK|COPY_MERGE|COPY_REPLACE|COPY_SIGINT|COPY_HARDLINKS);
8a794850
LP
2692 if (r < 0)
2693 return log_error_errno(r, "Failed to copy %s%s to %s: %m", strempty(arg_root), *source, *target);
2694 } else {
2695 /* We are looking at a regular file */
2696
2697 r = mkdir_p_root(fs, dn, UID_INVALID, GID_INVALID, 0755);
2698 if (r < 0)
2699 return log_error_errno(r, "Failed to create parent directory: %m");
2700
2701 pfd = chase_symlinks_and_open(dn, fs, CHASE_PREFIX_ROOT|CHASE_WARN, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
2702 if (pfd < 0)
a0ff9971 2703 return log_error_errno(pfd, "Failed to open parent directory of target: %m");
8a794850
LP
2704
2705 tfd = openat(pfd, basename(*target), O_CREAT|O_EXCL|O_WRONLY|O_CLOEXEC, 0700);
2706 if (tfd < 0)
2707 return log_error_errno(errno, "Failed to create target file '%s': %m", *target);
2708
2709 r = copy_bytes(sfd, tfd, UINT64_MAX, COPY_REFLINK|COPY_SIGINT);
2710 if (r < 0)
2711 return log_error_errno(r, "Failed to copy '%s%s' to '%s': %m", strempty(arg_root), *source, *target);
2712
2713 (void) copy_xattr(sfd, tfd);
2714 (void) copy_access(sfd, tfd);
2715 (void) copy_times(sfd, tfd, 0);
2716 }
2717 }
2718
2719 return 0;
2720}
2721
2722static int partition_copy_files(Partition *p, const char *node) {
2723 int r;
2724
2725 assert(p);
2726 assert(node);
2727
2728 if (strv_isempty(p->copy_files))
2729 return 0;
2730
2731 log_info("Populating partition %" PRIu64 " with files.", p->partno);
2732
2733 /* We copy in a child process, since we have to mount the fs for that, and we don't want that fs to
2734 * appear in the host namespace. Hence we fork a child that has its own file system namespace and
2735 * detached mount propagation. */
2736
2737 r = safe_fork("(sd-copy)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, NULL);
2738 if (r < 0)
2739 return r;
2740 if (r == 0) {
2741 static const char fs[] = "/run/systemd/mount-root";
2742 /* This is a child process with its own mount namespace and propagation to host turned off */
2743
2744 r = mkdir_p(fs, 0700);
2745 if (r < 0) {
2746 log_error_errno(r, "Failed to create mount point: %m");
2747 _exit(EXIT_FAILURE);
2748 }
2749
511a8cfe 2750 if (mount_nofollow_verbose(LOG_ERR, node, fs, p->format, MS_NOATIME|MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL) < 0)
8a794850
LP
2751 _exit(EXIT_FAILURE);
2752
2753 if (do_copy_files(p, fs) < 0)
2754 _exit(EXIT_FAILURE);
2755
2756 r = syncfs_path(AT_FDCWD, fs);
2757 if (r < 0) {
2758 log_error_errno(r, "Failed to synchronize written files: %m");
2759 _exit(EXIT_FAILURE);
2760 }
2761
2762 _exit(EXIT_SUCCESS);
2763 }
2764
2765 log_info("Successfully populated partition %" PRIu64 " with files.", p->partno);
2766 return 0;
2767}
2768
53171c04
LP
2769static int context_mkfs(Context *context) {
2770 Partition *p;
2771 int fd = -1, r;
2772
2773 assert(context);
2774
2775 /* Make a file system */
2776
2777 LIST_FOREACH(partitions, p, context->partitions) {
0d12936d 2778 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
53171c04 2779 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
b9df3536
LP
2780 _cleanup_free_ char *encrypted = NULL;
2781 _cleanup_close_ int encrypted_dev_fd = -1;
2782 const char *fsdev;
53171c04
LP
2783 sd_id128_t fs_uuid;
2784
2785 if (p->dropped)
2786 continue;
2787
2788 if (PARTITION_EXISTS(p)) /* Never format existing partitions */
2789 continue;
2790
2791 if (!p->format)
2792 continue;
2793
2794 assert(p->offset != UINT64_MAX);
2795 assert(p->new_size != UINT64_MAX);
2796
2797 if (fd < 0)
2798 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
2799
2800 /* Loopback block devices are not only useful to turn regular files into block devices, but
2801 * also to cut out sections of block devices into new block devices. */
2802
2803 r = loop_device_make(fd, O_RDWR, p->offset, p->new_size, 0, &d);
2804 if (r < 0)
5b5109e2 2805 return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno);
53171c04
LP
2806
2807 r = loop_device_flock(d, LOCK_EX);
2808 if (r < 0)
2809 return log_error_errno(r, "Failed to lock loopback device: %m");
2810
889914ef 2811 if (p->encrypt != ENCRYPT_OFF) {
b9df3536
LP
2812 r = partition_encrypt(p, d->node, &cd, &encrypted, &encrypted_dev_fd);
2813 if (r < 0)
2814 return log_error_errno(r, "Failed to encrypt device: %m");
2815
2816 if (flock(encrypted_dev_fd, LOCK_EX) < 0)
2817 return log_error_errno(errno, "Failed to lock LUKS device: %m");
2818
2819 fsdev = encrypted;
2820 } else
2821 fsdev = d->node;
2822
53171c04
LP
2823 log_info("Formatting future partition %" PRIu64 ".", p->partno);
2824
2825 /* Calculate the UUID for the file system as HMAC-SHA256 of the string "file-system-uuid",
2826 * keyed off the partition UUID. */
2827 r = derive_uuid(p->new_uuid, "file-system-uuid", &fs_uuid);
2828 if (r < 0)
2829 return r;
2830
b9df3536
LP
2831 r = make_filesystem(fsdev, p->format, p->new_label, fs_uuid, arg_discard);
2832 if (r < 0) {
2833 encrypted_dev_fd = safe_close(encrypted_dev_fd);
2834 (void) deactivate_luks(cd, encrypted);
53171c04 2835 return r;
b9df3536 2836 }
53171c04
LP
2837
2838 log_info("Successfully formatted future partition %" PRIu64 ".", p->partno);
2839
b9df3536 2840 /* The file system is now created, no need to delay udev further */
889914ef 2841 if (p->encrypt != ENCRYPT_OFF)
b9df3536
LP
2842 if (flock(encrypted_dev_fd, LOCK_UN) < 0)
2843 return log_error_errno(errno, "Failed to unlock LUKS device: %m");
2844
2845 r = partition_copy_files(p, fsdev);
2846 if (r < 0) {
2847 encrypted_dev_fd = safe_close(encrypted_dev_fd);
2848 (void) deactivate_luks(cd, encrypted);
8a794850 2849 return r;
b9df3536
LP
2850 }
2851
2852 /* Note that we always sync explicitly here, since mkfs.fat doesn't do that on its own, and
2853 * if we don't sync before detaching a block device the in-flight sectors possibly won't hit
2854 * the disk. */
2855
889914ef 2856 if (p->encrypt != ENCRYPT_OFF) {
b9df3536
LP
2857 if (fsync(encrypted_dev_fd) < 0)
2858 return log_error_errno(r, "Failed to synchronize LUKS volume: %m");
2859 encrypted_dev_fd = safe_close(encrypted_dev_fd);
2860
2861 r = deactivate_luks(cd, encrypted);
2862 if (r < 0)
2863 return r;
2864
0d12936d 2865 sym_crypt_free(cd);
b9df3536
LP
2866 cd = NULL;
2867 }
8a794850 2868
53171c04
LP
2869 r = loop_device_sync(d);
2870 if (r < 0)
2871 return log_error_errno(r, "Failed to sync loopback device: %m");
2872 }
2873
2874 return 0;
2875}
2876
e594a3b1
LP
2877static int partition_acquire_uuid(Context *context, Partition *p, sd_id128_t *ret) {
2878 struct {
2879 sd_id128_t type_uuid;
2880 uint64_t counter;
2881 } _packed_ plaintext = {};
2882 union {
2883 unsigned char md[SHA256_DIGEST_LENGTH];
2884 sd_id128_t id;
2885 } result;
2886
2887 uint64_t k = 0;
2888 Partition *q;
2889 int r;
2890
2891 assert(context);
2892 assert(p);
2893 assert(ret);
2894
2895 /* Calculate a good UUID for the indicated partition. We want a certain degree of reproducibility,
2896 * hence we won't generate the UUIDs randomly. Instead we use a cryptographic hash (precisely:
2897 * HMAC-SHA256) to derive them from a single seed. The seed is generally the machine ID of the
2898 * installation we are processing, but if random behaviour is desired can be random, too. We use the
2899 * seed value as key for the HMAC (since the machine ID is something we generally don't want to leak)
2900 * and the partition type as plaintext. The partition type is suffixed with a counter (only for the
2901 * second and later partition of the same type) if we have more than one partition of the same
2902 * time. Or in other words:
2903 *
2904 * With:
2905 * SEED := /etc/machine-id
2906 *
2907 * If first partition instance of type TYPE_UUID:
2908 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID)
2909 *
2910 * For all later partition instances of type TYPE_UUID with INSTANCE being the LE64 encoded instance number:
2911 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID || INSTANCE)
2912 */
2913
2914 LIST_FOREACH(partitions, q, context->partitions) {
2915 if (p == q)
2916 break;
2917
2918 if (!sd_id128_equal(p->type_uuid, q->type_uuid))
2919 continue;
2920
2921 k++;
2922 }
2923
2924 plaintext.type_uuid = p->type_uuid;
2925 plaintext.counter = htole64(k);
2926
2927 if (!HMAC(EVP_sha256(),
2928 &context->seed, sizeof(context->seed),
2929 (const unsigned char*) &plaintext, k == 0 ? sizeof(sd_id128_t) : sizeof(plaintext),
2930 result.md, NULL))
2931 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "SHA256 calculation failed.");
2932
2933 /* Take the first half, mark it as v4 UUID */
2934 assert_cc(sizeof(result.md) == sizeof(result.id) * 2);
2935 result.id = id128_make_v4_uuid(result.id);
2936
2937 /* Ensure this partition UUID is actually unique, and there's no remaining partition from an earlier run? */
2938 LIST_FOREACH(partitions, q, context->partitions) {
2939 if (p == q)
2940 continue;
2941
2942 if (sd_id128_equal(q->current_uuid, result.id) ||
2943 sd_id128_equal(q->new_uuid, result.id)) {
2944 log_warning("Partition UUID calculated from seed for partition %" PRIu64 " exists already, reverting to randomized UUID.", p->partno);
2945
2946 r = sd_id128_randomize(&result.id);
2947 if (r < 0)
2948 return log_error_errno(r, "Failed to generate randomized UUID: %m");
2949
2950 break;
2951 }
2952 }
2953
2954 *ret = result.id;
2955 return 0;
2956}
2957
2958static int partition_acquire_label(Context *context, Partition *p, char **ret) {
2959 _cleanup_free_ char *label = NULL;
2960 const char *prefix;
2961 unsigned k = 1;
2962
2963 assert(context);
2964 assert(p);
2965 assert(ret);
2966
2967 prefix = gpt_partition_type_uuid_to_string(p->type_uuid);
2968 if (!prefix)
2969 prefix = "linux";
2970
2971 for (;;) {
2972 const char *ll = label ?: prefix;
2973 bool retry = false;
2974 Partition *q;
2975
2976 LIST_FOREACH(partitions, q, context->partitions) {
2977 if (p == q)
2978 break;
2979
2980 if (streq_ptr(ll, q->current_label) ||
2981 streq_ptr(ll, q->new_label)) {
2982 retry = true;
2983 break;
2984 }
2985 }
2986
2987 if (!retry)
2988 break;
2989
2990 label = mfree(label);
e594a3b1
LP
2991 if (asprintf(&label, "%s-%u", prefix, ++k) < 0)
2992 return log_oom();
2993 }
2994
2995 if (!label) {
2996 label = strdup(prefix);
2997 if (!label)
2998 return log_oom();
2999 }
3000
3001 *ret = TAKE_PTR(label);
3002 return 0;
3003}
3004
3005static int context_acquire_partition_uuids_and_labels(Context *context) {
3006 Partition *p;
3007 int r;
3008
3009 assert(context);
3010
3011 LIST_FOREACH(partitions, p, context->partitions) {
e594a3b1
LP
3012 /* Never touch foreign partitions */
3013 if (PARTITION_IS_FOREIGN(p)) {
3014 p->new_uuid = p->current_uuid;
3015
3016 if (p->current_label) {
12963533 3017 free(p->new_label);
e594a3b1
LP
3018 p->new_label = strdup(p->current_label);
3019 if (!p->new_label)
3020 return log_oom();
3021 }
3022
3023 continue;
3024 }
3025
3026 if (!sd_id128_is_null(p->current_uuid))
3027 p->new_uuid = p->current_uuid; /* Never change initialized UUIDs */
12963533
TH
3028 else if (sd_id128_is_null(p->new_uuid)) {
3029 /* Not explicitly set by user! */
e594a3b1
LP
3030 r = partition_acquire_uuid(context, p, &p->new_uuid);
3031 if (r < 0)
3032 return r;
3033 }
3034
3035 if (!isempty(p->current_label)) {
12963533 3036 free(p->new_label);
e594a3b1
LP
3037 p->new_label = strdup(p->current_label); /* never change initialized labels */
3038 if (!p->new_label)
3039 return log_oom();
12963533
TH
3040 } else if (!p->new_label) {
3041 /* Not explicitly set by user! */
3042
e594a3b1
LP
3043 r = partition_acquire_label(context, p, &p->new_label);
3044 if (r < 0)
3045 return r;
3046 }
3047 }
3048
3049 return 0;
3050}
3051
f28d4f42 3052static int context_mangle_partitions(Context *context) {
e594a3b1 3053 Partition *p;
f28d4f42 3054 int r;
e594a3b1
LP
3055
3056 assert(context);
3057
e594a3b1
LP
3058 LIST_FOREACH(partitions, p, context->partitions) {
3059 if (p->dropped)
3060 continue;
3061
3062 assert(p->new_size != UINT64_MAX);
3063 assert(p->offset != UINT64_MAX);
3064 assert(p->partno != UINT64_MAX);
3065
3066 if (PARTITION_EXISTS(p)) {
3067 bool changed = false;
3068
3069 assert(p->current_partition);
3070
3071 if (p->new_size != p->current_size) {
3072 assert(p->new_size >= p->current_size);
3073 assert(p->new_size % 512 == 0);
3074
3075 r = fdisk_partition_size_explicit(p->current_partition, true);
3076 if (r < 0)
3077 return log_error_errno(r, "Failed to enable explicit sizing: %m");
3078
3079 r = fdisk_partition_set_size(p->current_partition, p->new_size / 512);
3080 if (r < 0)
3081 return log_error_errno(r, "Failed to grow partition: %m");
3082
3083 log_info("Growing existing partition %" PRIu64 ".", p->partno);
3084 changed = true;
3085 }
3086
3087 if (!sd_id128_equal(p->new_uuid, p->current_uuid)) {
3088 char buf[ID128_UUID_STRING_MAX];
3089
3090 assert(!sd_id128_is_null(p->new_uuid));
3091
3092 r = fdisk_partition_set_uuid(p->current_partition, id128_to_uuid_string(p->new_uuid, buf));
3093 if (r < 0)
3094 return log_error_errno(r, "Failed to set partition UUID: %m");
3095
3096 log_info("Initializing UUID of existing partition %" PRIu64 ".", p->partno);
3097 changed = true;
3098 }
3099
3100 if (!streq_ptr(p->new_label, p->current_label)) {
3101 assert(!isempty(p->new_label));
3102
3103 r = fdisk_partition_set_name(p->current_partition, p->new_label);
3104 if (r < 0)
3105 return log_error_errno(r, "Failed to set partition label: %m");
3106
3107 log_info("Setting partition label of existing partition %" PRIu64 ".", p->partno);
3108 changed = true;
3109 }
3110
3111 if (changed) {
3112 assert(!PARTITION_IS_FOREIGN(p)); /* never touch foreign partitions */
3113
3114 r = fdisk_set_partition(context->fdisk_context, p->partno, p->current_partition);
3115 if (r < 0)
3116 return log_error_errno(r, "Failed to update partition: %m");
3117 }
3118 } else {
3119 _cleanup_(fdisk_unref_partitionp) struct fdisk_partition *q = NULL;
3120 _cleanup_(fdisk_unref_parttypep) struct fdisk_parttype *t = NULL;
3121 char ids[ID128_UUID_STRING_MAX];
3122
3123 assert(!p->new_partition);
3124 assert(p->offset % 512 == 0);
3125 assert(p->new_size % 512 == 0);
3126 assert(!sd_id128_is_null(p->new_uuid));
3127 assert(!isempty(p->new_label));
3128
3129 t = fdisk_new_parttype();
3130 if (!t)
3131 return log_oom();
3132
3133 r = fdisk_parttype_set_typestr(t, id128_to_uuid_string(p->type_uuid, ids));
3134 if (r < 0)
3135 return log_error_errno(r, "Failed to initialize partition type: %m");
3136
3137 q = fdisk_new_partition();
3138 if (!q)
3139 return log_oom();
3140
3141 r = fdisk_partition_set_type(q, t);
3142 if (r < 0)
3143 return log_error_errno(r, "Failed to set partition type: %m");
3144
3145 r = fdisk_partition_size_explicit(q, true);
3146 if (r < 0)
3147 return log_error_errno(r, "Failed to enable explicit sizing: %m");
3148
3149 r = fdisk_partition_set_start(q, p->offset / 512);
3150 if (r < 0)
3151 return log_error_errno(r, "Failed to position partition: %m");
3152
3153 r = fdisk_partition_set_size(q, p->new_size / 512);
3154 if (r < 0)
3155 return log_error_errno(r, "Failed to grow partition: %m");
3156
3157 r = fdisk_partition_set_partno(q, p->partno);
3158 if (r < 0)
3159 return log_error_errno(r, "Failed to set partition number: %m");
3160
3161 r = fdisk_partition_set_uuid(q, id128_to_uuid_string(p->new_uuid, ids));
3162 if (r < 0)
3163 return log_error_errno(r, "Failed to set partition UUID: %m");
3164
3165 r = fdisk_partition_set_name(q, p->new_label);
3166 if (r < 0)
3167 return log_error_errno(r, "Failed to set partition label: %m");
3168
5b5109e2 3169 log_info("Adding new partition %" PRIu64 " to partition table.", p->partno);
e594a3b1
LP
3170
3171 r = fdisk_add_partition(context->fdisk_context, q, NULL);
3172 if (r < 0)
3173 return log_error_errno(r, "Failed to add partition: %m");
3174
3175 assert(!p->new_partition);
3176 p->new_partition = TAKE_PTR(q);
3177 }
3178 }
3179
f28d4f42
LP
3180 return 0;
3181}
3182
3183static int context_write_partition_table(
3184 Context *context,
3185 const char *node,
3186 bool from_scratch) {
3187
3188 _cleanup_(fdisk_unref_tablep) struct fdisk_table *original_table = NULL;
3189 int capable, r;
3190
3191 assert(context);
3192
3193 if (arg_pretty > 0 ||
3194 (arg_pretty < 0 && isatty(STDOUT_FILENO) > 0) ||
6a01ea4a 3195 !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF)) {
f28d4f42
LP
3196
3197 (void) context_dump_partitions(context, node);
3198
3199 putc('\n', stdout);
3200
6a01ea4a 3201 if (arg_json_format_flags & JSON_FORMAT_OFF)
f28d4f42
LP
3202 (void) context_dump_partition_bar(context, node);
3203 putc('\n', stdout);
3204 fflush(stdout);
3205 }
3206
3207 if (!from_scratch && !context_changed(context)) {
3208 log_info("No changes.");
3209 return 0;
3210 }
3211
3212 if (arg_dry_run) {
3213 log_notice("Refusing to repartition, please re-run with --dry-run=no.");
3214 return 0;
3215 }
3216
3217 log_info("Applying changes.");
3218
3219 if (from_scratch) {
81873a6b
LP
3220 r = context_wipe_range(context, 0, context->total);
3221 if (r < 0)
3222 return r;
3223
3224 log_info("Wiped block device.");
3225
f28d4f42
LP
3226 r = context_discard_range(context, 0, context->total);
3227 if (r == -EOPNOTSUPP)
5b5109e2 3228 log_info("Storage does not support discard, not discarding entire block device data.");
f28d4f42
LP
3229 else if (r < 0)
3230 return log_error_errno(r, "Failed to discard entire block device: %m");
3231 else if (r > 0)
3232 log_info("Discarded entire block device.");
3233 }
3234
3235 r = fdisk_get_partitions(context->fdisk_context, &original_table);
3236 if (r < 0)
3237 return log_error_errno(r, "Failed to acquire partition table: %m");
3238
3239 /* Wipe fs signatures and discard sectors where the new partitions are going to be placed and in the
3240 * gaps between partitions, just to be sure. */
3241 r = context_wipe_and_discard(context, from_scratch);
3242 if (r < 0)
3243 return r;
3244
3245 r = context_copy_blocks(context);
3246 if (r < 0)
3247 return r;
3248
3249 r = context_mkfs(context);
3250 if (r < 0)
3251 return r;
3252
3253 r = context_mangle_partitions(context);
3254 if (r < 0)
3255 return r;
3256
e594a3b1
LP
3257 log_info("Writing new partition table.");
3258
3259 r = fdisk_write_disklabel(context->fdisk_context);
3260 if (r < 0)
3261 return log_error_errno(r, "Failed to write partition table: %m");
3262
911ba624 3263 capable = blockdev_partscan_enabled(fdisk_get_devfd(context->fdisk_context));
9a1deb85
LP
3264 if (capable == -ENOTBLK)
3265 log_debug("Not telling kernel to reread partition table, since we are not operating on a block device.");
3266 else if (capable < 0)
911ba624 3267 return log_error_errno(capable, "Failed to check if block device supports partition scanning: %m");
9a1deb85 3268 else if (capable > 0) {
e594a3b1
LP
3269 log_info("Telling kernel to reread partition table.");
3270
3271 if (from_scratch)
3272 r = fdisk_reread_partition_table(context->fdisk_context);
3273 else
3274 r = fdisk_reread_changes(context->fdisk_context, original_table);
3275 if (r < 0)
3276 return log_error_errno(r, "Failed to reread partition table: %m");
3277 } else
3278 log_notice("Not telling kernel to reread partition table, because selected image does not support kernel partition block devices.");
3279
3280 log_info("All done.");
3281
3282 return 0;
3283}
3284
3285static int context_read_seed(Context *context, const char *root) {
3286 int r;
3287
3288 assert(context);
3289
3290 if (!sd_id128_is_null(context->seed))
3291 return 0;
3292
3293 if (!arg_randomize) {
3294 _cleanup_close_ int fd = -1;
3295
3296 fd = chase_symlinks_and_open("/etc/machine-id", root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC, NULL);
3297 if (fd == -ENOENT)
3298 log_info("No machine ID set, using randomized partition UUIDs.");
3299 else if (fd < 0)
3300 return log_error_errno(fd, "Failed to determine machine ID of image: %m");
3301 else {
448b782c 3302 r = id128_read_fd(fd, ID128_PLAIN_OR_UNINIT, &context->seed);
e594a3b1
LP
3303 if (r == -ENOMEDIUM)
3304 log_info("No machine ID set, using randomized partition UUIDs.");
3305 else if (r < 0)
3306 return log_error_errno(r, "Failed to parse machine ID of image: %m");
3307
3308 return 0;
3309 }
3310 }
3311
3312 r = sd_id128_randomize(&context->seed);
3313 if (r < 0)
3314 return log_error_errno(r, "Failed to generate randomized seed: %m");
3315
3316 return 0;
3317}
3318
3319static int context_factory_reset(Context *context, bool from_scratch) {
3320 Partition *p;
3321 size_t n = 0;
3322 int r;
3323
3324 assert(context);
3325
3326 if (arg_factory_reset <= 0)
3327 return 0;
3328
3329 if (from_scratch) /* Nothing to reset if we start from scratch */
3330 return 0;
3331
3332 if (arg_dry_run) {
3333 log_notice("Refusing to factory reset, please re-run with --dry-run=no.");
3334 return 0;
3335 }
3336
3337 log_info("Applying factory reset.");
3338
3339 LIST_FOREACH(partitions, p, context->partitions) {
3340
3341 if (!p->factory_reset || !PARTITION_EXISTS(p))
3342 continue;
3343
3344 assert(p->partno != UINT64_MAX);
3345
3346 log_info("Removing partition %" PRIu64 " for factory reset.", p->partno);
3347
3348 r = fdisk_delete_partition(context->fdisk_context, p->partno);
3349 if (r < 0)
3350 return log_error_errno(r, "Failed to remove partition %" PRIu64 ": %m", p->partno);
3351
3352 n++;
3353 }
3354
3355 if (n == 0) {
3356 log_info("Factory reset requested, but no partitions to delete found.");
3357 return 0;
3358 }
3359
3360 r = fdisk_write_disklabel(context->fdisk_context);
3361 if (r < 0)
3362 return log_error_errno(r, "Failed to write disk label: %m");
3363
3364 log_info("Successfully deleted %zu partitions.", n);
3365 return 1;
3366}
3367
3368static int context_can_factory_reset(Context *context) {
3369 Partition *p;
3370
3371 assert(context);
3372
3373 LIST_FOREACH(partitions, p, context->partitions)
3374 if (p->factory_reset && PARTITION_EXISTS(p))
3375 return true;
3376
3377 return false;
3378}
3379
757bc2e4
LP
3380static int context_open_copy_block_paths(Context *context) {
3381 Partition *p;
3382 int r;
3383
3384 assert(context);
3385
3386 LIST_FOREACH(partitions, p, context->partitions) {
3387 _cleanup_close_ int source_fd = -1;
3388 uint64_t size;
3389 struct stat st;
3390
3391 assert(p->copy_blocks_fd < 0);
3392 assert(p->copy_blocks_size == UINT64_MAX);
3393
3394 if (PARTITION_EXISTS(p)) /* Never copy over partitions that already exist! */
3395 continue;
3396
3397 if (!p->copy_blocks_path)
3398 continue;
3399
3400 source_fd = open(p->copy_blocks_path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
3401 if (source_fd < 0)
3402 return log_error_errno(errno, "Failed to open block copy file '%s': %m", p->copy_blocks_path);
3403
3404 if (fstat(source_fd, &st) < 0)
3405 return log_error_errno(errno, "Failed to stat block copy file '%s': %m", p->copy_blocks_path);
3406
3407 if (S_ISDIR(st.st_mode)) {
3408 _cleanup_free_ char *bdev = NULL;
3409
3410 /* If the file is a directory, automatically find the backing block device */
3411
3412 if (major(st.st_dev) != 0)
3413 r = device_path_make_major_minor(S_IFBLK, st.st_dev, &bdev);
3414 else {
3415 dev_t devt;
3416
3417 /* Special support for btrfs */
3418
3419 r = btrfs_get_block_device_fd(source_fd, &devt);
67f0ac8c
LP
3420 if (r == -EUCLEAN)
3421 return btrfs_log_dev_root(LOG_ERR, r, p->copy_blocks_path);
757bc2e4
LP
3422 if (r < 0)
3423 return log_error_errno(r, "Unable to determine backing block device of '%s': %m", p->copy_blocks_path);
3424
3425 r = device_path_make_major_minor(S_IFBLK, devt, &bdev);
3426 }
3427 if (r < 0)
3428 return log_error_errno(r, "Failed to determine block device path for block device backing '%s': %m", p->copy_blocks_path);
3429
3430 safe_close(source_fd);
3431
3432 source_fd = open(bdev, O_RDONLY|O_CLOEXEC|O_NOCTTY);
3433 if (source_fd < 0)
3434 return log_error_errno(errno, "Failed to open block device '%s': %m", bdev);
3435
3436 if (fstat(source_fd, &st) < 0)
3437 return log_error_errno(errno, "Failed to stat block device '%s': %m", bdev);
3438
3439 if (!S_ISBLK(st.st_mode))
3440 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Block device '%s' is not actually a block device, refusing.", bdev);
3441 }
3442
3443 if (S_ISREG(st.st_mode))
3444 size = st.st_size;
3445 else if (S_ISBLK(st.st_mode)) {
3446 if (ioctl(source_fd, BLKGETSIZE64, &size) != 0)
3447 return log_error_errno(errno, "Failed to determine size of block device to copy from: %m");
3448 } else
3449 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", p->copy_blocks_path);
3450
3451 if (size <= 0)
3452 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File to copy bytes from '%s' has zero size, refusing.", p->copy_blocks_path);
3453 if (size % 512 != 0)
3454 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File to copy bytes from '%s' has size that is not multiple of 512, refusing.", p->copy_blocks_path);
3455
3456 p->copy_blocks_fd = TAKE_FD(source_fd);
3457 p->copy_blocks_size = size;
3458 }
3459
3460 return 0;
3461}
3462
e594a3b1
LP
3463static int help(void) {
3464 _cleanup_free_ char *link = NULL;
3465 int r;
3466
3467 r = terminal_urlify_man("systemd-repart", "1", &link);
3468 if (r < 0)
3469 return log_oom();
3470
3471 printf("%s [OPTIONS...] [DEVICE]\n"
3472 "\n%sGrow and add partitions to partition table.%s\n\n"
3473 " -h --help Show this help\n"
3474 " --version Show package version\n"
896e678b
LP
3475 " --no-pager Do not pipe output into a pager\n"
3476 " --no-legend Do not show the headers and footers\n"
e594a3b1 3477 " --dry-run=BOOL Whether to run dry-run operation\n"
a26f4a49
LP
3478 " --empty=MODE One of refuse, allow, require, force, create; controls\n"
3479 " how to handle empty disks lacking partition tables\n"
e594a3b1 3480 " --discard=BOOL Whether to discard backing blocks for new partitions\n"
2d2d0a57 3481 " --pretty=BOOL Whether to show pretty summary before doing changes\n"
e594a3b1
LP
3482 " --factory-reset=BOOL Whether to remove data partitions before recreating\n"
3483 " them\n"
3484 " --can-factory-reset Test whether factory reset is defined\n"
3485 " --root=PATH Operate relative to root path\n"
9d252fbb 3486 " --definitions=DIR Find partition definitions in specified directory\n"
b9df3536 3487 " --key-file=PATH Key to use when encrypting partitions\n"
889914ef
LP
3488 " --tpm2-device=PATH Path to TPM2 device node to use\n"
3489 " --tpm2-pcrs=PCR1,PCR2,…\n"
3490 " TPM2 PCR indexes to use for TPM2 enrollment\n"
e594a3b1 3491 " --seed=UUID 128bit seed UUID to derive all UUIDs from\n"
a26f4a49 3492 " --size=BYTES Grow loopback file to specified size\n"
2d2d0a57 3493 " --json=pretty|short|off\n"
de8231b0 3494 " Generate JSON output\n"
bc556335
DDM
3495 "\nSee the %s for details.\n",
3496 program_invocation_short_name,
3497 ansi_highlight(),
3498 ansi_normal(),
3499 link);
e594a3b1
LP
3500
3501 return 0;
3502}
3503
3504static int parse_argv(int argc, char *argv[]) {
3505
3506 enum {
3507 ARG_VERSION = 0x100,
896e678b
LP
3508 ARG_NO_PAGER,
3509 ARG_NO_LEGEND,
e594a3b1
LP
3510 ARG_DRY_RUN,
3511 ARG_EMPTY,
3512 ARG_DISCARD,
3513 ARG_FACTORY_RESET,
3514 ARG_CAN_FACTORY_RESET,
3515 ARG_ROOT,
3516 ARG_SEED,
3517 ARG_PRETTY,
3518 ARG_DEFINITIONS,
a26f4a49 3519 ARG_SIZE,
a015fbe7 3520 ARG_JSON,
b9df3536 3521 ARG_KEY_FILE,
889914ef
LP
3522 ARG_TPM2_DEVICE,
3523 ARG_TPM2_PCRS,
e594a3b1
LP
3524 };
3525
3526 static const struct option options[] = {
3527 { "help", no_argument, NULL, 'h' },
3528 { "version", no_argument, NULL, ARG_VERSION },
896e678b
LP
3529 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
3530 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
e594a3b1
LP
3531 { "dry-run", required_argument, NULL, ARG_DRY_RUN },
3532 { "empty", required_argument, NULL, ARG_EMPTY },
3533 { "discard", required_argument, NULL, ARG_DISCARD },
3534 { "factory-reset", required_argument, NULL, ARG_FACTORY_RESET },
3535 { "can-factory-reset", no_argument, NULL, ARG_CAN_FACTORY_RESET },
3536 { "root", required_argument, NULL, ARG_ROOT },
3537 { "seed", required_argument, NULL, ARG_SEED },
3538 { "pretty", required_argument, NULL, ARG_PRETTY },
3539 { "definitions", required_argument, NULL, ARG_DEFINITIONS },
a26f4a49 3540 { "size", required_argument, NULL, ARG_SIZE },
a015fbe7 3541 { "json", required_argument, NULL, ARG_JSON },
b9df3536 3542 { "key-file", required_argument, NULL, ARG_KEY_FILE },
889914ef
LP
3543 { "tpm2-device", required_argument, NULL, ARG_TPM2_DEVICE },
3544 { "tpm2-pcrs", required_argument, NULL, ARG_TPM2_PCRS },
e594a3b1
LP
3545 {}
3546 };
3547
a26f4a49 3548 int c, r, dry_run = -1;
e594a3b1
LP
3549
3550 assert(argc >= 0);
3551 assert(argv);
3552
3553 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
3554
3555 switch (c) {
3556
3557 case 'h':
3558 return help();
3559
3560 case ARG_VERSION:
3561 return version();
3562
896e678b
LP
3563 case ARG_NO_PAGER:
3564 arg_pager_flags |= PAGER_DISABLE;
3565 break;
3566
3567 case ARG_NO_LEGEND:
3568 arg_legend = false;
3569 break;
3570
e594a3b1 3571 case ARG_DRY_RUN:
599c7c54 3572 r = parse_boolean_argument("--dry-run=", optarg, &arg_dry_run);
e594a3b1 3573 if (r < 0)
599c7c54 3574 return r;
e594a3b1
LP
3575 break;
3576
3577 case ARG_EMPTY:
3578 if (isempty(optarg) || streq(optarg, "refuse"))
3579 arg_empty = EMPTY_REFUSE;
3580 else if (streq(optarg, "allow"))
3581 arg_empty = EMPTY_ALLOW;
3582 else if (streq(optarg, "require"))
3583 arg_empty = EMPTY_REQUIRE;
3584 else if (streq(optarg, "force"))
3585 arg_empty = EMPTY_FORCE;
a26f4a49
LP
3586 else if (streq(optarg, "create")) {
3587 arg_empty = EMPTY_CREATE;
3588
3589 if (dry_run < 0)
3590 dry_run = false; /* Imply --dry-run=no if we create the loopback file
3591 * anew. After all we cannot really break anyone's
3592 * partition tables that way. */
3593 } else
e594a3b1
LP
3594 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3595 "Failed to parse --empty= parameter: %s", optarg);
3596 break;
3597
3598 case ARG_DISCARD:
599c7c54 3599 r = parse_boolean_argument("--discard=", optarg, &arg_discard);
e594a3b1 3600 if (r < 0)
599c7c54 3601 return r;
e594a3b1
LP
3602 break;
3603
3604 case ARG_FACTORY_RESET:
c3470872 3605 r = parse_boolean_argument("--factory-reset=", optarg, NULL);
e594a3b1 3606 if (r < 0)
c3470872 3607 return r;
e594a3b1
LP
3608 arg_factory_reset = r;
3609 break;
3610
3611 case ARG_CAN_FACTORY_RESET:
3612 arg_can_factory_reset = true;
3613 break;
3614
3615 case ARG_ROOT:
614b022c 3616 r = parse_path_argument(optarg, false, &arg_root);
e594a3b1
LP
3617 if (r < 0)
3618 return r;
3619 break;
3620
3621 case ARG_SEED:
3622 if (isempty(optarg)) {
3623 arg_seed = SD_ID128_NULL;
3624 arg_randomize = false;
3625 } else if (streq(optarg, "random"))
3626 arg_randomize = true;
3627 else {
3628 r = sd_id128_from_string(optarg, &arg_seed);
3629 if (r < 0)
3630 return log_error_errno(r, "Failed to parse seed: %s", optarg);
3631
3632 arg_randomize = false;
3633 }
3634
3635 break;
3636
3637 case ARG_PRETTY:
c3470872 3638 r = parse_boolean_argument("--pretty=", optarg, NULL);
e594a3b1 3639 if (r < 0)
c3470872 3640 return r;
e594a3b1
LP
3641 arg_pretty = r;
3642 break;
3643
3644 case ARG_DEFINITIONS:
614b022c 3645 r = parse_path_argument(optarg, false, &arg_definitions);
e594a3b1
LP
3646 if (r < 0)
3647 return r;
3648 break;
3649
a26f4a49
LP
3650 case ARG_SIZE: {
3651 uint64_t parsed, rounded;
3652
170c9823
LP
3653 if (streq(optarg, "auto")) {
3654 arg_size = UINT64_MAX;
3655 arg_size_auto = true;
3656 break;
3657 }
3658
a26f4a49
LP
3659 r = parse_size(optarg, 1024, &parsed);
3660 if (r < 0)
3661 return log_error_errno(r, "Failed to parse --size= parameter: %s", optarg);
3662
3663 rounded = round_up_size(parsed, 4096);
3664 if (rounded == 0)
3665 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too small, refusing.");
3666 if (rounded == UINT64_MAX)
3667 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too large, refusing.");
3668
3669 if (rounded != parsed)
3670 log_warning("Specified size is not a multiple of 4096, rounding up automatically. (%" PRIu64 " → %" PRIu64 ")",
3671 parsed, rounded);
3672
3673 arg_size = rounded;
170c9823 3674 arg_size_auto = false;
a26f4a49
LP
3675 break;
3676 }
b9df3536 3677
a015fbe7 3678 case ARG_JSON:
b1e8f46c 3679 r = parse_json_argument(optarg, &arg_json_format_flags);
6a01ea4a
LP
3680 if (r <= 0)
3681 return r;
a015fbe7
TH
3682
3683 break;
3684
b9df3536
LP
3685 case ARG_KEY_FILE: {
3686 _cleanup_(erase_and_freep) char *k = NULL;
3687 size_t n = 0;
3688
8b3c3a49 3689 r = read_full_file_full(
986311c2 3690 AT_FDCWD, optarg, UINT64_MAX, SIZE_MAX,
8b3c3a49
LP
3691 READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
3692 NULL,
3693 &k, &n);
b9df3536
LP
3694 if (r < 0)
3695 return log_error_errno(r, "Failed to read key file '%s': %m", optarg);
3696
3697 erase_and_free(arg_key);
3698 arg_key = TAKE_PTR(k);
3699 arg_key_size = n;
3700 break;
3701 }
a26f4a49 3702
889914ef
LP
3703 case ARG_TPM2_DEVICE: {
3704 _cleanup_free_ char *device = NULL;
3705
3706 if (streq(optarg, "list"))
3707 return tpm2_list_devices();
3708
3709 if (!streq(optarg, "auto")) {
3710 device = strdup(optarg);
3711 if (!device)
3712 return log_oom();
3713 }
3714
3715 free(arg_tpm2_device);
3716 arg_tpm2_device = TAKE_PTR(device);
3717 break;
3718 }
3719
3720 case ARG_TPM2_PCRS: {
3721 uint32_t mask;
3722
3723 if (isempty(optarg)) {
3724 arg_tpm2_pcr_mask = 0;
3725 break;
3726 }
3727
3728 r = tpm2_parse_pcrs(optarg, &mask);
3729 if (r < 0)
3730 return r;
3731
3732 if (arg_tpm2_pcr_mask == UINT32_MAX)
3733 arg_tpm2_pcr_mask = mask;
3734 else
3735 arg_tpm2_pcr_mask |= mask;
3736
3737 break;
3738 }
3739
e594a3b1
LP
3740 case '?':
3741 return -EINVAL;
3742
3743 default:
3744 assert_not_reached("Unhandled option");
3745 }
3746
3747 if (argc - optind > 1)
3748 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3749 "Expected at most one argument, the path to the block device.");
3750
a26f4a49 3751 if (arg_factory_reset > 0 && IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE))
e594a3b1 3752 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
a26f4a49 3753 "Combination of --factory-reset=yes and --empty=force/--empty=require/--empty=create is invalid.");
e594a3b1
LP
3754
3755 if (arg_can_factory_reset)
a26f4a49
LP
3756 arg_dry_run = true; /* When --can-factory-reset is specified we don't make changes, hence
3757 * non-dry-run mode makes no sense. Thus, imply dry run mode so that we
3758 * open things strictly read-only. */
3759 else if (dry_run >= 0)
3760 arg_dry_run = dry_run;
3761
170c9823 3762 if (arg_empty == EMPTY_CREATE && (arg_size == UINT64_MAX && !arg_size_auto))
a26f4a49
LP
3763 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3764 "If --empty=create is specified, --size= must be specified, too.");
e594a3b1
LP
3765
3766 arg_node = argc > optind ? argv[optind] : NULL;
a26f4a49
LP
3767
3768 if (IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE) && !arg_node)
3769 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3770 "A path to a device node or loopback file must be specified when --empty=force, --empty=require or --empty=create are used.");
3771
889914ef
LP
3772 if (arg_tpm2_pcr_mask == UINT32_MAX)
3773 arg_tpm2_pcr_mask = TPM2_PCR_MASK_DEFAULT;
3774
e594a3b1
LP
3775 return 1;
3776}
3777
3778static int parse_proc_cmdline_factory_reset(void) {
3779 bool b;
3780 int r;
3781
3782 if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */
3783 return 0;
3784
3785 if (!in_initrd()) /* Never honour kernel command line factory reset request outside of the initrd */
3786 return 0;
3787
3788 r = proc_cmdline_get_bool("systemd.factory_reset", &b);
3789 if (r < 0)
3790 return log_error_errno(r, "Failed to parse systemd.factory_reset kernel command line argument: %m");
3791 if (r > 0) {
3792 arg_factory_reset = b;
3793
3794 if (b)
3795 log_notice("Honouring factory reset requested via kernel command line.");
3796 }
3797
3798 return 0;
3799}
3800
3801static int parse_efi_variable_factory_reset(void) {
3802 _cleanup_free_ char *value = NULL;
3803 int r;
3804
3805 if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */
3806 return 0;
3807
3808 if (!in_initrd()) /* Never honour EFI variable factory reset request outside of the initrd */
3809 return 0;
3810
3811 r = efi_get_variable_string(EFI_VENDOR_SYSTEMD, "FactoryReset", &value);
3812 if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
3813 return 0;
3814 if (r < 0)
3815 return log_error_errno(r, "Failed to read EFI variable FactoryReset: %m");
3816
3817 r = parse_boolean(value);
3818 if (r < 0)
3819 return log_error_errno(r, "Failed to parse EFI variable FactoryReset: %m");
3820
3821 arg_factory_reset = r;
3822 if (r)
111a3aae 3823 log_notice("Factory reset requested via EFI variable FactoryReset.");
e594a3b1
LP
3824
3825 return 0;
3826}
3827
3828static int remove_efi_variable_factory_reset(void) {
3829 int r;
3830
3831 r = efi_set_variable(EFI_VENDOR_SYSTEMD, "FactoryReset", NULL, 0);
3832 if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
3833 return 0;
3834 if (r < 0)
3835 return log_error_errno(r, "Failed to remove EFI variable FactoryReset: %m");
3836
3837 log_info("Successfully unset EFI variable FactoryReset.");
3838 return 0;
3839}
3840
a26f4a49 3841static int acquire_root_devno(const char *p, int mode, char **ret, int *ret_fd) {
e594a3b1
LP
3842 _cleanup_close_ int fd = -1;
3843 struct stat st;
f5fbe71d 3844 dev_t devno, fd_devno = MODE_INVALID;
e594a3b1
LP
3845 int r;
3846
a26f4a49
LP
3847 assert(p);
3848 assert(ret);
3849 assert(ret_fd);
3850
e594a3b1
LP
3851 fd = open(p, mode);
3852 if (fd < 0)
3853 return -errno;
3854
3855 if (fstat(fd, &st) < 0)
3856 return -errno;
3857
3858 if (S_ISREG(st.st_mode)) {
3859 char *s;
3860
3861 s = strdup(p);
3862 if (!s)
3863 return log_oom();
3864
3865 *ret = s;
a26f4a49
LP
3866 *ret_fd = TAKE_FD(fd);
3867
e594a3b1
LP
3868 return 0;
3869 }
3870
3871 if (S_ISBLK(st.st_mode))
a26f4a49 3872 fd_devno = devno = st.st_rdev;
e594a3b1
LP
3873 else if (S_ISDIR(st.st_mode)) {
3874
3875 devno = st.st_dev;
a26f4a49 3876 if (major(devno) == 0) {
e594a3b1
LP
3877 r = btrfs_get_block_device_fd(fd, &devno);
3878 if (r == -ENOTTY) /* not btrfs */
3879 return -ENODEV;
3880 if (r < 0)
3881 return r;
3882 }
e594a3b1
LP
3883 } else
3884 return -ENOTBLK;
3885
3886 /* From dm-crypt to backing partition */
3887 r = block_get_originating(devno, &devno);
8e5f3cec
LP
3888 if (r == -ENOENT)
3889 log_debug_errno(r, "Device '%s' has no dm-crypt/dm-verity device, no need to look for underlying block device.", p);
3890 else if (r < 0)
e594a3b1
LP
3891 log_debug_errno(r, "Failed to find underlying block device for '%s', ignoring: %m", p);
3892
3893 /* From partition to whole disk containing it */
3894 r = block_get_whole_disk(devno, &devno);
3895 if (r < 0)
162392b7 3896 log_debug_errno(r, "Failed to find whole disk block device for '%s', ignoring: %m", p);
e594a3b1 3897
a26f4a49
LP
3898 r = device_path_make_canonical(S_IFBLK, devno, ret);
3899 if (r < 0)
3900 return log_debug_errno(r, "Failed to determine canonical path for '%s': %m", p);
3901
3902 /* Only if we still lock at the same block device we can reuse the fd. Otherwise return an
3903 * invalidated fd. */
f5fbe71d 3904 *ret_fd = fd_devno != MODE_INVALID && fd_devno == devno ? TAKE_FD(fd) : -1;
a26f4a49 3905 return 0;
e594a3b1
LP
3906}
3907
a26f4a49 3908static int find_root(char **ret, int *ret_fd) {
e594a3b1
LP
3909 const char *t;
3910 int r;
3911
a26f4a49
LP
3912 assert(ret);
3913 assert(ret_fd);
3914
e594a3b1 3915 if (arg_node) {
a26f4a49
LP
3916 if (arg_empty == EMPTY_CREATE) {
3917 _cleanup_close_ int fd = -1;
3918 _cleanup_free_ char *s = NULL;
3919
3920 s = strdup(arg_node);
3921 if (!s)
3922 return log_oom();
3923
5332d7c6 3924 fd = open(arg_node, O_RDONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOFOLLOW, 0666);
a26f4a49
LP
3925 if (fd < 0)
3926 return log_error_errno(errno, "Failed to create '%s': %m", arg_node);
3927
3928 *ret = TAKE_PTR(s);
3929 *ret_fd = TAKE_FD(fd);
3930 return 0;
3931 }
3932
3933 r = acquire_root_devno(arg_node, O_RDONLY|O_CLOEXEC, ret, ret_fd);
67f0ac8c
LP
3934 if (r == -EUCLEAN)
3935 return btrfs_log_dev_root(LOG_ERR, r, arg_node);
e594a3b1 3936 if (r < 0)
aa2a74ad 3937 return log_error_errno(r, "Failed to open file or determine backing device of %s: %m", arg_node);
e594a3b1
LP
3938
3939 return 0;
3940 }
3941
a26f4a49
LP
3942 assert(IN_SET(arg_empty, EMPTY_REFUSE, EMPTY_ALLOW));
3943
e594a3b1
LP
3944 /* Let's search for the root device. We look for two cases here: first in /, and then in /usr. The
3945 * latter we check for cases where / is a tmpfs and only /usr is an actual persistent block device
3946 * (think: volatile setups) */
3947
3948 FOREACH_STRING(t, "/", "/usr") {
3949 _cleanup_free_ char *j = NULL;
3950 const char *p;
3951
3952 if (in_initrd()) {
3953 j = path_join("/sysroot", t);
3954 if (!j)
3955 return log_oom();
3956
3957 p = j;
3958 } else
3959 p = t;
3960
a26f4a49 3961 r = acquire_root_devno(p, O_RDONLY|O_DIRECTORY|O_CLOEXEC, ret, ret_fd);
e594a3b1 3962 if (r < 0) {
67f0ac8c
LP
3963 if (r == -EUCLEAN)
3964 return btrfs_log_dev_root(LOG_ERR, r, p);
e594a3b1
LP
3965 if (r != -ENODEV)
3966 return log_error_errno(r, "Failed to determine backing device of %s: %m", p);
3967 } else
3968 return 0;
3969 }
3970
3971 return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "Failed to discover root block device.");
3972}
3973
f9b3afae
LP
3974static int resize_pt(int fd) {
3975 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
3976 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
3977 int r;
3978
3979 /* After resizing the backing file we need to resize the partition table itself too, so that it takes
3980 * possession of the enlarged backing file. For this it suffices to open the device with libfdisk and
3981 * immediately write it again, with no changes. */
3982
3983 c = fdisk_new_context();
3984 if (!c)
3985 return log_oom();
3986
3987 xsprintf(procfs_path, "/proc/self/fd/%i", fd);
3988 r = fdisk_assign_device(c, procfs_path, 0);
3989 if (r < 0)
3990 return log_error_errno(r, "Failed to open device '%s': %m", procfs_path);
3991
3992 r = fdisk_has_label(c);
3993 if (r < 0)
3994 return log_error_errno(r, "Failed to determine whether disk '%s' has a disk label: %m", procfs_path);
3995 if (r == 0) {
3996 log_debug("Not resizing partition table, as there currently is none.");
3997 return 0;
3998 }
3999
4000 r = fdisk_write_disklabel(c);
4001 if (r < 0)
4002 return log_error_errno(r, "Failed to write resized partition table: %m");
4003
4004 log_info("Resized partition table.");
4005 return 1;
4006}
4007
a26f4a49
LP
4008static int resize_backing_fd(const char *node, int *fd) {
4009 char buf1[FORMAT_BYTES_MAX], buf2[FORMAT_BYTES_MAX];
4010 _cleanup_close_ int writable_fd = -1;
4011 struct stat st;
4012 int r;
4013
4014 assert(node);
4015 assert(fd);
4016
4017 if (arg_size == UINT64_MAX) /* Nothing to do */
4018 return 0;
4019
4020 if (*fd < 0) {
4021 /* Open the file if we haven't opened it yet. Note that we open it read-only here, just to
4022 * keep a reference to the file we can pass around. */
4023 *fd = open(node, O_RDONLY|O_CLOEXEC);
4024 if (*fd < 0)
4025 return log_error_errno(errno, "Failed to open '%s' in order to adjust size: %m", node);
4026 }
4027
4028 if (fstat(*fd, &st) < 0)
4029 return log_error_errno(errno, "Failed to stat '%s': %m", node);
4030
4031 r = stat_verify_regular(&st);
4032 if (r < 0)
4033 return log_error_errno(r, "Specified path '%s' is not a regular file, cannot resize: %m", node);
4034
4035 assert_se(format_bytes(buf1, sizeof(buf1), st.st_size));
4036 assert_se(format_bytes(buf2, sizeof(buf2), arg_size));
4037
4038 if ((uint64_t) st.st_size >= arg_size) {
4039 log_info("File '%s' already is of requested size or larger, not growing. (%s >= %s)", node, buf1, buf2);
4040 return 0;
4041 }
4042
4043 /* The file descriptor is read-only. In order to grow the file we need to have a writable fd. We
4044 * reopen the file for that temporarily. We keep the writable fd only open for this operation though,
4045 * as fdisk can't accept it anyway. */
4046
4047 writable_fd = fd_reopen(*fd, O_WRONLY|O_CLOEXEC);
4048 if (writable_fd < 0)
4049 return log_error_errno(writable_fd, "Failed to reopen backing file '%s' writable: %m", node);
4050
4051 if (!arg_discard) {
4052 if (fallocate(writable_fd, 0, 0, arg_size) < 0) {
4053 if (!ERRNO_IS_NOT_SUPPORTED(errno))
4054 return log_error_errno(errno, "Failed to grow '%s' from %s to %s by allocation: %m",
4055 node, buf1, buf2);
4056
4057 /* Fallback to truncation, if fallocate() is not supported. */
4058 log_debug("Backing file system does not support fallocate(), falling back to ftruncate().");
4059 } else {
f9b3afae
LP
4060 r = resize_pt(writable_fd);
4061 if (r < 0)
4062 return r;
4063
a26f4a49
LP
4064 if (st.st_size == 0) /* Likely regular file just created by us */
4065 log_info("Allocated %s for '%s'.", buf2, node);
4066 else
4067 log_info("File '%s' grown from %s to %s by allocation.", node, buf1, buf2);
4068
4069 return 1;
4070 }
4071 }
4072
4073 if (ftruncate(writable_fd, arg_size) < 0)
4074 return log_error_errno(errno, "Failed to grow '%s' from %s to %s by truncation: %m",
4075 node, buf1, buf2);
4076
f9b3afae
LP
4077 r = resize_pt(writable_fd);
4078 if (r < 0)
4079 return r;
4080
a26f4a49
LP
4081 if (st.st_size == 0) /* Likely regular file just created by us */
4082 log_info("Sized '%s' to %s.", node, buf2);
4083 else
4084 log_info("File '%s' grown from %s to %s by truncation.", node, buf1, buf2);
4085
4086 return 1;
4087}
4088
170c9823
LP
4089static int determine_auto_size(Context *c) {
4090 uint64_t sum = round_up_size(GPT_METADATA_SIZE, 4096);
4091 char buf[FORMAT_BYTES_MAX];
4092 Partition *p;
4093
4094 assert_se(c);
4095 assert_se(arg_size == UINT64_MAX);
4096 assert_se(arg_size_auto);
4097
4098 LIST_FOREACH(partitions, p, c->partitions) {
4099 uint64_t m;
4100
4101 if (p->dropped)
4102 continue;
4103
4104 m = partition_min_size_with_padding(p);
4105 if (m > UINT64_MAX - sum)
4106 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Image would grow too large, refusing.");
4107
4108 sum += m;
4109 }
4110
4111 assert_se(format_bytes(buf, sizeof(buf), sum));
4112 log_info("Automatically determined minimal disk image size as %s.", buf);
4113
4114 arg_size = sum;
4115 return 0;
4116}
4117
e594a3b1
LP
4118static int run(int argc, char *argv[]) {
4119 _cleanup_(context_freep) Context* context = NULL;
4120 _cleanup_free_ char *node = NULL;
a26f4a49 4121 _cleanup_close_ int backing_fd = -1;
e594a3b1
LP
4122 bool from_scratch;
4123 int r;
4124
4125 log_show_color(true);
4126 log_parse_environment();
4127 log_open();
4128
4129 if (in_initrd()) {
4130 /* Default to operation on /sysroot when invoked in the initrd! */
4131 arg_root = strdup("/sysroot");
4132 if (!arg_root)
4133 return log_oom();
4134 }
4135
4136 r = parse_argv(argc, argv);
4137 if (r <= 0)
4138 return r;
4139
4140 r = parse_proc_cmdline_factory_reset();
4141 if (r < 0)
4142 return r;
4143
4144 r = parse_efi_variable_factory_reset();
4145 if (r < 0)
4146 return r;
4147
e594a3b1
LP
4148 context = context_new(arg_seed);
4149 if (!context)
4150 return log_oom();
4151
4152 r = context_read_definitions(context, arg_definitions, arg_root);
4153 if (r < 0)
4154 return r;
4155
a26f4a49 4156 if (context->n_partitions <= 0 && arg_empty == EMPTY_REFUSE) {
e2d65cd2 4157 log_info("Didn't find any partition definition files, nothing to do.");
0ae5ffe0 4158 return 0;
e2d65cd2 4159 }
0ae5ffe0 4160
a26f4a49 4161 r = find_root(&node, &backing_fd);
0ae5ffe0
YW
4162 if (r < 0)
4163 return r;
4164
a26f4a49
LP
4165 if (arg_size != UINT64_MAX) {
4166 r = resize_backing_fd(node, &backing_fd);
4167 if (r < 0)
4168 return r;
4169 }
4170
4171 r = context_load_partition_table(context, node, &backing_fd);
e594a3b1
LP
4172 if (r == -EHWPOISON)
4173 return 77; /* Special return value which means "Not GPT, so not doing anything". This isn't
4174 * really an error when called at boot. */
4175 if (r < 0)
4176 return r;
4177 from_scratch = r > 0; /* Starting from scratch */
4178
4179 if (arg_can_factory_reset) {
4180 r = context_can_factory_reset(context);
4181 if (r < 0)
4182 return r;
4183 if (r == 0)
4184 return EXIT_FAILURE;
4185
4186 return 0;
4187 }
4188
4189 r = context_factory_reset(context, from_scratch);
4190 if (r < 0)
4191 return r;
4192 if (r > 0) {
4193 /* We actually did a factory reset! */
4194 r = remove_efi_variable_factory_reset();
4195 if (r < 0)
4196 return r;
4197
4198 /* Reload the reduced partition table */
4199 context_unload_partition_table(context);
a26f4a49 4200 r = context_load_partition_table(context, node, &backing_fd);
e594a3b1
LP
4201 if (r < 0)
4202 return r;
4203 }
4204
4205#if 0
4206 (void) context_dump_partitions(context, node);
4207 putchar('\n');
4208#endif
4209
4210 r = context_read_seed(context, arg_root);
4211 if (r < 0)
4212 return r;
4213
757bc2e4
LP
4214 /* Open all files to copy blocks from now, since we want to take their size into consideration */
4215 r = context_open_copy_block_paths(context);
4216 if (r < 0)
4217 return r;
4218
170c9823
LP
4219 if (arg_size_auto) {
4220 r = determine_auto_size(context);
4221 if (r < 0)
4222 return r;
4223
4224 /* Flush out everything again, and let's grow the file first, then start fresh */
4225 context_unload_partition_table(context);
4226
4227 assert_se(arg_size != UINT64_MAX);
4228 r = resize_backing_fd(node, &backing_fd);
4229 if (r < 0)
4230 return r;
4231
4232 r = context_load_partition_table(context, node, &backing_fd);
4233 if (r < 0)
4234 return r;
4235 }
4236
e594a3b1
LP
4237 /* First try to fit new partitions in, dropping by priority until it fits */
4238 for (;;) {
4239 if (context_allocate_partitions(context))
4240 break; /* Success! */
4241
4242 if (!context_drop_one_priority(context))
4243 return log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
4244 "Can't fit requested partitions into free space, refusing.");
4245 }
4246
4247 /* Now assign free space according to the weight logic */
4248 r = context_grow_partitions(context);
4249 if (r < 0)
4250 return r;
4251
4252 /* Now calculate where each partition gets placed */
4253 context_place_partitions(context);
4254
4255 /* Make sure each partition has a unique UUID and unique label */
4256 r = context_acquire_partition_uuids_and_labels(context);
4257 if (r < 0)
4258 return r;
4259
4260 r = context_write_partition_table(context, node, from_scratch);
4261 if (r < 0)
4262 return r;
4263
4264 return 0;
4265}
4266
4267DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);