]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/partition/repart.c
userdbctl homectl use table_log_add_error()
[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", rvalue);
921 return 0;
922 }
923
924 free_and_replace(*label, resolved);
925 return 0;
926 }
927
928 static int config_parse_weight(
929 const char *unit,
930 const char *filename,
931 unsigned line,
932 const char *section,
933 unsigned section_line,
934 const char *lvalue,
935 int ltype,
936 const char *rvalue,
937 void *data,
938 void *userdata) {
939
940 uint32_t *priority = data, v;
941 int r;
942
943 assert(rvalue);
944 assert(priority);
945
946 r = safe_atou32(rvalue, &v);
947 if (r < 0) {
948 log_syntax(unit, LOG_WARNING, filename, line, r,
949 "Failed to parse weight value, ignoring: %s", rvalue);
950 return 0;
951 }
952
953 if (v > 1000U*1000U) {
954 log_syntax(unit, LOG_WARNING, filename, line, r,
955 "Weight needs to be in range 0…10000000, ignoring: %" PRIu32, v);
956 return 0;
957 }
958
959 *priority = v;
960 return 0;
961 }
962
963 static int config_parse_size4096(
964 const char *unit,
965 const char *filename,
966 unsigned line,
967 const char *section,
968 unsigned section_line,
969 const char *lvalue,
970 int ltype,
971 const char *rvalue,
972 void *data,
973 void *userdata) {
974
975 uint64_t *sz = data, parsed;
976 int r;
977
978 assert(rvalue);
979 assert(data);
980
981 r = parse_size(rvalue, 1024, &parsed);
982 if (r < 0)
983 return log_syntax(unit, LOG_WARNING, filename, line, r,
984 "Failed to parse size value: %s", rvalue);
985
986 if (ltype > 0)
987 *sz = round_up_size(parsed, 4096);
988 else if (ltype < 0)
989 *sz = round_down_size(parsed, 4096);
990 else
991 *sz = parsed;
992
993 if (*sz != parsed)
994 log_syntax(unit, LOG_NOTICE, filename, line, r, "Rounded %s= size %" PRIu64 " → %" PRIu64 ", a multiple of 4096.", lvalue, parsed, *sz);
995
996 return 0;
997 }
998
999 static int partition_read_definition(Partition *p, const char *path) {
1000
1001 ConfigTableItem table[] = {
1002 { "Partition", "Type", config_parse_type, 0, &p->type_uuid },
1003 { "Partition", "Label", config_parse_label, 0, &p->new_label },
1004 { "Partition", "UUID", config_parse_id128, 0, &p->new_uuid },
1005 { "Partition", "Priority", config_parse_int32, 0, &p->priority },
1006 { "Partition", "Weight", config_parse_weight, 0, &p->weight },
1007 { "Partition", "PaddingWeight", config_parse_weight, 0, &p->padding_weight },
1008 { "Partition", "SizeMinBytes", config_parse_size4096, 1, &p->size_min },
1009 { "Partition", "SizeMaxBytes", config_parse_size4096, -1, &p->size_max },
1010 { "Partition", "PaddingMinBytes", config_parse_size4096, 1, &p->padding_min },
1011 { "Partition", "PaddingMaxBytes", config_parse_size4096, -1, &p->padding_max },
1012 { "Partition", "FactoryReset", config_parse_bool, 0, &p->factory_reset },
1013 { "Partition", "CopyBlocks", config_parse_path, 0, &p->copy_blocks_path },
1014 {}
1015 };
1016 int r;
1017
1018 r = config_parse(NULL, path, NULL,
1019 "Partition\0",
1020 config_item_table_lookup, table,
1021 CONFIG_PARSE_WARN,
1022 p,
1023 NULL);
1024 if (r < 0)
1025 return r;
1026
1027 if (p->size_min != UINT64_MAX && p->size_max != UINT64_MAX && p->size_min > p->size_max)
1028 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1029 "SizeMinBytes= larger than SizeMaxBytes=, refusing.");
1030
1031 if (p->padding_min != UINT64_MAX && p->padding_max != UINT64_MAX && p->padding_min > p->padding_max)
1032 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1033 "PaddingMinBytes= larger than PaddingMaxBytes=, refusing.");
1034
1035 if (sd_id128_is_null(p->type_uuid))
1036 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1037 "Type= not defined, refusing.");
1038
1039 return 0;
1040 }
1041
1042 static int context_read_definitions(
1043 Context *context,
1044 const char *directory,
1045 const char *root) {
1046
1047 _cleanup_strv_free_ char **files = NULL;
1048 Partition *last = NULL;
1049 char **f;
1050 int r;
1051
1052 assert(context);
1053
1054 if (directory)
1055 r = conf_files_list_strv(&files, ".conf", NULL, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, (const char**) STRV_MAKE(directory));
1056 else
1057 r = conf_files_list_strv(&files, ".conf", root, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, (const char**) CONF_PATHS_STRV("repart.d"));
1058 if (r < 0)
1059 return log_error_errno(r, "Failed to enumerate *.conf files: %m");
1060
1061 STRV_FOREACH(f, files) {
1062 _cleanup_(partition_freep) Partition *p = NULL;
1063
1064 p = partition_new();
1065 if (!p)
1066 return log_oom();
1067
1068 p->definition_path = strdup(*f);
1069 if (!p->definition_path)
1070 return log_oom();
1071
1072 r = partition_read_definition(p, *f);
1073 if (r < 0)
1074 return r;
1075
1076 LIST_INSERT_AFTER(partitions, context->partitions, last, p);
1077 last = TAKE_PTR(p);
1078 context->n_partitions++;
1079 }
1080
1081 return 0;
1082 }
1083
1084 DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_context*, fdisk_unref_context);
1085 DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_partition*, fdisk_unref_partition);
1086 DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_parttype*, fdisk_unref_parttype);
1087 DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_table*, fdisk_unref_table);
1088
1089 static int determine_current_padding(
1090 struct fdisk_context *c,
1091 struct fdisk_table *t,
1092 struct fdisk_partition *p,
1093 uint64_t *ret) {
1094
1095 size_t n_partitions;
1096 uint64_t offset, next = UINT64_MAX;
1097
1098 assert(c);
1099 assert(t);
1100 assert(p);
1101
1102 if (!fdisk_partition_has_end(p))
1103 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition has no end!");
1104
1105 offset = fdisk_partition_get_end(p);
1106 assert(offset < UINT64_MAX / 512);
1107 offset *= 512;
1108
1109 n_partitions = fdisk_table_get_nents(t);
1110 for (size_t i = 0; i < n_partitions; i++) {
1111 struct fdisk_partition *q;
1112 uint64_t start;
1113
1114 q = fdisk_table_get_partition(t, i);
1115 if (!q)
1116 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
1117
1118 if (fdisk_partition_is_used(q) <= 0)
1119 continue;
1120
1121 if (!fdisk_partition_has_start(q))
1122 continue;
1123
1124 start = fdisk_partition_get_start(q);
1125 assert(start < UINT64_MAX / 512);
1126 start *= 512;
1127
1128 if (start >= offset && (next == UINT64_MAX || next > start))
1129 next = start;
1130 }
1131
1132 if (next == UINT64_MAX) {
1133 /* No later partition? In that case check the end of the usable area */
1134 next = fdisk_get_last_lba(c);
1135 assert(next < UINT64_MAX);
1136 next++; /* The last LBA is one sector before the end */
1137
1138 assert(next < UINT64_MAX / 512);
1139 next *= 512;
1140
1141 if (offset > next)
1142 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end.");
1143 }
1144
1145 assert(next >= offset);
1146 offset = round_up_size(offset, 4096);
1147 next = round_down_size(next, 4096);
1148
1149 if (next >= offset) /* Check again, rounding might have fucked things up */
1150 *ret = next - offset;
1151 else
1152 *ret = 0;
1153
1154 return 0;
1155 }
1156
1157 static int fdisk_ask_cb(struct fdisk_context *c, struct fdisk_ask *ask, void *data) {
1158 _cleanup_free_ char *ids = NULL;
1159 int r;
1160
1161 if (fdisk_ask_get_type(ask) != FDISK_ASKTYPE_STRING)
1162 return -EINVAL;
1163
1164 ids = new(char, ID128_UUID_STRING_MAX);
1165 if (!ids)
1166 return -ENOMEM;
1167
1168 r = fdisk_ask_string_set_result(ask, id128_to_uuid_string(*(sd_id128_t*) data, ids));
1169 if (r < 0)
1170 return r;
1171
1172 TAKE_PTR(ids);
1173 return 0;
1174 }
1175
1176 static int fdisk_set_disklabel_id_by_uuid(struct fdisk_context *c, sd_id128_t id) {
1177 int r;
1178
1179 r = fdisk_set_ask(c, fdisk_ask_cb, &id);
1180 if (r < 0)
1181 return r;
1182
1183 r = fdisk_set_disklabel_id(c);
1184 if (r < 0)
1185 return r;
1186
1187 return fdisk_set_ask(c, NULL, NULL);
1188 }
1189
1190 #define DISK_UUID_TOKEN "disk-uuid"
1191
1192 static int disk_acquire_uuid(Context *context, sd_id128_t *ret) {
1193 union {
1194 unsigned char md[SHA256_DIGEST_LENGTH];
1195 sd_id128_t id;
1196 } result;
1197
1198 assert(context);
1199 assert(ret);
1200
1201 /* Calculate the HMAC-SHA256 of the string "disk-uuid", keyed off the machine ID. We use the machine
1202 * ID as key (and not as cleartext!) since it's the machine ID we don't want to leak. */
1203
1204 if (!HMAC(EVP_sha256(),
1205 &context->seed, sizeof(context->seed),
1206 (const unsigned char*) DISK_UUID_TOKEN, strlen(DISK_UUID_TOKEN),
1207 result.md, NULL))
1208 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "HMAC-SHA256 calculation failed.");
1209
1210 /* Take the first half, mark it as v4 UUID */
1211 assert_cc(sizeof(result.md) == sizeof(result.id) * 2);
1212 *ret = id128_make_v4_uuid(result.id);
1213 return 0;
1214 }
1215
1216 static int context_load_partition_table(
1217 Context *context,
1218 const char *node,
1219 int *backing_fd) {
1220
1221 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
1222 _cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL;
1223 uint64_t left_boundary = UINT64_MAX, first_lba, last_lba, nsectors;
1224 _cleanup_free_ char *disk_uuid_string = NULL;
1225 bool from_scratch = false;
1226 sd_id128_t disk_uuid;
1227 size_t n_partitions;
1228 int r;
1229
1230 assert(context);
1231 assert(node);
1232 assert(backing_fd);
1233
1234 c = fdisk_new_context();
1235 if (!c)
1236 return log_oom();
1237
1238 /* libfdisk doesn't have an API to operate on arbitrary fds, hence reopen the fd going via the
1239 * /proc/self/fd/ magic path if we have an existing fd. Open the original file otherwise. */
1240 if (*backing_fd < 0)
1241 r = fdisk_assign_device(c, node, arg_dry_run);
1242 else {
1243 char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
1244 xsprintf(procfs_path, "/proc/self/fd/%i", *backing_fd);
1245
1246 r = fdisk_assign_device(c, procfs_path, arg_dry_run);
1247 }
1248 if (r < 0)
1249 return log_error_errno(r, "Failed to open device '%s': %m", node);
1250
1251 if (*backing_fd < 0) {
1252 /* If we have no fd referencing the device yet, make a copy of the fd now, so that we have one */
1253 *backing_fd = fcntl(fdisk_get_devfd(c), F_DUPFD_CLOEXEC, 3);
1254 if (*backing_fd < 0)
1255 return log_error_errno(errno, "Failed to duplicate fdisk fd: %m");
1256 }
1257
1258 /* Tell udev not to interfere while we are processing the device */
1259 if (flock(fdisk_get_devfd(c), arg_dry_run ? LOCK_SH : LOCK_EX) < 0)
1260 return log_error_errno(errno, "Failed to lock block device: %m");
1261
1262 switch (arg_empty) {
1263
1264 case EMPTY_REFUSE:
1265 /* Refuse empty disks, insist on an existing GPT partition table */
1266 if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
1267 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not repartitioning.", node);
1268
1269 break;
1270
1271 case EMPTY_REQUIRE:
1272 /* Require an empty disk, refuse any existing partition table */
1273 r = fdisk_has_label(c);
1274 if (r < 0)
1275 return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", node);
1276 if (r > 0)
1277 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s already has a disk label, refusing.", node);
1278
1279 from_scratch = true;
1280 break;
1281
1282 case EMPTY_ALLOW:
1283 /* Allow both an empty disk and an existing partition table, but only GPT */
1284 r = fdisk_has_label(c);
1285 if (r < 0)
1286 return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", node);
1287 if (r > 0) {
1288 if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
1289 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has non-GPT disk label, not repartitioning.", node);
1290 } else
1291 from_scratch = true;
1292
1293 break;
1294
1295 case EMPTY_FORCE:
1296 case EMPTY_CREATE:
1297 /* Always reinitiaize the disk, don't consider what there was on the disk before */
1298 from_scratch = true;
1299 break;
1300 }
1301
1302 if (from_scratch) {
1303 r = fdisk_enable_wipe(c, true);
1304 if (r < 0)
1305 return log_error_errno(r, "Failed to enable wiping of disk signature: %m");
1306
1307 r = fdisk_create_disklabel(c, "gpt");
1308 if (r < 0)
1309 return log_error_errno(r, "Failed to create GPT disk label: %m");
1310
1311 r = disk_acquire_uuid(context, &disk_uuid);
1312 if (r < 0)
1313 return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
1314
1315 r = fdisk_set_disklabel_id_by_uuid(c, disk_uuid);
1316 if (r < 0)
1317 return log_error_errno(r, "Failed to set GPT disk label: %m");
1318
1319 goto add_initial_free_area;
1320 }
1321
1322 r = fdisk_get_disklabel_id(c, &disk_uuid_string);
1323 if (r < 0)
1324 return log_error_errno(r, "Failed to get current GPT disk label UUID: %m");
1325
1326 r = sd_id128_from_string(disk_uuid_string, &disk_uuid);
1327 if (r < 0)
1328 return log_error_errno(r, "Failed to parse current GPT disk label UUID: %m");
1329
1330 if (sd_id128_is_null(disk_uuid)) {
1331 r = disk_acquire_uuid(context, &disk_uuid);
1332 if (r < 0)
1333 return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
1334
1335 r = fdisk_set_disklabel_id(c);
1336 if (r < 0)
1337 return log_error_errno(r, "Failed to set GPT disk label: %m");
1338 }
1339
1340 r = fdisk_get_partitions(c, &t);
1341 if (r < 0)
1342 return log_error_errno(r, "Failed to acquire partition table: %m");
1343
1344 n_partitions = fdisk_table_get_nents(t);
1345 for (size_t i = 0; i < n_partitions; i++) {
1346 _cleanup_free_ char *label_copy = NULL;
1347 Partition *pp, *last = NULL;
1348 struct fdisk_partition *p;
1349 struct fdisk_parttype *pt;
1350 const char *pts, *ids, *label;
1351 uint64_t sz, start;
1352 bool found = false;
1353 sd_id128_t ptid, id;
1354 size_t partno;
1355
1356 p = fdisk_table_get_partition(t, i);
1357 if (!p)
1358 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
1359
1360 if (fdisk_partition_is_used(p) <= 0)
1361 continue;
1362
1363 if (fdisk_partition_has_start(p) <= 0 ||
1364 fdisk_partition_has_size(p) <= 0 ||
1365 fdisk_partition_has_partno(p) <= 0)
1366 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a position, size or number.");
1367
1368 pt = fdisk_partition_get_type(p);
1369 if (!pt)
1370 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition: %m");
1371
1372 pts = fdisk_parttype_get_string(pt);
1373 if (!pts)
1374 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition as string: %m");
1375
1376 r = sd_id128_from_string(pts, &ptid);
1377 if (r < 0)
1378 return log_error_errno(r, "Failed to parse partition type UUID %s: %m", pts);
1379
1380 ids = fdisk_partition_get_uuid(p);
1381 if (!ids)
1382 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a UUID.");
1383
1384 r = sd_id128_from_string(ids, &id);
1385 if (r < 0)
1386 return log_error_errno(r, "Failed to parse partition UUID %s: %m", ids);
1387
1388 label = fdisk_partition_get_name(p);
1389 if (!isempty(label)) {
1390 label_copy = strdup(label);
1391 if (!label_copy)
1392 return log_oom();
1393 }
1394
1395 sz = fdisk_partition_get_size(p);
1396 assert_se(sz <= UINT64_MAX/512);
1397 sz *= 512;
1398
1399 start = fdisk_partition_get_start(p);
1400 assert_se(start <= UINT64_MAX/512);
1401 start *= 512;
1402
1403 partno = fdisk_partition_get_partno(p);
1404
1405 if (left_boundary == UINT64_MAX || left_boundary > start)
1406 left_boundary = start;
1407
1408 /* Assign this existing partition to the first partition of the right type that doesn't have
1409 * an existing one assigned yet. */
1410 LIST_FOREACH(partitions, pp, context->partitions) {
1411 last = pp;
1412
1413 if (!sd_id128_equal(pp->type_uuid, ptid))
1414 continue;
1415
1416 if (!pp->current_partition) {
1417 pp->current_uuid = id;
1418 pp->current_size = sz;
1419 pp->offset = start;
1420 pp->partno = partno;
1421 pp->current_label = TAKE_PTR(label_copy);
1422
1423 pp->current_partition = p;
1424 fdisk_ref_partition(p);
1425
1426 r = determine_current_padding(c, t, p, &pp->current_padding);
1427 if (r < 0)
1428 return r;
1429
1430 if (pp->current_padding > 0) {
1431 r = context_add_free_area(context, pp->current_padding, pp);
1432 if (r < 0)
1433 return r;
1434 }
1435
1436 found = true;
1437 break;
1438 }
1439 }
1440
1441 /* If we have no matching definition, create a new one. */
1442 if (!found) {
1443 _cleanup_(partition_freep) Partition *np = NULL;
1444
1445 np = partition_new();
1446 if (!np)
1447 return log_oom();
1448
1449 np->current_uuid = id;
1450 np->type_uuid = ptid;
1451 np->current_size = sz;
1452 np->offset = start;
1453 np->partno = partno;
1454 np->current_label = TAKE_PTR(label_copy);
1455
1456 np->current_partition = p;
1457 fdisk_ref_partition(p);
1458
1459 r = determine_current_padding(c, t, p, &np->current_padding);
1460 if (r < 0)
1461 return r;
1462
1463 if (np->current_padding > 0) {
1464 r = context_add_free_area(context, np->current_padding, np);
1465 if (r < 0)
1466 return r;
1467 }
1468
1469 LIST_INSERT_AFTER(partitions, context->partitions, last, TAKE_PTR(np));
1470 context->n_partitions++;
1471 }
1472 }
1473
1474 add_initial_free_area:
1475 nsectors = fdisk_get_nsectors(c);
1476 assert(nsectors <= UINT64_MAX/512);
1477 nsectors *= 512;
1478
1479 first_lba = fdisk_get_first_lba(c);
1480 assert(first_lba <= UINT64_MAX/512);
1481 first_lba *= 512;
1482
1483 last_lba = fdisk_get_last_lba(c);
1484 assert(last_lba < UINT64_MAX);
1485 last_lba++;
1486 assert(last_lba <= UINT64_MAX/512);
1487 last_lba *= 512;
1488
1489 assert(last_lba >= first_lba);
1490
1491 if (left_boundary == UINT64_MAX) {
1492 /* No partitions at all? Then the whole disk is up for grabs. */
1493
1494 first_lba = round_up_size(first_lba, 4096);
1495 last_lba = round_down_size(last_lba, 4096);
1496
1497 if (last_lba > first_lba) {
1498 r = context_add_free_area(context, last_lba - first_lba, NULL);
1499 if (r < 0)
1500 return r;
1501 }
1502 } else {
1503 /* Add space left of first partition */
1504 assert(left_boundary >= first_lba);
1505
1506 first_lba = round_up_size(first_lba, 4096);
1507 left_boundary = round_down_size(left_boundary, 4096);
1508 last_lba = round_down_size(last_lba, 4096);
1509
1510 if (left_boundary > first_lba) {
1511 r = context_add_free_area(context, left_boundary - first_lba, NULL);
1512 if (r < 0)
1513 return r;
1514 }
1515 }
1516
1517 context->start = first_lba;
1518 context->end = last_lba;
1519 context->total = nsectors;
1520 context->fdisk_context = TAKE_PTR(c);
1521
1522 return from_scratch;
1523 }
1524
1525 static void context_unload_partition_table(Context *context) {
1526 Partition *p, *next;
1527
1528 assert(context);
1529
1530 LIST_FOREACH_SAFE(partitions, p, next, context->partitions) {
1531
1532 /* Entirely remove partitions that have no configuration */
1533 if (PARTITION_IS_FOREIGN(p)) {
1534 partition_unlink_and_free(context, p);
1535 continue;
1536 }
1537
1538 /* Otherwise drop all data we read off the block device and everything we might have
1539 * calculated based on it */
1540
1541 p->dropped = false;
1542 p->current_size = UINT64_MAX;
1543 p->new_size = UINT64_MAX;
1544 p->current_padding = UINT64_MAX;
1545 p->new_padding = UINT64_MAX;
1546 p->partno = UINT64_MAX;
1547 p->offset = UINT64_MAX;
1548
1549 if (p->current_partition) {
1550 fdisk_unref_partition(p->current_partition);
1551 p->current_partition = NULL;
1552 }
1553
1554 if (p->new_partition) {
1555 fdisk_unref_partition(p->new_partition);
1556 p->new_partition = NULL;
1557 }
1558
1559 p->padding_area = NULL;
1560 p->allocated_to_area = NULL;
1561
1562 p->current_uuid = p->new_uuid = SD_ID128_NULL;
1563 }
1564
1565 context->start = UINT64_MAX;
1566 context->end = UINT64_MAX;
1567 context->total = UINT64_MAX;
1568
1569 if (context->fdisk_context) {
1570 fdisk_unref_context(context->fdisk_context);
1571 context->fdisk_context = NULL;
1572 }
1573
1574 context_free_free_areas(context);
1575 }
1576
1577 static int format_size_change(uint64_t from, uint64_t to, char **ret) {
1578 char format_buffer1[FORMAT_BYTES_MAX], format_buffer2[FORMAT_BYTES_MAX], *buf;
1579
1580 if (from != UINT64_MAX)
1581 format_bytes(format_buffer1, sizeof(format_buffer1), from);
1582 if (to != UINT64_MAX)
1583 format_bytes(format_buffer2, sizeof(format_buffer2), to);
1584
1585 if (from != UINT64_MAX) {
1586 if (from == to || to == UINT64_MAX)
1587 buf = strdup(format_buffer1);
1588 else
1589 buf = strjoin(format_buffer1, " ", special_glyph(SPECIAL_GLYPH_ARROW), " ", format_buffer2);
1590 } else if (to != UINT64_MAX)
1591 buf = strjoin(special_glyph(SPECIAL_GLYPH_ARROW), " ", format_buffer2);
1592 else {
1593 *ret = NULL;
1594 return 0;
1595 }
1596
1597 if (!buf)
1598 return log_oom();
1599
1600 *ret = TAKE_PTR(buf);
1601 return 1;
1602 }
1603
1604 static const char *partition_label(const Partition *p) {
1605 assert(p);
1606
1607 if (p->new_label)
1608 return p->new_label;
1609
1610 if (p->current_label)
1611 return p->current_label;
1612
1613 return gpt_partition_type_uuid_to_string(p->type_uuid);
1614 }
1615
1616 static int context_dump_partitions(Context *context, const char *node) {
1617 _cleanup_(table_unrefp) Table *t = NULL;
1618 uint64_t sum_padding = 0, sum_size = 0;
1619 Partition *p;
1620 int r;
1621
1622 t = table_new("type", "label", "uuid", "file", "node", "offset", "raw size", "size", "raw padding", "padding");
1623 if (!t)
1624 return log_oom();
1625
1626 if (!DEBUG_LOGGING)
1627 (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);
1628
1629 (void) table_set_align_percent(t, table_get_cell(t, 0, 4), 100);
1630 (void) table_set_align_percent(t, table_get_cell(t, 0, 5), 100);
1631
1632 LIST_FOREACH(partitions, p, context->partitions) {
1633 _cleanup_free_ char *size_change = NULL, *padding_change = NULL, *partname = NULL;
1634 char uuid_buffer[ID128_UUID_STRING_MAX];
1635 const char *label;
1636
1637 if (p->dropped)
1638 continue;
1639
1640 label = partition_label(p);
1641 partname = p->partno != UINT64_MAX ? fdisk_partname(node, p->partno+1) : NULL;
1642
1643 r = format_size_change(p->current_size, p->new_size, &size_change);
1644 if (r < 0)
1645 return r;
1646
1647 r = format_size_change(p->current_padding, p->new_padding, &padding_change);
1648 if (r < 0)
1649 return r;
1650
1651 if (p->new_size != UINT64_MAX)
1652 sum_size += p->new_size;
1653 if (p->new_padding != UINT64_MAX)
1654 sum_padding += p->new_padding;
1655
1656 r = table_add_many(
1657 t,
1658 TABLE_STRING, gpt_partition_type_uuid_to_string_harder(p->type_uuid, uuid_buffer),
1659 TABLE_STRING, label ?: "-", TABLE_SET_COLOR, label ? NULL : ansi_grey(),
1660 TABLE_UUID, sd_id128_is_null(p->new_uuid) ? p->current_uuid : p->new_uuid,
1661 TABLE_STRING, p->definition_path ? basename(p->definition_path) : "-", TABLE_SET_COLOR, p->definition_path ? NULL : ansi_grey(),
1662 TABLE_STRING, partname ?: "no", TABLE_SET_COLOR, partname ? NULL : ansi_highlight(),
1663 TABLE_UINT64, p->offset,
1664 TABLE_UINT64, p->new_size,
1665 TABLE_STRING, size_change, TABLE_SET_COLOR, !p->partitions_next && sum_size > 0 ? ansi_underline() : NULL,
1666 TABLE_UINT64, p->new_padding,
1667 TABLE_STRING, padding_change, TABLE_SET_COLOR, !p->partitions_next && sum_padding > 0 ? ansi_underline() : NULL);
1668 if (r < 0)
1669 return table_log_add_error(r);
1670 }
1671
1672 if (sum_padding > 0 || sum_size > 0) {
1673 char s[FORMAT_BYTES_MAX];
1674 const char *a, *b;
1675
1676 a = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", format_bytes(s, sizeof(s), sum_size));
1677 b = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", format_bytes(s, sizeof(s), sum_padding));
1678
1679 r = table_add_many(
1680 t,
1681 TABLE_EMPTY,
1682 TABLE_EMPTY,
1683 TABLE_EMPTY,
1684 TABLE_EMPTY,
1685 TABLE_EMPTY,
1686 TABLE_EMPTY,
1687 TABLE_EMPTY,
1688 TABLE_STRING, a,
1689 TABLE_EMPTY,
1690 TABLE_STRING, b);
1691 if (r < 0)
1692 return table_log_add_error(r);
1693 }
1694
1695 r = table_print(t, stdout);
1696 if (r < 0)
1697 return log_error_errno(r, "Failed to dump table: %m");
1698
1699 return 0;
1700 }
1701
1702 static void context_bar_char_process_partition(
1703 Context *context,
1704 Partition *bar[],
1705 size_t n,
1706 Partition *p,
1707 size_t *ret_start) {
1708
1709 uint64_t from, to, total;
1710 size_t x, y;
1711
1712 assert(context);
1713 assert(bar);
1714 assert(n > 0);
1715 assert(p);
1716
1717 if (p->dropped)
1718 return;
1719
1720 assert(p->offset != UINT64_MAX);
1721 assert(p->new_size != UINT64_MAX);
1722
1723 from = p->offset;
1724 to = from + p->new_size;
1725
1726 assert(context->end >= context->start);
1727 total = context->end - context->start;
1728
1729 assert(from >= context->start);
1730 assert(from <= context->end);
1731 x = (from - context->start) * n / total;
1732
1733 assert(to >= context->start);
1734 assert(to <= context->end);
1735 y = (to - context->start) * n / total;
1736
1737 assert(x <= y);
1738 assert(y <= n);
1739
1740 for (size_t i = x; i < y; i++)
1741 bar[i] = p;
1742
1743 *ret_start = x;
1744 }
1745
1746 static int partition_hint(const Partition *p, const char *node, char **ret) {
1747 _cleanup_free_ char *buf = NULL;
1748 char ids[ID128_UUID_STRING_MAX];
1749 const char *label;
1750 sd_id128_t id;
1751
1752 /* Tries really hard to find a suitable description for this partition */
1753
1754 if (p->definition_path) {
1755 buf = strdup(basename(p->definition_path));
1756 goto done;
1757 }
1758
1759 label = partition_label(p);
1760 if (!isempty(label)) {
1761 buf = strdup(label);
1762 goto done;
1763 }
1764
1765 if (p->partno != UINT64_MAX) {
1766 buf = fdisk_partname(node, p->partno+1);
1767 goto done;
1768 }
1769
1770 if (!sd_id128_is_null(p->new_uuid))
1771 id = p->new_uuid;
1772 else if (!sd_id128_is_null(p->current_uuid))
1773 id = p->current_uuid;
1774 else
1775 id = p->type_uuid;
1776
1777 buf = strdup(id128_to_uuid_string(id, ids));
1778
1779 done:
1780 if (!buf)
1781 return -ENOMEM;
1782
1783 *ret = TAKE_PTR(buf);
1784 return 0;
1785 }
1786
1787 static int context_dump_partition_bar(Context *context, const char *node) {
1788 _cleanup_free_ Partition **bar = NULL;
1789 _cleanup_free_ size_t *start_array = NULL;
1790 Partition *p, *last = NULL;
1791 bool z = false;
1792 size_t c, j = 0;
1793
1794 assert_se((c = columns()) >= 2);
1795 c -= 2; /* We do not use the leftmost and rightmost character cell */
1796
1797 bar = new0(Partition*, c);
1798 if (!bar)
1799 return log_oom();
1800
1801 start_array = new(size_t, context->n_partitions);
1802 if (!start_array)
1803 return log_oom();
1804
1805 LIST_FOREACH(partitions, p, context->partitions)
1806 context_bar_char_process_partition(context, bar, c, p, start_array + j++);
1807
1808 putc(' ', stdout);
1809
1810 for (size_t i = 0; i < c; i++) {
1811 if (bar[i]) {
1812 if (last != bar[i])
1813 z = !z;
1814
1815 fputs(z ? ansi_green() : ansi_yellow(), stdout);
1816 fputs(special_glyph(SPECIAL_GLYPH_DARK_SHADE), stdout);
1817 } else {
1818 fputs(ansi_normal(), stdout);
1819 fputs(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE), stdout);
1820 }
1821
1822 last = bar[i];
1823 }
1824
1825 fputs(ansi_normal(), stdout);
1826 putc('\n', stdout);
1827
1828 for (size_t i = 0; i < context->n_partitions; i++) {
1829 _cleanup_free_ char **line = NULL;
1830
1831 line = new0(char*, c);
1832 if (!line)
1833 return log_oom();
1834
1835 j = 0;
1836 LIST_FOREACH(partitions, p, context->partitions) {
1837 _cleanup_free_ char *d = NULL;
1838 j++;
1839
1840 if (i < context->n_partitions - j) {
1841
1842 if (line[start_array[j-1]]) {
1843 const char *e;
1844
1845 /* Upgrade final corner to the right with a branch to the right */
1846 e = startswith(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_RIGHT));
1847 if (e) {
1848 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), e);
1849 if (!d)
1850 return log_oom();
1851 }
1852 }
1853
1854 if (!d) {
1855 d = strdup(special_glyph(SPECIAL_GLYPH_TREE_VERTICAL));
1856 if (!d)
1857 return log_oom();
1858 }
1859
1860 } else if (i == context->n_partitions - j) {
1861 _cleanup_free_ char *hint = NULL;
1862
1863 (void) partition_hint(p, node, &hint);
1864
1865 if (streq_ptr(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_VERTICAL)))
1866 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), " ", strna(hint));
1867 else
1868 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_RIGHT), " ", strna(hint));
1869
1870 if (!d)
1871 return log_oom();
1872 }
1873
1874 if (d)
1875 free_and_replace(line[start_array[j-1]], d);
1876 }
1877
1878 putc(' ', stdout);
1879
1880 j = 0;
1881 while (j < c) {
1882 if (line[j]) {
1883 fputs(line[j], stdout);
1884 j += utf8_console_width(line[j]);
1885 } else {
1886 putc(' ', stdout);
1887 j++;
1888 }
1889 }
1890
1891 putc('\n', stdout);
1892
1893 for (j = 0; j < c; j++)
1894 free(line[j]);
1895 }
1896
1897 return 0;
1898 }
1899
1900 static bool context_changed(const Context *context) {
1901 Partition *p;
1902
1903 LIST_FOREACH(partitions, p, context->partitions) {
1904 if (p->dropped)
1905 continue;
1906
1907 if (p->allocated_to_area)
1908 return true;
1909
1910 if (p->new_size != p->current_size)
1911 return true;
1912 }
1913
1914 return false;
1915 }
1916
1917 static int context_wipe_partition(Context *context, Partition *p) {
1918 _cleanup_(blkid_free_probep) blkid_probe probe = NULL;
1919 int r;
1920
1921 assert(context);
1922 assert(p);
1923 assert(!PARTITION_EXISTS(p)); /* Safety check: never wipe existing partitions */
1924
1925 probe = blkid_new_probe();
1926 if (!probe)
1927 return log_oom();
1928
1929 assert(p->offset != UINT64_MAX);
1930 assert(p->new_size != UINT64_MAX);
1931
1932 errno = 0;
1933 r = blkid_probe_set_device(probe, fdisk_get_devfd(context->fdisk_context), p->offset, p->new_size);
1934 if (r < 0)
1935 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to allocate device probe for partition %" PRIu64 ".", p->partno);
1936
1937 errno = 0;
1938 if (blkid_probe_enable_superblocks(probe, true) < 0 ||
1939 blkid_probe_set_superblocks_flags(probe, BLKID_SUBLKS_MAGIC|BLKID_SUBLKS_BADCSUM) < 0 ||
1940 blkid_probe_enable_partitions(probe, true) < 0 ||
1941 blkid_probe_set_partitions_flags(probe, BLKID_PARTS_MAGIC) < 0)
1942 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to enable superblock and partition probing for partition %" PRIu64 ".", p->partno);
1943
1944 for (;;) {
1945 errno = 0;
1946 r = blkid_do_probe(probe);
1947 if (r < 0)
1948 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe for file systems.");
1949 if (r > 0)
1950 break;
1951
1952 errno = 0;
1953 if (blkid_do_wipe(probe, false) < 0)
1954 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to wipe file system signature.");
1955 }
1956
1957 log_info("Successfully wiped file system signatures from partition %" PRIu64 ".", p->partno);
1958 return 0;
1959 }
1960
1961 static int context_discard_range(Context *context, uint64_t offset, uint64_t size) {
1962 struct stat st;
1963 int fd;
1964
1965 assert(context);
1966 assert(offset != UINT64_MAX);
1967 assert(size != UINT64_MAX);
1968
1969 if (size <= 0)
1970 return 0;
1971
1972 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
1973
1974 if (fstat(fd, &st) < 0)
1975 return -errno;
1976
1977 if (S_ISREG(st.st_mode)) {
1978 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, offset, size) < 0) {
1979 if (ERRNO_IS_NOT_SUPPORTED(errno))
1980 return -EOPNOTSUPP;
1981
1982 return -errno;
1983 }
1984
1985 return 1;
1986 }
1987
1988 if (S_ISBLK(st.st_mode)) {
1989 uint64_t range[2], end;
1990
1991 range[0] = round_up_size(offset, 512);
1992
1993 end = offset + size;
1994 if (end <= range[0])
1995 return 0;
1996
1997 range[1] = round_down_size(end - range[0], 512);
1998 if (range[1] <= 0)
1999 return 0;
2000
2001 if (ioctl(fd, BLKDISCARD, range) < 0) {
2002 if (ERRNO_IS_NOT_SUPPORTED(errno))
2003 return -EOPNOTSUPP;
2004
2005 return -errno;
2006 }
2007
2008 return 1;
2009 }
2010
2011 return -EOPNOTSUPP;
2012 }
2013
2014 static int context_discard_partition(Context *context, Partition *p) {
2015 int r;
2016
2017 assert(context);
2018 assert(p);
2019
2020 assert(p->offset != UINT64_MAX);
2021 assert(p->new_size != UINT64_MAX);
2022 assert(!PARTITION_EXISTS(p)); /* Safety check: never discard existing partitions */
2023
2024 if (!arg_discard)
2025 return 0;
2026
2027 r = context_discard_range(context, p->offset, p->new_size);
2028 if (r == -EOPNOTSUPP) {
2029 log_info("Storage does not support discarding, not discarding data in new partition %" PRIu64 ".", p->partno);
2030 return 0;
2031 }
2032 if (r == 0) {
2033 log_info("Partition %" PRIu64 " too short for discard, skipping.", p->partno);
2034 return 0;
2035 }
2036 if (r < 0)
2037 return log_error_errno(r, "Failed to discard data for new partition %" PRIu64 ".", p->partno);
2038
2039 log_info("Successfully discarded data from partition %" PRIu64 ".", p->partno);
2040 return 1;
2041 }
2042
2043 static int context_discard_gap_after(Context *context, Partition *p) {
2044 uint64_t gap, next = UINT64_MAX;
2045 Partition *q;
2046 int r;
2047
2048 assert(context);
2049 assert(!p || (p->offset != UINT64_MAX && p->new_size != UINT64_MAX));
2050
2051 if (p)
2052 gap = p->offset + p->new_size;
2053 else
2054 gap = context->start;
2055
2056 LIST_FOREACH(partitions, q, context->partitions) {
2057 if (q->dropped)
2058 continue;
2059
2060 assert(q->offset != UINT64_MAX);
2061 assert(q->new_size != UINT64_MAX);
2062
2063 if (q->offset < gap)
2064 continue;
2065
2066 if (next == UINT64_MAX || q->offset < next)
2067 next = q->offset;
2068 }
2069
2070 if (next == UINT64_MAX) {
2071 next = context->end;
2072 if (gap > next)
2073 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end.");
2074 }
2075
2076 assert(next >= gap);
2077 r = context_discard_range(context, gap, next - gap);
2078 if (r == -EOPNOTSUPP) {
2079 if (p)
2080 log_info("Storage does not support discarding, not discarding gap after partition %" PRIu64 ".", p->partno);
2081 else
2082 log_info("Storage does not support discarding, not discarding gap at beginning of disk.");
2083 return 0;
2084 }
2085 if (r == 0) /* Too short */
2086 return 0;
2087 if (r < 0) {
2088 if (p)
2089 return log_error_errno(r, "Failed to discard gap after partition %" PRIu64 ".", p->partno);
2090 else
2091 return log_error_errno(r, "Failed to discard gap at beginning of disk.");
2092 }
2093
2094 if (p)
2095 log_info("Successfully discarded gap after partition %" PRIu64 ".", p->partno);
2096 else
2097 log_info("Successfully discarded gap at beginning of disk.");
2098
2099 return 0;
2100 }
2101
2102 static int context_wipe_and_discard(Context *context, bool from_scratch) {
2103 Partition *p;
2104 int r;
2105
2106 assert(context);
2107
2108 /* Wipe and discard the contents of all partitions we are about to create. We skip the discarding if
2109 * we were supposed to start from scratch anyway, as in that case we just discard the whole block
2110 * device in one go early on. */
2111
2112 LIST_FOREACH(partitions, p, context->partitions) {
2113
2114 if (!p->allocated_to_area)
2115 continue;
2116
2117 if (!from_scratch) {
2118 r = context_discard_partition(context, p);
2119 if (r < 0)
2120 return r;
2121 }
2122
2123 r = context_wipe_partition(context, p);
2124 if (r < 0)
2125 return r;
2126
2127 if (!from_scratch) {
2128 r = context_discard_gap_after(context, p);
2129 if (r < 0)
2130 return r;
2131 }
2132 }
2133
2134 if (!from_scratch) {
2135 r = context_discard_gap_after(context, NULL);
2136 if (r < 0)
2137 return r;
2138 }
2139
2140 return 0;
2141 }
2142
2143 static int context_copy_blocks(Context *context) {
2144 Partition *p;
2145 int fd = -1, r;
2146
2147 assert(context);
2148
2149 /* Copy in file systems on the block level */
2150
2151 LIST_FOREACH(partitions, p, context->partitions) {
2152 char buf[FORMAT_BYTES_MAX];
2153
2154 if (p->copy_blocks_fd < 0)
2155 continue;
2156
2157 if (p->dropped)
2158 continue;
2159
2160 if (PARTITION_EXISTS(p)) /* Never copy over existing partitions */
2161 continue;
2162
2163 assert(p->new_size != UINT64_MAX);
2164 assert(p->copy_blocks_size != UINT64_MAX);
2165 assert(p->new_size >= p->copy_blocks_size);
2166
2167 if (fd < 0)
2168 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
2169
2170 if (lseek(fd, p->offset, SEEK_SET) == (off_t) -1)
2171 return log_error_errno(errno, "Failed to seek to partition offset: %m");
2172
2173 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);
2174
2175 r = copy_bytes_full(p->copy_blocks_fd, fd, p->copy_blocks_size, 0, NULL, NULL, NULL, NULL);
2176 if (r < 0)
2177 return log_error_errno(r, "Failed to copy in data from '%s': %m", p->copy_blocks_path);
2178
2179 log_info("Copying in of '%s' on block level completed.", p->copy_blocks_path);
2180 }
2181
2182 return 0;
2183 }
2184
2185 static int partition_acquire_uuid(Context *context, Partition *p, sd_id128_t *ret) {
2186 struct {
2187 sd_id128_t type_uuid;
2188 uint64_t counter;
2189 } _packed_ plaintext = {};
2190 union {
2191 unsigned char md[SHA256_DIGEST_LENGTH];
2192 sd_id128_t id;
2193 } result;
2194
2195 uint64_t k = 0;
2196 Partition *q;
2197 int r;
2198
2199 assert(context);
2200 assert(p);
2201 assert(ret);
2202
2203 /* Calculate a good UUID for the indicated partition. We want a certain degree of reproducibility,
2204 * hence we won't generate the UUIDs randomly. Instead we use a cryptographic hash (precisely:
2205 * HMAC-SHA256) to derive them from a single seed. The seed is generally the machine ID of the
2206 * installation we are processing, but if random behaviour is desired can be random, too. We use the
2207 * seed value as key for the HMAC (since the machine ID is something we generally don't want to leak)
2208 * and the partition type as plaintext. The partition type is suffixed with a counter (only for the
2209 * second and later partition of the same type) if we have more than one partition of the same
2210 * time. Or in other words:
2211 *
2212 * With:
2213 * SEED := /etc/machine-id
2214 *
2215 * If first partition instance of type TYPE_UUID:
2216 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID)
2217 *
2218 * For all later partition instances of type TYPE_UUID with INSTANCE being the LE64 encoded instance number:
2219 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID || INSTANCE)
2220 */
2221
2222 LIST_FOREACH(partitions, q, context->partitions) {
2223 if (p == q)
2224 break;
2225
2226 if (!sd_id128_equal(p->type_uuid, q->type_uuid))
2227 continue;
2228
2229 k++;
2230 }
2231
2232 plaintext.type_uuid = p->type_uuid;
2233 plaintext.counter = htole64(k);
2234
2235 if (!HMAC(EVP_sha256(),
2236 &context->seed, sizeof(context->seed),
2237 (const unsigned char*) &plaintext, k == 0 ? sizeof(sd_id128_t) : sizeof(plaintext),
2238 result.md, NULL))
2239 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "SHA256 calculation failed.");
2240
2241 /* Take the first half, mark it as v4 UUID */
2242 assert_cc(sizeof(result.md) == sizeof(result.id) * 2);
2243 result.id = id128_make_v4_uuid(result.id);
2244
2245 /* Ensure this partition UUID is actually unique, and there's no remaining partition from an earlier run? */
2246 LIST_FOREACH(partitions, q, context->partitions) {
2247 if (p == q)
2248 continue;
2249
2250 if (sd_id128_equal(q->current_uuid, result.id) ||
2251 sd_id128_equal(q->new_uuid, result.id)) {
2252 log_warning("Partition UUID calculated from seed for partition %" PRIu64 " exists already, reverting to randomized UUID.", p->partno);
2253
2254 r = sd_id128_randomize(&result.id);
2255 if (r < 0)
2256 return log_error_errno(r, "Failed to generate randomized UUID: %m");
2257
2258 break;
2259 }
2260 }
2261
2262 *ret = result.id;
2263 return 0;
2264 }
2265
2266 static int partition_acquire_label(Context *context, Partition *p, char **ret) {
2267 _cleanup_free_ char *label = NULL;
2268 const char *prefix;
2269 unsigned k = 1;
2270
2271 assert(context);
2272 assert(p);
2273 assert(ret);
2274
2275 prefix = gpt_partition_type_uuid_to_string(p->type_uuid);
2276 if (!prefix)
2277 prefix = "linux";
2278
2279 for (;;) {
2280 const char *ll = label ?: prefix;
2281 bool retry = false;
2282 Partition *q;
2283
2284 LIST_FOREACH(partitions, q, context->partitions) {
2285 if (p == q)
2286 break;
2287
2288 if (streq_ptr(ll, q->current_label) ||
2289 streq_ptr(ll, q->new_label)) {
2290 retry = true;
2291 break;
2292 }
2293 }
2294
2295 if (!retry)
2296 break;
2297
2298 label = mfree(label);
2299
2300
2301 if (asprintf(&label, "%s-%u", prefix, ++k) < 0)
2302 return log_oom();
2303 }
2304
2305 if (!label) {
2306 label = strdup(prefix);
2307 if (!label)
2308 return log_oom();
2309 }
2310
2311 *ret = TAKE_PTR(label);
2312 return 0;
2313 }
2314
2315 static int context_acquire_partition_uuids_and_labels(Context *context) {
2316 Partition *p;
2317 int r;
2318
2319 assert(context);
2320
2321 LIST_FOREACH(partitions, p, context->partitions) {
2322 /* Never touch foreign partitions */
2323 if (PARTITION_IS_FOREIGN(p)) {
2324 p->new_uuid = p->current_uuid;
2325
2326 if (p->current_label) {
2327 free(p->new_label);
2328 p->new_label = strdup(p->current_label);
2329 if (!p->new_label)
2330 return log_oom();
2331 }
2332
2333 continue;
2334 }
2335
2336 if (!sd_id128_is_null(p->current_uuid))
2337 p->new_uuid = p->current_uuid; /* Never change initialized UUIDs */
2338 else if (sd_id128_is_null(p->new_uuid)) {
2339 /* Not explicitly set by user! */
2340 r = partition_acquire_uuid(context, p, &p->new_uuid);
2341 if (r < 0)
2342 return r;
2343 }
2344
2345 if (!isempty(p->current_label)) {
2346 free(p->new_label);
2347 p->new_label = strdup(p->current_label); /* never change initialized labels */
2348 if (!p->new_label)
2349 return log_oom();
2350 } else if (!p->new_label) {
2351 /* Not explicitly set by user! */
2352
2353 r = partition_acquire_label(context, p, &p->new_label);
2354 if (r < 0)
2355 return r;
2356 }
2357 }
2358
2359 return 0;
2360 }
2361
2362 static int device_kernel_partitions_supported(int fd) {
2363 struct loop_info64 info;
2364 struct stat st;
2365
2366 assert(fd >= 0);
2367
2368 if (fstat(fd, &st) < 0)
2369 return log_error_errno(fd, "Failed to fstat() image file: %m");
2370 if (!S_ISBLK(st.st_mode))
2371 return -ENOTBLK; /* we do not log in this one special case about errors */
2372
2373 if (ioctl(fd, LOOP_GET_STATUS64, &info) < 0) {
2374
2375 if (ERRNO_IS_NOT_SUPPORTED(errno) || errno == EINVAL)
2376 return true; /* not a loopback device, let's assume partition are supported */
2377
2378 return log_error_errno(fd, "Failed to issue LOOP_GET_STATUS64 on block device: %m");
2379 }
2380
2381 #if HAVE_VALGRIND_MEMCHECK_H
2382 /* Valgrind currently doesn't know LOOP_GET_STATUS64. Remove this once it does */
2383 VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
2384 #endif
2385
2386 return FLAGS_SET(info.lo_flags, LO_FLAGS_PARTSCAN);
2387 }
2388
2389 static int context_write_partition_table(
2390 Context *context,
2391 const char *node,
2392 bool from_scratch) {
2393
2394 _cleanup_(fdisk_unref_tablep) struct fdisk_table *original_table = NULL;
2395 int capable, r;
2396 Partition *p;
2397
2398 assert(context);
2399
2400 if (arg_pretty > 0 ||
2401 (arg_pretty < 0 && isatty(STDOUT_FILENO) > 0)) {
2402
2403 if (context->n_partitions == 0)
2404 puts("Empty partition table.");
2405 else
2406 (void) context_dump_partitions(context, node);
2407
2408 putc('\n', stdout);
2409
2410 (void) context_dump_partition_bar(context, node);
2411 putc('\n', stdout);
2412 fflush(stdout);
2413 }
2414
2415 if (!from_scratch && !context_changed(context)) {
2416 log_info("No changes.");
2417 return 0;
2418 }
2419
2420 if (arg_dry_run) {
2421 log_notice("Refusing to repartition, please re-run with --dry-run=no.");
2422 return 0;
2423 }
2424
2425 log_info("Applying changes.");
2426
2427 if (from_scratch) {
2428 r = context_discard_range(context, 0, context->total);
2429 if (r == -EOPNOTSUPP)
2430 log_info("Storage does not support discarding, not discarding entire block device data.");
2431 else if (r < 0)
2432 return log_error_errno(r, "Failed to discard entire block device: %m");
2433 else if (r > 0)
2434 log_info("Discarded entire block device.");
2435 }
2436
2437 r = fdisk_get_partitions(context->fdisk_context, &original_table);
2438 if (r < 0)
2439 return log_error_errno(r, "Failed to acquire partition table: %m");
2440
2441 /* Wipe fs signatures and discard sectors where the new partitions are going to be placed and in the
2442 * gaps between partitions, just to be sure. */
2443 r = context_wipe_and_discard(context, from_scratch);
2444 if (r < 0)
2445 return r;
2446
2447 r = context_copy_blocks(context);
2448 if (r < 0)
2449 return r;
2450
2451 LIST_FOREACH(partitions, p, context->partitions) {
2452 if (p->dropped)
2453 continue;
2454
2455 assert(p->new_size != UINT64_MAX);
2456 assert(p->offset != UINT64_MAX);
2457 assert(p->partno != UINT64_MAX);
2458
2459 if (PARTITION_EXISTS(p)) {
2460 bool changed = false;
2461
2462 assert(p->current_partition);
2463
2464 if (p->new_size != p->current_size) {
2465 assert(p->new_size >= p->current_size);
2466 assert(p->new_size % 512 == 0);
2467
2468 r = fdisk_partition_size_explicit(p->current_partition, true);
2469 if (r < 0)
2470 return log_error_errno(r, "Failed to enable explicit sizing: %m");
2471
2472 r = fdisk_partition_set_size(p->current_partition, p->new_size / 512);
2473 if (r < 0)
2474 return log_error_errno(r, "Failed to grow partition: %m");
2475
2476 log_info("Growing existing partition %" PRIu64 ".", p->partno);
2477 changed = true;
2478 }
2479
2480 if (!sd_id128_equal(p->new_uuid, p->current_uuid)) {
2481 char buf[ID128_UUID_STRING_MAX];
2482
2483 assert(!sd_id128_is_null(p->new_uuid));
2484
2485 r = fdisk_partition_set_uuid(p->current_partition, id128_to_uuid_string(p->new_uuid, buf));
2486 if (r < 0)
2487 return log_error_errno(r, "Failed to set partition UUID: %m");
2488
2489 log_info("Initializing UUID of existing partition %" PRIu64 ".", p->partno);
2490 changed = true;
2491 }
2492
2493 if (!streq_ptr(p->new_label, p->current_label)) {
2494 assert(!isempty(p->new_label));
2495
2496 r = fdisk_partition_set_name(p->current_partition, p->new_label);
2497 if (r < 0)
2498 return log_error_errno(r, "Failed to set partition label: %m");
2499
2500 log_info("Setting partition label of existing partition %" PRIu64 ".", p->partno);
2501 changed = true;
2502 }
2503
2504 if (changed) {
2505 assert(!PARTITION_IS_FOREIGN(p)); /* never touch foreign partitions */
2506
2507 r = fdisk_set_partition(context->fdisk_context, p->partno, p->current_partition);
2508 if (r < 0)
2509 return log_error_errno(r, "Failed to update partition: %m");
2510 }
2511 } else {
2512 _cleanup_(fdisk_unref_partitionp) struct fdisk_partition *q = NULL;
2513 _cleanup_(fdisk_unref_parttypep) struct fdisk_parttype *t = NULL;
2514 char ids[ID128_UUID_STRING_MAX];
2515
2516 assert(!p->new_partition);
2517 assert(p->offset % 512 == 0);
2518 assert(p->new_size % 512 == 0);
2519 assert(!sd_id128_is_null(p->new_uuid));
2520 assert(!isempty(p->new_label));
2521
2522 t = fdisk_new_parttype();
2523 if (!t)
2524 return log_oom();
2525
2526 r = fdisk_parttype_set_typestr(t, id128_to_uuid_string(p->type_uuid, ids));
2527 if (r < 0)
2528 return log_error_errno(r, "Failed to initialize partition type: %m");
2529
2530 q = fdisk_new_partition();
2531 if (!q)
2532 return log_oom();
2533
2534 r = fdisk_partition_set_type(q, t);
2535 if (r < 0)
2536 return log_error_errno(r, "Failed to set partition type: %m");
2537
2538 r = fdisk_partition_size_explicit(q, true);
2539 if (r < 0)
2540 return log_error_errno(r, "Failed to enable explicit sizing: %m");
2541
2542 r = fdisk_partition_set_start(q, p->offset / 512);
2543 if (r < 0)
2544 return log_error_errno(r, "Failed to position partition: %m");
2545
2546 r = fdisk_partition_set_size(q, p->new_size / 512);
2547 if (r < 0)
2548 return log_error_errno(r, "Failed to grow partition: %m");
2549
2550 r = fdisk_partition_set_partno(q, p->partno);
2551 if (r < 0)
2552 return log_error_errno(r, "Failed to set partition number: %m");
2553
2554 r = fdisk_partition_set_uuid(q, id128_to_uuid_string(p->new_uuid, ids));
2555 if (r < 0)
2556 return log_error_errno(r, "Failed to set partition UUID: %m");
2557
2558 r = fdisk_partition_set_name(q, p->new_label);
2559 if (r < 0)
2560 return log_error_errno(r, "Failed to set partition label: %m");
2561
2562 log_info("Creating new partition %" PRIu64 ".", p->partno);
2563
2564 r = fdisk_add_partition(context->fdisk_context, q, NULL);
2565 if (r < 0)
2566 return log_error_errno(r, "Failed to add partition: %m");
2567
2568 assert(!p->new_partition);
2569 p->new_partition = TAKE_PTR(q);
2570 }
2571 }
2572
2573 log_info("Writing new partition table.");
2574
2575 r = fdisk_write_disklabel(context->fdisk_context);
2576 if (r < 0)
2577 return log_error_errno(r, "Failed to write partition table: %m");
2578
2579 capable = device_kernel_partitions_supported(fdisk_get_devfd(context->fdisk_context));
2580 if (capable == -ENOTBLK)
2581 log_debug("Not telling kernel to reread partition table, since we are not operating on a block device.");
2582 else if (capable < 0)
2583 return capable;
2584 else if (capable > 0) {
2585 log_info("Telling kernel to reread partition table.");
2586
2587 if (from_scratch)
2588 r = fdisk_reread_partition_table(context->fdisk_context);
2589 else
2590 r = fdisk_reread_changes(context->fdisk_context, original_table);
2591 if (r < 0)
2592 return log_error_errno(r, "Failed to reread partition table: %m");
2593 } else
2594 log_notice("Not telling kernel to reread partition table, because selected image does not support kernel partition block devices.");
2595
2596 log_info("All done.");
2597
2598 return 0;
2599 }
2600
2601 static int context_read_seed(Context *context, const char *root) {
2602 int r;
2603
2604 assert(context);
2605
2606 if (!sd_id128_is_null(context->seed))
2607 return 0;
2608
2609 if (!arg_randomize) {
2610 _cleanup_close_ int fd = -1;
2611
2612 fd = chase_symlinks_and_open("/etc/machine-id", root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC, NULL);
2613 if (fd == -ENOENT)
2614 log_info("No machine ID set, using randomized partition UUIDs.");
2615 else if (fd < 0)
2616 return log_error_errno(fd, "Failed to determine machine ID of image: %m");
2617 else {
2618 r = id128_read_fd(fd, ID128_PLAIN, &context->seed);
2619 if (r == -ENOMEDIUM)
2620 log_info("No machine ID set, using randomized partition UUIDs.");
2621 else if (r < 0)
2622 return log_error_errno(r, "Failed to parse machine ID of image: %m");
2623
2624 return 0;
2625 }
2626 }
2627
2628 r = sd_id128_randomize(&context->seed);
2629 if (r < 0)
2630 return log_error_errno(r, "Failed to generate randomized seed: %m");
2631
2632 return 0;
2633 }
2634
2635 static int context_factory_reset(Context *context, bool from_scratch) {
2636 Partition *p;
2637 size_t n = 0;
2638 int r;
2639
2640 assert(context);
2641
2642 if (arg_factory_reset <= 0)
2643 return 0;
2644
2645 if (from_scratch) /* Nothing to reset if we start from scratch */
2646 return 0;
2647
2648 if (arg_dry_run) {
2649 log_notice("Refusing to factory reset, please re-run with --dry-run=no.");
2650 return 0;
2651 }
2652
2653 log_info("Applying factory reset.");
2654
2655 LIST_FOREACH(partitions, p, context->partitions) {
2656
2657 if (!p->factory_reset || !PARTITION_EXISTS(p))
2658 continue;
2659
2660 assert(p->partno != UINT64_MAX);
2661
2662 log_info("Removing partition %" PRIu64 " for factory reset.", p->partno);
2663
2664 r = fdisk_delete_partition(context->fdisk_context, p->partno);
2665 if (r < 0)
2666 return log_error_errno(r, "Failed to remove partition %" PRIu64 ": %m", p->partno);
2667
2668 n++;
2669 }
2670
2671 if (n == 0) {
2672 log_info("Factory reset requested, but no partitions to delete found.");
2673 return 0;
2674 }
2675
2676 r = fdisk_write_disklabel(context->fdisk_context);
2677 if (r < 0)
2678 return log_error_errno(r, "Failed to write disk label: %m");
2679
2680 log_info("Successfully deleted %zu partitions.", n);
2681 return 1;
2682 }
2683
2684 static int context_can_factory_reset(Context *context) {
2685 Partition *p;
2686
2687 assert(context);
2688
2689 LIST_FOREACH(partitions, p, context->partitions)
2690 if (p->factory_reset && PARTITION_EXISTS(p))
2691 return true;
2692
2693 return false;
2694 }
2695
2696 static int context_open_copy_block_paths(Context *context) {
2697 Partition *p;
2698 int r;
2699
2700 assert(context);
2701
2702 LIST_FOREACH(partitions, p, context->partitions) {
2703 _cleanup_close_ int source_fd = -1;
2704 uint64_t size;
2705 struct stat st;
2706
2707 assert(p->copy_blocks_fd < 0);
2708 assert(p->copy_blocks_size == UINT64_MAX);
2709
2710 if (PARTITION_EXISTS(p)) /* Never copy over partitions that already exist! */
2711 continue;
2712
2713 if (!p->copy_blocks_path)
2714 continue;
2715
2716 source_fd = open(p->copy_blocks_path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
2717 if (source_fd < 0)
2718 return log_error_errno(errno, "Failed to open block copy file '%s': %m", p->copy_blocks_path);
2719
2720 if (fstat(source_fd, &st) < 0)
2721 return log_error_errno(errno, "Failed to stat block copy file '%s': %m", p->copy_blocks_path);
2722
2723 if (S_ISDIR(st.st_mode)) {
2724 _cleanup_free_ char *bdev = NULL;
2725
2726 /* If the file is a directory, automatically find the backing block device */
2727
2728 if (major(st.st_dev) != 0)
2729 r = device_path_make_major_minor(S_IFBLK, st.st_dev, &bdev);
2730 else {
2731 dev_t devt;
2732
2733 /* Special support for btrfs */
2734
2735 r = btrfs_get_block_device_fd(source_fd, &devt);
2736 if (r < 0)
2737 return log_error_errno(r, "Unable to determine backing block device of '%s': %m", p->copy_blocks_path);
2738
2739 r = device_path_make_major_minor(S_IFBLK, devt, &bdev);
2740 }
2741 if (r < 0)
2742 return log_error_errno(r, "Failed to determine block device path for block device backing '%s': %m", p->copy_blocks_path);
2743
2744 safe_close(source_fd);
2745
2746 source_fd = open(bdev, O_RDONLY|O_CLOEXEC|O_NOCTTY);
2747 if (source_fd < 0)
2748 return log_error_errno(errno, "Failed to open block device '%s': %m", bdev);
2749
2750 if (fstat(source_fd, &st) < 0)
2751 return log_error_errno(errno, "Failed to stat block device '%s': %m", bdev);
2752
2753 if (!S_ISBLK(st.st_mode))
2754 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), "Block device '%s' is not actually a block device, refusing.", bdev);
2755 }
2756
2757 if (S_ISREG(st.st_mode))
2758 size = st.st_size;
2759 else if (S_ISBLK(st.st_mode)) {
2760 if (ioctl(source_fd, BLKGETSIZE64, &size) != 0)
2761 return log_error_errno(errno, "Failed to determine size of block device to copy from: %m");
2762 } else
2763 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);
2764
2765 if (size <= 0)
2766 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File to copy bytes from '%s' has zero size, refusing.", p->copy_blocks_path);
2767 if (size % 512 != 0)
2768 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);
2769
2770 p->copy_blocks_fd = TAKE_FD(source_fd);
2771 p->copy_blocks_size = size;
2772 }
2773
2774 return 0;
2775 }
2776
2777 static int help(void) {
2778 _cleanup_free_ char *link = NULL;
2779 int r;
2780
2781 r = terminal_urlify_man("systemd-repart", "1", &link);
2782 if (r < 0)
2783 return log_oom();
2784
2785 printf("%s [OPTIONS...] [DEVICE]\n"
2786 "\n%sGrow and add partitions to partition table.%s\n\n"
2787 " -h --help Show this help\n"
2788 " --version Show package version\n"
2789 " --dry-run=BOOL Whether to run dry-run operation\n"
2790 " --empty=MODE One of refuse, allow, require, force, create; controls\n"
2791 " how to handle empty disks lacking partition tables\n"
2792 " --discard=BOOL Whether to discard backing blocks for new partitions\n"
2793 " --pretty=BOOL Whether to show pretty summary before executing operation\n"
2794 " --factory-reset=BOOL Whether to remove data partitions before recreating\n"
2795 " them\n"
2796 " --can-factory-reset Test whether factory reset is defined\n"
2797 " --root=PATH Operate relative to root path\n"
2798 " --definitions=DIR Find partitions in specified directory\n"
2799 " --seed=UUID 128bit seed UUID to derive all UUIDs from\n"
2800 " --size=BYTES Grow loopback file to specified size\n"
2801 "\nSee the %s for details.\n"
2802 , program_invocation_short_name
2803 , ansi_highlight(), ansi_normal()
2804 , link
2805 );
2806
2807 return 0;
2808 }
2809
2810 static int parse_argv(int argc, char *argv[]) {
2811
2812 enum {
2813 ARG_VERSION = 0x100,
2814 ARG_DRY_RUN,
2815 ARG_EMPTY,
2816 ARG_DISCARD,
2817 ARG_FACTORY_RESET,
2818 ARG_CAN_FACTORY_RESET,
2819 ARG_ROOT,
2820 ARG_SEED,
2821 ARG_PRETTY,
2822 ARG_DEFINITIONS,
2823 ARG_SIZE,
2824 };
2825
2826 static const struct option options[] = {
2827 { "help", no_argument, NULL, 'h' },
2828 { "version", no_argument, NULL, ARG_VERSION },
2829 { "dry-run", required_argument, NULL, ARG_DRY_RUN },
2830 { "empty", required_argument, NULL, ARG_EMPTY },
2831 { "discard", required_argument, NULL, ARG_DISCARD },
2832 { "factory-reset", required_argument, NULL, ARG_FACTORY_RESET },
2833 { "can-factory-reset", no_argument, NULL, ARG_CAN_FACTORY_RESET },
2834 { "root", required_argument, NULL, ARG_ROOT },
2835 { "seed", required_argument, NULL, ARG_SEED },
2836 { "pretty", required_argument, NULL, ARG_PRETTY },
2837 { "definitions", required_argument, NULL, ARG_DEFINITIONS },
2838 { "size", required_argument, NULL, ARG_SIZE },
2839 {}
2840 };
2841
2842 int c, r, dry_run = -1;
2843
2844 assert(argc >= 0);
2845 assert(argv);
2846
2847 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
2848
2849 switch (c) {
2850
2851 case 'h':
2852 return help();
2853
2854 case ARG_VERSION:
2855 return version();
2856
2857 case ARG_DRY_RUN:
2858 r = parse_boolean(optarg);
2859 if (r < 0)
2860 return log_error_errno(r, "Failed to parse --dry-run= parameter: %s", optarg);
2861
2862 dry_run = r;
2863 break;
2864
2865 case ARG_EMPTY:
2866 if (isempty(optarg) || streq(optarg, "refuse"))
2867 arg_empty = EMPTY_REFUSE;
2868 else if (streq(optarg, "allow"))
2869 arg_empty = EMPTY_ALLOW;
2870 else if (streq(optarg, "require"))
2871 arg_empty = EMPTY_REQUIRE;
2872 else if (streq(optarg, "force"))
2873 arg_empty = EMPTY_FORCE;
2874 else if (streq(optarg, "create")) {
2875 arg_empty = EMPTY_CREATE;
2876
2877 if (dry_run < 0)
2878 dry_run = false; /* Imply --dry-run=no if we create the loopback file
2879 * anew. After all we cannot really break anyone's
2880 * partition tables that way. */
2881 } else
2882 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2883 "Failed to parse --empty= parameter: %s", optarg);
2884 break;
2885
2886 case ARG_DISCARD:
2887 r = parse_boolean(optarg);
2888 if (r < 0)
2889 return log_error_errno(r, "Failed to parse --discard= parameter: %s", optarg);
2890
2891 arg_discard = r;
2892 break;
2893
2894 case ARG_FACTORY_RESET:
2895 r = parse_boolean(optarg);
2896 if (r < 0)
2897 return log_error_errno(r, "Failed to parse --factory-reset= parameter: %s", optarg);
2898
2899 arg_factory_reset = r;
2900 break;
2901
2902 case ARG_CAN_FACTORY_RESET:
2903 arg_can_factory_reset = true;
2904 break;
2905
2906 case ARG_ROOT:
2907 r = parse_path_argument_and_warn(optarg, false, &arg_root);
2908 if (r < 0)
2909 return r;
2910 break;
2911
2912 case ARG_SEED:
2913 if (isempty(optarg)) {
2914 arg_seed = SD_ID128_NULL;
2915 arg_randomize = false;
2916 } else if (streq(optarg, "random"))
2917 arg_randomize = true;
2918 else {
2919 r = sd_id128_from_string(optarg, &arg_seed);
2920 if (r < 0)
2921 return log_error_errno(r, "Failed to parse seed: %s", optarg);
2922
2923 arg_randomize = false;
2924 }
2925
2926 break;
2927
2928 case ARG_PRETTY:
2929 r = parse_boolean(optarg);
2930 if (r < 0)
2931 return log_error_errno(r, "Failed to parse --pretty= parameter: %s", optarg);
2932
2933 arg_pretty = r;
2934 break;
2935
2936 case ARG_DEFINITIONS:
2937 r = parse_path_argument_and_warn(optarg, false, &arg_definitions);
2938 if (r < 0)
2939 return r;
2940 break;
2941
2942 case ARG_SIZE: {
2943 uint64_t parsed, rounded;
2944
2945 r = parse_size(optarg, 1024, &parsed);
2946 if (r < 0)
2947 return log_error_errno(r, "Failed to parse --size= parameter: %s", optarg);
2948
2949 rounded = round_up_size(parsed, 4096);
2950 if (rounded == 0)
2951 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too small, refusing.");
2952 if (rounded == UINT64_MAX)
2953 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too large, refusing.");
2954
2955 if (rounded != parsed)
2956 log_warning("Specified size is not a multiple of 4096, rounding up automatically. (%" PRIu64 " → %" PRIu64 ")",
2957 parsed, rounded);
2958
2959 arg_size = rounded;
2960 break;
2961 }
2962
2963 case '?':
2964 return -EINVAL;
2965
2966 default:
2967 assert_not_reached("Unhandled option");
2968 }
2969
2970 if (argc - optind > 1)
2971 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2972 "Expected at most one argument, the path to the block device.");
2973
2974 if (arg_factory_reset > 0 && IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE))
2975 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2976 "Combination of --factory-reset=yes and --empty=force/--empty=require/--empty=create is invalid.");
2977
2978 if (arg_can_factory_reset)
2979 arg_dry_run = true; /* When --can-factory-reset is specified we don't make changes, hence
2980 * non-dry-run mode makes no sense. Thus, imply dry run mode so that we
2981 * open things strictly read-only. */
2982 else if (dry_run >= 0)
2983 arg_dry_run = dry_run;
2984
2985 if (arg_empty == EMPTY_CREATE && arg_size == UINT64_MAX)
2986 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2987 "If --empty=create is specified, --size= must be specified, too.");
2988
2989 arg_node = argc > optind ? argv[optind] : NULL;
2990
2991 if (IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE) && !arg_node)
2992 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2993 "A path to a device node or loopback file must be specified when --empty=force, --empty=require or --empty=create are used.");
2994
2995 return 1;
2996 }
2997
2998 static int parse_proc_cmdline_factory_reset(void) {
2999 bool b;
3000 int r;
3001
3002 if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */
3003 return 0;
3004
3005 if (!in_initrd()) /* Never honour kernel command line factory reset request outside of the initrd */
3006 return 0;
3007
3008 r = proc_cmdline_get_bool("systemd.factory_reset", &b);
3009 if (r < 0)
3010 return log_error_errno(r, "Failed to parse systemd.factory_reset kernel command line argument: %m");
3011 if (r > 0) {
3012 arg_factory_reset = b;
3013
3014 if (b)
3015 log_notice("Honouring factory reset requested via kernel command line.");
3016 }
3017
3018 return 0;
3019 }
3020
3021 static int parse_efi_variable_factory_reset(void) {
3022 _cleanup_free_ char *value = NULL;
3023 int r;
3024
3025 if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */
3026 return 0;
3027
3028 if (!in_initrd()) /* Never honour EFI variable factory reset request outside of the initrd */
3029 return 0;
3030
3031 r = efi_get_variable_string(EFI_VENDOR_SYSTEMD, "FactoryReset", &value);
3032 if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
3033 return 0;
3034 if (r < 0)
3035 return log_error_errno(r, "Failed to read EFI variable FactoryReset: %m");
3036
3037 r = parse_boolean(value);
3038 if (r < 0)
3039 return log_error_errno(r, "Failed to parse EFI variable FactoryReset: %m");
3040
3041 arg_factory_reset = r;
3042 if (r)
3043 log_notice("Honouring factory reset requested via EFI variable FactoryReset: %m");
3044
3045 return 0;
3046 }
3047
3048 static int remove_efi_variable_factory_reset(void) {
3049 int r;
3050
3051 r = efi_set_variable(EFI_VENDOR_SYSTEMD, "FactoryReset", NULL, 0);
3052 if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
3053 return 0;
3054 if (r < 0)
3055 return log_error_errno(r, "Failed to remove EFI variable FactoryReset: %m");
3056
3057 log_info("Successfully unset EFI variable FactoryReset.");
3058 return 0;
3059 }
3060
3061 static int acquire_root_devno(const char *p, int mode, char **ret, int *ret_fd) {
3062 _cleanup_close_ int fd = -1;
3063 struct stat st;
3064 dev_t devno, fd_devno = (mode_t) -1;
3065 int r;
3066
3067 assert(p);
3068 assert(ret);
3069 assert(ret_fd);
3070
3071 fd = open(p, mode);
3072 if (fd < 0)
3073 return -errno;
3074
3075 if (fstat(fd, &st) < 0)
3076 return -errno;
3077
3078 if (S_ISREG(st.st_mode)) {
3079 char *s;
3080
3081 s = strdup(p);
3082 if (!s)
3083 return log_oom();
3084
3085 *ret = s;
3086 *ret_fd = TAKE_FD(fd);
3087
3088 return 0;
3089 }
3090
3091 if (S_ISBLK(st.st_mode))
3092 fd_devno = devno = st.st_rdev;
3093 else if (S_ISDIR(st.st_mode)) {
3094
3095 devno = st.st_dev;
3096 if (major(devno) == 0) {
3097 r = btrfs_get_block_device_fd(fd, &devno);
3098 if (r == -ENOTTY) /* not btrfs */
3099 return -ENODEV;
3100 if (r < 0)
3101 return r;
3102 }
3103 } else
3104 return -ENOTBLK;
3105
3106 /* From dm-crypt to backing partition */
3107 r = block_get_originating(devno, &devno);
3108 if (r < 0)
3109 log_debug_errno(r, "Failed to find underlying block device for '%s', ignoring: %m", p);
3110
3111 /* From partition to whole disk containing it */
3112 r = block_get_whole_disk(devno, &devno);
3113 if (r < 0)
3114 log_debug_errno(r, "Failed to find whole disk block device for '%s', ignoring: %m", p);
3115
3116 r = device_path_make_canonical(S_IFBLK, devno, ret);
3117 if (r < 0)
3118 return log_debug_errno(r, "Failed to determine canonical path for '%s': %m", p);
3119
3120 /* Only if we still lock at the same block device we can reuse the fd. Otherwise return an
3121 * invalidated fd. */
3122 *ret_fd = fd_devno != (mode_t) -1 && fd_devno == devno ? TAKE_FD(fd) : -1;
3123 return 0;
3124 }
3125
3126 static int find_root(char **ret, int *ret_fd) {
3127 const char *t;
3128 int r;
3129
3130 assert(ret);
3131 assert(ret_fd);
3132
3133 if (arg_node) {
3134 if (arg_empty == EMPTY_CREATE) {
3135 _cleanup_close_ int fd = -1;
3136 _cleanup_free_ char *s = NULL;
3137
3138 s = strdup(arg_node);
3139 if (!s)
3140 return log_oom();
3141
3142 fd = open(arg_node, O_RDONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOFOLLOW, 0777);
3143 if (fd < 0)
3144 return log_error_errno(errno, "Failed to create '%s': %m", arg_node);
3145
3146 *ret = TAKE_PTR(s);
3147 *ret_fd = TAKE_FD(fd);
3148 return 0;
3149 }
3150
3151 r = acquire_root_devno(arg_node, O_RDONLY|O_CLOEXEC, ret, ret_fd);
3152 if (r < 0)
3153 return log_error_errno(r, "Failed to determine backing device of %s: %m", arg_node);
3154
3155 return 0;
3156 }
3157
3158 assert(IN_SET(arg_empty, EMPTY_REFUSE, EMPTY_ALLOW));
3159
3160 /* Let's search for the root device. We look for two cases here: first in /, and then in /usr. The
3161 * latter we check for cases where / is a tmpfs and only /usr is an actual persistent block device
3162 * (think: volatile setups) */
3163
3164 FOREACH_STRING(t, "/", "/usr") {
3165 _cleanup_free_ char *j = NULL;
3166 const char *p;
3167
3168 if (in_initrd()) {
3169 j = path_join("/sysroot", t);
3170 if (!j)
3171 return log_oom();
3172
3173 p = j;
3174 } else
3175 p = t;
3176
3177 r = acquire_root_devno(p, O_RDONLY|O_DIRECTORY|O_CLOEXEC, ret, ret_fd);
3178 if (r < 0) {
3179 if (r != -ENODEV)
3180 return log_error_errno(r, "Failed to determine backing device of %s: %m", p);
3181 } else
3182 return 0;
3183 }
3184
3185 return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "Failed to discover root block device.");
3186 }
3187
3188 static int resize_backing_fd(const char *node, int *fd) {
3189 char buf1[FORMAT_BYTES_MAX], buf2[FORMAT_BYTES_MAX];
3190 _cleanup_close_ int writable_fd = -1;
3191 struct stat st;
3192 int r;
3193
3194 assert(node);
3195 assert(fd);
3196
3197 if (arg_size == UINT64_MAX) /* Nothing to do */
3198 return 0;
3199
3200 if (*fd < 0) {
3201 /* Open the file if we haven't opened it yet. Note that we open it read-only here, just to
3202 * keep a reference to the file we can pass around. */
3203 *fd = open(node, O_RDONLY|O_CLOEXEC);
3204 if (*fd < 0)
3205 return log_error_errno(errno, "Failed to open '%s' in order to adjust size: %m", node);
3206 }
3207
3208 if (fstat(*fd, &st) < 0)
3209 return log_error_errno(errno, "Failed to stat '%s': %m", node);
3210
3211 r = stat_verify_regular(&st);
3212 if (r < 0)
3213 return log_error_errno(r, "Specified path '%s' is not a regular file, cannot resize: %m", node);
3214
3215 assert_se(format_bytes(buf1, sizeof(buf1), st.st_size));
3216 assert_se(format_bytes(buf2, sizeof(buf2), arg_size));
3217
3218 if ((uint64_t) st.st_size >= arg_size) {
3219 log_info("File '%s' already is of requested size or larger, not growing. (%s >= %s)", node, buf1, buf2);
3220 return 0;
3221 }
3222
3223 /* The file descriptor is read-only. In order to grow the file we need to have a writable fd. We
3224 * reopen the file for that temporarily. We keep the writable fd only open for this operation though,
3225 * as fdisk can't accept it anyway. */
3226
3227 writable_fd = fd_reopen(*fd, O_WRONLY|O_CLOEXEC);
3228 if (writable_fd < 0)
3229 return log_error_errno(writable_fd, "Failed to reopen backing file '%s' writable: %m", node);
3230
3231 if (!arg_discard) {
3232 if (fallocate(writable_fd, 0, 0, arg_size) < 0) {
3233 if (!ERRNO_IS_NOT_SUPPORTED(errno))
3234 return log_error_errno(errno, "Failed to grow '%s' from %s to %s by allocation: %m",
3235 node, buf1, buf2);
3236
3237 /* Fallback to truncation, if fallocate() is not supported. */
3238 log_debug("Backing file system does not support fallocate(), falling back to ftruncate().");
3239 } else {
3240 if (st.st_size == 0) /* Likely regular file just created by us */
3241 log_info("Allocated %s for '%s'.", buf2, node);
3242 else
3243 log_info("File '%s' grown from %s to %s by allocation.", node, buf1, buf2);
3244
3245 return 1;
3246 }
3247 }
3248
3249 if (ftruncate(writable_fd, arg_size) < 0)
3250 return log_error_errno(errno, "Failed to grow '%s' from %s to %s by truncation: %m",
3251 node, buf1, buf2);
3252
3253 if (st.st_size == 0) /* Likely regular file just created by us */
3254 log_info("Sized '%s' to %s.", node, buf2);
3255 else
3256 log_info("File '%s' grown from %s to %s by truncation.", node, buf1, buf2);
3257
3258 return 1;
3259 }
3260
3261 static int run(int argc, char *argv[]) {
3262 _cleanup_(context_freep) Context* context = NULL;
3263 _cleanup_free_ char *node = NULL;
3264 _cleanup_close_ int backing_fd = -1;
3265 bool from_scratch;
3266 int r;
3267
3268 log_show_color(true);
3269 log_parse_environment();
3270 log_open();
3271
3272 if (in_initrd()) {
3273 /* Default to operation on /sysroot when invoked in the initrd! */
3274 arg_root = strdup("/sysroot");
3275 if (!arg_root)
3276 return log_oom();
3277 }
3278
3279 r = parse_argv(argc, argv);
3280 if (r <= 0)
3281 return r;
3282
3283 r = parse_proc_cmdline_factory_reset();
3284 if (r < 0)
3285 return r;
3286
3287 r = parse_efi_variable_factory_reset();
3288 if (r < 0)
3289 return r;
3290
3291 context = context_new(arg_seed);
3292 if (!context)
3293 return log_oom();
3294
3295 r = context_read_definitions(context, arg_definitions, arg_root);
3296 if (r < 0)
3297 return r;
3298
3299 if (context->n_partitions <= 0 && arg_empty == EMPTY_REFUSE) {
3300 log_info("Didn't find any partition definition files, nothing to do.");
3301 return 0;
3302 }
3303
3304 r = find_root(&node, &backing_fd);
3305 if (r < 0)
3306 return r;
3307
3308 if (arg_size != UINT64_MAX) {
3309 r = resize_backing_fd(node, &backing_fd);
3310 if (r < 0)
3311 return r;
3312 }
3313
3314 r = context_load_partition_table(context, node, &backing_fd);
3315 if (r == -EHWPOISON)
3316 return 77; /* Special return value which means "Not GPT, so not doing anything". This isn't
3317 * really an error when called at boot. */
3318 if (r < 0)
3319 return r;
3320 from_scratch = r > 0; /* Starting from scratch */
3321
3322 if (arg_can_factory_reset) {
3323 r = context_can_factory_reset(context);
3324 if (r < 0)
3325 return r;
3326 if (r == 0)
3327 return EXIT_FAILURE;
3328
3329 return 0;
3330 }
3331
3332 r = context_factory_reset(context, from_scratch);
3333 if (r < 0)
3334 return r;
3335 if (r > 0) {
3336 /* We actually did a factory reset! */
3337 r = remove_efi_variable_factory_reset();
3338 if (r < 0)
3339 return r;
3340
3341 /* Reload the reduced partition table */
3342 context_unload_partition_table(context);
3343 r = context_load_partition_table(context, node, &backing_fd);
3344 if (r < 0)
3345 return r;
3346 }
3347
3348 #if 0
3349 (void) context_dump_partitions(context, node);
3350 putchar('\n');
3351 #endif
3352
3353 r = context_read_seed(context, arg_root);
3354 if (r < 0)
3355 return r;
3356
3357 /* Open all files to copy blocks from now, since we want to take their size into consideration */
3358 r = context_open_copy_block_paths(context);
3359 if (r < 0)
3360 return r;
3361
3362 /* First try to fit new partitions in, dropping by priority until it fits */
3363 for (;;) {
3364 if (context_allocate_partitions(context))
3365 break; /* Success! */
3366
3367 if (!context_drop_one_priority(context))
3368 return log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
3369 "Can't fit requested partitions into free space, refusing.");
3370 }
3371
3372 /* Now assign free space according to the weight logic */
3373 r = context_grow_partitions(context);
3374 if (r < 0)
3375 return r;
3376
3377 /* Now calculate where each partition gets placed */
3378 context_place_partitions(context);
3379
3380 /* Make sure each partition has a unique UUID and unique label */
3381 r = context_acquire_partition_uuids_and_labels(context);
3382 if (r < 0)
3383 return r;
3384
3385 r = context_write_partition_table(context, node, from_scratch);
3386 if (r < 0)
3387 return r;
3388
3389 return 0;
3390 }
3391
3392 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);