1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #if HAVE_VALGRIND_MEMCHECK_H
4 #include <valgrind/memcheck.h>
11 #include <linux/loop.h>
13 #include <sys/ioctl.h>
16 #include <openssl/hmac.h>
17 #include <openssl/sha.h>
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"
29 #include "errno-util.h"
31 #include "format-table.h"
32 #include "format-util.h"
35 #include "id128-util.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"
49 #include "terminal-util.h"
52 /* If not configured otherwise use a minimal partition size of 10M */
53 #define DEFAULT_MIN_SIZE (10*1024*1024)
55 /* Hard lower limit for new partition sizes */
56 #define HARD_MIN_SIZE 4096
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. */
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
;
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
;
83 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
84 STATIC_DESTRUCTOR_REGISTER(arg_definitions
, freep
);
86 typedef struct Partition Partition
;
87 typedef struct FreeArea FreeArea
;
88 typedef struct Context Context
;
91 char *definition_path
;
94 sd_id128_t current_uuid
, new_uuid
;
95 char *current_label
, *new_label
;
101 uint32_t weight
, padding_weight
;
103 uint64_t current_size
, new_size
;
104 uint64_t size_min
, size_max
;
106 uint64_t current_padding
, new_padding
;
107 uint64_t padding_min
, padding_max
;
112 struct fdisk_partition
*current_partition
;
113 struct fdisk_partition
*new_partition
;
114 FreeArea
*padding_area
;
115 FreeArea
*allocated_to_area
;
117 char *copy_blocks_path
;
119 uint64_t copy_blocks_size
;
121 LIST_FIELDS(Partition
, partitions
);
124 #define PARTITION_IS_FOREIGN(p) (!(p)->definition_path)
125 #define PARTITION_EXISTS(p) (!!(p)->current_partition)
134 LIST_HEAD(Partition
, partitions
);
137 FreeArea
**free_areas
;
138 size_t n_free_areas
, n_allocated_free_areas
;
140 uint64_t start
, end
, total
;
142 struct fdisk_context
*fdisk_context
;
147 static uint64_t round_down_size(uint64_t v
, uint64_t p
) {
151 static uint64_t round_up_size(uint64_t v
, uint64_t p
) {
153 v
= DIV_ROUND_UP(v
, p
);
155 if (v
> UINT64_MAX
/ p
)
156 return UINT64_MAX
; /* overflow */
161 static Partition
*partition_new(void) {
164 p
= new(Partition
, 1);
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
,
188 static Partition
* partition_free(Partition
*p
) {
192 free(p
->current_label
);
194 free(p
->definition_path
);
196 if (p
->current_partition
)
197 fdisk_unref_partition(p
->current_partition
);
198 if (p
->new_partition
)
199 fdisk_unref_partition(p
->new_partition
);
201 free(p
->copy_blocks_path
);
202 safe_close(p
->copy_blocks_fd
);
207 static Partition
* partition_unlink_and_free(Context
*context
, Partition
*p
) {
211 LIST_REMOVE(partitions
, context
->partitions
, p
);
213 assert(context
->n_partitions
> 0);
214 context
->n_partitions
--;
216 return partition_free(p
);
219 DEFINE_TRIVIAL_CLEANUP_FUNC(Partition
*, partition_free
);
221 static Context
*context_new(sd_id128_t seed
) {
224 context
= new(Context
, 1);
228 *context
= (Context
) {
238 static void context_free_free_areas(Context
*context
) {
241 for (size_t i
= 0; i
< context
->n_free_areas
; i
++)
242 free(context
->free_areas
[i
]);
244 context
->free_areas
= mfree(context
->free_areas
);
245 context
->n_free_areas
= 0;
246 context
->n_allocated_free_areas
= 0;
249 static Context
*context_free(Context
*context
) {
253 while (context
->partitions
)
254 partition_unlink_and_free(context
, context
->partitions
);
255 assert(context
->n_partitions
== 0);
257 context_free_free_areas(context
);
259 if (context
->fdisk_context
)
260 fdisk_unref_context(context
->fdisk_context
);
262 return mfree(context
);
265 DEFINE_TRIVIAL_CLEANUP_FUNC(Context
*, context_free
);
267 static int context_add_free_area(
275 assert(!after
|| !after
->padding_area
);
277 if (!GREEDY_REALLOC(context
->free_areas
, context
->n_allocated_free_areas
, context
->n_free_areas
+ 1))
280 a
= new(FreeArea
, 1);
289 context
->free_areas
[context
->n_free_areas
++] = a
;
292 after
->padding_area
= a
;
297 static bool context_drop_one_priority(Context
*context
) {
298 int32_t priority
= 0;
302 LIST_FOREACH(partitions
, p
, context
->partitions
) {
305 if (p
->priority
< priority
)
307 if (p
->priority
== priority
) {
308 exists
= exists
|| PARTITION_EXISTS(p
);
312 priority
= p
->priority
;
313 exists
= PARTITION_EXISTS(p
);
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
)
321 LIST_FOREACH(partitions
, p
, context
->partitions
) {
322 if (p
->priority
< priority
)
329 log_info("Can't fit partition %s of priority %" PRIi32
", dropping.", p
->definition_path
, p
->priority
);
335 static uint64_t partition_min_size(const Partition
*p
) {
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
342 * DEFAULT_MIN_SIZE is the default SizeMin= we configure if nothing else is specified. */
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
;
350 sz
= p
->current_size
!= UINT64_MAX
? p
->current_size
: HARD_MIN_SIZE
;
352 if (p
->copy_blocks_size
!= UINT64_MAX
)
353 sz
= MAX(p
->copy_blocks_size
, sz
);
355 return MAX(p
->size_min
!= UINT64_MAX
? p
->size_min
: DEFAULT_MIN_SIZE
, sz
);
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. */
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
;
369 if (p
->current_size
!= UINT64_MAX
)
370 return MAX(p
->current_size
, p
->size_max
);
375 static uint64_t partition_min_size_with_padding(const Partition
*p
) {
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. */
382 sz
= partition_min_size(p
);
384 if (p
->padding_min
!= UINT64_MAX
)
385 sz
+= p
->padding_min
;
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
;
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);
397 static uint64_t free_area_available(const FreeArea
*a
) {
400 /* Determines how much of this free area is not allocated yet */
402 assert(a
->size
>= a
->allocated
);
403 return a
->size
- a
->allocated
;
406 static uint64_t free_area_available_for_new_partitions(const FreeArea
*a
) {
409 /* Similar to free_area_available(), but takes into account that the required size and padding of the
410 * preceding partition is honoured. */
412 avail
= free_area_available(a
);
414 uint64_t need
, space
;
416 need
= partition_min_size_with_padding(a
->after
);
418 assert(a
->after
->offset
!= UINT64_MAX
);
419 assert(a
->after
->current_size
!= UINT64_MAX
);
421 space
= round_up_size(a
->after
->offset
+ a
->after
->current_size
, 4096) - a
->after
->offset
+ avail
;
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
));
436 static uint64_t charge_size(uint64_t total
, uint64_t amount
) {
439 assert(amount
<= total
);
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
)
446 return total
- rounded
;
449 static uint64_t charge_weight(uint64_t total
, uint64_t amount
) {
450 assert(amount
<= total
);
451 return total
- amount
;
454 static bool context_allocate_partitions(Context
*context
) {
459 /* A simple first-fit algorithm, assuming the array of free areas is sorted by size in decreasing
462 LIST_FOREACH(partitions
, p
, context
->partitions
) {
467 /* Skip partitions we already dropped or that already exist */
468 if (p
->dropped
|| PARTITION_EXISTS(p
))
472 typesafe_qsort(context
->free_areas
, context
->n_free_areas
, free_area_compare
);
474 /* How much do we need to fit? */
475 required
= partition_min_size_with_padding(p
);
476 assert(required
% 4096 == 0);
478 for (size_t i
= 0; i
< context
->n_free_areas
; i
++) {
479 a
= context
->free_areas
[i
];
481 if (free_area_available_for_new_partitions(a
) >= required
) {
488 return false; /* 😢 Oh no! We can't fit this partition into any free area! */
490 /* Assign the partition to this free area */
491 p
->allocated_to_area
= a
;
493 /* Budget the minimal partition size */
494 a
->allocated
+= required
;
500 static int context_sum_weights(Context
*context
, FreeArea
*a
, uint64_t *ret
) {
501 uint64_t weight_sum
= 0;
508 /* Determine the sum of the weights of all partitions placed in or before the specified free area */
510 LIST_FOREACH(partitions
, p
, context
->partitions
) {
511 if (p
->padding_area
!= a
&& p
->allocated_to_area
!= a
)
514 if (p
->weight
> UINT64_MAX
- weight_sum
)
516 weight_sum
+= p
->weight
;
518 if (p
->padding_weight
> UINT64_MAX
- weight_sum
)
520 weight_sum
+= p
->padding_weight
;
527 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "Combined weight of partition exceeds unsigned 64bit range, refusing.");
530 static int scale_by_weight(uint64_t value
, uint64_t weight
, uint64_t weight_sum
, uint64_t *ret
) {
531 assert(weight_sum
>= weight
);
539 if (value
> UINT64_MAX
/ weight
)
540 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "Scaling by weight of partition exceeds unsigned 64bit range, refusing.");
542 *ret
= value
* weight
/ weight_sum
;
546 typedef enum GrowPartitionPhase
{
547 /* The first phase: we charge partitions which need more (according to constraints) than their weight-based share. */
550 /* The second phase: we charge partitions which need less (according to constraints) than their weight-based share. */
553 /* The third phase: we distribute what remains among the remaining partitions, according to the weights */
555 } GrowPartitionPhase
;
557 static int context_grow_partitions_phase(
560 GrowPartitionPhase phase
,
562 uint64_t *weight_sum
) {
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
) {
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
)
581 if (p
->new_size
== UINT64_MAX
) {
582 bool charge
= false, try_again
= false;
583 uint64_t share
, rsz
, xsz
;
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
);
591 rsz
= partition_min_size(p
);
592 xsz
= partition_max_size(p
);
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
600 charge
= try_again
= true;
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. */
608 charge
= try_again
= true;
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
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
;
620 p
->new_size
= MAX(round_down_size(share
, 4096), rsz
);
626 *span
= charge_size(*span
, p
->new_size
);
627 *weight_sum
= charge_weight(*weight_sum
, p
->weight
);
631 return 0; /* try again */
634 if (p
->new_padding
== UINT64_MAX
) {
635 bool charge
= false, try_again
= false;
638 r
= scale_by_weight(*span
, p
->padding_weight
, *weight_sum
, &share
);
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
) {
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
;
658 *span
= charge_size(*span
, p
->new_padding
);
659 *weight_sum
= charge_weight(*weight_sum
, p
->padding_weight
);
663 return 0; /* try again */
670 static int context_grow_partitions_on_free_area(Context
*context
, FreeArea
*a
) {
671 uint64_t weight_sum
= 0, span
;
677 r
= context_sum_weights(context
, a
, &weight_sum
);
681 /* Let's calculate the total area covered by this free area and the partition before it */
684 assert(a
->after
->offset
!= UINT64_MAX
);
685 assert(a
->after
->current_size
!= UINT64_MAX
);
687 span
+= round_up_size(a
->after
->offset
+ a
->after
->current_size
, 4096) - a
->after
->offset
;
690 GrowPartitionPhase phase
= PHASE_OVERCHARGE
;
692 r
= context_grow_partitions_phase(context
, a
, phase
, &span
, &weight_sum
);
695 if (r
== 0) /* not done yet, re-run this phase */
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
)
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
)) {
710 assert(a
->after
->new_size
!= UINT64_MAX
);
711 m
= a
->after
->new_size
+ span
;
713 xsz
= partition_max_size(a
->after
);
714 if (xsz
!= UINT64_MAX
&& m
> xsz
)
717 span
= charge_size(span
, m
- a
->after
->new_size
);
718 a
->after
->new_size
= m
;
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. */
726 LIST_FOREACH(partitions
, p
, context
->partitions
) {
729 if (p
->allocated_to_area
!= a
)
732 if (PARTITION_IS_FOREIGN(p
))
735 assert(p
->new_size
!= UINT64_MAX
);
736 m
= p
->new_size
+ span
;
738 xsz
= partition_max_size(p
);
739 if (xsz
!= UINT64_MAX
&& m
> xsz
)
742 span
= charge_size(span
, m
- p
->new_size
);
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
;
759 static int context_grow_partitions(Context
*context
) {
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
]);
771 /* All existing partitions that have no free space after them can't change size */
772 LIST_FOREACH(partitions
, p
, context
->partitions
) {
776 if (!PARTITION_EXISTS(p
) || p
->padding_area
) {
777 /* The algorithm above must have initialized this already */
778 assert(p
->new_size
!= UINT64_MAX
);
782 assert(p
->new_size
== UINT64_MAX
);
783 p
->new_size
= p
->current_size
;
785 assert(p
->new_padding
== UINT64_MAX
);
786 p
->new_padding
= p
->current_padding
;
792 static void context_place_partitions(Context
*context
) {
798 /* Determine next partition number to assign */
799 LIST_FOREACH(partitions
, p
, context
->partitions
) {
800 if (!PARTITION_EXISTS(p
))
803 assert(p
->partno
!= UINT64_MAX
);
804 if (p
->partno
>= partno
)
805 partno
= p
->partno
+ 1;
808 for (size_t i
= 0; i
< context
->n_free_areas
; i
++) {
809 FreeArea
*a
= context
->free_areas
[i
];
810 uint64_t start
, left
;
813 assert(a
->after
->offset
!= UINT64_MAX
);
814 assert(a
->after
->new_size
!= UINT64_MAX
);
815 assert(a
->after
->new_padding
!= UINT64_MAX
);
817 start
= a
->after
->offset
+ a
->after
->new_size
+ a
->after
->new_padding
;
819 start
= context
->start
;
821 start
= round_up_size(start
, 4096);
824 LIST_FOREACH(partitions
, p
, context
->partitions
) {
825 if (p
->allocated_to_area
!= a
)
829 p
->partno
= partno
++;
831 assert(left
>= p
->new_size
);
832 start
+= p
->new_size
;
835 assert(left
>= p
->new_padding
);
836 start
+= p
->new_padding
;
837 left
-= p
->new_padding
;
842 static int config_parse_type(
844 const char *filename
,
847 unsigned section_line
,
854 sd_id128_t
*type_uuid
= data
;
860 r
= gpt_partition_type_uuid_from_string(rvalue
, type_uuid
);
862 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse partition type: %s", rvalue
);
867 static int config_parse_label(
869 const char *filename
,
872 unsigned section_line
,
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
},
893 _cleanup_free_ char16_t
*recoded
= NULL
;
894 _cleanup_free_
char *resolved
= NULL
;
901 r
= specifier_printf(rvalue
, specifier_table
, NULL
, &resolved
);
903 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
904 "Failed to expand specifiers in Label=, ignoring: %s", rvalue
);
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
);
914 recoded
= utf8_to_utf16(resolved
, strlen(resolved
));
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
);
924 free_and_replace(*label
, resolved
);
928 static int config_parse_weight(
930 const char *filename
,
933 unsigned section_line
,
940 uint32_t *priority
= data
, v
;
946 r
= safe_atou32(rvalue
, &v
);
948 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
949 "Failed to parse weight value, ignoring: %s", rvalue
);
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
);
963 static int config_parse_size4096(
965 const char *filename
,
968 unsigned section_line
,
975 uint64_t *sz
= data
, parsed
;
981 r
= parse_size(rvalue
, 1024, &parsed
);
983 return log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
984 "Failed to parse size value: %s", rvalue
);
987 *sz
= round_up_size(parsed
, 4096);
989 *sz
= round_down_size(parsed
, 4096);
994 log_syntax(unit
, LOG_NOTICE
, filename
, line
, r
, "Rounded %s= size %" PRIu64
" → %" PRIu64
", a multiple of 4096.", lvalue
, parsed
, *sz
);
999 static int partition_read_definition(Partition
*p
, const char *path
) {
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
},
1018 r
= config_parse(NULL
, path
, NULL
,
1020 config_item_table_lookup
, table
,
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.");
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.");
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.");
1042 static int context_read_definitions(
1044 const char *directory
,
1047 _cleanup_strv_free_
char **files
= NULL
;
1048 Partition
*last
= NULL
;
1055 r
= conf_files_list_strv(&files
, ".conf", NULL
, CONF_FILES_REGULAR
|CONF_FILES_FILTER_MASKED
, (const char**) STRV_MAKE(directory
));
1057 r
= conf_files_list_strv(&files
, ".conf", root
, CONF_FILES_REGULAR
|CONF_FILES_FILTER_MASKED
, (const char**) CONF_PATHS_STRV("repart.d"));
1059 return log_error_errno(r
, "Failed to enumerate *.conf files: %m");
1061 STRV_FOREACH(f
, files
) {
1062 _cleanup_(partition_freep
) Partition
*p
= NULL
;
1064 p
= partition_new();
1068 p
->definition_path
= strdup(*f
);
1069 if (!p
->definition_path
)
1072 r
= partition_read_definition(p
, *f
);
1076 LIST_INSERT_AFTER(partitions
, context
->partitions
, last
, p
);
1078 context
->n_partitions
++;
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
);
1089 static int determine_current_padding(
1090 struct fdisk_context
*c
,
1091 struct fdisk_table
*t
,
1092 struct fdisk_partition
*p
,
1095 size_t n_partitions
;
1096 uint64_t offset
, next
= UINT64_MAX
;
1102 if (!fdisk_partition_has_end(p
))
1103 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition has no end!");
1105 offset
= fdisk_partition_get_end(p
);
1106 assert(offset
< UINT64_MAX
/ 512);
1109 n_partitions
= fdisk_table_get_nents(t
);
1110 for (size_t i
= 0; i
< n_partitions
; i
++) {
1111 struct fdisk_partition
*q
;
1114 q
= fdisk_table_get_partition(t
, i
);
1116 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to read partition metadata: %m");
1118 if (fdisk_partition_is_used(q
) <= 0)
1121 if (!fdisk_partition_has_start(q
))
1124 start
= fdisk_partition_get_start(q
);
1125 assert(start
< UINT64_MAX
/ 512);
1128 if (start
>= offset
&& (next
== UINT64_MAX
|| next
> start
))
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 */
1138 assert(next
< UINT64_MAX
/ 512);
1142 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition end beyond disk end.");
1145 assert(next
>= offset
);
1146 offset
= round_up_size(offset
, 4096);
1147 next
= round_down_size(next
, 4096);
1149 if (next
>= offset
) /* Check again, rounding might have fucked things up */
1150 *ret
= next
- offset
;
1157 static int fdisk_ask_cb(struct fdisk_context
*c
, struct fdisk_ask
*ask
, void *data
) {
1158 _cleanup_free_
char *ids
= NULL
;
1161 if (fdisk_ask_get_type(ask
) != FDISK_ASKTYPE_STRING
)
1164 ids
= new(char, ID128_UUID_STRING_MAX
);
1168 r
= fdisk_ask_string_set_result(ask
, id128_to_uuid_string(*(sd_id128_t
*) data
, ids
));
1176 static int fdisk_set_disklabel_id_by_uuid(struct fdisk_context
*c
, sd_id128_t id
) {
1179 r
= fdisk_set_ask(c
, fdisk_ask_cb
, &id
);
1183 r
= fdisk_set_disklabel_id(c
);
1187 return fdisk_set_ask(c
, NULL
, NULL
);
1190 #define DISK_UUID_TOKEN "disk-uuid"
1192 static int disk_acquire_uuid(Context
*context
, sd_id128_t
*ret
) {
1194 unsigned char md
[SHA256_DIGEST_LENGTH
];
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. */
1204 if (!HMAC(EVP_sha256(),
1205 &context
->seed
, sizeof(context
->seed
),
1206 (const unsigned char*) DISK_UUID_TOKEN
, strlen(DISK_UUID_TOKEN
),
1208 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE
), "HMAC-SHA256 calculation failed.");
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
);
1216 static int context_load_partition_table(
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
;
1234 c
= fdisk_new_context();
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
);
1243 char procfs_path
[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
1244 xsprintf(procfs_path
, "/proc/self/fd/%i", *backing_fd
);
1246 r
= fdisk_assign_device(c
, procfs_path
, arg_dry_run
);
1249 return log_error_errno(r
, "Failed to open device '%s': %m", node
);
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");
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");
1262 switch (arg_empty
) {
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
);
1272 /* Require an empty disk, refuse any existing partition table */
1273 r
= fdisk_has_label(c
);
1275 return log_error_errno(r
, "Failed to determine whether disk %s has a disk label: %m", node
);
1277 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON
), "Disk %s already has a disk label, refusing.", node
);
1279 from_scratch
= true;
1283 /* Allow both an empty disk and an existing partition table, but only GPT */
1284 r
= fdisk_has_label(c
);
1286 return log_error_errno(r
, "Failed to determine whether disk %s has a disk label: %m", node
);
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
);
1291 from_scratch
= true;
1297 /* Always reinitiaize the disk, don't consider what there was on the disk before */
1298 from_scratch
= true;
1303 r
= fdisk_enable_wipe(c
, true);
1305 return log_error_errno(r
, "Failed to enable wiping of disk signature: %m");
1307 r
= fdisk_create_disklabel(c
, "gpt");
1309 return log_error_errno(r
, "Failed to create GPT disk label: %m");
1311 r
= disk_acquire_uuid(context
, &disk_uuid
);
1313 return log_error_errno(r
, "Failed to acquire disk GPT uuid: %m");
1315 r
= fdisk_set_disklabel_id_by_uuid(c
, disk_uuid
);
1317 return log_error_errno(r
, "Failed to set GPT disk label: %m");
1319 goto add_initial_free_area
;
1322 r
= fdisk_get_disklabel_id(c
, &disk_uuid_string
);
1324 return log_error_errno(r
, "Failed to get current GPT disk label UUID: %m");
1326 r
= sd_id128_from_string(disk_uuid_string
, &disk_uuid
);
1328 return log_error_errno(r
, "Failed to parse current GPT disk label UUID: %m");
1330 if (sd_id128_is_null(disk_uuid
)) {
1331 r
= disk_acquire_uuid(context
, &disk_uuid
);
1333 return log_error_errno(r
, "Failed to acquire disk GPT uuid: %m");
1335 r
= fdisk_set_disklabel_id(c
);
1337 return log_error_errno(r
, "Failed to set GPT disk label: %m");
1340 r
= fdisk_get_partitions(c
, &t
);
1342 return log_error_errno(r
, "Failed to acquire partition table: %m");
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
;
1353 sd_id128_t ptid
, id
;
1356 p
= fdisk_table_get_partition(t
, i
);
1358 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to read partition metadata: %m");
1360 if (fdisk_partition_is_used(p
) <= 0)
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.");
1368 pt
= fdisk_partition_get_type(p
);
1370 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to acquire type of partition: %m");
1372 pts
= fdisk_parttype_get_string(pt
);
1374 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to acquire type of partition as string: %m");
1376 r
= sd_id128_from_string(pts
, &ptid
);
1378 return log_error_errno(r
, "Failed to parse partition type UUID %s: %m", pts
);
1380 ids
= fdisk_partition_get_uuid(p
);
1382 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Found a partition without a UUID.");
1384 r
= sd_id128_from_string(ids
, &id
);
1386 return log_error_errno(r
, "Failed to parse partition UUID %s: %m", ids
);
1388 label
= fdisk_partition_get_name(p
);
1389 if (!isempty(label
)) {
1390 label_copy
= strdup(label
);
1395 sz
= fdisk_partition_get_size(p
);
1396 assert_se(sz
<= UINT64_MAX
/512);
1399 start
= fdisk_partition_get_start(p
);
1400 assert_se(start
<= UINT64_MAX
/512);
1403 partno
= fdisk_partition_get_partno(p
);
1405 if (left_boundary
== UINT64_MAX
|| left_boundary
> start
)
1406 left_boundary
= start
;
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
) {
1413 if (!sd_id128_equal(pp
->type_uuid
, ptid
))
1416 if (!pp
->current_partition
) {
1417 pp
->current_uuid
= id
;
1418 pp
->current_size
= sz
;
1420 pp
->partno
= partno
;
1421 pp
->current_label
= TAKE_PTR(label_copy
);
1423 pp
->current_partition
= p
;
1424 fdisk_ref_partition(p
);
1426 r
= determine_current_padding(c
, t
, p
, &pp
->current_padding
);
1430 if (pp
->current_padding
> 0) {
1431 r
= context_add_free_area(context
, pp
->current_padding
, pp
);
1441 /* If we have no matching definition, create a new one. */
1443 _cleanup_(partition_freep
) Partition
*np
= NULL
;
1445 np
= partition_new();
1449 np
->current_uuid
= id
;
1450 np
->type_uuid
= ptid
;
1451 np
->current_size
= sz
;
1453 np
->partno
= partno
;
1454 np
->current_label
= TAKE_PTR(label_copy
);
1456 np
->current_partition
= p
;
1457 fdisk_ref_partition(p
);
1459 r
= determine_current_padding(c
, t
, p
, &np
->current_padding
);
1463 if (np
->current_padding
> 0) {
1464 r
= context_add_free_area(context
, np
->current_padding
, np
);
1469 LIST_INSERT_AFTER(partitions
, context
->partitions
, last
, TAKE_PTR(np
));
1470 context
->n_partitions
++;
1474 add_initial_free_area
:
1475 nsectors
= fdisk_get_nsectors(c
);
1476 assert(nsectors
<= UINT64_MAX
/512);
1479 first_lba
= fdisk_get_first_lba(c
);
1480 assert(first_lba
<= UINT64_MAX
/512);
1483 last_lba
= fdisk_get_last_lba(c
);
1484 assert(last_lba
< UINT64_MAX
);
1486 assert(last_lba
<= UINT64_MAX
/512);
1489 assert(last_lba
>= first_lba
);
1491 if (left_boundary
== UINT64_MAX
) {
1492 /* No partitions at all? Then the whole disk is up for grabs. */
1494 first_lba
= round_up_size(first_lba
, 4096);
1495 last_lba
= round_down_size(last_lba
, 4096);
1497 if (last_lba
> first_lba
) {
1498 r
= context_add_free_area(context
, last_lba
- first_lba
, NULL
);
1503 /* Add space left of first partition */
1504 assert(left_boundary
>= first_lba
);
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);
1510 if (left_boundary
> first_lba
) {
1511 r
= context_add_free_area(context
, left_boundary
- first_lba
, NULL
);
1517 context
->start
= first_lba
;
1518 context
->end
= last_lba
;
1519 context
->total
= nsectors
;
1520 context
->fdisk_context
= TAKE_PTR(c
);
1522 return from_scratch
;
1525 static void context_unload_partition_table(Context
*context
) {
1526 Partition
*p
, *next
;
1530 LIST_FOREACH_SAFE(partitions
, p
, next
, context
->partitions
) {
1532 /* Entirely remove partitions that have no configuration */
1533 if (PARTITION_IS_FOREIGN(p
)) {
1534 partition_unlink_and_free(context
, p
);
1538 /* Otherwise drop all data we read off the block device and everything we might have
1539 * calculated based on it */
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
;
1549 if (p
->current_partition
) {
1550 fdisk_unref_partition(p
->current_partition
);
1551 p
->current_partition
= NULL
;
1554 if (p
->new_partition
) {
1555 fdisk_unref_partition(p
->new_partition
);
1556 p
->new_partition
= NULL
;
1559 p
->padding_area
= NULL
;
1560 p
->allocated_to_area
= NULL
;
1562 p
->current_uuid
= p
->new_uuid
= SD_ID128_NULL
;
1565 context
->start
= UINT64_MAX
;
1566 context
->end
= UINT64_MAX
;
1567 context
->total
= UINT64_MAX
;
1569 if (context
->fdisk_context
) {
1570 fdisk_unref_context(context
->fdisk_context
);
1571 context
->fdisk_context
= NULL
;
1574 context_free_free_areas(context
);
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
;
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
);
1585 if (from
!= UINT64_MAX
) {
1586 if (from
== to
|| to
== UINT64_MAX
)
1587 buf
= strdup(format_buffer1
);
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
);
1600 *ret
= TAKE_PTR(buf
);
1604 static const char *partition_label(const Partition
*p
) {
1608 return p
->new_label
;
1610 if (p
->current_label
)
1611 return p
->current_label
;
1613 return gpt_partition_type_uuid_to_string(p
->type_uuid
);
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;
1622 t
= table_new("type", "label", "uuid", "file", "node", "offset", "raw size", "size", "raw padding", "padding");
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);
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);
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
];
1640 label
= partition_label(p
);
1641 partname
= p
->partno
!= UINT64_MAX
? fdisk_partname(node
, p
->partno
+1) : NULL
;
1643 r
= format_size_change(p
->current_size
, p
->new_size
, &size_change
);
1647 r
= format_size_change(p
->current_padding
, p
->new_padding
, &padding_change
);
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
;
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
);
1669 return table_log_add_error(r
);
1672 if (sum_padding
> 0 || sum_size
> 0) {
1673 char s
[FORMAT_BYTES_MAX
];
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
));
1692 return table_log_add_error(r
);
1695 r
= table_print(t
, stdout
);
1697 return log_error_errno(r
, "Failed to dump table: %m");
1702 static void context_bar_char_process_partition(
1707 size_t *ret_start
) {
1709 uint64_t from
, to
, total
;
1720 assert(p
->offset
!= UINT64_MAX
);
1721 assert(p
->new_size
!= UINT64_MAX
);
1724 to
= from
+ p
->new_size
;
1726 assert(context
->end
>= context
->start
);
1727 total
= context
->end
- context
->start
;
1729 assert(from
>= context
->start
);
1730 assert(from
<= context
->end
);
1731 x
= (from
- context
->start
) * n
/ total
;
1733 assert(to
>= context
->start
);
1734 assert(to
<= context
->end
);
1735 y
= (to
- context
->start
) * n
/ total
;
1740 for (size_t i
= x
; i
< y
; i
++)
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
];
1752 /* Tries really hard to find a suitable description for this partition */
1754 if (p
->definition_path
) {
1755 buf
= strdup(basename(p
->definition_path
));
1759 label
= partition_label(p
);
1760 if (!isempty(label
)) {
1761 buf
= strdup(label
);
1765 if (p
->partno
!= UINT64_MAX
) {
1766 buf
= fdisk_partname(node
, p
->partno
+1);
1770 if (!sd_id128_is_null(p
->new_uuid
))
1772 else if (!sd_id128_is_null(p
->current_uuid
))
1773 id
= p
->current_uuid
;
1777 buf
= strdup(id128_to_uuid_string(id
, ids
));
1783 *ret
= TAKE_PTR(buf
);
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
;
1794 assert_se((c
= columns()) >= 2);
1795 c
-= 2; /* We do not use the leftmost and rightmost character cell */
1797 bar
= new0(Partition
*, c
);
1801 start_array
= new(size_t, context
->n_partitions
);
1805 LIST_FOREACH(partitions
, p
, context
->partitions
)
1806 context_bar_char_process_partition(context
, bar
, c
, p
, start_array
+ j
++);
1810 for (size_t i
= 0; i
< c
; i
++) {
1815 fputs(z
? ansi_green() : ansi_yellow(), stdout
);
1816 fputs(special_glyph(SPECIAL_GLYPH_DARK_SHADE
), stdout
);
1818 fputs(ansi_normal(), stdout
);
1819 fputs(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE
), stdout
);
1825 fputs(ansi_normal(), stdout
);
1828 for (size_t i
= 0; i
< context
->n_partitions
; i
++) {
1829 _cleanup_free_
char **line
= NULL
;
1831 line
= new0(char*, c
);
1836 LIST_FOREACH(partitions
, p
, context
->partitions
) {
1837 _cleanup_free_
char *d
= NULL
;
1840 if (i
< context
->n_partitions
- j
) {
1842 if (line
[start_array
[j
-1]]) {
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
));
1848 d
= strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH
), e
);
1855 d
= strdup(special_glyph(SPECIAL_GLYPH_TREE_VERTICAL
));
1860 } else if (i
== context
->n_partitions
- j
) {
1861 _cleanup_free_
char *hint
= NULL
;
1863 (void) partition_hint(p
, node
, &hint
);
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
));
1868 d
= strjoin(special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), " ", strna(hint
));
1875 free_and_replace(line
[start_array
[j
-1]], d
);
1883 fputs(line
[j
], stdout
);
1884 j
+= utf8_console_width(line
[j
]);
1893 for (j
= 0; j
< c
; j
++)
1900 static bool context_changed(const Context
*context
) {
1903 LIST_FOREACH(partitions
, p
, context
->partitions
) {
1907 if (p
->allocated_to_area
)
1910 if (p
->new_size
!= p
->current_size
)
1917 static int context_wipe_partition(Context
*context
, Partition
*p
) {
1918 _cleanup_(blkid_free_probep
) blkid_probe probe
= NULL
;
1923 assert(!PARTITION_EXISTS(p
)); /* Safety check: never wipe existing partitions */
1925 probe
= blkid_new_probe();
1929 assert(p
->offset
!= UINT64_MAX
);
1930 assert(p
->new_size
!= UINT64_MAX
);
1933 r
= blkid_probe_set_device(probe
, fdisk_get_devfd(context
->fdisk_context
), p
->offset
, p
->new_size
);
1935 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to allocate device probe for partition %" PRIu64
".", p
->partno
);
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
);
1946 r
= blkid_do_probe(probe
);
1948 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe for file systems.");
1953 if (blkid_do_wipe(probe
, false) < 0)
1954 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to wipe file system signature.");
1957 log_info("Successfully wiped file system signatures from partition %" PRIu64
".", p
->partno
);
1961 static int context_discard_range(Context
*context
, uint64_t offset
, uint64_t size
) {
1966 assert(offset
!= UINT64_MAX
);
1967 assert(size
!= UINT64_MAX
);
1972 assert_se((fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
1974 if (fstat(fd
, &st
) < 0)
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
))
1988 if (S_ISBLK(st
.st_mode
)) {
1989 uint64_t range
[2], end
;
1991 range
[0] = round_up_size(offset
, 512);
1993 end
= offset
+ size
;
1994 if (end
<= range
[0])
1997 range
[1] = round_down_size(end
- range
[0], 512);
2001 if (ioctl(fd
, BLKDISCARD
, range
) < 0) {
2002 if (ERRNO_IS_NOT_SUPPORTED(errno
))
2014 static int context_discard_partition(Context
*context
, Partition
*p
) {
2020 assert(p
->offset
!= UINT64_MAX
);
2021 assert(p
->new_size
!= UINT64_MAX
);
2022 assert(!PARTITION_EXISTS(p
)); /* Safety check: never discard existing partitions */
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
);
2033 log_info("Partition %" PRIu64
" too short for discard, skipping.", p
->partno
);
2037 return log_error_errno(r
, "Failed to discard data for new partition %" PRIu64
".", p
->partno
);
2039 log_info("Successfully discarded data from partition %" PRIu64
".", p
->partno
);
2043 static int context_discard_gap_after(Context
*context
, Partition
*p
) {
2044 uint64_t gap
, next
= UINT64_MAX
;
2049 assert(!p
|| (p
->offset
!= UINT64_MAX
&& p
->new_size
!= UINT64_MAX
));
2052 gap
= p
->offset
+ p
->new_size
;
2054 gap
= context
->start
;
2056 LIST_FOREACH(partitions
, q
, context
->partitions
) {
2060 assert(q
->offset
!= UINT64_MAX
);
2061 assert(q
->new_size
!= UINT64_MAX
);
2063 if (q
->offset
< gap
)
2066 if (next
== UINT64_MAX
|| q
->offset
< next
)
2070 if (next
== UINT64_MAX
) {
2071 next
= context
->end
;
2073 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition end beyond disk end.");
2076 assert(next
>= gap
);
2077 r
= context_discard_range(context
, gap
, next
- gap
);
2078 if (r
== -EOPNOTSUPP
) {
2080 log_info("Storage does not support discarding, not discarding gap after partition %" PRIu64
".", p
->partno
);
2082 log_info("Storage does not support discarding, not discarding gap at beginning of disk.");
2085 if (r
== 0) /* Too short */
2089 return log_error_errno(r
, "Failed to discard gap after partition %" PRIu64
".", p
->partno
);
2091 return log_error_errno(r
, "Failed to discard gap at beginning of disk.");
2095 log_info("Successfully discarded gap after partition %" PRIu64
".", p
->partno
);
2097 log_info("Successfully discarded gap at beginning of disk.");
2102 static int context_wipe_and_discard(Context
*context
, bool from_scratch
) {
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. */
2112 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2114 if (!p
->allocated_to_area
)
2117 if (!from_scratch
) {
2118 r
= context_discard_partition(context
, p
);
2123 r
= context_wipe_partition(context
, p
);
2127 if (!from_scratch
) {
2128 r
= context_discard_gap_after(context
, p
);
2134 if (!from_scratch
) {
2135 r
= context_discard_gap_after(context
, NULL
);
2143 static int context_copy_blocks(Context
*context
) {
2149 /* Copy in file systems on the block level */
2151 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2152 char buf
[FORMAT_BYTES_MAX
];
2154 if (p
->copy_blocks_fd
< 0)
2160 if (PARTITION_EXISTS(p
)) /* Never copy over existing partitions */
2163 assert(p
->new_size
!= UINT64_MAX
);
2164 assert(p
->copy_blocks_size
!= UINT64_MAX
);
2165 assert(p
->new_size
>= p
->copy_blocks_size
);
2168 assert_se((fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
2170 if (lseek(fd
, p
->offset
, SEEK_SET
) == (off_t
) -1)
2171 return log_error_errno(errno
, "Failed to seek to partition offset: %m");
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
);
2175 r
= copy_bytes_full(p
->copy_blocks_fd
, fd
, p
->copy_blocks_size
, 0, NULL
, NULL
, NULL
, NULL
);
2177 return log_error_errno(r
, "Failed to copy in data from '%s': %m", p
->copy_blocks_path
);
2179 log_info("Copying in of '%s' on block level completed.", p
->copy_blocks_path
);
2185 static int partition_acquire_uuid(Context
*context
, Partition
*p
, sd_id128_t
*ret
) {
2187 sd_id128_t type_uuid
;
2189 } _packed_ plaintext
= {};
2191 unsigned char md
[SHA256_DIGEST_LENGTH
];
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:
2213 * SEED := /etc/machine-id
2215 * If first partition instance of type TYPE_UUID:
2216 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID)
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)
2222 LIST_FOREACH(partitions
, q
, context
->partitions
) {
2226 if (!sd_id128_equal(p
->type_uuid
, q
->type_uuid
))
2232 plaintext
.type_uuid
= p
->type_uuid
;
2233 plaintext
.counter
= htole64(k
);
2235 if (!HMAC(EVP_sha256(),
2236 &context
->seed
, sizeof(context
->seed
),
2237 (const unsigned char*) &plaintext
, k
== 0 ? sizeof(sd_id128_t
) : sizeof(plaintext
),
2239 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE
), "SHA256 calculation failed.");
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
);
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
) {
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
);
2254 r
= sd_id128_randomize(&result
.id
);
2256 return log_error_errno(r
, "Failed to generate randomized UUID: %m");
2266 static int partition_acquire_label(Context
*context
, Partition
*p
, char **ret
) {
2267 _cleanup_free_
char *label
= NULL
;
2275 prefix
= gpt_partition_type_uuid_to_string(p
->type_uuid
);
2280 const char *ll
= label
?: prefix
;
2284 LIST_FOREACH(partitions
, q
, context
->partitions
) {
2288 if (streq_ptr(ll
, q
->current_label
) ||
2289 streq_ptr(ll
, q
->new_label
)) {
2298 label
= mfree(label
);
2301 if (asprintf(&label
, "%s-%u", prefix
, ++k
) < 0)
2306 label
= strdup(prefix
);
2311 *ret
= TAKE_PTR(label
);
2315 static int context_acquire_partition_uuids_and_labels(Context
*context
) {
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
;
2326 if (p
->current_label
) {
2328 p
->new_label
= strdup(p
->current_label
);
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
);
2345 if (!isempty(p
->current_label
)) {
2347 p
->new_label
= strdup(p
->current_label
); /* never change initialized labels */
2350 } else if (!p
->new_label
) {
2351 /* Not explicitly set by user! */
2353 r
= partition_acquire_label(context
, p
, &p
->new_label
);
2362 static int device_kernel_partitions_supported(int fd
) {
2363 struct loop_info64 info
;
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 */
2373 if (ioctl(fd
, LOOP_GET_STATUS64
, &info
) < 0) {
2375 if (ERRNO_IS_NOT_SUPPORTED(errno
) || errno
== EINVAL
)
2376 return true; /* not a loopback device, let's assume partition are supported */
2378 return log_error_errno(fd
, "Failed to issue LOOP_GET_STATUS64 on block device: %m");
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
));
2386 return FLAGS_SET(info
.lo_flags
, LO_FLAGS_PARTSCAN
);
2389 static int context_write_partition_table(
2392 bool from_scratch
) {
2394 _cleanup_(fdisk_unref_tablep
) struct fdisk_table
*original_table
= NULL
;
2400 if (arg_pretty
> 0 ||
2401 (arg_pretty
< 0 && isatty(STDOUT_FILENO
) > 0)) {
2403 if (context
->n_partitions
== 0)
2404 puts("Empty partition table.");
2406 (void) context_dump_partitions(context
, node
);
2410 (void) context_dump_partition_bar(context
, node
);
2415 if (!from_scratch
&& !context_changed(context
)) {
2416 log_info("No changes.");
2421 log_notice("Refusing to repartition, please re-run with --dry-run=no.");
2425 log_info("Applying changes.");
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.");
2432 return log_error_errno(r
, "Failed to discard entire block device: %m");
2434 log_info("Discarded entire block device.");
2437 r
= fdisk_get_partitions(context
->fdisk_context
, &original_table
);
2439 return log_error_errno(r
, "Failed to acquire partition table: %m");
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
);
2447 r
= context_copy_blocks(context
);
2451 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2455 assert(p
->new_size
!= UINT64_MAX
);
2456 assert(p
->offset
!= UINT64_MAX
);
2457 assert(p
->partno
!= UINT64_MAX
);
2459 if (PARTITION_EXISTS(p
)) {
2460 bool changed
= false;
2462 assert(p
->current_partition
);
2464 if (p
->new_size
!= p
->current_size
) {
2465 assert(p
->new_size
>= p
->current_size
);
2466 assert(p
->new_size
% 512 == 0);
2468 r
= fdisk_partition_size_explicit(p
->current_partition
, true);
2470 return log_error_errno(r
, "Failed to enable explicit sizing: %m");
2472 r
= fdisk_partition_set_size(p
->current_partition
, p
->new_size
/ 512);
2474 return log_error_errno(r
, "Failed to grow partition: %m");
2476 log_info("Growing existing partition %" PRIu64
".", p
->partno
);
2480 if (!sd_id128_equal(p
->new_uuid
, p
->current_uuid
)) {
2481 char buf
[ID128_UUID_STRING_MAX
];
2483 assert(!sd_id128_is_null(p
->new_uuid
));
2485 r
= fdisk_partition_set_uuid(p
->current_partition
, id128_to_uuid_string(p
->new_uuid
, buf
));
2487 return log_error_errno(r
, "Failed to set partition UUID: %m");
2489 log_info("Initializing UUID of existing partition %" PRIu64
".", p
->partno
);
2493 if (!streq_ptr(p
->new_label
, p
->current_label
)) {
2494 assert(!isempty(p
->new_label
));
2496 r
= fdisk_partition_set_name(p
->current_partition
, p
->new_label
);
2498 return log_error_errno(r
, "Failed to set partition label: %m");
2500 log_info("Setting partition label of existing partition %" PRIu64
".", p
->partno
);
2505 assert(!PARTITION_IS_FOREIGN(p
)); /* never touch foreign partitions */
2507 r
= fdisk_set_partition(context
->fdisk_context
, p
->partno
, p
->current_partition
);
2509 return log_error_errno(r
, "Failed to update partition: %m");
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
];
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
));
2522 t
= fdisk_new_parttype();
2526 r
= fdisk_parttype_set_typestr(t
, id128_to_uuid_string(p
->type_uuid
, ids
));
2528 return log_error_errno(r
, "Failed to initialize partition type: %m");
2530 q
= fdisk_new_partition();
2534 r
= fdisk_partition_set_type(q
, t
);
2536 return log_error_errno(r
, "Failed to set partition type: %m");
2538 r
= fdisk_partition_size_explicit(q
, true);
2540 return log_error_errno(r
, "Failed to enable explicit sizing: %m");
2542 r
= fdisk_partition_set_start(q
, p
->offset
/ 512);
2544 return log_error_errno(r
, "Failed to position partition: %m");
2546 r
= fdisk_partition_set_size(q
, p
->new_size
/ 512);
2548 return log_error_errno(r
, "Failed to grow partition: %m");
2550 r
= fdisk_partition_set_partno(q
, p
->partno
);
2552 return log_error_errno(r
, "Failed to set partition number: %m");
2554 r
= fdisk_partition_set_uuid(q
, id128_to_uuid_string(p
->new_uuid
, ids
));
2556 return log_error_errno(r
, "Failed to set partition UUID: %m");
2558 r
= fdisk_partition_set_name(q
, p
->new_label
);
2560 return log_error_errno(r
, "Failed to set partition label: %m");
2562 log_info("Creating new partition %" PRIu64
".", p
->partno
);
2564 r
= fdisk_add_partition(context
->fdisk_context
, q
, NULL
);
2566 return log_error_errno(r
, "Failed to add partition: %m");
2568 assert(!p
->new_partition
);
2569 p
->new_partition
= TAKE_PTR(q
);
2573 log_info("Writing new partition table.");
2575 r
= fdisk_write_disklabel(context
->fdisk_context
);
2577 return log_error_errno(r
, "Failed to write partition table: %m");
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)
2584 else if (capable
> 0) {
2585 log_info("Telling kernel to reread partition table.");
2588 r
= fdisk_reread_partition_table(context
->fdisk_context
);
2590 r
= fdisk_reread_changes(context
->fdisk_context
, original_table
);
2592 return log_error_errno(r
, "Failed to reread partition table: %m");
2594 log_notice("Not telling kernel to reread partition table, because selected image does not support kernel partition block devices.");
2596 log_info("All done.");
2601 static int context_read_seed(Context
*context
, const char *root
) {
2606 if (!sd_id128_is_null(context
->seed
))
2609 if (!arg_randomize
) {
2610 _cleanup_close_
int fd
= -1;
2612 fd
= chase_symlinks_and_open("/etc/machine-id", root
, CHASE_PREFIX_ROOT
, O_RDONLY
|O_CLOEXEC
, NULL
);
2614 log_info("No machine ID set, using randomized partition UUIDs.");
2616 return log_error_errno(fd
, "Failed to determine machine ID of image: %m");
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.");
2622 return log_error_errno(r
, "Failed to parse machine ID of image: %m");
2628 r
= sd_id128_randomize(&context
->seed
);
2630 return log_error_errno(r
, "Failed to generate randomized seed: %m");
2635 static int context_factory_reset(Context
*context
, bool from_scratch
) {
2642 if (arg_factory_reset
<= 0)
2645 if (from_scratch
) /* Nothing to reset if we start from scratch */
2649 log_notice("Refusing to factory reset, please re-run with --dry-run=no.");
2653 log_info("Applying factory reset.");
2655 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2657 if (!p
->factory_reset
|| !PARTITION_EXISTS(p
))
2660 assert(p
->partno
!= UINT64_MAX
);
2662 log_info("Removing partition %" PRIu64
" for factory reset.", p
->partno
);
2664 r
= fdisk_delete_partition(context
->fdisk_context
, p
->partno
);
2666 return log_error_errno(r
, "Failed to remove partition %" PRIu64
": %m", p
->partno
);
2672 log_info("Factory reset requested, but no partitions to delete found.");
2676 r
= fdisk_write_disklabel(context
->fdisk_context
);
2678 return log_error_errno(r
, "Failed to write disk label: %m");
2680 log_info("Successfully deleted %zu partitions.", n
);
2684 static int context_can_factory_reset(Context
*context
) {
2689 LIST_FOREACH(partitions
, p
, context
->partitions
)
2690 if (p
->factory_reset
&& PARTITION_EXISTS(p
))
2696 static int context_open_copy_block_paths(Context
*context
) {
2702 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2703 _cleanup_close_
int source_fd
= -1;
2707 assert(p
->copy_blocks_fd
< 0);
2708 assert(p
->copy_blocks_size
== UINT64_MAX
);
2710 if (PARTITION_EXISTS(p
)) /* Never copy over partitions that already exist! */
2713 if (!p
->copy_blocks_path
)
2716 source_fd
= open(p
->copy_blocks_path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2718 return log_error_errno(errno
, "Failed to open block copy file '%s': %m", p
->copy_blocks_path
);
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
);
2723 if (S_ISDIR(st
.st_mode
)) {
2724 _cleanup_free_
char *bdev
= NULL
;
2726 /* If the file is a directory, automatically find the backing block device */
2728 if (major(st
.st_dev
) != 0)
2729 r
= device_path_make_major_minor(S_IFBLK
, st
.st_dev
, &bdev
);
2733 /* Special support for btrfs */
2735 r
= btrfs_get_block_device_fd(source_fd
, &devt
);
2737 return log_error_errno(r
, "Unable to determine backing block device of '%s': %m", p
->copy_blocks_path
);
2739 r
= device_path_make_major_minor(S_IFBLK
, devt
, &bdev
);
2742 return log_error_errno(r
, "Failed to determine block device path for block device backing '%s': %m", p
->copy_blocks_path
);
2744 safe_close(source_fd
);
2746 source_fd
= open(bdev
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2748 return log_error_errno(errno
, "Failed to open block device '%s': %m", bdev
);
2750 if (fstat(source_fd
, &st
) < 0)
2751 return log_error_errno(errno
, "Failed to stat block device '%s': %m", bdev
);
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
);
2757 if (S_ISREG(st
.st_mode
))
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");
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
);
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
);
2770 p
->copy_blocks_fd
= TAKE_FD(source_fd
);
2771 p
->copy_blocks_size
= size
;
2777 static int help(void) {
2778 _cleanup_free_
char *link
= NULL
;
2781 r
= terminal_urlify_man("systemd-repart", "1", &link
);
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"
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()
2810 static int parse_argv(int argc
, char *argv
[]) {
2813 ARG_VERSION
= 0x100,
2818 ARG_CAN_FACTORY_RESET
,
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
},
2842 int c
, r
, dry_run
= -1;
2847 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
2858 r
= parse_boolean(optarg
);
2860 return log_error_errno(r
, "Failed to parse --dry-run= parameter: %s", optarg
);
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
;
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. */
2882 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2883 "Failed to parse --empty= parameter: %s", optarg
);
2887 r
= parse_boolean(optarg
);
2889 return log_error_errno(r
, "Failed to parse --discard= parameter: %s", optarg
);
2894 case ARG_FACTORY_RESET
:
2895 r
= parse_boolean(optarg
);
2897 return log_error_errno(r
, "Failed to parse --factory-reset= parameter: %s", optarg
);
2899 arg_factory_reset
= r
;
2902 case ARG_CAN_FACTORY_RESET
:
2903 arg_can_factory_reset
= true;
2907 r
= parse_path_argument_and_warn(optarg
, false, &arg_root
);
2913 if (isempty(optarg
)) {
2914 arg_seed
= SD_ID128_NULL
;
2915 arg_randomize
= false;
2916 } else if (streq(optarg
, "random"))
2917 arg_randomize
= true;
2919 r
= sd_id128_from_string(optarg
, &arg_seed
);
2921 return log_error_errno(r
, "Failed to parse seed: %s", optarg
);
2923 arg_randomize
= false;
2929 r
= parse_boolean(optarg
);
2931 return log_error_errno(r
, "Failed to parse --pretty= parameter: %s", optarg
);
2936 case ARG_DEFINITIONS
:
2937 r
= parse_path_argument_and_warn(optarg
, false, &arg_definitions
);
2943 uint64_t parsed
, rounded
;
2945 r
= parse_size(optarg
, 1024, &parsed
);
2947 return log_error_errno(r
, "Failed to parse --size= parameter: %s", optarg
);
2949 rounded
= round_up_size(parsed
, 4096);
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.");
2955 if (rounded
!= parsed
)
2956 log_warning("Specified size is not a multiple of 4096, rounding up automatically. (%" PRIu64
" → %" PRIu64
")",
2967 assert_not_reached("Unhandled option");
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.");
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.");
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
;
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.");
2989 arg_node
= argc
> optind
? argv
[optind
] : NULL
;
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.");
2998 static int parse_proc_cmdline_factory_reset(void) {
3002 if (arg_factory_reset
>= 0) /* Never override what is specified on the process command line */
3005 if (!in_initrd()) /* Never honour kernel command line factory reset request outside of the initrd */
3008 r
= proc_cmdline_get_bool("systemd.factory_reset", &b
);
3010 return log_error_errno(r
, "Failed to parse systemd.factory_reset kernel command line argument: %m");
3012 arg_factory_reset
= b
;
3015 log_notice("Honouring factory reset requested via kernel command line.");
3021 static int parse_efi_variable_factory_reset(void) {
3022 _cleanup_free_
char *value
= NULL
;
3025 if (arg_factory_reset
>= 0) /* Never override what is specified on the process command line */
3028 if (!in_initrd()) /* Never honour EFI variable factory reset request outside of the initrd */
3031 r
= efi_get_variable_string(EFI_VENDOR_SYSTEMD
, "FactoryReset", &value
);
3032 if (r
== -ENOENT
|| ERRNO_IS_NOT_SUPPORTED(r
))
3035 return log_error_errno(r
, "Failed to read EFI variable FactoryReset: %m");
3037 r
= parse_boolean(value
);
3039 return log_error_errno(r
, "Failed to parse EFI variable FactoryReset: %m");
3041 arg_factory_reset
= r
;
3043 log_notice("Honouring factory reset requested via EFI variable FactoryReset: %m");
3048 static int remove_efi_variable_factory_reset(void) {
3051 r
= efi_set_variable(EFI_VENDOR_SYSTEMD
, "FactoryReset", NULL
, 0);
3052 if (r
== -ENOENT
|| ERRNO_IS_NOT_SUPPORTED(r
))
3055 return log_error_errno(r
, "Failed to remove EFI variable FactoryReset: %m");
3057 log_info("Successfully unset EFI variable FactoryReset.");
3061 static int acquire_root_devno(const char *p
, int mode
, char **ret
, int *ret_fd
) {
3062 _cleanup_close_
int fd
= -1;
3064 dev_t devno
, fd_devno
= (mode_t
) -1;
3075 if (fstat(fd
, &st
) < 0)
3078 if (S_ISREG(st
.st_mode
)) {
3086 *ret_fd
= TAKE_FD(fd
);
3091 if (S_ISBLK(st
.st_mode
))
3092 fd_devno
= devno
= st
.st_rdev
;
3093 else if (S_ISDIR(st
.st_mode
)) {
3096 if (major(devno
) == 0) {
3097 r
= btrfs_get_block_device_fd(fd
, &devno
);
3098 if (r
== -ENOTTY
) /* not btrfs */
3106 /* From dm-crypt to backing partition */
3107 r
= block_get_originating(devno
, &devno
);
3109 log_debug_errno(r
, "Failed to find underlying block device for '%s', ignoring: %m", p
);
3111 /* From partition to whole disk containing it */
3112 r
= block_get_whole_disk(devno
, &devno
);
3114 log_debug_errno(r
, "Failed to find whole disk block device for '%s', ignoring: %m", p
);
3116 r
= device_path_make_canonical(S_IFBLK
, devno
, ret
);
3118 return log_debug_errno(r
, "Failed to determine canonical path for '%s': %m", p
);
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;
3126 static int find_root(char **ret
, int *ret_fd
) {
3134 if (arg_empty
== EMPTY_CREATE
) {
3135 _cleanup_close_
int fd
= -1;
3136 _cleanup_free_
char *s
= NULL
;
3138 s
= strdup(arg_node
);
3142 fd
= open(arg_node
, O_RDONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
|O_NOFOLLOW
, 0777);
3144 return log_error_errno(errno
, "Failed to create '%s': %m", arg_node
);
3147 *ret_fd
= TAKE_FD(fd
);
3151 r
= acquire_root_devno(arg_node
, O_RDONLY
|O_CLOEXEC
, ret
, ret_fd
);
3153 return log_error_errno(r
, "Failed to determine backing device of %s: %m", arg_node
);
3158 assert(IN_SET(arg_empty
, EMPTY_REFUSE
, EMPTY_ALLOW
));
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) */
3164 FOREACH_STRING(t
, "/", "/usr") {
3165 _cleanup_free_
char *j
= NULL
;
3169 j
= path_join("/sysroot", t
);
3177 r
= acquire_root_devno(p
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
, ret
, ret_fd
);
3180 return log_error_errno(r
, "Failed to determine backing device of %s: %m", p
);
3185 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "Failed to discover root block device.");
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;
3197 if (arg_size
== UINT64_MAX
) /* Nothing to do */
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
);
3205 return log_error_errno(errno
, "Failed to open '%s' in order to adjust size: %m", node
);
3208 if (fstat(*fd
, &st
) < 0)
3209 return log_error_errno(errno
, "Failed to stat '%s': %m", node
);
3211 r
= stat_verify_regular(&st
);
3213 return log_error_errno(r
, "Specified path '%s' is not a regular file, cannot resize: %m", node
);
3215 assert_se(format_bytes(buf1
, sizeof(buf1
), st
.st_size
));
3216 assert_se(format_bytes(buf2
, sizeof(buf2
), arg_size
));
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
);
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. */
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
);
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",
3237 /* Fallback to truncation, if fallocate() is not supported. */
3238 log_debug("Backing file system does not support fallocate(), falling back to ftruncate().");
3240 if (st
.st_size
== 0) /* Likely regular file just created by us */
3241 log_info("Allocated %s for '%s'.", buf2
, node
);
3243 log_info("File '%s' grown from %s to %s by allocation.", node
, buf1
, buf2
);
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",
3253 if (st
.st_size
== 0) /* Likely regular file just created by us */
3254 log_info("Sized '%s' to %s.", node
, buf2
);
3256 log_info("File '%s' grown from %s to %s by truncation.", node
, buf1
, buf2
);
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;
3268 log_show_color(true);
3269 log_parse_environment();
3273 /* Default to operation on /sysroot when invoked in the initrd! */
3274 arg_root
= strdup("/sysroot");
3279 r
= parse_argv(argc
, argv
);
3283 r
= parse_proc_cmdline_factory_reset();
3287 r
= parse_efi_variable_factory_reset();
3291 context
= context_new(arg_seed
);
3295 r
= context_read_definitions(context
, arg_definitions
, arg_root
);
3299 if (context
->n_partitions
<= 0 && arg_empty
== EMPTY_REFUSE
) {
3300 log_info("Didn't find any partition definition files, nothing to do.");
3304 r
= find_root(&node
, &backing_fd
);
3308 if (arg_size
!= UINT64_MAX
) {
3309 r
= resize_backing_fd(node
, &backing_fd
);
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. */
3320 from_scratch
= r
> 0; /* Starting from scratch */
3322 if (arg_can_factory_reset
) {
3323 r
= context_can_factory_reset(context
);
3327 return EXIT_FAILURE
;
3332 r
= context_factory_reset(context
, from_scratch
);
3336 /* We actually did a factory reset! */
3337 r
= remove_efi_variable_factory_reset();
3341 /* Reload the reduced partition table */
3342 context_unload_partition_table(context
);
3343 r
= context_load_partition_table(context
, node
, &backing_fd
);
3349 (void) context_dump_partitions(context
, node
);
3353 r
= context_read_seed(context
, arg_root
);
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
);
3362 /* First try to fit new partitions in, dropping by priority until it fits */
3364 if (context_allocate_partitions(context
))
3365 break; /* Success! */
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.");
3372 /* Now assign free space according to the weight logic */
3373 r
= context_grow_partitions(context
);
3377 /* Now calculate where each partition gets placed */
3378 context_place_partitions(context
);
3380 /* Make sure each partition has a unique UUID and unique label */
3381 r
= context_acquire_partition_uuids_and_labels(context
);
3385 r
= context_write_partition_table(context
, node
, from_scratch
);
3392 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);