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"
38 #include "locale-util.h"
39 #include "main-func.h"
40 #include "parse-util.h"
41 #include "path-util.h"
42 #include "pretty-print.h"
43 #include "proc-cmdline.h"
44 #include "sort-util.h"
45 #include "specifier.h"
46 #include "stat-util.h"
47 #include "stdio-util.h"
48 #include "string-util.h"
50 #include "terminal-util.h"
53 /* If not configured otherwise use a minimal partition size of 10M */
54 #define DEFAULT_MIN_SIZE (10*1024*1024)
56 /* Hard lower limit for new partition sizes */
57 #define HARD_MIN_SIZE 4096
59 /* Note: When growing and placing new partitions we always align to 4K sector size. It's how newer hard disks
60 * are designed, and if everything is aligned to that performance is best. And for older hard disks with 512B
61 * sector size devices were generally assumed to have an even number of sectors, hence at the worst we'll
62 * waste 3K per partition, which is probably fine. */
65 EMPTY_REFUSE
, /* refuse empty disks, never create a partition table */
66 EMPTY_ALLOW
, /* allow empty disks, create partition table if necessary */
67 EMPTY_REQUIRE
, /* require an empty disk, create a partition table */
68 EMPTY_FORCE
, /* make disk empty, erase everything, create a partition table always */
69 EMPTY_CREATE
, /* create disk as loopback file, create a partition table always */
70 } arg_empty
= EMPTY_REFUSE
;
72 static bool arg_dry_run
= true;
73 static const char *arg_node
= NULL
;
74 static char *arg_root
= NULL
;
75 static char *arg_definitions
= NULL
;
76 static bool arg_discard
= true;
77 static bool arg_can_factory_reset
= false;
78 static int arg_factory_reset
= -1;
79 static sd_id128_t arg_seed
= SD_ID128_NULL
;
80 static bool arg_randomize
= false;
81 static int arg_pretty
= -1;
82 static uint64_t arg_size
= UINT64_MAX
;
83 static bool arg_json
= false;
84 static JsonFormatFlags arg_json_format_flags
= 0;
86 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
87 STATIC_DESTRUCTOR_REGISTER(arg_definitions
, freep
);
89 typedef struct Partition Partition
;
90 typedef struct FreeArea FreeArea
;
91 typedef struct Context Context
;
94 char *definition_path
;
97 sd_id128_t current_uuid
, new_uuid
;
98 char *current_label
, *new_label
;
104 uint32_t weight
, padding_weight
;
106 uint64_t current_size
, new_size
;
107 uint64_t size_min
, size_max
;
109 uint64_t current_padding
, new_padding
;
110 uint64_t padding_min
, padding_max
;
115 struct fdisk_partition
*current_partition
;
116 struct fdisk_partition
*new_partition
;
117 FreeArea
*padding_area
;
118 FreeArea
*allocated_to_area
;
120 char *copy_blocks_path
;
122 uint64_t copy_blocks_size
;
124 LIST_FIELDS(Partition
, partitions
);
127 #define PARTITION_IS_FOREIGN(p) (!(p)->definition_path)
128 #define PARTITION_EXISTS(p) (!!(p)->current_partition)
137 LIST_HEAD(Partition
, partitions
);
140 FreeArea
**free_areas
;
141 size_t n_free_areas
, n_allocated_free_areas
;
143 uint64_t start
, end
, total
;
145 struct fdisk_context
*fdisk_context
;
150 static uint64_t round_down_size(uint64_t v
, uint64_t p
) {
154 static uint64_t round_up_size(uint64_t v
, uint64_t p
) {
156 v
= DIV_ROUND_UP(v
, p
);
158 if (v
> UINT64_MAX
/ p
)
159 return UINT64_MAX
; /* overflow */
164 static Partition
*partition_new(void) {
167 p
= new(Partition
, 1);
174 .current_size
= UINT64_MAX
,
175 .new_size
= UINT64_MAX
,
176 .size_min
= UINT64_MAX
,
177 .size_max
= UINT64_MAX
,
178 .current_padding
= UINT64_MAX
,
179 .new_padding
= UINT64_MAX
,
180 .padding_min
= UINT64_MAX
,
181 .padding_max
= UINT64_MAX
,
182 .partno
= UINT64_MAX
,
183 .offset
= UINT64_MAX
,
184 .copy_blocks_fd
= -1,
185 .copy_blocks_size
= UINT64_MAX
,
191 static Partition
* partition_free(Partition
*p
) {
195 free(p
->current_label
);
197 free(p
->definition_path
);
199 if (p
->current_partition
)
200 fdisk_unref_partition(p
->current_partition
);
201 if (p
->new_partition
)
202 fdisk_unref_partition(p
->new_partition
);
204 free(p
->copy_blocks_path
);
205 safe_close(p
->copy_blocks_fd
);
210 static Partition
* partition_unlink_and_free(Context
*context
, Partition
*p
) {
214 LIST_REMOVE(partitions
, context
->partitions
, p
);
216 assert(context
->n_partitions
> 0);
217 context
->n_partitions
--;
219 return partition_free(p
);
222 DEFINE_TRIVIAL_CLEANUP_FUNC(Partition
*, partition_free
);
224 static Context
*context_new(sd_id128_t seed
) {
227 context
= new(Context
, 1);
231 *context
= (Context
) {
241 static void context_free_free_areas(Context
*context
) {
244 for (size_t i
= 0; i
< context
->n_free_areas
; i
++)
245 free(context
->free_areas
[i
]);
247 context
->free_areas
= mfree(context
->free_areas
);
248 context
->n_free_areas
= 0;
249 context
->n_allocated_free_areas
= 0;
252 static Context
*context_free(Context
*context
) {
256 while (context
->partitions
)
257 partition_unlink_and_free(context
, context
->partitions
);
258 assert(context
->n_partitions
== 0);
260 context_free_free_areas(context
);
262 if (context
->fdisk_context
)
263 fdisk_unref_context(context
->fdisk_context
);
265 return mfree(context
);
268 DEFINE_TRIVIAL_CLEANUP_FUNC(Context
*, context_free
);
270 static int context_add_free_area(
278 assert(!after
|| !after
->padding_area
);
280 if (!GREEDY_REALLOC(context
->free_areas
, context
->n_allocated_free_areas
, context
->n_free_areas
+ 1))
283 a
= new(FreeArea
, 1);
292 context
->free_areas
[context
->n_free_areas
++] = a
;
295 after
->padding_area
= a
;
300 static bool context_drop_one_priority(Context
*context
) {
301 int32_t priority
= 0;
305 LIST_FOREACH(partitions
, p
, context
->partitions
) {
308 if (p
->priority
< priority
)
310 if (p
->priority
== priority
) {
311 exists
= exists
|| PARTITION_EXISTS(p
);
315 priority
= p
->priority
;
316 exists
= PARTITION_EXISTS(p
);
319 /* Refuse to drop partitions with 0 or negative priorities or partitions of priorities that have at
320 * least one existing priority */
321 if (priority
<= 0 || exists
)
324 LIST_FOREACH(partitions
, p
, context
->partitions
) {
325 if (p
->priority
< priority
)
332 log_info("Can't fit partition %s of priority %" PRIi32
", dropping.", p
->definition_path
, p
->priority
);
338 static uint64_t partition_min_size(const Partition
*p
) {
341 /* Calculate the disk space we really need at minimum for this partition. If the partition already
342 * exists the current size is what we really need. If it doesn't exist yet refuse to allocate less
345 * DEFAULT_MIN_SIZE is the default SizeMin= we configure if nothing else is specified. */
347 if (PARTITION_IS_FOREIGN(p
)) {
348 /* Don't allow changing size of partitions not managed by us */
349 assert(p
->current_size
!= UINT64_MAX
);
350 return p
->current_size
;
353 sz
= p
->current_size
!= UINT64_MAX
? p
->current_size
: HARD_MIN_SIZE
;
355 if (p
->copy_blocks_size
!= UINT64_MAX
)
356 sz
= MAX(p
->copy_blocks_size
, sz
);
358 return MAX(p
->size_min
!= UINT64_MAX
? p
->size_min
: DEFAULT_MIN_SIZE
, sz
);
361 static uint64_t partition_max_size(const Partition
*p
) {
362 /* Calculate how large the partition may become at max. This is generally the configured maximum
363 * size, except when it already exists and is larger than that. In that case it's the existing size,
364 * since we never want to shrink partitions. */
366 if (PARTITION_IS_FOREIGN(p
)) {
367 /* Don't allow changing size of partitions not managed by us */
368 assert(p
->current_size
!= UINT64_MAX
);
369 return p
->current_size
;
372 if (p
->current_size
!= UINT64_MAX
)
373 return MAX(p
->current_size
, p
->size_max
);
378 static uint64_t partition_min_size_with_padding(const Partition
*p
) {
381 /* Calculate the disk space we need for this partition plus any free space coming after it. This
382 * takes user configured padding into account as well as any additional whitespace needed to align
383 * the next partition to 4K again. */
385 sz
= partition_min_size(p
);
387 if (p
->padding_min
!= UINT64_MAX
)
388 sz
+= p
->padding_min
;
390 if (PARTITION_EXISTS(p
)) {
391 /* If the partition wasn't aligned, add extra space so that any we might add will be aligned */
392 assert(p
->offset
!= UINT64_MAX
);
393 return round_up_size(p
->offset
+ sz
, 4096) - p
->offset
;
396 /* If this is a new partition we'll place it aligned, hence we just need to round up the required size here */
397 return round_up_size(sz
, 4096);
400 static uint64_t free_area_available(const FreeArea
*a
) {
403 /* Determines how much of this free area is not allocated yet */
405 assert(a
->size
>= a
->allocated
);
406 return a
->size
- a
->allocated
;
409 static uint64_t free_area_available_for_new_partitions(const FreeArea
*a
) {
412 /* Similar to free_area_available(), but takes into account that the required size and padding of the
413 * preceding partition is honoured. */
415 avail
= free_area_available(a
);
417 uint64_t need
, space
;
419 need
= partition_min_size_with_padding(a
->after
);
421 assert(a
->after
->offset
!= UINT64_MAX
);
422 assert(a
->after
->current_size
!= UINT64_MAX
);
424 space
= round_up_size(a
->after
->offset
+ a
->after
->current_size
, 4096) - a
->after
->offset
+ avail
;
434 static int free_area_compare(FreeArea
*const *a
, FreeArea
*const*b
) {
435 return CMP(free_area_available_for_new_partitions(*a
),
436 free_area_available_for_new_partitions(*b
));
439 static uint64_t charge_size(uint64_t total
, uint64_t amount
) {
442 assert(amount
<= total
);
444 /* Subtract the specified amount from total, rounding up to multiple of 4K if there's room */
445 rounded
= round_up_size(amount
, 4096);
446 if (rounded
>= total
)
449 return total
- rounded
;
452 static uint64_t charge_weight(uint64_t total
, uint64_t amount
) {
453 assert(amount
<= total
);
454 return total
- amount
;
457 static bool context_allocate_partitions(Context
*context
) {
462 /* A simple first-fit algorithm, assuming the array of free areas is sorted by size in decreasing
465 LIST_FOREACH(partitions
, p
, context
->partitions
) {
470 /* Skip partitions we already dropped or that already exist */
471 if (p
->dropped
|| PARTITION_EXISTS(p
))
475 typesafe_qsort(context
->free_areas
, context
->n_free_areas
, free_area_compare
);
477 /* How much do we need to fit? */
478 required
= partition_min_size_with_padding(p
);
479 assert(required
% 4096 == 0);
481 for (size_t i
= 0; i
< context
->n_free_areas
; i
++) {
482 a
= context
->free_areas
[i
];
484 if (free_area_available_for_new_partitions(a
) >= required
) {
491 return false; /* 😢 Oh no! We can't fit this partition into any free area! */
493 /* Assign the partition to this free area */
494 p
->allocated_to_area
= a
;
496 /* Budget the minimal partition size */
497 a
->allocated
+= required
;
503 static int context_sum_weights(Context
*context
, FreeArea
*a
, uint64_t *ret
) {
504 uint64_t weight_sum
= 0;
511 /* Determine the sum of the weights of all partitions placed in or before the specified free area */
513 LIST_FOREACH(partitions
, p
, context
->partitions
) {
514 if (p
->padding_area
!= a
&& p
->allocated_to_area
!= a
)
517 if (p
->weight
> UINT64_MAX
- weight_sum
)
519 weight_sum
+= p
->weight
;
521 if (p
->padding_weight
> UINT64_MAX
- weight_sum
)
523 weight_sum
+= p
->padding_weight
;
530 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "Combined weight of partition exceeds unsigned 64bit range, refusing.");
533 static int scale_by_weight(uint64_t value
, uint64_t weight
, uint64_t weight_sum
, uint64_t *ret
) {
534 assert(weight_sum
>= weight
);
542 if (value
> UINT64_MAX
/ weight
)
543 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "Scaling by weight of partition exceeds unsigned 64bit range, refusing.");
545 *ret
= value
* weight
/ weight_sum
;
549 typedef enum GrowPartitionPhase
{
550 /* The first phase: we charge partitions which need more (according to constraints) than their weight-based share. */
553 /* The second phase: we charge partitions which need less (according to constraints) than their weight-based share. */
556 /* The third phase: we distribute what remains among the remaining partitions, according to the weights */
558 } GrowPartitionPhase
;
560 static int context_grow_partitions_phase(
563 GrowPartitionPhase phase
,
565 uint64_t *weight_sum
) {
573 /* Now let's look at the intended weights and adjust them taking the minimum space assignments into
574 * account. i.e. if a partition has a small weight but a high minimum space value set it should not
575 * get any additional room from the left-overs. Similar, if two partitions have the same weight they
576 * should get the same space if possible, even if one has a smaller minimum size than the other. */
577 LIST_FOREACH(partitions
, p
, context
->partitions
) {
579 /* Look only at partitions associated with this free area, i.e. immediately
580 * preceding it, or allocated into it */
581 if (p
->allocated_to_area
!= a
&& p
->padding_area
!= a
)
584 if (p
->new_size
== UINT64_MAX
) {
585 bool charge
= false, try_again
= false;
586 uint64_t share
, rsz
, xsz
;
588 /* Calculate how much this space this partition needs if everyone would get
589 * the weight based share */
590 r
= scale_by_weight(*span
, p
->weight
, *weight_sum
, &share
);
594 rsz
= partition_min_size(p
);
595 xsz
= partition_max_size(p
);
597 if (phase
== PHASE_OVERCHARGE
&& rsz
> share
) {
598 /* This partition needs more than its calculated share. Let's assign
599 * it that, and take this partition out of all calculations and start
603 charge
= try_again
= true;
605 } else if (phase
== PHASE_UNDERCHARGE
&& xsz
!= UINT64_MAX
&& xsz
< share
) {
606 /* This partition accepts less than its calculated
607 * share. Let's assign it that, and take this partition out
608 * of all calculations and start again. */
611 charge
= try_again
= true;
613 } else if (phase
== PHASE_DISTRIBUTE
) {
614 /* This partition can accept its calculated share. Let's
615 * assign it. There's no need to restart things here since
616 * assigning this shouldn't impact the shares of the other
619 if (PARTITION_IS_FOREIGN(p
))
620 /* Never change of foreign partitions (i.e. those we don't manage) */
621 p
->new_size
= p
->current_size
;
623 p
->new_size
= MAX(round_down_size(share
, 4096), rsz
);
629 *span
= charge_size(*span
, p
->new_size
);
630 *weight_sum
= charge_weight(*weight_sum
, p
->weight
);
634 return 0; /* try again */
637 if (p
->new_padding
== UINT64_MAX
) {
638 bool charge
= false, try_again
= false;
641 r
= scale_by_weight(*span
, p
->padding_weight
, *weight_sum
, &share
);
645 if (phase
== PHASE_OVERCHARGE
&& p
->padding_min
!= UINT64_MAX
&& p
->padding_min
> share
) {
646 p
->new_padding
= p
->padding_min
;
647 charge
= try_again
= true;
648 } else if (phase
== PHASE_UNDERCHARGE
&& p
->padding_max
!= UINT64_MAX
&& p
->padding_max
< share
) {
649 p
->new_padding
= p
->padding_max
;
650 charge
= try_again
= true;
651 } else if (phase
== PHASE_DISTRIBUTE
) {
653 p
->new_padding
= round_down_size(share
, 4096);
654 if (p
->padding_min
!= UINT64_MAX
&& p
->new_padding
< p
->padding_min
)
655 p
->new_padding
= p
->padding_min
;
661 *span
= charge_size(*span
, p
->new_padding
);
662 *weight_sum
= charge_weight(*weight_sum
, p
->padding_weight
);
666 return 0; /* try again */
673 static int context_grow_partitions_on_free_area(Context
*context
, FreeArea
*a
) {
674 uint64_t weight_sum
= 0, span
;
680 r
= context_sum_weights(context
, a
, &weight_sum
);
684 /* Let's calculate the total area covered by this free area and the partition before it */
687 assert(a
->after
->offset
!= UINT64_MAX
);
688 assert(a
->after
->current_size
!= UINT64_MAX
);
690 span
+= round_up_size(a
->after
->offset
+ a
->after
->current_size
, 4096) - a
->after
->offset
;
693 GrowPartitionPhase phase
= PHASE_OVERCHARGE
;
695 r
= context_grow_partitions_phase(context
, a
, phase
, &span
, &weight_sum
);
698 if (r
== 0) /* not done yet, re-run this phase */
701 if (phase
== PHASE_OVERCHARGE
)
702 phase
= PHASE_UNDERCHARGE
;
703 else if (phase
== PHASE_UNDERCHARGE
)
704 phase
= PHASE_DISTRIBUTE
;
705 else if (phase
== PHASE_DISTRIBUTE
)
709 /* We still have space left over? Donate to preceding partition if we have one */
710 if (span
> 0 && a
->after
&& !PARTITION_IS_FOREIGN(a
->after
)) {
713 assert(a
->after
->new_size
!= UINT64_MAX
);
714 m
= a
->after
->new_size
+ span
;
716 xsz
= partition_max_size(a
->after
);
717 if (xsz
!= UINT64_MAX
&& m
> xsz
)
720 span
= charge_size(span
, m
- a
->after
->new_size
);
721 a
->after
->new_size
= m
;
724 /* What? Even still some space left (maybe because there was no preceding partition, or it had a
725 * size limit), then let's donate it to whoever wants it. */
729 LIST_FOREACH(partitions
, p
, context
->partitions
) {
732 if (p
->allocated_to_area
!= a
)
735 if (PARTITION_IS_FOREIGN(p
))
738 assert(p
->new_size
!= UINT64_MAX
);
739 m
= p
->new_size
+ span
;
741 xsz
= partition_max_size(p
);
742 if (xsz
!= UINT64_MAX
&& m
> xsz
)
745 span
= charge_size(span
, m
- p
->new_size
);
753 /* Yuck, still no one? Then make it padding */
754 if (span
> 0 && a
->after
) {
755 assert(a
->after
->new_padding
!= UINT64_MAX
);
756 a
->after
->new_padding
+= span
;
762 static int context_grow_partitions(Context
*context
) {
768 for (size_t i
= 0; i
< context
->n_free_areas
; i
++) {
769 r
= context_grow_partitions_on_free_area(context
, context
->free_areas
[i
]);
774 /* All existing partitions that have no free space after them can't change size */
775 LIST_FOREACH(partitions
, p
, context
->partitions
) {
779 if (!PARTITION_EXISTS(p
) || p
->padding_area
) {
780 /* The algorithm above must have initialized this already */
781 assert(p
->new_size
!= UINT64_MAX
);
785 assert(p
->new_size
== UINT64_MAX
);
786 p
->new_size
= p
->current_size
;
788 assert(p
->new_padding
== UINT64_MAX
);
789 p
->new_padding
= p
->current_padding
;
795 static void context_place_partitions(Context
*context
) {
801 /* Determine next partition number to assign */
802 LIST_FOREACH(partitions
, p
, context
->partitions
) {
803 if (!PARTITION_EXISTS(p
))
806 assert(p
->partno
!= UINT64_MAX
);
807 if (p
->partno
>= partno
)
808 partno
= p
->partno
+ 1;
811 for (size_t i
= 0; i
< context
->n_free_areas
; i
++) {
812 FreeArea
*a
= context
->free_areas
[i
];
813 uint64_t start
, left
;
816 assert(a
->after
->offset
!= UINT64_MAX
);
817 assert(a
->after
->new_size
!= UINT64_MAX
);
818 assert(a
->after
->new_padding
!= UINT64_MAX
);
820 start
= a
->after
->offset
+ a
->after
->new_size
+ a
->after
->new_padding
;
822 start
= context
->start
;
824 start
= round_up_size(start
, 4096);
827 LIST_FOREACH(partitions
, p
, context
->partitions
) {
828 if (p
->allocated_to_area
!= a
)
832 p
->partno
= partno
++;
834 assert(left
>= p
->new_size
);
835 start
+= p
->new_size
;
838 assert(left
>= p
->new_padding
);
839 start
+= p
->new_padding
;
840 left
-= p
->new_padding
;
845 static int config_parse_type(
847 const char *filename
,
850 unsigned section_line
,
857 sd_id128_t
*type_uuid
= data
;
863 r
= gpt_partition_type_uuid_from_string(rvalue
, type_uuid
);
865 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse partition type: %s", rvalue
);
870 static int config_parse_label(
872 const char *filename
,
875 unsigned section_line
,
882 static const Specifier specifier_table
[] = {
883 { 'm', specifier_machine_id
, NULL
},
884 { 'b', specifier_boot_id
, NULL
},
885 { 'H', specifier_host_name
, NULL
},
886 { 'l', specifier_short_host_name
, NULL
},
887 { 'v', specifier_kernel_release
, NULL
},
888 { 'a', specifier_architecture
, NULL
},
889 { 'o', specifier_os_id
, NULL
},
890 { 'w', specifier_os_version_id
, NULL
},
891 { 'B', specifier_os_build_id
, NULL
},
892 { 'W', specifier_os_variant_id
, NULL
},
896 _cleanup_free_ char16_t
*recoded
= NULL
;
897 _cleanup_free_
char *resolved
= NULL
;
904 r
= specifier_printf(rvalue
, specifier_table
, NULL
, &resolved
);
906 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
907 "Failed to expand specifiers in Label=, ignoring: %s", rvalue
);
911 if (!utf8_is_valid(resolved
)) {
912 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
913 "Partition label not valid UTF-8, ignoring: %s", rvalue
);
917 recoded
= utf8_to_utf16(resolved
, strlen(resolved
));
921 if (char16_strlen(recoded
) > 36) {
922 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
923 "Partition label too long for GPT table, ignoring: \"%s\" (from \"%s\")",
928 free_and_replace(*label
, resolved
);
932 static int config_parse_weight(
934 const char *filename
,
937 unsigned section_line
,
944 uint32_t *priority
= data
, v
;
950 r
= safe_atou32(rvalue
, &v
);
952 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
953 "Failed to parse weight value, ignoring: %s", rvalue
);
957 if (v
> 1000U*1000U) {
958 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
959 "Weight needs to be in range 0…10000000, ignoring: %" PRIu32
, v
);
967 static int config_parse_size4096(
969 const char *filename
,
972 unsigned section_line
,
979 uint64_t *sz
= data
, parsed
;
985 r
= parse_size(rvalue
, 1024, &parsed
);
987 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
988 "Failed to parse size value: %s", rvalue
);
991 *sz
= round_up_size(parsed
, 4096);
993 *sz
= round_down_size(parsed
, 4096);
998 log_syntax(unit
, LOG_NOTICE
, filename
, line
, r
, "Rounded %s= size %" PRIu64
" → %" PRIu64
", a multiple of 4096.", lvalue
, parsed
, *sz
);
1003 static int partition_read_definition(Partition
*p
, const char *path
) {
1005 ConfigTableItem table
[] = {
1006 { "Partition", "Type", config_parse_type
, 0, &p
->type_uuid
},
1007 { "Partition", "Label", config_parse_label
, 0, &p
->new_label
},
1008 { "Partition", "UUID", config_parse_id128
, 0, &p
->new_uuid
},
1009 { "Partition", "Priority", config_parse_int32
, 0, &p
->priority
},
1010 { "Partition", "Weight", config_parse_weight
, 0, &p
->weight
},
1011 { "Partition", "PaddingWeight", config_parse_weight
, 0, &p
->padding_weight
},
1012 { "Partition", "SizeMinBytes", config_parse_size4096
, 1, &p
->size_min
},
1013 { "Partition", "SizeMaxBytes", config_parse_size4096
, -1, &p
->size_max
},
1014 { "Partition", "PaddingMinBytes", config_parse_size4096
, 1, &p
->padding_min
},
1015 { "Partition", "PaddingMaxBytes", config_parse_size4096
, -1, &p
->padding_max
},
1016 { "Partition", "FactoryReset", config_parse_bool
, 0, &p
->factory_reset
},
1017 { "Partition", "CopyBlocks", config_parse_path
, 0, &p
->copy_blocks_path
},
1022 r
= config_parse(NULL
, path
, NULL
,
1024 config_item_table_lookup
, table
,
1031 if (p
->size_min
!= UINT64_MAX
&& p
->size_max
!= UINT64_MAX
&& p
->size_min
> p
->size_max
)
1032 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1033 "SizeMinBytes= larger than SizeMaxBytes=, refusing.");
1035 if (p
->padding_min
!= UINT64_MAX
&& p
->padding_max
!= UINT64_MAX
&& p
->padding_min
> p
->padding_max
)
1036 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1037 "PaddingMinBytes= larger than PaddingMaxBytes=, refusing.");
1039 if (sd_id128_is_null(p
->type_uuid
))
1040 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1041 "Type= not defined, refusing.");
1046 static int context_read_definitions(
1048 const char *directory
,
1051 _cleanup_strv_free_
char **files
= NULL
;
1052 Partition
*last
= NULL
;
1059 r
= conf_files_list_strv(&files
, ".conf", NULL
, CONF_FILES_REGULAR
|CONF_FILES_FILTER_MASKED
, (const char**) STRV_MAKE(directory
));
1061 r
= conf_files_list_strv(&files
, ".conf", root
, CONF_FILES_REGULAR
|CONF_FILES_FILTER_MASKED
, (const char**) CONF_PATHS_STRV("repart.d"));
1063 return log_error_errno(r
, "Failed to enumerate *.conf files: %m");
1065 STRV_FOREACH(f
, files
) {
1066 _cleanup_(partition_freep
) Partition
*p
= NULL
;
1068 p
= partition_new();
1072 p
->definition_path
= strdup(*f
);
1073 if (!p
->definition_path
)
1076 r
= partition_read_definition(p
, *f
);
1080 LIST_INSERT_AFTER(partitions
, context
->partitions
, last
, p
);
1082 context
->n_partitions
++;
1088 DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_context
*, fdisk_unref_context
);
1089 DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_partition
*, fdisk_unref_partition
);
1090 DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_parttype
*, fdisk_unref_parttype
);
1091 DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_table
*, fdisk_unref_table
);
1093 static int determine_current_padding(
1094 struct fdisk_context
*c
,
1095 struct fdisk_table
*t
,
1096 struct fdisk_partition
*p
,
1099 size_t n_partitions
;
1100 uint64_t offset
, next
= UINT64_MAX
;
1106 if (!fdisk_partition_has_end(p
))
1107 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition has no end!");
1109 offset
= fdisk_partition_get_end(p
);
1110 assert(offset
< UINT64_MAX
/ 512);
1113 n_partitions
= fdisk_table_get_nents(t
);
1114 for (size_t i
= 0; i
< n_partitions
; i
++) {
1115 struct fdisk_partition
*q
;
1118 q
= fdisk_table_get_partition(t
, i
);
1120 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to read partition metadata: %m");
1122 if (fdisk_partition_is_used(q
) <= 0)
1125 if (!fdisk_partition_has_start(q
))
1128 start
= fdisk_partition_get_start(q
);
1129 assert(start
< UINT64_MAX
/ 512);
1132 if (start
>= offset
&& (next
== UINT64_MAX
|| next
> start
))
1136 if (next
== UINT64_MAX
) {
1137 /* No later partition? In that case check the end of the usable area */
1138 next
= fdisk_get_last_lba(c
);
1139 assert(next
< UINT64_MAX
);
1140 next
++; /* The last LBA is one sector before the end */
1142 assert(next
< UINT64_MAX
/ 512);
1146 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition end beyond disk end.");
1149 assert(next
>= offset
);
1150 offset
= round_up_size(offset
, 4096);
1151 next
= round_down_size(next
, 4096);
1153 if (next
>= offset
) /* Check again, rounding might have fucked things up */
1154 *ret
= next
- offset
;
1161 static int fdisk_ask_cb(struct fdisk_context
*c
, struct fdisk_ask
*ask
, void *data
) {
1162 _cleanup_free_
char *ids
= NULL
;
1165 if (fdisk_ask_get_type(ask
) != FDISK_ASKTYPE_STRING
)
1168 ids
= new(char, ID128_UUID_STRING_MAX
);
1172 r
= fdisk_ask_string_set_result(ask
, id128_to_uuid_string(*(sd_id128_t
*) data
, ids
));
1180 static int fdisk_set_disklabel_id_by_uuid(struct fdisk_context
*c
, sd_id128_t id
) {
1183 r
= fdisk_set_ask(c
, fdisk_ask_cb
, &id
);
1187 r
= fdisk_set_disklabel_id(c
);
1191 return fdisk_set_ask(c
, NULL
, NULL
);
1194 #define DISK_UUID_TOKEN "disk-uuid"
1196 static int disk_acquire_uuid(Context
*context
, sd_id128_t
*ret
) {
1198 unsigned char md
[SHA256_DIGEST_LENGTH
];
1205 /* Calculate the HMAC-SHA256 of the string "disk-uuid", keyed off the machine ID. We use the machine
1206 * ID as key (and not as cleartext!) since it's the machine ID we don't want to leak. */
1208 if (!HMAC(EVP_sha256(),
1209 &context
->seed
, sizeof(context
->seed
),
1210 (const unsigned char*) DISK_UUID_TOKEN
, strlen(DISK_UUID_TOKEN
),
1212 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE
), "HMAC-SHA256 calculation failed.");
1214 /* Take the first half, mark it as v4 UUID */
1215 assert_cc(sizeof(result
.md
) == sizeof(result
.id
) * 2);
1216 *ret
= id128_make_v4_uuid(result
.id
);
1220 static int context_load_partition_table(
1225 _cleanup_(fdisk_unref_contextp
) struct fdisk_context
*c
= NULL
;
1226 _cleanup_(fdisk_unref_tablep
) struct fdisk_table
*t
= NULL
;
1227 uint64_t left_boundary
= UINT64_MAX
, first_lba
, last_lba
, nsectors
;
1228 _cleanup_free_
char *disk_uuid_string
= NULL
;
1229 bool from_scratch
= false;
1230 sd_id128_t disk_uuid
;
1231 size_t n_partitions
;
1238 c
= fdisk_new_context();
1242 /* libfdisk doesn't have an API to operate on arbitrary fds, hence reopen the fd going via the
1243 * /proc/self/fd/ magic path if we have an existing fd. Open the original file otherwise. */
1244 if (*backing_fd
< 0)
1245 r
= fdisk_assign_device(c
, node
, arg_dry_run
);
1247 char procfs_path
[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
1248 xsprintf(procfs_path
, "/proc/self/fd/%i", *backing_fd
);
1250 r
= fdisk_assign_device(c
, procfs_path
, arg_dry_run
);
1253 return log_error_errno(r
, "Failed to open device '%s': %m", node
);
1255 if (*backing_fd
< 0) {
1256 /* If we have no fd referencing the device yet, make a copy of the fd now, so that we have one */
1257 *backing_fd
= fcntl(fdisk_get_devfd(c
), F_DUPFD_CLOEXEC
, 3);
1258 if (*backing_fd
< 0)
1259 return log_error_errno(errno
, "Failed to duplicate fdisk fd: %m");
1262 /* Tell udev not to interfere while we are processing the device */
1263 if (flock(fdisk_get_devfd(c
), arg_dry_run
? LOCK_SH
: LOCK_EX
) < 0)
1264 return log_error_errno(errno
, "Failed to lock block device: %m");
1266 switch (arg_empty
) {
1269 /* Refuse empty disks, insist on an existing GPT partition table */
1270 if (!fdisk_is_labeltype(c
, FDISK_DISKLABEL_GPT
))
1271 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON
), "Disk %s has no GPT disk label, not repartitioning.", node
);
1276 /* Require an empty disk, refuse any existing partition table */
1277 r
= fdisk_has_label(c
);
1279 return log_error_errno(r
, "Failed to determine whether disk %s has a disk label: %m", node
);
1281 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON
), "Disk %s already has a disk label, refusing.", node
);
1283 from_scratch
= true;
1287 /* Allow both an empty disk and an existing partition table, but only GPT */
1288 r
= fdisk_has_label(c
);
1290 return log_error_errno(r
, "Failed to determine whether disk %s has a disk label: %m", node
);
1292 if (!fdisk_is_labeltype(c
, FDISK_DISKLABEL_GPT
))
1293 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON
), "Disk %s has non-GPT disk label, not repartitioning.", node
);
1295 from_scratch
= true;
1301 /* Always reinitiaize the disk, don't consider what there was on the disk before */
1302 from_scratch
= true;
1307 r
= fdisk_enable_wipe(c
, true);
1309 return log_error_errno(r
, "Failed to enable wiping of disk signature: %m");
1311 r
= fdisk_create_disklabel(c
, "gpt");
1313 return log_error_errno(r
, "Failed to create GPT disk label: %m");
1315 r
= disk_acquire_uuid(context
, &disk_uuid
);
1317 return log_error_errno(r
, "Failed to acquire disk GPT uuid: %m");
1319 r
= fdisk_set_disklabel_id_by_uuid(c
, disk_uuid
);
1321 return log_error_errno(r
, "Failed to set GPT disk label: %m");
1323 goto add_initial_free_area
;
1326 r
= fdisk_get_disklabel_id(c
, &disk_uuid_string
);
1328 return log_error_errno(r
, "Failed to get current GPT disk label UUID: %m");
1330 r
= sd_id128_from_string(disk_uuid_string
, &disk_uuid
);
1332 return log_error_errno(r
, "Failed to parse current GPT disk label UUID: %m");
1334 if (sd_id128_is_null(disk_uuid
)) {
1335 r
= disk_acquire_uuid(context
, &disk_uuid
);
1337 return log_error_errno(r
, "Failed to acquire disk GPT uuid: %m");
1339 r
= fdisk_set_disklabel_id(c
);
1341 return log_error_errno(r
, "Failed to set GPT disk label: %m");
1344 r
= fdisk_get_partitions(c
, &t
);
1346 return log_error_errno(r
, "Failed to acquire partition table: %m");
1348 n_partitions
= fdisk_table_get_nents(t
);
1349 for (size_t i
= 0; i
< n_partitions
; i
++) {
1350 _cleanup_free_
char *label_copy
= NULL
;
1351 Partition
*pp
, *last
= NULL
;
1352 struct fdisk_partition
*p
;
1353 struct fdisk_parttype
*pt
;
1354 const char *pts
, *ids
, *label
;
1357 sd_id128_t ptid
, id
;
1360 p
= fdisk_table_get_partition(t
, i
);
1362 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to read partition metadata: %m");
1364 if (fdisk_partition_is_used(p
) <= 0)
1367 if (fdisk_partition_has_start(p
) <= 0 ||
1368 fdisk_partition_has_size(p
) <= 0 ||
1369 fdisk_partition_has_partno(p
) <= 0)
1370 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Found a partition without a position, size or number.");
1372 pt
= fdisk_partition_get_type(p
);
1374 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to acquire type of partition: %m");
1376 pts
= fdisk_parttype_get_string(pt
);
1378 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to acquire type of partition as string: %m");
1380 r
= sd_id128_from_string(pts
, &ptid
);
1382 return log_error_errno(r
, "Failed to parse partition type UUID %s: %m", pts
);
1384 ids
= fdisk_partition_get_uuid(p
);
1386 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Found a partition without a UUID.");
1388 r
= sd_id128_from_string(ids
, &id
);
1390 return log_error_errno(r
, "Failed to parse partition UUID %s: %m", ids
);
1392 label
= fdisk_partition_get_name(p
);
1393 if (!isempty(label
)) {
1394 label_copy
= strdup(label
);
1399 sz
= fdisk_partition_get_size(p
);
1400 assert_se(sz
<= UINT64_MAX
/512);
1403 start
= fdisk_partition_get_start(p
);
1404 assert_se(start
<= UINT64_MAX
/512);
1407 partno
= fdisk_partition_get_partno(p
);
1409 if (left_boundary
== UINT64_MAX
|| left_boundary
> start
)
1410 left_boundary
= start
;
1412 /* Assign this existing partition to the first partition of the right type that doesn't have
1413 * an existing one assigned yet. */
1414 LIST_FOREACH(partitions
, pp
, context
->partitions
) {
1417 if (!sd_id128_equal(pp
->type_uuid
, ptid
))
1420 if (!pp
->current_partition
) {
1421 pp
->current_uuid
= id
;
1422 pp
->current_size
= sz
;
1424 pp
->partno
= partno
;
1425 pp
->current_label
= TAKE_PTR(label_copy
);
1427 pp
->current_partition
= p
;
1428 fdisk_ref_partition(p
);
1430 r
= determine_current_padding(c
, t
, p
, &pp
->current_padding
);
1434 if (pp
->current_padding
> 0) {
1435 r
= context_add_free_area(context
, pp
->current_padding
, pp
);
1445 /* If we have no matching definition, create a new one. */
1447 _cleanup_(partition_freep
) Partition
*np
= NULL
;
1449 np
= partition_new();
1453 np
->current_uuid
= id
;
1454 np
->type_uuid
= ptid
;
1455 np
->current_size
= sz
;
1457 np
->partno
= partno
;
1458 np
->current_label
= TAKE_PTR(label_copy
);
1460 np
->current_partition
= p
;
1461 fdisk_ref_partition(p
);
1463 r
= determine_current_padding(c
, t
, p
, &np
->current_padding
);
1467 if (np
->current_padding
> 0) {
1468 r
= context_add_free_area(context
, np
->current_padding
, np
);
1473 LIST_INSERT_AFTER(partitions
, context
->partitions
, last
, TAKE_PTR(np
));
1474 context
->n_partitions
++;
1478 add_initial_free_area
:
1479 nsectors
= fdisk_get_nsectors(c
);
1480 assert(nsectors
<= UINT64_MAX
/512);
1483 first_lba
= fdisk_get_first_lba(c
);
1484 assert(first_lba
<= UINT64_MAX
/512);
1487 last_lba
= fdisk_get_last_lba(c
);
1488 assert(last_lba
< UINT64_MAX
);
1490 assert(last_lba
<= UINT64_MAX
/512);
1493 assert(last_lba
>= first_lba
);
1495 if (left_boundary
== UINT64_MAX
) {
1496 /* No partitions at all? Then the whole disk is up for grabs. */
1498 first_lba
= round_up_size(first_lba
, 4096);
1499 last_lba
= round_down_size(last_lba
, 4096);
1501 if (last_lba
> first_lba
) {
1502 r
= context_add_free_area(context
, last_lba
- first_lba
, NULL
);
1507 /* Add space left of first partition */
1508 assert(left_boundary
>= first_lba
);
1510 first_lba
= round_up_size(first_lba
, 4096);
1511 left_boundary
= round_down_size(left_boundary
, 4096);
1512 last_lba
= round_down_size(last_lba
, 4096);
1514 if (left_boundary
> first_lba
) {
1515 r
= context_add_free_area(context
, left_boundary
- first_lba
, NULL
);
1521 context
->start
= first_lba
;
1522 context
->end
= last_lba
;
1523 context
->total
= nsectors
;
1524 context
->fdisk_context
= TAKE_PTR(c
);
1526 return from_scratch
;
1529 static void context_unload_partition_table(Context
*context
) {
1530 Partition
*p
, *next
;
1534 LIST_FOREACH_SAFE(partitions
, p
, next
, context
->partitions
) {
1536 /* Entirely remove partitions that have no configuration */
1537 if (PARTITION_IS_FOREIGN(p
)) {
1538 partition_unlink_and_free(context
, p
);
1542 /* Otherwise drop all data we read off the block device and everything we might have
1543 * calculated based on it */
1546 p
->current_size
= UINT64_MAX
;
1547 p
->new_size
= UINT64_MAX
;
1548 p
->current_padding
= UINT64_MAX
;
1549 p
->new_padding
= UINT64_MAX
;
1550 p
->partno
= UINT64_MAX
;
1551 p
->offset
= UINT64_MAX
;
1553 if (p
->current_partition
) {
1554 fdisk_unref_partition(p
->current_partition
);
1555 p
->current_partition
= NULL
;
1558 if (p
->new_partition
) {
1559 fdisk_unref_partition(p
->new_partition
);
1560 p
->new_partition
= NULL
;
1563 p
->padding_area
= NULL
;
1564 p
->allocated_to_area
= NULL
;
1566 p
->current_uuid
= p
->new_uuid
= SD_ID128_NULL
;
1569 context
->start
= UINT64_MAX
;
1570 context
->end
= UINT64_MAX
;
1571 context
->total
= UINT64_MAX
;
1573 if (context
->fdisk_context
) {
1574 fdisk_unref_context(context
->fdisk_context
);
1575 context
->fdisk_context
= NULL
;
1578 context_free_free_areas(context
);
1581 static int format_size_change(uint64_t from
, uint64_t to
, char **ret
) {
1582 char format_buffer1
[FORMAT_BYTES_MAX
], format_buffer2
[FORMAT_BYTES_MAX
], *buf
;
1584 if (from
!= UINT64_MAX
)
1585 format_bytes(format_buffer1
, sizeof(format_buffer1
), from
);
1586 if (to
!= UINT64_MAX
)
1587 format_bytes(format_buffer2
, sizeof(format_buffer2
), to
);
1589 if (from
!= UINT64_MAX
) {
1590 if (from
== to
|| to
== UINT64_MAX
)
1591 buf
= strdup(format_buffer1
);
1593 buf
= strjoin(format_buffer1
, " ", special_glyph(SPECIAL_GLYPH_ARROW
), " ", format_buffer2
);
1594 } else if (to
!= UINT64_MAX
)
1595 buf
= strjoin(special_glyph(SPECIAL_GLYPH_ARROW
), " ", format_buffer2
);
1604 *ret
= TAKE_PTR(buf
);
1608 static const char *partition_label(const Partition
*p
) {
1612 return p
->new_label
;
1614 if (p
->current_label
)
1615 return p
->current_label
;
1617 return gpt_partition_type_uuid_to_string(p
->type_uuid
);
1620 static int context_dump_partitions(Context
*context
, const char *node
) {
1621 _cleanup_(table_unrefp
) Table
*t
= NULL
;
1622 uint64_t sum_padding
= 0, sum_size
= 0;
1626 if (!arg_json
&& context
->n_partitions
== 0) {
1627 log_info("Empty partition table.");
1631 t
= table_new("type", "label", "uuid", "file", "node", "offset", "old size", "raw size", "size", "old padding", "raw padding", "padding", "activity");
1635 if (!DEBUG_LOGGING
) {
1637 (void) table_set_display(t
, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
1638 (size_t) 5, (size_t) 6, (size_t) 7, (size_t) 9, (size_t) 10, (size_t) 12, (size_t) -1);
1640 (void) table_set_display(t
, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
1641 (size_t) 8, (size_t) 11, (size_t) -1);
1644 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 4), 100);
1645 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 5), 100);
1647 LIST_FOREACH(partitions
, p
, context
->partitions
) {
1648 _cleanup_free_
char *size_change
= NULL
, *padding_change
= NULL
, *partname
= NULL
;
1649 char uuid_buffer
[ID128_UUID_STRING_MAX
];
1650 const char *label
, *activity
= NULL
;
1655 if (p
->current_size
== UINT64_MAX
)
1656 activity
= "create";
1657 else if (p
->current_size
!= p
->new_size
)
1658 activity
= "resize";
1660 label
= partition_label(p
);
1661 partname
= p
->partno
!= UINT64_MAX
? fdisk_partname(node
, p
->partno
+1) : NULL
;
1663 r
= format_size_change(p
->current_size
, p
->new_size
, &size_change
);
1667 r
= format_size_change(p
->current_padding
, p
->new_padding
, &padding_change
);
1671 if (p
->new_size
!= UINT64_MAX
)
1672 sum_size
+= p
->new_size
;
1673 if (p
->new_padding
!= UINT64_MAX
)
1674 sum_padding
+= p
->new_padding
;
1678 TABLE_STRING
, gpt_partition_type_uuid_to_string_harder(p
->type_uuid
, uuid_buffer
),
1679 TABLE_STRING
, label
?: "-", TABLE_SET_COLOR
, label
? NULL
: ansi_grey(),
1680 TABLE_UUID
, sd_id128_is_null(p
->new_uuid
) ? p
->current_uuid
: p
->new_uuid
,
1681 TABLE_STRING
, p
->definition_path
? basename(p
->definition_path
) : "-", TABLE_SET_COLOR
, p
->definition_path
? NULL
: ansi_grey(),
1682 TABLE_STRING
, partname
?: "-", TABLE_SET_COLOR
, partname
? NULL
: ansi_highlight(),
1683 TABLE_UINT64
, p
->offset
,
1684 TABLE_UINT64
, p
->current_size
== UINT64_MAX
? 0 : p
->current_size
,
1685 TABLE_UINT64
, p
->new_size
,
1686 TABLE_STRING
, size_change
, TABLE_SET_COLOR
, !p
->partitions_next
&& sum_size
> 0 ? ansi_underline() : NULL
,
1687 TABLE_UINT64
, p
->current_padding
== UINT64_MAX
? 0 : p
->current_padding
,
1688 TABLE_UINT64
, p
->new_padding
,
1689 TABLE_STRING
, padding_change
, TABLE_SET_COLOR
, !p
->partitions_next
&& sum_padding
> 0 ? ansi_underline() : NULL
,
1690 TABLE_STRING
, activity
?: "unknown");
1692 return table_log_add_error(r
);
1695 if (!arg_json
&& (sum_padding
> 0 || sum_size
> 0)) {
1696 char s
[FORMAT_BYTES_MAX
];
1699 a
= strjoina(special_glyph(SPECIAL_GLYPH_SIGMA
), " = ", format_bytes(s
, sizeof(s
), sum_size
));
1700 b
= strjoina(special_glyph(SPECIAL_GLYPH_SIGMA
), " = ", format_bytes(s
, sizeof(s
), sum_padding
));
1718 return table_log_add_error(r
);
1722 r
= table_print_json(t
, stdout
, arg_json_format_flags
);
1724 r
= table_print(t
, stdout
);
1726 return log_error_errno(r
, "Failed to dump table: %m");
1731 static void context_bar_char_process_partition(
1736 size_t *ret_start
) {
1738 uint64_t from
, to
, total
;
1749 assert(p
->offset
!= UINT64_MAX
);
1750 assert(p
->new_size
!= UINT64_MAX
);
1753 to
= from
+ p
->new_size
;
1755 assert(context
->end
>= context
->start
);
1756 total
= context
->end
- context
->start
;
1758 assert(from
>= context
->start
);
1759 assert(from
<= context
->end
);
1760 x
= (from
- context
->start
) * n
/ total
;
1762 assert(to
>= context
->start
);
1763 assert(to
<= context
->end
);
1764 y
= (to
- context
->start
) * n
/ total
;
1769 for (size_t i
= x
; i
< y
; i
++)
1775 static int partition_hint(const Partition
*p
, const char *node
, char **ret
) {
1776 _cleanup_free_
char *buf
= NULL
;
1777 char ids
[ID128_UUID_STRING_MAX
];
1781 /* Tries really hard to find a suitable description for this partition */
1783 if (p
->definition_path
) {
1784 buf
= strdup(basename(p
->definition_path
));
1788 label
= partition_label(p
);
1789 if (!isempty(label
)) {
1790 buf
= strdup(label
);
1794 if (p
->partno
!= UINT64_MAX
) {
1795 buf
= fdisk_partname(node
, p
->partno
+1);
1799 if (!sd_id128_is_null(p
->new_uuid
))
1801 else if (!sd_id128_is_null(p
->current_uuid
))
1802 id
= p
->current_uuid
;
1806 buf
= strdup(id128_to_uuid_string(id
, ids
));
1812 *ret
= TAKE_PTR(buf
);
1816 static int context_dump_partition_bar(Context
*context
, const char *node
) {
1817 _cleanup_free_ Partition
**bar
= NULL
;
1818 _cleanup_free_
size_t *start_array
= NULL
;
1819 Partition
*p
, *last
= NULL
;
1823 assert_se((c
= columns()) >= 2);
1824 c
-= 2; /* We do not use the leftmost and rightmost character cell */
1826 bar
= new0(Partition
*, c
);
1830 start_array
= new(size_t, context
->n_partitions
);
1834 LIST_FOREACH(partitions
, p
, context
->partitions
)
1835 context_bar_char_process_partition(context
, bar
, c
, p
, start_array
+ j
++);
1839 for (size_t i
= 0; i
< c
; i
++) {
1844 fputs(z
? ansi_green() : ansi_yellow(), stdout
);
1845 fputs(special_glyph(SPECIAL_GLYPH_DARK_SHADE
), stdout
);
1847 fputs(ansi_normal(), stdout
);
1848 fputs(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE
), stdout
);
1854 fputs(ansi_normal(), stdout
);
1857 for (size_t i
= 0; i
< context
->n_partitions
; i
++) {
1858 _cleanup_free_
char **line
= NULL
;
1860 line
= new0(char*, c
);
1865 LIST_FOREACH(partitions
, p
, context
->partitions
) {
1866 _cleanup_free_
char *d
= NULL
;
1869 if (i
< context
->n_partitions
- j
) {
1871 if (line
[start_array
[j
-1]]) {
1874 /* Upgrade final corner to the right with a branch to the right */
1875 e
= startswith(line
[start_array
[j
-1]], special_glyph(SPECIAL_GLYPH_TREE_RIGHT
));
1877 d
= strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH
), e
);
1884 d
= strdup(special_glyph(SPECIAL_GLYPH_TREE_VERTICAL
));
1889 } else if (i
== context
->n_partitions
- j
) {
1890 _cleanup_free_
char *hint
= NULL
;
1892 (void) partition_hint(p
, node
, &hint
);
1894 if (streq_ptr(line
[start_array
[j
-1]], special_glyph(SPECIAL_GLYPH_TREE_VERTICAL
)))
1895 d
= strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH
), " ", strna(hint
));
1897 d
= strjoin(special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), " ", strna(hint
));
1904 free_and_replace(line
[start_array
[j
-1]], d
);
1912 fputs(line
[j
], stdout
);
1913 j
+= utf8_console_width(line
[j
]);
1922 for (j
= 0; j
< c
; j
++)
1929 static bool context_changed(const Context
*context
) {
1932 LIST_FOREACH(partitions
, p
, context
->partitions
) {
1936 if (p
->allocated_to_area
)
1939 if (p
->new_size
!= p
->current_size
)
1946 static int context_wipe_partition(Context
*context
, Partition
*p
) {
1947 _cleanup_(blkid_free_probep
) blkid_probe probe
= NULL
;
1952 assert(!PARTITION_EXISTS(p
)); /* Safety check: never wipe existing partitions */
1954 probe
= blkid_new_probe();
1958 assert(p
->offset
!= UINT64_MAX
);
1959 assert(p
->new_size
!= UINT64_MAX
);
1962 r
= blkid_probe_set_device(probe
, fdisk_get_devfd(context
->fdisk_context
), p
->offset
, p
->new_size
);
1964 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to allocate device probe for partition %" PRIu64
".", p
->partno
);
1967 if (blkid_probe_enable_superblocks(probe
, true) < 0 ||
1968 blkid_probe_set_superblocks_flags(probe
, BLKID_SUBLKS_MAGIC
|BLKID_SUBLKS_BADCSUM
) < 0 ||
1969 blkid_probe_enable_partitions(probe
, true) < 0 ||
1970 blkid_probe_set_partitions_flags(probe
, BLKID_PARTS_MAGIC
) < 0)
1971 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to enable superblock and partition probing for partition %" PRIu64
".", p
->partno
);
1975 r
= blkid_do_probe(probe
);
1977 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe for file systems.");
1982 if (blkid_do_wipe(probe
, false) < 0)
1983 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to wipe file system signature.");
1986 log_info("Successfully wiped file system signatures from partition %" PRIu64
".", p
->partno
);
1990 static int context_discard_range(Context
*context
, uint64_t offset
, uint64_t size
) {
1995 assert(offset
!= UINT64_MAX
);
1996 assert(size
!= UINT64_MAX
);
2001 assert_se((fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
2003 if (fstat(fd
, &st
) < 0)
2006 if (S_ISREG(st
.st_mode
)) {
2007 if (fallocate(fd
, FALLOC_FL_PUNCH_HOLE
|FALLOC_FL_KEEP_SIZE
, offset
, size
) < 0) {
2008 if (ERRNO_IS_NOT_SUPPORTED(errno
))
2017 if (S_ISBLK(st
.st_mode
)) {
2018 uint64_t range
[2], end
;
2020 range
[0] = round_up_size(offset
, 512);
2022 end
= offset
+ size
;
2023 if (end
<= range
[0])
2026 range
[1] = round_down_size(end
- range
[0], 512);
2030 if (ioctl(fd
, BLKDISCARD
, range
) < 0) {
2031 if (ERRNO_IS_NOT_SUPPORTED(errno
))
2043 static int context_discard_partition(Context
*context
, Partition
*p
) {
2049 assert(p
->offset
!= UINT64_MAX
);
2050 assert(p
->new_size
!= UINT64_MAX
);
2051 assert(!PARTITION_EXISTS(p
)); /* Safety check: never discard existing partitions */
2056 r
= context_discard_range(context
, p
->offset
, p
->new_size
);
2057 if (r
== -EOPNOTSUPP
) {
2058 log_info("Storage does not support discarding, not discarding data in new partition %" PRIu64
".", p
->partno
);
2062 log_info("Partition %" PRIu64
" too short for discard, skipping.", p
->partno
);
2066 return log_error_errno(r
, "Failed to discard data for new partition %" PRIu64
".", p
->partno
);
2068 log_info("Successfully discarded data from partition %" PRIu64
".", p
->partno
);
2072 static int context_discard_gap_after(Context
*context
, Partition
*p
) {
2073 uint64_t gap
, next
= UINT64_MAX
;
2078 assert(!p
|| (p
->offset
!= UINT64_MAX
&& p
->new_size
!= UINT64_MAX
));
2081 gap
= p
->offset
+ p
->new_size
;
2083 gap
= context
->start
;
2085 LIST_FOREACH(partitions
, q
, context
->partitions
) {
2089 assert(q
->offset
!= UINT64_MAX
);
2090 assert(q
->new_size
!= UINT64_MAX
);
2092 if (q
->offset
< gap
)
2095 if (next
== UINT64_MAX
|| q
->offset
< next
)
2099 if (next
== UINT64_MAX
) {
2100 next
= context
->end
;
2102 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition end beyond disk end.");
2105 assert(next
>= gap
);
2106 r
= context_discard_range(context
, gap
, next
- gap
);
2107 if (r
== -EOPNOTSUPP
) {
2109 log_info("Storage does not support discarding, not discarding gap after partition %" PRIu64
".", p
->partno
);
2111 log_info("Storage does not support discarding, not discarding gap at beginning of disk.");
2114 if (r
== 0) /* Too short */
2118 return log_error_errno(r
, "Failed to discard gap after partition %" PRIu64
".", p
->partno
);
2120 return log_error_errno(r
, "Failed to discard gap at beginning of disk.");
2124 log_info("Successfully discarded gap after partition %" PRIu64
".", p
->partno
);
2126 log_info("Successfully discarded gap at beginning of disk.");
2131 static int context_wipe_and_discard(Context
*context
, bool from_scratch
) {
2137 /* Wipe and discard the contents of all partitions we are about to create. We skip the discarding if
2138 * we were supposed to start from scratch anyway, as in that case we just discard the whole block
2139 * device in one go early on. */
2141 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2143 if (!p
->allocated_to_area
)
2146 if (!from_scratch
) {
2147 r
= context_discard_partition(context
, p
);
2152 r
= context_wipe_partition(context
, p
);
2156 if (!from_scratch
) {
2157 r
= context_discard_gap_after(context
, p
);
2163 if (!from_scratch
) {
2164 r
= context_discard_gap_after(context
, NULL
);
2172 static int context_copy_blocks(Context
*context
) {
2178 /* Copy in file systems on the block level */
2180 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2181 char buf
[FORMAT_BYTES_MAX
];
2183 if (p
->copy_blocks_fd
< 0)
2189 if (PARTITION_EXISTS(p
)) /* Never copy over existing partitions */
2192 assert(p
->new_size
!= UINT64_MAX
);
2193 assert(p
->copy_blocks_size
!= UINT64_MAX
);
2194 assert(p
->new_size
>= p
->copy_blocks_size
);
2197 assert_se((fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
2199 if (lseek(fd
, p
->offset
, SEEK_SET
) == (off_t
) -1)
2200 return log_error_errno(errno
, "Failed to seek to partition offset: %m");
2202 log_info("Copying in '%s' (%s) on block level into partition %" PRIu64
".", p
->copy_blocks_path
, format_bytes(buf
, sizeof(buf
), p
->copy_blocks_size
), p
->partno
);
2204 r
= copy_bytes_full(p
->copy_blocks_fd
, fd
, p
->copy_blocks_size
, 0, NULL
, NULL
, NULL
, NULL
);
2206 return log_error_errno(r
, "Failed to copy in data from '%s': %m", p
->copy_blocks_path
);
2208 log_info("Copying in of '%s' on block level completed.", p
->copy_blocks_path
);
2214 static int partition_acquire_uuid(Context
*context
, Partition
*p
, sd_id128_t
*ret
) {
2216 sd_id128_t type_uuid
;
2218 } _packed_ plaintext
= {};
2220 unsigned char md
[SHA256_DIGEST_LENGTH
];
2232 /* Calculate a good UUID for the indicated partition. We want a certain degree of reproducibility,
2233 * hence we won't generate the UUIDs randomly. Instead we use a cryptographic hash (precisely:
2234 * HMAC-SHA256) to derive them from a single seed. The seed is generally the machine ID of the
2235 * installation we are processing, but if random behaviour is desired can be random, too. We use the
2236 * seed value as key for the HMAC (since the machine ID is something we generally don't want to leak)
2237 * and the partition type as plaintext. The partition type is suffixed with a counter (only for the
2238 * second and later partition of the same type) if we have more than one partition of the same
2239 * time. Or in other words:
2242 * SEED := /etc/machine-id
2244 * If first partition instance of type TYPE_UUID:
2245 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID)
2247 * For all later partition instances of type TYPE_UUID with INSTANCE being the LE64 encoded instance number:
2248 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID || INSTANCE)
2251 LIST_FOREACH(partitions
, q
, context
->partitions
) {
2255 if (!sd_id128_equal(p
->type_uuid
, q
->type_uuid
))
2261 plaintext
.type_uuid
= p
->type_uuid
;
2262 plaintext
.counter
= htole64(k
);
2264 if (!HMAC(EVP_sha256(),
2265 &context
->seed
, sizeof(context
->seed
),
2266 (const unsigned char*) &plaintext
, k
== 0 ? sizeof(sd_id128_t
) : sizeof(plaintext
),
2268 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE
), "SHA256 calculation failed.");
2270 /* Take the first half, mark it as v4 UUID */
2271 assert_cc(sizeof(result
.md
) == sizeof(result
.id
) * 2);
2272 result
.id
= id128_make_v4_uuid(result
.id
);
2274 /* Ensure this partition UUID is actually unique, and there's no remaining partition from an earlier run? */
2275 LIST_FOREACH(partitions
, q
, context
->partitions
) {
2279 if (sd_id128_equal(q
->current_uuid
, result
.id
) ||
2280 sd_id128_equal(q
->new_uuid
, result
.id
)) {
2281 log_warning("Partition UUID calculated from seed for partition %" PRIu64
" exists already, reverting to randomized UUID.", p
->partno
);
2283 r
= sd_id128_randomize(&result
.id
);
2285 return log_error_errno(r
, "Failed to generate randomized UUID: %m");
2295 static int partition_acquire_label(Context
*context
, Partition
*p
, char **ret
) {
2296 _cleanup_free_
char *label
= NULL
;
2304 prefix
= gpt_partition_type_uuid_to_string(p
->type_uuid
);
2309 const char *ll
= label
?: prefix
;
2313 LIST_FOREACH(partitions
, q
, context
->partitions
) {
2317 if (streq_ptr(ll
, q
->current_label
) ||
2318 streq_ptr(ll
, q
->new_label
)) {
2327 label
= mfree(label
);
2330 if (asprintf(&label
, "%s-%u", prefix
, ++k
) < 0)
2335 label
= strdup(prefix
);
2340 *ret
= TAKE_PTR(label
);
2344 static int context_acquire_partition_uuids_and_labels(Context
*context
) {
2350 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2351 /* Never touch foreign partitions */
2352 if (PARTITION_IS_FOREIGN(p
)) {
2353 p
->new_uuid
= p
->current_uuid
;
2355 if (p
->current_label
) {
2357 p
->new_label
= strdup(p
->current_label
);
2365 if (!sd_id128_is_null(p
->current_uuid
))
2366 p
->new_uuid
= p
->current_uuid
; /* Never change initialized UUIDs */
2367 else if (sd_id128_is_null(p
->new_uuid
)) {
2368 /* Not explicitly set by user! */
2369 r
= partition_acquire_uuid(context
, p
, &p
->new_uuid
);
2374 if (!isempty(p
->current_label
)) {
2376 p
->new_label
= strdup(p
->current_label
); /* never change initialized labels */
2379 } else if (!p
->new_label
) {
2380 /* Not explicitly set by user! */
2382 r
= partition_acquire_label(context
, p
, &p
->new_label
);
2391 static int device_kernel_partitions_supported(int fd
) {
2392 struct loop_info64 info
;
2397 if (fstat(fd
, &st
) < 0)
2398 return log_error_errno(fd
, "Failed to fstat() image file: %m");
2399 if (!S_ISBLK(st
.st_mode
))
2400 return -ENOTBLK
; /* we do not log in this one special case about errors */
2402 if (ioctl(fd
, LOOP_GET_STATUS64
, &info
) < 0) {
2404 if (ERRNO_IS_NOT_SUPPORTED(errno
) || errno
== EINVAL
)
2405 return true; /* not a loopback device, let's assume partition are supported */
2407 return log_error_errno(fd
, "Failed to issue LOOP_GET_STATUS64 on block device: %m");
2410 #if HAVE_VALGRIND_MEMCHECK_H
2411 /* Valgrind currently doesn't know LOOP_GET_STATUS64. Remove this once it does */
2412 VALGRIND_MAKE_MEM_DEFINED(&info
, sizeof(info
));
2415 return FLAGS_SET(info
.lo_flags
, LO_FLAGS_PARTSCAN
);
2418 static int context_write_partition_table(
2421 bool from_scratch
) {
2423 _cleanup_(fdisk_unref_tablep
) struct fdisk_table
*original_table
= NULL
;
2429 if (arg_pretty
> 0 ||
2430 (arg_pretty
< 0 && isatty(STDOUT_FILENO
) > 0) || arg_json
) {
2432 (void) context_dump_partitions(context
, node
);
2437 (void) context_dump_partition_bar(context
, node
);
2442 if (!from_scratch
&& !context_changed(context
)) {
2443 log_info("No changes.");
2448 log_notice("Refusing to repartition, please re-run with --dry-run=no.");
2452 log_info("Applying changes.");
2455 r
= context_discard_range(context
, 0, context
->total
);
2456 if (r
== -EOPNOTSUPP
)
2457 log_info("Storage does not support discarding, not discarding entire block device data.");
2459 return log_error_errno(r
, "Failed to discard entire block device: %m");
2461 log_info("Discarded entire block device.");
2464 r
= fdisk_get_partitions(context
->fdisk_context
, &original_table
);
2466 return log_error_errno(r
, "Failed to acquire partition table: %m");
2468 /* Wipe fs signatures and discard sectors where the new partitions are going to be placed and in the
2469 * gaps between partitions, just to be sure. */
2470 r
= context_wipe_and_discard(context
, from_scratch
);
2474 r
= context_copy_blocks(context
);
2478 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2482 assert(p
->new_size
!= UINT64_MAX
);
2483 assert(p
->offset
!= UINT64_MAX
);
2484 assert(p
->partno
!= UINT64_MAX
);
2486 if (PARTITION_EXISTS(p
)) {
2487 bool changed
= false;
2489 assert(p
->current_partition
);
2491 if (p
->new_size
!= p
->current_size
) {
2492 assert(p
->new_size
>= p
->current_size
);
2493 assert(p
->new_size
% 512 == 0);
2495 r
= fdisk_partition_size_explicit(p
->current_partition
, true);
2497 return log_error_errno(r
, "Failed to enable explicit sizing: %m");
2499 r
= fdisk_partition_set_size(p
->current_partition
, p
->new_size
/ 512);
2501 return log_error_errno(r
, "Failed to grow partition: %m");
2503 log_info("Growing existing partition %" PRIu64
".", p
->partno
);
2507 if (!sd_id128_equal(p
->new_uuid
, p
->current_uuid
)) {
2508 char buf
[ID128_UUID_STRING_MAX
];
2510 assert(!sd_id128_is_null(p
->new_uuid
));
2512 r
= fdisk_partition_set_uuid(p
->current_partition
, id128_to_uuid_string(p
->new_uuid
, buf
));
2514 return log_error_errno(r
, "Failed to set partition UUID: %m");
2516 log_info("Initializing UUID of existing partition %" PRIu64
".", p
->partno
);
2520 if (!streq_ptr(p
->new_label
, p
->current_label
)) {
2521 assert(!isempty(p
->new_label
));
2523 r
= fdisk_partition_set_name(p
->current_partition
, p
->new_label
);
2525 return log_error_errno(r
, "Failed to set partition label: %m");
2527 log_info("Setting partition label of existing partition %" PRIu64
".", p
->partno
);
2532 assert(!PARTITION_IS_FOREIGN(p
)); /* never touch foreign partitions */
2534 r
= fdisk_set_partition(context
->fdisk_context
, p
->partno
, p
->current_partition
);
2536 return log_error_errno(r
, "Failed to update partition: %m");
2539 _cleanup_(fdisk_unref_partitionp
) struct fdisk_partition
*q
= NULL
;
2540 _cleanup_(fdisk_unref_parttypep
) struct fdisk_parttype
*t
= NULL
;
2541 char ids
[ID128_UUID_STRING_MAX
];
2543 assert(!p
->new_partition
);
2544 assert(p
->offset
% 512 == 0);
2545 assert(p
->new_size
% 512 == 0);
2546 assert(!sd_id128_is_null(p
->new_uuid
));
2547 assert(!isempty(p
->new_label
));
2549 t
= fdisk_new_parttype();
2553 r
= fdisk_parttype_set_typestr(t
, id128_to_uuid_string(p
->type_uuid
, ids
));
2555 return log_error_errno(r
, "Failed to initialize partition type: %m");
2557 q
= fdisk_new_partition();
2561 r
= fdisk_partition_set_type(q
, t
);
2563 return log_error_errno(r
, "Failed to set partition type: %m");
2565 r
= fdisk_partition_size_explicit(q
, true);
2567 return log_error_errno(r
, "Failed to enable explicit sizing: %m");
2569 r
= fdisk_partition_set_start(q
, p
->offset
/ 512);
2571 return log_error_errno(r
, "Failed to position partition: %m");
2573 r
= fdisk_partition_set_size(q
, p
->new_size
/ 512);
2575 return log_error_errno(r
, "Failed to grow partition: %m");
2577 r
= fdisk_partition_set_partno(q
, p
->partno
);
2579 return log_error_errno(r
, "Failed to set partition number: %m");
2581 r
= fdisk_partition_set_uuid(q
, id128_to_uuid_string(p
->new_uuid
, ids
));
2583 return log_error_errno(r
, "Failed to set partition UUID: %m");
2585 r
= fdisk_partition_set_name(q
, p
->new_label
);
2587 return log_error_errno(r
, "Failed to set partition label: %m");
2589 log_info("Creating new partition %" PRIu64
".", p
->partno
);
2591 r
= fdisk_add_partition(context
->fdisk_context
, q
, NULL
);
2593 return log_error_errno(r
, "Failed to add partition: %m");
2595 assert(!p
->new_partition
);
2596 p
->new_partition
= TAKE_PTR(q
);
2600 log_info("Writing new partition table.");
2602 r
= fdisk_write_disklabel(context
->fdisk_context
);
2604 return log_error_errno(r
, "Failed to write partition table: %m");
2606 capable
= device_kernel_partitions_supported(fdisk_get_devfd(context
->fdisk_context
));
2607 if (capable
== -ENOTBLK
)
2608 log_debug("Not telling kernel to reread partition table, since we are not operating on a block device.");
2609 else if (capable
< 0)
2611 else if (capable
> 0) {
2612 log_info("Telling kernel to reread partition table.");
2615 r
= fdisk_reread_partition_table(context
->fdisk_context
);
2617 r
= fdisk_reread_changes(context
->fdisk_context
, original_table
);
2619 return log_error_errno(r
, "Failed to reread partition table: %m");
2621 log_notice("Not telling kernel to reread partition table, because selected image does not support kernel partition block devices.");
2623 log_info("All done.");
2628 static int context_read_seed(Context
*context
, const char *root
) {
2633 if (!sd_id128_is_null(context
->seed
))
2636 if (!arg_randomize
) {
2637 _cleanup_close_
int fd
= -1;
2639 fd
= chase_symlinks_and_open("/etc/machine-id", root
, CHASE_PREFIX_ROOT
, O_RDONLY
|O_CLOEXEC
, NULL
);
2641 log_info("No machine ID set, using randomized partition UUIDs.");
2643 return log_error_errno(fd
, "Failed to determine machine ID of image: %m");
2645 r
= id128_read_fd(fd
, ID128_PLAIN
, &context
->seed
);
2646 if (r
== -ENOMEDIUM
)
2647 log_info("No machine ID set, using randomized partition UUIDs.");
2649 return log_error_errno(r
, "Failed to parse machine ID of image: %m");
2655 r
= sd_id128_randomize(&context
->seed
);
2657 return log_error_errno(r
, "Failed to generate randomized seed: %m");
2662 static int context_factory_reset(Context
*context
, bool from_scratch
) {
2669 if (arg_factory_reset
<= 0)
2672 if (from_scratch
) /* Nothing to reset if we start from scratch */
2676 log_notice("Refusing to factory reset, please re-run with --dry-run=no.");
2680 log_info("Applying factory reset.");
2682 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2684 if (!p
->factory_reset
|| !PARTITION_EXISTS(p
))
2687 assert(p
->partno
!= UINT64_MAX
);
2689 log_info("Removing partition %" PRIu64
" for factory reset.", p
->partno
);
2691 r
= fdisk_delete_partition(context
->fdisk_context
, p
->partno
);
2693 return log_error_errno(r
, "Failed to remove partition %" PRIu64
": %m", p
->partno
);
2699 log_info("Factory reset requested, but no partitions to delete found.");
2703 r
= fdisk_write_disklabel(context
->fdisk_context
);
2705 return log_error_errno(r
, "Failed to write disk label: %m");
2707 log_info("Successfully deleted %zu partitions.", n
);
2711 static int context_can_factory_reset(Context
*context
) {
2716 LIST_FOREACH(partitions
, p
, context
->partitions
)
2717 if (p
->factory_reset
&& PARTITION_EXISTS(p
))
2723 static int context_open_copy_block_paths(Context
*context
) {
2729 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2730 _cleanup_close_
int source_fd
= -1;
2734 assert(p
->copy_blocks_fd
< 0);
2735 assert(p
->copy_blocks_size
== UINT64_MAX
);
2737 if (PARTITION_EXISTS(p
)) /* Never copy over partitions that already exist! */
2740 if (!p
->copy_blocks_path
)
2743 source_fd
= open(p
->copy_blocks_path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2745 return log_error_errno(errno
, "Failed to open block copy file '%s': %m", p
->copy_blocks_path
);
2747 if (fstat(source_fd
, &st
) < 0)
2748 return log_error_errno(errno
, "Failed to stat block copy file '%s': %m", p
->copy_blocks_path
);
2750 if (S_ISDIR(st
.st_mode
)) {
2751 _cleanup_free_
char *bdev
= NULL
;
2753 /* If the file is a directory, automatically find the backing block device */
2755 if (major(st
.st_dev
) != 0)
2756 r
= device_path_make_major_minor(S_IFBLK
, st
.st_dev
, &bdev
);
2760 /* Special support for btrfs */
2762 r
= btrfs_get_block_device_fd(source_fd
, &devt
);
2764 return log_error_errno(r
, "Unable to determine backing block device of '%s': %m", p
->copy_blocks_path
);
2766 r
= device_path_make_major_minor(S_IFBLK
, devt
, &bdev
);
2769 return log_error_errno(r
, "Failed to determine block device path for block device backing '%s': %m", p
->copy_blocks_path
);
2771 safe_close(source_fd
);
2773 source_fd
= open(bdev
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
2775 return log_error_errno(errno
, "Failed to open block device '%s': %m", bdev
);
2777 if (fstat(source_fd
, &st
) < 0)
2778 return log_error_errno(errno
, "Failed to stat block device '%s': %m", bdev
);
2780 if (!S_ISBLK(st
.st_mode
))
2781 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK
), "Block device '%s' is not actually a block device, refusing.", bdev
);
2784 if (S_ISREG(st
.st_mode
))
2786 else if (S_ISBLK(st
.st_mode
)) {
2787 if (ioctl(source_fd
, BLKGETSIZE64
, &size
) != 0)
2788 return log_error_errno(errno
, "Failed to determine size of block device to copy from: %m");
2790 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified path to copy blocks from '%s' is not a regular file, block device or directory, refusing: %m", p
->copy_blocks_path
);
2793 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "File to copy bytes from '%s' has zero size, refusing.", p
->copy_blocks_path
);
2794 if (size
% 512 != 0)
2795 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "File to copy bytes from '%s' has size that is not multiple of 512, refusing.", p
->copy_blocks_path
);
2797 p
->copy_blocks_fd
= TAKE_FD(source_fd
);
2798 p
->copy_blocks_size
= size
;
2804 static int help(void) {
2805 _cleanup_free_
char *link
= NULL
;
2808 r
= terminal_urlify_man("systemd-repart", "1", &link
);
2812 printf("%s [OPTIONS...] [DEVICE]\n"
2813 "\n%sGrow and add partitions to partition table.%s\n\n"
2814 " -h --help Show this help\n"
2815 " --version Show package version\n"
2816 " --dry-run=BOOL Whether to run dry-run operation\n"
2817 " --empty=MODE One of refuse, allow, require, force, create; controls\n"
2818 " how to handle empty disks lacking partition tables\n"
2819 " --discard=BOOL Whether to discard backing blocks for new partitions\n"
2820 " --pretty=BOOL Whether to show pretty summary before doing changes\n"
2821 " --factory-reset=BOOL Whether to remove data partitions before recreating\n"
2823 " --can-factory-reset Test whether factory reset is defined\n"
2824 " --root=PATH Operate relative to root path\n"
2825 " --definitions=DIR Find partitions in specified directory\n"
2826 " --seed=UUID 128bit seed UUID to derive all UUIDs from\n"
2827 " --size=BYTES Grow loopback file to specified size\n"
2828 " --json=pretty|short|off\n"
2829 " Generate json output\n"
2830 "\nSee the %s for details.\n"
2831 , program_invocation_short_name
2832 , ansi_highlight(), ansi_normal()
2839 static int parse_argv(int argc
, char *argv
[]) {
2842 ARG_VERSION
= 0x100,
2847 ARG_CAN_FACTORY_RESET
,
2856 static const struct option options
[] = {
2857 { "help", no_argument
, NULL
, 'h' },
2858 { "version", no_argument
, NULL
, ARG_VERSION
},
2859 { "dry-run", required_argument
, NULL
, ARG_DRY_RUN
},
2860 { "empty", required_argument
, NULL
, ARG_EMPTY
},
2861 { "discard", required_argument
, NULL
, ARG_DISCARD
},
2862 { "factory-reset", required_argument
, NULL
, ARG_FACTORY_RESET
},
2863 { "can-factory-reset", no_argument
, NULL
, ARG_CAN_FACTORY_RESET
},
2864 { "root", required_argument
, NULL
, ARG_ROOT
},
2865 { "seed", required_argument
, NULL
, ARG_SEED
},
2866 { "pretty", required_argument
, NULL
, ARG_PRETTY
},
2867 { "definitions", required_argument
, NULL
, ARG_DEFINITIONS
},
2868 { "size", required_argument
, NULL
, ARG_SIZE
},
2869 { "json", required_argument
, NULL
, ARG_JSON
},
2873 int c
, r
, dry_run
= -1;
2878 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
2889 r
= parse_boolean(optarg
);
2891 return log_error_errno(r
, "Failed to parse --dry-run= parameter: %s", optarg
);
2897 if (isempty(optarg
) || streq(optarg
, "refuse"))
2898 arg_empty
= EMPTY_REFUSE
;
2899 else if (streq(optarg
, "allow"))
2900 arg_empty
= EMPTY_ALLOW
;
2901 else if (streq(optarg
, "require"))
2902 arg_empty
= EMPTY_REQUIRE
;
2903 else if (streq(optarg
, "force"))
2904 arg_empty
= EMPTY_FORCE
;
2905 else if (streq(optarg
, "create")) {
2906 arg_empty
= EMPTY_CREATE
;
2909 dry_run
= false; /* Imply --dry-run=no if we create the loopback file
2910 * anew. After all we cannot really break anyone's
2911 * partition tables that way. */
2913 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
2914 "Failed to parse --empty= parameter: %s", optarg
);
2918 r
= parse_boolean(optarg
);
2920 return log_error_errno(r
, "Failed to parse --discard= parameter: %s", optarg
);
2925 case ARG_FACTORY_RESET
:
2926 r
= parse_boolean(optarg
);
2928 return log_error_errno(r
, "Failed to parse --factory-reset= parameter: %s", optarg
);
2930 arg_factory_reset
= r
;
2933 case ARG_CAN_FACTORY_RESET
:
2934 arg_can_factory_reset
= true;
2938 r
= parse_path_argument_and_warn(optarg
, false, &arg_root
);
2944 if (isempty(optarg
)) {
2945 arg_seed
= SD_ID128_NULL
;
2946 arg_randomize
= false;
2947 } else if (streq(optarg
, "random"))
2948 arg_randomize
= true;
2950 r
= sd_id128_from_string(optarg
, &arg_seed
);
2952 return log_error_errno(r
, "Failed to parse seed: %s", optarg
);
2954 arg_randomize
= false;
2960 r
= parse_boolean(optarg
);
2962 return log_error_errno(r
, "Failed to parse --pretty= parameter: %s", optarg
);
2967 case ARG_DEFINITIONS
:
2968 r
= parse_path_argument_and_warn(optarg
, false, &arg_definitions
);
2974 uint64_t parsed
, rounded
;
2976 r
= parse_size(optarg
, 1024, &parsed
);
2978 return log_error_errno(r
, "Failed to parse --size= parameter: %s", optarg
);
2980 rounded
= round_up_size(parsed
, 4096);
2982 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Specified image size too small, refusing.");
2983 if (rounded
== UINT64_MAX
)
2984 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Specified image size too large, refusing.");
2986 if (rounded
!= parsed
)
2987 log_warning("Specified size is not a multiple of 4096, rounding up automatically. (%" PRIu64
" → %" PRIu64
")",
2994 if (streq(optarg
, "pretty")) {
2996 arg_json_format_flags
= JSON_FORMAT_PRETTY
|JSON_FORMAT_COLOR_AUTO
;
2997 } else if (streq(optarg
, "short")) {
2999 arg_json_format_flags
= JSON_FORMAT_NEWLINE
;
3000 } else if (streq(optarg
, "off")) {
3002 arg_json_format_flags
= 0;
3003 } else if (streq(optarg
, "help")) {
3009 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown argument to --json=: %s", optarg
);
3018 assert_not_reached("Unhandled option");
3021 if (argc
- optind
> 1)
3022 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3023 "Expected at most one argument, the path to the block device.");
3025 if (arg_factory_reset
> 0 && IN_SET(arg_empty
, EMPTY_FORCE
, EMPTY_REQUIRE
, EMPTY_CREATE
))
3026 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3027 "Combination of --factory-reset=yes and --empty=force/--empty=require/--empty=create is invalid.");
3029 if (arg_can_factory_reset
)
3030 arg_dry_run
= true; /* When --can-factory-reset is specified we don't make changes, hence
3031 * non-dry-run mode makes no sense. Thus, imply dry run mode so that we
3032 * open things strictly read-only. */
3033 else if (dry_run
>= 0)
3034 arg_dry_run
= dry_run
;
3036 if (arg_empty
== EMPTY_CREATE
&& arg_size
== UINT64_MAX
)
3037 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3038 "If --empty=create is specified, --size= must be specified, too.");
3040 arg_node
= argc
> optind
? argv
[optind
] : NULL
;
3042 if (IN_SET(arg_empty
, EMPTY_FORCE
, EMPTY_REQUIRE
, EMPTY_CREATE
) && !arg_node
)
3043 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3044 "A path to a device node or loopback file must be specified when --empty=force, --empty=require or --empty=create are used.");
3049 static int parse_proc_cmdline_factory_reset(void) {
3053 if (arg_factory_reset
>= 0) /* Never override what is specified on the process command line */
3056 if (!in_initrd()) /* Never honour kernel command line factory reset request outside of the initrd */
3059 r
= proc_cmdline_get_bool("systemd.factory_reset", &b
);
3061 return log_error_errno(r
, "Failed to parse systemd.factory_reset kernel command line argument: %m");
3063 arg_factory_reset
= b
;
3066 log_notice("Honouring factory reset requested via kernel command line.");
3072 static int parse_efi_variable_factory_reset(void) {
3073 _cleanup_free_
char *value
= NULL
;
3076 if (arg_factory_reset
>= 0) /* Never override what is specified on the process command line */
3079 if (!in_initrd()) /* Never honour EFI variable factory reset request outside of the initrd */
3082 r
= efi_get_variable_string(EFI_VENDOR_SYSTEMD
, "FactoryReset", &value
);
3083 if (r
== -ENOENT
|| ERRNO_IS_NOT_SUPPORTED(r
))
3086 return log_error_errno(r
, "Failed to read EFI variable FactoryReset: %m");
3088 r
= parse_boolean(value
);
3090 return log_error_errno(r
, "Failed to parse EFI variable FactoryReset: %m");
3092 arg_factory_reset
= r
;
3094 log_notice("Honouring factory reset requested via EFI variable FactoryReset: %m");
3099 static int remove_efi_variable_factory_reset(void) {
3102 r
= efi_set_variable(EFI_VENDOR_SYSTEMD
, "FactoryReset", NULL
, 0);
3103 if (r
== -ENOENT
|| ERRNO_IS_NOT_SUPPORTED(r
))
3106 return log_error_errno(r
, "Failed to remove EFI variable FactoryReset: %m");
3108 log_info("Successfully unset EFI variable FactoryReset.");
3112 static int acquire_root_devno(const char *p
, int mode
, char **ret
, int *ret_fd
) {
3113 _cleanup_close_
int fd
= -1;
3115 dev_t devno
, fd_devno
= (mode_t
) -1;
3126 if (fstat(fd
, &st
) < 0)
3129 if (S_ISREG(st
.st_mode
)) {
3137 *ret_fd
= TAKE_FD(fd
);
3142 if (S_ISBLK(st
.st_mode
))
3143 fd_devno
= devno
= st
.st_rdev
;
3144 else if (S_ISDIR(st
.st_mode
)) {
3147 if (major(devno
) == 0) {
3148 r
= btrfs_get_block_device_fd(fd
, &devno
);
3149 if (r
== -ENOTTY
) /* not btrfs */
3157 /* From dm-crypt to backing partition */
3158 r
= block_get_originating(devno
, &devno
);
3160 log_debug_errno(r
, "Failed to find underlying block device for '%s', ignoring: %m", p
);
3162 /* From partition to whole disk containing it */
3163 r
= block_get_whole_disk(devno
, &devno
);
3165 log_debug_errno(r
, "Failed to find whole disk block device for '%s', ignoring: %m", p
);
3167 r
= device_path_make_canonical(S_IFBLK
, devno
, ret
);
3169 return log_debug_errno(r
, "Failed to determine canonical path for '%s': %m", p
);
3171 /* Only if we still lock at the same block device we can reuse the fd. Otherwise return an
3172 * invalidated fd. */
3173 *ret_fd
= fd_devno
!= (mode_t
) -1 && fd_devno
== devno
? TAKE_FD(fd
) : -1;
3177 static int find_root(char **ret
, int *ret_fd
) {
3185 if (arg_empty
== EMPTY_CREATE
) {
3186 _cleanup_close_
int fd
= -1;
3187 _cleanup_free_
char *s
= NULL
;
3189 s
= strdup(arg_node
);
3193 fd
= open(arg_node
, O_RDONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
|O_NOFOLLOW
, 0777);
3195 return log_error_errno(errno
, "Failed to create '%s': %m", arg_node
);
3198 *ret_fd
= TAKE_FD(fd
);
3202 r
= acquire_root_devno(arg_node
, O_RDONLY
|O_CLOEXEC
, ret
, ret_fd
);
3204 return log_error_errno(r
, "Failed to determine backing device of %s: %m", arg_node
);
3209 assert(IN_SET(arg_empty
, EMPTY_REFUSE
, EMPTY_ALLOW
));
3211 /* Let's search for the root device. We look for two cases here: first in /, and then in /usr. The
3212 * latter we check for cases where / is a tmpfs and only /usr is an actual persistent block device
3213 * (think: volatile setups) */
3215 FOREACH_STRING(t
, "/", "/usr") {
3216 _cleanup_free_
char *j
= NULL
;
3220 j
= path_join("/sysroot", t
);
3228 r
= acquire_root_devno(p
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
, ret
, ret_fd
);
3231 return log_error_errno(r
, "Failed to determine backing device of %s: %m", p
);
3236 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "Failed to discover root block device.");
3239 static int resize_backing_fd(const char *node
, int *fd
) {
3240 char buf1
[FORMAT_BYTES_MAX
], buf2
[FORMAT_BYTES_MAX
];
3241 _cleanup_close_
int writable_fd
= -1;
3248 if (arg_size
== UINT64_MAX
) /* Nothing to do */
3252 /* Open the file if we haven't opened it yet. Note that we open it read-only here, just to
3253 * keep a reference to the file we can pass around. */
3254 *fd
= open(node
, O_RDONLY
|O_CLOEXEC
);
3256 return log_error_errno(errno
, "Failed to open '%s' in order to adjust size: %m", node
);
3259 if (fstat(*fd
, &st
) < 0)
3260 return log_error_errno(errno
, "Failed to stat '%s': %m", node
);
3262 r
= stat_verify_regular(&st
);
3264 return log_error_errno(r
, "Specified path '%s' is not a regular file, cannot resize: %m", node
);
3266 assert_se(format_bytes(buf1
, sizeof(buf1
), st
.st_size
));
3267 assert_se(format_bytes(buf2
, sizeof(buf2
), arg_size
));
3269 if ((uint64_t) st
.st_size
>= arg_size
) {
3270 log_info("File '%s' already is of requested size or larger, not growing. (%s >= %s)", node
, buf1
, buf2
);
3274 /* The file descriptor is read-only. In order to grow the file we need to have a writable fd. We
3275 * reopen the file for that temporarily. We keep the writable fd only open for this operation though,
3276 * as fdisk can't accept it anyway. */
3278 writable_fd
= fd_reopen(*fd
, O_WRONLY
|O_CLOEXEC
);
3279 if (writable_fd
< 0)
3280 return log_error_errno(writable_fd
, "Failed to reopen backing file '%s' writable: %m", node
);
3283 if (fallocate(writable_fd
, 0, 0, arg_size
) < 0) {
3284 if (!ERRNO_IS_NOT_SUPPORTED(errno
))
3285 return log_error_errno(errno
, "Failed to grow '%s' from %s to %s by allocation: %m",
3288 /* Fallback to truncation, if fallocate() is not supported. */
3289 log_debug("Backing file system does not support fallocate(), falling back to ftruncate().");
3291 if (st
.st_size
== 0) /* Likely regular file just created by us */
3292 log_info("Allocated %s for '%s'.", buf2
, node
);
3294 log_info("File '%s' grown from %s to %s by allocation.", node
, buf1
, buf2
);
3300 if (ftruncate(writable_fd
, arg_size
) < 0)
3301 return log_error_errno(errno
, "Failed to grow '%s' from %s to %s by truncation: %m",
3304 if (st
.st_size
== 0) /* Likely regular file just created by us */
3305 log_info("Sized '%s' to %s.", node
, buf2
);
3307 log_info("File '%s' grown from %s to %s by truncation.", node
, buf1
, buf2
);
3312 static int run(int argc
, char *argv
[]) {
3313 _cleanup_(context_freep
) Context
* context
= NULL
;
3314 _cleanup_free_
char *node
= NULL
;
3315 _cleanup_close_
int backing_fd
= -1;
3319 log_show_color(true);
3320 log_parse_environment();
3324 /* Default to operation on /sysroot when invoked in the initrd! */
3325 arg_root
= strdup("/sysroot");
3330 r
= parse_argv(argc
, argv
);
3334 r
= parse_proc_cmdline_factory_reset();
3338 r
= parse_efi_variable_factory_reset();
3342 context
= context_new(arg_seed
);
3346 r
= context_read_definitions(context
, arg_definitions
, arg_root
);
3350 if (context
->n_partitions
<= 0 && arg_empty
== EMPTY_REFUSE
) {
3351 log_info("Didn't find any partition definition files, nothing to do.");
3355 r
= find_root(&node
, &backing_fd
);
3359 if (arg_size
!= UINT64_MAX
) {
3360 r
= resize_backing_fd(node
, &backing_fd
);
3365 r
= context_load_partition_table(context
, node
, &backing_fd
);
3366 if (r
== -EHWPOISON
)
3367 return 77; /* Special return value which means "Not GPT, so not doing anything". This isn't
3368 * really an error when called at boot. */
3371 from_scratch
= r
> 0; /* Starting from scratch */
3373 if (arg_can_factory_reset
) {
3374 r
= context_can_factory_reset(context
);
3378 return EXIT_FAILURE
;
3383 r
= context_factory_reset(context
, from_scratch
);
3387 /* We actually did a factory reset! */
3388 r
= remove_efi_variable_factory_reset();
3392 /* Reload the reduced partition table */
3393 context_unload_partition_table(context
);
3394 r
= context_load_partition_table(context
, node
, &backing_fd
);
3400 (void) context_dump_partitions(context
, node
);
3404 r
= context_read_seed(context
, arg_root
);
3408 /* Open all files to copy blocks from now, since we want to take their size into consideration */
3409 r
= context_open_copy_block_paths(context
);
3413 /* First try to fit new partitions in, dropping by priority until it fits */
3415 if (context_allocate_partitions(context
))
3416 break; /* Success! */
3418 if (!context_drop_one_priority(context
))
3419 return log_error_errno(SYNTHETIC_ERRNO(ENOSPC
),
3420 "Can't fit requested partitions into free space, refusing.");
3423 /* Now assign free space according to the weight logic */
3424 r
= context_grow_partitions(context
);
3428 /* Now calculate where each partition gets placed */
3429 context_place_partitions(context
);
3431 /* Make sure each partition has a unique UUID and unique label */
3432 r
= context_acquire_partition_uuids_and_labels(context
);
3436 r
= context_write_partition_table(context
, node
, from_scratch
);
3443 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);