1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
27 #include "cryptsetup-util.h"
29 #include "dirent-util.h"
31 #include "errno-util.h"
34 #include "format-table.h"
35 #include "format-util.h"
38 #include "hexdecoct.h"
39 #include "id128-util.h"
42 #include "locale-util.h"
43 #include "loop-util.h"
44 #include "main-func.h"
46 #include "mkfs-util.h"
47 #include "mount-util.h"
48 #include "mountpoint-util.h"
49 #include "parse-argument.h"
50 #include "parse-util.h"
51 #include "path-util.h"
52 #include "pretty-print.h"
53 #include "proc-cmdline.h"
54 #include "process-util.h"
55 #include "random-util.h"
56 #include "resize-fs.h"
57 #include "sort-util.h"
58 #include "specifier.h"
59 #include "stat-util.h"
60 #include "stdio-util.h"
61 #include "string-table.h"
62 #include "string-util.h"
64 #include "terminal-util.h"
65 #include "tpm2-util.h"
66 #include "user-util.h"
69 /* If not configured otherwise use a minimal partition size of 10M */
70 #define DEFAULT_MIN_SIZE (10*1024*1024)
72 /* Hard lower limit for new partition sizes */
73 #define HARD_MIN_SIZE 4096
75 /* libfdisk takes off slightly more than 1M of the disk size when creating a GPT disk label */
76 #define GPT_METADATA_SIZE (1044*1024)
78 /* LUKS2 takes off 16M of the partition size with its metadata by default */
79 #define LUKS2_METADATA_SIZE (16*1024*1024)
81 /* Note: When growing and placing new partitions we always align to 4K sector size. It's how newer hard disks
82 * are designed, and if everything is aligned to that performance is best. And for older hard disks with 512B
83 * sector size devices were generally assumed to have an even number of sectors, hence at the worst we'll
84 * waste 3K per partition, which is probably fine. */
87 EMPTY_REFUSE
, /* refuse empty disks, never create a partition table */
88 EMPTY_ALLOW
, /* allow empty disks, create partition table if necessary */
89 EMPTY_REQUIRE
, /* require an empty disk, create a partition table */
90 EMPTY_FORCE
, /* make disk empty, erase everything, create a partition table always */
91 EMPTY_CREATE
, /* create disk as loopback file, create a partition table always */
92 } arg_empty
= EMPTY_REFUSE
;
94 static bool arg_dry_run
= true;
95 static const char *arg_node
= NULL
;
96 static char *arg_root
= NULL
;
97 static char *arg_image
= NULL
;
98 static char *arg_definitions
= NULL
;
99 static bool arg_discard
= true;
100 static bool arg_can_factory_reset
= false;
101 static int arg_factory_reset
= -1;
102 static sd_id128_t arg_seed
= SD_ID128_NULL
;
103 static bool arg_randomize
= false;
104 static int arg_pretty
= -1;
105 static uint64_t arg_size
= UINT64_MAX
;
106 static bool arg_size_auto
= false;
107 static JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
108 static PagerFlags arg_pager_flags
= 0;
109 static bool arg_legend
= true;
110 static void *arg_key
= NULL
;
111 static size_t arg_key_size
= 0;
112 static char *arg_tpm2_device
= NULL
;
113 static uint32_t arg_tpm2_pcr_mask
= UINT32_MAX
;
115 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
116 STATIC_DESTRUCTOR_REGISTER(arg_image
, freep
);
117 STATIC_DESTRUCTOR_REGISTER(arg_definitions
, freep
);
118 STATIC_DESTRUCTOR_REGISTER(arg_key
, erase_and_freep
);
119 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device
, freep
);
121 typedef struct Partition Partition
;
122 typedef struct FreeArea FreeArea
;
123 typedef struct Context Context
;
125 typedef enum EncryptMode
{
129 ENCRYPT_KEY_FILE_TPM2
,
131 _ENCRYPT_MODE_INVALID
= -EINVAL
,
135 char *definition_path
;
137 sd_id128_t type_uuid
;
138 sd_id128_t current_uuid
, new_uuid
;
139 char *current_label
, *new_label
;
145 uint32_t weight
, padding_weight
;
147 uint64_t current_size
, new_size
;
148 uint64_t size_min
, size_max
;
150 uint64_t current_padding
, new_padding
;
151 uint64_t padding_min
, padding_max
;
156 struct fdisk_partition
*current_partition
;
157 struct fdisk_partition
*new_partition
;
158 FreeArea
*padding_area
;
159 FreeArea
*allocated_to_area
;
161 char *copy_blocks_path
;
162 bool copy_blocks_auto
;
164 uint64_t copy_blocks_size
;
168 char **make_directories
;
176 LIST_FIELDS(Partition
, partitions
);
179 #define PARTITION_IS_FOREIGN(p) (!(p)->definition_path)
180 #define PARTITION_EXISTS(p) (!!(p)->current_partition)
189 LIST_HEAD(Partition
, partitions
);
192 FreeArea
**free_areas
;
195 uint64_t start
, end
, total
;
197 struct fdisk_context
*fdisk_context
;
202 static const char *encrypt_mode_table
[_ENCRYPT_MODE_MAX
] = {
203 [ENCRYPT_OFF
] = "off",
204 [ENCRYPT_KEY_FILE
] = "key-file",
205 [ENCRYPT_TPM2
] = "tpm2",
206 [ENCRYPT_KEY_FILE_TPM2
] = "key-file+tpm2",
209 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(encrypt_mode
, EncryptMode
, ENCRYPT_KEY_FILE
);
211 static uint64_t round_down_size(uint64_t v
, uint64_t p
) {
215 static uint64_t round_up_size(uint64_t v
, uint64_t p
) {
217 v
= DIV_ROUND_UP(v
, p
);
219 if (v
> UINT64_MAX
/ p
)
220 return UINT64_MAX
; /* overflow */
225 static Partition
*partition_new(void) {
228 p
= new(Partition
, 1);
235 .current_size
= UINT64_MAX
,
236 .new_size
= UINT64_MAX
,
237 .size_min
= UINT64_MAX
,
238 .size_max
= UINT64_MAX
,
239 .current_padding
= UINT64_MAX
,
240 .new_padding
= UINT64_MAX
,
241 .padding_min
= UINT64_MAX
,
242 .padding_max
= UINT64_MAX
,
243 .partno
= UINT64_MAX
,
244 .offset
= UINT64_MAX
,
245 .copy_blocks_fd
= -1,
246 .copy_blocks_size
= UINT64_MAX
,
255 static Partition
* partition_free(Partition
*p
) {
259 free(p
->current_label
);
261 free(p
->definition_path
);
263 if (p
->current_partition
)
264 fdisk_unref_partition(p
->current_partition
);
265 if (p
->new_partition
)
266 fdisk_unref_partition(p
->new_partition
);
268 free(p
->copy_blocks_path
);
269 safe_close(p
->copy_blocks_fd
);
272 strv_free(p
->copy_files
);
273 strv_free(p
->make_directories
);
278 static Partition
* partition_unlink_and_free(Context
*context
, Partition
*p
) {
282 LIST_REMOVE(partitions
, context
->partitions
, p
);
284 assert(context
->n_partitions
> 0);
285 context
->n_partitions
--;
287 return partition_free(p
);
290 DEFINE_TRIVIAL_CLEANUP_FUNC(Partition
*, partition_free
);
292 static Context
*context_new(sd_id128_t seed
) {
295 context
= new(Context
, 1);
299 *context
= (Context
) {
309 static void context_free_free_areas(Context
*context
) {
312 for (size_t i
= 0; i
< context
->n_free_areas
; i
++)
313 free(context
->free_areas
[i
]);
315 context
->free_areas
= mfree(context
->free_areas
);
316 context
->n_free_areas
= 0;
319 static Context
*context_free(Context
*context
) {
323 while (context
->partitions
)
324 partition_unlink_and_free(context
, context
->partitions
);
325 assert(context
->n_partitions
== 0);
327 context_free_free_areas(context
);
329 if (context
->fdisk_context
)
330 fdisk_unref_context(context
->fdisk_context
);
332 return mfree(context
);
335 DEFINE_TRIVIAL_CLEANUP_FUNC(Context
*, context_free
);
337 static int context_add_free_area(
345 assert(!after
|| !after
->padding_area
);
347 if (!GREEDY_REALLOC(context
->free_areas
, context
->n_free_areas
+ 1))
350 a
= new(FreeArea
, 1);
359 context
->free_areas
[context
->n_free_areas
++] = a
;
362 after
->padding_area
= a
;
367 static bool context_drop_one_priority(Context
*context
) {
368 int32_t priority
= 0;
372 LIST_FOREACH(partitions
, p
, context
->partitions
) {
375 if (p
->priority
< priority
)
377 if (p
->priority
== priority
) {
378 exists
= exists
|| PARTITION_EXISTS(p
);
382 priority
= p
->priority
;
383 exists
= PARTITION_EXISTS(p
);
386 /* Refuse to drop partitions with 0 or negative priorities or partitions of priorities that have at
387 * least one existing priority */
388 if (priority
<= 0 || exists
)
391 LIST_FOREACH(partitions
, p
, context
->partitions
) {
392 if (p
->priority
< priority
)
399 log_info("Can't fit partition %s of priority %" PRIi32
", dropping.", p
->definition_path
, p
->priority
);
405 static uint64_t partition_min_size(const Partition
*p
) {
408 /* Calculate the disk space we really need at minimum for this partition. If the partition already
409 * exists the current size is what we really need. If it doesn't exist yet refuse to allocate less
412 * DEFAULT_MIN_SIZE is the default SizeMin= we configure if nothing else is specified. */
414 if (PARTITION_IS_FOREIGN(p
)) {
415 /* Don't allow changing size of partitions not managed by us */
416 assert(p
->current_size
!= UINT64_MAX
);
417 return p
->current_size
;
420 sz
= p
->current_size
!= UINT64_MAX
? p
->current_size
: HARD_MIN_SIZE
;
422 if (!PARTITION_EXISTS(p
)) {
425 if (p
->encrypt
!= ENCRYPT_OFF
)
426 d
+= round_up_size(LUKS2_METADATA_SIZE
, 4096);
428 if (p
->copy_blocks_size
!= UINT64_MAX
)
429 d
+= round_up_size(p
->copy_blocks_size
, 4096);
430 else if (p
->format
|| p
->encrypt
!= ENCRYPT_OFF
) {
433 /* If we shall synthesize a file system, take minimal fs size into account (assumed to be 4K if not known) */
434 f
= p
->format
? minimal_size_by_fs_name(p
->format
) : UINT64_MAX
;
435 d
+= f
== UINT64_MAX
? 4096 : f
;
442 return MAX(p
->size_min
!= UINT64_MAX
? p
->size_min
: DEFAULT_MIN_SIZE
, sz
);
445 static uint64_t partition_max_size(const Partition
*p
) {
446 /* Calculate how large the partition may become at max. This is generally the configured maximum
447 * size, except when it already exists and is larger than that. In that case it's the existing size,
448 * since we never want to shrink partitions. */
450 if (PARTITION_IS_FOREIGN(p
)) {
451 /* Don't allow changing size of partitions not managed by us */
452 assert(p
->current_size
!= UINT64_MAX
);
453 return p
->current_size
;
456 if (p
->current_size
!= UINT64_MAX
)
457 return MAX(p
->current_size
, p
->size_max
);
462 static uint64_t partition_min_size_with_padding(const Partition
*p
) {
465 /* Calculate the disk space we need for this partition plus any free space coming after it. This
466 * takes user configured padding into account as well as any additional whitespace needed to align
467 * the next partition to 4K again. */
469 sz
= partition_min_size(p
);
471 if (p
->padding_min
!= UINT64_MAX
)
472 sz
+= p
->padding_min
;
474 if (PARTITION_EXISTS(p
)) {
475 /* If the partition wasn't aligned, add extra space so that any we might add will be aligned */
476 assert(p
->offset
!= UINT64_MAX
);
477 return round_up_size(p
->offset
+ sz
, 4096) - p
->offset
;
480 /* If this is a new partition we'll place it aligned, hence we just need to round up the required size here */
481 return round_up_size(sz
, 4096);
484 static uint64_t free_area_available(const FreeArea
*a
) {
487 /* Determines how much of this free area is not allocated yet */
489 assert(a
->size
>= a
->allocated
);
490 return a
->size
- a
->allocated
;
493 static uint64_t free_area_available_for_new_partitions(const FreeArea
*a
) {
496 /* Similar to free_area_available(), but takes into account that the required size and padding of the
497 * preceding partition is honoured. */
499 avail
= free_area_available(a
);
501 uint64_t need
, space
;
503 need
= partition_min_size_with_padding(a
->after
);
505 assert(a
->after
->offset
!= UINT64_MAX
);
506 assert(a
->after
->current_size
!= UINT64_MAX
);
508 space
= round_up_size(a
->after
->offset
+ a
->after
->current_size
, 4096) - a
->after
->offset
+ avail
;
518 static int free_area_compare(FreeArea
*const *a
, FreeArea
*const*b
) {
519 return CMP(free_area_available_for_new_partitions(*a
),
520 free_area_available_for_new_partitions(*b
));
523 static uint64_t charge_size(uint64_t total
, uint64_t amount
) {
526 assert(amount
<= total
);
528 /* Subtract the specified amount from total, rounding up to multiple of 4K if there's room */
529 rounded
= round_up_size(amount
, 4096);
530 if (rounded
>= total
)
533 return total
- rounded
;
536 static uint64_t charge_weight(uint64_t total
, uint64_t amount
) {
537 assert(amount
<= total
);
538 return total
- amount
;
541 static bool context_allocate_partitions(Context
*context
, uint64_t *ret_largest_free_area
) {
546 /* Sort free areas by size, putting smallest first */
547 typesafe_qsort(context
->free_areas
, context
->n_free_areas
, free_area_compare
);
549 /* In any case return size of the largest free area (i.e. not the size of all free areas
551 if (ret_largest_free_area
)
552 *ret_largest_free_area
=
553 context
->n_free_areas
== 0 ? 0 :
554 free_area_available_for_new_partitions(context
->free_areas
[context
->n_free_areas
-1]);
556 /* A simple first-fit algorithm. We return true if we can fit the partitions in, otherwise false. */
557 LIST_FOREACH(partitions
, p
, context
->partitions
) {
562 /* Skip partitions we already dropped or that already exist */
563 if (p
->dropped
|| PARTITION_EXISTS(p
))
566 /* How much do we need to fit? */
567 required
= partition_min_size_with_padding(p
);
568 assert(required
% 4096 == 0);
570 for (size_t i
= 0; i
< context
->n_free_areas
; i
++) {
571 a
= context
->free_areas
[i
];
573 if (free_area_available_for_new_partitions(a
) >= required
) {
580 return false; /* 😢 Oh no! We can't fit this partition into any free area! */
582 /* Assign the partition to this free area */
583 p
->allocated_to_area
= a
;
585 /* Budget the minimal partition size */
586 a
->allocated
+= required
;
592 static int context_sum_weights(Context
*context
, FreeArea
*a
, uint64_t *ret
) {
593 uint64_t weight_sum
= 0;
600 /* Determine the sum of the weights of all partitions placed in or before the specified free area */
602 LIST_FOREACH(partitions
, p
, context
->partitions
) {
603 if (p
->padding_area
!= a
&& p
->allocated_to_area
!= a
)
606 if (p
->weight
> UINT64_MAX
- weight_sum
)
608 weight_sum
+= p
->weight
;
610 if (p
->padding_weight
> UINT64_MAX
- weight_sum
)
612 weight_sum
+= p
->padding_weight
;
619 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "Combined weight of partition exceeds unsigned 64bit range, refusing.");
622 static int scale_by_weight(uint64_t value
, uint64_t weight
, uint64_t weight_sum
, uint64_t *ret
) {
623 assert(weight_sum
>= weight
);
631 if (value
> UINT64_MAX
/ weight
)
632 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "Scaling by weight of partition exceeds unsigned 64bit range, refusing.");
634 *ret
= value
* weight
/ weight_sum
;
638 typedef enum GrowPartitionPhase
{
639 /* The first phase: we charge partitions which need more (according to constraints) than their weight-based share. */
642 /* The second phase: we charge partitions which need less (according to constraints) than their weight-based share. */
645 /* The third phase: we distribute what remains among the remaining partitions, according to the weights */
647 } GrowPartitionPhase
;
649 static int context_grow_partitions_phase(
652 GrowPartitionPhase phase
,
654 uint64_t *weight_sum
) {
662 /* Now let's look at the intended weights and adjust them taking the minimum space assignments into
663 * account. i.e. if a partition has a small weight but a high minimum space value set it should not
664 * get any additional room from the left-overs. Similar, if two partitions have the same weight they
665 * should get the same space if possible, even if one has a smaller minimum size than the other. */
666 LIST_FOREACH(partitions
, p
, context
->partitions
) {
668 /* Look only at partitions associated with this free area, i.e. immediately
669 * preceding it, or allocated into it */
670 if (p
->allocated_to_area
!= a
&& p
->padding_area
!= a
)
673 if (p
->new_size
== UINT64_MAX
) {
674 bool charge
= false, try_again
= false;
675 uint64_t share
, rsz
, xsz
;
677 /* Calculate how much this space this partition needs if everyone would get
678 * the weight based share */
679 r
= scale_by_weight(*span
, p
->weight
, *weight_sum
, &share
);
683 rsz
= partition_min_size(p
);
684 xsz
= partition_max_size(p
);
686 if (phase
== PHASE_OVERCHARGE
&& rsz
> share
) {
687 /* This partition needs more than its calculated share. Let's assign
688 * it that, and take this partition out of all calculations and start
692 charge
= try_again
= true;
694 } else if (phase
== PHASE_UNDERCHARGE
&& xsz
!= UINT64_MAX
&& xsz
< share
) {
695 /* This partition accepts less than its calculated
696 * share. Let's assign it that, and take this partition out
697 * of all calculations and start again. */
700 charge
= try_again
= true;
702 } else if (phase
== PHASE_DISTRIBUTE
) {
703 /* This partition can accept its calculated share. Let's
704 * assign it. There's no need to restart things here since
705 * assigning this shouldn't impact the shares of the other
708 if (PARTITION_IS_FOREIGN(p
))
709 /* Never change of foreign partitions (i.e. those we don't manage) */
710 p
->new_size
= p
->current_size
;
712 p
->new_size
= MAX(round_down_size(share
, 4096), rsz
);
718 *span
= charge_size(*span
, p
->new_size
);
719 *weight_sum
= charge_weight(*weight_sum
, p
->weight
);
723 return 0; /* try again */
726 if (p
->new_padding
== UINT64_MAX
) {
727 bool charge
= false, try_again
= false;
730 r
= scale_by_weight(*span
, p
->padding_weight
, *weight_sum
, &share
);
734 if (phase
== PHASE_OVERCHARGE
&& p
->padding_min
!= UINT64_MAX
&& p
->padding_min
> share
) {
735 p
->new_padding
= p
->padding_min
;
736 charge
= try_again
= true;
737 } else if (phase
== PHASE_UNDERCHARGE
&& p
->padding_max
!= UINT64_MAX
&& p
->padding_max
< share
) {
738 p
->new_padding
= p
->padding_max
;
739 charge
= try_again
= true;
740 } else if (phase
== PHASE_DISTRIBUTE
) {
742 p
->new_padding
= round_down_size(share
, 4096);
743 if (p
->padding_min
!= UINT64_MAX
&& p
->new_padding
< p
->padding_min
)
744 p
->new_padding
= p
->padding_min
;
750 *span
= charge_size(*span
, p
->new_padding
);
751 *weight_sum
= charge_weight(*weight_sum
, p
->padding_weight
);
755 return 0; /* try again */
762 static int context_grow_partitions_on_free_area(Context
*context
, FreeArea
*a
) {
763 uint64_t weight_sum
= 0, span
;
769 r
= context_sum_weights(context
, a
, &weight_sum
);
773 /* Let's calculate the total area covered by this free area and the partition before it */
776 assert(a
->after
->offset
!= UINT64_MAX
);
777 assert(a
->after
->current_size
!= UINT64_MAX
);
779 span
+= round_up_size(a
->after
->offset
+ a
->after
->current_size
, 4096) - a
->after
->offset
;
782 GrowPartitionPhase phase
= PHASE_OVERCHARGE
;
784 r
= context_grow_partitions_phase(context
, a
, phase
, &span
, &weight_sum
);
787 if (r
== 0) /* not done yet, re-run this phase */
790 if (phase
== PHASE_OVERCHARGE
)
791 phase
= PHASE_UNDERCHARGE
;
792 else if (phase
== PHASE_UNDERCHARGE
)
793 phase
= PHASE_DISTRIBUTE
;
794 else if (phase
== PHASE_DISTRIBUTE
)
798 /* We still have space left over? Donate to preceding partition if we have one */
799 if (span
> 0 && a
->after
&& !PARTITION_IS_FOREIGN(a
->after
)) {
802 assert(a
->after
->new_size
!= UINT64_MAX
);
803 m
= a
->after
->new_size
+ span
;
805 xsz
= partition_max_size(a
->after
);
806 if (xsz
!= UINT64_MAX
&& m
> xsz
)
809 span
= charge_size(span
, m
- a
->after
->new_size
);
810 a
->after
->new_size
= m
;
813 /* What? Even still some space left (maybe because there was no preceding partition, or it had a
814 * size limit), then let's donate it to whoever wants it. */
818 LIST_FOREACH(partitions
, p
, context
->partitions
) {
821 if (p
->allocated_to_area
!= a
)
824 if (PARTITION_IS_FOREIGN(p
))
827 assert(p
->new_size
!= UINT64_MAX
);
828 m
= p
->new_size
+ span
;
830 xsz
= partition_max_size(p
);
831 if (xsz
!= UINT64_MAX
&& m
> xsz
)
834 span
= charge_size(span
, m
- p
->new_size
);
842 /* Yuck, still no one? Then make it padding */
843 if (span
> 0 && a
->after
) {
844 assert(a
->after
->new_padding
!= UINT64_MAX
);
845 a
->after
->new_padding
+= span
;
851 static int context_grow_partitions(Context
*context
) {
857 for (size_t i
= 0; i
< context
->n_free_areas
; i
++) {
858 r
= context_grow_partitions_on_free_area(context
, context
->free_areas
[i
]);
863 /* All existing partitions that have no free space after them can't change size */
864 LIST_FOREACH(partitions
, p
, context
->partitions
) {
868 if (!PARTITION_EXISTS(p
) || p
->padding_area
) {
869 /* The algorithm above must have initialized this already */
870 assert(p
->new_size
!= UINT64_MAX
);
874 assert(p
->new_size
== UINT64_MAX
);
875 p
->new_size
= p
->current_size
;
877 assert(p
->new_padding
== UINT64_MAX
);
878 p
->new_padding
= p
->current_padding
;
884 static void context_place_partitions(Context
*context
) {
890 /* Determine next partition number to assign */
891 LIST_FOREACH(partitions
, p
, context
->partitions
) {
892 if (!PARTITION_EXISTS(p
))
895 assert(p
->partno
!= UINT64_MAX
);
896 if (p
->partno
>= partno
)
897 partno
= p
->partno
+ 1;
900 for (size_t i
= 0; i
< context
->n_free_areas
; i
++) {
901 FreeArea
*a
= context
->free_areas
[i
];
902 uint64_t start
, left
;
905 assert(a
->after
->offset
!= UINT64_MAX
);
906 assert(a
->after
->new_size
!= UINT64_MAX
);
907 assert(a
->after
->new_padding
!= UINT64_MAX
);
909 start
= a
->after
->offset
+ a
->after
->new_size
+ a
->after
->new_padding
;
911 start
= context
->start
;
913 start
= round_up_size(start
, 4096);
916 LIST_FOREACH(partitions
, p
, context
->partitions
) {
917 if (p
->allocated_to_area
!= a
)
921 p
->partno
= partno
++;
923 assert(left
>= p
->new_size
);
924 start
+= p
->new_size
;
927 assert(left
>= p
->new_padding
);
928 start
+= p
->new_padding
;
929 left
-= p
->new_padding
;
934 static int config_parse_type(
936 const char *filename
,
939 unsigned section_line
,
946 sd_id128_t
*type_uuid
= data
;
952 r
= gpt_partition_type_uuid_from_string(rvalue
, type_uuid
);
954 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse partition type: %s", rvalue
);
959 static int config_parse_label(
961 const char *filename
,
964 unsigned section_line
,
971 _cleanup_free_
char *resolved
= NULL
;
978 /* Nota bene: the empty label is a totally valid one. Let's hence not follow our usual rule of
979 * assigning the empty string to reset to default here, but really accept it as label to set. */
981 r
= specifier_printf(rvalue
, GPT_LABEL_MAX
, system_and_tmp_specifier_table
, arg_root
, NULL
, &resolved
);
983 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
984 "Failed to expand specifiers in Label=, ignoring: %s", rvalue
);
988 if (!utf8_is_valid(resolved
)) {
989 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
990 "Partition label not valid UTF-8, ignoring: %s", rvalue
);
994 r
= gpt_partition_label_valid(resolved
);
996 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
997 "Failed to check if string is valid as GPT partition label, ignoring: \"%s\" (from \"%s\")",
1002 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1003 "Partition label too long for GPT table, ignoring: \"%s\" (from \"%s\")",
1008 free_and_replace(*label
, resolved
);
1012 static int config_parse_weight(
1014 const char *filename
,
1016 const char *section
,
1017 unsigned section_line
,
1024 uint32_t *priority
= data
, v
;
1030 r
= safe_atou32(rvalue
, &v
);
1032 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1033 "Failed to parse weight value, ignoring: %s", rvalue
);
1037 if (v
> 1000U*1000U) {
1038 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1039 "Weight needs to be in range 0…10000000, ignoring: %" PRIu32
, v
);
1047 static int config_parse_size4096(
1049 const char *filename
,
1051 const char *section
,
1052 unsigned section_line
,
1059 uint64_t *sz
= data
, parsed
;
1065 r
= parse_size(rvalue
, 1024, &parsed
);
1067 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1068 "Failed to parse size value: %s", rvalue
);
1071 *sz
= round_up_size(parsed
, 4096);
1073 *sz
= round_down_size(parsed
, 4096);
1078 log_syntax(unit
, LOG_NOTICE
, filename
, line
, r
, "Rounded %s= size %" PRIu64
" → %" PRIu64
", a multiple of 4096.", lvalue
, parsed
, *sz
);
1083 static int config_parse_fstype(
1085 const char *filename
,
1087 const char *section
,
1088 unsigned section_line
,
1095 char **fstype
= data
;
1100 if (!filename_is_valid(rvalue
))
1101 return log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1102 "File system type is not valid, refusing: %s", rvalue
);
1104 return free_and_strdup_warn(fstype
, rvalue
);
1107 static int config_parse_copy_files(
1109 const char *filename
,
1111 const char *section
,
1112 unsigned section_line
,
1119 _cleanup_free_
char *source
= NULL
, *buffer
= NULL
, *resolved_source
= NULL
, *resolved_target
= NULL
;
1120 const char *p
= rvalue
, *target
;
1121 Partition
*partition
= data
;
1127 r
= extract_first_word(&p
, &source
, ":", EXTRACT_CUNESCAPE
|EXTRACT_DONT_COALESCE_SEPARATORS
);
1129 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract source path: %s", rvalue
);
1131 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "No argument specified: %s", rvalue
);
1135 r
= extract_first_word(&p
, &buffer
, ":", EXTRACT_CUNESCAPE
|EXTRACT_DONT_COALESCE_SEPARATORS
);
1137 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract target path: %s", rvalue
);
1139 target
= source
; /* No target, then it's the same as the source */
1144 return log_syntax(unit
, LOG_ERR
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
), "Too many arguments: %s", rvalue
);
1146 r
= specifier_printf(source
, PATH_MAX
-1, system_and_tmp_specifier_table
, arg_root
, NULL
, &resolved_source
);
1148 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1149 "Failed to expand specifiers in CopyFiles= source, ignoring: %s", rvalue
);
1153 r
= path_simplify_and_warn(resolved_source
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
1157 r
= specifier_printf(target
, PATH_MAX
-1, system_and_tmp_specifier_table
, arg_root
, NULL
, &resolved_target
);
1159 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1160 "Failed to expand specifiers in CopyFiles= target, ignoring: %s", resolved_target
);
1164 r
= path_simplify_and_warn(resolved_target
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
1168 r
= strv_consume_pair(&partition
->copy_files
, TAKE_PTR(resolved_source
), TAKE_PTR(resolved_target
));
1175 static int config_parse_copy_blocks(
1177 const char *filename
,
1179 const char *section
,
1180 unsigned section_line
,
1187 _cleanup_free_
char *d
= NULL
;
1188 Partition
*partition
= data
;
1194 if (isempty(rvalue
)) {
1195 partition
->copy_blocks_path
= mfree(partition
->copy_blocks_path
);
1196 partition
->copy_blocks_auto
= false;
1200 if (streq(rvalue
, "auto")) {
1201 partition
->copy_blocks_path
= mfree(partition
->copy_blocks_path
);
1202 partition
->copy_blocks_auto
= true;
1206 r
= specifier_printf(rvalue
, PATH_MAX
-1, system_and_tmp_specifier_table
, arg_root
, NULL
, &d
);
1208 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1209 "Failed to expand specifiers in CopyBlocks= source path, ignoring: %s", rvalue
);
1213 r
= path_simplify_and_warn(d
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
1217 free_and_replace(partition
->copy_blocks_path
, d
);
1218 partition
->copy_blocks_auto
= false;
1222 static int config_parse_make_dirs(
1224 const char *filename
,
1226 const char *section
,
1227 unsigned section_line
,
1234 Partition
*partition
= data
;
1235 const char *p
= rvalue
;
1242 _cleanup_free_
char *word
= NULL
, *d
= NULL
;
1244 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_UNQUOTE
);
1248 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
1254 r
= specifier_printf(word
, PATH_MAX
-1, system_and_tmp_specifier_table
, arg_root
, NULL
, &d
);
1256 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1257 "Failed to expand specifiers in MakeDirectories= parameter, ignoring: %s", word
);
1261 r
= path_simplify_and_warn(d
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
1265 r
= strv_consume(&partition
->make_directories
, TAKE_PTR(d
));
1271 static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_encrypt
, encrypt_mode
, EncryptMode
, ENCRYPT_OFF
, "Invalid encryption mode");
1273 static int config_parse_gpt_flags(
1275 const char *filename
,
1277 const char *section
,
1278 unsigned section_line
,
1285 uint64_t *gpt_flags
= data
;
1291 r
= safe_atou64(rvalue
, gpt_flags
);
1293 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1294 "Failed to parse Flags= value, ignoring: %s", rvalue
);
1301 static int partition_read_definition(Partition
*p
, const char *path
) {
1303 ConfigTableItem table
[] = {
1304 { "Partition", "Type", config_parse_type
, 0, &p
->type_uuid
},
1305 { "Partition", "Label", config_parse_label
, 0, &p
->new_label
},
1306 { "Partition", "UUID", config_parse_id128
, 0, &p
->new_uuid
},
1307 { "Partition", "Priority", config_parse_int32
, 0, &p
->priority
},
1308 { "Partition", "Weight", config_parse_weight
, 0, &p
->weight
},
1309 { "Partition", "PaddingWeight", config_parse_weight
, 0, &p
->padding_weight
},
1310 { "Partition", "SizeMinBytes", config_parse_size4096
, 1, &p
->size_min
},
1311 { "Partition", "SizeMaxBytes", config_parse_size4096
, -1, &p
->size_max
},
1312 { "Partition", "PaddingMinBytes", config_parse_size4096
, 1, &p
->padding_min
},
1313 { "Partition", "PaddingMaxBytes", config_parse_size4096
, -1, &p
->padding_max
},
1314 { "Partition", "FactoryReset", config_parse_bool
, 0, &p
->factory_reset
},
1315 { "Partition", "CopyBlocks", config_parse_copy_blocks
, 0, p
},
1316 { "Partition", "Format", config_parse_fstype
, 0, &p
->format
},
1317 { "Partition", "CopyFiles", config_parse_copy_files
, 0, p
},
1318 { "Partition", "MakeDirectories", config_parse_make_dirs
, 0, p
},
1319 { "Partition", "Encrypt", config_parse_encrypt
, 0, &p
->encrypt
},
1320 { "Partition", "Flags", config_parse_gpt_flags
, 0, &p
->gpt_flags
},
1321 { "Partition", "ReadOnly", config_parse_tristate
, 0, &p
->read_only
},
1322 { "Partition", "NoAuto", config_parse_tristate
, 0, &p
->no_auto
},
1323 { "Partition", "GrowFileSystem", config_parse_tristate
, 0, &p
->growfs
},
1328 r
= config_parse(NULL
, path
, NULL
,
1330 config_item_table_lookup
, table
,
1337 if (p
->size_min
!= UINT64_MAX
&& p
->size_max
!= UINT64_MAX
&& p
->size_min
> p
->size_max
)
1338 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1339 "SizeMinBytes= larger than SizeMaxBytes=, refusing.");
1341 if (p
->padding_min
!= UINT64_MAX
&& p
->padding_max
!= UINT64_MAX
&& p
->padding_min
> p
->padding_max
)
1342 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1343 "PaddingMinBytes= larger than PaddingMaxBytes=, refusing.");
1345 if (sd_id128_is_null(p
->type_uuid
))
1346 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1347 "Type= not defined, refusing.");
1349 if ((p
->copy_blocks_path
|| p
->copy_blocks_auto
) &&
1350 (p
->format
|| !strv_isempty(p
->copy_files
) || !strv_isempty(p
->make_directories
)))
1351 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1352 "Format=/CopyFiles=/MakeDirectories= and CopyBlocks= cannot be combined, refusing.");
1354 if ((!strv_isempty(p
->copy_files
) || !strv_isempty(p
->make_directories
)) && streq_ptr(p
->format
, "swap"))
1355 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1356 "Format=swap and CopyFiles= cannot be combined, refusing.");
1358 if (!p
->format
&& (!strv_isempty(p
->copy_files
) || !strv_isempty(p
->make_directories
) || (p
->encrypt
!= ENCRYPT_OFF
&& !(p
->copy_blocks_path
|| p
->copy_blocks_auto
)))) {
1359 /* Pick "ext4" as file system if we are configured to copy files or encrypt the device */
1360 p
->format
= strdup("ext4");
1365 /* Verity partitions are read only, let's imply the RO flag hence, unless explicitly configured otherwise. */
1366 if ((gpt_partition_type_is_root_verity(p
->type_uuid
) ||
1367 gpt_partition_type_is_usr_verity(p
->type_uuid
)) &&
1369 p
->read_only
= true;
1371 /* Default to "growfs" on, unless read-only */
1372 if (gpt_partition_type_knows_growfs(p
->type_uuid
) &&
1379 static int context_read_definitions(
1381 const char *directory
,
1384 _cleanup_strv_free_
char **files
= NULL
;
1385 Partition
*last
= NULL
;
1392 r
= conf_files_list_strv(&files
, ".conf", NULL
, CONF_FILES_REGULAR
|CONF_FILES_FILTER_MASKED
, (const char**) STRV_MAKE(directory
));
1394 r
= conf_files_list_strv(&files
, ".conf", root
, CONF_FILES_REGULAR
|CONF_FILES_FILTER_MASKED
, (const char**) CONF_PATHS_STRV("repart.d"));
1396 return log_error_errno(r
, "Failed to enumerate *.conf files: %m");
1398 STRV_FOREACH(f
, files
) {
1399 _cleanup_(partition_freep
) Partition
*p
= NULL
;
1401 p
= partition_new();
1405 p
->definition_path
= strdup(*f
);
1406 if (!p
->definition_path
)
1409 r
= partition_read_definition(p
, *f
);
1413 LIST_INSERT_AFTER(partitions
, context
->partitions
, last
, p
);
1415 context
->n_partitions
++;
1421 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_context
*, fdisk_unref_context
, NULL
);
1422 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_partition
*, fdisk_unref_partition
, NULL
);
1423 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_parttype
*, fdisk_unref_parttype
, NULL
);
1424 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(struct fdisk_table
*, fdisk_unref_table
, NULL
);
1426 static int determine_current_padding(
1427 struct fdisk_context
*c
,
1428 struct fdisk_table
*t
,
1429 struct fdisk_partition
*p
,
1432 size_t n_partitions
;
1433 uint64_t offset
, next
= UINT64_MAX
;
1439 if (!fdisk_partition_has_end(p
))
1440 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition has no end!");
1442 offset
= fdisk_partition_get_end(p
);
1443 assert(offset
< UINT64_MAX
/ 512);
1446 n_partitions
= fdisk_table_get_nents(t
);
1447 for (size_t i
= 0; i
< n_partitions
; i
++) {
1448 struct fdisk_partition
*q
;
1451 q
= fdisk_table_get_partition(t
, i
);
1453 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to read partition metadata: %m");
1455 if (fdisk_partition_is_used(q
) <= 0)
1458 if (!fdisk_partition_has_start(q
))
1461 start
= fdisk_partition_get_start(q
);
1462 assert(start
< UINT64_MAX
/ 512);
1465 if (start
>= offset
&& (next
== UINT64_MAX
|| next
> start
))
1469 if (next
== UINT64_MAX
) {
1470 /* No later partition? In that case check the end of the usable area */
1471 next
= fdisk_get_last_lba(c
);
1472 assert(next
< UINT64_MAX
);
1473 next
++; /* The last LBA is one sector before the end */
1475 assert(next
< UINT64_MAX
/ 512);
1479 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition end beyond disk end.");
1482 assert(next
>= offset
);
1483 offset
= round_up_size(offset
, 4096);
1484 next
= round_down_size(next
, 4096);
1486 if (next
>= offset
) /* Check again, rounding might have fucked things up */
1487 *ret
= next
- offset
;
1494 static int fdisk_ask_cb(struct fdisk_context
*c
, struct fdisk_ask
*ask
, void *data
) {
1495 _cleanup_free_
char *ids
= NULL
;
1498 if (fdisk_ask_get_type(ask
) != FDISK_ASKTYPE_STRING
)
1501 ids
= new(char, ID128_UUID_STRING_MAX
);
1505 r
= fdisk_ask_string_set_result(ask
, id128_to_uuid_string(*(sd_id128_t
*) data
, ids
));
1513 static int fdisk_set_disklabel_id_by_uuid(struct fdisk_context
*c
, sd_id128_t id
) {
1516 r
= fdisk_set_ask(c
, fdisk_ask_cb
, &id
);
1520 r
= fdisk_set_disklabel_id(c
);
1524 return fdisk_set_ask(c
, NULL
, NULL
);
1527 static int derive_uuid(sd_id128_t base
, const char *token
, sd_id128_t
*ret
) {
1529 unsigned char md
[SHA256_DIGEST_LENGTH
];
1536 /* Derive a new UUID from the specified UUID in a stable and reasonably safe way. Specifically, we
1537 * calculate the HMAC-SHA256 of the specified token string, keyed by the supplied base (typically the
1538 * machine ID). We use the machine ID as key (and not as cleartext!) of the HMAC operation since it's
1539 * the machine ID we don't want to leak. */
1541 if (!HMAC(EVP_sha256(),
1542 &base
, sizeof(base
),
1543 (const unsigned char*) token
, strlen(token
),
1545 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE
), "HMAC-SHA256 calculation failed.");
1547 /* Take the first half, mark it as v4 UUID */
1548 assert_cc(sizeof(result
.md
) == sizeof(result
.id
) * 2);
1549 *ret
= id128_make_v4_uuid(result
.id
);
1553 static int context_load_partition_table(
1558 _cleanup_(fdisk_unref_contextp
) struct fdisk_context
*c
= NULL
;
1559 _cleanup_(fdisk_unref_tablep
) struct fdisk_table
*t
= NULL
;
1560 uint64_t left_boundary
= UINT64_MAX
, first_lba
, last_lba
, nsectors
;
1561 _cleanup_free_
char *disk_uuid_string
= NULL
;
1562 bool from_scratch
= false;
1563 sd_id128_t disk_uuid
;
1564 size_t n_partitions
;
1570 assert(!context
->fdisk_context
);
1571 assert(!context
->free_areas
);
1572 assert(context
->start
== UINT64_MAX
);
1573 assert(context
->end
== UINT64_MAX
);
1574 assert(context
->total
== UINT64_MAX
);
1576 c
= fdisk_new_context();
1580 /* libfdisk doesn't have an API to operate on arbitrary fds, hence reopen the fd going via the
1581 * /proc/self/fd/ magic path if we have an existing fd. Open the original file otherwise. */
1582 if (*backing_fd
< 0)
1583 r
= fdisk_assign_device(c
, node
, arg_dry_run
);
1585 r
= fdisk_assign_device(c
, FORMAT_PROC_FD_PATH(*backing_fd
), arg_dry_run
);
1586 if (r
== -EINVAL
&& arg_size_auto
) {
1589 /* libfdisk returns EINVAL if opening a file of size zero. Let's check for that, and accept
1590 * it if automatic sizing is requested. */
1592 if (*backing_fd
< 0)
1593 r
= stat(node
, &st
);
1595 r
= fstat(*backing_fd
, &st
);
1597 return log_error_errno(errno
, "Failed to stat block device '%s': %m", node
);
1599 if (S_ISREG(st
.st_mode
) && st
.st_size
== 0)
1600 return /* from_scratch = */ true;
1605 return log_error_errno(r
, "Failed to open device '%s': %m", node
);
1607 if (*backing_fd
< 0) {
1608 /* If we have no fd referencing the device yet, make a copy of the fd now, so that we have one */
1609 *backing_fd
= fcntl(fdisk_get_devfd(c
), F_DUPFD_CLOEXEC
, 3);
1610 if (*backing_fd
< 0)
1611 return log_error_errno(errno
, "Failed to duplicate fdisk fd: %m");
1614 /* Tell udev not to interfere while we are processing the device */
1615 if (flock(fdisk_get_devfd(c
), arg_dry_run
? LOCK_SH
: LOCK_EX
) < 0)
1616 return log_error_errno(errno
, "Failed to lock block device: %m");
1618 switch (arg_empty
) {
1621 /* Refuse empty disks, insist on an existing GPT partition table */
1622 if (!fdisk_is_labeltype(c
, FDISK_DISKLABEL_GPT
))
1623 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON
), "Disk %s has no GPT disk label, not repartitioning.", node
);
1628 /* Require an empty disk, refuse any existing partition table */
1629 r
= fdisk_has_label(c
);
1631 return log_error_errno(r
, "Failed to determine whether disk %s has a disk label: %m", node
);
1633 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON
), "Disk %s already has a disk label, refusing.", node
);
1635 from_scratch
= true;
1639 /* Allow both an empty disk and an existing partition table, but only GPT */
1640 r
= fdisk_has_label(c
);
1642 return log_error_errno(r
, "Failed to determine whether disk %s has a disk label: %m", node
);
1644 if (!fdisk_is_labeltype(c
, FDISK_DISKLABEL_GPT
))
1645 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON
), "Disk %s has non-GPT disk label, not repartitioning.", node
);
1647 from_scratch
= true;
1653 /* Always reinitiaize the disk, don't consider what there was on the disk before */
1654 from_scratch
= true;
1659 r
= fdisk_create_disklabel(c
, "gpt");
1661 return log_error_errno(r
, "Failed to create GPT disk label: %m");
1663 r
= derive_uuid(context
->seed
, "disk-uuid", &disk_uuid
);
1665 return log_error_errno(r
, "Failed to acquire disk GPT uuid: %m");
1667 r
= fdisk_set_disklabel_id_by_uuid(c
, disk_uuid
);
1669 return log_error_errno(r
, "Failed to set GPT disk label: %m");
1671 goto add_initial_free_area
;
1674 r
= fdisk_get_disklabel_id(c
, &disk_uuid_string
);
1676 return log_error_errno(r
, "Failed to get current GPT disk label UUID: %m");
1678 r
= sd_id128_from_string(disk_uuid_string
, &disk_uuid
);
1680 return log_error_errno(r
, "Failed to parse current GPT disk label UUID: %m");
1682 if (sd_id128_is_null(disk_uuid
)) {
1683 r
= derive_uuid(context
->seed
, "disk-uuid", &disk_uuid
);
1685 return log_error_errno(r
, "Failed to acquire disk GPT uuid: %m");
1687 r
= fdisk_set_disklabel_id(c
);
1689 return log_error_errno(r
, "Failed to set GPT disk label: %m");
1692 r
= fdisk_get_partitions(c
, &t
);
1694 return log_error_errno(r
, "Failed to acquire partition table: %m");
1696 n_partitions
= fdisk_table_get_nents(t
);
1697 for (size_t i
= 0; i
< n_partitions
; i
++) {
1698 _cleanup_free_
char *label_copy
= NULL
;
1699 Partition
*pp
, *last
= NULL
;
1700 struct fdisk_partition
*p
;
1701 struct fdisk_parttype
*pt
;
1702 const char *pts
, *ids
, *label
;
1705 sd_id128_t ptid
, id
;
1708 p
= fdisk_table_get_partition(t
, i
);
1710 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to read partition metadata: %m");
1712 if (fdisk_partition_is_used(p
) <= 0)
1715 if (fdisk_partition_has_start(p
) <= 0 ||
1716 fdisk_partition_has_size(p
) <= 0 ||
1717 fdisk_partition_has_partno(p
) <= 0)
1718 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Found a partition without a position, size or number.");
1720 pt
= fdisk_partition_get_type(p
);
1722 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to acquire type of partition: %m");
1724 pts
= fdisk_parttype_get_string(pt
);
1726 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to acquire type of partition as string: %m");
1728 r
= sd_id128_from_string(pts
, &ptid
);
1730 return log_error_errno(r
, "Failed to parse partition type UUID %s: %m", pts
);
1732 ids
= fdisk_partition_get_uuid(p
);
1734 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Found a partition without a UUID.");
1736 r
= sd_id128_from_string(ids
, &id
);
1738 return log_error_errno(r
, "Failed to parse partition UUID %s: %m", ids
);
1740 label
= fdisk_partition_get_name(p
);
1741 if (!isempty(label
)) {
1742 label_copy
= strdup(label
);
1747 sz
= fdisk_partition_get_size(p
);
1748 assert_se(sz
<= UINT64_MAX
/512);
1751 start
= fdisk_partition_get_start(p
);
1752 assert_se(start
<= UINT64_MAX
/512);
1755 partno
= fdisk_partition_get_partno(p
);
1757 if (left_boundary
== UINT64_MAX
|| left_boundary
> start
)
1758 left_boundary
= start
;
1760 /* Assign this existing partition to the first partition of the right type that doesn't have
1761 * an existing one assigned yet. */
1762 LIST_FOREACH(partitions
, pp
, context
->partitions
) {
1765 if (!sd_id128_equal(pp
->type_uuid
, ptid
))
1768 if (!pp
->current_partition
) {
1769 pp
->current_uuid
= id
;
1770 pp
->current_size
= sz
;
1772 pp
->partno
= partno
;
1773 pp
->current_label
= TAKE_PTR(label_copy
);
1775 pp
->current_partition
= p
;
1776 fdisk_ref_partition(p
);
1778 r
= determine_current_padding(c
, t
, p
, &pp
->current_padding
);
1782 if (pp
->current_padding
> 0) {
1783 r
= context_add_free_area(context
, pp
->current_padding
, pp
);
1793 /* If we have no matching definition, create a new one. */
1795 _cleanup_(partition_freep
) Partition
*np
= NULL
;
1797 np
= partition_new();
1801 np
->current_uuid
= id
;
1802 np
->type_uuid
= ptid
;
1803 np
->current_size
= sz
;
1805 np
->partno
= partno
;
1806 np
->current_label
= TAKE_PTR(label_copy
);
1808 np
->current_partition
= p
;
1809 fdisk_ref_partition(p
);
1811 r
= determine_current_padding(c
, t
, p
, &np
->current_padding
);
1815 if (np
->current_padding
> 0) {
1816 r
= context_add_free_area(context
, np
->current_padding
, np
);
1821 LIST_INSERT_AFTER(partitions
, context
->partitions
, last
, TAKE_PTR(np
));
1822 context
->n_partitions
++;
1826 add_initial_free_area
:
1827 nsectors
= fdisk_get_nsectors(c
);
1828 assert(nsectors
<= UINT64_MAX
/512);
1831 first_lba
= fdisk_get_first_lba(c
);
1832 assert(first_lba
<= UINT64_MAX
/512);
1835 last_lba
= fdisk_get_last_lba(c
);
1836 assert(last_lba
< UINT64_MAX
);
1838 assert(last_lba
<= UINT64_MAX
/512);
1841 assert(last_lba
>= first_lba
);
1843 if (left_boundary
== UINT64_MAX
) {
1844 /* No partitions at all? Then the whole disk is up for grabs. */
1846 first_lba
= round_up_size(first_lba
, 4096);
1847 last_lba
= round_down_size(last_lba
, 4096);
1849 if (last_lba
> first_lba
) {
1850 r
= context_add_free_area(context
, last_lba
- first_lba
, NULL
);
1855 /* Add space left of first partition */
1856 assert(left_boundary
>= first_lba
);
1858 first_lba
= round_up_size(first_lba
, 4096);
1859 left_boundary
= round_down_size(left_boundary
, 4096);
1860 last_lba
= round_down_size(last_lba
, 4096);
1862 if (left_boundary
> first_lba
) {
1863 r
= context_add_free_area(context
, left_boundary
- first_lba
, NULL
);
1869 context
->start
= first_lba
;
1870 context
->end
= last_lba
;
1871 context
->total
= nsectors
;
1872 context
->fdisk_context
= TAKE_PTR(c
);
1874 return from_scratch
;
1877 static void context_unload_partition_table(Context
*context
) {
1878 Partition
*p
, *next
;
1882 LIST_FOREACH_SAFE(partitions
, p
, next
, context
->partitions
) {
1884 /* Entirely remove partitions that have no configuration */
1885 if (PARTITION_IS_FOREIGN(p
)) {
1886 partition_unlink_and_free(context
, p
);
1890 /* Otherwise drop all data we read off the block device and everything we might have
1891 * calculated based on it */
1894 p
->current_size
= UINT64_MAX
;
1895 p
->new_size
= UINT64_MAX
;
1896 p
->current_padding
= UINT64_MAX
;
1897 p
->new_padding
= UINT64_MAX
;
1898 p
->partno
= UINT64_MAX
;
1899 p
->offset
= UINT64_MAX
;
1901 if (p
->current_partition
) {
1902 fdisk_unref_partition(p
->current_partition
);
1903 p
->current_partition
= NULL
;
1906 if (p
->new_partition
) {
1907 fdisk_unref_partition(p
->new_partition
);
1908 p
->new_partition
= NULL
;
1911 p
->padding_area
= NULL
;
1912 p
->allocated_to_area
= NULL
;
1914 p
->current_uuid
= SD_ID128_NULL
;
1915 p
->current_label
= mfree(p
->current_label
);
1918 context
->start
= UINT64_MAX
;
1919 context
->end
= UINT64_MAX
;
1920 context
->total
= UINT64_MAX
;
1922 if (context
->fdisk_context
) {
1923 fdisk_unref_context(context
->fdisk_context
);
1924 context
->fdisk_context
= NULL
;
1927 context_free_free_areas(context
);
1930 static int format_size_change(uint64_t from
, uint64_t to
, char **ret
) {
1933 if (from
!= UINT64_MAX
) {
1934 if (from
== to
|| to
== UINT64_MAX
)
1935 t
= strdup(FORMAT_BYTES(from
));
1937 t
= strjoin(FORMAT_BYTES(from
), " ", special_glyph(SPECIAL_GLYPH_ARROW
), " ", FORMAT_BYTES(to
));
1938 } else if (to
!= UINT64_MAX
)
1939 t
= strjoin(special_glyph(SPECIAL_GLYPH_ARROW
), " ", FORMAT_BYTES(to
));
1952 static const char *partition_label(const Partition
*p
) {
1956 return p
->new_label
;
1958 if (p
->current_label
)
1959 return p
->current_label
;
1961 return gpt_partition_type_uuid_to_string(p
->type_uuid
);
1964 static int context_dump_partitions(Context
*context
, const char *node
) {
1965 _cleanup_(table_unrefp
) Table
*t
= NULL
;
1966 uint64_t sum_padding
= 0, sum_size
= 0;
1970 if ((arg_json_format_flags
& JSON_FORMAT_OFF
) && context
->n_partitions
== 0) {
1971 log_info("Empty partition table.");
1975 t
= table_new("type", "label", "uuid", "file", "node", "offset", "old size", "raw size", "size", "old padding", "raw padding", "padding", "activity");
1979 if (!DEBUG_LOGGING
) {
1980 if (arg_json_format_flags
& JSON_FORMAT_OFF
)
1981 (void) table_set_display(t
, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
1982 (size_t) 8, (size_t) 11);
1984 (void) table_set_display(t
, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
1985 (size_t) 5, (size_t) 6, (size_t) 7, (size_t) 9, (size_t) 10, (size_t) 12);
1988 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 5), 100);
1989 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 6), 100);
1990 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 7), 100);
1991 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 8), 100);
1992 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 9), 100);
1993 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 10), 100);
1994 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 11), 100);
1996 LIST_FOREACH(partitions
, p
, context
->partitions
) {
1997 _cleanup_free_
char *size_change
= NULL
, *padding_change
= NULL
, *partname
= NULL
;
1998 char uuid_buffer
[ID128_UUID_STRING_MAX
];
1999 const char *label
, *activity
= NULL
;
2004 if (p
->current_size
== UINT64_MAX
)
2005 activity
= "create";
2006 else if (p
->current_size
!= p
->new_size
)
2007 activity
= "resize";
2009 label
= partition_label(p
);
2010 partname
= p
->partno
!= UINT64_MAX
? fdisk_partname(node
, p
->partno
+1) : NULL
;
2012 r
= format_size_change(p
->current_size
, p
->new_size
, &size_change
);
2016 r
= format_size_change(p
->current_padding
, p
->new_padding
, &padding_change
);
2020 if (p
->new_size
!= UINT64_MAX
)
2021 sum_size
+= p
->new_size
;
2022 if (p
->new_padding
!= UINT64_MAX
)
2023 sum_padding
+= p
->new_padding
;
2027 TABLE_STRING
, gpt_partition_type_uuid_to_string_harder(p
->type_uuid
, uuid_buffer
),
2028 TABLE_STRING
, empty_to_null(label
) ?: "-", TABLE_SET_COLOR
, empty_to_null(label
) ? NULL
: ansi_grey(),
2029 TABLE_UUID
, sd_id128_is_null(p
->new_uuid
) ? p
->current_uuid
: p
->new_uuid
,
2030 TABLE_STRING
, p
->definition_path
? basename(p
->definition_path
) : "-", TABLE_SET_COLOR
, p
->definition_path
? NULL
: ansi_grey(),
2031 TABLE_STRING
, partname
?: "-", TABLE_SET_COLOR
, partname
? NULL
: ansi_highlight(),
2032 TABLE_UINT64
, p
->offset
,
2033 TABLE_UINT64
, p
->current_size
== UINT64_MAX
? 0 : p
->current_size
,
2034 TABLE_UINT64
, p
->new_size
,
2035 TABLE_STRING
, size_change
, TABLE_SET_COLOR
, !p
->partitions_next
&& sum_size
> 0 ? ansi_underline() : NULL
,
2036 TABLE_UINT64
, p
->current_padding
== UINT64_MAX
? 0 : p
->current_padding
,
2037 TABLE_UINT64
, p
->new_padding
,
2038 TABLE_STRING
, padding_change
, TABLE_SET_COLOR
, !p
->partitions_next
&& sum_padding
> 0 ? ansi_underline() : NULL
,
2039 TABLE_STRING
, activity
?: "unchanged");
2041 return table_log_add_error(r
);
2044 if ((arg_json_format_flags
& JSON_FORMAT_OFF
) && (sum_padding
> 0 || sum_size
> 0)) {
2047 a
= strjoina(special_glyph(SPECIAL_GLYPH_SIGMA
), " = ", FORMAT_BYTES(sum_size
));
2048 b
= strjoina(special_glyph(SPECIAL_GLYPH_SIGMA
), " = ", FORMAT_BYTES(sum_padding
));
2066 return table_log_add_error(r
);
2069 return table_print_with_pager(t
, arg_json_format_flags
, arg_pager_flags
, arg_legend
);
2072 static void context_bar_char_process_partition(
2077 size_t *ret_start
) {
2079 uint64_t from
, to
, total
;
2090 assert(p
->offset
!= UINT64_MAX
);
2091 assert(p
->new_size
!= UINT64_MAX
);
2094 to
= from
+ p
->new_size
;
2096 assert(context
->end
>= context
->start
);
2097 total
= context
->end
- context
->start
;
2099 assert(from
>= context
->start
);
2100 assert(from
<= context
->end
);
2101 x
= (from
- context
->start
) * n
/ total
;
2103 assert(to
>= context
->start
);
2104 assert(to
<= context
->end
);
2105 y
= (to
- context
->start
) * n
/ total
;
2110 for (size_t i
= x
; i
< y
; i
++)
2116 static int partition_hint(const Partition
*p
, const char *node
, char **ret
) {
2117 _cleanup_free_
char *buf
= NULL
;
2118 char ids
[ID128_UUID_STRING_MAX
];
2122 /* Tries really hard to find a suitable description for this partition */
2124 if (p
->definition_path
) {
2125 buf
= strdup(basename(p
->definition_path
));
2129 label
= partition_label(p
);
2130 if (!isempty(label
)) {
2131 buf
= strdup(label
);
2135 if (p
->partno
!= UINT64_MAX
) {
2136 buf
= fdisk_partname(node
, p
->partno
+1);
2140 if (!sd_id128_is_null(p
->new_uuid
))
2142 else if (!sd_id128_is_null(p
->current_uuid
))
2143 id
= p
->current_uuid
;
2147 buf
= strdup(id128_to_uuid_string(id
, ids
));
2153 *ret
= TAKE_PTR(buf
);
2157 static int context_dump_partition_bar(Context
*context
, const char *node
) {
2158 _cleanup_free_ Partition
**bar
= NULL
;
2159 _cleanup_free_
size_t *start_array
= NULL
;
2160 Partition
*p
, *last
= NULL
;
2164 assert_se((c
= columns()) >= 2);
2165 c
-= 2; /* We do not use the leftmost and rightmost character cell */
2167 bar
= new0(Partition
*, c
);
2171 start_array
= new(size_t, context
->n_partitions
);
2175 LIST_FOREACH(partitions
, p
, context
->partitions
)
2176 context_bar_char_process_partition(context
, bar
, c
, p
, start_array
+ j
++);
2180 for (size_t i
= 0; i
< c
; i
++) {
2185 fputs(z
? ansi_green() : ansi_yellow(), stdout
);
2186 fputs(special_glyph(SPECIAL_GLYPH_DARK_SHADE
), stdout
);
2188 fputs(ansi_normal(), stdout
);
2189 fputs(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE
), stdout
);
2195 fputs(ansi_normal(), stdout
);
2198 for (size_t i
= 0; i
< context
->n_partitions
; i
++) {
2199 _cleanup_free_
char **line
= NULL
;
2201 line
= new0(char*, c
);
2206 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2207 _cleanup_free_
char *d
= NULL
;
2210 if (i
< context
->n_partitions
- j
) {
2212 if (line
[start_array
[j
-1]]) {
2215 /* Upgrade final corner to the right with a branch to the right */
2216 e
= startswith(line
[start_array
[j
-1]], special_glyph(SPECIAL_GLYPH_TREE_RIGHT
));
2218 d
= strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH
), e
);
2225 d
= strdup(special_glyph(SPECIAL_GLYPH_TREE_VERTICAL
));
2230 } else if (i
== context
->n_partitions
- j
) {
2231 _cleanup_free_
char *hint
= NULL
;
2233 (void) partition_hint(p
, node
, &hint
);
2235 if (streq_ptr(line
[start_array
[j
-1]], special_glyph(SPECIAL_GLYPH_TREE_VERTICAL
)))
2236 d
= strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH
), " ", strna(hint
));
2238 d
= strjoin(special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), " ", strna(hint
));
2245 free_and_replace(line
[start_array
[j
-1]], d
);
2253 fputs(line
[j
], stdout
);
2254 j
+= utf8_console_width(line
[j
]);
2263 for (j
= 0; j
< c
; j
++)
2270 static bool context_changed(const Context
*context
) {
2273 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2277 if (p
->allocated_to_area
)
2280 if (p
->new_size
!= p
->current_size
)
2287 static int context_wipe_range(Context
*context
, uint64_t offset
, uint64_t size
) {
2288 _cleanup_(blkid_free_probep
) blkid_probe probe
= NULL
;
2292 assert(offset
!= UINT64_MAX
);
2293 assert(size
!= UINT64_MAX
);
2295 probe
= blkid_new_probe();
2300 r
= blkid_probe_set_device(probe
, fdisk_get_devfd(context
->fdisk_context
), offset
, size
);
2302 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to allocate device probe for wiping.");
2305 if (blkid_probe_enable_superblocks(probe
, true) < 0 ||
2306 blkid_probe_set_superblocks_flags(probe
, BLKID_SUBLKS_MAGIC
|BLKID_SUBLKS_BADCSUM
) < 0 ||
2307 blkid_probe_enable_partitions(probe
, true) < 0 ||
2308 blkid_probe_set_partitions_flags(probe
, BLKID_PARTS_MAGIC
) < 0)
2309 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to enable superblock and partition probing.");
2313 r
= blkid_do_probe(probe
);
2315 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe for file systems.");
2320 if (blkid_do_wipe(probe
, false) < 0)
2321 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to wipe file system signature.");
2327 static int context_wipe_partition(Context
*context
, Partition
*p
) {
2332 assert(!PARTITION_EXISTS(p
)); /* Safety check: never wipe existing partitions */
2334 assert(p
->offset
!= UINT64_MAX
);
2335 assert(p
->new_size
!= UINT64_MAX
);
2337 r
= context_wipe_range(context
, p
->offset
, p
->new_size
);
2341 log_info("Successfully wiped file system signatures from future partition %" PRIu64
".", p
->partno
);
2345 static int context_discard_range(
2354 assert(offset
!= UINT64_MAX
);
2355 assert(size
!= UINT64_MAX
);
2360 assert_se((fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
2362 if (fstat(fd
, &st
) < 0)
2365 if (S_ISREG(st
.st_mode
)) {
2366 if (fallocate(fd
, FALLOC_FL_PUNCH_HOLE
|FALLOC_FL_KEEP_SIZE
, offset
, size
) < 0) {
2367 if (ERRNO_IS_NOT_SUPPORTED(errno
))
2376 if (S_ISBLK(st
.st_mode
)) {
2377 uint64_t range
[2], end
;
2379 range
[0] = round_up_size(offset
, 512);
2381 if (offset
> UINT64_MAX
- size
)
2384 end
= offset
+ size
;
2385 if (end
<= range
[0])
2388 range
[1] = round_down_size(end
- range
[0], 512);
2392 if (ioctl(fd
, BLKDISCARD
, range
) < 0) {
2393 if (ERRNO_IS_NOT_SUPPORTED(errno
))
2405 static int context_discard_partition(Context
*context
, Partition
*p
) {
2411 assert(p
->offset
!= UINT64_MAX
);
2412 assert(p
->new_size
!= UINT64_MAX
);
2413 assert(!PARTITION_EXISTS(p
)); /* Safety check: never discard existing partitions */
2418 r
= context_discard_range(context
, p
->offset
, p
->new_size
);
2419 if (r
== -EOPNOTSUPP
) {
2420 log_info("Storage does not support discard, not discarding data in future partition %" PRIu64
".", p
->partno
);
2424 /* Let's handle this gracefully: https://bugzilla.kernel.org/show_bug.cgi?id=211167 */
2425 log_info("Block device is busy, not discarding partition %" PRIu64
" because it probably is mounted.", p
->partno
);
2429 log_info("Partition %" PRIu64
" too short for discard, skipping.", p
->partno
);
2433 return log_error_errno(r
, "Failed to discard data for future partition %" PRIu64
".", p
->partno
);
2435 log_info("Successfully discarded data from future partition %" PRIu64
".", p
->partno
);
2439 static int context_discard_gap_after(Context
*context
, Partition
*p
) {
2440 uint64_t gap
, next
= UINT64_MAX
;
2445 assert(!p
|| (p
->offset
!= UINT64_MAX
&& p
->new_size
!= UINT64_MAX
));
2448 gap
= p
->offset
+ p
->new_size
;
2450 gap
= context
->start
;
2452 LIST_FOREACH(partitions
, q
, context
->partitions
) {
2456 assert(q
->offset
!= UINT64_MAX
);
2457 assert(q
->new_size
!= UINT64_MAX
);
2459 if (q
->offset
< gap
)
2462 if (next
== UINT64_MAX
|| q
->offset
< next
)
2466 if (next
== UINT64_MAX
) {
2467 next
= context
->end
;
2469 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition end beyond disk end.");
2472 assert(next
>= gap
);
2473 r
= context_discard_range(context
, gap
, next
- gap
);
2474 if (r
== -EOPNOTSUPP
) {
2476 log_info("Storage does not support discard, not discarding gap after partition %" PRIu64
".", p
->partno
);
2478 log_info("Storage does not support discard, not discarding gap at beginning of disk.");
2481 if (r
== 0) /* Too short */
2485 return log_error_errno(r
, "Failed to discard gap after partition %" PRIu64
".", p
->partno
);
2487 return log_error_errno(r
, "Failed to discard gap at beginning of disk.");
2491 log_info("Successfully discarded gap after partition %" PRIu64
".", p
->partno
);
2493 log_info("Successfully discarded gap at beginning of disk.");
2498 static int context_wipe_and_discard(Context
*context
, bool from_scratch
) {
2504 /* Wipe and discard the contents of all partitions we are about to create. We skip the discarding if
2505 * we were supposed to start from scratch anyway, as in that case we just discard the whole block
2506 * device in one go early on. */
2508 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2510 if (!p
->allocated_to_area
)
2513 r
= context_wipe_partition(context
, p
);
2517 if (!from_scratch
) {
2518 r
= context_discard_partition(context
, p
);
2522 r
= context_discard_gap_after(context
, p
);
2528 if (!from_scratch
) {
2529 r
= context_discard_gap_after(context
, NULL
);
2537 static int partition_encrypt(
2540 struct crypt_device
**ret_cd
,
2543 #if HAVE_LIBCRYPTSETUP
2544 _cleanup_(sym_crypt_freep
) struct crypt_device
*cd
= NULL
;
2545 _cleanup_(erase_and_freep
) void *volume_key
= NULL
;
2546 _cleanup_free_
char *dm_name
= NULL
, *vol
= NULL
;
2547 char suuid
[ID128_UUID_STRING_MAX
];
2548 size_t volume_key_size
= 256 / 8;
2553 assert(p
->encrypt
!= ENCRYPT_OFF
);
2555 log_debug("Encryption mode for partition %" PRIu64
": %s", p
->partno
, encrypt_mode_to_string(p
->encrypt
));
2557 r
= dlopen_cryptsetup();
2559 return log_error_errno(r
, "libcryptsetup not found, cannot encrypt: %m");
2561 if (asprintf(&dm_name
, "luks-repart-%08" PRIx64
, random_u64()) < 0)
2565 vol
= path_join("/dev/mapper/", dm_name
);
2570 r
= derive_uuid(p
->new_uuid
, "luks-uuid", &uuid
);
2574 log_info("Encrypting future partition %" PRIu64
"...", p
->partno
);
2576 volume_key
= malloc(volume_key_size
);
2580 r
= genuine_random_bytes(volume_key
, volume_key_size
, RANDOM_BLOCK
);
2582 return log_error_errno(r
, "Failed to generate volume key: %m");
2584 r
= sym_crypt_init(&cd
, node
);
2586 return log_error_errno(r
, "Failed to allocate libcryptsetup context: %m");
2588 cryptsetup_enable_logging(cd
);
2590 r
= sym_crypt_format(cd
,
2594 id128_to_uuid_string(uuid
, suuid
),
2597 &(struct crypt_params_luks2
) {
2598 .label
= strempty(p
->new_label
),
2599 .sector_size
= 512U,
2602 return log_error_errno(r
, "Failed to LUKS2 format future partition: %m");
2604 if (IN_SET(p
->encrypt
, ENCRYPT_KEY_FILE
, ENCRYPT_KEY_FILE_TPM2
)) {
2605 r
= sym_crypt_keyslot_add_by_volume_key(
2613 return log_error_errno(r
, "Failed to add LUKS2 key: %m");
2616 if (IN_SET(p
->encrypt
, ENCRYPT_TPM2
, ENCRYPT_KEY_FILE_TPM2
)) {
2618 _cleanup_(erase_and_freep
) char *base64_encoded
= NULL
;
2619 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
2620 _cleanup_(erase_and_freep
) void *secret
= NULL
;
2621 _cleanup_free_
void *blob
= NULL
, *hash
= NULL
;
2622 size_t secret_size
, blob_size
, hash_size
;
2626 r
= tpm2_seal(arg_tpm2_device
, arg_tpm2_pcr_mask
, &secret
, &secret_size
, &blob
, &blob_size
, &hash
, &hash_size
, &pcr_bank
);
2628 return log_error_errno(r
, "Failed to seal to TPM2: %m");
2630 r
= base64mem(secret
, secret_size
, &base64_encoded
);
2632 return log_error_errno(r
, "Failed to base64 encode secret key: %m");
2634 r
= cryptsetup_set_minimal_pbkdf(cd
);
2636 return log_error_errno(r
, "Failed to set minimal PBKDF: %m");
2638 keyslot
= sym_crypt_keyslot_add_by_volume_key(
2644 strlen(base64_encoded
));
2646 return log_error_errno(keyslot
, "Failed to add new TPM2 key to %s: %m", node
);
2648 r
= tpm2_make_luks2_json(keyslot
, arg_tpm2_pcr_mask
, pcr_bank
, blob
, blob_size
, hash
, hash_size
, &v
);
2650 return log_error_errno(r
, "Failed to prepare TPM2 JSON token object: %m");
2652 r
= cryptsetup_add_token_json(cd
, v
);
2654 return log_error_errno(r
, "Failed to add TPM2 JSON token to LUKS2 header: %m");
2656 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
2657 "Support for TPM2 enrollment not enabled.");
2661 r
= sym_crypt_activate_by_volume_key(
2666 arg_discard
? CRYPT_ACTIVATE_ALLOW_DISCARDS
: 0);
2668 return log_error_errno(r
, "Failed to activate LUKS superblock: %m");
2670 log_info("Successfully encrypted future partition %" PRIu64
".", p
->partno
);
2673 _cleanup_close_
int dev_fd
= -1;
2675 dev_fd
= open(vol
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
);
2677 return log_error_errno(errno
, "Failed to open LUKS volume '%s': %m", vol
);
2679 *ret_fd
= TAKE_FD(dev_fd
);
2683 *ret_cd
= TAKE_PTR(cd
);
2685 *ret_volume
= TAKE_PTR(vol
);
2689 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "libcryptsetup is not supported, cannot encrypt: %m");
2693 static int deactivate_luks(struct crypt_device
*cd
, const char *node
) {
2694 #if HAVE_LIBCRYPTSETUP
2702 /* udev or so might access out block device in the background while we are done. Let's hence force
2703 * detach the volume. We sync'ed before, hence this should be safe. */
2705 r
= sym_crypt_deactivate_by_name(cd
, basename(node
), CRYPT_DEACTIVATE_FORCE
);
2707 return log_error_errno(r
, "Failed to deactivate LUKS device: %m");
2715 static int context_copy_blocks(Context
*context
) {
2717 int whole_fd
= -1, r
;
2721 /* Copy in file systems on the block level */
2723 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2724 _cleanup_(sym_crypt_freep
) struct crypt_device
*cd
= NULL
;
2725 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
2726 _cleanup_free_
char *encrypted
= NULL
;
2727 _cleanup_close_
int encrypted_dev_fd
= -1;
2730 if (p
->copy_blocks_fd
< 0)
2736 if (PARTITION_EXISTS(p
)) /* Never copy over existing partitions */
2739 assert(p
->new_size
!= UINT64_MAX
);
2740 assert(p
->copy_blocks_size
!= UINT64_MAX
);
2741 assert(p
->new_size
>= p
->copy_blocks_size
);
2744 assert_se((whole_fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
2746 if (p
->encrypt
!= ENCRYPT_OFF
) {
2747 r
= loop_device_make(whole_fd
, O_RDWR
, p
->offset
, p
->new_size
, 0, &d
);
2749 return log_error_errno(r
, "Failed to make loopback device of future partition %" PRIu64
": %m", p
->partno
);
2751 r
= loop_device_flock(d
, LOCK_EX
);
2753 return log_error_errno(r
, "Failed to lock loopback device: %m");
2755 r
= partition_encrypt(p
, d
->node
, &cd
, &encrypted
, &encrypted_dev_fd
);
2757 return log_error_errno(r
, "Failed to encrypt device: %m");
2759 if (flock(encrypted_dev_fd
, LOCK_EX
) < 0)
2760 return log_error_errno(errno
, "Failed to lock LUKS device: %m");
2762 target_fd
= encrypted_dev_fd
;
2764 if (lseek(whole_fd
, p
->offset
, SEEK_SET
) == (off_t
) -1)
2765 return log_error_errno(errno
, "Failed to seek to partition offset: %m");
2767 target_fd
= whole_fd
;
2770 log_info("Copying in '%s' (%s) on block level into future partition %" PRIu64
".",
2771 p
->copy_blocks_path
, FORMAT_BYTES(p
->copy_blocks_size
), p
->partno
);
2773 r
= copy_bytes_full(p
->copy_blocks_fd
, target_fd
, p
->copy_blocks_size
, 0, NULL
, NULL
, NULL
, NULL
);
2775 return log_error_errno(r
, "Failed to copy in data from '%s': %m", p
->copy_blocks_path
);
2777 if (fsync(target_fd
) < 0)
2778 return log_error_errno(r
, "Failed to synchronize copied data blocks: %m");
2780 if (p
->encrypt
!= ENCRYPT_OFF
) {
2781 encrypted_dev_fd
= safe_close(encrypted_dev_fd
);
2783 r
= deactivate_luks(cd
, encrypted
);
2790 r
= loop_device_sync(d
);
2792 return log_error_errno(r
, "Failed to sync loopback device: %m");
2795 log_info("Copying in of '%s' on block level completed.", p
->copy_blocks_path
);
2801 static int do_copy_files(Partition
*p
, const char *fs
) {
2802 char **source
, **target
;
2808 STRV_FOREACH_PAIR(source
, target
, p
->copy_files
) {
2809 _cleanup_close_
int sfd
= -1, pfd
= -1, tfd
= -1;
2811 sfd
= chase_symlinks_and_open(*source
, arg_root
, CHASE_PREFIX_ROOT
|CHASE_WARN
, O_CLOEXEC
|O_NOCTTY
, NULL
);
2813 return log_error_errno(sfd
, "Failed to open source file '%s%s': %m", strempty(arg_root
), *source
);
2815 r
= fd_verify_regular(sfd
);
2818 return log_error_errno(r
, "Failed to check type of source file '%s': %m", *source
);
2820 /* We are looking at a directory */
2821 tfd
= chase_symlinks_and_open(*target
, fs
, CHASE_PREFIX_ROOT
|CHASE_WARN
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
, NULL
);
2823 _cleanup_free_
char *dn
= NULL
, *fn
= NULL
;
2826 return log_error_errno(tfd
, "Failed to open target directory '%s': %m", *target
);
2828 r
= path_extract_filename(*target
, &fn
);
2830 return log_error_errno(r
, "Failed to extract filename from '%s': %m", *target
);
2832 r
= path_extract_directory(*target
, &dn
);
2834 return log_error_errno(r
, "Failed to extract directory from '%s': %m", *target
);
2836 r
= mkdir_p_root(fs
, dn
, UID_INVALID
, GID_INVALID
, 0755);
2838 return log_error_errno(r
, "Failed to create parent directory '%s': %m", dn
);
2840 pfd
= chase_symlinks_and_open(dn
, fs
, CHASE_PREFIX_ROOT
|CHASE_WARN
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
, NULL
);
2842 return log_error_errno(pfd
, "Failed to open parent directory of target: %m");
2847 UID_INVALID
, GID_INVALID
,
2848 COPY_REFLINK
|COPY_MERGE
|COPY_REPLACE
|COPY_SIGINT
|COPY_HARDLINKS
|COPY_ALL_XATTRS
);
2853 UID_INVALID
, GID_INVALID
,
2854 COPY_REFLINK
|COPY_MERGE
|COPY_REPLACE
|COPY_SIGINT
|COPY_HARDLINKS
|COPY_ALL_XATTRS
);
2856 return log_error_errno(r
, "Failed to copy '%s' to '%s%s': %m", *source
, strempty(arg_root
), *target
);
2858 _cleanup_free_
char *dn
= NULL
, *fn
= NULL
;
2860 /* We are looking at a regular file */
2862 r
= path_extract_filename(*target
, &fn
);
2863 if (r
== -EADDRNOTAVAIL
|| r
== O_DIRECTORY
)
2864 return log_error_errno(SYNTHETIC_ERRNO(EISDIR
),
2865 "Target path '%s' refers to a directory, but source path '%s' refers to regular file, can't copy.", *target
, *source
);
2867 return log_error_errno(r
, "Failed to extract filename from '%s': %m", *target
);
2869 r
= path_extract_directory(*target
, &dn
);
2871 return log_error_errno(r
, "Failed to extract directory from '%s': %m", *target
);
2873 r
= mkdir_p_root(fs
, dn
, UID_INVALID
, GID_INVALID
, 0755);
2875 return log_error_errno(r
, "Failed to create parent directory: %m");
2877 pfd
= chase_symlinks_and_open(dn
, fs
, CHASE_PREFIX_ROOT
|CHASE_WARN
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
, NULL
);
2879 return log_error_errno(pfd
, "Failed to open parent directory of target: %m");
2881 tfd
= openat(pfd
, fn
, O_CREAT
|O_EXCL
|O_WRONLY
|O_CLOEXEC
, 0700);
2883 return log_error_errno(errno
, "Failed to create target file '%s': %m", *target
);
2885 r
= copy_bytes(sfd
, tfd
, UINT64_MAX
, COPY_REFLINK
|COPY_SIGINT
);
2887 return log_error_errno(r
, "Failed to copy '%s' to '%s%s': %m", *source
, strempty(arg_root
), *target
);
2889 (void) copy_xattr(sfd
, tfd
, COPY_ALL_XATTRS
);
2890 (void) copy_access(sfd
, tfd
);
2891 (void) copy_times(sfd
, tfd
, 0);
2898 static int do_make_directories(Partition
*p
, const char *fs
) {
2905 STRV_FOREACH(d
, p
->make_directories
) {
2907 r
= mkdir_p_root(fs
, *d
, UID_INVALID
, GID_INVALID
, 0755);
2909 return log_error_errno(r
, "Failed to create directory '%s' in file system: %m", *d
);
2915 static int partition_populate(Partition
*p
, const char *node
) {
2921 if (strv_isempty(p
->copy_files
) && strv_isempty(p
->make_directories
))
2924 log_info("Populating partition %" PRIu64
" with files.", p
->partno
);
2926 /* We copy in a child process, since we have to mount the fs for that, and we don't want that fs to
2927 * appear in the host namespace. Hence we fork a child that has its own file system namespace and
2928 * detached mount propagation. */
2930 r
= safe_fork("(sd-copy)", FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
|FORK_NEW_MOUNTNS
|FORK_MOUNTNS_SLAVE
, NULL
);
2934 static const char fs
[] = "/run/systemd/mount-root";
2935 /* This is a child process with its own mount namespace and propagation to host turned off */
2937 r
= mkdir_p(fs
, 0700);
2939 log_error_errno(r
, "Failed to create mount point: %m");
2940 _exit(EXIT_FAILURE
);
2943 if (mount_nofollow_verbose(LOG_ERR
, node
, fs
, p
->format
, MS_NOATIME
|MS_NODEV
|MS_NOEXEC
|MS_NOSUID
, NULL
) < 0)
2944 _exit(EXIT_FAILURE
);
2946 if (do_copy_files(p
, fs
) < 0)
2947 _exit(EXIT_FAILURE
);
2949 if (do_make_directories(p
, fs
) < 0)
2950 _exit(EXIT_FAILURE
);
2952 r
= syncfs_path(AT_FDCWD
, fs
);
2954 log_error_errno(r
, "Failed to synchronize written files: %m");
2955 _exit(EXIT_FAILURE
);
2958 _exit(EXIT_SUCCESS
);
2961 log_info("Successfully populated partition %" PRIu64
" with files.", p
->partno
);
2965 static int context_mkfs(Context
*context
) {
2971 /* Make a file system */
2973 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2974 _cleanup_(sym_crypt_freep
) struct crypt_device
*cd
= NULL
;
2975 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
2976 _cleanup_free_
char *encrypted
= NULL
;
2977 _cleanup_close_
int encrypted_dev_fd
= -1;
2984 if (PARTITION_EXISTS(p
)) /* Never format existing partitions */
2990 assert(p
->offset
!= UINT64_MAX
);
2991 assert(p
->new_size
!= UINT64_MAX
);
2994 assert_se((fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
2996 /* Loopback block devices are not only useful to turn regular files into block devices, but
2997 * also to cut out sections of block devices into new block devices. */
2999 r
= loop_device_make(fd
, O_RDWR
, p
->offset
, p
->new_size
, 0, &d
);
3001 return log_error_errno(r
, "Failed to make loopback device of future partition %" PRIu64
": %m", p
->partno
);
3003 r
= loop_device_flock(d
, LOCK_EX
);
3005 return log_error_errno(r
, "Failed to lock loopback device: %m");
3007 if (p
->encrypt
!= ENCRYPT_OFF
) {
3008 r
= partition_encrypt(p
, d
->node
, &cd
, &encrypted
, &encrypted_dev_fd
);
3010 return log_error_errno(r
, "Failed to encrypt device: %m");
3012 if (flock(encrypted_dev_fd
, LOCK_EX
) < 0)
3013 return log_error_errno(errno
, "Failed to lock LUKS device: %m");
3019 log_info("Formatting future partition %" PRIu64
".", p
->partno
);
3021 /* Calculate the UUID for the file system as HMAC-SHA256 of the string "file-system-uuid",
3022 * keyed off the partition UUID. */
3023 r
= derive_uuid(p
->new_uuid
, "file-system-uuid", &fs_uuid
);
3027 r
= make_filesystem(fsdev
, p
->format
, strempty(p
->new_label
), fs_uuid
, arg_discard
);
3029 encrypted_dev_fd
= safe_close(encrypted_dev_fd
);
3030 (void) deactivate_luks(cd
, encrypted
);
3034 log_info("Successfully formatted future partition %" PRIu64
".", p
->partno
);
3036 /* The file system is now created, no need to delay udev further */
3037 if (p
->encrypt
!= ENCRYPT_OFF
)
3038 if (flock(encrypted_dev_fd
, LOCK_UN
) < 0)
3039 return log_error_errno(errno
, "Failed to unlock LUKS device: %m");
3041 r
= partition_populate(p
, fsdev
);
3043 encrypted_dev_fd
= safe_close(encrypted_dev_fd
);
3044 (void) deactivate_luks(cd
, encrypted
);
3048 /* Note that we always sync explicitly here, since mkfs.fat doesn't do that on its own, and
3049 * if we don't sync before detaching a block device the in-flight sectors possibly won't hit
3052 if (p
->encrypt
!= ENCRYPT_OFF
) {
3053 if (fsync(encrypted_dev_fd
) < 0)
3054 return log_error_errno(r
, "Failed to synchronize LUKS volume: %m");
3055 encrypted_dev_fd
= safe_close(encrypted_dev_fd
);
3057 r
= deactivate_luks(cd
, encrypted
);
3065 r
= loop_device_sync(d
);
3067 return log_error_errno(r
, "Failed to sync loopback device: %m");
3073 static int partition_acquire_uuid(Context
*context
, Partition
*p
, sd_id128_t
*ret
) {
3075 sd_id128_t type_uuid
;
3077 } _packed_ plaintext
= {};
3079 unsigned char md
[SHA256_DIGEST_LENGTH
];
3091 /* Calculate a good UUID for the indicated partition. We want a certain degree of reproducibility,
3092 * hence we won't generate the UUIDs randomly. Instead we use a cryptographic hash (precisely:
3093 * HMAC-SHA256) to derive them from a single seed. The seed is generally the machine ID of the
3094 * installation we are processing, but if random behaviour is desired can be random, too. We use the
3095 * seed value as key for the HMAC (since the machine ID is something we generally don't want to leak)
3096 * and the partition type as plaintext. The partition type is suffixed with a counter (only for the
3097 * second and later partition of the same type) if we have more than one partition of the same
3098 * time. Or in other words:
3101 * SEED := /etc/machine-id
3103 * If first partition instance of type TYPE_UUID:
3104 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID)
3106 * For all later partition instances of type TYPE_UUID with INSTANCE being the LE64 encoded instance number:
3107 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID || INSTANCE)
3110 LIST_FOREACH(partitions
, q
, context
->partitions
) {
3114 if (!sd_id128_equal(p
->type_uuid
, q
->type_uuid
))
3120 plaintext
.type_uuid
= p
->type_uuid
;
3121 plaintext
.counter
= htole64(k
);
3123 if (!HMAC(EVP_sha256(),
3124 &context
->seed
, sizeof(context
->seed
),
3125 (const unsigned char*) &plaintext
, k
== 0 ? sizeof(sd_id128_t
) : sizeof(plaintext
),
3127 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE
), "SHA256 calculation failed.");
3129 /* Take the first half, mark it as v4 UUID */
3130 assert_cc(sizeof(result
.md
) == sizeof(result
.id
) * 2);
3131 result
.id
= id128_make_v4_uuid(result
.id
);
3133 /* Ensure this partition UUID is actually unique, and there's no remaining partition from an earlier run? */
3134 LIST_FOREACH(partitions
, q
, context
->partitions
) {
3138 if (sd_id128_in_set(result
.id
, q
->current_uuid
, q
->new_uuid
)) {
3139 log_warning("Partition UUID calculated from seed for partition %" PRIu64
" already used, reverting to randomized UUID.", p
->partno
);
3141 r
= sd_id128_randomize(&result
.id
);
3143 return log_error_errno(r
, "Failed to generate randomized UUID: %m");
3153 static int partition_acquire_label(Context
*context
, Partition
*p
, char **ret
) {
3154 _cleanup_free_
char *label
= NULL
;
3162 prefix
= gpt_partition_type_uuid_to_string(p
->type_uuid
);
3167 const char *ll
= label
?: prefix
;
3171 LIST_FOREACH(partitions
, q
, context
->partitions
) {
3175 if (streq_ptr(ll
, q
->current_label
) ||
3176 streq_ptr(ll
, q
->new_label
)) {
3185 label
= mfree(label
);
3186 if (asprintf(&label
, "%s-%u", prefix
, ++k
) < 0)
3191 label
= strdup(prefix
);
3196 *ret
= TAKE_PTR(label
);
3200 static int context_acquire_partition_uuids_and_labels(Context
*context
) {
3206 LIST_FOREACH(partitions
, p
, context
->partitions
) {
3207 /* Never touch foreign partitions */
3208 if (PARTITION_IS_FOREIGN(p
)) {
3209 p
->new_uuid
= p
->current_uuid
;
3211 if (p
->current_label
) {
3212 r
= free_and_strdup_warn(&p
->new_label
, strempty(p
->current_label
));
3220 if (!sd_id128_is_null(p
->current_uuid
))
3221 p
->new_uuid
= p
->current_uuid
; /* Never change initialized UUIDs */
3222 else if (sd_id128_is_null(p
->new_uuid
)) {
3223 /* Not explicitly set by user! */
3224 r
= partition_acquire_uuid(context
, p
, &p
->new_uuid
);
3229 if (!isempty(p
->current_label
)) {
3230 /* never change initialized labels */
3231 r
= free_and_strdup_warn(&p
->new_label
, p
->current_label
);
3234 } else if (!p
->new_label
) {
3235 /* Not explicitly set by user! */
3237 r
= partition_acquire_label(context
, p
, &p
->new_label
);
3246 static int set_gpt_flags(struct fdisk_partition
*q
, uint64_t flags
) {
3247 _cleanup_free_
char *a
= NULL
;
3249 for (unsigned i
= 0; i
< sizeof(flags
) * 8; i
++) {
3250 uint64_t bit
= UINT64_C(1) << i
;
3251 char buf
[DECIMAL_STR_MAX(unsigned)+1];
3253 if (!FLAGS_SET(flags
, bit
))
3256 xsprintf(buf
, "%u", i
);
3257 if (!strextend_with_separator(&a
, ",", buf
))
3261 return fdisk_partition_set_attrs(q
, a
);
3264 static uint64_t partition_merge_flags(Partition
*p
) {
3271 if (p
->no_auto
>= 0) {
3272 if (gpt_partition_type_knows_no_auto(p
->type_uuid
))
3273 SET_FLAG(f
, GPT_FLAG_NO_AUTO
, p
->no_auto
);
3275 char buffer
[ID128_UUID_STRING_MAX
];
3276 log_warning("Configured NoAuto=%s for partition type '%s' that doesn't support it, ignoring.",
3278 gpt_partition_type_uuid_to_string_harder(p
->type_uuid
, buffer
));
3282 if (p
->read_only
>= 0) {
3283 if (gpt_partition_type_knows_read_only(p
->type_uuid
))
3284 SET_FLAG(f
, GPT_FLAG_READ_ONLY
, p
->read_only
);
3286 char buffer
[ID128_UUID_STRING_MAX
];
3287 log_warning("Configured ReadOnly=%s for partition type '%s' that doesn't support it, ignoring.",
3288 yes_no(p
->read_only
),
3289 gpt_partition_type_uuid_to_string_harder(p
->type_uuid
, buffer
));
3293 if (p
->growfs
>= 0) {
3294 if (gpt_partition_type_knows_growfs(p
->type_uuid
))
3295 SET_FLAG(f
, GPT_FLAG_GROWFS
, p
->growfs
);
3297 char buffer
[ID128_UUID_STRING_MAX
];
3298 log_warning("Configured GrowFileSystem=%s for partition type '%s' that doesn't support it, ignoring.",
3300 gpt_partition_type_uuid_to_string_harder(p
->type_uuid
, buffer
));
3307 static int context_mangle_partitions(Context
*context
) {
3313 LIST_FOREACH(partitions
, p
, context
->partitions
) {
3317 assert(p
->new_size
!= UINT64_MAX
);
3318 assert(p
->offset
!= UINT64_MAX
);
3319 assert(p
->partno
!= UINT64_MAX
);
3321 if (PARTITION_EXISTS(p
)) {
3322 bool changed
= false;
3324 assert(p
->current_partition
);
3326 if (p
->new_size
!= p
->current_size
) {
3327 assert(p
->new_size
>= p
->current_size
);
3328 assert(p
->new_size
% 512 == 0);
3330 r
= fdisk_partition_size_explicit(p
->current_partition
, true);
3332 return log_error_errno(r
, "Failed to enable explicit sizing: %m");
3334 r
= fdisk_partition_set_size(p
->current_partition
, p
->new_size
/ 512);
3336 return log_error_errno(r
, "Failed to grow partition: %m");
3338 log_info("Growing existing partition %" PRIu64
".", p
->partno
);
3342 if (!sd_id128_equal(p
->new_uuid
, p
->current_uuid
)) {
3343 char buf
[ID128_UUID_STRING_MAX
];
3345 assert(!sd_id128_is_null(p
->new_uuid
));
3347 r
= fdisk_partition_set_uuid(p
->current_partition
, id128_to_uuid_string(p
->new_uuid
, buf
));
3349 return log_error_errno(r
, "Failed to set partition UUID: %m");
3351 log_info("Initializing UUID of existing partition %" PRIu64
".", p
->partno
);
3355 if (!streq_ptr(p
->new_label
, p
->current_label
)) {
3356 r
= fdisk_partition_set_name(p
->current_partition
, strempty(p
->new_label
));
3358 return log_error_errno(r
, "Failed to set partition label: %m");
3360 log_info("Setting partition label of existing partition %" PRIu64
".", p
->partno
);
3365 assert(!PARTITION_IS_FOREIGN(p
)); /* never touch foreign partitions */
3367 r
= fdisk_set_partition(context
->fdisk_context
, p
->partno
, p
->current_partition
);
3369 return log_error_errno(r
, "Failed to update partition: %m");
3372 _cleanup_(fdisk_unref_partitionp
) struct fdisk_partition
*q
= NULL
;
3373 _cleanup_(fdisk_unref_parttypep
) struct fdisk_parttype
*t
= NULL
;
3374 char ids
[ID128_UUID_STRING_MAX
];
3376 assert(!p
->new_partition
);
3377 assert(p
->offset
% 512 == 0);
3378 assert(p
->new_size
% 512 == 0);
3379 assert(!sd_id128_is_null(p
->new_uuid
));
3380 assert(p
->new_label
);
3382 t
= fdisk_new_parttype();
3386 r
= fdisk_parttype_set_typestr(t
, id128_to_uuid_string(p
->type_uuid
, ids
));
3388 return log_error_errno(r
, "Failed to initialize partition type: %m");
3390 q
= fdisk_new_partition();
3394 r
= fdisk_partition_set_type(q
, t
);
3396 return log_error_errno(r
, "Failed to set partition type: %m");
3398 r
= fdisk_partition_size_explicit(q
, true);
3400 return log_error_errno(r
, "Failed to enable explicit sizing: %m");
3402 r
= fdisk_partition_set_start(q
, p
->offset
/ 512);
3404 return log_error_errno(r
, "Failed to position partition: %m");
3406 r
= fdisk_partition_set_size(q
, p
->new_size
/ 512);
3408 return log_error_errno(r
, "Failed to grow partition: %m");
3410 r
= fdisk_partition_set_partno(q
, p
->partno
);
3412 return log_error_errno(r
, "Failed to set partition number: %m");
3414 r
= fdisk_partition_set_uuid(q
, id128_to_uuid_string(p
->new_uuid
, ids
));
3416 return log_error_errno(r
, "Failed to set partition UUID: %m");
3418 r
= fdisk_partition_set_name(q
, strempty(p
->new_label
));
3420 return log_error_errno(r
, "Failed to set partition label: %m");
3422 /* Merge the no auto + read only + growfs setting with the literal flags, and set them for the partition */
3423 r
= set_gpt_flags(q
, partition_merge_flags(p
));
3425 return log_error_errno(r
, "Failed to set GPT partition flags: %m");
3427 log_info("Adding new partition %" PRIu64
" to partition table.", p
->partno
);
3429 r
= fdisk_add_partition(context
->fdisk_context
, q
, NULL
);
3431 return log_error_errno(r
, "Failed to add partition: %m");
3433 assert(!p
->new_partition
);
3434 p
->new_partition
= TAKE_PTR(q
);
3441 static int context_write_partition_table(
3444 bool from_scratch
) {
3446 _cleanup_(fdisk_unref_tablep
) struct fdisk_table
*original_table
= NULL
;
3451 if (arg_pretty
> 0 ||
3452 (arg_pretty
< 0 && isatty(STDOUT_FILENO
) > 0) ||
3453 !FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
)) {
3455 (void) context_dump_partitions(context
, node
);
3459 if (arg_json_format_flags
& JSON_FORMAT_OFF
)
3460 (void) context_dump_partition_bar(context
, node
);
3465 if (!from_scratch
&& !context_changed(context
)) {
3466 log_info("No changes.");
3471 log_notice("Refusing to repartition, please re-run with --dry-run=no.");
3475 log_info("Applying changes.");
3478 r
= context_wipe_range(context
, 0, context
->total
);
3482 log_info("Wiped block device.");
3484 r
= context_discard_range(context
, 0, context
->total
);
3485 if (r
== -EOPNOTSUPP
)
3486 log_info("Storage does not support discard, not discarding entire block device data.");
3488 return log_error_errno(r
, "Failed to discard entire block device: %m");
3490 log_info("Discarded entire block device.");
3493 r
= fdisk_get_partitions(context
->fdisk_context
, &original_table
);
3495 return log_error_errno(r
, "Failed to acquire partition table: %m");
3497 /* Wipe fs signatures and discard sectors where the new partitions are going to be placed and in the
3498 * gaps between partitions, just to be sure. */
3499 r
= context_wipe_and_discard(context
, from_scratch
);
3503 r
= context_copy_blocks(context
);
3507 r
= context_mkfs(context
);
3511 r
= context_mangle_partitions(context
);
3515 log_info("Writing new partition table.");
3517 r
= fdisk_write_disklabel(context
->fdisk_context
);
3519 return log_error_errno(r
, "Failed to write partition table: %m");
3521 capable
= blockdev_partscan_enabled(fdisk_get_devfd(context
->fdisk_context
));
3522 if (capable
== -ENOTBLK
)
3523 log_debug("Not telling kernel to reread partition table, since we are not operating on a block device.");
3524 else if (capable
< 0)
3525 return log_error_errno(capable
, "Failed to check if block device supports partition scanning: %m");
3526 else if (capable
> 0) {
3527 log_info("Telling kernel to reread partition table.");
3530 r
= fdisk_reread_partition_table(context
->fdisk_context
);
3532 r
= fdisk_reread_changes(context
->fdisk_context
, original_table
);
3534 return log_error_errno(r
, "Failed to reread partition table: %m");
3536 log_notice("Not telling kernel to reread partition table, because selected image does not support kernel partition block devices.");
3538 log_info("All done.");
3543 static int context_read_seed(Context
*context
, const char *root
) {
3548 if (!sd_id128_is_null(context
->seed
))
3551 if (!arg_randomize
) {
3552 _cleanup_close_
int fd
= -1;
3554 fd
= chase_symlinks_and_open("/etc/machine-id", root
, CHASE_PREFIX_ROOT
, O_RDONLY
|O_CLOEXEC
, NULL
);
3556 log_info("No machine ID set, using randomized partition UUIDs.");
3558 return log_error_errno(fd
, "Failed to determine machine ID of image: %m");
3560 r
= id128_read_fd(fd
, ID128_PLAIN_OR_UNINIT
, &context
->seed
);
3561 if (r
== -ENOMEDIUM
)
3562 log_info("No machine ID set, using randomized partition UUIDs.");
3564 return log_error_errno(r
, "Failed to parse machine ID of image: %m");
3570 r
= sd_id128_randomize(&context
->seed
);
3572 return log_error_errno(r
, "Failed to generate randomized seed: %m");
3577 static int context_factory_reset(Context
*context
, bool from_scratch
) {
3584 if (arg_factory_reset
<= 0)
3587 if (from_scratch
) /* Nothing to reset if we start from scratch */
3591 log_notice("Refusing to factory reset, please re-run with --dry-run=no.");
3595 log_info("Applying factory reset.");
3597 LIST_FOREACH(partitions
, p
, context
->partitions
) {
3599 if (!p
->factory_reset
|| !PARTITION_EXISTS(p
))
3602 assert(p
->partno
!= UINT64_MAX
);
3604 log_info("Removing partition %" PRIu64
" for factory reset.", p
->partno
);
3606 r
= fdisk_delete_partition(context
->fdisk_context
, p
->partno
);
3608 return log_error_errno(r
, "Failed to remove partition %" PRIu64
": %m", p
->partno
);
3614 log_info("Factory reset requested, but no partitions to delete found.");
3618 r
= fdisk_write_disklabel(context
->fdisk_context
);
3620 return log_error_errno(r
, "Failed to write disk label: %m");
3622 log_info("Successfully deleted %zu partitions.", n
);
3626 static int context_can_factory_reset(Context
*context
) {
3631 LIST_FOREACH(partitions
, p
, context
->partitions
)
3632 if (p
->factory_reset
&& PARTITION_EXISTS(p
))
3638 static int resolve_copy_blocks_auto_candidate(
3639 dev_t partition_devno
,
3640 sd_id128_t partition_type_uuid
,
3641 dev_t restrict_devno
,
3642 sd_id128_t
*ret_uuid
) {
3644 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
3645 _cleanup_free_
char *p
= NULL
;
3646 _cleanup_close_
int fd
= -1;
3647 const char *pttype
, *t
;
3648 sd_id128_t pt_parsed
, u
;
3655 /* Checks if the specified partition has the specified GPT type UUID, and is located on the specified
3656 * 'restrict_devno' device. The type check is particularly relevant if we have Verity volume which is
3657 * backed by two separate partitions: the data and the hash partitions, and we need to find the right
3658 * one of the two. */
3660 r
= block_get_whole_disk(partition_devno
, &whole_devno
);
3662 return log_error_errno(
3664 "Unable to determine containing block device of partition %u:%u: %m",
3665 major(partition_devno
), minor(partition_devno
));
3667 if (restrict_devno
!= (dev_t
) -1 &&
3668 restrict_devno
!= whole_devno
)
3669 return log_error_errno(
3670 SYNTHETIC_ERRNO(EPERM
),
3671 "Partition %u:%u is located outside of block device %u:%u, refusing.",
3672 major(partition_devno
), minor(partition_devno
),
3673 major(restrict_devno
), minor(restrict_devno
));
3675 r
= device_path_make_major_minor(S_IFBLK
, whole_devno
, &p
);
3677 return log_error_errno(r
, "Failed to convert block device to device node path: %m");
3679 fd
= open(p
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
3681 return log_error_errno(r
, "Failed to open '%s': %m", p
);
3683 if (fstat(fd
, &st
) < 0)
3684 return log_error_errno(r
, "Failed to stat '%s': %m", p
);
3686 if (!S_ISBLK(st
.st_mode
) || st
.st_rdev
!= whole_devno
)
3687 return log_error_errno(
3688 SYNTHETIC_ERRNO(EPERM
),
3689 "Opened and determined block device don't match, refusing.");
3691 b
= blkid_new_probe();
3696 r
= blkid_probe_set_device(b
, fd
, 0, 0);
3698 return log_error_errno(errno_or_else(ENOMEM
), "Failed to open block device '%s': %m", p
);
3700 (void) blkid_probe_enable_partitions(b
, 1);
3701 (void) blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
3704 r
= blkid_do_safeprobe(b
);
3705 if (IN_SET(r
, -2, 1)) { /* nothing found or ambiguous result */
3706 log_debug("Didn't find partition table on block device '%s'.", p
);
3710 return log_error_errno(errno_or_else(EIO
), "Unable to probe for partition table of '%s': %m", p
);
3712 (void) blkid_probe_lookup_value(b
, "PTTYPE", &pttype
, NULL
);
3713 if (!streq_ptr(pttype
, "gpt")) {
3714 log_debug("Didn't find a GPT partition table on '%s'.", p
);
3719 pl
= blkid_probe_get_partitions(b
);
3721 return log_error_errno(errno_or_else(EIO
), "Unable read partition table of '%s': %m", p
);
3724 pp
= blkid_partlist_devno_to_partition(pl
, partition_devno
);
3726 log_debug("Partition %u:%u has no matching partition table entry on '%s'.",
3727 major(partition_devno
), minor(partition_devno
), p
);
3731 t
= blkid_partition_get_type_string(pp
);
3733 log_debug("Partition %u:%u has no type on '%s'.",
3734 major(partition_devno
), minor(partition_devno
), p
);
3738 r
= sd_id128_from_string(t
, &pt_parsed
);
3740 log_debug_errno(r
, "Failed to parse partition type \"%s\": %m", t
);
3744 if (!sd_id128_equal(pt_parsed
, partition_type_uuid
)) {
3745 log_debug("Partition %u:%u has non-matching partition type " SD_ID128_FORMAT_STR
" (needed: " SD_ID128_FORMAT_STR
"), ignoring.",
3746 major(partition_devno
), minor(partition_devno
),
3747 SD_ID128_FORMAT_VAL(pt_parsed
), SD_ID128_FORMAT_VAL(partition_type_uuid
));
3751 t
= blkid_partition_get_uuid(pp
);
3753 log_debug("Partition %u:%u has no UUID.",
3754 major(partition_devno
), minor(partition_devno
));
3758 r
= sd_id128_from_string(t
, &u
);
3760 log_debug_errno(r
, "Failed to parse partition UUID \"%s\": %m", t
);
3764 log_debug("Automatically found partition %u:%u of right type " SD_ID128_FORMAT_STR
".",
3765 major(partition_devno
), minor(partition_devno
),
3766 SD_ID128_FORMAT_VAL(pt_parsed
));
3774 static int find_backing_devno(
3779 _cleanup_free_
char *resolved
= NULL
;
3784 r
= chase_symlinks(path
, root
, CHASE_PREFIX_ROOT
, &resolved
, NULL
);
3788 r
= path_is_mount_point(resolved
, NULL
, 0);
3791 if (r
== 0) /* Not a mount point, then it's not a partition of its own, let's not automatically use it. */
3794 r
= get_block_device(resolved
, ret
);
3797 if (r
== 0) /* Not backed by physical file system, we can't use this */
3803 static int resolve_copy_blocks_auto(
3804 sd_id128_t type_uuid
,
3806 dev_t restrict_devno
,
3808 sd_id128_t
*ret_uuid
) {
3810 const char *try1
= NULL
, *try2
= NULL
;
3811 char p
[SYS_BLOCK_PATH_MAX("/slaves")];
3812 _cleanup_(closedirp
) DIR *d
= NULL
;
3813 sd_id128_t found_uuid
= SD_ID128_NULL
;
3814 dev_t devno
, found
= 0;
3819 /* Enforce some security restrictions: CopyBlocks=auto should not be an avenue to get outside of the
3820 * --root=/--image= confinement. Specifically, refuse CopyBlocks= in combination with --root= at all,
3821 * and restrict block device references in the --image= case to loopback block device we set up.
3823 * restrict_devno contain the dev_t of the loop back device we operate on in case of --image=, and
3824 * thus declares which device (and its partition subdevices) we shall limit access to. If
3825 * restrict_devno is zero no device probing access shall be allowed at all (used for --root=) and if
3826 * it is (dev_t) -1 then free access shall be allowed (if neither switch is used). */
3828 if (restrict_devno
== 0)
3829 return log_error_errno(SYNTHETIC_ERRNO(EPERM
),
3830 "Automatic discovery of backing block devices not permitted in --root= mode, refusing.");
3832 /* Handles CopyBlocks=auto, and finds the right source partition to copy from. We look for matching
3833 * partitions in the host, using the appropriate directory as key and ensuring that the partition
3836 if (gpt_partition_type_is_root(type_uuid
))
3838 else if (gpt_partition_type_is_usr(type_uuid
))
3840 else if (gpt_partition_type_is_root_verity(type_uuid
))
3842 else if (gpt_partition_type_is_usr_verity(type_uuid
))
3844 else if (sd_id128_equal(type_uuid
, GPT_ESP
)) {
3847 } else if (sd_id128_equal(type_uuid
, GPT_XBOOTLDR
))
3850 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
3851 "Partition type " SD_ID128_FORMAT_STR
" not supported from automatic source block device discovery.",
3852 SD_ID128_FORMAT_VAL(type_uuid
));
3854 r
= find_backing_devno(try1
, root
, &devno
);
3855 if (r
== -ENOENT
&& try2
)
3856 r
= find_backing_devno(try2
, root
, &devno
);
3858 return log_error_errno(r
, "Failed to resolve automatic CopyBlocks= path for partition type " SD_ID128_FORMAT_STR
", sorry: %m",
3859 SD_ID128_FORMAT_VAL(type_uuid
));
3861 xsprintf_sys_block_path(p
, "/slaves", devno
);
3867 _cleanup_free_
char *q
= NULL
, *t
= NULL
;
3872 de
= readdir_no_dot(d
);
3875 return log_error_errno(errno
, "Failed to read directory '%s': %m", p
);
3880 if (!IN_SET(de
->d_type
, DT_LNK
, DT_UNKNOWN
))
3883 q
= path_join(p
, de
->d_name
, "/dev");
3887 r
= read_one_line_file(q
, &t
);
3889 return log_error_errno(r
, "Failed to read %s: %m", q
);
3891 r
= parse_dev(t
, &sl
);
3893 log_debug_errno(r
, "Failed to parse %s, ignoring: %m", q
);
3896 if (major(sl
) == 0) {
3897 log_debug_errno(r
, "Device backing %s is special, ignoring: %m", q
);
3901 r
= resolve_copy_blocks_auto_candidate(sl
, type_uuid
, restrict_devno
, &u
);
3905 /* We found a matching one! */
3907 return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ
),
3908 "Multiple matching partitions found, refusing.");
3914 } else if (errno
!= ENOENT
)
3915 return log_error_errno(errno
, "Failed open %s: %m", p
);
3917 r
= resolve_copy_blocks_auto_candidate(devno
, type_uuid
, restrict_devno
, &found_uuid
);
3925 return log_error_errno(SYNTHETIC_ERRNO(ENXIO
),
3926 "Unable to automatically discover suitable partition to copy blocks from.");
3928 r
= device_path_make_major_minor(S_IFBLK
, found
, ret_path
);
3930 return log_error_errno(r
, "Failed to convert dev_t to device node path: %m");
3933 *ret_uuid
= found_uuid
;
3938 static int context_open_copy_block_paths(
3941 dev_t restrict_devno
) {
3948 LIST_FOREACH(partitions
, p
, context
->partitions
) {
3949 _cleanup_close_
int source_fd
= -1;
3950 _cleanup_free_
char *opened
= NULL
;
3951 sd_id128_t uuid
= SD_ID128_NULL
;
3955 assert(p
->copy_blocks_fd
< 0);
3956 assert(p
->copy_blocks_size
== UINT64_MAX
);
3958 if (PARTITION_EXISTS(p
)) /* Never copy over partitions that already exist! */
3961 if (p
->copy_blocks_path
) {
3963 source_fd
= chase_symlinks_and_open(p
->copy_blocks_path
, root
, CHASE_PREFIX_ROOT
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
, &opened
);
3965 return log_error_errno(source_fd
, "Failed to open '%s': %m", p
->copy_blocks_path
);
3967 if (fstat(source_fd
, &st
) < 0)
3968 return log_error_errno(errno
, "Failed to stat block copy file '%s': %m", opened
);
3970 if (!S_ISREG(st
.st_mode
) && restrict_devno
!= (dev_t
) -1)
3971 return log_error_errno(SYNTHETIC_ERRNO(EPERM
),
3972 "Copying from block device node is not permitted in --image=/--root= mode, refusing.");
3974 } else if (p
->copy_blocks_auto
) {
3976 r
= resolve_copy_blocks_auto(p
->type_uuid
, root
, restrict_devno
, &opened
, &uuid
);
3980 source_fd
= open(opened
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
3982 return log_error_errno(errno
, "Failed to open automatically determined source block copy device '%s': %m", opened
);
3984 if (fstat(source_fd
, &st
) < 0)
3985 return log_error_errno(errno
, "Failed to stat block copy file '%s': %m", opened
);
3987 /* If we found it automatically, it must be a block device, let's enforce that */
3988 if (!S_ISBLK(st
.st_mode
))
3989 return log_error_errno(SYNTHETIC_ERRNO(EBADF
),
3990 "Automatically detected source block copy device '%s' is not a block device, refusing: %m", opened
);
3994 if (S_ISDIR(st
.st_mode
)) {
3995 _cleanup_free_
char *bdev
= NULL
;
3997 /* If the file is a directory, automatically find the backing block device */
3999 if (major(st
.st_dev
) != 0)
4000 r
= device_path_make_major_minor(S_IFBLK
, st
.st_dev
, &bdev
);
4004 /* Special support for btrfs */
4006 r
= btrfs_get_block_device_fd(source_fd
, &devt
);
4008 return btrfs_log_dev_root(LOG_ERR
, r
, opened
);
4010 return log_error_errno(r
, "Unable to determine backing block device of '%s': %m", opened
);
4012 r
= device_path_make_major_minor(S_IFBLK
, devt
, &bdev
);
4015 return log_error_errno(r
, "Failed to determine block device path for block device backing '%s': %m", opened
);
4017 safe_close(source_fd
);
4019 source_fd
= open(bdev
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
4021 return log_error_errno(errno
, "Failed to open block device '%s': %m", bdev
);
4023 if (fstat(source_fd
, &st
) < 0)
4024 return log_error_errno(errno
, "Failed to stat block device '%s': %m", bdev
);
4026 if (!S_ISBLK(st
.st_mode
))
4027 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK
), "Block device '%s' is not actually a block device, refusing.", bdev
);
4030 if (S_ISREG(st
.st_mode
))
4032 else if (S_ISBLK(st
.st_mode
)) {
4033 if (ioctl(source_fd
, BLKGETSIZE64
, &size
) != 0)
4034 return log_error_errno(errno
, "Failed to determine size of block device to copy from: %m");
4036 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", opened
);
4039 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "File to copy bytes from '%s' has zero size, refusing.", opened
);
4040 if (size
% 512 != 0)
4041 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "File to copy bytes from '%s' has size that is not multiple of 512, refusing.", opened
);
4043 p
->copy_blocks_fd
= TAKE_FD(source_fd
);
4044 p
->copy_blocks_size
= size
;
4046 free_and_replace(p
->copy_blocks_path
, opened
);
4048 /* When copying from an existing partition copy that partitions UUID if none is configured explicitly */
4049 if (sd_id128_is_null(p
->new_uuid
) && !sd_id128_is_null(uuid
))
4056 static int help(void) {
4057 _cleanup_free_
char *link
= NULL
;
4060 r
= terminal_urlify_man("systemd-repart", "1", &link
);
4064 printf("%s [OPTIONS...] [DEVICE]\n"
4065 "\n%sGrow and add partitions to partition table.%s\n\n"
4066 " -h --help Show this help\n"
4067 " --version Show package version\n"
4068 " --no-pager Do not pipe output into a pager\n"
4069 " --no-legend Do not show the headers and footers\n"
4070 " --dry-run=BOOL Whether to run dry-run operation\n"
4071 " --empty=MODE One of refuse, allow, require, force, create; controls\n"
4072 " how to handle empty disks lacking partition tables\n"
4073 " --discard=BOOL Whether to discard backing blocks for new partitions\n"
4074 " --pretty=BOOL Whether to show pretty summary before doing changes\n"
4075 " --factory-reset=BOOL Whether to remove data partitions before recreating\n"
4077 " --can-factory-reset Test whether factory reset is defined\n"
4078 " --root=PATH Operate relative to root path\n"
4079 " --image=PATH Operate relative to image file\n"
4080 " --definitions=DIR Find partition definitions in specified directory\n"
4081 " --key-file=PATH Key to use when encrypting partitions\n"
4082 " --tpm2-device=PATH Path to TPM2 device node to use\n"
4083 " --tpm2-pcrs=PCR1+PCR2+PCR3+…\n"
4084 " TPM2 PCR indexes to use for TPM2 enrollment\n"
4085 " --seed=UUID 128bit seed UUID to derive all UUIDs from\n"
4086 " --size=BYTES Grow loopback file to specified size\n"
4087 " --json=pretty|short|off\n"
4088 " Generate JSON output\n"
4089 "\nSee the %s for details.\n",
4090 program_invocation_short_name
,
4098 static int parse_argv(int argc
, char *argv
[]) {
4101 ARG_VERSION
= 0x100,
4108 ARG_CAN_FACTORY_RESET
,
4121 static const struct option options
[] = {
4122 { "help", no_argument
, NULL
, 'h' },
4123 { "version", no_argument
, NULL
, ARG_VERSION
},
4124 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
4125 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
4126 { "dry-run", required_argument
, NULL
, ARG_DRY_RUN
},
4127 { "empty", required_argument
, NULL
, ARG_EMPTY
},
4128 { "discard", required_argument
, NULL
, ARG_DISCARD
},
4129 { "factory-reset", required_argument
, NULL
, ARG_FACTORY_RESET
},
4130 { "can-factory-reset", no_argument
, NULL
, ARG_CAN_FACTORY_RESET
},
4131 { "root", required_argument
, NULL
, ARG_ROOT
},
4132 { "image", required_argument
, NULL
, ARG_IMAGE
},
4133 { "seed", required_argument
, NULL
, ARG_SEED
},
4134 { "pretty", required_argument
, NULL
, ARG_PRETTY
},
4135 { "definitions", required_argument
, NULL
, ARG_DEFINITIONS
},
4136 { "size", required_argument
, NULL
, ARG_SIZE
},
4137 { "json", required_argument
, NULL
, ARG_JSON
},
4138 { "key-file", required_argument
, NULL
, ARG_KEY_FILE
},
4139 { "tpm2-device", required_argument
, NULL
, ARG_TPM2_DEVICE
},
4140 { "tpm2-pcrs", required_argument
, NULL
, ARG_TPM2_PCRS
},
4144 int c
, r
, dry_run
= -1;
4149 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
4160 arg_pager_flags
|= PAGER_DISABLE
;
4168 r
= parse_boolean_argument("--dry-run=", optarg
, &arg_dry_run
);
4174 if (isempty(optarg
) || streq(optarg
, "refuse"))
4175 arg_empty
= EMPTY_REFUSE
;
4176 else if (streq(optarg
, "allow"))
4177 arg_empty
= EMPTY_ALLOW
;
4178 else if (streq(optarg
, "require"))
4179 arg_empty
= EMPTY_REQUIRE
;
4180 else if (streq(optarg
, "force"))
4181 arg_empty
= EMPTY_FORCE
;
4182 else if (streq(optarg
, "create")) {
4183 arg_empty
= EMPTY_CREATE
;
4186 dry_run
= false; /* Imply --dry-run=no if we create the loopback file
4187 * anew. After all we cannot really break anyone's
4188 * partition tables that way. */
4190 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
4191 "Failed to parse --empty= parameter: %s", optarg
);
4195 r
= parse_boolean_argument("--discard=", optarg
, &arg_discard
);
4200 case ARG_FACTORY_RESET
:
4201 r
= parse_boolean_argument("--factory-reset=", optarg
, NULL
);
4204 arg_factory_reset
= r
;
4207 case ARG_CAN_FACTORY_RESET
:
4208 arg_can_factory_reset
= true;
4212 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_root
);
4218 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_image
);
4224 if (isempty(optarg
)) {
4225 arg_seed
= SD_ID128_NULL
;
4226 arg_randomize
= false;
4227 } else if (streq(optarg
, "random"))
4228 arg_randomize
= true;
4230 r
= sd_id128_from_string(optarg
, &arg_seed
);
4232 return log_error_errno(r
, "Failed to parse seed: %s", optarg
);
4234 arg_randomize
= false;
4240 r
= parse_boolean_argument("--pretty=", optarg
, NULL
);
4246 case ARG_DEFINITIONS
:
4247 r
= parse_path_argument(optarg
, false, &arg_definitions
);
4253 uint64_t parsed
, rounded
;
4255 if (streq(optarg
, "auto")) {
4256 arg_size
= UINT64_MAX
;
4257 arg_size_auto
= true;
4261 r
= parse_size(optarg
, 1024, &parsed
);
4263 return log_error_errno(r
, "Failed to parse --size= parameter: %s", optarg
);
4265 rounded
= round_up_size(parsed
, 4096);
4267 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Specified image size too small, refusing.");
4268 if (rounded
== UINT64_MAX
)
4269 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Specified image size too large, refusing.");
4271 if (rounded
!= parsed
)
4272 log_warning("Specified size is not a multiple of 4096, rounding up automatically. (%" PRIu64
" → %" PRIu64
")",
4276 arg_size_auto
= false;
4281 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
4287 case ARG_KEY_FILE
: {
4288 _cleanup_(erase_and_freep
) char *k
= NULL
;
4291 r
= read_full_file_full(
4292 AT_FDCWD
, optarg
, UINT64_MAX
, SIZE_MAX
,
4293 READ_FULL_FILE_SECURE
|READ_FULL_FILE_WARN_WORLD_READABLE
|READ_FULL_FILE_CONNECT_SOCKET
,
4297 return log_error_errno(r
, "Failed to read key file '%s': %m", optarg
);
4299 erase_and_free(arg_key
);
4300 arg_key
= TAKE_PTR(k
);
4305 case ARG_TPM2_DEVICE
: {
4306 _cleanup_free_
char *device
= NULL
;
4308 if (streq(optarg
, "list"))
4309 return tpm2_list_devices();
4311 if (!streq(optarg
, "auto")) {
4312 device
= strdup(optarg
);
4317 free(arg_tpm2_device
);
4318 arg_tpm2_device
= TAKE_PTR(device
);
4322 case ARG_TPM2_PCRS
: {
4325 if (isempty(optarg
)) {
4326 arg_tpm2_pcr_mask
= 0;
4330 r
= tpm2_parse_pcrs(optarg
, &mask
);
4334 if (arg_tpm2_pcr_mask
== UINT32_MAX
)
4335 arg_tpm2_pcr_mask
= mask
;
4337 arg_tpm2_pcr_mask
|= mask
;
4346 assert_not_reached();
4349 if (argc
- optind
> 1)
4350 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
4351 "Expected at most one argument, the path to the block device.");
4353 if (arg_factory_reset
> 0 && IN_SET(arg_empty
, EMPTY_FORCE
, EMPTY_REQUIRE
, EMPTY_CREATE
))
4354 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
4355 "Combination of --factory-reset=yes and --empty=force/--empty=require/--empty=create is invalid.");
4357 if (arg_can_factory_reset
)
4358 arg_dry_run
= true; /* When --can-factory-reset is specified we don't make changes, hence
4359 * non-dry-run mode makes no sense. Thus, imply dry run mode so that we
4360 * open things strictly read-only. */
4361 else if (dry_run
>= 0)
4362 arg_dry_run
= dry_run
;
4364 if (arg_empty
== EMPTY_CREATE
&& (arg_size
== UINT64_MAX
&& !arg_size_auto
))
4365 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
4366 "If --empty=create is specified, --size= must be specified, too.");
4368 if (arg_image
&& arg_root
)
4369 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Please specify either --root= or --image=, the combination of both is not supported.");
4370 else if (!arg_image
&& !arg_root
&& in_initrd()) {
4372 /* By default operate on /sysusr/ or /sysroot/ when invoked in the initrd. We prefer the
4373 * former, if it is mounted, so that we have deterministic behaviour on systems where /usr/
4374 * is vendor-supplied but the root fs formatted on first boot. */
4375 r
= path_is_mount_point("/sysusr/usr", NULL
, 0);
4377 if (r
< 0 && r
!= -ENOENT
)
4378 log_debug_errno(r
, "Unable to determine whether /sysusr/usr is a mount point, assuming it is not: %m");
4380 arg_root
= strdup("/sysroot");
4382 arg_root
= strdup("/sysusr");
4387 arg_node
= argc
> optind
? argv
[optind
] : NULL
;
4389 if (IN_SET(arg_empty
, EMPTY_FORCE
, EMPTY_REQUIRE
, EMPTY_CREATE
) && !arg_node
&& !arg_image
)
4390 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
4391 "A path to a device node or loopback file must be specified when --empty=force, --empty=require or --empty=create are used.");
4393 if (arg_tpm2_pcr_mask
== UINT32_MAX
)
4394 arg_tpm2_pcr_mask
= TPM2_PCR_MASK_DEFAULT
;
4399 static int parse_proc_cmdline_factory_reset(void) {
4403 if (arg_factory_reset
>= 0) /* Never override what is specified on the process command line */
4406 if (!in_initrd()) /* Never honour kernel command line factory reset request outside of the initrd */
4409 r
= proc_cmdline_get_bool("systemd.factory_reset", &b
);
4411 return log_error_errno(r
, "Failed to parse systemd.factory_reset kernel command line argument: %m");
4413 arg_factory_reset
= b
;
4416 log_notice("Honouring factory reset requested via kernel command line.");
4422 static int parse_efi_variable_factory_reset(void) {
4423 _cleanup_free_
char *value
= NULL
;
4426 if (arg_factory_reset
>= 0) /* Never override what is specified on the process command line */
4429 if (!in_initrd()) /* Never honour EFI variable factory reset request outside of the initrd */
4432 r
= efi_get_variable_string(EFI_SYSTEMD_VARIABLE(FactoryReset
), &value
);
4433 if (r
== -ENOENT
|| ERRNO_IS_NOT_SUPPORTED(r
))
4436 return log_error_errno(r
, "Failed to read EFI variable FactoryReset: %m");
4438 r
= parse_boolean(value
);
4440 return log_error_errno(r
, "Failed to parse EFI variable FactoryReset: %m");
4442 arg_factory_reset
= r
;
4444 log_notice("Factory reset requested via EFI variable FactoryReset.");
4449 static int remove_efi_variable_factory_reset(void) {
4452 r
= efi_set_variable(EFI_SYSTEMD_VARIABLE(FactoryReset
), NULL
, 0);
4453 if (r
== -ENOENT
|| ERRNO_IS_NOT_SUPPORTED(r
))
4456 return log_error_errno(r
, "Failed to remove EFI variable FactoryReset: %m");
4458 log_info("Successfully unset EFI variable FactoryReset.");
4462 static int acquire_root_devno(
4469 _cleanup_free_
char *found_path
= NULL
;
4470 dev_t devno
, fd_devno
= MODE_INVALID
;
4471 _cleanup_close_
int fd
= -1;
4479 fd
= chase_symlinks_and_open(p
, root
, CHASE_PREFIX_ROOT
, mode
, &found_path
);
4483 if (fstat(fd
, &st
) < 0)
4486 if (S_ISREG(st
.st_mode
)) {
4487 *ret
= TAKE_PTR(found_path
);
4488 *ret_fd
= TAKE_FD(fd
);
4492 if (S_ISBLK(st
.st_mode
)) {
4493 /* Refuse referencing explicit block devices if a root dir is specified, after all we should
4494 * not be able to leave the image the root path constrains us to. */
4498 fd_devno
= devno
= st
.st_rdev
;
4499 } else if (S_ISDIR(st
.st_mode
)) {
4502 if (major(devno
) == 0) {
4503 r
= btrfs_get_block_device_fd(fd
, &devno
);
4504 if (r
== -ENOTTY
) /* not btrfs */
4512 /* From dm-crypt to backing partition */
4513 r
= block_get_originating(devno
, &devno
);
4515 log_debug_errno(r
, "Device '%s' has no dm-crypt/dm-verity device, no need to look for underlying block device.", p
);
4517 log_debug_errno(r
, "Failed to find underlying block device for '%s', ignoring: %m", p
);
4519 /* From partition to whole disk containing it */
4520 r
= block_get_whole_disk(devno
, &devno
);
4522 log_debug_errno(r
, "Failed to find whole disk block device for '%s', ignoring: %m", p
);
4524 r
= device_path_make_canonical(S_IFBLK
, devno
, ret
);
4526 return log_debug_errno(r
, "Failed to determine canonical path for '%s': %m", p
);
4528 /* Only if we still look at the same block device we can reuse the fd. Otherwise return an
4529 * invalidated fd. */
4530 *ret_fd
= fd_devno
!= MODE_INVALID
&& fd_devno
== devno
? TAKE_FD(fd
) : -1;
4534 static int find_root(char **ret
, int *ret_fd
) {
4542 if (arg_empty
== EMPTY_CREATE
) {
4543 _cleanup_close_
int fd
= -1;
4544 _cleanup_free_
char *s
= NULL
;
4546 s
= strdup(arg_node
);
4550 fd
= open(arg_node
, O_RDONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
|O_NOFOLLOW
, 0666);
4552 return log_error_errno(errno
, "Failed to create '%s': %m", arg_node
);
4555 *ret_fd
= TAKE_FD(fd
);
4559 /* Note that we don't specify a root argument here: if the user explicitly configured a node
4560 * we'll take it relative to the host, not the image */
4561 r
= acquire_root_devno(arg_node
, NULL
, O_RDONLY
|O_CLOEXEC
, ret
, ret_fd
);
4563 return btrfs_log_dev_root(LOG_ERR
, r
, arg_node
);
4565 return log_error_errno(r
, "Failed to open file or determine backing device of %s: %m", arg_node
);
4570 assert(IN_SET(arg_empty
, EMPTY_REFUSE
, EMPTY_ALLOW
));
4572 /* Let's search for the root device. We look for two cases here: first in /, and then in /usr. The
4573 * latter we check for cases where / is a tmpfs and only /usr is an actual persistent block device
4574 * (think: volatile setups) */
4576 FOREACH_STRING(p
, "/", "/usr") {
4578 r
= acquire_root_devno(p
, arg_root
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
, ret
, ret_fd
);
4581 return btrfs_log_dev_root(LOG_ERR
, r
, p
);
4583 return log_error_errno(r
, "Failed to determine backing device of %s: %m", p
);
4588 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "Failed to discover root block device.");
4591 static int resize_pt(int fd
) {
4592 _cleanup_(fdisk_unref_contextp
) struct fdisk_context
*c
= NULL
;
4595 /* After resizing the backing file we need to resize the partition table itself too, so that it takes
4596 * possession of the enlarged backing file. For this it suffices to open the device with libfdisk and
4597 * immediately write it again, with no changes. */
4599 c
= fdisk_new_context();
4603 r
= fdisk_assign_device(c
, FORMAT_PROC_FD_PATH(fd
), 0);
4605 return log_error_errno(r
, "Failed to open device '%s': %m", FORMAT_PROC_FD_PATH(fd
));
4607 r
= fdisk_has_label(c
);
4609 return log_error_errno(r
, "Failed to determine whether disk '%s' has a disk label: %m", FORMAT_PROC_FD_PATH(fd
));
4611 log_debug("Not resizing partition table, as there currently is none.");
4615 r
= fdisk_write_disklabel(c
);
4617 return log_error_errno(r
, "Failed to write resized partition table: %m");
4619 log_info("Resized partition table.");
4623 static int resize_backing_fd(
4624 const char *node
, /* The primary way we access the disk image to operate on */
4625 int *fd
, /* An O_RDONLY fd referring to that inode */
4626 const char *backing_file
, /* If the above refers to a loopback device, the backing regular file for that, which we can grow */
4627 LoopDevice
*loop_device
) {
4629 _cleanup_close_
int writable_fd
= -1;
4630 uint64_t current_size
;
4637 if (arg_size
== UINT64_MAX
) /* Nothing to do */
4641 /* Open the file if we haven't opened it yet. Note that we open it read-only here, just to
4642 * keep a reference to the file we can pass around. */
4643 *fd
= open(node
, O_RDONLY
|O_CLOEXEC
);
4645 return log_error_errno(errno
, "Failed to open '%s' in order to adjust size: %m", node
);
4648 if (fstat(*fd
, &st
) < 0)
4649 return log_error_errno(errno
, "Failed to stat '%s': %m", node
);
4651 if (S_ISBLK(st
.st_mode
)) {
4653 return log_error_errno(SYNTHETIC_ERRNO(EBADF
), "Cannot resize block device '%s'.", node
);
4655 assert(loop_device
);
4657 if (ioctl(*fd
, BLKGETSIZE64
, ¤t_size
) < 0)
4658 return log_error_errno(errno
, "Failed to determine size of block device %s: %m", node
);
4660 r
= stat_verify_regular(&st
);
4662 return log_error_errno(r
, "Specified path '%s' is not a regular file or loopback block device, cannot resize: %m", node
);
4664 assert(!backing_file
);
4665 assert(!loop_device
);
4666 current_size
= st
.st_size
;
4669 if (current_size
>= arg_size
) {
4670 log_info("File '%s' already is of requested size or larger, not growing. (%s >= %s)",
4671 node
, FORMAT_BYTES(current_size
), FORMAT_BYTES(arg_size
));
4675 if (S_ISBLK(st
.st_mode
)) {
4676 assert(backing_file
);
4678 /* This is a loopback device. We can't really grow those directly, but we can grow the
4679 * backing file, hence let's do that. */
4681 writable_fd
= open(backing_file
, O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
);
4682 if (writable_fd
< 0)
4683 return log_error_errno(errno
, "Failed to open backing file '%s': %m", backing_file
);
4685 if (fstat(writable_fd
, &st
) < 0)
4686 return log_error_errno(errno
, "Failed to stat() backing file '%s': %m", backing_file
);
4688 r
= stat_verify_regular(&st
);
4690 return log_error_errno(r
, "Backing file '%s' of block device is not a regular file: %m", backing_file
);
4692 if ((uint64_t) st
.st_size
!= current_size
)
4693 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
4694 "Size of backing file '%s' of loopback block device '%s' don't match, refusing.",
4695 node
, backing_file
);
4697 assert(S_ISREG(st
.st_mode
));
4698 assert(!backing_file
);
4700 /* The file descriptor is read-only. In order to grow the file we need to have a writable fd. We
4701 * reopen the file for that temporarily. We keep the writable fd only open for this operation though,
4702 * as fdisk can't accept it anyway. */
4704 writable_fd
= fd_reopen(*fd
, O_WRONLY
|O_CLOEXEC
);
4705 if (writable_fd
< 0)
4706 return log_error_errno(writable_fd
, "Failed to reopen backing file '%s' writable: %m", node
);
4710 if (fallocate(writable_fd
, 0, 0, arg_size
) < 0) {
4711 if (!ERRNO_IS_NOT_SUPPORTED(errno
))
4712 return log_error_errno(errno
, "Failed to grow '%s' from %s to %s by allocation: %m",
4713 node
, FORMAT_BYTES(current_size
), FORMAT_BYTES(arg_size
));
4715 /* Fallback to truncation, if fallocate() is not supported. */
4716 log_debug("Backing file system does not support fallocate(), falling back to ftruncate().");
4718 if (current_size
== 0) /* Likely regular file just created by us */
4719 log_info("Allocated %s for '%s'.", FORMAT_BYTES(arg_size
), node
);
4721 log_info("File '%s' grown from %s to %s by allocation.",
4722 node
, FORMAT_BYTES(current_size
), FORMAT_BYTES(arg_size
));
4728 if (ftruncate(writable_fd
, arg_size
) < 0)
4729 return log_error_errno(errno
, "Failed to grow '%s' from %s to %s by truncation: %m",
4730 node
, FORMAT_BYTES(current_size
), FORMAT_BYTES(arg_size
));
4732 if (current_size
== 0) /* Likely regular file just created by us */
4733 log_info("Sized '%s' to %s.", node
, FORMAT_BYTES(arg_size
));
4735 log_info("File '%s' grown from %s to %s by truncation.",
4736 node
, FORMAT_BYTES(current_size
), FORMAT_BYTES(arg_size
));
4739 r
= resize_pt(writable_fd
);
4744 r
= loop_device_refresh_size(loop_device
, UINT64_MAX
, arg_size
);
4746 return log_error_errno(r
, "Failed to update loop device size: %m");
4752 static int determine_auto_size(Context
*c
) {
4753 uint64_t sum
= round_up_size(GPT_METADATA_SIZE
, 4096);
4758 LIST_FOREACH(partitions
, p
, c
->partitions
) {
4764 m
= partition_min_size_with_padding(p
);
4765 if (m
> UINT64_MAX
- sum
)
4766 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "Image would grow too large, refusing.");
4771 if (c
->total
!= UINT64_MAX
)
4772 /* Image already allocated? Then show its size. */
4773 log_info("Automatically determined minimal disk image size as %s, current image size is %s.",
4774 FORMAT_BYTES(sum
), FORMAT_BYTES(c
->total
));
4776 /* If the image is being created right now, then it has no previous size, suppress any comment about it hence. */
4777 log_info("Automatically determined minimal disk image size as %s.",
4784 static int run(int argc
, char *argv
[]) {
4785 _cleanup_(loop_device_unrefp
) LoopDevice
*loop_device
= NULL
;
4786 _cleanup_(decrypted_image_unrefp
) DecryptedImage
*decrypted_image
= NULL
;
4787 _cleanup_(umount_and_rmdir_and_freep
) char *mounted_dir
= NULL
;
4788 _cleanup_(context_freep
) Context
* context
= NULL
;
4789 _cleanup_free_
char *node
= NULL
;
4790 _cleanup_close_
int backing_fd
= -1;
4791 bool from_scratch
, node_is_our_loop
= false;
4794 log_show_color(true);
4795 log_parse_environment();
4798 r
= parse_argv(argc
, argv
);
4802 r
= parse_proc_cmdline_factory_reset();
4806 r
= parse_efi_variable_factory_reset();
4813 /* Mount this strictly read-only: we shall modify the partition table, not the file
4815 r
= mount_image_privately_interactively(
4817 DISSECT_IMAGE_MOUNT_READ_ONLY
|
4818 (arg_node
? DISSECT_IMAGE_DEVICE_READ_ONLY
: 0) | /* If a different node to make changes to is specified let's open the device in read-only mode) */
4819 DISSECT_IMAGE_GPT_ONLY
|
4820 DISSECT_IMAGE_RELAX_VAR_CHECK
|
4821 DISSECT_IMAGE_USR_NO_ROOT
|
4822 DISSECT_IMAGE_REQUIRE_ROOT
,
4829 arg_root
= strdup(mounted_dir
);
4834 arg_node
= strdup(loop_device
->node
);
4838 /* Remember that the device we are about to manipulate is actually the one we
4839 * allocated here, and thus to increase its backing file we know what to do */
4840 node_is_our_loop
= true;
4844 context
= context_new(arg_seed
);
4848 r
= context_read_definitions(context
, arg_definitions
, arg_root
);
4852 if (context
->n_partitions
<= 0 && arg_empty
== EMPTY_REFUSE
) {
4853 log_info("Didn't find any partition definition files, nothing to do.");
4857 r
= find_root(&node
, &backing_fd
);
4861 if (arg_size
!= UINT64_MAX
) {
4862 r
= resize_backing_fd(
4865 node_is_our_loop
? arg_image
: NULL
,
4866 node_is_our_loop
? loop_device
: NULL
);
4871 r
= context_load_partition_table(context
, node
, &backing_fd
);
4872 if (r
== -EHWPOISON
)
4873 return 77; /* Special return value which means "Not GPT, so not doing anything". This isn't
4874 * really an error when called at boot. */
4877 from_scratch
= r
> 0; /* Starting from scratch */
4879 if (arg_can_factory_reset
) {
4880 r
= context_can_factory_reset(context
);
4884 return EXIT_FAILURE
;
4889 r
= context_factory_reset(context
, from_scratch
);
4893 /* We actually did a factory reset! */
4894 r
= remove_efi_variable_factory_reset();
4898 /* Reload the reduced partition table */
4899 context_unload_partition_table(context
);
4900 r
= context_load_partition_table(context
, node
, &backing_fd
);
4906 (void) context_dump_partitions(context
, node
);
4910 r
= context_read_seed(context
, arg_root
);
4914 /* Open all files to copy blocks from now, since we want to take their size into consideration */
4915 r
= context_open_copy_block_paths(
4918 loop_device
? loop_device
->devno
: /* if --image= is specified, only allow partitions on the loopback device */
4919 arg_root
&& !arg_image
? 0 : /* if --root= is specified, don't accept any block device */
4920 (dev_t
) -1); /* if neither is specified, make no restrictions */
4924 if (arg_size_auto
) {
4925 r
= determine_auto_size(context
);
4929 /* Flush out everything again, and let's grow the file first, then start fresh */
4930 context_unload_partition_table(context
);
4932 assert_se(arg_size
!= UINT64_MAX
);
4933 r
= resize_backing_fd(
4936 node_is_our_loop
? arg_image
: NULL
,
4937 node_is_our_loop
? loop_device
: NULL
);
4941 r
= context_load_partition_table(context
, node
, &backing_fd
);
4946 /* First try to fit new partitions in, dropping by priority until it fits */
4948 uint64_t largest_free_area
;
4950 if (context_allocate_partitions(context
, &largest_free_area
))
4951 break; /* Success! */
4953 if (!context_drop_one_priority(context
)) {
4954 r
= log_error_errno(SYNTHETIC_ERRNO(ENOSPC
),
4955 "Can't fit requested partitions into available free space (%s), refusing.",
4956 FORMAT_BYTES(largest_free_area
));
4957 determine_auto_size(context
);
4962 /* Now assign free space according to the weight logic */
4963 r
= context_grow_partitions(context
);
4967 /* Now calculate where each partition gets placed */
4968 context_place_partitions(context
);
4970 /* Make sure each partition has a unique UUID and unique label */
4971 r
= context_acquire_partition_uuids_and_labels(context
);
4975 r
= context_write_partition_table(context
, node
, from_scratch
);
4982 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);