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