1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #if HAVE_VALGRIND_MEMCHECK_H
4 #include <valgrind/memcheck.h>
11 #include <linux/loop.h>
13 #include <sys/ioctl.h>
16 #include <openssl/hmac.h>
17 #include <openssl/sha.h>
21 #include "alloc-util.h"
22 #include "blkid-util.h"
23 #include "blockdev-util.h"
24 #include "btrfs-util.h"
25 #include "conf-files.h"
26 #include "conf-parser.h"
27 #include "cryptsetup-util.h"
30 #include "errno-util.h"
33 #include "format-table.h"
34 #include "format-util.h"
37 #include "id128-util.h"
40 #include "locale-util.h"
41 #include "loop-util.h"
42 #include "main-func.h"
44 #include "mkfs-util.h"
45 #include "mount-util.h"
46 #include "parse-util.h"
47 #include "path-util.h"
48 #include "pretty-print.h"
49 #include "proc-cmdline.h"
50 #include "process-util.h"
51 #include "random-util.h"
52 #include "resize-fs.h"
53 #include "sort-util.h"
54 #include "specifier.h"
55 #include "stat-util.h"
56 #include "stdio-util.h"
57 #include "string-util.h"
59 #include "terminal-util.h"
60 #include "user-util.h"
63 /* If not configured otherwise use a minimal partition size of 10M */
64 #define DEFAULT_MIN_SIZE (10*1024*1024)
66 /* Hard lower limit for new partition sizes */
67 #define HARD_MIN_SIZE 4096
69 /* libfdisk takes off slightly more than 1M of the disk size when creating a GPT disk label */
70 #define GPT_METADATA_SIZE (1044*1024)
72 /* LUKS2 takes off 16M of the partition size with its metadata by default */
73 #define LUKS2_METADATA_SIZE (16*1024*1024)
75 #if !HAVE_LIBCRYPTSETUP
77 static inline void sym_crypt_free(struct crypt_device
* cd
) {}
78 static inline void sym_crypt_freep(struct crypt_device
** cd
) {}
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_definitions
= NULL
;
98 static bool arg_discard
= true;
99 static bool arg_can_factory_reset
= false;
100 static int arg_factory_reset
= -1;
101 static sd_id128_t arg_seed
= SD_ID128_NULL
;
102 static bool arg_randomize
= false;
103 static int arg_pretty
= -1;
104 static uint64_t arg_size
= UINT64_MAX
;
105 static bool arg_size_auto
= false;
106 static bool arg_json
= false;
107 static JsonFormatFlags arg_json_format_flags
= 0;
108 static void *arg_key
= NULL
;
109 static size_t arg_key_size
= 0;
111 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
112 STATIC_DESTRUCTOR_REGISTER(arg_definitions
, freep
);
113 STATIC_DESTRUCTOR_REGISTER(arg_key
, erase_and_freep
);
115 typedef struct Partition Partition
;
116 typedef struct FreeArea FreeArea
;
117 typedef struct Context Context
;
120 char *definition_path
;
122 sd_id128_t type_uuid
;
123 sd_id128_t current_uuid
, new_uuid
;
124 char *current_label
, *new_label
;
130 uint32_t weight
, padding_weight
;
132 uint64_t current_size
, new_size
;
133 uint64_t size_min
, size_max
;
135 uint64_t current_padding
, new_padding
;
136 uint64_t padding_min
, padding_max
;
141 struct fdisk_partition
*current_partition
;
142 struct fdisk_partition
*new_partition
;
143 FreeArea
*padding_area
;
144 FreeArea
*allocated_to_area
;
146 char *copy_blocks_path
;
148 uint64_t copy_blocks_size
;
154 LIST_FIELDS(Partition
, partitions
);
157 #define PARTITION_IS_FOREIGN(p) (!(p)->definition_path)
158 #define PARTITION_EXISTS(p) (!!(p)->current_partition)
167 LIST_HEAD(Partition
, partitions
);
170 FreeArea
**free_areas
;
171 size_t n_free_areas
, n_allocated_free_areas
;
173 uint64_t start
, end
, total
;
175 struct fdisk_context
*fdisk_context
;
180 static uint64_t round_down_size(uint64_t v
, uint64_t p
) {
184 static uint64_t round_up_size(uint64_t v
, uint64_t p
) {
186 v
= DIV_ROUND_UP(v
, p
);
188 if (v
> UINT64_MAX
/ p
)
189 return UINT64_MAX
; /* overflow */
194 static Partition
*partition_new(void) {
197 p
= new(Partition
, 1);
204 .current_size
= UINT64_MAX
,
205 .new_size
= UINT64_MAX
,
206 .size_min
= UINT64_MAX
,
207 .size_max
= UINT64_MAX
,
208 .current_padding
= UINT64_MAX
,
209 .new_padding
= UINT64_MAX
,
210 .padding_min
= UINT64_MAX
,
211 .padding_max
= UINT64_MAX
,
212 .partno
= UINT64_MAX
,
213 .offset
= UINT64_MAX
,
214 .copy_blocks_fd
= -1,
215 .copy_blocks_size
= UINT64_MAX
,
221 static Partition
* partition_free(Partition
*p
) {
225 free(p
->current_label
);
227 free(p
->definition_path
);
229 if (p
->current_partition
)
230 fdisk_unref_partition(p
->current_partition
);
231 if (p
->new_partition
)
232 fdisk_unref_partition(p
->new_partition
);
234 free(p
->copy_blocks_path
);
235 safe_close(p
->copy_blocks_fd
);
238 strv_free(p
->copy_files
);
243 static Partition
* partition_unlink_and_free(Context
*context
, Partition
*p
) {
247 LIST_REMOVE(partitions
, context
->partitions
, p
);
249 assert(context
->n_partitions
> 0);
250 context
->n_partitions
--;
252 return partition_free(p
);
255 DEFINE_TRIVIAL_CLEANUP_FUNC(Partition
*, partition_free
);
257 static Context
*context_new(sd_id128_t seed
) {
260 context
= new(Context
, 1);
264 *context
= (Context
) {
274 static void context_free_free_areas(Context
*context
) {
277 for (size_t i
= 0; i
< context
->n_free_areas
; i
++)
278 free(context
->free_areas
[i
]);
280 context
->free_areas
= mfree(context
->free_areas
);
281 context
->n_free_areas
= 0;
282 context
->n_allocated_free_areas
= 0;
285 static Context
*context_free(Context
*context
) {
289 while (context
->partitions
)
290 partition_unlink_and_free(context
, context
->partitions
);
291 assert(context
->n_partitions
== 0);
293 context_free_free_areas(context
);
295 if (context
->fdisk_context
)
296 fdisk_unref_context(context
->fdisk_context
);
298 return mfree(context
);
301 DEFINE_TRIVIAL_CLEANUP_FUNC(Context
*, context_free
);
303 static int context_add_free_area(
311 assert(!after
|| !after
->padding_area
);
313 if (!GREEDY_REALLOC(context
->free_areas
, context
->n_allocated_free_areas
, context
->n_free_areas
+ 1))
316 a
= new(FreeArea
, 1);
325 context
->free_areas
[context
->n_free_areas
++] = a
;
328 after
->padding_area
= a
;
333 static bool context_drop_one_priority(Context
*context
) {
334 int32_t priority
= 0;
338 LIST_FOREACH(partitions
, p
, context
->partitions
) {
341 if (p
->priority
< priority
)
343 if (p
->priority
== priority
) {
344 exists
= exists
|| PARTITION_EXISTS(p
);
348 priority
= p
->priority
;
349 exists
= PARTITION_EXISTS(p
);
352 /* Refuse to drop partitions with 0 or negative priorities or partitions of priorities that have at
353 * least one existing priority */
354 if (priority
<= 0 || exists
)
357 LIST_FOREACH(partitions
, p
, context
->partitions
) {
358 if (p
->priority
< priority
)
365 log_info("Can't fit partition %s of priority %" PRIi32
", dropping.", p
->definition_path
, p
->priority
);
371 static uint64_t partition_min_size(const Partition
*p
) {
374 /* Calculate the disk space we really need at minimum for this partition. If the partition already
375 * exists the current size is what we really need. If it doesn't exist yet refuse to allocate less
378 * DEFAULT_MIN_SIZE is the default SizeMin= we configure if nothing else is specified. */
380 if (PARTITION_IS_FOREIGN(p
)) {
381 /* Don't allow changing size of partitions not managed by us */
382 assert(p
->current_size
!= UINT64_MAX
);
383 return p
->current_size
;
386 sz
= p
->current_size
!= UINT64_MAX
? p
->current_size
: HARD_MIN_SIZE
;
388 if (!PARTITION_EXISTS(p
)) {
392 d
+= round_up_size(LUKS2_METADATA_SIZE
, 4096);
394 if (p
->copy_blocks_size
!= UINT64_MAX
)
395 d
+= round_up_size(p
->copy_blocks_size
, 4096);
396 else if (p
->format
|| p
->encrypt
) {
399 /* If we shall synthesize a file system, take minimal fs size into account (assumed to be 4K if not known) */
400 f
= p
->format
? minimal_size_by_fs_name(p
->format
) : UINT64_MAX
;
401 d
+= f
== UINT64_MAX
? 4096 : f
;
408 return MAX(p
->size_min
!= UINT64_MAX
? p
->size_min
: DEFAULT_MIN_SIZE
, sz
);
411 static uint64_t partition_max_size(const Partition
*p
) {
412 /* Calculate how large the partition may become at max. This is generally the configured maximum
413 * size, except when it already exists and is larger than that. In that case it's the existing size,
414 * since we never want to shrink partitions. */
416 if (PARTITION_IS_FOREIGN(p
)) {
417 /* Don't allow changing size of partitions not managed by us */
418 assert(p
->current_size
!= UINT64_MAX
);
419 return p
->current_size
;
422 if (p
->current_size
!= UINT64_MAX
)
423 return MAX(p
->current_size
, p
->size_max
);
428 static uint64_t partition_min_size_with_padding(const Partition
*p
) {
431 /* Calculate the disk space we need for this partition plus any free space coming after it. This
432 * takes user configured padding into account as well as any additional whitespace needed to align
433 * the next partition to 4K again. */
435 sz
= partition_min_size(p
);
437 if (p
->padding_min
!= UINT64_MAX
)
438 sz
+= p
->padding_min
;
440 if (PARTITION_EXISTS(p
)) {
441 /* If the partition wasn't aligned, add extra space so that any we might add will be aligned */
442 assert(p
->offset
!= UINT64_MAX
);
443 return round_up_size(p
->offset
+ sz
, 4096) - p
->offset
;
446 /* If this is a new partition we'll place it aligned, hence we just need to round up the required size here */
447 return round_up_size(sz
, 4096);
450 static uint64_t free_area_available(const FreeArea
*a
) {
453 /* Determines how much of this free area is not allocated yet */
455 assert(a
->size
>= a
->allocated
);
456 return a
->size
- a
->allocated
;
459 static uint64_t free_area_available_for_new_partitions(const FreeArea
*a
) {
462 /* Similar to free_area_available(), but takes into account that the required size and padding of the
463 * preceding partition is honoured. */
465 avail
= free_area_available(a
);
467 uint64_t need
, space
;
469 need
= partition_min_size_with_padding(a
->after
);
471 assert(a
->after
->offset
!= UINT64_MAX
);
472 assert(a
->after
->current_size
!= UINT64_MAX
);
474 space
= round_up_size(a
->after
->offset
+ a
->after
->current_size
, 4096) - a
->after
->offset
+ avail
;
484 static int free_area_compare(FreeArea
*const *a
, FreeArea
*const*b
) {
485 return CMP(free_area_available_for_new_partitions(*a
),
486 free_area_available_for_new_partitions(*b
));
489 static uint64_t charge_size(uint64_t total
, uint64_t amount
) {
492 assert(amount
<= total
);
494 /* Subtract the specified amount from total, rounding up to multiple of 4K if there's room */
495 rounded
= round_up_size(amount
, 4096);
496 if (rounded
>= total
)
499 return total
- rounded
;
502 static uint64_t charge_weight(uint64_t total
, uint64_t amount
) {
503 assert(amount
<= total
);
504 return total
- amount
;
507 static bool context_allocate_partitions(Context
*context
) {
512 /* A simple first-fit algorithm, assuming the array of free areas is sorted by size in decreasing
515 LIST_FOREACH(partitions
, p
, context
->partitions
) {
520 /* Skip partitions we already dropped or that already exist */
521 if (p
->dropped
|| PARTITION_EXISTS(p
))
525 typesafe_qsort(context
->free_areas
, context
->n_free_areas
, free_area_compare
);
527 /* How much do we need to fit? */
528 required
= partition_min_size_with_padding(p
);
529 assert(required
% 4096 == 0);
531 for (size_t i
= 0; i
< context
->n_free_areas
; i
++) {
532 a
= context
->free_areas
[i
];
534 if (free_area_available_for_new_partitions(a
) >= required
) {
541 return false; /* 😢 Oh no! We can't fit this partition into any free area! */
543 /* Assign the partition to this free area */
544 p
->allocated_to_area
= a
;
546 /* Budget the minimal partition size */
547 a
->allocated
+= required
;
553 static int context_sum_weights(Context
*context
, FreeArea
*a
, uint64_t *ret
) {
554 uint64_t weight_sum
= 0;
561 /* Determine the sum of the weights of all partitions placed in or before the specified free area */
563 LIST_FOREACH(partitions
, p
, context
->partitions
) {
564 if (p
->padding_area
!= a
&& p
->allocated_to_area
!= a
)
567 if (p
->weight
> UINT64_MAX
- weight_sum
)
569 weight_sum
+= p
->weight
;
571 if (p
->padding_weight
> UINT64_MAX
- weight_sum
)
573 weight_sum
+= p
->padding_weight
;
580 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "Combined weight of partition exceeds unsigned 64bit range, refusing.");
583 static int scale_by_weight(uint64_t value
, uint64_t weight
, uint64_t weight_sum
, uint64_t *ret
) {
584 assert(weight_sum
>= weight
);
592 if (value
> UINT64_MAX
/ weight
)
593 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "Scaling by weight of partition exceeds unsigned 64bit range, refusing.");
595 *ret
= value
* weight
/ weight_sum
;
599 typedef enum GrowPartitionPhase
{
600 /* The first phase: we charge partitions which need more (according to constraints) than their weight-based share. */
603 /* The second phase: we charge partitions which need less (according to constraints) than their weight-based share. */
606 /* The third phase: we distribute what remains among the remaining partitions, according to the weights */
608 } GrowPartitionPhase
;
610 static int context_grow_partitions_phase(
613 GrowPartitionPhase phase
,
615 uint64_t *weight_sum
) {
623 /* Now let's look at the intended weights and adjust them taking the minimum space assignments into
624 * account. i.e. if a partition has a small weight but a high minimum space value set it should not
625 * get any additional room from the left-overs. Similar, if two partitions have the same weight they
626 * should get the same space if possible, even if one has a smaller minimum size than the other. */
627 LIST_FOREACH(partitions
, p
, context
->partitions
) {
629 /* Look only at partitions associated with this free area, i.e. immediately
630 * preceding it, or allocated into it */
631 if (p
->allocated_to_area
!= a
&& p
->padding_area
!= a
)
634 if (p
->new_size
== UINT64_MAX
) {
635 bool charge
= false, try_again
= false;
636 uint64_t share
, rsz
, xsz
;
638 /* Calculate how much this space this partition needs if everyone would get
639 * the weight based share */
640 r
= scale_by_weight(*span
, p
->weight
, *weight_sum
, &share
);
644 rsz
= partition_min_size(p
);
645 xsz
= partition_max_size(p
);
647 if (phase
== PHASE_OVERCHARGE
&& rsz
> share
) {
648 /* This partition needs more than its calculated share. Let's assign
649 * it that, and take this partition out of all calculations and start
653 charge
= try_again
= true;
655 } else if (phase
== PHASE_UNDERCHARGE
&& xsz
!= UINT64_MAX
&& xsz
< share
) {
656 /* This partition accepts less than its calculated
657 * share. Let's assign it that, and take this partition out
658 * of all calculations and start again. */
661 charge
= try_again
= true;
663 } else if (phase
== PHASE_DISTRIBUTE
) {
664 /* This partition can accept its calculated share. Let's
665 * assign it. There's no need to restart things here since
666 * assigning this shouldn't impact the shares of the other
669 if (PARTITION_IS_FOREIGN(p
))
670 /* Never change of foreign partitions (i.e. those we don't manage) */
671 p
->new_size
= p
->current_size
;
673 p
->new_size
= MAX(round_down_size(share
, 4096), rsz
);
679 *span
= charge_size(*span
, p
->new_size
);
680 *weight_sum
= charge_weight(*weight_sum
, p
->weight
);
684 return 0; /* try again */
687 if (p
->new_padding
== UINT64_MAX
) {
688 bool charge
= false, try_again
= false;
691 r
= scale_by_weight(*span
, p
->padding_weight
, *weight_sum
, &share
);
695 if (phase
== PHASE_OVERCHARGE
&& p
->padding_min
!= UINT64_MAX
&& p
->padding_min
> share
) {
696 p
->new_padding
= p
->padding_min
;
697 charge
= try_again
= true;
698 } else if (phase
== PHASE_UNDERCHARGE
&& p
->padding_max
!= UINT64_MAX
&& p
->padding_max
< share
) {
699 p
->new_padding
= p
->padding_max
;
700 charge
= try_again
= true;
701 } else if (phase
== PHASE_DISTRIBUTE
) {
703 p
->new_padding
= round_down_size(share
, 4096);
704 if (p
->padding_min
!= UINT64_MAX
&& p
->new_padding
< p
->padding_min
)
705 p
->new_padding
= p
->padding_min
;
711 *span
= charge_size(*span
, p
->new_padding
);
712 *weight_sum
= charge_weight(*weight_sum
, p
->padding_weight
);
716 return 0; /* try again */
723 static int context_grow_partitions_on_free_area(Context
*context
, FreeArea
*a
) {
724 uint64_t weight_sum
= 0, span
;
730 r
= context_sum_weights(context
, a
, &weight_sum
);
734 /* Let's calculate the total area covered by this free area and the partition before it */
737 assert(a
->after
->offset
!= UINT64_MAX
);
738 assert(a
->after
->current_size
!= UINT64_MAX
);
740 span
+= round_up_size(a
->after
->offset
+ a
->after
->current_size
, 4096) - a
->after
->offset
;
743 GrowPartitionPhase phase
= PHASE_OVERCHARGE
;
745 r
= context_grow_partitions_phase(context
, a
, phase
, &span
, &weight_sum
);
748 if (r
== 0) /* not done yet, re-run this phase */
751 if (phase
== PHASE_OVERCHARGE
)
752 phase
= PHASE_UNDERCHARGE
;
753 else if (phase
== PHASE_UNDERCHARGE
)
754 phase
= PHASE_DISTRIBUTE
;
755 else if (phase
== PHASE_DISTRIBUTE
)
759 /* We still have space left over? Donate to preceding partition if we have one */
760 if (span
> 0 && a
->after
&& !PARTITION_IS_FOREIGN(a
->after
)) {
763 assert(a
->after
->new_size
!= UINT64_MAX
);
764 m
= a
->after
->new_size
+ span
;
766 xsz
= partition_max_size(a
->after
);
767 if (xsz
!= UINT64_MAX
&& m
> xsz
)
770 span
= charge_size(span
, m
- a
->after
->new_size
);
771 a
->after
->new_size
= m
;
774 /* What? Even still some space left (maybe because there was no preceding partition, or it had a
775 * size limit), then let's donate it to whoever wants it. */
779 LIST_FOREACH(partitions
, p
, context
->partitions
) {
782 if (p
->allocated_to_area
!= a
)
785 if (PARTITION_IS_FOREIGN(p
))
788 assert(p
->new_size
!= UINT64_MAX
);
789 m
= p
->new_size
+ span
;
791 xsz
= partition_max_size(p
);
792 if (xsz
!= UINT64_MAX
&& m
> xsz
)
795 span
= charge_size(span
, m
- p
->new_size
);
803 /* Yuck, still no one? Then make it padding */
804 if (span
> 0 && a
->after
) {
805 assert(a
->after
->new_padding
!= UINT64_MAX
);
806 a
->after
->new_padding
+= span
;
812 static int context_grow_partitions(Context
*context
) {
818 for (size_t i
= 0; i
< context
->n_free_areas
; i
++) {
819 r
= context_grow_partitions_on_free_area(context
, context
->free_areas
[i
]);
824 /* All existing partitions that have no free space after them can't change size */
825 LIST_FOREACH(partitions
, p
, context
->partitions
) {
829 if (!PARTITION_EXISTS(p
) || p
->padding_area
) {
830 /* The algorithm above must have initialized this already */
831 assert(p
->new_size
!= UINT64_MAX
);
835 assert(p
->new_size
== UINT64_MAX
);
836 p
->new_size
= p
->current_size
;
838 assert(p
->new_padding
== UINT64_MAX
);
839 p
->new_padding
= p
->current_padding
;
845 static void context_place_partitions(Context
*context
) {
851 /* Determine next partition number to assign */
852 LIST_FOREACH(partitions
, p
, context
->partitions
) {
853 if (!PARTITION_EXISTS(p
))
856 assert(p
->partno
!= UINT64_MAX
);
857 if (p
->partno
>= partno
)
858 partno
= p
->partno
+ 1;
861 for (size_t i
= 0; i
< context
->n_free_areas
; i
++) {
862 FreeArea
*a
= context
->free_areas
[i
];
863 uint64_t start
, left
;
866 assert(a
->after
->offset
!= UINT64_MAX
);
867 assert(a
->after
->new_size
!= UINT64_MAX
);
868 assert(a
->after
->new_padding
!= UINT64_MAX
);
870 start
= a
->after
->offset
+ a
->after
->new_size
+ a
->after
->new_padding
;
872 start
= context
->start
;
874 start
= round_up_size(start
, 4096);
877 LIST_FOREACH(partitions
, p
, context
->partitions
) {
878 if (p
->allocated_to_area
!= a
)
882 p
->partno
= partno
++;
884 assert(left
>= p
->new_size
);
885 start
+= p
->new_size
;
888 assert(left
>= p
->new_padding
);
889 start
+= p
->new_padding
;
890 left
-= p
->new_padding
;
895 static int config_parse_type(
897 const char *filename
,
900 unsigned section_line
,
907 sd_id128_t
*type_uuid
= data
;
913 r
= gpt_partition_type_uuid_from_string(rvalue
, type_uuid
);
915 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse partition type: %s", rvalue
);
920 static const Specifier specifier_table
[] = {
921 { 'm', specifier_machine_id
, NULL
},
922 { 'b', specifier_boot_id
, NULL
},
923 { 'H', specifier_host_name
, NULL
},
924 { 'l', specifier_short_host_name
, NULL
},
925 { 'v', specifier_kernel_release
, NULL
},
926 { 'a', specifier_architecture
, NULL
},
927 { 'o', specifier_os_id
, NULL
},
928 { 'w', specifier_os_version_id
, NULL
},
929 { 'B', specifier_os_build_id
, NULL
},
930 { 'W', specifier_os_variant_id
, NULL
},
934 static int config_parse_label(
936 const char *filename
,
939 unsigned section_line
,
946 _cleanup_free_ char16_t
*recoded
= NULL
;
947 _cleanup_free_
char *resolved
= NULL
;
954 r
= specifier_printf(rvalue
, specifier_table
, NULL
, &resolved
);
956 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
957 "Failed to expand specifiers in Label=, ignoring: %s", rvalue
);
961 if (!utf8_is_valid(resolved
)) {
962 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
963 "Partition label not valid UTF-8, ignoring: %s", rvalue
);
967 recoded
= utf8_to_utf16(resolved
, strlen(resolved
));
971 if (char16_strlen(recoded
) > 36) {
972 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
973 "Partition label too long for GPT table, ignoring: \"%s\" (from \"%s\")",
978 free_and_replace(*label
, resolved
);
982 static int config_parse_weight(
984 const char *filename
,
987 unsigned section_line
,
994 uint32_t *priority
= data
, v
;
1000 r
= safe_atou32(rvalue
, &v
);
1002 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1003 "Failed to parse weight value, ignoring: %s", rvalue
);
1007 if (v
> 1000U*1000U) {
1008 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1009 "Weight needs to be in range 0…10000000, ignoring: %" PRIu32
, v
);
1017 static int config_parse_size4096(
1019 const char *filename
,
1021 const char *section
,
1022 unsigned section_line
,
1029 uint64_t *sz
= data
, parsed
;
1035 r
= parse_size(rvalue
, 1024, &parsed
);
1037 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1038 "Failed to parse size value: %s", rvalue
);
1041 *sz
= round_up_size(parsed
, 4096);
1043 *sz
= round_down_size(parsed
, 4096);
1048 log_syntax(unit
, LOG_NOTICE
, filename
, line
, r
, "Rounded %s= size %" PRIu64
" → %" PRIu64
", a multiple of 4096.", lvalue
, parsed
, *sz
);
1053 static int config_parse_fstype(
1055 const char *filename
,
1057 const char *section
,
1058 unsigned section_line
,
1065 char **fstype
= data
;
1070 if (!filename_is_valid(rvalue
))
1071 return log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1072 "File system type is not valid, refusing: %s", rvalue
);
1074 return free_and_strdup_warn(fstype
, rvalue
);
1077 static int config_parse_copy_files(
1079 const char *filename
,
1081 const char *section
,
1082 unsigned section_line
,
1089 _cleanup_free_
char *source
= NULL
, *buffer
= NULL
, *resolved_source
= NULL
, *resolved_target
= NULL
;
1090 const char *p
= rvalue
, *target
;
1091 Partition
*partition
= data
;
1097 r
= extract_first_word(&p
, &source
, ":", EXTRACT_CUNESCAPE
|EXTRACT_DONT_COALESCE_SEPARATORS
);
1099 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract source path: %s", rvalue
);
1101 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "No argument specified: %s", rvalue
);
1105 r
= extract_first_word(&p
, &buffer
, ":", EXTRACT_CUNESCAPE
|EXTRACT_DONT_COALESCE_SEPARATORS
);
1107 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract target path: %s", rvalue
);
1109 target
= source
; /* No target, then it's the same as the source */
1114 return log_syntax(unit
, LOG_ERR
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
), "Too many arguments: %s", rvalue
);
1116 r
= specifier_printf(source
, specifier_table
, NULL
, &resolved_source
);
1118 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1119 "Failed to expand specifiers in CopyFiles= source, ignoring: %s", rvalue
);
1123 if (!path_is_absolute(resolved_source
) || !path_is_normalized(resolved_source
)) {
1124 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1125 "Invalid path name in CopyFiles= source, ignoring: %s", resolved_source
);
1129 r
= specifier_printf(target
, specifier_table
, NULL
, &resolved_target
);
1131 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1132 "Failed to expand specifiers in CopyFiles= target, ignoring: %s", resolved_target
);
1136 if (!path_is_absolute(resolved_target
) || !path_is_normalized(resolved_target
)) {
1137 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1138 "Invalid path name in CopyFiles= source, ignoring: %s", resolved_target
);
1142 r
= strv_consume_pair(&partition
->copy_files
, TAKE_PTR(resolved_source
), TAKE_PTR(resolved_target
));
1149 static int partition_read_definition(Partition
*p
, const char *path
) {
1151 ConfigTableItem table
[] = {
1152 { "Partition", "Type", config_parse_type
, 0, &p
->type_uuid
},
1153 { "Partition", "Label", config_parse_label
, 0, &p
->new_label
},
1154 { "Partition", "UUID", config_parse_id128
, 0, &p
->new_uuid
},
1155 { "Partition", "Priority", config_parse_int32
, 0, &p
->priority
},
1156 { "Partition", "Weight", config_parse_weight
, 0, &p
->weight
},
1157 { "Partition", "PaddingWeight", config_parse_weight
, 0, &p
->padding_weight
},
1158 { "Partition", "SizeMinBytes", config_parse_size4096
, 1, &p
->size_min
},
1159 { "Partition", "SizeMaxBytes", config_parse_size4096
, -1, &p
->size_max
},
1160 { "Partition", "PaddingMinBytes", config_parse_size4096
, 1, &p
->padding_min
},
1161 { "Partition", "PaddingMaxBytes", config_parse_size4096
, -1, &p
->padding_max
},
1162 { "Partition", "FactoryReset", config_parse_bool
, 0, &p
->factory_reset
},
1163 { "Partition", "CopyBlocks", config_parse_path
, 0, &p
->copy_blocks_path
},
1164 { "Partition", "Format", config_parse_fstype
, 0, &p
->format
},
1165 { "Partition", "CopyFiles", config_parse_copy_files
, 0, p
},
1166 { "Partition", "Encrypt", config_parse_bool
, 0, &p
->encrypt
},
1171 r
= config_parse(NULL
, path
, NULL
,
1173 config_item_table_lookup
, table
,
1180 if (p
->size_min
!= UINT64_MAX
&& p
->size_max
!= UINT64_MAX
&& p
->size_min
> p
->size_max
)
1181 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1182 "SizeMinBytes= larger than SizeMaxBytes=, refusing.");
1184 if (p
->padding_min
!= UINT64_MAX
&& p
->padding_max
!= UINT64_MAX
&& p
->padding_min
> p
->padding_max
)
1185 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1186 "PaddingMinBytes= larger than PaddingMaxBytes=, refusing.");
1188 if (sd_id128_is_null(p
->type_uuid
))
1189 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1190 "Type= not defined, refusing.");
1192 if (p
->copy_blocks_path
&& (p
->format
|| !strv_isempty(p
->copy_files
)))
1193 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1194 "Format= and CopyBlocks= cannot be combined, refusing.");
1196 if (!strv_isempty(p
->copy_files
) && streq_ptr(p
->format
, "swap"))
1197 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1198 "Format=swap and CopyFiles= cannot be combined, refusing.");
1200 if (!p
->format
&& (!strv_isempty(p
->copy_files
) || (p
->encrypt
&& !p
->copy_blocks_path
))) {
1201 /* Pick "ext4" as file system if we are configured to copy files or encrypt the device */
1202 p
->format
= strdup("ext4");
1210 static int context_read_definitions(
1212 const char *directory
,
1215 _cleanup_strv_free_
char **files
= NULL
;
1216 Partition
*last
= NULL
;
1223 r
= conf_files_list_strv(&files
, ".conf", NULL
, CONF_FILES_REGULAR
|CONF_FILES_FILTER_MASKED
, (const char**) STRV_MAKE(directory
));
1225 r
= conf_files_list_strv(&files
, ".conf", root
, CONF_FILES_REGULAR
|CONF_FILES_FILTER_MASKED
, (const char**) CONF_PATHS_STRV("repart.d"));
1227 return log_error_errno(r
, "Failed to enumerate *.conf files: %m");
1229 STRV_FOREACH(f
, files
) {
1230 _cleanup_(partition_freep
) Partition
*p
= NULL
;
1232 p
= partition_new();
1236 p
->definition_path
= strdup(*f
);
1237 if (!p
->definition_path
)
1240 r
= partition_read_definition(p
, *f
);
1244 LIST_INSERT_AFTER(partitions
, context
->partitions
, last
, p
);
1246 context
->n_partitions
++;
1252 DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_context
*, fdisk_unref_context
);
1253 DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_partition
*, fdisk_unref_partition
);
1254 DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_parttype
*, fdisk_unref_parttype
);
1255 DEFINE_TRIVIAL_CLEANUP_FUNC(struct fdisk_table
*, fdisk_unref_table
);
1257 static int determine_current_padding(
1258 struct fdisk_context
*c
,
1259 struct fdisk_table
*t
,
1260 struct fdisk_partition
*p
,
1263 size_t n_partitions
;
1264 uint64_t offset
, next
= UINT64_MAX
;
1270 if (!fdisk_partition_has_end(p
))
1271 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition has no end!");
1273 offset
= fdisk_partition_get_end(p
);
1274 assert(offset
< UINT64_MAX
/ 512);
1277 n_partitions
= fdisk_table_get_nents(t
);
1278 for (size_t i
= 0; i
< n_partitions
; i
++) {
1279 struct fdisk_partition
*q
;
1282 q
= fdisk_table_get_partition(t
, i
);
1284 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to read partition metadata: %m");
1286 if (fdisk_partition_is_used(q
) <= 0)
1289 if (!fdisk_partition_has_start(q
))
1292 start
= fdisk_partition_get_start(q
);
1293 assert(start
< UINT64_MAX
/ 512);
1296 if (start
>= offset
&& (next
== UINT64_MAX
|| next
> start
))
1300 if (next
== UINT64_MAX
) {
1301 /* No later partition? In that case check the end of the usable area */
1302 next
= fdisk_get_last_lba(c
);
1303 assert(next
< UINT64_MAX
);
1304 next
++; /* The last LBA is one sector before the end */
1306 assert(next
< UINT64_MAX
/ 512);
1310 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition end beyond disk end.");
1313 assert(next
>= offset
);
1314 offset
= round_up_size(offset
, 4096);
1315 next
= round_down_size(next
, 4096);
1317 if (next
>= offset
) /* Check again, rounding might have fucked things up */
1318 *ret
= next
- offset
;
1325 static int fdisk_ask_cb(struct fdisk_context
*c
, struct fdisk_ask
*ask
, void *data
) {
1326 _cleanup_free_
char *ids
= NULL
;
1329 if (fdisk_ask_get_type(ask
) != FDISK_ASKTYPE_STRING
)
1332 ids
= new(char, ID128_UUID_STRING_MAX
);
1336 r
= fdisk_ask_string_set_result(ask
, id128_to_uuid_string(*(sd_id128_t
*) data
, ids
));
1344 static int fdisk_set_disklabel_id_by_uuid(struct fdisk_context
*c
, sd_id128_t id
) {
1347 r
= fdisk_set_ask(c
, fdisk_ask_cb
, &id
);
1351 r
= fdisk_set_disklabel_id(c
);
1355 return fdisk_set_ask(c
, NULL
, NULL
);
1358 static int derive_uuid(sd_id128_t base
, const char *token
, sd_id128_t
*ret
) {
1360 unsigned char md
[SHA256_DIGEST_LENGTH
];
1367 /* Derive a new UUID from the specified UUID in a stable and reasonably safe way. Specifically, we
1368 * calculate the HMAC-SHA256 of the specified token string, keyed by the supplied base (typically the
1369 * machine ID). We use the machine ID as key (and not as cleartext!) of the HMAC operation since it's
1370 * the machine ID we don't want to leak. */
1372 if (!HMAC(EVP_sha256(),
1373 &base
, sizeof(base
),
1374 (const unsigned char*) token
, strlen(token
),
1376 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE
), "HMAC-SHA256 calculation failed.");
1378 /* Take the first half, mark it as v4 UUID */
1379 assert_cc(sizeof(result
.md
) == sizeof(result
.id
) * 2);
1380 *ret
= id128_make_v4_uuid(result
.id
);
1384 static int context_load_partition_table(
1389 _cleanup_(fdisk_unref_contextp
) struct fdisk_context
*c
= NULL
;
1390 _cleanup_(fdisk_unref_tablep
) struct fdisk_table
*t
= NULL
;
1391 uint64_t left_boundary
= UINT64_MAX
, first_lba
, last_lba
, nsectors
;
1392 _cleanup_free_
char *disk_uuid_string
= NULL
;
1393 bool from_scratch
= false;
1394 sd_id128_t disk_uuid
;
1395 size_t n_partitions
;
1401 assert(!context
->fdisk_context
);
1402 assert(!context
->free_areas
);
1403 assert(context
->start
== UINT64_MAX
);
1404 assert(context
->end
== UINT64_MAX
);
1405 assert(context
->total
== UINT64_MAX
);
1407 c
= fdisk_new_context();
1411 /* libfdisk doesn't have an API to operate on arbitrary fds, hence reopen the fd going via the
1412 * /proc/self/fd/ magic path if we have an existing fd. Open the original file otherwise. */
1413 if (*backing_fd
< 0)
1414 r
= fdisk_assign_device(c
, node
, arg_dry_run
);
1416 char procfs_path
[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
1417 xsprintf(procfs_path
, "/proc/self/fd/%i", *backing_fd
);
1419 r
= fdisk_assign_device(c
, procfs_path
, arg_dry_run
);
1421 if (r
== -EINVAL
&& arg_size_auto
) {
1424 /* libfdisk returns EINVAL if opening a file of size zero. Let's check for that, and accept
1425 * it if automatic sizing is requested. */
1427 if (*backing_fd
< 0)
1428 r
= stat(node
, &st
);
1430 r
= fstat(*backing_fd
, &st
);
1432 return log_error_errno(errno
, "Failed to stat block device '%s': %m", node
);
1434 if (S_ISREG(st
.st_mode
) && st
.st_size
== 0)
1435 return /* from_scratch = */ true;
1440 return log_error_errno(r
, "Failed to open device '%s': %m", node
);
1442 if (*backing_fd
< 0) {
1443 /* If we have no fd referencing the device yet, make a copy of the fd now, so that we have one */
1444 *backing_fd
= fcntl(fdisk_get_devfd(c
), F_DUPFD_CLOEXEC
, 3);
1445 if (*backing_fd
< 0)
1446 return log_error_errno(errno
, "Failed to duplicate fdisk fd: %m");
1449 /* Tell udev not to interfere while we are processing the device */
1450 if (flock(fdisk_get_devfd(c
), arg_dry_run
? LOCK_SH
: LOCK_EX
) < 0)
1451 return log_error_errno(errno
, "Failed to lock block device: %m");
1453 switch (arg_empty
) {
1456 /* Refuse empty disks, insist on an existing GPT partition table */
1457 if (!fdisk_is_labeltype(c
, FDISK_DISKLABEL_GPT
))
1458 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON
), "Disk %s has no GPT disk label, not repartitioning.", node
);
1463 /* Require an empty disk, refuse any existing partition table */
1464 r
= fdisk_has_label(c
);
1466 return log_error_errno(r
, "Failed to determine whether disk %s has a disk label: %m", node
);
1468 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON
), "Disk %s already has a disk label, refusing.", node
);
1470 from_scratch
= true;
1474 /* Allow both an empty disk and an existing partition table, but only GPT */
1475 r
= fdisk_has_label(c
);
1477 return log_error_errno(r
, "Failed to determine whether disk %s has a disk label: %m", node
);
1479 if (!fdisk_is_labeltype(c
, FDISK_DISKLABEL_GPT
))
1480 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON
), "Disk %s has non-GPT disk label, not repartitioning.", node
);
1482 from_scratch
= true;
1488 /* Always reinitiaize the disk, don't consider what there was on the disk before */
1489 from_scratch
= true;
1494 r
= fdisk_create_disklabel(c
, "gpt");
1496 return log_error_errno(r
, "Failed to create GPT disk label: %m");
1498 r
= derive_uuid(context
->seed
, "disk-uuid", &disk_uuid
);
1500 return log_error_errno(r
, "Failed to acquire disk GPT uuid: %m");
1502 r
= fdisk_set_disklabel_id_by_uuid(c
, disk_uuid
);
1504 return log_error_errno(r
, "Failed to set GPT disk label: %m");
1506 goto add_initial_free_area
;
1509 r
= fdisk_get_disklabel_id(c
, &disk_uuid_string
);
1511 return log_error_errno(r
, "Failed to get current GPT disk label UUID: %m");
1513 r
= sd_id128_from_string(disk_uuid_string
, &disk_uuid
);
1515 return log_error_errno(r
, "Failed to parse current GPT disk label UUID: %m");
1517 if (sd_id128_is_null(disk_uuid
)) {
1518 r
= derive_uuid(context
->seed
, "disk-uuid", &disk_uuid
);
1520 return log_error_errno(r
, "Failed to acquire disk GPT uuid: %m");
1522 r
= fdisk_set_disklabel_id(c
);
1524 return log_error_errno(r
, "Failed to set GPT disk label: %m");
1527 r
= fdisk_get_partitions(c
, &t
);
1529 return log_error_errno(r
, "Failed to acquire partition table: %m");
1531 n_partitions
= fdisk_table_get_nents(t
);
1532 for (size_t i
= 0; i
< n_partitions
; i
++) {
1533 _cleanup_free_
char *label_copy
= NULL
;
1534 Partition
*pp
, *last
= NULL
;
1535 struct fdisk_partition
*p
;
1536 struct fdisk_parttype
*pt
;
1537 const char *pts
, *ids
, *label
;
1540 sd_id128_t ptid
, id
;
1543 p
= fdisk_table_get_partition(t
, i
);
1545 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to read partition metadata: %m");
1547 if (fdisk_partition_is_used(p
) <= 0)
1550 if (fdisk_partition_has_start(p
) <= 0 ||
1551 fdisk_partition_has_size(p
) <= 0 ||
1552 fdisk_partition_has_partno(p
) <= 0)
1553 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Found a partition without a position, size or number.");
1555 pt
= fdisk_partition_get_type(p
);
1557 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to acquire type of partition: %m");
1559 pts
= fdisk_parttype_get_string(pt
);
1561 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to acquire type of partition as string: %m");
1563 r
= sd_id128_from_string(pts
, &ptid
);
1565 return log_error_errno(r
, "Failed to parse partition type UUID %s: %m", pts
);
1567 ids
= fdisk_partition_get_uuid(p
);
1569 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Found a partition without a UUID.");
1571 r
= sd_id128_from_string(ids
, &id
);
1573 return log_error_errno(r
, "Failed to parse partition UUID %s: %m", ids
);
1575 label
= fdisk_partition_get_name(p
);
1576 if (!isempty(label
)) {
1577 label_copy
= strdup(label
);
1582 sz
= fdisk_partition_get_size(p
);
1583 assert_se(sz
<= UINT64_MAX
/512);
1586 start
= fdisk_partition_get_start(p
);
1587 assert_se(start
<= UINT64_MAX
/512);
1590 partno
= fdisk_partition_get_partno(p
);
1592 if (left_boundary
== UINT64_MAX
|| left_boundary
> start
)
1593 left_boundary
= start
;
1595 /* Assign this existing partition to the first partition of the right type that doesn't have
1596 * an existing one assigned yet. */
1597 LIST_FOREACH(partitions
, pp
, context
->partitions
) {
1600 if (!sd_id128_equal(pp
->type_uuid
, ptid
))
1603 if (!pp
->current_partition
) {
1604 pp
->current_uuid
= id
;
1605 pp
->current_size
= sz
;
1607 pp
->partno
= partno
;
1608 pp
->current_label
= TAKE_PTR(label_copy
);
1610 pp
->current_partition
= p
;
1611 fdisk_ref_partition(p
);
1613 r
= determine_current_padding(c
, t
, p
, &pp
->current_padding
);
1617 if (pp
->current_padding
> 0) {
1618 r
= context_add_free_area(context
, pp
->current_padding
, pp
);
1628 /* If we have no matching definition, create a new one. */
1630 _cleanup_(partition_freep
) Partition
*np
= NULL
;
1632 np
= partition_new();
1636 np
->current_uuid
= id
;
1637 np
->type_uuid
= ptid
;
1638 np
->current_size
= sz
;
1640 np
->partno
= partno
;
1641 np
->current_label
= TAKE_PTR(label_copy
);
1643 np
->current_partition
= p
;
1644 fdisk_ref_partition(p
);
1646 r
= determine_current_padding(c
, t
, p
, &np
->current_padding
);
1650 if (np
->current_padding
> 0) {
1651 r
= context_add_free_area(context
, np
->current_padding
, np
);
1656 LIST_INSERT_AFTER(partitions
, context
->partitions
, last
, TAKE_PTR(np
));
1657 context
->n_partitions
++;
1661 add_initial_free_area
:
1662 nsectors
= fdisk_get_nsectors(c
);
1663 assert(nsectors
<= UINT64_MAX
/512);
1666 first_lba
= fdisk_get_first_lba(c
);
1667 assert(first_lba
<= UINT64_MAX
/512);
1670 last_lba
= fdisk_get_last_lba(c
);
1671 assert(last_lba
< UINT64_MAX
);
1673 assert(last_lba
<= UINT64_MAX
/512);
1676 assert(last_lba
>= first_lba
);
1678 if (left_boundary
== UINT64_MAX
) {
1679 /* No partitions at all? Then the whole disk is up for grabs. */
1681 first_lba
= round_up_size(first_lba
, 4096);
1682 last_lba
= round_down_size(last_lba
, 4096);
1684 if (last_lba
> first_lba
) {
1685 r
= context_add_free_area(context
, last_lba
- first_lba
, NULL
);
1690 /* Add space left of first partition */
1691 assert(left_boundary
>= first_lba
);
1693 first_lba
= round_up_size(first_lba
, 4096);
1694 left_boundary
= round_down_size(left_boundary
, 4096);
1695 last_lba
= round_down_size(last_lba
, 4096);
1697 if (left_boundary
> first_lba
) {
1698 r
= context_add_free_area(context
, left_boundary
- first_lba
, NULL
);
1704 context
->start
= first_lba
;
1705 context
->end
= last_lba
;
1706 context
->total
= nsectors
;
1707 context
->fdisk_context
= TAKE_PTR(c
);
1709 return from_scratch
;
1712 static void context_unload_partition_table(Context
*context
) {
1713 Partition
*p
, *next
;
1717 LIST_FOREACH_SAFE(partitions
, p
, next
, context
->partitions
) {
1719 /* Entirely remove partitions that have no configuration */
1720 if (PARTITION_IS_FOREIGN(p
)) {
1721 partition_unlink_and_free(context
, p
);
1725 /* Otherwise drop all data we read off the block device and everything we might have
1726 * calculated based on it */
1729 p
->current_size
= UINT64_MAX
;
1730 p
->new_size
= UINT64_MAX
;
1731 p
->current_padding
= UINT64_MAX
;
1732 p
->new_padding
= UINT64_MAX
;
1733 p
->partno
= UINT64_MAX
;
1734 p
->offset
= UINT64_MAX
;
1736 if (p
->current_partition
) {
1737 fdisk_unref_partition(p
->current_partition
);
1738 p
->current_partition
= NULL
;
1741 if (p
->new_partition
) {
1742 fdisk_unref_partition(p
->new_partition
);
1743 p
->new_partition
= NULL
;
1746 p
->padding_area
= NULL
;
1747 p
->allocated_to_area
= NULL
;
1749 p
->current_uuid
= SD_ID128_NULL
;
1750 p
->current_label
= mfree(p
->current_label
);
1753 context
->start
= UINT64_MAX
;
1754 context
->end
= UINT64_MAX
;
1755 context
->total
= UINT64_MAX
;
1757 if (context
->fdisk_context
) {
1758 fdisk_unref_context(context
->fdisk_context
);
1759 context
->fdisk_context
= NULL
;
1762 context_free_free_areas(context
);
1765 static int format_size_change(uint64_t from
, uint64_t to
, char **ret
) {
1766 char format_buffer1
[FORMAT_BYTES_MAX
], format_buffer2
[FORMAT_BYTES_MAX
], *buf
;
1768 if (from
!= UINT64_MAX
)
1769 format_bytes(format_buffer1
, sizeof(format_buffer1
), from
);
1770 if (to
!= UINT64_MAX
)
1771 format_bytes(format_buffer2
, sizeof(format_buffer2
), to
);
1773 if (from
!= UINT64_MAX
) {
1774 if (from
== to
|| to
== UINT64_MAX
)
1775 buf
= strdup(format_buffer1
);
1777 buf
= strjoin(format_buffer1
, " ", special_glyph(SPECIAL_GLYPH_ARROW
), " ", format_buffer2
);
1778 } else if (to
!= UINT64_MAX
)
1779 buf
= strjoin(special_glyph(SPECIAL_GLYPH_ARROW
), " ", format_buffer2
);
1788 *ret
= TAKE_PTR(buf
);
1792 static const char *partition_label(const Partition
*p
) {
1796 return p
->new_label
;
1798 if (p
->current_label
)
1799 return p
->current_label
;
1801 return gpt_partition_type_uuid_to_string(p
->type_uuid
);
1804 static int context_dump_partitions(Context
*context
, const char *node
) {
1805 _cleanup_(table_unrefp
) Table
*t
= NULL
;
1806 uint64_t sum_padding
= 0, sum_size
= 0;
1810 if (!arg_json
&& context
->n_partitions
== 0) {
1811 log_info("Empty partition table.");
1815 t
= table_new("type", "label", "uuid", "file", "node", "offset", "old size", "raw size", "size", "old padding", "raw padding", "padding", "activity");
1819 if (!DEBUG_LOGGING
) {
1821 (void) table_set_display(t
, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
1822 (size_t) 5, (size_t) 6, (size_t) 7, (size_t) 9, (size_t) 10, (size_t) 12, (size_t) -1);
1824 (void) table_set_display(t
, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
1825 (size_t) 8, (size_t) 11, (size_t) -1);
1828 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 4), 100);
1829 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 5), 100);
1831 LIST_FOREACH(partitions
, p
, context
->partitions
) {
1832 _cleanup_free_
char *size_change
= NULL
, *padding_change
= NULL
, *partname
= NULL
;
1833 char uuid_buffer
[ID128_UUID_STRING_MAX
];
1834 const char *label
, *activity
= NULL
;
1839 if (p
->current_size
== UINT64_MAX
)
1840 activity
= "create";
1841 else if (p
->current_size
!= p
->new_size
)
1842 activity
= "resize";
1844 label
= partition_label(p
);
1845 partname
= p
->partno
!= UINT64_MAX
? fdisk_partname(node
, p
->partno
+1) : NULL
;
1847 r
= format_size_change(p
->current_size
, p
->new_size
, &size_change
);
1851 r
= format_size_change(p
->current_padding
, p
->new_padding
, &padding_change
);
1855 if (p
->new_size
!= UINT64_MAX
)
1856 sum_size
+= p
->new_size
;
1857 if (p
->new_padding
!= UINT64_MAX
)
1858 sum_padding
+= p
->new_padding
;
1862 TABLE_STRING
, gpt_partition_type_uuid_to_string_harder(p
->type_uuid
, uuid_buffer
),
1863 TABLE_STRING
, label
?: "-", TABLE_SET_COLOR
, label
? NULL
: ansi_grey(),
1864 TABLE_UUID
, sd_id128_is_null(p
->new_uuid
) ? p
->current_uuid
: p
->new_uuid
,
1865 TABLE_STRING
, p
->definition_path
? basename(p
->definition_path
) : "-", TABLE_SET_COLOR
, p
->definition_path
? NULL
: ansi_grey(),
1866 TABLE_STRING
, partname
?: "-", TABLE_SET_COLOR
, partname
? NULL
: ansi_highlight(),
1867 TABLE_UINT64
, p
->offset
,
1868 TABLE_UINT64
, p
->current_size
== UINT64_MAX
? 0 : p
->current_size
,
1869 TABLE_UINT64
, p
->new_size
,
1870 TABLE_STRING
, size_change
, TABLE_SET_COLOR
, !p
->partitions_next
&& sum_size
> 0 ? ansi_underline() : NULL
,
1871 TABLE_UINT64
, p
->current_padding
== UINT64_MAX
? 0 : p
->current_padding
,
1872 TABLE_UINT64
, p
->new_padding
,
1873 TABLE_STRING
, padding_change
, TABLE_SET_COLOR
, !p
->partitions_next
&& sum_padding
> 0 ? ansi_underline() : NULL
,
1874 TABLE_STRING
, activity
?: "unknown");
1876 return table_log_add_error(r
);
1879 if (!arg_json
&& (sum_padding
> 0 || sum_size
> 0)) {
1880 char s
[FORMAT_BYTES_MAX
];
1883 a
= strjoina(special_glyph(SPECIAL_GLYPH_SIGMA
), " = ", format_bytes(s
, sizeof(s
), sum_size
));
1884 b
= strjoina(special_glyph(SPECIAL_GLYPH_SIGMA
), " = ", format_bytes(s
, sizeof(s
), sum_padding
));
1902 return table_log_add_error(r
);
1906 r
= table_print_json(t
, stdout
, arg_json_format_flags
);
1908 r
= table_print(t
, stdout
);
1910 return log_error_errno(r
, "Failed to dump table: %m");
1915 static void context_bar_char_process_partition(
1920 size_t *ret_start
) {
1922 uint64_t from
, to
, total
;
1933 assert(p
->offset
!= UINT64_MAX
);
1934 assert(p
->new_size
!= UINT64_MAX
);
1937 to
= from
+ p
->new_size
;
1939 assert(context
->end
>= context
->start
);
1940 total
= context
->end
- context
->start
;
1942 assert(from
>= context
->start
);
1943 assert(from
<= context
->end
);
1944 x
= (from
- context
->start
) * n
/ total
;
1946 assert(to
>= context
->start
);
1947 assert(to
<= context
->end
);
1948 y
= (to
- context
->start
) * n
/ total
;
1953 for (size_t i
= x
; i
< y
; i
++)
1959 static int partition_hint(const Partition
*p
, const char *node
, char **ret
) {
1960 _cleanup_free_
char *buf
= NULL
;
1961 char ids
[ID128_UUID_STRING_MAX
];
1965 /* Tries really hard to find a suitable description for this partition */
1967 if (p
->definition_path
) {
1968 buf
= strdup(basename(p
->definition_path
));
1972 label
= partition_label(p
);
1973 if (!isempty(label
)) {
1974 buf
= strdup(label
);
1978 if (p
->partno
!= UINT64_MAX
) {
1979 buf
= fdisk_partname(node
, p
->partno
+1);
1983 if (!sd_id128_is_null(p
->new_uuid
))
1985 else if (!sd_id128_is_null(p
->current_uuid
))
1986 id
= p
->current_uuid
;
1990 buf
= strdup(id128_to_uuid_string(id
, ids
));
1996 *ret
= TAKE_PTR(buf
);
2000 static int context_dump_partition_bar(Context
*context
, const char *node
) {
2001 _cleanup_free_ Partition
**bar
= NULL
;
2002 _cleanup_free_
size_t *start_array
= NULL
;
2003 Partition
*p
, *last
= NULL
;
2007 assert_se((c
= columns()) >= 2);
2008 c
-= 2; /* We do not use the leftmost and rightmost character cell */
2010 bar
= new0(Partition
*, c
);
2014 start_array
= new(size_t, context
->n_partitions
);
2018 LIST_FOREACH(partitions
, p
, context
->partitions
)
2019 context_bar_char_process_partition(context
, bar
, c
, p
, start_array
+ j
++);
2023 for (size_t i
= 0; i
< c
; i
++) {
2028 fputs(z
? ansi_green() : ansi_yellow(), stdout
);
2029 fputs(special_glyph(SPECIAL_GLYPH_DARK_SHADE
), stdout
);
2031 fputs(ansi_normal(), stdout
);
2032 fputs(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE
), stdout
);
2038 fputs(ansi_normal(), stdout
);
2041 for (size_t i
= 0; i
< context
->n_partitions
; i
++) {
2042 _cleanup_free_
char **line
= NULL
;
2044 line
= new0(char*, c
);
2049 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2050 _cleanup_free_
char *d
= NULL
;
2053 if (i
< context
->n_partitions
- j
) {
2055 if (line
[start_array
[j
-1]]) {
2058 /* Upgrade final corner to the right with a branch to the right */
2059 e
= startswith(line
[start_array
[j
-1]], special_glyph(SPECIAL_GLYPH_TREE_RIGHT
));
2061 d
= strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH
), e
);
2068 d
= strdup(special_glyph(SPECIAL_GLYPH_TREE_VERTICAL
));
2073 } else if (i
== context
->n_partitions
- j
) {
2074 _cleanup_free_
char *hint
= NULL
;
2076 (void) partition_hint(p
, node
, &hint
);
2078 if (streq_ptr(line
[start_array
[j
-1]], special_glyph(SPECIAL_GLYPH_TREE_VERTICAL
)))
2079 d
= strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH
), " ", strna(hint
));
2081 d
= strjoin(special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), " ", strna(hint
));
2088 free_and_replace(line
[start_array
[j
-1]], d
);
2096 fputs(line
[j
], stdout
);
2097 j
+= utf8_console_width(line
[j
]);
2106 for (j
= 0; j
< c
; j
++)
2113 static bool context_changed(const Context
*context
) {
2116 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2120 if (p
->allocated_to_area
)
2123 if (p
->new_size
!= p
->current_size
)
2130 static int context_wipe_range(Context
*context
, uint64_t offset
, uint64_t size
) {
2131 _cleanup_(blkid_free_probep
) blkid_probe probe
= NULL
;
2135 assert(offset
!= UINT64_MAX
);
2136 assert(size
!= UINT64_MAX
);
2138 probe
= blkid_new_probe();
2143 r
= blkid_probe_set_device(probe
, fdisk_get_devfd(context
->fdisk_context
), offset
, size
);
2145 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to allocate device probe for wiping.");
2148 if (blkid_probe_enable_superblocks(probe
, true) < 0 ||
2149 blkid_probe_set_superblocks_flags(probe
, BLKID_SUBLKS_MAGIC
|BLKID_SUBLKS_BADCSUM
) < 0 ||
2150 blkid_probe_enable_partitions(probe
, true) < 0 ||
2151 blkid_probe_set_partitions_flags(probe
, BLKID_PARTS_MAGIC
) < 0)
2152 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to enable superblock and partition probing.");
2156 r
= blkid_do_probe(probe
);
2158 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to probe for file systems.");
2163 if (blkid_do_wipe(probe
, false) < 0)
2164 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to wipe file system signature.");
2170 static int context_wipe_partition(Context
*context
, Partition
*p
) {
2175 assert(!PARTITION_EXISTS(p
)); /* Safety check: never wipe existing partitions */
2177 assert(p
->offset
!= UINT64_MAX
);
2178 assert(p
->new_size
!= UINT64_MAX
);
2180 r
= context_wipe_range(context
, p
->offset
, p
->new_size
);
2184 log_info("Successfully wiped file system signatures from future partition %" PRIu64
".", p
->partno
);
2188 static int context_discard_range(
2197 assert(offset
!= UINT64_MAX
);
2198 assert(size
!= UINT64_MAX
);
2203 assert_se((fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
2205 if (fstat(fd
, &st
) < 0)
2208 if (S_ISREG(st
.st_mode
)) {
2209 if (fallocate(fd
, FALLOC_FL_PUNCH_HOLE
|FALLOC_FL_KEEP_SIZE
, offset
, size
) < 0) {
2210 if (ERRNO_IS_NOT_SUPPORTED(errno
))
2219 if (S_ISBLK(st
.st_mode
)) {
2220 uint64_t range
[2], end
;
2222 range
[0] = round_up_size(offset
, 512);
2224 end
= offset
+ size
;
2225 if (end
<= range
[0])
2228 range
[1] = round_down_size(end
- range
[0], 512);
2232 if (ioctl(fd
, BLKDISCARD
, range
) < 0) {
2233 if (ERRNO_IS_NOT_SUPPORTED(errno
))
2245 static int context_discard_partition(Context
*context
, Partition
*p
) {
2251 assert(p
->offset
!= UINT64_MAX
);
2252 assert(p
->new_size
!= UINT64_MAX
);
2253 assert(!PARTITION_EXISTS(p
)); /* Safety check: never discard existing partitions */
2258 r
= context_discard_range(context
, p
->offset
, p
->new_size
);
2259 if (r
== -EOPNOTSUPP
) {
2260 log_info("Storage does not support discard, not discarding data in future partition %" PRIu64
".", p
->partno
);
2264 log_info("Partition %" PRIu64
" too short for discard, skipping.", p
->partno
);
2268 return log_error_errno(r
, "Failed to discard data for future partition %" PRIu64
".", p
->partno
);
2270 log_info("Successfully discarded data from future partition %" PRIu64
".", p
->partno
);
2274 static int context_discard_gap_after(Context
*context
, Partition
*p
) {
2275 uint64_t gap
, next
= UINT64_MAX
;
2280 assert(!p
|| (p
->offset
!= UINT64_MAX
&& p
->new_size
!= UINT64_MAX
));
2283 gap
= p
->offset
+ p
->new_size
;
2285 gap
= context
->start
;
2287 LIST_FOREACH(partitions
, q
, context
->partitions
) {
2291 assert(q
->offset
!= UINT64_MAX
);
2292 assert(q
->new_size
!= UINT64_MAX
);
2294 if (q
->offset
< gap
)
2297 if (next
== UINT64_MAX
|| q
->offset
< next
)
2301 if (next
== UINT64_MAX
) {
2302 next
= context
->end
;
2304 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition end beyond disk end.");
2307 assert(next
>= gap
);
2308 r
= context_discard_range(context
, gap
, next
- gap
);
2309 if (r
== -EOPNOTSUPP
) {
2311 log_info("Storage does not support discard, not discarding gap after partition %" PRIu64
".", p
->partno
);
2313 log_info("Storage does not support discard, not discarding gap at beginning of disk.");
2316 if (r
== 0) /* Too short */
2320 return log_error_errno(r
, "Failed to discard gap after partition %" PRIu64
".", p
->partno
);
2322 return log_error_errno(r
, "Failed to discard gap at beginning of disk.");
2326 log_info("Successfully discarded gap after partition %" PRIu64
".", p
->partno
);
2328 log_info("Successfully discarded gap at beginning of disk.");
2333 static int context_wipe_and_discard(Context
*context
, bool from_scratch
) {
2339 /* Wipe and discard the contents of all partitions we are about to create. We skip the discarding if
2340 * we were supposed to start from scratch anyway, as in that case we just discard the whole block
2341 * device in one go early on. */
2343 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2345 if (!p
->allocated_to_area
)
2348 r
= context_wipe_partition(context
, p
);
2352 if (!from_scratch
) {
2353 r
= context_discard_partition(context
, p
);
2357 r
= context_discard_gap_after(context
, p
);
2363 if (!from_scratch
) {
2364 r
= context_discard_gap_after(context
, NULL
);
2372 static int partition_encrypt(
2375 struct crypt_device
**ret_cd
,
2378 #if HAVE_LIBCRYPTSETUP
2379 _cleanup_(sym_crypt_freep
) struct crypt_device
*cd
= NULL
;
2380 _cleanup_(erase_and_freep
) void *volume_key
= NULL
;
2381 _cleanup_free_
char *dm_name
= NULL
, *vol
= NULL
;
2382 char suuid
[ID128_UUID_STRING_MAX
];
2383 size_t volume_key_size
= 256 / 8;
2390 r
= dlopen_cryptsetup();
2392 return log_error_errno(r
, "libcryptsetup not found, cannot encrypt: %m");
2394 if (asprintf(&dm_name
, "luks-repart-%08" PRIx64
, random_u64()) < 0)
2398 vol
= path_join("/dev/mapper/", dm_name
);
2403 r
= derive_uuid(p
->new_uuid
, "luks-uuid", &uuid
);
2407 log_info("Encrypting future partition %" PRIu64
"...", p
->partno
);
2409 volume_key
= malloc(volume_key_size
);
2413 r
= genuine_random_bytes(volume_key
, volume_key_size
, RANDOM_BLOCK
);
2415 return log_error_errno(r
, "Failed to generate volume key: %m");
2417 r
= sym_crypt_init(&cd
, node
);
2419 return log_error_errno(r
, "Failed to allocate libcryptsetup context: %m");
2421 cryptsetup_enable_logging(cd
);
2423 r
= sym_crypt_format(cd
,
2427 id128_to_uuid_string(uuid
, suuid
),
2430 &(struct crypt_params_luks2
) {
2431 .label
= p
->new_label
,
2432 .sector_size
= 512U,
2435 return log_error_errno(r
, "Failed to LUKS2 format future partition: %m");
2437 r
= sym_crypt_keyslot_add_by_volume_key(
2445 return log_error_errno(r
, "Failed to add LUKS2 key: %m");
2447 r
= sym_crypt_activate_by_volume_key(
2452 arg_discard
? CRYPT_ACTIVATE_ALLOW_DISCARDS
: 0);
2454 return log_error_errno(r
, "Failed to activate LUKS superblock: %m");
2456 log_info("Successfully encrypted future partition %" PRIu64
".", p
->partno
);
2459 _cleanup_close_
int dev_fd
= -1;
2461 dev_fd
= open(vol
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
);
2463 return log_error_errno(errno
, "Failed to open LUKS volume '%s': %m", vol
);
2465 *ret_fd
= TAKE_FD(dev_fd
);
2469 *ret_cd
= TAKE_PTR(cd
);
2471 *ret_volume
= TAKE_PTR(vol
);
2475 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "libcryptsetup is not supported, cannot encrypt: %m");
2479 static int deactivate_luks(struct crypt_device
*cd
, const char *node
) {
2480 #if HAVE_LIBCRYPTSETUP
2488 /* udev or so might access out block device in the background while we are done. Let's hence force
2489 * detach the volume. We sync'ed before, hence this should be safe. */
2491 r
= sym_crypt_deactivate_by_name(cd
, basename(node
), CRYPT_DEACTIVATE_FORCE
);
2493 return log_error_errno(r
, "Failed to deactivate LUKS device: %m");
2501 static int context_copy_blocks(Context
*context
) {
2503 int whole_fd
= -1, r
;
2507 /* Copy in file systems on the block level */
2509 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2510 _cleanup_(sym_crypt_freep
) struct crypt_device
*cd
= NULL
;
2511 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
2512 _cleanup_free_
char *encrypted
= NULL
;
2513 _cleanup_close_
int encrypted_dev_fd
= -1;
2514 char buf
[FORMAT_BYTES_MAX
];
2517 if (p
->copy_blocks_fd
< 0)
2523 if (PARTITION_EXISTS(p
)) /* Never copy over existing partitions */
2526 assert(p
->new_size
!= UINT64_MAX
);
2527 assert(p
->copy_blocks_size
!= UINT64_MAX
);
2528 assert(p
->new_size
>= p
->copy_blocks_size
);
2531 assert_se((whole_fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
2534 r
= loop_device_make(whole_fd
, O_RDWR
, p
->offset
, p
->new_size
, 0, &d
);
2536 return log_error_errno(r
, "Failed to make loopback device of future partition %" PRIu64
": %m", p
->partno
);
2538 r
= loop_device_flock(d
, LOCK_EX
);
2540 return log_error_errno(r
, "Failed to lock loopback device: %m");
2542 r
= partition_encrypt(p
, d
->node
, &cd
, &encrypted
, &encrypted_dev_fd
);
2544 return log_error_errno(r
, "Failed to encrypt device: %m");
2546 if (flock(encrypted_dev_fd
, LOCK_EX
) < 0)
2547 return log_error_errno(errno
, "Failed to lock LUKS device: %m");
2549 target_fd
= encrypted_dev_fd
;
2551 if (lseek(whole_fd
, p
->offset
, SEEK_SET
) == (off_t
) -1)
2552 return log_error_errno(errno
, "Failed to seek to partition offset: %m");
2554 target_fd
= whole_fd
;
2557 log_info("Copying in '%s' (%s) on block level into future partition %" PRIu64
".", p
->copy_blocks_path
, format_bytes(buf
, sizeof(buf
), p
->copy_blocks_size
), p
->partno
);
2559 r
= copy_bytes_full(p
->copy_blocks_fd
, target_fd
, p
->copy_blocks_size
, 0, NULL
, NULL
, NULL
, NULL
);
2561 return log_error_errno(r
, "Failed to copy in data from '%s': %m", p
->copy_blocks_path
);
2563 if (fsync(target_fd
) < 0)
2564 return log_error_errno(r
, "Failed to synchronize copied data blocks: %m");
2567 encrypted_dev_fd
= safe_close(encrypted_dev_fd
);
2569 r
= deactivate_luks(cd
, encrypted
);
2576 r
= loop_device_sync(d
);
2578 return log_error_errno(r
, "Failed to sync loopback device: %m");
2581 log_info("Copying in of '%s' on block level completed.", p
->copy_blocks_path
);
2587 static int do_copy_files(Partition
*p
, const char *fs
) {
2588 char **source
, **target
;
2594 STRV_FOREACH_PAIR(source
, target
, p
->copy_files
) {
2595 _cleanup_close_
int sfd
= -1, pfd
= -1, tfd
= -1;
2596 _cleanup_free_
char *dn
= NULL
;
2598 dn
= dirname_malloc(*target
);
2602 sfd
= chase_symlinks_and_open(*source
, arg_root
, CHASE_PREFIX_ROOT
|CHASE_WARN
, O_CLOEXEC
|O_NOCTTY
, NULL
);
2604 return log_error_errno(sfd
, "Failed to open source file '%s%s': %m", strempty(arg_root
), *source
);
2606 r
= fd_verify_regular(sfd
);
2609 return log_error_errno(r
, "Failed to check type of source file '%s': %m", *source
);
2611 /* We are looking at a directory */
2612 tfd
= chase_symlinks_and_open(*target
, fs
, CHASE_PREFIX_ROOT
|CHASE_WARN
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
, NULL
);
2615 return log_error_errno(tfd
, "Failed to open target directory '%s': %m", *target
);
2617 r
= mkdir_p_root(fs
, dn
, UID_INVALID
, GID_INVALID
, 0755);
2619 return log_error_errno(r
, "Failed to create parent directory '%s': %m", dn
);
2621 pfd
= chase_symlinks_and_open(dn
, fs
, CHASE_PREFIX_ROOT
|CHASE_WARN
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
, NULL
);
2623 return log_error_errno(pfd
, "Failed to open parent directory of target: %m");
2627 pfd
, basename(*target
),
2628 UID_INVALID
, GID_INVALID
,
2629 COPY_REFLINK
|COPY_MERGE
|COPY_REPLACE
|COPY_SIGINT
|COPY_HARDLINKS
);
2634 UID_INVALID
, GID_INVALID
,
2635 COPY_REFLINK
|COPY_MERGE
|COPY_REPLACE
|COPY_SIGINT
|COPY_HARDLINKS
);
2637 return log_error_errno(r
, "Failed to copy %s%s to %s: %m", strempty(arg_root
), *source
, *target
);
2639 /* We are looking at a regular file */
2641 r
= mkdir_p_root(fs
, dn
, UID_INVALID
, GID_INVALID
, 0755);
2643 return log_error_errno(r
, "Failed to create parent directory: %m");
2645 pfd
= chase_symlinks_and_open(dn
, fs
, CHASE_PREFIX_ROOT
|CHASE_WARN
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
, NULL
);
2647 return log_error_errno(tfd
, "Failed to open parent directory of target: %m");
2649 tfd
= openat(pfd
, basename(*target
), O_CREAT
|O_EXCL
|O_WRONLY
|O_CLOEXEC
, 0700);
2651 return log_error_errno(errno
, "Failed to create target file '%s': %m", *target
);
2653 r
= copy_bytes(sfd
, tfd
, UINT64_MAX
, COPY_REFLINK
|COPY_SIGINT
);
2655 return log_error_errno(r
, "Failed to copy '%s%s' to '%s': %m", strempty(arg_root
), *source
, *target
);
2657 (void) copy_xattr(sfd
, tfd
);
2658 (void) copy_access(sfd
, tfd
);
2659 (void) copy_times(sfd
, tfd
, 0);
2666 static int partition_copy_files(Partition
*p
, const char *node
) {
2672 if (strv_isempty(p
->copy_files
))
2675 log_info("Populating partition %" PRIu64
" with files.", p
->partno
);
2677 /* We copy in a child process, since we have to mount the fs for that, and we don't want that fs to
2678 * appear in the host namespace. Hence we fork a child that has its own file system namespace and
2679 * detached mount propagation. */
2681 r
= safe_fork("(sd-copy)", FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
|FORK_NEW_MOUNTNS
|FORK_MOUNTNS_SLAVE
, NULL
);
2685 static const char fs
[] = "/run/systemd/mount-root";
2686 /* This is a child process with its own mount namespace and propagation to host turned off */
2688 r
= mkdir_p(fs
, 0700);
2690 log_error_errno(r
, "Failed to create mount point: %m");
2691 _exit(EXIT_FAILURE
);
2694 if (mount_nofollow_verbose(LOG_ERR
, node
, fs
, p
->format
, MS_NOATIME
|MS_NODEV
|MS_NOEXEC
|MS_NOSUID
, NULL
) < 0)
2695 _exit(EXIT_FAILURE
);
2697 if (do_copy_files(p
, fs
) < 0)
2698 _exit(EXIT_FAILURE
);
2700 r
= syncfs_path(AT_FDCWD
, fs
);
2702 log_error_errno(r
, "Failed to synchronize written files: %m");
2703 _exit(EXIT_FAILURE
);
2706 _exit(EXIT_SUCCESS
);
2709 log_info("Successfully populated partition %" PRIu64
" with files.", p
->partno
);
2713 static int context_mkfs(Context
*context
) {
2719 /* Make a file system */
2721 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2722 _cleanup_(sym_crypt_freep
) struct crypt_device
*cd
= NULL
;
2723 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
2724 _cleanup_free_
char *encrypted
= NULL
;
2725 _cleanup_close_
int encrypted_dev_fd
= -1;
2732 if (PARTITION_EXISTS(p
)) /* Never format existing partitions */
2738 assert(p
->offset
!= UINT64_MAX
);
2739 assert(p
->new_size
!= UINT64_MAX
);
2742 assert_se((fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
2744 /* Loopback block devices are not only useful to turn regular files into block devices, but
2745 * also to cut out sections of block devices into new block devices. */
2747 r
= loop_device_make(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");
2756 r
= partition_encrypt(p
, d
->node
, &cd
, &encrypted
, &encrypted_dev_fd
);
2758 return log_error_errno(r
, "Failed to encrypt device: %m");
2760 if (flock(encrypted_dev_fd
, LOCK_EX
) < 0)
2761 return log_error_errno(errno
, "Failed to lock LUKS device: %m");
2767 log_info("Formatting future partition %" PRIu64
".", p
->partno
);
2769 /* Calculate the UUID for the file system as HMAC-SHA256 of the string "file-system-uuid",
2770 * keyed off the partition UUID. */
2771 r
= derive_uuid(p
->new_uuid
, "file-system-uuid", &fs_uuid
);
2775 r
= make_filesystem(fsdev
, p
->format
, p
->new_label
, fs_uuid
, arg_discard
);
2777 encrypted_dev_fd
= safe_close(encrypted_dev_fd
);
2778 (void) deactivate_luks(cd
, encrypted
);
2782 log_info("Successfully formatted future partition %" PRIu64
".", p
->partno
);
2784 /* The file system is now created, no need to delay udev further */
2786 if (flock(encrypted_dev_fd
, LOCK_UN
) < 0)
2787 return log_error_errno(errno
, "Failed to unlock LUKS device: %m");
2789 r
= partition_copy_files(p
, fsdev
);
2791 encrypted_dev_fd
= safe_close(encrypted_dev_fd
);
2792 (void) deactivate_luks(cd
, encrypted
);
2796 /* Note that we always sync explicitly here, since mkfs.fat doesn't do that on its own, and
2797 * if we don't sync before detaching a block device the in-flight sectors possibly won't hit
2801 if (fsync(encrypted_dev_fd
) < 0)
2802 return log_error_errno(r
, "Failed to synchronize LUKS volume: %m");
2803 encrypted_dev_fd
= safe_close(encrypted_dev_fd
);
2805 r
= deactivate_luks(cd
, encrypted
);
2813 r
= loop_device_sync(d
);
2815 return log_error_errno(r
, "Failed to sync loopback device: %m");
2821 static int partition_acquire_uuid(Context
*context
, Partition
*p
, sd_id128_t
*ret
) {
2823 sd_id128_t type_uuid
;
2825 } _packed_ plaintext
= {};
2827 unsigned char md
[SHA256_DIGEST_LENGTH
];
2839 /* Calculate a good UUID for the indicated partition. We want a certain degree of reproducibility,
2840 * hence we won't generate the UUIDs randomly. Instead we use a cryptographic hash (precisely:
2841 * HMAC-SHA256) to derive them from a single seed. The seed is generally the machine ID of the
2842 * installation we are processing, but if random behaviour is desired can be random, too. We use the
2843 * seed value as key for the HMAC (since the machine ID is something we generally don't want to leak)
2844 * and the partition type as plaintext. The partition type is suffixed with a counter (only for the
2845 * second and later partition of the same type) if we have more than one partition of the same
2846 * time. Or in other words:
2849 * SEED := /etc/machine-id
2851 * If first partition instance of type TYPE_UUID:
2852 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID)
2854 * For all later partition instances of type TYPE_UUID with INSTANCE being the LE64 encoded instance number:
2855 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID || INSTANCE)
2858 LIST_FOREACH(partitions
, q
, context
->partitions
) {
2862 if (!sd_id128_equal(p
->type_uuid
, q
->type_uuid
))
2868 plaintext
.type_uuid
= p
->type_uuid
;
2869 plaintext
.counter
= htole64(k
);
2871 if (!HMAC(EVP_sha256(),
2872 &context
->seed
, sizeof(context
->seed
),
2873 (const unsigned char*) &plaintext
, k
== 0 ? sizeof(sd_id128_t
) : sizeof(plaintext
),
2875 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE
), "SHA256 calculation failed.");
2877 /* Take the first half, mark it as v4 UUID */
2878 assert_cc(sizeof(result
.md
) == sizeof(result
.id
) * 2);
2879 result
.id
= id128_make_v4_uuid(result
.id
);
2881 /* Ensure this partition UUID is actually unique, and there's no remaining partition from an earlier run? */
2882 LIST_FOREACH(partitions
, q
, context
->partitions
) {
2886 if (sd_id128_equal(q
->current_uuid
, result
.id
) ||
2887 sd_id128_equal(q
->new_uuid
, result
.id
)) {
2888 log_warning("Partition UUID calculated from seed for partition %" PRIu64
" exists already, reverting to randomized UUID.", p
->partno
);
2890 r
= sd_id128_randomize(&result
.id
);
2892 return log_error_errno(r
, "Failed to generate randomized UUID: %m");
2902 static int partition_acquire_label(Context
*context
, Partition
*p
, char **ret
) {
2903 _cleanup_free_
char *label
= NULL
;
2911 prefix
= gpt_partition_type_uuid_to_string(p
->type_uuid
);
2916 const char *ll
= label
?: prefix
;
2920 LIST_FOREACH(partitions
, q
, context
->partitions
) {
2924 if (streq_ptr(ll
, q
->current_label
) ||
2925 streq_ptr(ll
, q
->new_label
)) {
2934 label
= mfree(label
);
2937 if (asprintf(&label
, "%s-%u", prefix
, ++k
) < 0)
2942 label
= strdup(prefix
);
2947 *ret
= TAKE_PTR(label
);
2951 static int context_acquire_partition_uuids_and_labels(Context
*context
) {
2957 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2958 /* Never touch foreign partitions */
2959 if (PARTITION_IS_FOREIGN(p
)) {
2960 p
->new_uuid
= p
->current_uuid
;
2962 if (p
->current_label
) {
2964 p
->new_label
= strdup(p
->current_label
);
2972 if (!sd_id128_is_null(p
->current_uuid
))
2973 p
->new_uuid
= p
->current_uuid
; /* Never change initialized UUIDs */
2974 else if (sd_id128_is_null(p
->new_uuid
)) {
2975 /* Not explicitly set by user! */
2976 r
= partition_acquire_uuid(context
, p
, &p
->new_uuid
);
2981 if (!isempty(p
->current_label
)) {
2983 p
->new_label
= strdup(p
->current_label
); /* never change initialized labels */
2986 } else if (!p
->new_label
) {
2987 /* Not explicitly set by user! */
2989 r
= partition_acquire_label(context
, p
, &p
->new_label
);
2998 static int context_mangle_partitions(Context
*context
) {
3004 LIST_FOREACH(partitions
, p
, context
->partitions
) {
3008 assert(p
->new_size
!= UINT64_MAX
);
3009 assert(p
->offset
!= UINT64_MAX
);
3010 assert(p
->partno
!= UINT64_MAX
);
3012 if (PARTITION_EXISTS(p
)) {
3013 bool changed
= false;
3015 assert(p
->current_partition
);
3017 if (p
->new_size
!= p
->current_size
) {
3018 assert(p
->new_size
>= p
->current_size
);
3019 assert(p
->new_size
% 512 == 0);
3021 r
= fdisk_partition_size_explicit(p
->current_partition
, true);
3023 return log_error_errno(r
, "Failed to enable explicit sizing: %m");
3025 r
= fdisk_partition_set_size(p
->current_partition
, p
->new_size
/ 512);
3027 return log_error_errno(r
, "Failed to grow partition: %m");
3029 log_info("Growing existing partition %" PRIu64
".", p
->partno
);
3033 if (!sd_id128_equal(p
->new_uuid
, p
->current_uuid
)) {
3034 char buf
[ID128_UUID_STRING_MAX
];
3036 assert(!sd_id128_is_null(p
->new_uuid
));
3038 r
= fdisk_partition_set_uuid(p
->current_partition
, id128_to_uuid_string(p
->new_uuid
, buf
));
3040 return log_error_errno(r
, "Failed to set partition UUID: %m");
3042 log_info("Initializing UUID of existing partition %" PRIu64
".", p
->partno
);
3046 if (!streq_ptr(p
->new_label
, p
->current_label
)) {
3047 assert(!isempty(p
->new_label
));
3049 r
= fdisk_partition_set_name(p
->current_partition
, p
->new_label
);
3051 return log_error_errno(r
, "Failed to set partition label: %m");
3053 log_info("Setting partition label of existing partition %" PRIu64
".", p
->partno
);
3058 assert(!PARTITION_IS_FOREIGN(p
)); /* never touch foreign partitions */
3060 r
= fdisk_set_partition(context
->fdisk_context
, p
->partno
, p
->current_partition
);
3062 return log_error_errno(r
, "Failed to update partition: %m");
3065 _cleanup_(fdisk_unref_partitionp
) struct fdisk_partition
*q
= NULL
;
3066 _cleanup_(fdisk_unref_parttypep
) struct fdisk_parttype
*t
= NULL
;
3067 char ids
[ID128_UUID_STRING_MAX
];
3069 assert(!p
->new_partition
);
3070 assert(p
->offset
% 512 == 0);
3071 assert(p
->new_size
% 512 == 0);
3072 assert(!sd_id128_is_null(p
->new_uuid
));
3073 assert(!isempty(p
->new_label
));
3075 t
= fdisk_new_parttype();
3079 r
= fdisk_parttype_set_typestr(t
, id128_to_uuid_string(p
->type_uuid
, ids
));
3081 return log_error_errno(r
, "Failed to initialize partition type: %m");
3083 q
= fdisk_new_partition();
3087 r
= fdisk_partition_set_type(q
, t
);
3089 return log_error_errno(r
, "Failed to set partition type: %m");
3091 r
= fdisk_partition_size_explicit(q
, true);
3093 return log_error_errno(r
, "Failed to enable explicit sizing: %m");
3095 r
= fdisk_partition_set_start(q
, p
->offset
/ 512);
3097 return log_error_errno(r
, "Failed to position partition: %m");
3099 r
= fdisk_partition_set_size(q
, p
->new_size
/ 512);
3101 return log_error_errno(r
, "Failed to grow partition: %m");
3103 r
= fdisk_partition_set_partno(q
, p
->partno
);
3105 return log_error_errno(r
, "Failed to set partition number: %m");
3107 r
= fdisk_partition_set_uuid(q
, id128_to_uuid_string(p
->new_uuid
, ids
));
3109 return log_error_errno(r
, "Failed to set partition UUID: %m");
3111 r
= fdisk_partition_set_name(q
, p
->new_label
);
3113 return log_error_errno(r
, "Failed to set partition label: %m");
3115 log_info("Adding new partition %" PRIu64
" to partition table.", p
->partno
);
3117 r
= fdisk_add_partition(context
->fdisk_context
, q
, NULL
);
3119 return log_error_errno(r
, "Failed to add partition: %m");
3121 assert(!p
->new_partition
);
3122 p
->new_partition
= TAKE_PTR(q
);
3129 static int context_write_partition_table(
3132 bool from_scratch
) {
3134 _cleanup_(fdisk_unref_tablep
) struct fdisk_table
*original_table
= NULL
;
3139 if (arg_pretty
> 0 ||
3140 (arg_pretty
< 0 && isatty(STDOUT_FILENO
) > 0) ||
3143 (void) context_dump_partitions(context
, node
);
3148 (void) context_dump_partition_bar(context
, node
);
3153 if (!from_scratch
&& !context_changed(context
)) {
3154 log_info("No changes.");
3159 log_notice("Refusing to repartition, please re-run with --dry-run=no.");
3163 log_info("Applying changes.");
3166 r
= context_wipe_range(context
, 0, context
->total
);
3170 log_info("Wiped block device.");
3172 r
= context_discard_range(context
, 0, context
->total
);
3173 if (r
== -EOPNOTSUPP
)
3174 log_info("Storage does not support discard, not discarding entire block device data.");
3176 return log_error_errno(r
, "Failed to discard entire block device: %m");
3178 log_info("Discarded entire block device.");
3181 r
= fdisk_get_partitions(context
->fdisk_context
, &original_table
);
3183 return log_error_errno(r
, "Failed to acquire partition table: %m");
3185 /* Wipe fs signatures and discard sectors where the new partitions are going to be placed and in the
3186 * gaps between partitions, just to be sure. */
3187 r
= context_wipe_and_discard(context
, from_scratch
);
3191 r
= context_copy_blocks(context
);
3195 r
= context_mkfs(context
);
3199 r
= context_mangle_partitions(context
);
3203 log_info("Writing new partition table.");
3205 r
= fdisk_write_disklabel(context
->fdisk_context
);
3207 return log_error_errno(r
, "Failed to write partition table: %m");
3209 capable
= blockdev_partscan_enabled(fdisk_get_devfd(context
->fdisk_context
));
3210 if (capable
== -ENOTBLK
)
3211 log_debug("Not telling kernel to reread partition table, since we are not operating on a block device.");
3212 else if (capable
< 0)
3213 return log_error_errno(capable
, "Failed to check if block device supports partition scanning: %m");
3214 else if (capable
> 0) {
3215 log_info("Telling kernel to reread partition table.");
3218 r
= fdisk_reread_partition_table(context
->fdisk_context
);
3220 r
= fdisk_reread_changes(context
->fdisk_context
, original_table
);
3222 return log_error_errno(r
, "Failed to reread partition table: %m");
3224 log_notice("Not telling kernel to reread partition table, because selected image does not support kernel partition block devices.");
3226 log_info("All done.");
3231 static int context_read_seed(Context
*context
, const char *root
) {
3236 if (!sd_id128_is_null(context
->seed
))
3239 if (!arg_randomize
) {
3240 _cleanup_close_
int fd
= -1;
3242 fd
= chase_symlinks_and_open("/etc/machine-id", root
, CHASE_PREFIX_ROOT
, O_RDONLY
|O_CLOEXEC
, NULL
);
3244 log_info("No machine ID set, using randomized partition UUIDs.");
3246 return log_error_errno(fd
, "Failed to determine machine ID of image: %m");
3248 r
= id128_read_fd(fd
, ID128_PLAIN
, &context
->seed
);
3249 if (r
== -ENOMEDIUM
)
3250 log_info("No machine ID set, using randomized partition UUIDs.");
3252 return log_error_errno(r
, "Failed to parse machine ID of image: %m");
3258 r
= sd_id128_randomize(&context
->seed
);
3260 return log_error_errno(r
, "Failed to generate randomized seed: %m");
3265 static int context_factory_reset(Context
*context
, bool from_scratch
) {
3272 if (arg_factory_reset
<= 0)
3275 if (from_scratch
) /* Nothing to reset if we start from scratch */
3279 log_notice("Refusing to factory reset, please re-run with --dry-run=no.");
3283 log_info("Applying factory reset.");
3285 LIST_FOREACH(partitions
, p
, context
->partitions
) {
3287 if (!p
->factory_reset
|| !PARTITION_EXISTS(p
))
3290 assert(p
->partno
!= UINT64_MAX
);
3292 log_info("Removing partition %" PRIu64
" for factory reset.", p
->partno
);
3294 r
= fdisk_delete_partition(context
->fdisk_context
, p
->partno
);
3296 return log_error_errno(r
, "Failed to remove partition %" PRIu64
": %m", p
->partno
);
3302 log_info("Factory reset requested, but no partitions to delete found.");
3306 r
= fdisk_write_disklabel(context
->fdisk_context
);
3308 return log_error_errno(r
, "Failed to write disk label: %m");
3310 log_info("Successfully deleted %zu partitions.", n
);
3314 static int context_can_factory_reset(Context
*context
) {
3319 LIST_FOREACH(partitions
, p
, context
->partitions
)
3320 if (p
->factory_reset
&& PARTITION_EXISTS(p
))
3326 static int context_open_copy_block_paths(Context
*context
) {
3332 LIST_FOREACH(partitions
, p
, context
->partitions
) {
3333 _cleanup_close_
int source_fd
= -1;
3337 assert(p
->copy_blocks_fd
< 0);
3338 assert(p
->copy_blocks_size
== UINT64_MAX
);
3340 if (PARTITION_EXISTS(p
)) /* Never copy over partitions that already exist! */
3343 if (!p
->copy_blocks_path
)
3346 source_fd
= open(p
->copy_blocks_path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
3348 return log_error_errno(errno
, "Failed to open block copy file '%s': %m", p
->copy_blocks_path
);
3350 if (fstat(source_fd
, &st
) < 0)
3351 return log_error_errno(errno
, "Failed to stat block copy file '%s': %m", p
->copy_blocks_path
);
3353 if (S_ISDIR(st
.st_mode
)) {
3354 _cleanup_free_
char *bdev
= NULL
;
3356 /* If the file is a directory, automatically find the backing block device */
3358 if (major(st
.st_dev
) != 0)
3359 r
= device_path_make_major_minor(S_IFBLK
, st
.st_dev
, &bdev
);
3363 /* Special support for btrfs */
3365 r
= btrfs_get_block_device_fd(source_fd
, &devt
);
3367 return btrfs_log_dev_root(LOG_ERR
, r
, p
->copy_blocks_path
);
3369 return log_error_errno(r
, "Unable to determine backing block device of '%s': %m", p
->copy_blocks_path
);
3371 r
= device_path_make_major_minor(S_IFBLK
, devt
, &bdev
);
3374 return log_error_errno(r
, "Failed to determine block device path for block device backing '%s': %m", p
->copy_blocks_path
);
3376 safe_close(source_fd
);
3378 source_fd
= open(bdev
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
3380 return log_error_errno(errno
, "Failed to open block device '%s': %m", bdev
);
3382 if (fstat(source_fd
, &st
) < 0)
3383 return log_error_errno(errno
, "Failed to stat block device '%s': %m", bdev
);
3385 if (!S_ISBLK(st
.st_mode
))
3386 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK
), "Block device '%s' is not actually a block device, refusing.", bdev
);
3389 if (S_ISREG(st
.st_mode
))
3391 else if (S_ISBLK(st
.st_mode
)) {
3392 if (ioctl(source_fd
, BLKGETSIZE64
, &size
) != 0)
3393 return log_error_errno(errno
, "Failed to determine size of block device to copy from: %m");
3395 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified path to copy blocks from '%s' is not a regular file, block device or directory, refusing: %m", p
->copy_blocks_path
);
3398 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "File to copy bytes from '%s' has zero size, refusing.", p
->copy_blocks_path
);
3399 if (size
% 512 != 0)
3400 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "File to copy bytes from '%s' has size that is not multiple of 512, refusing.", p
->copy_blocks_path
);
3402 p
->copy_blocks_fd
= TAKE_FD(source_fd
);
3403 p
->copy_blocks_size
= size
;
3409 static int help(void) {
3410 _cleanup_free_
char *link
= NULL
;
3413 r
= terminal_urlify_man("systemd-repart", "1", &link
);
3417 printf("%s [OPTIONS...] [DEVICE]\n"
3418 "\n%sGrow and add partitions to partition table.%s\n\n"
3419 " -h --help Show this help\n"
3420 " --version Show package version\n"
3421 " --dry-run=BOOL Whether to run dry-run operation\n"
3422 " --empty=MODE One of refuse, allow, require, force, create; controls\n"
3423 " how to handle empty disks lacking partition tables\n"
3424 " --discard=BOOL Whether to discard backing blocks for new partitions\n"
3425 " --pretty=BOOL Whether to show pretty summary before doing changes\n"
3426 " --factory-reset=BOOL Whether to remove data partitions before recreating\n"
3428 " --can-factory-reset Test whether factory reset is defined\n"
3429 " --root=PATH Operate relative to root path\n"
3430 " --definitions=DIR Find partitions in specified directory\n"
3431 " --key-file=PATH Key to use when encrypting partitions\n"
3432 " --seed=UUID 128bit seed UUID to derive all UUIDs from\n"
3433 " --size=BYTES Grow loopback file to specified size\n"
3434 " --json=pretty|short|off\n"
3435 " Generate JSON output\n"
3436 "\nSee the %s for details.\n"
3437 , program_invocation_short_name
3438 , ansi_highlight(), ansi_normal()
3445 static int parse_argv(int argc
, char *argv
[]) {
3448 ARG_VERSION
= 0x100,
3453 ARG_CAN_FACTORY_RESET
,
3463 static const struct option options
[] = {
3464 { "help", no_argument
, NULL
, 'h' },
3465 { "version", no_argument
, NULL
, ARG_VERSION
},
3466 { "dry-run", required_argument
, NULL
, ARG_DRY_RUN
},
3467 { "empty", required_argument
, NULL
, ARG_EMPTY
},
3468 { "discard", required_argument
, NULL
, ARG_DISCARD
},
3469 { "factory-reset", required_argument
, NULL
, ARG_FACTORY_RESET
},
3470 { "can-factory-reset", no_argument
, NULL
, ARG_CAN_FACTORY_RESET
},
3471 { "root", required_argument
, NULL
, ARG_ROOT
},
3472 { "seed", required_argument
, NULL
, ARG_SEED
},
3473 { "pretty", required_argument
, NULL
, ARG_PRETTY
},
3474 { "definitions", required_argument
, NULL
, ARG_DEFINITIONS
},
3475 { "size", required_argument
, NULL
, ARG_SIZE
},
3476 { "json", required_argument
, NULL
, ARG_JSON
},
3477 { "key-file", required_argument
, NULL
, ARG_KEY_FILE
},
3481 int c
, r
, dry_run
= -1;
3486 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
3497 r
= parse_boolean(optarg
);
3499 return log_error_errno(r
, "Failed to parse --dry-run= parameter: %s", optarg
);
3505 if (isempty(optarg
) || streq(optarg
, "refuse"))
3506 arg_empty
= EMPTY_REFUSE
;
3507 else if (streq(optarg
, "allow"))
3508 arg_empty
= EMPTY_ALLOW
;
3509 else if (streq(optarg
, "require"))
3510 arg_empty
= EMPTY_REQUIRE
;
3511 else if (streq(optarg
, "force"))
3512 arg_empty
= EMPTY_FORCE
;
3513 else if (streq(optarg
, "create")) {
3514 arg_empty
= EMPTY_CREATE
;
3517 dry_run
= false; /* Imply --dry-run=no if we create the loopback file
3518 * anew. After all we cannot really break anyone's
3519 * partition tables that way. */
3521 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3522 "Failed to parse --empty= parameter: %s", optarg
);
3526 r
= parse_boolean(optarg
);
3528 return log_error_errno(r
, "Failed to parse --discard= parameter: %s", optarg
);
3533 case ARG_FACTORY_RESET
:
3534 r
= parse_boolean(optarg
);
3536 return log_error_errno(r
, "Failed to parse --factory-reset= parameter: %s", optarg
);
3538 arg_factory_reset
= r
;
3541 case ARG_CAN_FACTORY_RESET
:
3542 arg_can_factory_reset
= true;
3546 r
= parse_path_argument_and_warn(optarg
, false, &arg_root
);
3552 if (isempty(optarg
)) {
3553 arg_seed
= SD_ID128_NULL
;
3554 arg_randomize
= false;
3555 } else if (streq(optarg
, "random"))
3556 arg_randomize
= true;
3558 r
= sd_id128_from_string(optarg
, &arg_seed
);
3560 return log_error_errno(r
, "Failed to parse seed: %s", optarg
);
3562 arg_randomize
= false;
3568 r
= parse_boolean(optarg
);
3570 return log_error_errno(r
, "Failed to parse --pretty= parameter: %s", optarg
);
3575 case ARG_DEFINITIONS
:
3576 r
= parse_path_argument_and_warn(optarg
, false, &arg_definitions
);
3582 uint64_t parsed
, rounded
;
3584 if (streq(optarg
, "auto")) {
3585 arg_size
= UINT64_MAX
;
3586 arg_size_auto
= true;
3590 r
= parse_size(optarg
, 1024, &parsed
);
3592 return log_error_errno(r
, "Failed to parse --size= parameter: %s", optarg
);
3594 rounded
= round_up_size(parsed
, 4096);
3596 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Specified image size too small, refusing.");
3597 if (rounded
== UINT64_MAX
)
3598 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Specified image size too large, refusing.");
3600 if (rounded
!= parsed
)
3601 log_warning("Specified size is not a multiple of 4096, rounding up automatically. (%" PRIu64
" → %" PRIu64
")",
3605 arg_size_auto
= false;
3610 if (streq(optarg
, "pretty")) {
3612 arg_json_format_flags
= JSON_FORMAT_PRETTY
|JSON_FORMAT_COLOR_AUTO
;
3613 } else if (streq(optarg
, "short")) {
3615 arg_json_format_flags
= JSON_FORMAT_NEWLINE
;
3616 } else if (streq(optarg
, "off")) {
3618 arg_json_format_flags
= 0;
3619 } else if (streq(optarg
, "help")) {
3625 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown argument to --json=: %s", optarg
);
3629 case ARG_KEY_FILE
: {
3630 _cleanup_(erase_and_freep
) char *k
= NULL
;
3633 r
= read_full_file_full(AT_FDCWD
, optarg
, READ_FULL_FILE_SECURE
|READ_FULL_FILE_CONNECT_SOCKET
, &k
, &n
);
3635 return log_error_errno(r
, "Failed to read key file '%s': %m", optarg
);
3637 erase_and_free(arg_key
);
3638 arg_key
= TAKE_PTR(k
);
3647 assert_not_reached("Unhandled option");
3650 if (argc
- optind
> 1)
3651 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3652 "Expected at most one argument, the path to the block device.");
3654 if (arg_factory_reset
> 0 && IN_SET(arg_empty
, EMPTY_FORCE
, EMPTY_REQUIRE
, EMPTY_CREATE
))
3655 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3656 "Combination of --factory-reset=yes and --empty=force/--empty=require/--empty=create is invalid.");
3658 if (arg_can_factory_reset
)
3659 arg_dry_run
= true; /* When --can-factory-reset is specified we don't make changes, hence
3660 * non-dry-run mode makes no sense. Thus, imply dry run mode so that we
3661 * open things strictly read-only. */
3662 else if (dry_run
>= 0)
3663 arg_dry_run
= dry_run
;
3665 if (arg_empty
== EMPTY_CREATE
&& (arg_size
== UINT64_MAX
&& !arg_size_auto
))
3666 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3667 "If --empty=create is specified, --size= must be specified, too.");
3669 arg_node
= argc
> optind
? argv
[optind
] : NULL
;
3671 if (IN_SET(arg_empty
, EMPTY_FORCE
, EMPTY_REQUIRE
, EMPTY_CREATE
) && !arg_node
)
3672 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
3673 "A path to a device node or loopback file must be specified when --empty=force, --empty=require or --empty=create are used.");
3678 static int parse_proc_cmdline_factory_reset(void) {
3682 if (arg_factory_reset
>= 0) /* Never override what is specified on the process command line */
3685 if (!in_initrd()) /* Never honour kernel command line factory reset request outside of the initrd */
3688 r
= proc_cmdline_get_bool("systemd.factory_reset", &b
);
3690 return log_error_errno(r
, "Failed to parse systemd.factory_reset kernel command line argument: %m");
3692 arg_factory_reset
= b
;
3695 log_notice("Honouring factory reset requested via kernel command line.");
3701 static int parse_efi_variable_factory_reset(void) {
3702 _cleanup_free_
char *value
= NULL
;
3705 if (arg_factory_reset
>= 0) /* Never override what is specified on the process command line */
3708 if (!in_initrd()) /* Never honour EFI variable factory reset request outside of the initrd */
3711 r
= efi_get_variable_string(EFI_VENDOR_SYSTEMD
, "FactoryReset", &value
);
3712 if (r
== -ENOENT
|| ERRNO_IS_NOT_SUPPORTED(r
))
3715 return log_error_errno(r
, "Failed to read EFI variable FactoryReset: %m");
3717 r
= parse_boolean(value
);
3719 return log_error_errno(r
, "Failed to parse EFI variable FactoryReset: %m");
3721 arg_factory_reset
= r
;
3723 log_notice("Honouring factory reset requested via EFI variable FactoryReset: %m");
3728 static int remove_efi_variable_factory_reset(void) {
3731 r
= efi_set_variable(EFI_VENDOR_SYSTEMD
, "FactoryReset", NULL
, 0);
3732 if (r
== -ENOENT
|| ERRNO_IS_NOT_SUPPORTED(r
))
3735 return log_error_errno(r
, "Failed to remove EFI variable FactoryReset: %m");
3737 log_info("Successfully unset EFI variable FactoryReset.");
3741 static int acquire_root_devno(const char *p
, int mode
, char **ret
, int *ret_fd
) {
3742 _cleanup_close_
int fd
= -1;
3744 dev_t devno
, fd_devno
= (mode_t
) -1;
3755 if (fstat(fd
, &st
) < 0)
3758 if (S_ISREG(st
.st_mode
)) {
3766 *ret_fd
= TAKE_FD(fd
);
3771 if (S_ISBLK(st
.st_mode
))
3772 fd_devno
= devno
= st
.st_rdev
;
3773 else if (S_ISDIR(st
.st_mode
)) {
3776 if (major(devno
) == 0) {
3777 r
= btrfs_get_block_device_fd(fd
, &devno
);
3778 if (r
== -ENOTTY
) /* not btrfs */
3786 /* From dm-crypt to backing partition */
3787 r
= block_get_originating(devno
, &devno
);
3789 log_debug_errno(r
, "Failed to find underlying block device for '%s', ignoring: %m", p
);
3791 /* From partition to whole disk containing it */
3792 r
= block_get_whole_disk(devno
, &devno
);
3794 log_debug_errno(r
, "Failed to find whole disk block device for '%s', ignoring: %m", p
);
3796 r
= device_path_make_canonical(S_IFBLK
, devno
, ret
);
3798 return log_debug_errno(r
, "Failed to determine canonical path for '%s': %m", p
);
3800 /* Only if we still lock at the same block device we can reuse the fd. Otherwise return an
3801 * invalidated fd. */
3802 *ret_fd
= fd_devno
!= (mode_t
) -1 && fd_devno
== devno
? TAKE_FD(fd
) : -1;
3806 static int find_root(char **ret
, int *ret_fd
) {
3814 if (arg_empty
== EMPTY_CREATE
) {
3815 _cleanup_close_
int fd
= -1;
3816 _cleanup_free_
char *s
= NULL
;
3818 s
= strdup(arg_node
);
3822 fd
= open(arg_node
, O_RDONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
|O_NOFOLLOW
, 0666);
3824 return log_error_errno(errno
, "Failed to create '%s': %m", arg_node
);
3827 *ret_fd
= TAKE_FD(fd
);
3831 r
= acquire_root_devno(arg_node
, O_RDONLY
|O_CLOEXEC
, ret
, ret_fd
);
3833 return btrfs_log_dev_root(LOG_ERR
, r
, arg_node
);
3835 return log_error_errno(r
, "Failed to open file or determine backing device of %s: %m", arg_node
);
3840 assert(IN_SET(arg_empty
, EMPTY_REFUSE
, EMPTY_ALLOW
));
3842 /* Let's search for the root device. We look for two cases here: first in /, and then in /usr. The
3843 * latter we check for cases where / is a tmpfs and only /usr is an actual persistent block device
3844 * (think: volatile setups) */
3846 FOREACH_STRING(t
, "/", "/usr") {
3847 _cleanup_free_
char *j
= NULL
;
3851 j
= path_join("/sysroot", t
);
3859 r
= acquire_root_devno(p
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
, ret
, ret_fd
);
3862 return btrfs_log_dev_root(LOG_ERR
, r
, p
);
3864 return log_error_errno(r
, "Failed to determine backing device of %s: %m", p
);
3869 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "Failed to discover root block device.");
3872 static int resize_backing_fd(const char *node
, int *fd
) {
3873 char buf1
[FORMAT_BYTES_MAX
], buf2
[FORMAT_BYTES_MAX
];
3874 _cleanup_close_
int writable_fd
= -1;
3881 if (arg_size
== UINT64_MAX
) /* Nothing to do */
3885 /* Open the file if we haven't opened it yet. Note that we open it read-only here, just to
3886 * keep a reference to the file we can pass around. */
3887 *fd
= open(node
, O_RDONLY
|O_CLOEXEC
);
3889 return log_error_errno(errno
, "Failed to open '%s' in order to adjust size: %m", node
);
3892 if (fstat(*fd
, &st
) < 0)
3893 return log_error_errno(errno
, "Failed to stat '%s': %m", node
);
3895 r
= stat_verify_regular(&st
);
3897 return log_error_errno(r
, "Specified path '%s' is not a regular file, cannot resize: %m", node
);
3899 assert_se(format_bytes(buf1
, sizeof(buf1
), st
.st_size
));
3900 assert_se(format_bytes(buf2
, sizeof(buf2
), arg_size
));
3902 if ((uint64_t) st
.st_size
>= arg_size
) {
3903 log_info("File '%s' already is of requested size or larger, not growing. (%s >= %s)", node
, buf1
, buf2
);
3907 /* The file descriptor is read-only. In order to grow the file we need to have a writable fd. We
3908 * reopen the file for that temporarily. We keep the writable fd only open for this operation though,
3909 * as fdisk can't accept it anyway. */
3911 writable_fd
= fd_reopen(*fd
, O_WRONLY
|O_CLOEXEC
);
3912 if (writable_fd
< 0)
3913 return log_error_errno(writable_fd
, "Failed to reopen backing file '%s' writable: %m", node
);
3916 if (fallocate(writable_fd
, 0, 0, arg_size
) < 0) {
3917 if (!ERRNO_IS_NOT_SUPPORTED(errno
))
3918 return log_error_errno(errno
, "Failed to grow '%s' from %s to %s by allocation: %m",
3921 /* Fallback to truncation, if fallocate() is not supported. */
3922 log_debug("Backing file system does not support fallocate(), falling back to ftruncate().");
3924 if (st
.st_size
== 0) /* Likely regular file just created by us */
3925 log_info("Allocated %s for '%s'.", buf2
, node
);
3927 log_info("File '%s' grown from %s to %s by allocation.", node
, buf1
, buf2
);
3933 if (ftruncate(writable_fd
, arg_size
) < 0)
3934 return log_error_errno(errno
, "Failed to grow '%s' from %s to %s by truncation: %m",
3937 if (st
.st_size
== 0) /* Likely regular file just created by us */
3938 log_info("Sized '%s' to %s.", node
, buf2
);
3940 log_info("File '%s' grown from %s to %s by truncation.", node
, buf1
, buf2
);
3945 static int determine_auto_size(Context
*c
) {
3946 uint64_t sum
= round_up_size(GPT_METADATA_SIZE
, 4096);
3947 char buf
[FORMAT_BYTES_MAX
];
3951 assert_se(arg_size
== UINT64_MAX
);
3952 assert_se(arg_size_auto
);
3954 LIST_FOREACH(partitions
, p
, c
->partitions
) {
3960 m
= partition_min_size_with_padding(p
);
3961 if (m
> UINT64_MAX
- sum
)
3962 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "Image would grow too large, refusing.");
3967 assert_se(format_bytes(buf
, sizeof(buf
), sum
));
3968 log_info("Automatically determined minimal disk image size as %s.", buf
);
3974 static int run(int argc
, char *argv
[]) {
3975 _cleanup_(context_freep
) Context
* context
= NULL
;
3976 _cleanup_free_
char *node
= NULL
;
3977 _cleanup_close_
int backing_fd
= -1;
3981 log_show_color(true);
3982 log_parse_environment();
3986 /* Default to operation on /sysroot when invoked in the initrd! */
3987 arg_root
= strdup("/sysroot");
3992 r
= parse_argv(argc
, argv
);
3996 r
= parse_proc_cmdline_factory_reset();
4000 r
= parse_efi_variable_factory_reset();
4004 context
= context_new(arg_seed
);
4008 r
= context_read_definitions(context
, arg_definitions
, arg_root
);
4012 if (context
->n_partitions
<= 0 && arg_empty
== EMPTY_REFUSE
) {
4013 log_info("Didn't find any partition definition files, nothing to do.");
4017 r
= find_root(&node
, &backing_fd
);
4021 if (arg_size
!= UINT64_MAX
) {
4022 r
= resize_backing_fd(node
, &backing_fd
);
4027 r
= context_load_partition_table(context
, node
, &backing_fd
);
4028 if (r
== -EHWPOISON
)
4029 return 77; /* Special return value which means "Not GPT, so not doing anything". This isn't
4030 * really an error when called at boot. */
4033 from_scratch
= r
> 0; /* Starting from scratch */
4035 if (arg_can_factory_reset
) {
4036 r
= context_can_factory_reset(context
);
4040 return EXIT_FAILURE
;
4045 r
= context_factory_reset(context
, from_scratch
);
4049 /* We actually did a factory reset! */
4050 r
= remove_efi_variable_factory_reset();
4054 /* Reload the reduced partition table */
4055 context_unload_partition_table(context
);
4056 r
= context_load_partition_table(context
, node
, &backing_fd
);
4062 (void) context_dump_partitions(context
, node
);
4066 r
= context_read_seed(context
, arg_root
);
4070 /* Open all files to copy blocks from now, since we want to take their size into consideration */
4071 r
= context_open_copy_block_paths(context
);
4075 if (arg_size_auto
) {
4076 r
= determine_auto_size(context
);
4080 /* Flush out everything again, and let's grow the file first, then start fresh */
4081 context_unload_partition_table(context
);
4083 assert_se(arg_size
!= UINT64_MAX
);
4084 r
= resize_backing_fd(node
, &backing_fd
);
4088 r
= context_load_partition_table(context
, node
, &backing_fd
);
4093 /* First try to fit new partitions in, dropping by priority until it fits */
4095 if (context_allocate_partitions(context
))
4096 break; /* Success! */
4098 if (!context_drop_one_priority(context
))
4099 return log_error_errno(SYNTHETIC_ERRNO(ENOSPC
),
4100 "Can't fit requested partitions into free space, refusing.");
4103 /* Now assign free space according to the weight logic */
4104 r
= context_grow_partitions(context
);
4108 /* Now calculate where each partition gets placed */
4109 context_place_partitions(context
);
4111 /* Make sure each partition has a unique UUID and unique label */
4112 r
= context_acquire_partition_uuids_and_labels(context
);
4116 r
= context_write_partition_table(context
, node
, from_scratch
);
4123 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);