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