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