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