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