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