1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #if HAVE_VALGRIND_MEMCHECK_H
4 #include <valgrind/memcheck.h>
10 #include <linux/loop.h>
12 #include <sys/ioctl.h>
15 #include "sd-device.h"
18 #include "alloc-util.h"
19 #include "blkid-util.h"
20 #include "blockdev-util.h"
21 #include "btrfs-util.h"
24 #include "conf-files.h"
25 #include "conf-parser.h"
26 #include "constants.h"
27 #include "cryptsetup-util.h"
28 #include "device-util.h"
29 #include "devnum-util.h"
30 #include "dirent-util.h"
32 #include "errno-util.h"
34 #include "fdisk-util.h"
36 #include "format-table.h"
37 #include "format-util.h"
39 #include "glyph-util.h"
41 #include "hexdecoct.h"
43 #include "id128-util.h"
44 #include "initrd-util.h"
48 #include "loop-util.h"
49 #include "main-func.h"
51 #include "mkfs-util.h"
52 #include "mount-util.h"
53 #include "mountpoint-util.h"
54 #include "nulstr-util.h"
55 #include "openssl-util.h"
56 #include "parse-argument.h"
57 #include "parse-helpers.h"
58 #include "pretty-print.h"
59 #include "proc-cmdline.h"
60 #include "process-util.h"
61 #include "random-util.h"
62 #include "resize-fs.h"
64 #include "sort-util.h"
65 #include "specifier.h"
66 #include "stdio-util.h"
67 #include "string-table.h"
68 #include "string-util.h"
70 #include "sync-util.h"
71 #include "terminal-util.h"
72 #include "tmpfile-util.h"
74 #include "tpm2-util.h"
75 #include "user-util.h"
78 /* If not configured otherwise use a minimal partition size of 10M */
79 #define DEFAULT_MIN_SIZE (10ULL*1024ULL*1024ULL)
81 /* Hard lower limit for new partition sizes */
82 #define HARD_MIN_SIZE 4096ULL
84 /* We know up front we're never going to put more than this in a verity sig partition. */
85 #define VERITY_SIG_SIZE (HARD_MIN_SIZE*4ULL)
87 /* libfdisk takes off slightly more than 1M of the disk size when creating a GPT disk label */
88 #define GPT_METADATA_SIZE (1044ULL*1024ULL)
90 /* LUKS2 takes off 16M of the partition size with its metadata by default */
91 #define LUKS2_METADATA_SIZE (16ULL*1024ULL*1024ULL)
93 /* To do LUKS2 offline encryption, we need to keep some extra free space at the end of the partition. */
94 #define LUKS2_METADATA_KEEP_FREE (LUKS2_METADATA_SIZE*2ULL)
96 /* LUKS2 volume key size. */
97 #define VOLUME_KEY_SIZE (512ULL/8ULL)
99 /* Use 4K as the default filesystem sector size because as long as the partitions are aligned to 4K, the
100 * filesystems will then also be compatible with sector sizes 512, 1024 and 2048. */
101 #define DEFAULT_FILESYSTEM_SECTOR_SIZE 4096ULL
103 #define APIVFS_TMP_DIRS_NULSTR "proc\0sys\0dev\0tmp\0run\0var/tmp\0"
105 /* Note: When growing and placing new partitions we always align to 4K sector size. It's how newer hard disks
106 * are designed, and if everything is aligned to that performance is best. And for older hard disks with 512B
107 * sector size devices were generally assumed to have an even number of sectors, hence at the worst we'll
108 * waste 3K per partition, which is probably fine. */
110 typedef enum EmptyMode
{
111 EMPTY_UNSET
, /* no choice has been made yet */
112 EMPTY_REFUSE
, /* refuse empty disks, never create a partition table */
113 EMPTY_ALLOW
, /* allow empty disks, create partition table if necessary */
114 EMPTY_REQUIRE
, /* require an empty disk, create a partition table */
115 EMPTY_FORCE
, /* make disk empty, erase everything, create a partition table always */
116 EMPTY_CREATE
, /* create disk as loopback file, create a partition table always */
118 _EMPTY_MODE_INVALID
= -EINVAL
,
121 typedef enum FilterPartitionType
{
122 FILTER_PARTITIONS_NONE
,
123 FILTER_PARTITIONS_EXCLUDE
,
124 FILTER_PARTITIONS_INCLUDE
,
125 _FILTER_PARTITIONS_MAX
,
126 _FILTER_PARTITIONS_INVALID
= -EINVAL
,
127 } FilterPartitionsType
;
129 static EmptyMode arg_empty
= EMPTY_UNSET
;
130 static bool arg_dry_run
= true;
131 static char *arg_node
= NULL
;
132 static char *arg_root
= NULL
;
133 static char *arg_image
= NULL
;
134 static char **arg_definitions
= NULL
;
135 static bool arg_discard
= true;
136 static bool arg_can_factory_reset
= false;
137 static int arg_factory_reset
= -1;
138 static sd_id128_t arg_seed
= SD_ID128_NULL
;
139 static bool arg_randomize
= false;
140 static int arg_pretty
= -1;
141 static uint64_t arg_size
= UINT64_MAX
;
142 static bool arg_size_auto
= false;
143 static JsonFormatFlags arg_json_format_flags
= JSON_FORMAT_OFF
;
144 static PagerFlags arg_pager_flags
= 0;
145 static bool arg_legend
= true;
146 static void *arg_key
= NULL
;
147 static size_t arg_key_size
= 0;
148 static EVP_PKEY
*arg_private_key
= NULL
;
149 static X509
*arg_certificate
= NULL
;
150 static char *arg_tpm2_device
= NULL
;
151 static uint32_t arg_tpm2_seal_key_handle
= 0;
152 static char *arg_tpm2_device_key
= NULL
;
153 static Tpm2PCRValue
*arg_tpm2_hash_pcr_values
= NULL
;
154 static size_t arg_tpm2_n_hash_pcr_values
= 0;
155 static char *arg_tpm2_public_key
= NULL
;
156 static uint32_t arg_tpm2_public_key_pcr_mask
= 0;
157 static char *arg_tpm2_pcrlock
= NULL
;
158 static bool arg_split
= false;
159 static GptPartitionType
*arg_filter_partitions
= NULL
;
160 static size_t arg_n_filter_partitions
= 0;
161 static FilterPartitionsType arg_filter_partitions_type
= FILTER_PARTITIONS_NONE
;
162 static GptPartitionType
*arg_defer_partitions
= NULL
;
163 static size_t arg_n_defer_partitions
= 0;
164 static uint64_t arg_sector_size
= 0;
165 static ImagePolicy
*arg_image_policy
= NULL
;
166 static Architecture arg_architecture
= _ARCHITECTURE_INVALID
;
167 static int arg_offline
= -1;
168 static char **arg_copy_from
= NULL
;
169 static char *arg_copy_source
= NULL
;
170 static char *arg_make_ddi
= NULL
;
171 static char *arg_generate_fstab
= NULL
;
172 static char *arg_generate_crypttab
= NULL
;
174 STATIC_DESTRUCTOR_REGISTER(arg_node
, freep
);
175 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
176 STATIC_DESTRUCTOR_REGISTER(arg_image
, freep
);
177 STATIC_DESTRUCTOR_REGISTER(arg_definitions
, strv_freep
);
178 STATIC_DESTRUCTOR_REGISTER(arg_key
, erase_and_freep
);
179 STATIC_DESTRUCTOR_REGISTER(arg_private_key
, EVP_PKEY_freep
);
180 STATIC_DESTRUCTOR_REGISTER(arg_certificate
, X509_freep
);
181 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device
, freep
);
182 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device_key
, freep
);
183 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_hash_pcr_values
, freep
);
184 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_public_key
, freep
);
185 STATIC_DESTRUCTOR_REGISTER(arg_tpm2_pcrlock
, freep
);
186 STATIC_DESTRUCTOR_REGISTER(arg_filter_partitions
, freep
);
187 STATIC_DESTRUCTOR_REGISTER(arg_image_policy
, image_policy_freep
);
188 STATIC_DESTRUCTOR_REGISTER(arg_copy_from
, strv_freep
);
189 STATIC_DESTRUCTOR_REGISTER(arg_copy_source
, freep
);
190 STATIC_DESTRUCTOR_REGISTER(arg_make_ddi
, freep
);
191 STATIC_DESTRUCTOR_REGISTER(arg_generate_fstab
, freep
);
192 STATIC_DESTRUCTOR_REGISTER(arg_generate_crypttab
, freep
);
194 typedef struct FreeArea FreeArea
;
196 typedef enum EncryptMode
{
200 ENCRYPT_KEY_FILE_TPM2
,
202 _ENCRYPT_MODE_INVALID
= -EINVAL
,
205 typedef enum VerityMode
{
211 _VERITY_MODE_INVALID
= -EINVAL
,
214 typedef enum MinimizeMode
{
219 _MINIMIZE_MODE_INVALID
= -EINVAL
,
222 typedef struct PartitionMountPoint
{
225 } PartitionMountPoint
;
227 static void partition_mountpoint_free_many(PartitionMountPoint
*f
, size_t n
) {
230 FOREACH_ARRAY(i
, f
, n
) {
238 typedef struct PartitionEncryptedVolume
{
242 } PartitionEncryptedVolume
;
244 static PartitionEncryptedVolume
* partition_encrypted_volume_free(PartitionEncryptedVolume
*c
) {
255 typedef struct Partition
{
256 char *definition_path
;
257 char **drop_in_files
;
259 GptPartitionType type
;
260 sd_id128_t current_uuid
, new_uuid
;
261 bool new_uuid_is_set
;
262 char *current_label
, *new_label
;
263 sd_id128_t fs_uuid
, luks_uuid
, verity_uuid
;
264 uint8_t verity_salt
[SHA256_DIGEST_SIZE
];
270 uint32_t weight
, padding_weight
;
272 uint64_t current_size
, new_size
;
273 uint64_t size_min
, size_max
;
275 uint64_t current_padding
, new_padding
;
276 uint64_t padding_min
, padding_max
;
281 struct fdisk_partition
*current_partition
;
282 struct fdisk_partition
*new_partition
;
283 FreeArea
*padding_area
;
284 FreeArea
*allocated_to_area
;
286 char *copy_blocks_path
;
287 bool copy_blocks_path_is_our_file
;
288 bool copy_blocks_auto
;
289 const char *copy_blocks_root
;
291 uint64_t copy_blocks_offset
;
292 uint64_t copy_blocks_size
;
296 char **exclude_files_source
;
297 char **exclude_files_target
;
298 char **make_directories
;
302 char *verity_match_key
;
303 MinimizeMode minimize
;
304 uint64_t verity_data_block_size
;
305 uint64_t verity_hash_block_size
;
312 struct iovec roothash
;
314 char *split_name_format
;
317 PartitionMountPoint
*mountpoints
;
318 size_t n_mountpoints
;
320 PartitionEncryptedVolume
*encrypted_volume
;
322 struct Partition
*siblings
[_VERITY_MODE_MAX
];
324 LIST_FIELDS(struct Partition
, partitions
);
327 #define PARTITION_IS_FOREIGN(p) (!(p)->definition_path)
328 #define PARTITION_EXISTS(p) (!!(p)->current_partition)
336 typedef struct Context
{
337 LIST_HEAD(Partition
, partitions
);
340 FreeArea
**free_areas
;
343 uint64_t start
, end
, total
;
345 struct fdisk_context
*fdisk_context
;
346 uint64_t sector_size
, grain_size
, fs_sector_size
;
351 bool node_is_our_file
;
357 static const char *empty_mode_table
[_EMPTY_MODE_MAX
] = {
358 [EMPTY_UNSET
] = "unset",
359 [EMPTY_REFUSE
] = "refuse",
360 [EMPTY_ALLOW
] = "allow",
361 [EMPTY_REQUIRE
] = "require",
362 [EMPTY_FORCE
] = "force",
363 [EMPTY_CREATE
] = "create",
366 static const char *encrypt_mode_table
[_ENCRYPT_MODE_MAX
] = {
367 [ENCRYPT_OFF
] = "off",
368 [ENCRYPT_KEY_FILE
] = "key-file",
369 [ENCRYPT_TPM2
] = "tpm2",
370 [ENCRYPT_KEY_FILE_TPM2
] = "key-file+tpm2",
373 static const char *verity_mode_table
[_VERITY_MODE_MAX
] = {
374 [VERITY_OFF
] = "off",
375 [VERITY_DATA
] = "data",
376 [VERITY_HASH
] = "hash",
377 [VERITY_SIG
] = "signature",
380 static const char *minimize_mode_table
[_MINIMIZE_MODE_MAX
] = {
381 [MINIMIZE_OFF
] = "off",
382 [MINIMIZE_BEST
] = "best",
383 [MINIMIZE_GUESS
] = "guess",
386 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(empty_mode
, EmptyMode
);
387 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(encrypt_mode
, EncryptMode
, ENCRYPT_KEY_FILE
);
388 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(verity_mode
, VerityMode
);
389 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(minimize_mode
, MinimizeMode
, MINIMIZE_BEST
);
391 static uint64_t round_down_size(uint64_t v
, uint64_t p
) {
395 static uint64_t round_up_size(uint64_t v
, uint64_t p
) {
397 v
= DIV_ROUND_UP(v
, p
);
399 if (v
> UINT64_MAX
/ p
)
400 return UINT64_MAX
; /* overflow */
405 static Partition
*partition_new(void) {
408 p
= new(Partition
, 1);
415 .current_size
= UINT64_MAX
,
416 .new_size
= UINT64_MAX
,
417 .size_min
= UINT64_MAX
,
418 .size_max
= UINT64_MAX
,
419 .current_padding
= UINT64_MAX
,
420 .new_padding
= UINT64_MAX
,
421 .padding_min
= UINT64_MAX
,
422 .padding_max
= UINT64_MAX
,
423 .partno
= UINT64_MAX
,
424 .offset
= UINT64_MAX
,
425 .copy_blocks_fd
= -EBADF
,
426 .copy_blocks_offset
= UINT64_MAX
,
427 .copy_blocks_size
= UINT64_MAX
,
431 .verity_data_block_size
= UINT64_MAX
,
432 .verity_hash_block_size
= UINT64_MAX
,
438 static Partition
* partition_free(Partition
*p
) {
442 free(p
->current_label
);
444 free(p
->definition_path
);
445 strv_free(p
->drop_in_files
);
447 if (p
->current_partition
)
448 fdisk_unref_partition(p
->current_partition
);
449 if (p
->new_partition
)
450 fdisk_unref_partition(p
->new_partition
);
452 if (p
->copy_blocks_path_is_our_file
)
453 unlink_and_free(p
->copy_blocks_path
);
455 free(p
->copy_blocks_path
);
456 safe_close(p
->copy_blocks_fd
);
459 strv_free(p
->copy_files
);
460 strv_free(p
->exclude_files_source
);
461 strv_free(p
->exclude_files_target
);
462 strv_free(p
->make_directories
);
463 strv_free(p
->subvolumes
);
464 free(p
->verity_match_key
);
466 iovec_done(&p
->roothash
);
468 free(p
->split_name_format
);
469 unlink_and_free(p
->split_path
);
471 partition_mountpoint_free_many(p
->mountpoints
, p
->n_mountpoints
);
472 p
->mountpoints
= NULL
;
473 p
->n_mountpoints
= 0;
475 partition_encrypted_volume_free(p
->encrypted_volume
);
480 static void partition_foreignize(Partition
*p
) {
482 assert(PARTITION_EXISTS(p
));
484 /* Reset several parameters set through definition file to make the partition foreign. */
486 p
->definition_path
= mfree(p
->definition_path
);
487 p
->drop_in_files
= strv_free(p
->drop_in_files
);
489 p
->copy_blocks_path
= mfree(p
->copy_blocks_path
);
490 p
->copy_blocks_fd
= safe_close(p
->copy_blocks_fd
);
491 p
->copy_blocks_root
= NULL
;
493 p
->format
= mfree(p
->format
);
494 p
->copy_files
= strv_free(p
->copy_files
);
495 p
->exclude_files_source
= strv_free(p
->exclude_files_source
);
496 p
->exclude_files_target
= strv_free(p
->exclude_files_target
);
497 p
->make_directories
= strv_free(p
->make_directories
);
498 p
->subvolumes
= strv_free(p
->subvolumes
);
499 p
->verity_match_key
= mfree(p
->verity_match_key
);
503 p
->padding_weight
= 0;
504 p
->size_min
= UINT64_MAX
;
505 p
->size_max
= UINT64_MAX
;
506 p
->padding_min
= UINT64_MAX
;
507 p
->padding_max
= UINT64_MAX
;
511 p
->verity
= VERITY_OFF
;
513 partition_mountpoint_free_many(p
->mountpoints
, p
->n_mountpoints
);
514 p
->mountpoints
= NULL
;
515 p
->n_mountpoints
= 0;
517 p
->encrypted_volume
= partition_encrypted_volume_free(p
->encrypted_volume
);
520 static bool partition_type_exclude(const GptPartitionType
*type
) {
521 if (arg_filter_partitions_type
== FILTER_PARTITIONS_NONE
)
524 for (size_t i
= 0; i
< arg_n_filter_partitions
; i
++)
525 if (sd_id128_equal(type
->uuid
, arg_filter_partitions
[i
].uuid
))
526 return arg_filter_partitions_type
== FILTER_PARTITIONS_EXCLUDE
;
528 return arg_filter_partitions_type
== FILTER_PARTITIONS_INCLUDE
;
531 static bool partition_type_defer(const GptPartitionType
*type
) {
532 for (size_t i
= 0; i
< arg_n_defer_partitions
; i
++)
533 if (sd_id128_equal(type
->uuid
, arg_defer_partitions
[i
].uuid
))
539 static Partition
* partition_unlink_and_free(Context
*context
, Partition
*p
) {
543 LIST_REMOVE(partitions
, context
->partitions
, p
);
545 assert(context
->n_partitions
> 0);
546 context
->n_partitions
--;
548 return partition_free(p
);
551 DEFINE_TRIVIAL_CLEANUP_FUNC(Partition
*, partition_free
);
553 static Context
*context_new(sd_id128_t seed
) {
556 context
= new(Context
, 1);
560 *context
= (Context
) {
570 static void context_free_free_areas(Context
*context
) {
573 for (size_t i
= 0; i
< context
->n_free_areas
; i
++)
574 free(context
->free_areas
[i
]);
576 context
->free_areas
= mfree(context
->free_areas
);
577 context
->n_free_areas
= 0;
580 static Context
*context_free(Context
*context
) {
584 while (context
->partitions
)
585 partition_unlink_and_free(context
, context
->partitions
);
586 assert(context
->n_partitions
== 0);
588 context_free_free_areas(context
);
590 if (context
->fdisk_context
)
591 fdisk_unref_context(context
->fdisk_context
);
593 safe_close(context
->backing_fd
);
594 if (context
->node_is_our_file
)
595 unlink_and_free(context
->node
);
599 return mfree(context
);
602 DEFINE_TRIVIAL_CLEANUP_FUNC(Context
*, context_free
);
604 static int context_add_free_area(
612 assert(!after
|| !after
->padding_area
);
614 if (!GREEDY_REALLOC(context
->free_areas
, context
->n_free_areas
+ 1))
617 a
= new(FreeArea
, 1);
626 context
->free_areas
[context
->n_free_areas
++] = a
;
629 after
->padding_area
= a
;
634 static void partition_drop_or_foreignize(Partition
*p
) {
635 if (!p
|| p
->dropped
|| PARTITION_IS_FOREIGN(p
))
638 if (PARTITION_EXISTS(p
)) {
639 log_info("Can't grow existing partition %s of priority %" PRIi32
", ignoring.",
640 strna(p
->current_label
?: p
->new_label
), p
->priority
);
642 /* Handle the partition as foreign. Do not set dropped flag. */
643 partition_foreignize(p
);
645 log_info("Can't fit partition %s of priority %" PRIi32
", dropping.",
646 p
->definition_path
, p
->priority
);
649 p
->allocated_to_area
= NULL
;
653 static bool context_drop_or_foreignize_one_priority(Context
*context
) {
654 int32_t priority
= 0;
656 LIST_FOREACH(partitions
, p
, context
->partitions
) {
660 priority
= MAX(priority
, p
->priority
);
663 /* Refuse to drop partitions with 0 or negative priorities or partitions of priorities that have at
664 * least one existing priority */
668 LIST_FOREACH(partitions
, p
, context
->partitions
) {
669 if (p
->priority
< priority
)
672 partition_drop_or_foreignize(p
);
674 /* We ensure that all verity sibling partitions have the same priority, so it's safe
675 * to drop all siblings here as well. */
677 for (VerityMode mode
= VERITY_OFF
+ 1; mode
< _VERITY_MODE_MAX
; mode
++)
678 partition_drop_or_foreignize(p
->siblings
[mode
]);
684 static uint64_t partition_min_size(const Context
*context
, const Partition
*p
) {
690 /* Calculate the disk space we really need at minimum for this partition. If the partition already
691 * exists the current size is what we really need. If it doesn't exist yet refuse to allocate less
694 * DEFAULT_MIN_SIZE is the default SizeMin= we configure if nothing else is specified. */
696 if (PARTITION_IS_FOREIGN(p
)) {
697 /* Don't allow changing size of partitions not managed by us */
698 assert(p
->current_size
!= UINT64_MAX
);
699 return p
->current_size
;
702 if (p
->verity
== VERITY_SIG
)
703 return VERITY_SIG_SIZE
;
705 sz
= p
->current_size
!= UINT64_MAX
? p
->current_size
: HARD_MIN_SIZE
;
707 if (!PARTITION_EXISTS(p
)) {
710 if (p
->encrypt
!= ENCRYPT_OFF
)
711 d
+= round_up_size(LUKS2_METADATA_KEEP_FREE
, context
->grain_size
);
713 if (p
->copy_blocks_size
!= UINT64_MAX
)
714 d
+= round_up_size(p
->copy_blocks_size
, context
->grain_size
);
715 else if (p
->format
|| p
->encrypt
!= ENCRYPT_OFF
) {
718 /* If we shall synthesize a file system, take minimal fs size into account (assumed to be 4K if not known) */
719 f
= p
->format
? round_up_size(minimal_size_by_fs_name(p
->format
), context
->grain_size
) : UINT64_MAX
;
720 d
+= f
== UINT64_MAX
? context
->grain_size
: f
;
727 return MAX(round_up_size(p
->size_min
!= UINT64_MAX
? p
->size_min
: DEFAULT_MIN_SIZE
, context
->grain_size
), sz
);
730 static uint64_t partition_max_size(const Context
*context
, const Partition
*p
) {
733 /* Calculate how large the partition may become at max. This is generally the configured maximum
734 * size, except when it already exists and is larger than that. In that case it's the existing size,
735 * since we never want to shrink partitions. */
740 if (PARTITION_IS_FOREIGN(p
)) {
741 /* Don't allow changing size of partitions not managed by us */
742 assert(p
->current_size
!= UINT64_MAX
);
743 return p
->current_size
;
746 if (p
->verity
== VERITY_SIG
)
747 return VERITY_SIG_SIZE
;
749 if (p
->size_max
== UINT64_MAX
)
752 sm
= round_down_size(p
->size_max
, context
->grain_size
);
754 if (p
->current_size
!= UINT64_MAX
)
755 sm
= MAX(p
->current_size
, sm
);
757 return MAX(partition_min_size(context
, p
), sm
);
760 static uint64_t partition_min_padding(const Partition
*p
) {
762 return p
->padding_min
!= UINT64_MAX
? p
->padding_min
: 0;
765 static uint64_t partition_max_padding(const Partition
*p
) {
767 return p
->padding_max
;
770 static uint64_t partition_min_size_with_padding(Context
*context
, const Partition
*p
) {
773 /* Calculate the disk space we need for this partition plus any free space coming after it. This
774 * takes user configured padding into account as well as any additional whitespace needed to align
775 * the next partition to 4K again. */
780 sz
= partition_min_size(context
, p
) + partition_min_padding(p
);
782 if (PARTITION_EXISTS(p
)) {
783 /* If the partition wasn't aligned, add extra space so that any we might add will be aligned */
784 assert(p
->offset
!= UINT64_MAX
);
785 return round_up_size(p
->offset
+ sz
, context
->grain_size
) - p
->offset
;
788 /* If this is a new partition we'll place it aligned, hence we just need to round up the required size here */
789 return round_up_size(sz
, context
->grain_size
);
792 static uint64_t free_area_available(const FreeArea
*a
) {
795 /* Determines how much of this free area is not allocated yet */
797 assert(a
->size
>= a
->allocated
);
798 return a
->size
- a
->allocated
;
801 static uint64_t free_area_current_end(Context
*context
, const FreeArea
*a
) {
806 return free_area_available(a
);
808 assert(a
->after
->offset
!= UINT64_MAX
);
809 assert(a
->after
->current_size
!= UINT64_MAX
);
811 /* Calculate where the free area ends, based on the offset of the partition preceding it. */
812 return round_up_size(a
->after
->offset
+ a
->after
->current_size
, context
->grain_size
) + free_area_available(a
);
815 static uint64_t free_area_min_end(Context
*context
, const FreeArea
*a
) {
822 assert(a
->after
->offset
!= UINT64_MAX
);
823 assert(a
->after
->current_size
!= UINT64_MAX
);
825 /* Calculate where the partition would end when we give it as much as it needs. */
826 return round_up_size(a
->after
->offset
+ partition_min_size_with_padding(context
, a
->after
), context
->grain_size
);
829 static uint64_t free_area_available_for_new_partitions(Context
*context
, const FreeArea
*a
) {
833 /* Similar to free_area_available(), but takes into account that the required size and padding of the
834 * preceding partition is honoured. */
836 return LESS_BY(free_area_current_end(context
, a
), free_area_min_end(context
, a
));
839 static int free_area_compare(FreeArea
*const *a
, FreeArea
*const*b
, Context
*context
) {
842 return CMP(free_area_available_for_new_partitions(context
, *a
),
843 free_area_available_for_new_partitions(context
, *b
));
846 static uint64_t charge_size(Context
*context
, uint64_t total
, uint64_t amount
) {
848 /* Subtract the specified amount from total, rounding up to multiple of 4K if there's room */
849 assert(amount
<= total
);
850 return LESS_BY(total
, round_up_size(amount
, context
->grain_size
));
853 static uint64_t charge_weight(uint64_t total
, uint64_t amount
) {
854 assert(amount
<= total
);
855 return total
- amount
;
858 static bool context_allocate_partitions(Context
*context
, uint64_t *ret_largest_free_area
) {
861 /* This may be called multiple times. Reset previous assignments. */
862 for (size_t i
= 0; i
< context
->n_free_areas
; i
++)
863 context
->free_areas
[i
]->allocated
= 0;
865 /* Sort free areas by size, putting smallest first */
866 typesafe_qsort_r(context
->free_areas
, context
->n_free_areas
, free_area_compare
, context
);
868 /* In any case return size of the largest free area (i.e. not the size of all free areas
870 if (ret_largest_free_area
)
871 *ret_largest_free_area
=
872 context
->n_free_areas
== 0 ? 0 :
873 free_area_available_for_new_partitions(context
, context
->free_areas
[context
->n_free_areas
-1]);
875 /* Check that each existing partition can fit its area. */
876 for (size_t i
= 0; i
< context
->n_free_areas
; i
++)
877 if (free_area_current_end(context
, context
->free_areas
[i
]) <
878 free_area_min_end(context
, context
->free_areas
[i
]))
881 /* A simple first-fit algorithm. We return true if we can fit the partitions in, otherwise false. */
882 LIST_FOREACH(partitions
, p
, context
->partitions
) {
887 /* Skip partitions we already dropped or that already exist */
888 if (p
->dropped
|| PARTITION_EXISTS(p
))
891 /* How much do we need to fit? */
892 required
= partition_min_size_with_padding(context
, p
);
893 assert(required
% context
->grain_size
== 0);
895 for (size_t i
= 0; i
< context
->n_free_areas
; i
++) {
896 a
= context
->free_areas
[i
];
898 if (free_area_available_for_new_partitions(context
, a
) >= required
) {
905 return false; /* 😢 Oh no! We can't fit this partition into any free area! */
907 /* Assign the partition to this free area */
908 p
->allocated_to_area
= a
;
910 /* Budget the minimal partition size */
911 a
->allocated
+= required
;
917 static int context_sum_weights(Context
*context
, FreeArea
*a
, uint64_t *ret
) {
918 uint64_t weight_sum
= 0;
924 /* Determine the sum of the weights of all partitions placed in or before the specified free area */
926 LIST_FOREACH(partitions
, p
, context
->partitions
) {
927 if (p
->padding_area
!= a
&& p
->allocated_to_area
!= a
)
930 if (p
->weight
> UINT64_MAX
- weight_sum
)
932 weight_sum
+= p
->weight
;
934 if (p
->padding_weight
> UINT64_MAX
- weight_sum
)
936 weight_sum
+= p
->padding_weight
;
943 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "Combined weight of partition exceeds unsigned 64-bit range, refusing.");
946 static uint64_t scale_by_weight(uint64_t value
, uint64_t weight
, uint64_t weight_sum
) {
947 assert(weight_sum
>= weight
);
952 if (weight
== weight_sum
)
954 if (value
<= UINT64_MAX
/ weight
)
955 return value
* weight
/ weight_sum
;
957 /* Rescale weight and weight_sum to make not the calculation overflow. To satisfy the
958 * following conditions, 'weight_sum' is rounded up but 'weight' is rounded down:
959 * - the sum of scale_by_weight() for all weights must not be larger than the input value,
960 * - scale_by_weight() must not be larger than the ideal value (i.e. calculated with uint128_t). */
961 weight_sum
= DIV_ROUND_UP(weight_sum
, 2);
966 typedef enum GrowPartitionPhase
{
967 /* The zeroth phase: do not touch foreign partitions (i.e. those we don't manage). */
970 /* The first phase: we charge partitions which need more (according to constraints) than their weight-based share. */
973 /* The second phase: we charge partitions which need less (according to constraints) than their weight-based share. */
976 /* The third phase: we distribute what remains among the remaining partitions, according to the weights */
979 _GROW_PARTITION_PHASE_MAX
,
980 } GrowPartitionPhase
;
982 static bool context_grow_partitions_phase(
985 GrowPartitionPhase phase
,
987 uint64_t *weight_sum
) {
989 bool try_again
= false;
996 /* Now let's look at the intended weights and adjust them taking the minimum space assignments into
997 * account. i.e. if a partition has a small weight but a high minimum space value set it should not
998 * get any additional room from the left-overs. Similar, if two partitions have the same weight they
999 * should get the same space if possible, even if one has a smaller minimum size than the other. */
1000 LIST_FOREACH(partitions
, p
, context
->partitions
) {
1002 /* Look only at partitions associated with this free area, i.e. immediately
1003 * preceding it, or allocated into it */
1004 if (p
->allocated_to_area
!= a
&& p
->padding_area
!= a
)
1007 if (p
->new_size
== UINT64_MAX
) {
1008 uint64_t share
, rsz
, xsz
;
1009 bool charge
= false;
1011 /* Calculate how much this space this partition needs if everyone would get
1012 * the weight based share */
1013 share
= scale_by_weight(*span
, p
->weight
, *weight_sum
);
1015 rsz
= partition_min_size(context
, p
);
1016 xsz
= partition_max_size(context
, p
);
1018 if (phase
== PHASE_FOREIGN
&& PARTITION_IS_FOREIGN(p
)) {
1019 /* Never change of foreign partitions (i.e. those we don't manage) */
1021 p
->new_size
= p
->current_size
;
1024 } else if (phase
== PHASE_OVERCHARGE
&& rsz
> share
) {
1025 /* This partition needs more than its calculated share. Let's assign
1026 * it that, and take this partition out of all calculations and start
1030 charge
= try_again
= true;
1032 } else if (phase
== PHASE_UNDERCHARGE
&& xsz
< share
) {
1033 /* This partition accepts less than its calculated
1034 * share. Let's assign it that, and take this partition out
1035 * of all calculations and start again. */
1038 charge
= try_again
= true;
1040 } else if (phase
== PHASE_DISTRIBUTE
) {
1041 /* This partition can accept its calculated share. Let's
1042 * assign it. There's no need to restart things here since
1043 * assigning this shouldn't impact the shares of the other
1046 assert(share
>= rsz
);
1047 p
->new_size
= CLAMP(round_down_size(share
, context
->grain_size
), rsz
, xsz
);
1052 *span
= charge_size(context
, *span
, p
->new_size
);
1053 *weight_sum
= charge_weight(*weight_sum
, p
->weight
);
1057 if (p
->new_padding
== UINT64_MAX
) {
1058 uint64_t share
, rsz
, xsz
;
1059 bool charge
= false;
1061 share
= scale_by_weight(*span
, p
->padding_weight
, *weight_sum
);
1063 rsz
= partition_min_padding(p
);
1064 xsz
= partition_max_padding(p
);
1066 if (phase
== PHASE_OVERCHARGE
&& rsz
> share
) {
1067 p
->new_padding
= rsz
;
1068 charge
= try_again
= true;
1069 } else if (phase
== PHASE_UNDERCHARGE
&& xsz
< share
) {
1070 p
->new_padding
= xsz
;
1071 charge
= try_again
= true;
1072 } else if (phase
== PHASE_DISTRIBUTE
) {
1073 assert(share
>= rsz
);
1074 p
->new_padding
= CLAMP(round_down_size(share
, context
->grain_size
), rsz
, xsz
);
1079 *span
= charge_size(context
, *span
, p
->new_padding
);
1080 *weight_sum
= charge_weight(*weight_sum
, p
->padding_weight
);
1088 static void context_grow_partition_one(Context
*context
, FreeArea
*a
, Partition
*p
, uint64_t *span
) {
1099 if (p
->allocated_to_area
!= a
)
1102 if (PARTITION_IS_FOREIGN(p
))
1105 assert(p
->new_size
!= UINT64_MAX
);
1107 /* Calculate new size and align. */
1108 m
= round_down_size(p
->new_size
+ *span
, context
->grain_size
);
1109 /* But ensure this doesn't shrink the size. */
1110 m
= MAX(m
, p
->new_size
);
1111 /* And ensure this doesn't exceed the maximum size. */
1112 m
= MIN(m
, partition_max_size(context
, p
));
1114 assert(m
>= p
->new_size
);
1116 *span
= charge_size(context
, *span
, m
- p
->new_size
);
1120 static int context_grow_partitions_on_free_area(Context
*context
, FreeArea
*a
) {
1121 uint64_t weight_sum
= 0, span
;
1127 r
= context_sum_weights(context
, a
, &weight_sum
);
1131 /* Let's calculate the total area covered by this free area and the partition before it */
1134 assert(a
->after
->offset
!= UINT64_MAX
);
1135 assert(a
->after
->current_size
!= UINT64_MAX
);
1137 span
+= round_up_size(a
->after
->offset
+ a
->after
->current_size
, context
->grain_size
) - a
->after
->offset
;
1140 for (GrowPartitionPhase phase
= 0; phase
< _GROW_PARTITION_PHASE_MAX
;)
1141 if (context_grow_partitions_phase(context
, a
, phase
, &span
, &weight_sum
))
1142 phase
++; /* go to the next phase */
1144 /* We still have space left over? Donate to preceding partition if we have one */
1145 if (span
> 0 && a
->after
)
1146 context_grow_partition_one(context
, a
, a
->after
, &span
);
1148 /* What? Even still some space left (maybe because there was no preceding partition, or it had a
1149 * size limit), then let's donate it to whoever wants it. */
1151 LIST_FOREACH(partitions
, p
, context
->partitions
) {
1152 context_grow_partition_one(context
, a
, p
, &span
);
1157 /* Yuck, still no one? Then make it padding */
1158 if (span
> 0 && a
->after
) {
1159 assert(a
->after
->new_padding
!= UINT64_MAX
);
1160 a
->after
->new_padding
+= span
;
1166 static int context_grow_partitions(Context
*context
) {
1171 for (size_t i
= 0; i
< context
->n_free_areas
; i
++) {
1172 r
= context_grow_partitions_on_free_area(context
, context
->free_areas
[i
]);
1177 /* All existing partitions that have no free space after them can't change size */
1178 LIST_FOREACH(partitions
, p
, context
->partitions
) {
1182 if (!PARTITION_EXISTS(p
) || p
->padding_area
) {
1183 /* The algorithm above must have initialized this already */
1184 assert(p
->new_size
!= UINT64_MAX
);
1188 assert(p
->new_size
== UINT64_MAX
);
1189 p
->new_size
= p
->current_size
;
1191 assert(p
->new_padding
== UINT64_MAX
);
1192 p
->new_padding
= p
->current_padding
;
1198 static uint64_t find_first_unused_partno(Context
*context
) {
1199 uint64_t partno
= 0;
1203 for (partno
= 0;; partno
++) {
1205 LIST_FOREACH(partitions
, p
, context
->partitions
)
1206 if (p
->partno
!= UINT64_MAX
&& p
->partno
== partno
)
1215 static void context_place_partitions(Context
*context
) {
1219 for (size_t i
= 0; i
< context
->n_free_areas
; i
++) {
1220 FreeArea
*a
= context
->free_areas
[i
];
1221 _unused_
uint64_t left
;
1225 assert(a
->after
->offset
!= UINT64_MAX
);
1226 assert(a
->after
->new_size
!= UINT64_MAX
);
1227 assert(a
->after
->new_padding
!= UINT64_MAX
);
1229 start
= a
->after
->offset
+ a
->after
->new_size
+ a
->after
->new_padding
;
1231 start
= context
->start
;
1233 start
= round_up_size(start
, context
->grain_size
);
1236 LIST_FOREACH(partitions
, p
, context
->partitions
) {
1237 if (p
->allocated_to_area
!= a
)
1241 p
->partno
= find_first_unused_partno(context
);
1243 assert(left
>= p
->new_size
);
1244 start
+= p
->new_size
;
1245 left
-= p
->new_size
;
1247 assert(left
>= p
->new_padding
);
1248 start
+= p
->new_padding
;
1249 left
-= p
->new_padding
;
1254 static int config_parse_type(
1256 const char *filename
,
1258 const char *section
,
1259 unsigned section_line
,
1266 GptPartitionType
*type
= ASSERT_PTR(data
);
1271 r
= gpt_partition_type_from_string(rvalue
, type
);
1273 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse partition type: %s", rvalue
);
1275 if (arg_architecture
>= 0)
1276 *type
= gpt_partition_type_override_architecture(*type
, arg_architecture
);
1281 static int config_parse_label(
1283 const char *filename
,
1285 const char *section
,
1286 unsigned section_line
,
1293 _cleanup_free_
char *resolved
= NULL
;
1294 char **label
= ASSERT_PTR(data
);
1299 /* Nota bene: the empty label is a totally valid one. Let's hence not follow our usual rule of
1300 * assigning the empty string to reset to default here, but really accept it as label to set. */
1302 r
= specifier_printf(rvalue
, GPT_LABEL_MAX
, system_and_tmp_specifier_table
, arg_root
, NULL
, &resolved
);
1304 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1305 "Failed to expand specifiers in Label=, ignoring: %s", rvalue
);
1309 if (!utf8_is_valid(resolved
)) {
1310 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1311 "Partition label not valid UTF-8, ignoring: %s", rvalue
);
1315 r
= gpt_partition_label_valid(resolved
);
1317 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1318 "Failed to check if string is valid as GPT partition label, ignoring: \"%s\" (from \"%s\")",
1323 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1324 "Partition label too long for GPT table, ignoring: \"%s\" (from \"%s\")",
1329 free_and_replace(*label
, resolved
);
1333 static int config_parse_weight(
1335 const char *filename
,
1337 const char *section
,
1338 unsigned section_line
,
1345 uint32_t *w
= ASSERT_PTR(data
), v
;
1350 r
= safe_atou32(rvalue
, &v
);
1352 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1353 "Failed to parse weight value, ignoring: %s", rvalue
);
1357 if (v
> 1000U*1000U) {
1358 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1359 "Weight needs to be in range 0…10000000, ignoring: %" PRIu32
, v
);
1367 static int config_parse_size4096(
1369 const char *filename
,
1371 const char *section
,
1372 unsigned section_line
,
1379 uint64_t *sz
= data
, parsed
;
1385 r
= parse_size(rvalue
, 1024, &parsed
);
1387 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1388 "Failed to parse size value: %s", rvalue
);
1391 *sz
= round_up_size(parsed
, 4096);
1393 *sz
= round_down_size(parsed
, 4096);
1398 log_syntax(unit
, LOG_NOTICE
, filename
, line
, r
, "Rounded %s= size %" PRIu64
" %s %" PRIu64
", a multiple of 4096.",
1399 lvalue
, parsed
, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
), *sz
);
1404 static int config_parse_block_size(
1406 const char *filename
,
1408 const char *section
,
1409 unsigned section_line
,
1416 uint64_t *blksz
= ASSERT_PTR(data
), parsed
;
1421 r
= parse_size(rvalue
, 1024, &parsed
);
1423 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1424 "Failed to parse size value: %s", rvalue
);
1426 if (parsed
< 512 || parsed
> 4096)
1427 return log_syntax(unit
, LOG_ERR
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
1428 "Value not between 512 and 4096: %s", rvalue
);
1430 if (!ISPOWEROF2(parsed
))
1431 return log_syntax(unit
, LOG_ERR
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
1432 "Value not a power of 2: %s", rvalue
);
1438 static int config_parse_fstype(
1440 const char *filename
,
1442 const char *section
,
1443 unsigned section_line
,
1450 char **fstype
= ASSERT_PTR(data
);
1455 /* Let's provide an easy way to override the chosen fstype for file system partitions */
1456 e
= secure_getenv("SYSTEMD_REPART_OVERRIDE_FSTYPE");
1457 if (e
&& !streq(rvalue
, e
)) {
1458 log_syntax(unit
, LOG_NOTICE
, filename
, line
, 0,
1459 "Overriding defined file system type '%s' with '%s'.", rvalue
, e
);
1463 if (!filename_is_valid(rvalue
))
1464 return log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1465 "File system type is not valid, refusing: %s", rvalue
);
1467 return free_and_strdup_warn(fstype
, rvalue
);
1470 static int config_parse_copy_files(
1472 const char *filename
,
1474 const char *section
,
1475 unsigned section_line
,
1482 _cleanup_free_
char *source
= NULL
, *buffer
= NULL
, *resolved_source
= NULL
, *resolved_target
= NULL
;
1483 const char *p
= rvalue
, *target
;
1484 char ***copy_files
= ASSERT_PTR(data
);
1489 r
= extract_first_word(&p
, &source
, ":", EXTRACT_CUNESCAPE
|EXTRACT_DONT_COALESCE_SEPARATORS
);
1491 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract source path: %s", rvalue
);
1493 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "No argument specified: %s", rvalue
);
1497 r
= extract_first_word(&p
, &buffer
, ":", EXTRACT_CUNESCAPE
|EXTRACT_DONT_COALESCE_SEPARATORS
);
1499 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to extract target path: %s", rvalue
);
1501 target
= source
; /* No target, then it's the same as the source */
1506 return log_syntax(unit
, LOG_ERR
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
), "Too many arguments: %s", rvalue
);
1508 r
= specifier_printf(source
, PATH_MAX
-1, system_and_tmp_specifier_table
, arg_root
, NULL
, &resolved_source
);
1510 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1511 "Failed to expand specifiers in CopyFiles= source, ignoring: %s", rvalue
);
1515 r
= path_simplify_and_warn(resolved_source
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
1519 r
= specifier_printf(target
, PATH_MAX
-1, system_and_tmp_specifier_table
, arg_root
, NULL
, &resolved_target
);
1521 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1522 "Failed to expand specifiers in CopyFiles= target, ignoring: %s", resolved_target
);
1526 r
= path_simplify_and_warn(resolved_target
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
1530 r
= strv_consume_pair(copy_files
, TAKE_PTR(resolved_source
), TAKE_PTR(resolved_target
));
1537 static int config_parse_exclude_files(
1539 const char *filename
,
1541 const char *section
,
1542 unsigned section_line
,
1548 _cleanup_free_
char *resolved
= NULL
;
1549 char ***exclude_files
= ASSERT_PTR(data
);
1552 if (isempty(rvalue
)) {
1553 *exclude_files
= strv_free(*exclude_files
);
1557 r
= specifier_printf(rvalue
, PATH_MAX
-1, system_and_tmp_specifier_table
, arg_root
, NULL
, &resolved
);
1559 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1560 "Failed to expand specifiers in ExcludeFiles= path, ignoring: %s", rvalue
);
1564 r
= path_simplify_and_warn(resolved
, PATH_CHECK_ABSOLUTE
|PATH_KEEP_TRAILING_SLASH
, unit
, filename
, line
, lvalue
);
1568 if (strv_consume(exclude_files
, TAKE_PTR(resolved
)) < 0)
1574 static int config_parse_copy_blocks(
1576 const char *filename
,
1578 const char *section
,
1579 unsigned section_line
,
1586 _cleanup_free_
char *d
= NULL
;
1587 Partition
*partition
= ASSERT_PTR(data
);
1592 if (isempty(rvalue
)) {
1593 partition
->copy_blocks_path
= mfree(partition
->copy_blocks_path
);
1594 partition
->copy_blocks_auto
= false;
1598 if (streq(rvalue
, "auto")) {
1599 partition
->copy_blocks_path
= mfree(partition
->copy_blocks_path
);
1600 partition
->copy_blocks_auto
= true;
1601 partition
->copy_blocks_root
= arg_root
;
1605 r
= specifier_printf(rvalue
, PATH_MAX
-1, system_and_tmp_specifier_table
, arg_root
, NULL
, &d
);
1607 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1608 "Failed to expand specifiers in CopyBlocks= source path, ignoring: %s", rvalue
);
1612 r
= path_simplify_and_warn(d
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
1616 free_and_replace(partition
->copy_blocks_path
, d
);
1617 partition
->copy_blocks_auto
= false;
1618 partition
->copy_blocks_root
= arg_root
;
1622 static int config_parse_make_dirs(
1624 const char *filename
,
1626 const char *section
,
1627 unsigned section_line
,
1634 char ***sv
= ASSERT_PTR(data
);
1635 const char *p
= ASSERT_PTR(rvalue
);
1639 _cleanup_free_
char *word
= NULL
, *d
= NULL
;
1641 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_UNQUOTE
);
1645 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
1651 r
= specifier_printf(word
, PATH_MAX
-1, system_and_tmp_specifier_table
, arg_root
, NULL
, &d
);
1653 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1654 "Failed to expand specifiers in MakeDirectories= parameter, ignoring: %s", word
);
1658 r
= path_simplify_and_warn(d
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
1662 r
= strv_consume(sv
, TAKE_PTR(d
));
1668 static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_encrypt
, encrypt_mode
, EncryptMode
, ENCRYPT_OFF
, "Invalid encryption mode");
1670 static int config_parse_gpt_flags(
1672 const char *filename
,
1674 const char *section
,
1675 unsigned section_line
,
1682 uint64_t *gpt_flags
= ASSERT_PTR(data
);
1687 r
= safe_atou64(rvalue
, gpt_flags
);
1689 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1690 "Failed to parse Flags= value, ignoring: %s", rvalue
);
1697 static int config_parse_uuid(
1699 const char *filename
,
1701 const char *section
,
1702 unsigned section_line
,
1709 Partition
*partition
= ASSERT_PTR(data
);
1712 if (isempty(rvalue
)) {
1713 partition
->new_uuid
= SD_ID128_NULL
;
1714 partition
->new_uuid_is_set
= false;
1718 if (streq(rvalue
, "null")) {
1719 partition
->new_uuid
= SD_ID128_NULL
;
1720 partition
->new_uuid_is_set
= true;
1724 r
= sd_id128_from_string(rvalue
, &partition
->new_uuid
);
1726 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse 128-bit ID/UUID, ignoring: %s", rvalue
);
1730 partition
->new_uuid_is_set
= true;
1735 static int config_parse_mountpoint(
1737 const char *filename
,
1739 const char *section
,
1740 unsigned section_line
,
1747 _cleanup_free_
char *where
= NULL
, *options
= NULL
;
1748 Partition
*p
= ASSERT_PTR(data
);
1751 if (isempty(rvalue
)) {
1752 partition_mountpoint_free_many(p
->mountpoints
, p
->n_mountpoints
);
1756 const char *q
= rvalue
;
1757 r
= extract_many_words(&q
, ":", EXTRACT_CUNESCAPE
|EXTRACT_DONT_COALESCE_SEPARATORS
|EXTRACT_UNQUOTE
,
1758 &where
, &options
, NULL
);
1762 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1763 "Invalid syntax in %s=, ignoring: %s", lvalue
, rvalue
);
1767 log_syntax(unit
, LOG_WARNING
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
1768 "Too few arguments in %s=, ignoring: %s", lvalue
, rvalue
);
1772 log_syntax(unit
, LOG_WARNING
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
1773 "Too many arguments in %s=, ignoring: %s", lvalue
, rvalue
);
1777 r
= path_simplify_and_warn(where
, PATH_CHECK_ABSOLUTE
, unit
, filename
, line
, lvalue
);
1781 if (!GREEDY_REALLOC(p
->mountpoints
, p
->n_mountpoints
+ 1))
1784 p
->mountpoints
[p
->n_mountpoints
++] = (PartitionMountPoint
) {
1785 .where
= TAKE_PTR(where
),
1786 .options
= TAKE_PTR(options
),
1792 static int config_parse_encrypted_volume(
1794 const char *filename
,
1796 const char *section
,
1797 unsigned section_line
,
1804 _cleanup_free_
char *volume
= NULL
, *keyfile
= NULL
, *options
= NULL
;
1805 Partition
*p
= ASSERT_PTR(data
);
1808 if (isempty(rvalue
)) {
1809 p
->encrypted_volume
= mfree(p
->encrypted_volume
);
1813 const char *q
= rvalue
;
1814 r
= extract_many_words(&q
, ":", EXTRACT_CUNESCAPE
|EXTRACT_DONT_COALESCE_SEPARATORS
|EXTRACT_UNQUOTE
,
1815 &volume
, &keyfile
, &options
, NULL
);
1819 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1820 "Invalid syntax in %s=, ignoring: %s", lvalue
, rvalue
);
1824 log_syntax(unit
, LOG_WARNING
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
1825 "Too few arguments in %s=, ignoring: %s", lvalue
, rvalue
);
1829 log_syntax(unit
, LOG_WARNING
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
1830 "Too many arguments in %s=, ignoring: %s", lvalue
, rvalue
);
1834 if (!filename_is_valid(volume
)) {
1835 log_syntax(unit
, LOG_WARNING
, filename
, line
, SYNTHETIC_ERRNO(EINVAL
),
1836 "Volume name %s is not valid, ignoring", volume
);
1840 partition_encrypted_volume_free(p
->encrypted_volume
);
1842 p
->encrypted_volume
= new(PartitionEncryptedVolume
, 1);
1843 if (!p
->encrypted_volume
)
1846 *p
->encrypted_volume
= (PartitionEncryptedVolume
) {
1847 .name
= TAKE_PTR(volume
),
1848 .keyfile
= TAKE_PTR(keyfile
),
1849 .options
= TAKE_PTR(options
),
1855 static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_verity
, verity_mode
, VerityMode
, VERITY_OFF
, "Invalid verity mode");
1856 static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_minimize
, minimize_mode
, MinimizeMode
, MINIMIZE_OFF
, "Invalid minimize mode");
1858 static int partition_read_definition(Partition
*p
, const char *path
, const char *const *conf_file_dirs
) {
1860 ConfigTableItem table
[] = {
1861 { "Partition", "Type", config_parse_type
, 0, &p
->type
},
1862 { "Partition", "Label", config_parse_label
, 0, &p
->new_label
},
1863 { "Partition", "UUID", config_parse_uuid
, 0, p
},
1864 { "Partition", "Priority", config_parse_int32
, 0, &p
->priority
},
1865 { "Partition", "Weight", config_parse_weight
, 0, &p
->weight
},
1866 { "Partition", "PaddingWeight", config_parse_weight
, 0, &p
->padding_weight
},
1867 { "Partition", "SizeMinBytes", config_parse_size4096
, -1, &p
->size_min
},
1868 { "Partition", "SizeMaxBytes", config_parse_size4096
, 1, &p
->size_max
},
1869 { "Partition", "PaddingMinBytes", config_parse_size4096
, -1, &p
->padding_min
},
1870 { "Partition", "PaddingMaxBytes", config_parse_size4096
, 1, &p
->padding_max
},
1871 { "Partition", "FactoryReset", config_parse_bool
, 0, &p
->factory_reset
},
1872 { "Partition", "CopyBlocks", config_parse_copy_blocks
, 0, p
},
1873 { "Partition", "Format", config_parse_fstype
, 0, &p
->format
},
1874 { "Partition", "CopyFiles", config_parse_copy_files
, 0, &p
->copy_files
},
1875 { "Partition", "ExcludeFiles", config_parse_exclude_files
, 0, &p
->exclude_files_source
},
1876 { "Partition", "ExcludeFilesTarget", config_parse_exclude_files
, 0, &p
->exclude_files_target
},
1877 { "Partition", "MakeDirectories", config_parse_make_dirs
, 0, &p
->make_directories
},
1878 { "Partition", "Encrypt", config_parse_encrypt
, 0, &p
->encrypt
},
1879 { "Partition", "Verity", config_parse_verity
, 0, &p
->verity
},
1880 { "Partition", "VerityMatchKey", config_parse_string
, 0, &p
->verity_match_key
},
1881 { "Partition", "Flags", config_parse_gpt_flags
, 0, &p
->gpt_flags
},
1882 { "Partition", "ReadOnly", config_parse_tristate
, 0, &p
->read_only
},
1883 { "Partition", "NoAuto", config_parse_tristate
, 0, &p
->no_auto
},
1884 { "Partition", "GrowFileSystem", config_parse_tristate
, 0, &p
->growfs
},
1885 { "Partition", "SplitName", config_parse_string
, 0, &p
->split_name_format
},
1886 { "Partition", "Minimize", config_parse_minimize
, 0, &p
->minimize
},
1887 { "Partition", "Subvolumes", config_parse_make_dirs
, 0, &p
->subvolumes
},
1888 { "Partition", "VerityDataBlockSizeBytes", config_parse_block_size
, 0, &p
->verity_data_block_size
},
1889 { "Partition", "VerityHashBlockSizeBytes", config_parse_block_size
, 0, &p
->verity_hash_block_size
},
1890 { "Partition", "MountPoint", config_parse_mountpoint
, 0, p
},
1891 { "Partition", "EncryptedVolume", config_parse_encrypted_volume
, 0, p
},
1895 _cleanup_free_
char *filename
= NULL
;
1896 const char* dropin_dirname
;
1898 r
= path_extract_filename(path
, &filename
);
1900 return log_error_errno(r
, "Failed to extract filename from path '%s': %m", path
);
1902 dropin_dirname
= strjoina(filename
, ".d");
1904 r
= config_parse_many(
1905 STRV_MAKE_CONST(path
),
1908 arg_definitions
? NULL
: arg_root
,
1910 config_item_table_lookup
, table
,
1918 if (partition_type_exclude(&p
->type
))
1921 if (p
->size_min
!= UINT64_MAX
&& p
->size_max
!= UINT64_MAX
&& p
->size_min
> p
->size_max
)
1922 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1923 "SizeMinBytes= larger than SizeMaxBytes=, refusing.");
1925 if (p
->padding_min
!= UINT64_MAX
&& p
->padding_max
!= UINT64_MAX
&& p
->padding_min
> p
->padding_max
)
1926 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1927 "PaddingMinBytes= larger than PaddingMaxBytes=, refusing.");
1929 if (sd_id128_is_null(p
->type
.uuid
))
1930 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1931 "Type= not defined, refusing.");
1933 if ((p
->copy_blocks_path
|| p
->copy_blocks_auto
) &&
1934 (p
->format
|| !strv_isempty(p
->copy_files
) || !strv_isempty(p
->make_directories
)))
1935 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1936 "Format=/CopyFiles=/MakeDirectories= and CopyBlocks= cannot be combined, refusing.");
1938 if ((!strv_isempty(p
->copy_files
) || !strv_isempty(p
->make_directories
)) && streq_ptr(p
->format
, "swap"))
1939 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1940 "Format=swap and CopyFiles= cannot be combined, refusing.");
1943 const char *format
= NULL
;
1945 if (!strv_isempty(p
->copy_files
) || !strv_isempty(p
->make_directories
) || (p
->encrypt
!= ENCRYPT_OFF
&& !(p
->copy_blocks_path
|| p
->copy_blocks_auto
)))
1946 /* Pick "vfat" as file system for esp and xbootldr partitions, otherwise default to "ext4". */
1947 format
= IN_SET(p
->type
.designator
, PARTITION_ESP
, PARTITION_XBOOTLDR
) ? "vfat" : "ext4";
1948 else if (p
->type
.designator
== PARTITION_SWAP
)
1952 p
->format
= strdup(format
);
1958 if (p
->minimize
!= MINIMIZE_OFF
&& !p
->format
&& p
->verity
!= VERITY_HASH
)
1959 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1960 "Minimize= can only be enabled if Format= or Verity=hash are set");
1962 if (p
->minimize
== MINIMIZE_BEST
&& (p
->format
&& !fstype_is_ro(p
->format
)) && p
->verity
!= VERITY_HASH
)
1963 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1964 "Minimize=best can only be used with read-only filesystems or Verity=hash");
1966 if ((!strv_isempty(p
->copy_files
) || !strv_isempty(p
->make_directories
)) && !mkfs_supports_root_option(p
->format
) && geteuid() != 0)
1967 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EPERM
),
1968 "Need to be root to populate %s filesystems with CopyFiles=/MakeDirectories=",
1971 if (p
->format
&& fstype_is_ro(p
->format
) && strv_isempty(p
->copy_files
) && strv_isempty(p
->make_directories
))
1972 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1973 "Cannot format %s filesystem without source files, refusing", p
->format
);
1975 if (p
->verity
!= VERITY_OFF
|| p
->encrypt
!= ENCRYPT_OFF
) {
1976 r
= dlopen_cryptsetup();
1978 return log_syntax(NULL
, LOG_ERR
, path
, 1, r
,
1979 "libcryptsetup not found, Verity=/Encrypt= are not supported: %m");
1982 if (p
->verity
!= VERITY_OFF
&& !p
->verity_match_key
)
1983 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1984 "VerityMatchKey= must be set if Verity=%s", verity_mode_to_string(p
->verity
));
1986 if (p
->verity
== VERITY_OFF
&& p
->verity_match_key
)
1987 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1988 "VerityMatchKey= can only be set if Verity= is not \"%s\"",
1989 verity_mode_to_string(p
->verity
));
1991 if (IN_SET(p
->verity
, VERITY_HASH
, VERITY_SIG
) &&
1992 (p
->copy_files
|| p
->copy_blocks_path
|| p
->copy_blocks_auto
|| p
->format
|| p
->make_directories
))
1993 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1994 "CopyBlocks=/CopyFiles=/Format=/MakeDirectories= cannot be used with Verity=%s",
1995 verity_mode_to_string(p
->verity
));
1997 if (p
->verity
!= VERITY_OFF
&& p
->encrypt
!= ENCRYPT_OFF
)
1998 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
1999 "Encrypting verity hash/data partitions is not supported");
2001 if (p
->verity
== VERITY_SIG
&& !arg_private_key
)
2002 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
2003 "Verity signature partition requested but no private key provided (--private-key=)");
2005 if (p
->verity
== VERITY_SIG
&& !arg_certificate
)
2006 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
2007 "Verity signature partition requested but no PEM certificate provided (--certificate=)");
2009 if (p
->verity
== VERITY_SIG
&& (p
->size_min
!= UINT64_MAX
|| p
->size_max
!= UINT64_MAX
))
2010 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EINVAL
),
2011 "SizeMinBytes=/SizeMaxBytes= cannot be used with Verity=%s",
2012 verity_mode_to_string(p
->verity
));
2014 if (!strv_isempty(p
->subvolumes
) && arg_offline
> 0)
2015 return log_syntax(NULL
, LOG_ERR
, path
, 1, SYNTHETIC_ERRNO(EOPNOTSUPP
),
2016 "Subvolumes= cannot be used with --offline=yes");
2018 /* Verity partitions are read only, let's imply the RO flag hence, unless explicitly configured otherwise. */
2019 if ((IN_SET(p
->type
.designator
,
2020 PARTITION_ROOT_VERITY
,
2021 PARTITION_USR_VERITY
) || p
->verity
== VERITY_DATA
) && p
->read_only
< 0)
2022 p
->read_only
= true;
2024 /* Default to "growfs" on, unless read-only */
2025 if (gpt_partition_type_knows_growfs(p
->type
) &&
2029 if (!p
->split_name_format
) {
2030 char *s
= strdup("%t");
2034 p
->split_name_format
= s
;
2035 } else if (streq(p
->split_name_format
, "-"))
2036 p
->split_name_format
= mfree(p
->split_name_format
);
2041 static int find_verity_sibling(Context
*context
, Partition
*p
, VerityMode mode
, Partition
**ret
) {
2042 Partition
*s
= NULL
;
2045 assert(p
->verity
!= VERITY_OFF
);
2046 assert(p
->verity_match_key
);
2047 assert(mode
!= VERITY_OFF
);
2048 assert(p
->verity
!= mode
);
2051 /* Try to find the matching sibling partition of the given type for a verity partition. For a data
2052 * partition, this is the corresponding hash partition with the same verity name (and vice versa for
2053 * the hash partition). */
2055 LIST_FOREACH(partitions
, q
, context
->partitions
) {
2059 if (q
->verity
!= mode
)
2062 assert(q
->verity_match_key
);
2064 if (!streq(p
->verity_match_key
, q
->verity_match_key
))
2081 static int context_open_and_lock_backing_fd(const char *node
, int operation
, int *backing_fd
) {
2082 _cleanup_close_
int fd
= -EBADF
;
2087 if (*backing_fd
>= 0)
2090 fd
= open(node
, O_RDONLY
|O_CLOEXEC
);
2092 return log_error_errno(errno
, "Failed to open device '%s': %m", node
);
2094 /* Tell udev not to interfere while we are processing the device */
2095 if (flock(fd
, operation
) < 0)
2096 return log_error_errno(errno
, "Failed to lock device '%s': %m", node
);
2098 log_debug("Device %s opened and locked.", node
);
2099 *backing_fd
= TAKE_FD(fd
);
2103 static int determine_current_padding(
2104 struct fdisk_context
*c
,
2105 struct fdisk_table
*t
,
2106 struct fdisk_partition
*p
,
2111 size_t n_partitions
;
2112 uint64_t offset
, next
= UINT64_MAX
;
2119 if (!fdisk_partition_has_end(p
))
2120 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition has no end!");
2122 offset
= fdisk_partition_get_end(p
);
2123 assert(offset
< UINT64_MAX
);
2124 offset
++; /* The end is one sector before the next partition or padding. */
2125 assert(offset
< UINT64_MAX
/ secsz
);
2128 n_partitions
= fdisk_table_get_nents(t
);
2129 for (size_t i
= 0; i
< n_partitions
; i
++) {
2130 struct fdisk_partition
*q
;
2133 q
= fdisk_table_get_partition(t
, i
);
2135 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to read partition metadata: %m");
2137 if (fdisk_partition_is_used(q
) <= 0)
2140 if (!fdisk_partition_has_start(q
))
2143 start
= fdisk_partition_get_start(q
);
2144 assert(start
< UINT64_MAX
/ secsz
);
2147 if (start
>= offset
&& (next
== UINT64_MAX
|| next
> start
))
2151 if (next
== UINT64_MAX
) {
2152 /* No later partition? In that case check the end of the usable area */
2153 next
= fdisk_get_last_lba(c
);
2154 assert(next
< UINT64_MAX
);
2155 next
++; /* The last LBA is one sector before the end */
2157 assert(next
< UINT64_MAX
/ secsz
);
2161 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition end beyond disk end.");
2164 assert(next
>= offset
);
2165 offset
= round_up_size(offset
, grainsz
);
2166 next
= round_down_size(next
, grainsz
);
2168 *ret
= LESS_BY(next
, offset
); /* Saturated subtraction, rounding might have fucked things up */
2172 static int context_copy_from_one(Context
*context
, const char *src
) {
2173 _cleanup_close_
int fd
= -EBADF
;
2174 _cleanup_(fdisk_unref_contextp
) struct fdisk_context
*c
= NULL
;
2175 _cleanup_(fdisk_unref_tablep
) struct fdisk_table
*t
= NULL
;
2176 Partition
*last
= NULL
;
2177 unsigned long secsz
, grainsz
;
2178 size_t n_partitions
;
2183 r
= context_open_and_lock_backing_fd(src
, LOCK_SH
, &fd
);
2187 r
= fd_verify_regular(fd
);
2189 return log_error_errno(r
, "%s is not a file: %m", src
);
2191 r
= fdisk_new_context_at(fd
, /* path = */ NULL
, /* read_only = */ true, /* sector_size = */ UINT32_MAX
, &c
);
2193 return log_error_errno(r
, "Failed to create fdisk context: %m");
2195 secsz
= fdisk_get_sector_size(c
);
2196 grainsz
= fdisk_get_grain_size(c
);
2198 /* Insist on a power of two, and that it's a multiple of 512, i.e. the traditional sector size. */
2199 if (secsz
< 512 || !ISPOWEROF2(secsz
))
2200 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Sector size %lu is not a power of two larger than 512? Refusing.", secsz
);
2202 if (!fdisk_is_labeltype(c
, FDISK_DISKLABEL_GPT
))
2203 return log_error_errno(SYNTHETIC_ERRNO(EHWPOISON
), "Cannot copy from disk %s with no GPT disk label.", src
);
2205 r
= fdisk_get_partitions(c
, &t
);
2207 return log_error_errno(r
, "Failed to acquire partition table: %m");
2209 n_partitions
= fdisk_table_get_nents(t
);
2210 for (size_t i
= 0; i
< n_partitions
; i
++) {
2211 _cleanup_(partition_freep
) Partition
*np
= NULL
;
2212 _cleanup_free_
char *label_copy
= NULL
;
2213 struct fdisk_partition
*p
;
2215 uint64_t sz
, start
, padding
;
2216 sd_id128_t ptid
, id
;
2217 GptPartitionType type
;
2219 p
= fdisk_table_get_partition(t
, i
);
2221 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to read partition metadata: %m");
2223 if (fdisk_partition_is_used(p
) <= 0)
2226 if (fdisk_partition_has_start(p
) <= 0 ||
2227 fdisk_partition_has_size(p
) <= 0 ||
2228 fdisk_partition_has_partno(p
) <= 0)
2229 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Found a partition without a position, size or number.");
2231 r
= fdisk_partition_get_type_as_id128(p
, &ptid
);
2233 return log_error_errno(r
, "Failed to query partition type UUID: %m");
2235 type
= gpt_partition_type_from_uuid(ptid
);
2237 r
= fdisk_partition_get_uuid_as_id128(p
, &id
);
2239 return log_error_errno(r
, "Failed to query partition UUID: %m");
2241 label
= fdisk_partition_get_name(p
);
2242 if (!isempty(label
)) {
2243 label_copy
= strdup(label
);
2248 sz
= fdisk_partition_get_size(p
);
2249 assert(sz
<= UINT64_MAX
/secsz
);
2252 start
= fdisk_partition_get_start(p
);
2253 assert(start
<= UINT64_MAX
/secsz
);
2256 if (partition_type_exclude(&type
))
2259 np
= partition_new();
2265 np
->new_uuid_is_set
= true;
2266 np
->size_min
= np
->size_max
= sz
;
2267 np
->new_label
= TAKE_PTR(label_copy
);
2269 np
->definition_path
= strdup(src
);
2270 if (!np
->definition_path
)
2273 r
= determine_current_padding(c
, t
, p
, secsz
, grainsz
, &padding
);
2277 np
->padding_min
= np
->padding_max
= padding
;
2279 np
->copy_blocks_path
= strdup(src
);
2280 if (!np
->copy_blocks_path
)
2283 np
->copy_blocks_fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
2284 if (np
->copy_blocks_fd
< 0)
2285 return log_error_errno(r
, "Failed to duplicate file descriptor of %s: %m", src
);
2287 np
->copy_blocks_offset
= start
;
2288 np
->copy_blocks_size
= sz
;
2290 r
= fdisk_partition_get_attrs_as_uint64(p
, &np
->gpt_flags
);
2292 return log_error_errno(r
, "Failed to get partition flags: %m");
2294 LIST_INSERT_AFTER(partitions
, context
->partitions
, last
, np
);
2295 last
= TAKE_PTR(np
);
2296 context
->n_partitions
++;
2302 static int context_copy_from(Context
*context
) {
2307 STRV_FOREACH(src
, arg_copy_from
) {
2308 r
= context_copy_from_one(context
, *src
);
2316 static int context_read_definitions(Context
*context
) {
2317 _cleanup_strv_free_
char **files
= NULL
;
2318 Partition
*last
= LIST_FIND_TAIL(partitions
, context
->partitions
);
2319 const char *const *dirs
;
2324 dirs
= (const char* const*) (arg_definitions
?: CONF_PATHS_STRV("repart.d"));
2326 r
= conf_files_list_strv(&files
, ".conf", arg_definitions
? NULL
: arg_root
, CONF_FILES_REGULAR
|CONF_FILES_FILTER_MASKED
, dirs
);
2328 return log_error_errno(r
, "Failed to enumerate *.conf files: %m");
2330 STRV_FOREACH(f
, files
) {
2331 _cleanup_(partition_freep
) Partition
*p
= NULL
;
2333 p
= partition_new();
2337 p
->definition_path
= strdup(*f
);
2338 if (!p
->definition_path
)
2341 r
= partition_read_definition(p
, *f
, dirs
);
2347 LIST_INSERT_AFTER(partitions
, context
->partitions
, last
, p
);
2349 context
->n_partitions
++;
2352 /* Check that each configured verity hash/data partition has a matching verity data/hash partition. */
2354 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2355 if (p
->verity
== VERITY_OFF
)
2358 for (VerityMode mode
= VERITY_OFF
+ 1; mode
< _VERITY_MODE_MAX
; mode
++) {
2359 Partition
*q
= NULL
;
2361 if (p
->verity
== mode
)
2364 if (p
->siblings
[mode
])
2367 r
= find_verity_sibling(context
, p
, mode
, &q
);
2369 if (mode
!= VERITY_SIG
)
2370 return log_syntax(NULL
, LOG_ERR
, p
->definition_path
, 1, SYNTHETIC_ERRNO(EINVAL
),
2371 "Missing verity %s partition for verity %s partition with VerityMatchKey=%s",
2372 verity_mode_to_string(mode
), verity_mode_to_string(p
->verity
), p
->verity_match_key
);
2373 } else if (r
== -ENOTUNIQ
)
2374 return log_syntax(NULL
, LOG_ERR
, p
->definition_path
, 1, SYNTHETIC_ERRNO(EINVAL
),
2375 "Multiple verity %s partitions found for verity %s partition with VerityMatchKey=%s",
2376 verity_mode_to_string(mode
), verity_mode_to_string(p
->verity
), p
->verity_match_key
);
2378 return log_syntax(NULL
, LOG_ERR
, p
->definition_path
, 1, r
,
2379 "Failed to find verity %s partition for verity %s partition with VerityMatchKey=%s",
2380 verity_mode_to_string(mode
), verity_mode_to_string(p
->verity
), p
->verity_match_key
);
2383 if (q
->priority
!= p
->priority
)
2384 return log_syntax(NULL
, LOG_ERR
, p
->definition_path
, 1, SYNTHETIC_ERRNO(EINVAL
),
2385 "Priority mismatch (%i != %i) for verity sibling partitions with VerityMatchKey=%s",
2386 p
->priority
, q
->priority
, p
->verity_match_key
);
2388 p
->siblings
[mode
] = q
;
2393 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2396 if (p
->verity
!= VERITY_HASH
)
2399 if (p
->minimize
== MINIMIZE_OFF
)
2402 assert_se(dp
= p
->siblings
[VERITY_DATA
]);
2404 if (dp
->minimize
== MINIMIZE_OFF
&& !(dp
->copy_blocks_path
|| dp
->copy_blocks_auto
))
2405 return log_syntax(NULL
, LOG_ERR
, p
->definition_path
, 1, SYNTHETIC_ERRNO(EINVAL
),
2406 "Minimize= set for verity hash partition but data partition does "
2407 "not set CopyBlocks= or Minimize=");
2414 static int fdisk_ask_cb(struct fdisk_context
*c
, struct fdisk_ask
*ask
, void *data
) {
2415 _cleanup_free_
char *ids
= NULL
;
2418 if (fdisk_ask_get_type(ask
) != FDISK_ASKTYPE_STRING
)
2421 ids
= new(char, SD_ID128_UUID_STRING_MAX
);
2425 r
= fdisk_ask_string_set_result(ask
, sd_id128_to_uuid_string(*(sd_id128_t
*) data
, ids
));
2433 static int fdisk_set_disklabel_id_by_uuid(struct fdisk_context
*c
, sd_id128_t id
) {
2436 r
= fdisk_set_ask(c
, fdisk_ask_cb
, &id
);
2440 r
= fdisk_set_disklabel_id(c
);
2444 return fdisk_set_ask(c
, NULL
, NULL
);
2447 static int derive_uuid(sd_id128_t base
, const char *token
, sd_id128_t
*ret
) {
2449 uint8_t md
[SHA256_DIGEST_SIZE
];
2456 /* Derive a new UUID from the specified UUID in a stable and reasonably safe way. Specifically, we
2457 * calculate the HMAC-SHA256 of the specified token string, keyed by the supplied base (typically the
2458 * machine ID). We use the machine ID as key (and not as cleartext!) of the HMAC operation since it's
2459 * the machine ID we don't want to leak. */
2461 hmac_sha256(base
.bytes
, sizeof(base
.bytes
), token
, strlen(token
), result
.md
);
2463 /* Take the first half, mark it as v4 UUID */
2464 assert_cc(sizeof(result
.md
) == sizeof(result
.id
) * 2);
2465 *ret
= id128_make_v4_uuid(result
.id
);
2469 static void derive_salt(sd_id128_t base
, const char *token
, uint8_t ret
[static SHA256_DIGEST_SIZE
]) {
2472 hmac_sha256(base
.bytes
, sizeof(base
.bytes
), token
, strlen(token
), ret
);
2475 static int context_load_partition_table(Context
*context
) {
2476 _cleanup_(fdisk_unref_contextp
) struct fdisk_context
*c
= NULL
;
2477 _cleanup_(fdisk_unref_tablep
) struct fdisk_table
*t
= NULL
;
2478 uint64_t left_boundary
= UINT64_MAX
, first_lba
, last_lba
, nsectors
;
2479 _cleanup_free_
char *disk_uuid_string
= NULL
;
2480 bool from_scratch
= false;
2481 sd_id128_t disk_uuid
;
2482 size_t n_partitions
;
2483 unsigned long secsz
;
2484 uint64_t grainsz
, fs_secsz
= DEFAULT_FILESYSTEM_SECTOR_SIZE
;
2488 assert(!context
->fdisk_context
);
2489 assert(!context
->free_areas
);
2490 assert(context
->start
== UINT64_MAX
);
2491 assert(context
->end
== UINT64_MAX
);
2492 assert(context
->total
== UINT64_MAX
);
2494 c
= fdisk_new_context();
2498 if (arg_sector_size
> 0) {
2499 fs_secsz
= arg_sector_size
;
2500 r
= fdisk_save_user_sector_size(c
, /* phy= */ 0, arg_sector_size
);
2505 r
= context_open_and_lock_backing_fd(
2507 arg_dry_run
? LOCK_SH
: LOCK_EX
,
2508 &context
->backing_fd
);
2512 if (fstat(context
->backing_fd
, &st
) < 0)
2513 return log_error_errno(errno
, "Failed to stat %s: %m", context
->node
);
2515 if (IN_SET(arg_empty
, EMPTY_REQUIRE
, EMPTY_FORCE
, EMPTY_CREATE
) && S_ISREG(st
.st_mode
))
2516 /* Don't probe sector size from partition table if we are supposed to start from an empty disk */
2517 fs_secsz
= ssz
= 512;
2519 /* Auto-detect sector size if not specified. */
2520 r
= probe_sector_size_prefer_ioctl(context
->backing_fd
, &ssz
);
2522 return log_error_errno(r
, "Failed to probe sector size of '%s': %m", context
->node
);
2524 /* If we found the sector size and we're operating on a block device, use it as the file
2525 * system sector size as well, as we know its the sector size of the actual block device and
2526 * not just the offset at which we found the GPT header. */
2527 if (r
> 0 && S_ISBLK(st
.st_mode
))
2531 r
= fdisk_save_user_sector_size(c
, /* phy= */ 0, ssz
);
2534 return log_error_errno(r
, "Failed to set sector size: %m");
2536 /* libfdisk doesn't have an API to operate on arbitrary fds, hence reopen the fd going via the
2537 * /proc/self/fd/ magic path if we have an existing fd. Open the original file otherwise. */
2538 r
= fdisk_assign_device(
2540 context
->backing_fd
>= 0 ? FORMAT_PROC_FD_PATH(context
->backing_fd
) : context
->node
,
2542 if (r
== -EINVAL
&& arg_size_auto
) {
2545 /* libfdisk returns EINVAL if opening a file of size zero. Let's check for that, and accept
2546 * it if automatic sizing is requested. */
2548 if (context
->backing_fd
< 0)
2549 r
= stat(context
->node
, &st
);
2551 r
= fstat(context
->backing_fd
, &st
);
2553 return log_error_errno(errno
, "Failed to stat block device '%s': %m", context
->node
);
2555 if (S_ISREG(st
.st_mode
) && st
.st_size
== 0) {
2556 /* Use the fallback values if we have no better idea */
2557 context
->sector_size
= fdisk_get_sector_size(c
);
2558 context
->fs_sector_size
= fs_secsz
;
2559 context
->grain_size
= 4096;
2560 return /* from_scratch = */ true;
2566 return log_error_errno(r
, "Failed to open device '%s': %m", context
->node
);
2568 if (context
->backing_fd
< 0) {
2569 /* If we have no fd referencing the device yet, make a copy of the fd now, so that we have one */
2570 r
= context_open_and_lock_backing_fd(FORMAT_PROC_FD_PATH(fdisk_get_devfd(c
)),
2571 arg_dry_run
? LOCK_SH
: LOCK_EX
,
2572 &context
->backing_fd
);
2577 /* The offsets/sizes libfdisk returns to us will be in multiple of the sector size of the
2578 * device. This is typically 512, and sometimes 4096. Let's query libfdisk once for it, and then use
2579 * it for all our needs. Note that the values we use ourselves always are in bytes though, thus mean
2580 * the same thing universally. Also note that regardless what kind of sector size is in use we'll
2581 * place partitions at multiples of 4K. */
2582 secsz
= fdisk_get_sector_size(c
);
2584 /* Insist on a power of two, and that it's a multiple of 512, i.e. the traditional sector size. */
2585 if (secsz
< 512 || !ISPOWEROF2(secsz
))
2586 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Sector size %lu is not a power of two larger than 512? Refusing.", secsz
);
2588 /* Use at least 4K, and ensure it's a multiple of the sector size, regardless if that is smaller or
2590 grainsz
= secsz
< 4096 ? 4096 : secsz
;
2592 log_debug("Sector size of device is %lu bytes. Using grain size of %" PRIu64
".", secsz
, grainsz
);
2594 switch (arg_empty
) {
2597 /* Refuse empty disks, insist on an existing GPT partition table */
2598 if (!fdisk_is_labeltype(c
, FDISK_DISKLABEL_GPT
))
2599 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON
), "Disk %s has no GPT disk label, not repartitioning.", context
->node
);
2604 /* Require an empty disk, refuse any existing partition table */
2605 r
= fdisk_has_label(c
);
2607 return log_error_errno(r
, "Failed to determine whether disk %s has a disk label: %m", context
->node
);
2609 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON
), "Disk %s already has a disk label, refusing.", context
->node
);
2611 from_scratch
= true;
2615 /* Allow both an empty disk and an existing partition table, but only GPT */
2616 r
= fdisk_has_label(c
);
2618 return log_error_errno(r
, "Failed to determine whether disk %s has a disk label: %m", context
->node
);
2620 if (!fdisk_is_labeltype(c
, FDISK_DISKLABEL_GPT
))
2621 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON
), "Disk %s has non-GPT disk label, not repartitioning.", context
->node
);
2623 from_scratch
= true;
2629 /* Always reinitiaize the disk, don't consider what there was on the disk before */
2630 from_scratch
= true;
2634 assert_not_reached();
2638 r
= fdisk_create_disklabel(c
, "gpt");
2640 return log_error_errno(r
, "Failed to create GPT disk label: %m");
2642 r
= derive_uuid(context
->seed
, "disk-uuid", &disk_uuid
);
2644 return log_error_errno(r
, "Failed to acquire disk GPT uuid: %m");
2646 r
= fdisk_set_disklabel_id_by_uuid(c
, disk_uuid
);
2648 return log_error_errno(r
, "Failed to set GPT disk label: %m");
2650 goto add_initial_free_area
;
2653 r
= fdisk_get_disklabel_id(c
, &disk_uuid_string
);
2655 return log_error_errno(r
, "Failed to get current GPT disk label UUID: %m");
2657 r
= id128_from_string_nonzero(disk_uuid_string
, &disk_uuid
);
2659 r
= derive_uuid(context
->seed
, "disk-uuid", &disk_uuid
);
2661 return log_error_errno(r
, "Failed to acquire disk GPT uuid: %m");
2663 r
= fdisk_set_disklabel_id(c
);
2665 return log_error_errno(r
, "Failed to set GPT disk label: %m");
2667 return log_error_errno(r
, "Failed to parse current GPT disk label UUID: %m");
2669 r
= fdisk_get_partitions(c
, &t
);
2671 return log_error_errno(r
, "Failed to acquire partition table: %m");
2673 n_partitions
= fdisk_table_get_nents(t
);
2674 for (size_t i
= 0; i
< n_partitions
; i
++) {
2675 _cleanup_free_
char *label_copy
= NULL
;
2676 Partition
*last
= NULL
;
2677 struct fdisk_partition
*p
;
2681 sd_id128_t ptid
, id
;
2684 p
= fdisk_table_get_partition(t
, i
);
2686 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to read partition metadata: %m");
2688 if (fdisk_partition_is_used(p
) <= 0)
2691 if (fdisk_partition_has_start(p
) <= 0 ||
2692 fdisk_partition_has_size(p
) <= 0 ||
2693 fdisk_partition_has_partno(p
) <= 0)
2694 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Found a partition without a position, size or number.");
2696 r
= fdisk_partition_get_type_as_id128(p
, &ptid
);
2698 return log_error_errno(r
, "Failed to query partition type UUID: %m");
2700 r
= fdisk_partition_get_uuid_as_id128(p
, &id
);
2702 return log_error_errno(r
, "Failed to query partition UUID: %m");
2704 label
= fdisk_partition_get_name(p
);
2705 if (!isempty(label
)) {
2706 label_copy
= strdup(label
);
2711 sz
= fdisk_partition_get_size(p
);
2712 assert(sz
<= UINT64_MAX
/secsz
);
2715 start
= fdisk_partition_get_start(p
);
2716 assert(start
<= UINT64_MAX
/secsz
);
2719 partno
= fdisk_partition_get_partno(p
);
2721 if (left_boundary
== UINT64_MAX
|| left_boundary
> start
)
2722 left_boundary
= start
;
2724 /* Assign this existing partition to the first partition of the right type that doesn't have
2725 * an existing one assigned yet. */
2726 LIST_FOREACH(partitions
, pp
, context
->partitions
) {
2729 if (!sd_id128_equal(pp
->type
.uuid
, ptid
))
2732 if (!pp
->current_partition
) {
2733 pp
->current_uuid
= id
;
2734 pp
->current_size
= sz
;
2736 pp
->partno
= partno
;
2737 pp
->current_label
= TAKE_PTR(label_copy
);
2739 pp
->current_partition
= p
;
2740 fdisk_ref_partition(p
);
2742 r
= determine_current_padding(c
, t
, p
, secsz
, grainsz
, &pp
->current_padding
);
2746 if (pp
->current_padding
> 0) {
2747 r
= context_add_free_area(context
, pp
->current_padding
, pp
);
2757 /* If we have no matching definition, create a new one. */
2759 _cleanup_(partition_freep
) Partition
*np
= NULL
;
2761 np
= partition_new();
2765 np
->current_uuid
= id
;
2766 np
->type
= gpt_partition_type_from_uuid(ptid
);
2767 np
->current_size
= sz
;
2769 np
->partno
= partno
;
2770 np
->current_label
= TAKE_PTR(label_copy
);
2772 np
->current_partition
= p
;
2773 fdisk_ref_partition(p
);
2775 r
= determine_current_padding(c
, t
, p
, secsz
, grainsz
, &np
->current_padding
);
2779 if (np
->current_padding
> 0) {
2780 r
= context_add_free_area(context
, np
->current_padding
, np
);
2785 LIST_INSERT_AFTER(partitions
, context
->partitions
, last
, TAKE_PTR(np
));
2786 context
->n_partitions
++;
2790 add_initial_free_area
:
2791 nsectors
= fdisk_get_nsectors(c
);
2792 assert(nsectors
<= UINT64_MAX
/secsz
);
2795 first_lba
= fdisk_get_first_lba(c
);
2796 assert(first_lba
<= UINT64_MAX
/secsz
);
2799 last_lba
= fdisk_get_last_lba(c
);
2800 assert(last_lba
< UINT64_MAX
);
2802 assert(last_lba
<= UINT64_MAX
/secsz
);
2805 assert(last_lba
>= first_lba
);
2807 if (left_boundary
== UINT64_MAX
) {
2808 /* No partitions at all? Then the whole disk is up for grabs. */
2810 first_lba
= round_up_size(first_lba
, grainsz
);
2811 last_lba
= round_down_size(last_lba
, grainsz
);
2813 if (last_lba
> first_lba
) {
2814 r
= context_add_free_area(context
, last_lba
- first_lba
, NULL
);
2819 /* Add space left of first partition */
2820 assert(left_boundary
>= first_lba
);
2822 first_lba
= round_up_size(first_lba
, grainsz
);
2823 left_boundary
= round_down_size(left_boundary
, grainsz
);
2824 last_lba
= round_down_size(last_lba
, grainsz
);
2826 if (left_boundary
> first_lba
) {
2827 r
= context_add_free_area(context
, left_boundary
- first_lba
, NULL
);
2833 context
->start
= first_lba
;
2834 context
->end
= last_lba
;
2835 context
->total
= nsectors
;
2836 context
->sector_size
= secsz
;
2837 context
->fs_sector_size
= fs_secsz
;
2838 context
->grain_size
= grainsz
;
2839 context
->fdisk_context
= TAKE_PTR(c
);
2841 return from_scratch
;
2844 static void context_unload_partition_table(Context
*context
) {
2847 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2849 /* Entirely remove partitions that have no configuration */
2850 if (PARTITION_IS_FOREIGN(p
)) {
2851 partition_unlink_and_free(context
, p
);
2855 /* Otherwise drop all data we read off the block device and everything we might have
2856 * calculated based on it */
2859 p
->current_size
= UINT64_MAX
;
2860 p
->new_size
= UINT64_MAX
;
2861 p
->current_padding
= UINT64_MAX
;
2862 p
->new_padding
= UINT64_MAX
;
2863 p
->partno
= UINT64_MAX
;
2864 p
->offset
= UINT64_MAX
;
2866 if (p
->current_partition
) {
2867 fdisk_unref_partition(p
->current_partition
);
2868 p
->current_partition
= NULL
;
2871 if (p
->new_partition
) {
2872 fdisk_unref_partition(p
->new_partition
);
2873 p
->new_partition
= NULL
;
2876 p
->padding_area
= NULL
;
2877 p
->allocated_to_area
= NULL
;
2879 p
->current_uuid
= SD_ID128_NULL
;
2880 p
->current_label
= mfree(p
->current_label
);
2883 context
->start
= UINT64_MAX
;
2884 context
->end
= UINT64_MAX
;
2885 context
->total
= UINT64_MAX
;
2887 if (context
->fdisk_context
) {
2888 fdisk_unref_context(context
->fdisk_context
);
2889 context
->fdisk_context
= NULL
;
2892 context_free_free_areas(context
);
2895 static int format_size_change(uint64_t from
, uint64_t to
, char **ret
) {
2898 if (from
!= UINT64_MAX
) {
2899 if (from
== to
|| to
== UINT64_MAX
)
2900 t
= strdup(FORMAT_BYTES(from
));
2902 t
= strjoin(FORMAT_BYTES(from
), " ", special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
), " ", FORMAT_BYTES(to
));
2903 } else if (to
!= UINT64_MAX
)
2904 t
= strjoin(special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
), " ", FORMAT_BYTES(to
));
2917 static const char *partition_label(const Partition
*p
) {
2921 return p
->new_label
;
2923 if (p
->current_label
)
2924 return p
->current_label
;
2926 return gpt_partition_type_uuid_to_string(p
->type
.uuid
);
2929 static int context_dump_partitions(Context
*context
) {
2930 _cleanup_(table_unrefp
) Table
*t
= NULL
;
2931 uint64_t sum_padding
= 0, sum_size
= 0;
2933 const size_t roothash_col
= 14, dropin_files_col
= 15, split_path_col
= 16;
2934 bool has_roothash
= false, has_dropin_files
= false, has_split_path
= false;
2936 if ((arg_json_format_flags
& JSON_FORMAT_OFF
) && context
->n_partitions
== 0) {
2937 log_info("Empty partition table.");
2941 t
= table_new("type",
2961 if (!DEBUG_LOGGING
) {
2962 if (arg_json_format_flags
& JSON_FORMAT_OFF
)
2963 (void) table_set_display(t
, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
2964 (size_t) 8, (size_t) 9, (size_t) 12, roothash_col
, dropin_files_col
,
2967 (void) table_set_display(t
, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
2968 (size_t) 5, (size_t) 6, (size_t) 7, (size_t) 8, (size_t) 10,
2969 (size_t) 11, (size_t) 13, roothash_col
, dropin_files_col
,
2973 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 5), 100);
2974 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 6), 100);
2975 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 7), 100);
2976 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 8), 100);
2977 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 9), 100);
2978 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 10), 100);
2979 (void) table_set_align_percent(t
, table_get_cell(t
, 0, 11), 100);
2981 LIST_FOREACH(partitions
, p
, context
->partitions
) {
2982 _cleanup_free_
char *size_change
= NULL
, *padding_change
= NULL
, *partname
= NULL
, *rh
= NULL
;
2983 char uuid_buffer
[SD_ID128_UUID_STRING_MAX
];
2984 const char *label
, *activity
= NULL
;
2989 if (p
->current_size
== UINT64_MAX
)
2990 activity
= "create";
2991 else if (p
->current_size
!= p
->new_size
)
2992 activity
= "resize";
2994 label
= partition_label(p
);
2995 partname
= p
->partno
!= UINT64_MAX
? fdisk_partname(context
->node
, p
->partno
+1) : NULL
;
2997 r
= format_size_change(p
->current_size
, p
->new_size
, &size_change
);
3001 r
= format_size_change(p
->current_padding
, p
->new_padding
, &padding_change
);
3005 if (p
->new_size
!= UINT64_MAX
)
3006 sum_size
+= p
->new_size
;
3007 if (p
->new_padding
!= UINT64_MAX
)
3008 sum_padding
+= p
->new_padding
;
3010 if (p
->verity
!= VERITY_OFF
) {
3011 Partition
*hp
= p
->verity
== VERITY_HASH
? p
: p
->siblings
[VERITY_HASH
];
3013 rh
= iovec_is_set(&hp
->roothash
) ? hexmem(hp
->roothash
.iov_base
, hp
->roothash
.iov_len
) : strdup("TBD");
3020 TABLE_STRING
, gpt_partition_type_uuid_to_string_harder(p
->type
.uuid
, uuid_buffer
),
3021 TABLE_STRING
, empty_to_null(label
) ?: "-", TABLE_SET_COLOR
, empty_to_null(label
) ? NULL
: ansi_grey(),
3022 TABLE_UUID
, p
->new_uuid_is_set
? p
->new_uuid
: p
->current_uuid
,
3023 TABLE_UINT64
, p
->partno
,
3024 TABLE_PATH_BASENAME
, p
->definition_path
, TABLE_SET_COLOR
, p
->definition_path
? NULL
: ansi_grey(),
3025 TABLE_STRING
, partname
?: "-", TABLE_SET_COLOR
, partname
? NULL
: ansi_highlight(),
3026 TABLE_UINT64
, p
->offset
,
3027 TABLE_UINT64
, p
->current_size
== UINT64_MAX
? 0 : p
->current_size
,
3028 TABLE_UINT64
, p
->new_size
,
3029 TABLE_STRING
, size_change
, TABLE_SET_COLOR
, !p
->partitions_next
&& sum_size
> 0 ? ansi_underline() : NULL
,
3030 TABLE_UINT64
, p
->current_padding
== UINT64_MAX
? 0 : p
->current_padding
,
3031 TABLE_UINT64
, p
->new_padding
,
3032 TABLE_STRING
, padding_change
, TABLE_SET_COLOR
, !p
->partitions_next
&& sum_padding
> 0 ? ansi_underline() : NULL
,
3033 TABLE_STRING
, activity
?: "unchanged",
3035 TABLE_STRV
, p
->drop_in_files
,
3036 TABLE_STRING
, empty_to_null(p
->split_path
) ?: "-");
3038 return table_log_add_error(r
);
3040 has_roothash
= has_roothash
|| !isempty(rh
);
3041 has_dropin_files
= has_dropin_files
|| !strv_isempty(p
->drop_in_files
);
3042 has_split_path
= has_split_path
|| !isempty(p
->split_path
);
3045 if ((arg_json_format_flags
& JSON_FORMAT_OFF
) && (sum_padding
> 0 || sum_size
> 0)) {
3048 a
= strjoina(special_glyph(SPECIAL_GLYPH_SIGMA
), " = ", FORMAT_BYTES(sum_size
));
3049 b
= strjoina(special_glyph(SPECIAL_GLYPH_SIGMA
), " = ", FORMAT_BYTES(sum_padding
));
3071 return table_log_add_error(r
);
3074 if (!has_roothash
) {
3075 r
= table_hide_column_from_display(t
, roothash_col
);
3077 return log_error_errno(r
, "Failed to set columns to display: %m");
3080 if (!has_dropin_files
) {
3081 r
= table_hide_column_from_display(t
, dropin_files_col
);
3083 return log_error_errno(r
, "Failed to set columns to display: %m");
3086 if (!has_split_path
) {
3087 r
= table_hide_column_from_display(t
, split_path_col
);
3089 return log_error_errno(r
, "Failed to set columns to display: %m");
3092 return table_print_with_pager(t
, arg_json_format_flags
, arg_pager_flags
, arg_legend
);
3095 static int context_bar_char_process_partition(
3100 size_t **start_array
,
3101 size_t *n_start_array
) {
3103 uint64_t from
, to
, total
;
3110 assert(start_array
);
3111 assert(n_start_array
);
3116 assert(p
->offset
!= UINT64_MAX
);
3117 assert(p
->new_size
!= UINT64_MAX
);
3120 to
= from
+ p
->new_size
;
3122 assert(context
->total
> 0);
3123 total
= context
->total
;
3125 assert(from
<= total
);
3126 x
= from
* n
/ total
;
3128 assert(to
<= total
);
3134 for (size_t i
= x
; i
< y
; i
++)
3137 if (!GREEDY_REALLOC_APPEND(*start_array
, *n_start_array
, &x
, 1))
3143 static int partition_hint(const Partition
*p
, const char *node
, char **ret
) {
3144 _cleanup_free_
char *buf
= NULL
;
3148 /* Tries really hard to find a suitable description for this partition */
3150 if (p
->definition_path
)
3151 return path_extract_filename(p
->definition_path
, ret
);
3153 label
= partition_label(p
);
3154 if (!isempty(label
)) {
3155 buf
= strdup(label
);
3159 if (p
->partno
!= UINT64_MAX
) {
3160 buf
= fdisk_partname(node
, p
->partno
+1);
3164 if (p
->new_uuid_is_set
)
3166 else if (!sd_id128_is_null(p
->current_uuid
))
3167 id
= p
->current_uuid
;
3171 buf
= strdup(SD_ID128_TO_UUID_STRING(id
));
3177 *ret
= TAKE_PTR(buf
);
3181 static int context_dump_partition_bar(Context
*context
) {
3182 _cleanup_free_ Partition
**bar
= NULL
;
3183 _cleanup_free_
size_t *start_array
= NULL
;
3184 size_t n_start_array
= 0;
3185 Partition
*last
= NULL
;
3190 assert_se((c
= columns()) >= 2);
3191 c
-= 2; /* We do not use the leftmost and rightmost character cell */
3193 bar
= new0(Partition
*, c
);
3197 LIST_FOREACH(partitions
, p
, context
->partitions
) {
3198 r
= context_bar_char_process_partition(context
, bar
, c
, p
, &start_array
, &n_start_array
);
3205 for (size_t i
= 0; i
< c
; i
++) {
3210 fputs(z
? ansi_green() : ansi_yellow(), stdout
);
3211 fputs(special_glyph(SPECIAL_GLYPH_DARK_SHADE
), stdout
);
3213 fputs(ansi_normal(), stdout
);
3214 fputs(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE
), stdout
);
3220 fputs(ansi_normal(), stdout
);
3223 for (size_t i
= 0; i
< n_start_array
; i
++) {
3224 _cleanup_free_
char **line
= NULL
;
3226 line
= new0(char*, c
);
3231 LIST_FOREACH(partitions
, p
, context
->partitions
) {
3232 _cleanup_free_
char *d
= NULL
;
3239 if (i
< n_start_array
- j
) {
3241 if (line
[start_array
[j
-1]]) {
3244 /* Upgrade final corner to the right with a branch to the right */
3245 e
= startswith(line
[start_array
[j
-1]], special_glyph(SPECIAL_GLYPH_TREE_RIGHT
));
3247 d
= strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH
), e
);
3254 d
= strdup(special_glyph(SPECIAL_GLYPH_TREE_VERTICAL
));
3259 } else if (i
== n_start_array
- j
) {
3260 _cleanup_free_
char *hint
= NULL
;
3262 (void) partition_hint(p
, context
->node
, &hint
);
3264 if (streq_ptr(line
[start_array
[j
-1]], special_glyph(SPECIAL_GLYPH_TREE_VERTICAL
)))
3265 d
= strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH
), " ", strna(hint
));
3267 d
= strjoin(special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), " ", strna(hint
));
3274 free_and_replace(line
[start_array
[j
-1]], d
);
3282 fputs(line
[j
], stdout
);
3283 j
+= utf8_console_width(line
[j
]);
3292 for (j
= 0; j
< c
; j
++)
3299 static bool context_has_roothash(Context
*context
) {
3300 LIST_FOREACH(partitions
, p
, context
->partitions
)
3301 if (iovec_is_set(&p
->roothash
))
3307 static int context_dump(Context
*context
, bool late
) {
3312 if (arg_pretty
== 0 && FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
))
3315 /* If we're outputting JSON, only dump after doing all operations so we can include the roothashes
3317 if (!late
&& !FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
))
3320 /* If we're not outputting JSON, only dump again after doing all operations if there are any
3321 * roothashes that we need to communicate to the user. */
3322 if (late
&& FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
) && !context_has_roothash(context
))
3325 r
= context_dump_partitions(context
);
3329 /* Make sure we only write the partition bar once, even if we're writing the partition table twice to
3330 * communicate roothashes. */
3331 if (FLAGS_SET(arg_json_format_flags
, JSON_FORMAT_OFF
) && !late
) {
3334 r
= context_dump_partition_bar(context
);
3347 static bool context_changed(const Context
*context
) {
3350 LIST_FOREACH(partitions
, p
, context
->partitions
) {
3354 if (p
->allocated_to_area
)
3357 if (p
->new_size
!= p
->current_size
)
3364 static int context_wipe_range(Context
*context
, uint64_t offset
, uint64_t size
) {
3365 _cleanup_(blkid_free_probep
) blkid_probe probe
= NULL
;
3369 assert(offset
!= UINT64_MAX
);
3370 assert(size
!= UINT64_MAX
);
3372 probe
= blkid_new_probe();
3377 r
= blkid_probe_set_device(probe
, fdisk_get_devfd(context
->fdisk_context
), offset
, size
);
3379 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to allocate device probe for wiping.");
3382 if (blkid_probe_enable_superblocks(probe
, true) < 0 ||
3383 blkid_probe_set_superblocks_flags(probe
, BLKID_SUBLKS_MAGIC
|BLKID_SUBLKS_BADCSUM
) < 0 ||
3384 blkid_probe_enable_partitions(probe
, true) < 0 ||
3385 blkid_probe_set_partitions_flags(probe
, BLKID_PARTS_MAGIC
) < 0)
3386 return log_error_errno(errno
?: SYNTHETIC_ERRNO(EIO
), "Failed to enable superblock and partition probing.");
3390 r
= blkid_do_probe(probe
);
3392 return log_error_errno(errno_or_else(EIO
), "Failed to probe for file systems.");
3397 if (blkid_do_wipe(probe
, false) < 0)
3398 return log_error_errno(errno_or_else(EIO
), "Failed to wipe file system signature.");
3404 static int context_wipe_partition(Context
*context
, Partition
*p
) {
3409 assert(!PARTITION_EXISTS(p
)); /* Safety check: never wipe existing partitions */
3411 assert(p
->offset
!= UINT64_MAX
);
3412 assert(p
->new_size
!= UINT64_MAX
);
3414 r
= context_wipe_range(context
, p
->offset
, p
->new_size
);
3418 log_info("Successfully wiped file system signatures from future partition %" PRIu64
".", p
->partno
);
3422 static int context_discard_range(
3431 assert(offset
!= UINT64_MAX
);
3432 assert(size
!= UINT64_MAX
);
3437 assert_se((fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
3439 if (fstat(fd
, &st
) < 0)
3442 if (S_ISREG(st
.st_mode
)) {
3443 if (fallocate(fd
, FALLOC_FL_PUNCH_HOLE
|FALLOC_FL_KEEP_SIZE
, offset
, size
) < 0) {
3444 if (ERRNO_IS_NOT_SUPPORTED(errno
))
3453 if (S_ISBLK(st
.st_mode
)) {
3454 uint64_t range
[2], end
;
3456 range
[0] = round_up_size(offset
, context
->sector_size
);
3458 if (offset
> UINT64_MAX
- size
)
3461 end
= offset
+ size
;
3462 if (end
<= range
[0])
3465 range
[1] = round_down_size(end
- range
[0], context
->sector_size
);
3469 if (ioctl(fd
, BLKDISCARD
, range
) < 0) {
3470 if (ERRNO_IS_NOT_SUPPORTED(errno
))
3482 static int context_discard_partition(Context
*context
, Partition
*p
) {
3488 assert(p
->offset
!= UINT64_MAX
);
3489 assert(p
->new_size
!= UINT64_MAX
);
3490 assert(!PARTITION_EXISTS(p
)); /* Safety check: never discard existing partitions */
3495 r
= context_discard_range(context
, p
->offset
, p
->new_size
);
3496 if (r
== -EOPNOTSUPP
) {
3497 log_info("Storage does not support discard, not discarding data in future partition %" PRIu64
".", p
->partno
);
3501 /* Let's handle this gracefully: https://bugzilla.kernel.org/show_bug.cgi?id=211167 */
3502 log_info("Block device is busy, not discarding partition %" PRIu64
" because it probably is mounted.", p
->partno
);
3506 log_info("Partition %" PRIu64
" too short for discard, skipping.", p
->partno
);
3510 return log_error_errno(r
, "Failed to discard data for future partition %" PRIu64
".", p
->partno
);
3512 log_info("Successfully discarded data from future partition %" PRIu64
".", p
->partno
);
3516 static int context_discard_gap_after(Context
*context
, Partition
*p
) {
3517 uint64_t gap
, next
= UINT64_MAX
;
3521 assert(!p
|| (p
->offset
!= UINT64_MAX
&& p
->new_size
!= UINT64_MAX
));
3527 gap
= p
->offset
+ p
->new_size
;
3529 /* The context start gets rounded up to grain_size, however
3530 * existing partitions may be before that so ensure the gap
3531 * starts at the first actually usable lba
3533 gap
= fdisk_get_first_lba(context
->fdisk_context
) * context
->sector_size
;
3535 LIST_FOREACH(partitions
, q
, context
->partitions
) {
3539 assert(q
->offset
!= UINT64_MAX
);
3540 assert(q
->new_size
!= UINT64_MAX
);
3542 if (q
->offset
< gap
)
3545 if (next
== UINT64_MAX
|| q
->offset
< next
)
3549 if (next
== UINT64_MAX
) {
3550 next
= (fdisk_get_last_lba(context
->fdisk_context
) + 1) * context
->sector_size
;
3552 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Partition end beyond disk end.");
3555 assert(next
>= gap
);
3556 r
= context_discard_range(context
, gap
, next
- gap
);
3557 if (r
== -EOPNOTSUPP
) {
3559 log_info("Storage does not support discard, not discarding gap after partition %" PRIu64
".", p
->partno
);
3561 log_info("Storage does not support discard, not discarding gap at beginning of disk.");
3564 if (r
== 0) /* Too short */
3568 return log_error_errno(r
, "Failed to discard gap after partition %" PRIu64
".", p
->partno
);
3570 return log_error_errno(r
, "Failed to discard gap at beginning of disk.");
3574 log_info("Successfully discarded gap after partition %" PRIu64
".", p
->partno
);
3576 log_info("Successfully discarded gap at beginning of disk.");
3581 static int context_wipe_and_discard(Context
*context
) {
3586 if (arg_empty
== EMPTY_CREATE
) /* If we just created the image, no need to wipe */
3589 /* Wipe and discard the contents of all partitions we are about to create. We skip the discarding if
3590 * we were supposed to start from scratch anyway, as in that case we just discard the whole block
3591 * device in one go early on. */
3593 LIST_FOREACH(partitions
, p
, context
->partitions
) {
3595 if (!p
->allocated_to_area
)
3598 if (partition_type_defer(&p
->type
))
3601 r
= context_wipe_partition(context
, p
);
3605 if (!context
->from_scratch
) {
3606 r
= context_discard_partition(context
, p
);
3610 r
= context_discard_gap_after(context
, p
);
3616 if (!context
->from_scratch
) {
3617 r
= context_discard_gap_after(context
, NULL
);
3625 typedef struct DecryptedPartitionTarget
{
3629 struct crypt_device
*device
;
3630 } DecryptedPartitionTarget
;
3632 static DecryptedPartitionTarget
* decrypted_partition_target_free(DecryptedPartitionTarget
*t
) {
3633 #if HAVE_LIBCRYPTSETUP
3641 /* udev or so might access out block device in the background while we are done. Let's hence
3642 * force detach the volume. We sync'ed before, hence this should be safe. */
3643 r
= sym_crypt_deactivate_by_name(t
->device
, t
->dm_name
, CRYPT_DEACTIVATE_FORCE
);
3645 log_warning_errno(r
, "Failed to deactivate LUKS device, ignoring: %m");
3647 sym_crypt_free(t
->device
);
3660 DecryptedPartitionTarget
*decrypted
;
3663 static int partition_target_fd(PartitionTarget
*t
) {
3665 assert(t
->loop
|| t
->fd
>= 0 || t
->whole_fd
>= 0);
3668 return t
->decrypted
->fd
;
3679 static const char* partition_target_path(PartitionTarget
*t
) {
3681 assert(t
->loop
|| t
->path
);
3684 return t
->decrypted
->volume
;
3687 return t
->loop
->node
;
3692 static PartitionTarget
*partition_target_free(PartitionTarget
*t
) {
3696 decrypted_partition_target_free(t
->decrypted
);
3697 loop_device_unref(t
->loop
);
3699 unlink_and_free(t
->path
);
3704 DEFINE_TRIVIAL_CLEANUP_FUNC(PartitionTarget
*, partition_target_free
);
3706 static int prepare_temporary_file(PartitionTarget
*t
, uint64_t size
) {
3707 _cleanup_(unlink_and_freep
) char *temp
= NULL
;
3708 _cleanup_close_
int fd
= -EBADF
;
3714 r
= var_tmp_dir(&vt
);
3716 return log_error_errno(r
, "Could not determine temporary directory: %m");
3718 temp
= path_join(vt
, "repart-XXXXXX");
3722 fd
= mkostemp_safe(temp
);
3724 return log_error_errno(fd
, "Failed to create temporary file: %m");
3726 if (ftruncate(fd
, size
) < 0)
3727 return log_error_errno(errno
, "Failed to truncate temporary file to %s: %m",
3728 FORMAT_BYTES(size
));
3730 t
->fd
= TAKE_FD(fd
);
3731 t
->path
= TAKE_PTR(temp
);
3736 static int partition_target_prepare(
3741 PartitionTarget
**ret
) {
3743 _cleanup_(partition_target_freep
) PartitionTarget
*t
= NULL
;
3744 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
3751 assert_se((whole_fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
3753 t
= new(PartitionTarget
, 1);
3756 *t
= (PartitionTarget
) {
3762 if (lseek(whole_fd
, p
->offset
, SEEK_SET
) < 0)
3763 return log_error_errno(errno
, "Failed to seek to partition offset: %m");
3765 t
->whole_fd
= whole_fd
;
3770 /* Loopback block devices are not only useful to turn regular files into block devices, but
3771 * also to cut out sections of block devices into new block devices. */
3773 if (arg_offline
<= 0) {
3774 r
= loop_device_make(whole_fd
, O_RDWR
, p
->offset
, size
, context
->sector_size
, 0, LOCK_EX
, &d
);
3775 if (r
< 0 && (arg_offline
== 0 || (r
!= -ENOENT
&& !ERRNO_IS_PRIVILEGE(r
)) || !strv_isempty(p
->subvolumes
)))
3776 return log_error_errno(r
, "Failed to make loopback device of future partition %" PRIu64
": %m", p
->partno
);
3778 t
->loop
= TAKE_PTR(d
);
3783 log_debug_errno(r
, "No access to loop devices, falling back to a regular file");
3786 /* If we can't allocate a loop device, let's write to a regular file that we copy into the final
3787 * image so we can run in containers and without needing root privileges. On filesystems with
3788 * reflinking support, we can take advantage of this and just reflink the result into the image.
3791 r
= prepare_temporary_file(t
, size
);
3800 static int partition_target_grow(PartitionTarget
*t
, uint64_t size
) {
3804 assert(!t
->decrypted
);
3807 r
= loop_device_refresh_size(t
->loop
, UINT64_MAX
, size
);
3809 return log_error_errno(r
, "Failed to refresh loopback device size: %m");
3810 } else if (t
->fd
>= 0) {
3811 if (ftruncate(t
->fd
, size
) < 0)
3812 return log_error_errno(errno
, "Failed to grow '%s' to %s by truncation: %m",
3813 t
->path
, FORMAT_BYTES(size
));
3819 static int partition_target_sync(Context
*context
, Partition
*p
, PartitionTarget
*t
) {
3826 assert_se((whole_fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
3828 if (t
->decrypted
&& fsync(t
->decrypted
->fd
) < 0)
3829 return log_error_errno(errno
, "Failed to sync changes to '%s': %m", t
->decrypted
->volume
);
3832 r
= loop_device_sync(t
->loop
);
3834 return log_error_errno(r
, "Failed to sync loopback device: %m");
3835 } else if (t
->fd
>= 0) {
3838 if (lseek(whole_fd
, p
->offset
, SEEK_SET
) < 0)
3839 return log_error_errno(errno
, "Failed to seek to partition offset: %m");
3841 if (lseek(t
->fd
, 0, SEEK_SET
) < 0)
3842 return log_error_errno(errno
, "Failed to seek to start of temporary file: %m");
3844 if (fstat(t
->fd
, &st
) < 0)
3845 return log_error_errno(errno
, "Failed to stat temporary file: %m");
3847 if (st
.st_size
> (off_t
) p
->new_size
)
3848 return log_error_errno(SYNTHETIC_ERRNO(ENOSPC
),
3849 "Partition %" PRIu64
"'s contents (%s) don't fit in the partition (%s)",
3850 p
->partno
, FORMAT_BYTES(st
.st_size
), FORMAT_BYTES(p
->new_size
));
3852 r
= copy_bytes(t
->fd
, whole_fd
, UINT64_MAX
, COPY_REFLINK
|COPY_HOLES
|COPY_FSYNC
);
3854 return log_error_errno(r
, "Failed to copy bytes to partition: %m");
3856 if (fsync(t
->whole_fd
) < 0)
3857 return log_error_errno(errno
, "Failed to sync changes: %m");
3863 static int partition_encrypt(Context
*context
, Partition
*p
, PartitionTarget
*target
, bool offline
) {
3864 #if HAVE_LIBCRYPTSETUP && HAVE_CRYPT_SET_DATA_OFFSET && HAVE_CRYPT_REENCRYPT_INIT_BY_PASSPHRASE && HAVE_CRYPT_REENCRYPT
3865 const char *node
= partition_target_path(target
);
3866 struct crypt_params_luks2 luks_params
= {
3867 .label
= strempty(ASSERT_PTR(p
)->new_label
),
3868 .sector_size
= ASSERT_PTR(context
)->fs_sector_size
,
3869 .data_device
= offline
? node
: NULL
,
3871 struct crypt_params_reencrypt reencrypt_params
= {
3872 .mode
= CRYPT_REENCRYPT_ENCRYPT
,
3873 .direction
= CRYPT_REENCRYPT_BACKWARD
,
3874 .resilience
= "datashift",
3875 .data_shift
= LUKS2_METADATA_SIZE
/ 512,
3876 .luks2
= &luks_params
,
3877 .flags
= CRYPT_REENCRYPT_INITIALIZE_ONLY
|CRYPT_REENCRYPT_MOVE_FIRST_SEGMENT
,
3879 _cleanup_(sym_crypt_freep
) struct crypt_device
*cd
= NULL
;
3880 _cleanup_(erase_and_freep
) char *base64_encoded
= NULL
;
3881 _cleanup_fclose_
FILE *h
= NULL
;
3882 _cleanup_free_
char *hp
= NULL
, *vol
= NULL
, *dm_name
= NULL
;
3883 const char *passphrase
= NULL
;
3884 size_t passphrase_size
= 0;
3890 assert(p
->encrypt
!= ENCRYPT_OFF
);
3892 r
= dlopen_cryptsetup();
3894 return log_error_errno(r
, "libcryptsetup not found, cannot encrypt: %m");
3896 log_info("Encrypting future partition %" PRIu64
"...", p
->partno
);
3899 r
= var_tmp_dir(&vt
);
3901 return log_error_errno(r
, "Failed to determine temporary files directory: %m");
3903 r
= fopen_temporary_child(vt
, &h
, &hp
);
3905 return log_error_errno(r
, "Failed to create temporary LUKS header file: %m");
3907 /* Weird cryptsetup requirement which requires the header file to be the size of at least one
3909 if (ftruncate(fileno(h
), luks_params
.sector_size
) < 0)
3910 return log_error_errno(errno
, "Failed to grow temporary LUKS header file: %m");
3912 if (asprintf(&dm_name
, "luks-repart-%08" PRIx64
, random_u64()) < 0)
3915 vol
= path_join("/dev/mapper/", dm_name
);
3920 r
= sym_crypt_init(&cd
, offline
? hp
: node
);
3922 return log_error_errno(r
, "Failed to allocate libcryptsetup context for %s: %m", hp
);
3924 cryptsetup_enable_logging(cd
);
3927 /* Disable kernel keyring usage by libcryptsetup as a workaround for
3928 * https://gitlab.com/cryptsetup/cryptsetup/-/merge_requests/273. This makes sure that we can
3929 * do offline encryption even when repart is running in a container. */
3930 r
= sym_crypt_volume_key_keyring(cd
, false);
3932 return log_error_errno(r
, "Failed to disable kernel keyring: %m");
3934 r
= sym_crypt_metadata_locking(cd
, false);
3936 return log_error_errno(r
, "Failed to disable metadata locking: %m");
3938 r
= sym_crypt_set_data_offset(cd
, LUKS2_METADATA_SIZE
/ 512);
3940 return log_error_errno(r
, "Failed to set data offset: %m");
3943 r
= sym_crypt_format(
3948 SD_ID128_TO_UUID_STRING(p
->luks_uuid
),
3953 return log_error_errno(r
, "Failed to LUKS2 format future partition: %m");
3955 if (IN_SET(p
->encrypt
, ENCRYPT_KEY_FILE
, ENCRYPT_KEY_FILE_TPM2
)) {
3956 r
= sym_crypt_keyslot_add_by_volume_key(
3964 return log_error_errno(r
, "Failed to add LUKS2 key: %m");
3966 passphrase
= strempty(arg_key
);
3967 passphrase_size
= arg_key_size
;
3970 if (IN_SET(p
->encrypt
, ENCRYPT_TPM2
, ENCRYPT_KEY_FILE_TPM2
)) {
3972 _cleanup_(iovec_done
) struct iovec pubkey
= {}, blob
= {}, srk
= {};
3973 _cleanup_(iovec_done_erase
) struct iovec secret
= {};
3974 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
3975 ssize_t base64_encoded_size
;
3977 TPM2Flags flags
= 0;
3979 if (arg_tpm2_public_key_pcr_mask
!= 0) {
3980 r
= tpm2_load_pcr_public_key(arg_tpm2_public_key
, &pubkey
.iov_base
, &pubkey
.iov_len
);
3982 if (arg_tpm2_public_key
|| r
!= -ENOENT
)
3983 return log_error_errno(r
, "Failed to read TPM PCR public key: %m");
3985 log_debug_errno(r
, "Failed to read TPM2 PCR public key, proceeding without: %m");
3986 arg_tpm2_public_key_pcr_mask
= 0;
3990 TPM2B_PUBLIC
public;
3991 if (iovec_is_set(&pubkey
)) {
3992 r
= tpm2_tpm2b_public_from_pem(pubkey
.iov_base
, pubkey
.iov_len
, &public);
3994 return log_error_errno(r
, "Could not convert public key to TPM2B_PUBLIC: %m");
3997 _cleanup_(tpm2_pcrlock_policy_done
) Tpm2PCRLockPolicy pcrlock_policy
= {};
3998 if (arg_tpm2_pcrlock
) {
3999 r
= tpm2_pcrlock_policy_load(arg_tpm2_pcrlock
, &pcrlock_policy
);
4003 flags
|= TPM2_FLAGS_USE_PCRLOCK
;
4006 _cleanup_(tpm2_context_unrefp
) Tpm2Context
*tpm2_context
= NULL
;
4007 TPM2B_PUBLIC device_key_public
= {};
4008 if (arg_tpm2_device_key
) {
4009 r
= tpm2_load_public_key_file(arg_tpm2_device_key
, &device_key_public
);
4013 if (!tpm2_pcr_values_has_all_values(arg_tpm2_hash_pcr_values
, arg_tpm2_n_hash_pcr_values
))
4014 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
4015 "Must provide all PCR values when using TPM2 device key.");
4017 r
= tpm2_context_new(arg_tpm2_device
, &tpm2_context
);
4019 return log_error_errno(r
, "Failed to create TPM2 context: %m");
4021 if (!tpm2_pcr_values_has_all_values(arg_tpm2_hash_pcr_values
, arg_tpm2_n_hash_pcr_values
)) {
4022 r
= tpm2_pcr_read_missing_values(tpm2_context
, arg_tpm2_hash_pcr_values
, arg_tpm2_n_hash_pcr_values
);
4024 return log_error_errno(r
, "Could not read pcr values: %m");
4028 uint16_t hash_pcr_bank
= 0;
4029 uint32_t hash_pcr_mask
= 0;
4030 if (arg_tpm2_n_hash_pcr_values
> 0) {
4032 r
= tpm2_pcr_values_hash_count(arg_tpm2_hash_pcr_values
, arg_tpm2_n_hash_pcr_values
, &hash_count
);
4034 return log_error_errno(r
, "Could not get hash count: %m");
4037 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Multiple PCR banks selected.");
4039 hash_pcr_bank
= arg_tpm2_hash_pcr_values
[0].hash
;
4040 r
= tpm2_pcr_values_to_mask(arg_tpm2_hash_pcr_values
, arg_tpm2_n_hash_pcr_values
, hash_pcr_bank
, &hash_pcr_mask
);
4042 return log_error_errno(r
, "Could not get hash mask: %m");
4045 TPM2B_DIGEST policy
= TPM2B_DIGEST_MAKE(NULL
, TPM2_SHA256_DIGEST_SIZE
);
4046 r
= tpm2_calculate_sealing_policy(
4047 arg_tpm2_hash_pcr_values
,
4048 arg_tpm2_n_hash_pcr_values
,
4049 iovec_is_set(&pubkey
) ? &public : NULL
,
4050 /* use_pin= */ false,
4051 arg_tpm2_pcrlock
? &pcrlock_policy
: NULL
,
4054 return log_error_errno(r
, "Could not calculate sealing policy digest: %m");
4056 if (arg_tpm2_device_key
)
4057 r
= tpm2_calculate_seal(
4058 arg_tpm2_seal_key_handle
,
4060 /* attributes= */ NULL
,
4068 r
= tpm2_seal(tpm2_context
,
4069 arg_tpm2_seal_key_handle
,
4074 /* ret_primary_alg= */ NULL
,
4077 return log_error_errno(r
, "Failed to seal to TPM2: %m");
4079 base64_encoded_size
= base64mem(secret
.iov_base
, secret
.iov_len
, &base64_encoded
);
4080 if (base64_encoded_size
< 0)
4081 return log_error_errno(base64_encoded_size
, "Failed to base64 encode secret key: %m");
4083 r
= cryptsetup_set_minimal_pbkdf(cd
);
4085 return log_error_errno(r
, "Failed to set minimal PBKDF: %m");
4087 keyslot
= sym_crypt_keyslot_add_by_volume_key(
4090 /* volume_key= */ NULL
,
4091 /* volume_key_size= */ VOLUME_KEY_SIZE
,
4093 base64_encoded_size
);
4095 return log_error_errno(keyslot
, "Failed to add new TPM2 key: %m");
4097 r
= tpm2_make_luks2_json(
4102 arg_tpm2_public_key_pcr_mask
,
4103 /* primary_alg= */ 0,
4105 &IOVEC_MAKE(policy
.buffer
, policy
.size
),
4106 /* salt= */ NULL
, /* no salt because tpm2_seal has no pin */
4108 &pcrlock_policy
.nv_handle
,
4112 return log_error_errno(r
, "Failed to prepare TPM2 JSON token object: %m");
4114 r
= cryptsetup_add_token_json(cd
, v
);
4116 return log_error_errno(r
, "Failed to add TPM2 JSON token to LUKS2 header: %m");
4118 passphrase
= base64_encoded
;
4119 passphrase_size
= strlen(base64_encoded
);
4121 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
4122 "Support for TPM2 enrollment not enabled.");
4127 r
= sym_crypt_reencrypt_init_by_passphrase(
4134 sym_crypt_get_cipher(cd
),
4135 sym_crypt_get_cipher_mode(cd
),
4138 return log_error_errno(r
, "Failed to prepare for reencryption: %m");
4140 /* crypt_reencrypt_init_by_passphrase() doesn't actually put the LUKS header at the front, we
4141 * have to do that ourselves. */
4146 r
= sym_crypt_init(&cd
, node
);
4148 return log_error_errno(r
, "Failed to allocate libcryptsetup context for %s: %m", node
);
4150 r
= sym_crypt_header_restore(cd
, CRYPT_LUKS2
, hp
);
4152 return log_error_errno(r
, "Failed to place new LUKS header at head of %s: %m", node
);
4154 reencrypt_params
.flags
&= ~CRYPT_REENCRYPT_INITIALIZE_ONLY
;
4156 r
= sym_crypt_reencrypt_init_by_passphrase(
4167 return log_error_errno(r
, "Failed to load reencryption context: %m");
4169 r
= sym_crypt_reencrypt(cd
, NULL
);
4171 return log_error_errno(r
, "Failed to encrypt %s: %m", node
);
4173 _cleanup_free_ DecryptedPartitionTarget
*t
= NULL
;
4174 _cleanup_close_
int dev_fd
= -1;
4176 r
= sym_crypt_activate_by_volume_key(
4181 arg_discard
? CRYPT_ACTIVATE_ALLOW_DISCARDS
: 0);
4183 return log_error_errno(r
, "Failed to activate LUKS superblock: %m");
4185 dev_fd
= open(vol
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
);
4187 return log_error_errno(errno
, "Failed to open LUKS volume '%s': %m", vol
);
4189 if (flock(dev_fd
, LOCK_EX
) < 0)
4190 return log_error_errno(errno
, "Failed to lock '%s': %m", vol
);
4192 t
= new(DecryptedPartitionTarget
, 1);
4196 *t
= (DecryptedPartitionTarget
) {
4197 .fd
= TAKE_FD(dev_fd
),
4198 .dm_name
= TAKE_PTR(dm_name
),
4199 .volume
= TAKE_PTR(vol
),
4200 .device
= TAKE_PTR(cd
),
4203 target
->decrypted
= TAKE_PTR(t
);
4206 log_info("Successfully encrypted future partition %" PRIu64
".", p
->partno
);
4210 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
4211 "libcryptsetup is not supported or is missing required symbols, cannot encrypt: %m");
4215 static int partition_format_verity_hash(
4219 const char *data_node
) {
4221 #if HAVE_LIBCRYPTSETUP
4223 _cleanup_(partition_target_freep
) PartitionTarget
*t
= NULL
;
4224 _cleanup_(sym_crypt_freep
) struct crypt_device
*cd
= NULL
;
4225 _cleanup_free_
char *hint
= NULL
;
4230 assert(p
->verity
== VERITY_HASH
);
4236 if (PARTITION_EXISTS(p
)) /* Never format existing partitions */
4239 /* Minimized partitions will use the copy blocks logic so let's make sure to skip those here. */
4240 if (p
->copy_blocks_fd
>= 0)
4243 assert_se(dp
= p
->siblings
[VERITY_DATA
]);
4244 assert(!dp
->dropped
);
4246 (void) partition_hint(p
, node
, &hint
);
4248 r
= dlopen_cryptsetup();
4250 return log_error_errno(r
, "libcryptsetup not found, cannot setup verity: %m");
4253 r
= partition_target_prepare(context
, p
, p
->new_size
, /*need_path=*/ true, &t
);
4257 node
= partition_target_path(t
);
4260 if (p
->verity_data_block_size
== UINT64_MAX
)
4261 p
->verity_data_block_size
= context
->fs_sector_size
;
4262 if (p
->verity_hash_block_size
== UINT64_MAX
)
4263 p
->verity_hash_block_size
= context
->fs_sector_size
;
4265 r
= sym_crypt_init(&cd
, node
);
4267 return log_error_errno(r
, "Failed to allocate libcryptsetup context for %s: %m", node
);
4269 cryptsetup_enable_logging(cd
);
4271 r
= sym_crypt_format(
4272 cd
, CRYPT_VERITY
, NULL
, NULL
, SD_ID128_TO_UUID_STRING(p
->verity_uuid
), NULL
, 0,
4273 &(struct crypt_params_verity
){
4274 .data_device
= data_node
,
4275 .flags
= CRYPT_VERITY_CREATE_HASH
,
4276 .hash_name
= "sha256",
4278 .data_block_size
= p
->verity_data_block_size
,
4279 .hash_block_size
= p
->verity_hash_block_size
,
4280 .salt_size
= sizeof(p
->verity_salt
),
4281 .salt
= (const char*)p
->verity_salt
,
4284 /* libcryptsetup reports non-descriptive EIO errors for every I/O failure. Luckily, it
4285 * doesn't clobber errno so let's check for ENOSPC so we can report a better error if the
4286 * partition is too small. */
4287 if (r
== -EIO
&& errno
== ENOSPC
)
4288 return log_error_errno(errno
,
4289 "Verity hash data does not fit in partition %s with size %s",
4290 strna(hint
), FORMAT_BYTES(p
->new_size
));
4292 return log_error_errno(r
, "Failed to setup verity hash data of partition %s: %m", strna(hint
));
4296 r
= partition_target_sync(context
, p
, t
);
4301 r
= sym_crypt_get_volume_key_size(cd
);
4303 return log_error_errno(r
, "Failed to determine verity root hash size of partition %s: %m", strna(hint
));
4305 _cleanup_(iovec_done
) struct iovec rh
= {
4306 .iov_base
= malloc(r
),
4312 r
= sym_crypt_volume_key_get(cd
, CRYPT_ANY_SLOT
, (char *) rh
.iov_base
, &rh
.iov_len
, NULL
, 0);
4314 return log_error_errno(r
, "Failed to get verity root hash of partition %s: %m", strna(hint
));
4316 assert(rh
.iov_len
>= sizeof(sd_id128_t
) * 2);
4318 if (!dp
->new_uuid_is_set
) {
4319 memcpy_safe(dp
->new_uuid
.bytes
, rh
.iov_base
, sizeof(sd_id128_t
));
4320 dp
->new_uuid_is_set
= true;
4323 if (!p
->new_uuid_is_set
) {
4324 memcpy_safe(p
->new_uuid
.bytes
, (uint8_t*) rh
.iov_base
+ (rh
.iov_len
- sizeof(sd_id128_t
)), sizeof(sd_id128_t
));
4325 p
->new_uuid_is_set
= true;
4328 p
->roothash
= TAKE_STRUCT(rh
);
4332 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "libcryptsetup is not supported, cannot setup verity hashes: %m");
4336 static int sign_verity_roothash(
4337 const struct iovec
*roothash
,
4338 struct iovec
*ret_signature
) {
4341 _cleanup_(BIO_freep
) BIO
*rb
= NULL
;
4342 _cleanup_(PKCS7_freep
) PKCS7
*p7
= NULL
;
4343 _cleanup_free_
char *hex
= NULL
;
4344 _cleanup_free_
uint8_t *sig
= NULL
;
4348 assert(iovec_is_set(roothash
));
4349 assert(ret_signature
);
4351 hex
= hexmem(roothash
->iov_base
, roothash
->iov_len
);
4355 rb
= BIO_new_mem_buf(hex
, -1);
4359 p7
= PKCS7_sign(arg_certificate
, arg_private_key
, NULL
, rb
, PKCS7_DETACHED
|PKCS7_NOATTR
|PKCS7_BINARY
);
4361 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to calculate PKCS7 signature: %s",
4362 ERR_error_string(ERR_get_error(), NULL
));
4364 sigsz
= i2d_PKCS7(p7
, &sig
);
4366 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to convert PKCS7 signature to DER: %s",
4367 ERR_error_string(ERR_get_error(), NULL
));
4369 *ret_signature
= IOVEC_MAKE(TAKE_PTR(sig
), sigsz
);
4373 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "OpenSSL is not supported, cannot setup verity signature: %m");
4377 static int partition_format_verity_sig(Context
*context
, Partition
*p
) {
4378 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
4379 _cleanup_(iovec_done
) struct iovec sig
= {};
4380 _cleanup_free_
char *text
= NULL
, *hint
= NULL
;
4382 uint8_t fp
[X509_FINGERPRINT_SIZE
];
4385 assert(p
->verity
== VERITY_SIG
);
4390 if (PARTITION_EXISTS(p
))
4393 (void) partition_hint(p
, context
->node
, &hint
);
4395 assert_se(hp
= p
->siblings
[VERITY_HASH
]);
4396 assert(!hp
->dropped
);
4398 assert(arg_certificate
);
4400 assert_se((whole_fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
4402 r
= sign_verity_roothash(&hp
->roothash
, &sig
);
4406 r
= x509_fingerprint(arg_certificate
, fp
);
4408 return log_error_errno(r
, "Unable to calculate X509 certificate fingerprint: %m");
4412 JSON_BUILD_PAIR("rootHash", JSON_BUILD_HEX(hp
->roothash
.iov_base
, hp
->roothash
.iov_len
)),
4414 "certificateFingerprint",
4415 JSON_BUILD_HEX(fp
, sizeof(fp
))
4417 JSON_BUILD_PAIR("signature", JSON_BUILD_IOVEC_BASE64(&sig
))
4421 return log_error_errno(r
, "Failed to build verity signature JSON object: %m");
4423 r
= json_variant_format(v
, 0, &text
);
4425 return log_error_errno(r
, "Failed to format verity signature JSON object: %m");
4427 if (strlen(text
)+1 > p
->new_size
)
4428 return log_error_errno(SYNTHETIC_ERRNO(E2BIG
), "Verity signature too long for partition: %m");
4430 r
= strgrowpad0(&text
, p
->new_size
);
4432 return log_error_errno(r
, "Failed to pad string to %s", FORMAT_BYTES(p
->new_size
));
4434 if (lseek(whole_fd
, p
->offset
, SEEK_SET
) < 0)
4435 return log_error_errno(errno
, "Failed to seek to partition %s offset: %m", strna(hint
));
4437 r
= loop_write(whole_fd
, text
, p
->new_size
);
4439 return log_error_errno(r
, "Failed to write verity signature to partition %s: %m", strna(hint
));
4441 if (fsync(whole_fd
) < 0)
4442 return log_error_errno(errno
, "Failed to synchronize partition %s: %m", strna(hint
));
4447 static int context_copy_blocks(Context
*context
) {
4452 /* Copy in file systems on the block level */
4454 LIST_FOREACH(partitions
, p
, context
->partitions
) {
4455 _cleanup_(partition_target_freep
) PartitionTarget
*t
= NULL
;
4457 if (p
->copy_blocks_fd
< 0)
4463 if (PARTITION_EXISTS(p
)) /* Never copy over existing partitions */
4466 if (partition_type_defer(&p
->type
))
4469 assert(p
->new_size
!= UINT64_MAX
);
4470 assert(p
->copy_blocks_size
!= UINT64_MAX
);
4471 assert(p
->new_size
>= p
->copy_blocks_size
+ (p
->encrypt
!= ENCRYPT_OFF
? LUKS2_METADATA_KEEP_FREE
: 0));
4473 usec_t start_timestamp
= now(CLOCK_MONOTONIC
);
4475 r
= partition_target_prepare(context
, p
, p
->new_size
,
4476 /*need_path=*/ p
->encrypt
!= ENCRYPT_OFF
|| p
->siblings
[VERITY_HASH
],
4481 if (p
->encrypt
!= ENCRYPT_OFF
&& t
->loop
) {
4482 r
= partition_encrypt(context
, p
, t
, /* offline = */ false);
4487 if (p
->copy_blocks_offset
== UINT64_MAX
)
4488 log_info("Copying in '%s' (%s) on block level into future partition %" PRIu64
".",
4489 p
->copy_blocks_path
, FORMAT_BYTES(p
->copy_blocks_size
), p
->partno
);
4491 log_info("Copying in '%s' @ %" PRIu64
" (%s) on block level into future partition %" PRIu64
".",
4492 p
->copy_blocks_path
, p
->copy_blocks_offset
, FORMAT_BYTES(p
->copy_blocks_size
), p
->partno
);
4494 if (lseek(p
->copy_blocks_fd
, p
->copy_blocks_offset
, SEEK_SET
) < 0)
4495 return log_error_errno(errno
, "Failed to seek to copy blocks offset in %s: %m", p
->copy_blocks_path
);
4498 r
= copy_bytes(p
->copy_blocks_fd
, partition_target_fd(t
), p
->copy_blocks_size
, COPY_REFLINK
);
4500 return log_error_errno(r
, "Failed to copy in data from '%s': %m", p
->copy_blocks_path
);
4502 log_info("Copying in of '%s' on block level completed.", p
->copy_blocks_path
);
4504 if (p
->encrypt
!= ENCRYPT_OFF
&& !t
->loop
) {
4505 r
= partition_encrypt(context
, p
, t
, /* offline = */ true);
4510 r
= partition_target_sync(context
, p
, t
);
4514 usec_t time_spent
= usec_sub_unsigned(now(CLOCK_MONOTONIC
), start_timestamp
);
4515 if (time_spent
> 250 * USEC_PER_MSEC
) /* Show throughput, but not if we spent too little time on it, since it's just noise then */
4516 log_info("Block level copying and synchronization of partition %" PRIu64
" complete in %s (%s/s).",
4517 p
->partno
, FORMAT_TIMESPAN(time_spent
, 0), FORMAT_BYTES((uint64_t) ((double) p
->copy_blocks_size
/ time_spent
* USEC_PER_SEC
)));
4519 log_info("Block level copying and synchronization of partition %" PRIu64
" complete in %s.",
4520 p
->partno
, FORMAT_TIMESPAN(time_spent
, 0));
4522 if (p
->siblings
[VERITY_HASH
] && !partition_type_defer(&p
->siblings
[VERITY_HASH
]->type
)) {
4523 r
= partition_format_verity_hash(context
, p
->siblings
[VERITY_HASH
],
4524 /* node = */ NULL
, partition_target_path(t
));
4529 if (p
->siblings
[VERITY_SIG
] && !partition_type_defer(&p
->siblings
[VERITY_SIG
]->type
)) {
4530 r
= partition_format_verity_sig(context
, p
->siblings
[VERITY_SIG
]);
4539 static int add_exclude_path(const char *path
, Hashmap
**denylist
, DenyType type
) {
4540 _cleanup_free_
struct stat
*st
= NULL
;
4546 st
= new(struct stat
, 1);
4550 r
= chase_and_stat(path
, arg_copy_source
, CHASE_PREFIX_ROOT
, NULL
, st
);
4554 return log_error_errno(r
, "Failed to stat source file '%s/%s': %m", strempty(arg_copy_source
), path
);
4556 r
= hashmap_ensure_put(denylist
, &inode_hash_ops
, st
, INT_TO_PTR(type
));
4567 static int make_copy_files_denylist(
4574 _cleanup_hashmap_free_ Hashmap
*denylist
= NULL
;
4583 /* Always exclude the top level APIVFS and temporary directories since the contents of these
4584 * directories are almost certainly not intended to end up in an image. */
4586 NULSTR_FOREACH(s
, APIVFS_TMP_DIRS_NULSTR
) {
4587 r
= add_exclude_path(s
, &denylist
, DENY_CONTENTS
);
4592 /* Add the user configured excludes. */
4594 STRV_FOREACH(e
, p
->exclude_files_source
) {
4595 r
= add_exclude_path(*e
, &denylist
, endswith(*e
, "/") ? DENY_CONTENTS
: DENY_INODE
);
4600 STRV_FOREACH(e
, p
->exclude_files_target
) {
4601 _cleanup_free_
char *path
= NULL
;
4603 const char *s
= path_startswith(*e
, target
);
4607 path
= path_join(source
, s
);
4611 r
= add_exclude_path(path
, &denylist
, endswith(*e
, "/") ? DENY_CONTENTS
: DENY_INODE
);
4616 /* If we're populating a root partition, we don't want any files to end up under the APIVFS mount
4617 * points. While we already exclude <source>/proc, users could still do something such as
4618 * "CopyFiles=/abc:/". Now, if /abc has a proc subdirectory with files in it, those will end up in
4619 * the top level proc directory in the root partition, which we want to avoid. To deal with these
4620 * cases, whenever we're populating a root partition and the target of CopyFiles= is the root
4621 * directory of the root partition, we exclude all directories under the source that are named after
4622 * APIVFS directories or named after mount points of other partitions that are also going to be part
4625 if (p
->type
.designator
== PARTITION_ROOT
&& empty_or_root(target
)) {
4626 LIST_FOREACH(partitions
, q
, context
->partitions
) {
4627 if (q
->type
.designator
== PARTITION_ROOT
)
4630 const char *sources
= gpt_partition_type_mountpoint_nulstr(q
->type
);
4634 NULSTR_FOREACH(s
, sources
) {
4635 _cleanup_free_
char *path
= NULL
;
4637 /* Exclude only the children of partition mount points so that the nested
4638 * partition mount point itself still ends up in the upper partition. */
4640 path
= path_join(source
, s
);
4644 r
= add_exclude_path(path
, &denylist
, DENY_CONTENTS
);
4650 NULSTR_FOREACH(s
, APIVFS_TMP_DIRS_NULSTR
) {
4651 _cleanup_free_
char *path
= NULL
;
4653 path
= path_join(source
, s
);
4657 r
= add_exclude_path(path
, &denylist
, DENY_CONTENTS
);
4663 *ret
= TAKE_PTR(denylist
);
4667 static int add_subvolume_path(const char *path
, Set
**subvolumes
) {
4668 _cleanup_free_
struct stat
*st
= NULL
;
4674 st
= new(struct stat
, 1);
4678 r
= chase_and_stat(path
, arg_copy_source
, CHASE_PREFIX_ROOT
, NULL
, st
);
4682 return log_error_errno(r
, "Failed to stat source file '%s/%s': %m", strempty(arg_copy_source
), path
);
4684 r
= set_ensure_consume(subvolumes
, &inode_hash_ops
, TAKE_PTR(st
));
4691 static int make_subvolumes_set(
4697 _cleanup_set_free_ Set
*subvolumes
= NULL
;
4705 STRV_FOREACH(subvolume
, p
->subvolumes
) {
4706 _cleanup_free_
char *path
= NULL
;
4708 const char *s
= path_startswith(*subvolume
, target
);
4712 path
= path_join(source
, s
);
4716 r
= add_subvolume_path(path
, &subvolumes
);
4721 *ret
= TAKE_PTR(subvolumes
);
4725 static int do_copy_files(Context
*context
, Partition
*p
, const char *root
) {
4731 /* copy_tree_at() automatically copies the permissions of source directories to target directories if
4732 * it created them. However, the root directory is created by us, so we have to manually take care
4733 * that it is initialized. We use the first source directory targeting "/" as the metadata source for
4734 * the root directory. */
4735 STRV_FOREACH_PAIR(source
, target
, p
->copy_files
) {
4736 _cleanup_close_
int rfd
= -EBADF
, sfd
= -EBADF
;
4738 if (!path_equal(*target
, "/"))
4741 rfd
= open(root
, O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
);
4745 sfd
= chase_and_open(*source
, arg_copy_source
, CHASE_PREFIX_ROOT
, O_PATH
|O_DIRECTORY
|O_CLOEXEC
|O_NOCTTY
, NULL
);
4747 return log_error_errno(sfd
, "Failed to open source file '%s%s': %m", strempty(arg_copy_source
), *source
);
4749 (void) copy_xattr(sfd
, NULL
, rfd
, NULL
, COPY_ALL_XATTRS
);
4750 (void) copy_access(sfd
, rfd
);
4751 (void) copy_times(sfd
, rfd
, 0);
4756 STRV_FOREACH_PAIR(source
, target
, p
->copy_files
) {
4757 _cleanup_hashmap_free_ Hashmap
*denylist
= NULL
;
4758 _cleanup_set_free_ Set
*subvolumes_by_source_inode
= NULL
;
4759 _cleanup_close_
int sfd
= -EBADF
, pfd
= -EBADF
, tfd
= -EBADF
;
4761 r
= make_copy_files_denylist(context
, p
, *source
, *target
, &denylist
);
4765 r
= make_subvolumes_set(context
, p
, *source
, *target
, &subvolumes_by_source_inode
);
4769 sfd
= chase_and_open(*source
, arg_copy_source
, CHASE_PREFIX_ROOT
, O_CLOEXEC
|O_NOCTTY
, NULL
);
4770 if (sfd
== -ENOENT
) {
4771 log_notice_errno(sfd
, "Failed to open source file '%s%s', skipping: %m", strempty(arg_copy_source
), *source
);
4775 return log_error_errno(sfd
, "Failed to open source file '%s%s': %m", strempty(arg_copy_source
), *source
);
4777 r
= fd_verify_regular(sfd
);
4780 return log_error_errno(r
, "Failed to check type of source file '%s': %m", *source
);
4782 /* We are looking at a directory */
4783 tfd
= chase_and_open(*target
, root
, CHASE_PREFIX_ROOT
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
, NULL
);
4785 _cleanup_free_
char *dn
= NULL
, *fn
= NULL
;
4788 return log_error_errno(tfd
, "Failed to open target directory '%s': %m", *target
);
4790 r
= path_extract_filename(*target
, &fn
);
4792 return log_error_errno(r
, "Failed to extract filename from '%s': %m", *target
);
4794 r
= path_extract_directory(*target
, &dn
);
4796 return log_error_errno(r
, "Failed to extract directory from '%s': %m", *target
);
4798 r
= mkdir_p_root(root
, dn
, UID_INVALID
, GID_INVALID
, 0755, p
->subvolumes
);
4800 return log_error_errno(r
, "Failed to create parent directory '%s': %m", dn
);
4802 pfd
= chase_and_open(dn
, root
, CHASE_PREFIX_ROOT
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
, NULL
);
4804 return log_error_errno(pfd
, "Failed to open parent directory of target: %m");
4809 UID_INVALID
, GID_INVALID
,
4810 COPY_REFLINK
|COPY_HOLES
|COPY_MERGE
|COPY_REPLACE
|COPY_SIGINT
|COPY_HARDLINKS
|COPY_ALL_XATTRS
|COPY_GRACEFUL_WARN
|COPY_TRUNCATE
,
4811 denylist
, subvolumes_by_source_inode
);
4816 UID_INVALID
, GID_INVALID
,
4817 COPY_REFLINK
|COPY_HOLES
|COPY_MERGE
|COPY_REPLACE
|COPY_SIGINT
|COPY_HARDLINKS
|COPY_ALL_XATTRS
|COPY_GRACEFUL_WARN
|COPY_TRUNCATE
,
4818 denylist
, subvolumes_by_source_inode
);
4820 return log_error_errno(r
, "Failed to copy '%s%s' to '%s%s': %m",
4821 strempty(arg_copy_source
), *source
, strempty(root
), *target
);
4823 _cleanup_free_
char *dn
= NULL
, *fn
= NULL
;
4825 /* We are looking at a regular file */
4827 r
= path_extract_filename(*target
, &fn
);
4828 if (r
== -EADDRNOTAVAIL
|| r
== O_DIRECTORY
)
4829 return log_error_errno(SYNTHETIC_ERRNO(EISDIR
),
4830 "Target path '%s' refers to a directory, but source path '%s' refers to regular file, can't copy.", *target
, *source
);
4832 return log_error_errno(r
, "Failed to extract filename from '%s': %m", *target
);
4834 r
= path_extract_directory(*target
, &dn
);
4836 return log_error_errno(r
, "Failed to extract directory from '%s': %m", *target
);
4838 r
= mkdir_p_root(root
, dn
, UID_INVALID
, GID_INVALID
, 0755, p
->subvolumes
);
4840 return log_error_errno(r
, "Failed to create parent directory: %m");
4842 pfd
= chase_and_open(dn
, root
, CHASE_PREFIX_ROOT
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
, NULL
);
4844 return log_error_errno(pfd
, "Failed to open parent directory of target: %m");
4846 tfd
= openat(pfd
, fn
, O_CREAT
|O_EXCL
|O_WRONLY
|O_CLOEXEC
, 0700);
4848 return log_error_errno(errno
, "Failed to create target file '%s': %m", *target
);
4850 r
= copy_bytes(sfd
, tfd
, UINT64_MAX
, COPY_REFLINK
|COPY_HOLES
|COPY_SIGINT
|COPY_TRUNCATE
);
4852 return log_error_errno(r
, "Failed to copy '%s' to '%s%s': %m", *source
, strempty(arg_copy_source
), *target
);
4854 (void) copy_xattr(sfd
, NULL
, tfd
, NULL
, COPY_ALL_XATTRS
);
4855 (void) copy_access(sfd
, tfd
);
4856 (void) copy_times(sfd
, tfd
, 0);
4863 static int do_make_directories(Partition
*p
, const char *root
) {
4869 STRV_FOREACH(d
, p
->make_directories
) {
4870 r
= mkdir_p_root(root
, *d
, UID_INVALID
, GID_INVALID
, 0755, p
->subvolumes
);
4872 return log_error_errno(r
, "Failed to create directory '%s' in file system: %m", *d
);
4878 static bool partition_needs_populate(Partition
*p
) {
4880 return !strv_isempty(p
->copy_files
) || !strv_isempty(p
->make_directories
);
4883 static int partition_populate_directory(Context
*context
, Partition
*p
, char **ret
) {
4884 _cleanup_(rm_rf_physical_and_freep
) char *root
= NULL
;
4890 log_info("Populating %s filesystem.", p
->format
);
4892 r
= var_tmp_dir(&vt
);
4894 return log_error_errno(r
, "Could not determine temporary directory: %m");
4896 r
= tempfn_random_child(vt
, "repart", &root
);
4898 return log_error_errno(r
, "Failed to generate temporary directory: %m");
4900 r
= mkdir(root
, 0755);
4902 return log_error_errno(errno
, "Failed to create temporary directory: %m");
4904 r
= do_copy_files(context
, p
, root
);
4908 r
= do_make_directories(p
, root
);
4912 log_info("Successfully populated %s filesystem.", p
->format
);
4914 *ret
= TAKE_PTR(root
);
4918 static int partition_populate_filesystem(Context
*context
, Partition
*p
, const char *node
) {
4924 log_info("Populating %s filesystem.", p
->format
);
4926 /* We copy in a child process, since we have to mount the fs for that, and we don't want that fs to
4927 * appear in the host namespace. Hence we fork a child that has its own file system namespace and
4928 * detached mount propagation. */
4930 r
= safe_fork("(sd-copy)", FORK_DEATHSIG_SIGTERM
|FORK_LOG
|FORK_WAIT
|FORK_NEW_MOUNTNS
|FORK_MOUNTNS_SLAVE
, NULL
);
4934 static const char fs
[] = "/run/systemd/mount-root";
4935 /* This is a child process with its own mount namespace and propagation to host turned off */
4937 r
= mkdir_p(fs
, 0700);
4939 log_error_errno(r
, "Failed to create mount point: %m");
4940 _exit(EXIT_FAILURE
);
4943 if (mount_nofollow_verbose(LOG_ERR
, node
, fs
, p
->format
, MS_NOATIME
|MS_NODEV
|MS_NOEXEC
|MS_NOSUID
, NULL
) < 0)
4944 _exit(EXIT_FAILURE
);
4946 if (do_copy_files(context
, p
, fs
) < 0)
4947 _exit(EXIT_FAILURE
);
4949 if (do_make_directories(p
, fs
) < 0)
4950 _exit(EXIT_FAILURE
);
4952 r
= syncfs_path(AT_FDCWD
, fs
);
4954 log_error_errno(r
, "Failed to synchronize written files: %m");
4955 _exit(EXIT_FAILURE
);
4958 _exit(EXIT_SUCCESS
);
4961 log_info("Successfully populated %s filesystem.", p
->format
);
4965 static int context_mkfs(Context
*context
) {
4970 /* Make a file system */
4972 LIST_FOREACH(partitions
, p
, context
->partitions
) {
4973 _cleanup_(rm_rf_physical_and_freep
) char *root
= NULL
;
4974 _cleanup_(partition_target_freep
) PartitionTarget
*t
= NULL
;
4975 _cleanup_strv_free_
char **extra_mkfs_options
= NULL
;
4980 if (PARTITION_EXISTS(p
)) /* Never format existing partitions */
4986 /* Minimized partitions will use the copy blocks logic so let's make sure to skip those here. */
4987 if (p
->copy_blocks_fd
>= 0)
4990 if (partition_type_defer(&p
->type
))
4993 assert(p
->offset
!= UINT64_MAX
);
4994 assert(p
->new_size
!= UINT64_MAX
);
4995 assert(p
->new_size
>= (p
->encrypt
!= ENCRYPT_OFF
? LUKS2_METADATA_KEEP_FREE
: 0));
4997 /* If we're doing encryption, we make sure we keep free space at the end which is required
4998 * for cryptsetup's offline encryption. */
4999 r
= partition_target_prepare(context
, p
,
5000 p
->new_size
- (p
->encrypt
!= ENCRYPT_OFF
? LUKS2_METADATA_KEEP_FREE
: 0),
5001 /*need_path=*/ true,
5006 if (p
->encrypt
!= ENCRYPT_OFF
&& t
->loop
) {
5007 r
= partition_target_grow(t
, p
->new_size
);
5011 r
= partition_encrypt(context
, p
, t
, /* offline = */ false);
5013 return log_error_errno(r
, "Failed to encrypt device: %m");
5016 log_info("Formatting future partition %" PRIu64
".", p
->partno
);
5018 /* If we're not writing to a loop device or if we're populating a read-only filesystem, we
5019 * have to populate using the filesystem's mkfs's --root (or equivalent) option. To do that,
5020 * we need to set up the final directory tree beforehand. */
5022 if (partition_needs_populate(p
) && (!t
->loop
|| fstype_is_ro(p
->format
))) {
5023 if (!mkfs_supports_root_option(p
->format
))
5024 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
),
5025 "Loop device access is required to populate %s filesystems.",
5028 r
= partition_populate_directory(context
, p
, &root
);
5033 r
= mkfs_options_from_env("REPART", p
->format
, &extra_mkfs_options
);
5035 return log_error_errno(r
,
5036 "Failed to determine mkfs command line options for '%s': %m",
5039 r
= make_filesystem(partition_target_path(t
), p
->format
, strempty(p
->new_label
), root
,
5040 p
->fs_uuid
, arg_discard
, /* quiet = */ false,
5041 context
->fs_sector_size
, extra_mkfs_options
);
5045 /* The mkfs binary we invoked might have removed our temporary file when we're not operating
5046 * on a loop device, so let's make sure we open the file again to make sure our file
5047 * descriptor points to any potential new file. */
5049 if (t
->fd
>= 0 && t
->path
&& !t
->loop
) {
5051 t
->fd
= open(t
->path
, O_RDWR
|O_CLOEXEC
);
5053 return log_error_errno(errno
, "Failed to reopen temporary file: %m");
5056 log_info("Successfully formatted future partition %" PRIu64
".", p
->partno
);
5058 /* If we're writing to a loop device, we can now mount the empty filesystem and populate it. */
5059 if (partition_needs_populate(p
) && !root
) {
5062 r
= partition_populate_filesystem(context
, p
, partition_target_path(t
));
5067 if (p
->encrypt
!= ENCRYPT_OFF
&& !t
->loop
) {
5068 r
= partition_target_grow(t
, p
->new_size
);
5072 r
= partition_encrypt(context
, p
, t
, /* offline = */ true);
5074 return log_error_errno(r
, "Failed to encrypt device: %m");
5077 /* Note that we always sync explicitly here, since mkfs.fat doesn't do that on its own, and
5078 * if we don't sync before detaching a block device the in-flight sectors possibly won't hit
5081 r
= partition_target_sync(context
, p
, t
);
5085 if (p
->siblings
[VERITY_HASH
] && !partition_type_defer(&p
->siblings
[VERITY_HASH
]->type
)) {
5086 r
= partition_format_verity_hash(context
, p
->siblings
[VERITY_HASH
],
5087 /* node = */ NULL
, partition_target_path(t
));
5092 if (p
->siblings
[VERITY_SIG
] && !partition_type_defer(&p
->siblings
[VERITY_SIG
]->type
)) {
5093 r
= partition_format_verity_sig(context
, p
->siblings
[VERITY_SIG
]);
5102 static int parse_x509_certificate(const char *certificate
, size_t certificate_size
, X509
**ret
) {
5104 _cleanup_(X509_freep
) X509
*cert
= NULL
;
5105 _cleanup_(BIO_freep
) BIO
*cb
= NULL
;
5107 assert(certificate
);
5108 assert(certificate_size
> 0);
5111 cb
= BIO_new_mem_buf(certificate
, certificate_size
);
5115 cert
= PEM_read_bio_X509(cb
, NULL
, NULL
, NULL
);
5117 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Failed to parse X.509 certificate: %s",
5118 ERR_error_string(ERR_get_error(), NULL
));
5121 *ret
= TAKE_PTR(cert
);
5125 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "OpenSSL is not supported, cannot parse X509 certificate.");
5129 static int parse_private_key(const char *key
, size_t key_size
, EVP_PKEY
**ret
) {
5131 _cleanup_(BIO_freep
) BIO
*kb
= NULL
;
5132 _cleanup_(EVP_PKEY_freep
) EVP_PKEY
*pk
= NULL
;
5135 assert(key_size
> 0);
5138 kb
= BIO_new_mem_buf(key
, key_size
);
5142 pk
= PEM_read_bio_PrivateKey(kb
, NULL
, NULL
, NULL
);
5144 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Failed to parse PEM private key: %s",
5145 ERR_error_string(ERR_get_error(), NULL
));
5148 *ret
= TAKE_PTR(pk
);
5152 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "OpenSSL is not supported, cannot parse private key.");
5156 static int partition_acquire_uuid(Context
*context
, Partition
*p
, sd_id128_t
*ret
) {
5158 sd_id128_t type_uuid
;
5160 } _packed_ plaintext
= {};
5162 uint8_t md
[SHA256_DIGEST_SIZE
];
5173 /* Calculate a good UUID for the indicated partition. We want a certain degree of reproducibility,
5174 * hence we won't generate the UUIDs randomly. Instead we use a cryptographic hash (precisely:
5175 * HMAC-SHA256) to derive them from a single seed. The seed is generally the machine ID of the
5176 * installation we are processing, but if random behaviour is desired can be random, too. We use the
5177 * seed value as key for the HMAC (since the machine ID is something we generally don't want to leak)
5178 * and the partition type as plaintext. The partition type is suffixed with a counter (only for the
5179 * second and later partition of the same type) if we have more than one partition of the same
5180 * time. Or in other words:
5183 * SEED := /etc/machine-id
5185 * If first partition instance of type TYPE_UUID:
5186 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID)
5188 * For all later partition instances of type TYPE_UUID with INSTANCE being the LE64 encoded instance number:
5189 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID || INSTANCE)
5192 LIST_FOREACH(partitions
, q
, context
->partitions
) {
5196 if (!sd_id128_equal(p
->type
.uuid
, q
->type
.uuid
))
5202 plaintext
.type_uuid
= p
->type
.uuid
;
5203 plaintext
.counter
= htole64(k
);
5205 hmac_sha256(context
->seed
.bytes
, sizeof(context
->seed
.bytes
),
5207 k
== 0 ? sizeof(sd_id128_t
) : sizeof(plaintext
),
5210 /* Take the first half, mark it as v4 UUID */
5211 assert_cc(sizeof(result
.md
) == sizeof(result
.id
) * 2);
5212 result
.id
= id128_make_v4_uuid(result
.id
);
5214 /* Ensure this partition UUID is actually unique, and there's no remaining partition from an earlier run? */
5215 LIST_FOREACH(partitions
, q
, context
->partitions
) {
5219 if (sd_id128_in_set(result
.id
, q
->current_uuid
, q
->new_uuid
)) {
5220 log_warning("Partition UUID calculated from seed for partition %" PRIu64
" already used, reverting to randomized UUID.", p
->partno
);
5222 r
= sd_id128_randomize(&result
.id
);
5224 return log_error_errno(r
, "Failed to generate randomized UUID: %m");
5234 static int partition_acquire_label(Context
*context
, Partition
*p
, char **ret
) {
5235 _cleanup_free_
char *label
= NULL
;
5243 prefix
= gpt_partition_type_uuid_to_string(p
->type
.uuid
);
5248 const char *ll
= label
?: prefix
;
5251 LIST_FOREACH(partitions
, q
, context
->partitions
) {
5255 if (streq_ptr(ll
, q
->current_label
) ||
5256 streq_ptr(ll
, q
->new_label
)) {
5265 label
= mfree(label
);
5266 if (asprintf(&label
, "%s-%u", prefix
, ++k
) < 0)
5271 label
= strdup(prefix
);
5276 *ret
= TAKE_PTR(label
);
5280 static int context_acquire_partition_uuids_and_labels(Context
*context
) {
5285 LIST_FOREACH(partitions
, p
, context
->partitions
) {
5288 /* Never touch foreign partitions */
5289 if (PARTITION_IS_FOREIGN(p
)) {
5290 p
->new_uuid
= p
->current_uuid
;
5292 if (p
->current_label
) {
5293 r
= free_and_strdup_warn(&p
->new_label
, strempty(p
->current_label
));
5301 if (!sd_id128_is_null(p
->current_uuid
))
5302 p
->new_uuid
= uuid
= p
->current_uuid
; /* Never change initialized UUIDs */
5303 else if (p
->new_uuid_is_set
)
5306 /* Not explicitly set by user! */
5307 r
= partition_acquire_uuid(context
, p
, &uuid
);
5311 /* The final verity hash/data UUIDs can only be determined after formatting the
5312 * verity hash partition. However, we still want to use the generated partition UUID
5313 * to derive other UUIDs to keep things unique and reproducible, so we always
5314 * generate a UUID if none is set, but we only use it as the actual partition UUID if
5315 * verity is not configured. */
5316 if (!IN_SET(p
->verity
, VERITY_DATA
, VERITY_HASH
)) {
5318 p
->new_uuid_is_set
= true;
5322 /* Calculate the UUID for the file system as HMAC-SHA256 of the string "file-system-uuid",
5323 * keyed off the partition UUID. */
5324 r
= derive_uuid(uuid
, "file-system-uuid", &p
->fs_uuid
);
5328 if (p
->encrypt
!= ENCRYPT_OFF
) {
5329 r
= derive_uuid(uuid
, "luks-uuid", &p
->luks_uuid
);
5334 /* Derive the verity salt and verity superblock UUID from the seed to keep them reproducible */
5335 if (p
->verity
== VERITY_HASH
) {
5336 derive_salt(context
->seed
, "verity-salt", p
->verity_salt
);
5338 r
= derive_uuid(context
->seed
, "verity-uuid", &p
->verity_uuid
);
5340 return log_error_errno(r
, "Failed to acquire verity uuid: %m");
5343 if (!isempty(p
->current_label
)) {
5344 /* never change initialized labels */
5345 r
= free_and_strdup_warn(&p
->new_label
, p
->current_label
);
5348 } else if (!p
->new_label
) {
5349 /* Not explicitly set by user! */
5351 r
= partition_acquire_label(context
, p
, &p
->new_label
);
5360 static int set_gpt_flags(struct fdisk_partition
*q
, uint64_t flags
) {
5361 _cleanup_free_
char *a
= NULL
;
5363 for (unsigned i
= 0; i
< sizeof(flags
) * 8; i
++) {
5364 uint64_t bit
= UINT64_C(1) << i
;
5365 char buf
[DECIMAL_STR_MAX(unsigned)+1];
5367 if (!FLAGS_SET(flags
, bit
))
5370 xsprintf(buf
, "%u", i
);
5371 if (!strextend_with_separator(&a
, ",", buf
))
5375 return fdisk_partition_set_attrs(q
, a
);
5378 static uint64_t partition_merge_flags(Partition
*p
) {
5385 if (p
->no_auto
>= 0) {
5386 if (gpt_partition_type_knows_no_auto(p
->type
))
5387 SET_FLAG(f
, SD_GPT_FLAG_NO_AUTO
, p
->no_auto
);
5389 char buffer
[SD_ID128_UUID_STRING_MAX
];
5390 log_warning("Configured NoAuto=%s for partition type '%s' that doesn't support it, ignoring.",
5392 gpt_partition_type_uuid_to_string_harder(p
->type
.uuid
, buffer
));
5396 if (p
->read_only
>= 0) {
5397 if (gpt_partition_type_knows_read_only(p
->type
))
5398 SET_FLAG(f
, SD_GPT_FLAG_READ_ONLY
, p
->read_only
);
5400 char buffer
[SD_ID128_UUID_STRING_MAX
];
5401 log_warning("Configured ReadOnly=%s for partition type '%s' that doesn't support it, ignoring.",
5402 yes_no(p
->read_only
),
5403 gpt_partition_type_uuid_to_string_harder(p
->type
.uuid
, buffer
));
5407 if (p
->growfs
>= 0) {
5408 if (gpt_partition_type_knows_growfs(p
->type
))
5409 SET_FLAG(f
, SD_GPT_FLAG_GROWFS
, p
->growfs
);
5411 char buffer
[SD_ID128_UUID_STRING_MAX
];
5412 log_warning("Configured GrowFileSystem=%s for partition type '%s' that doesn't support it, ignoring.",
5414 gpt_partition_type_uuid_to_string_harder(p
->type
.uuid
, buffer
));
5421 static int context_mangle_partitions(Context
*context
) {
5426 LIST_FOREACH(partitions
, p
, context
->partitions
) {
5430 if (partition_type_defer(&p
->type
))
5433 assert(p
->new_size
!= UINT64_MAX
);
5434 assert(p
->offset
!= UINT64_MAX
);
5435 assert(p
->partno
!= UINT64_MAX
);
5437 if (PARTITION_EXISTS(p
)) {
5438 bool changed
= false;
5440 assert(p
->current_partition
);
5442 if (p
->new_size
!= p
->current_size
) {
5443 assert(p
->new_size
>= p
->current_size
);
5444 assert(p
->new_size
% context
->sector_size
== 0);
5446 r
= fdisk_partition_size_explicit(p
->current_partition
, true);
5448 return log_error_errno(r
, "Failed to enable explicit sizing: %m");
5450 r
= fdisk_partition_set_size(p
->current_partition
, p
->new_size
/ context
->sector_size
);
5452 return log_error_errno(r
, "Failed to grow partition: %m");
5454 log_info("Growing existing partition %" PRIu64
".", p
->partno
);
5458 if (!sd_id128_equal(p
->new_uuid
, p
->current_uuid
)) {
5459 r
= fdisk_partition_set_uuid(p
->current_partition
, SD_ID128_TO_UUID_STRING(p
->new_uuid
));
5461 return log_error_errno(r
, "Failed to set partition UUID: %m");
5463 log_info("Initializing UUID of existing partition %" PRIu64
".", p
->partno
);
5467 if (!streq_ptr(p
->new_label
, p
->current_label
)) {
5468 r
= fdisk_partition_set_name(p
->current_partition
, strempty(p
->new_label
));
5470 return log_error_errno(r
, "Failed to set partition label: %m");
5472 log_info("Setting partition label of existing partition %" PRIu64
".", p
->partno
);
5477 assert(!PARTITION_IS_FOREIGN(p
)); /* never touch foreign partitions */
5479 r
= fdisk_set_partition(context
->fdisk_context
, p
->partno
, p
->current_partition
);
5481 return log_error_errno(r
, "Failed to update partition: %m");
5484 _cleanup_(fdisk_unref_partitionp
) struct fdisk_partition
*q
= NULL
;
5485 _cleanup_(fdisk_unref_parttypep
) struct fdisk_parttype
*t
= NULL
;
5487 assert(!p
->new_partition
);
5488 assert(p
->offset
% context
->sector_size
== 0);
5489 assert(p
->new_size
% context
->sector_size
== 0);
5490 assert(p
->new_label
);
5492 t
= fdisk_new_parttype();
5496 r
= fdisk_parttype_set_typestr(t
, SD_ID128_TO_UUID_STRING(p
->type
.uuid
));
5498 return log_error_errno(r
, "Failed to initialize partition type: %m");
5500 q
= fdisk_new_partition();
5504 r
= fdisk_partition_set_type(q
, t
);
5506 return log_error_errno(r
, "Failed to set partition type: %m");
5508 r
= fdisk_partition_size_explicit(q
, true);
5510 return log_error_errno(r
, "Failed to enable explicit sizing: %m");
5512 r
= fdisk_partition_set_start(q
, p
->offset
/ context
->sector_size
);
5514 return log_error_errno(r
, "Failed to position partition: %m");
5516 r
= fdisk_partition_set_size(q
, p
->new_size
/ context
->sector_size
);
5518 return log_error_errno(r
, "Failed to grow partition: %m");
5520 r
= fdisk_partition_set_partno(q
, p
->partno
);
5522 return log_error_errno(r
, "Failed to set partition number: %m");
5524 r
= fdisk_partition_set_uuid(q
, SD_ID128_TO_UUID_STRING(p
->new_uuid
));
5526 return log_error_errno(r
, "Failed to set partition UUID: %m");
5528 r
= fdisk_partition_set_name(q
, strempty(p
->new_label
));
5530 return log_error_errno(r
, "Failed to set partition label: %m");
5532 /* Merge the no auto + read only + growfs setting with the literal flags, and set them for the partition */
5533 r
= set_gpt_flags(q
, partition_merge_flags(p
));
5535 return log_error_errno(r
, "Failed to set GPT partition flags: %m");
5537 log_info("Adding new partition %" PRIu64
" to partition table.", p
->partno
);
5539 r
= fdisk_add_partition(context
->fdisk_context
, q
, NULL
);
5541 return log_error_errno(r
, "Failed to add partition: %m");
5543 assert(!p
->new_partition
);
5544 p
->new_partition
= TAKE_PTR(q
);
5551 static int split_name_printf(Partition
*p
, char **ret
) {
5554 const Specifier table
[] = {
5555 { 't', specifier_string
, GPT_PARTITION_TYPE_UUID_TO_STRING_HARDER(p
->type
.uuid
) },
5556 { 'T', specifier_id128
, &p
->type
.uuid
},
5557 { 'U', specifier_id128
, &p
->new_uuid
},
5558 { 'n', specifier_uint64
, &p
->partno
},
5560 COMMON_SYSTEM_SPECIFIERS
,
5564 return specifier_printf(p
->split_name_format
, NAME_MAX
, table
, arg_root
, p
, ret
);
5567 static int split_node(const char *node
, char **ret_base
, char **ret_ext
) {
5568 _cleanup_free_
char *base
= NULL
, *ext
= NULL
;
5576 r
= path_extract_filename(node
, &base
);
5577 if (r
== O_DIRECTORY
|| r
== -EADDRNOTAVAIL
)
5578 return log_error_errno(r
, "Device node %s cannot be a directory", node
);
5580 return log_error_errno(r
, "Failed to extract filename from %s: %m", node
);
5582 e
= endswith(base
, ".raw");
5591 *ret_base
= TAKE_PTR(base
);
5592 *ret_ext
= TAKE_PTR(ext
);
5597 static int split_name_resolve(Context
*context
) {
5598 _cleanup_free_
char *parent
= NULL
, *base
= NULL
, *ext
= NULL
;
5603 r
= path_extract_directory(context
->node
, &parent
);
5604 if (r
< 0 && r
!= -EDESTADDRREQ
)
5605 return log_error_errno(r
, "Failed to extract directory from %s: %m", context
->node
);
5607 r
= split_node(context
->node
, &base
, &ext
);
5611 LIST_FOREACH(partitions
, p
, context
->partitions
) {
5612 _cleanup_free_
char *resolved
= NULL
;
5617 if (!p
->split_name_format
)
5620 r
= split_name_printf(p
, &resolved
);
5622 return log_error_errno(r
, "Failed to resolve specifiers in %s: %m", p
->split_name_format
);
5625 p
->split_path
= strjoin(parent
, "/", base
, ".", resolved
, ext
);
5627 p
->split_path
= strjoin(base
, ".", resolved
, ext
);
5632 LIST_FOREACH(partitions
, p
, context
->partitions
) {
5636 LIST_FOREACH(partitions
, q
, context
->partitions
) {
5643 if (!streq(p
->split_path
, q
->split_path
))
5646 return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ
),
5647 "%s and %s have the same resolved split name \"%s\", refusing",
5648 p
->definition_path
, q
->definition_path
, p
->split_path
);
5655 static int context_split(Context
*context
) {
5663 /* We can't do resolution earlier because the partition UUIDs for verity partitions are only filled
5664 * in after they've been generated. */
5666 r
= split_name_resolve(context
);
5670 LIST_FOREACH(partitions
, p
, context
->partitions
) {
5671 _cleanup_close_
int fdt
= -EBADF
;
5679 if (partition_type_defer(&p
->type
))
5682 fdt
= open(p
->split_path
, O_WRONLY
|O_NOCTTY
|O_CLOEXEC
|O_NOFOLLOW
|O_CREAT
|O_EXCL
, 0666);
5684 return log_error_errno(fdt
, "Failed to open split partition file %s: %m", p
->split_path
);
5687 assert_se((fd
= fdisk_get_devfd(context
->fdisk_context
)) >= 0);
5689 if (lseek(fd
, p
->offset
, SEEK_SET
) < 0)
5690 return log_error_errno(errno
, "Failed to seek to partition offset: %m");
5692 r
= copy_bytes(fd
, fdt
, p
->new_size
, COPY_REFLINK
|COPY_HOLES
|COPY_TRUNCATE
);
5694 return log_error_errno(r
, "Failed to copy to split partition %s: %m", p
->split_path
);
5700 static int context_write_partition_table(Context
*context
) {
5701 _cleanup_(fdisk_unref_tablep
) struct fdisk_table
*original_table
= NULL
;
5706 if (!context
->from_scratch
&& !context_changed(context
)) {
5707 log_info("No changes.");
5712 log_notice("Refusing to repartition, please re-run with --dry-run=no.");
5716 log_info("Applying changes to %s.", context
->node
);
5718 if (context
->from_scratch
&& arg_empty
!= EMPTY_CREATE
) {
5719 /* Erase everything if we operate from scratch, except if the image was just created anyway, and thus is definitely empty. */
5720 r
= context_wipe_range(context
, 0, context
->total
);
5724 log_info("Wiped block device.");
5727 r
= context_discard_range(context
, 0, context
->total
);
5728 if (r
== -EOPNOTSUPP
)
5729 log_info("Storage does not support discard, not discarding entire block device data.");
5731 return log_error_errno(r
, "Failed to discard entire block device: %m");
5733 log_info("Discarded entire block device.");
5737 r
= fdisk_get_partitions(context
->fdisk_context
, &original_table
);
5739 return log_error_errno(r
, "Failed to acquire partition table: %m");
5741 /* Wipe fs signatures and discard sectors where the new partitions are going to be placed and in the
5742 * gaps between partitions, just to be sure. */
5743 r
= context_wipe_and_discard(context
);
5747 r
= context_copy_blocks(context
);
5751 r
= context_mkfs(context
);
5755 r
= context_mangle_partitions(context
);
5759 log_info("Writing new partition table.");
5761 r
= fdisk_write_disklabel(context
->fdisk_context
);
5763 return log_error_errno(r
, "Failed to write partition table: %m");
5765 capable
= blockdev_partscan_enabled(fdisk_get_devfd(context
->fdisk_context
));
5766 if (capable
== -ENOTBLK
)
5767 log_debug("Not telling kernel to reread partition table, since we are not operating on a block device.");
5768 else if (capable
< 0)
5769 return log_error_errno(capable
, "Failed to check if block device supports partition scanning: %m");
5770 else if (capable
> 0) {
5771 log_info("Telling kernel to reread partition table.");
5773 if (context
->from_scratch
)
5774 r
= fdisk_reread_partition_table(context
->fdisk_context
);
5776 r
= fdisk_reread_changes(context
->fdisk_context
, original_table
);
5778 return log_error_errno(r
, "Failed to reread partition table: %m");
5780 log_notice("Not telling kernel to reread partition table, because selected image does not support kernel partition block devices.");
5782 log_info("All done.");
5787 static int context_read_seed(Context
*context
, const char *root
) {
5792 if (!sd_id128_is_null(context
->seed
))
5795 if (!arg_randomize
) {
5796 r
= id128_get_machine(root
, &context
->seed
);
5800 if (!ERRNO_IS_MACHINE_ID_UNSET(r
))
5801 return log_error_errno(r
, "Failed to parse machine ID of image: %m");
5803 log_info("No machine ID set, using randomized partition UUIDs.");
5806 r
= sd_id128_randomize(&context
->seed
);
5808 return log_error_errno(r
, "Failed to generate randomized seed: %m");
5813 static int context_factory_reset(Context
*context
) {
5819 if (arg_factory_reset
<= 0)
5822 if (context
->from_scratch
) /* Nothing to reset if we start from scratch */
5826 log_notice("Refusing to factory reset, please re-run with --dry-run=no.");
5830 log_info("Applying factory reset.");
5832 LIST_FOREACH(partitions
, p
, context
->partitions
) {
5834 if (!p
->factory_reset
|| !PARTITION_EXISTS(p
))
5837 assert(p
->partno
!= UINT64_MAX
);
5839 log_info("Removing partition %" PRIu64
" for factory reset.", p
->partno
);
5841 r
= fdisk_delete_partition(context
->fdisk_context
, p
->partno
);
5843 return log_error_errno(r
, "Failed to remove partition %" PRIu64
": %m", p
->partno
);
5849 log_info("Factory reset requested, but no partitions to delete found.");
5853 r
= fdisk_write_disklabel(context
->fdisk_context
);
5855 return log_error_errno(r
, "Failed to write disk label: %m");
5857 log_info("Successfully deleted %zu partitions.", n
);
5861 static int context_can_factory_reset(Context
*context
) {
5864 LIST_FOREACH(partitions
, p
, context
->partitions
)
5865 if (p
->factory_reset
&& PARTITION_EXISTS(p
))
5871 static int resolve_copy_blocks_auto_candidate(
5872 dev_t partition_devno
,
5873 GptPartitionType partition_type
,
5874 dev_t restrict_devno
,
5875 sd_id128_t
*ret_uuid
) {
5877 _cleanup_(blkid_free_probep
) blkid_probe b
= NULL
;
5878 _cleanup_close_
int fd
= -EBADF
;
5879 _cleanup_free_
char *p
= NULL
;
5880 const char *pttype
, *t
;
5881 sd_id128_t pt_parsed
, u
;
5887 /* Checks if the specified partition has the specified GPT type UUID, and is located on the specified
5888 * 'restrict_devno' device. The type check is particularly relevant if we have Verity volume which is
5889 * backed by two separate partitions: the data and the hash partitions, and we need to find the right
5890 * one of the two. */
5892 r
= block_get_whole_disk(partition_devno
, &whole_devno
);
5894 return log_error_errno(
5896 "Unable to determine containing block device of partition %u:%u: %m",
5897 major(partition_devno
), minor(partition_devno
));
5899 if (restrict_devno
!= (dev_t
) -1 &&
5900 restrict_devno
!= whole_devno
)
5901 return log_error_errno(
5902 SYNTHETIC_ERRNO(EPERM
),
5903 "Partition %u:%u is located outside of block device %u:%u, refusing.",
5904 major(partition_devno
), minor(partition_devno
),
5905 major(restrict_devno
), minor(restrict_devno
));
5907 fd
= r
= device_open_from_devnum(S_IFBLK
, whole_devno
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
, &p
);
5909 return log_error_errno(r
, "Failed to open block device " DEVNUM_FORMAT_STR
": %m",
5910 DEVNUM_FORMAT_VAL(whole_devno
));
5912 b
= blkid_new_probe();
5917 r
= blkid_probe_set_device(b
, fd
, 0, 0);
5919 return log_error_errno(errno_or_else(ENOMEM
), "Failed to open block device '%s': %m", p
);
5921 (void) blkid_probe_enable_partitions(b
, 1);
5922 (void) blkid_probe_set_partitions_flags(b
, BLKID_PARTS_ENTRY_DETAILS
);
5925 r
= blkid_do_safeprobe(b
);
5926 if (r
== _BLKID_SAFEPROBE_ERROR
)
5927 return log_error_errno(errno_or_else(EIO
), "Unable to probe for partition table of '%s': %m", p
);
5928 if (IN_SET(r
, _BLKID_SAFEPROBE_AMBIGUOUS
, _BLKID_SAFEPROBE_NOT_FOUND
)) {
5929 log_debug("Didn't find partition table on block device '%s'.", p
);
5933 assert(r
== _BLKID_SAFEPROBE_FOUND
);
5935 (void) blkid_probe_lookup_value(b
, "PTTYPE", &pttype
, NULL
);
5936 if (!streq_ptr(pttype
, "gpt")) {
5937 log_debug("Didn't find a GPT partition table on '%s'.", p
);
5942 pl
= blkid_probe_get_partitions(b
);
5944 return log_error_errno(errno_or_else(EIO
), "Unable read partition table of '%s': %m", p
);
5946 pp
= blkid_partlist_devno_to_partition(pl
, partition_devno
);
5948 log_debug("Partition %u:%u has no matching partition table entry on '%s'.",
5949 major(partition_devno
), minor(partition_devno
), p
);
5953 t
= blkid_partition_get_type_string(pp
);
5955 log_debug("Partition %u:%u has no type on '%s'.",
5956 major(partition_devno
), minor(partition_devno
), p
);
5960 r
= sd_id128_from_string(t
, &pt_parsed
);
5962 log_debug_errno(r
, "Failed to parse partition type \"%s\": %m", t
);
5966 if (!sd_id128_equal(pt_parsed
, partition_type
.uuid
)) {
5967 log_debug("Partition %u:%u has non-matching partition type " SD_ID128_FORMAT_STR
" (needed: " SD_ID128_FORMAT_STR
"), ignoring.",
5968 major(partition_devno
), minor(partition_devno
),
5969 SD_ID128_FORMAT_VAL(pt_parsed
), SD_ID128_FORMAT_VAL(partition_type
.uuid
));
5973 r
= blkid_partition_get_uuid_id128(pp
, &u
);
5975 log_debug_errno(r
, "Partition " DEVNUM_FORMAT_STR
" has no UUID.", DEVNUM_FORMAT_VAL(partition_devno
));
5979 log_debug_errno(r
, "Failed to read partition UUID of " DEVNUM_FORMAT_STR
": %m", DEVNUM_FORMAT_VAL(partition_devno
));
5983 log_debug("Automatically found partition " DEVNUM_FORMAT_STR
" of right type " SD_ID128_FORMAT_STR
".",
5984 DEVNUM_FORMAT_VAL(partition_devno
),
5985 SD_ID128_FORMAT_VAL(pt_parsed
));
5993 static int find_backing_devno(
5998 _cleanup_free_
char *resolved
= NULL
;
6003 r
= chase(path
, root
, CHASE_PREFIX_ROOT
, &resolved
, NULL
);
6007 r
= path_is_mount_point(resolved
);
6010 if (r
== 0) /* Not a mount point, then it's not a partition of its own, let's not automatically use it. */
6013 r
= get_block_device(resolved
, ret
);
6016 if (r
== 0) /* Not backed by physical file system, we can't use this */
6022 static int resolve_copy_blocks_auto(
6023 GptPartitionType type
,
6025 dev_t restrict_devno
,
6027 sd_id128_t
*ret_uuid
) {
6029 const char *try1
= NULL
, *try2
= NULL
;
6030 char p
[SYS_BLOCK_PATH_MAX("/slaves")];
6031 _cleanup_closedir_
DIR *d
= NULL
;
6032 sd_id128_t found_uuid
= SD_ID128_NULL
;
6033 dev_t devno
, found
= 0;
6036 /* Enforce some security restrictions: CopyBlocks=auto should not be an avenue to get outside of the
6037 * --root=/--image= confinement. Specifically, refuse CopyBlocks= in combination with --root= at all,
6038 * and restrict block device references in the --image= case to loopback block device we set up.
6040 * restrict_devno contain the dev_t of the loop back device we operate on in case of --image=, and
6041 * thus declares which device (and its partition subdevices) we shall limit access to. If
6042 * restrict_devno is zero no device probing access shall be allowed at all (used for --root=) and if
6043 * it is (dev_t) -1 then free access shall be allowed (if neither switch is used). */
6045 if (restrict_devno
== 0)
6046 return log_error_errno(SYNTHETIC_ERRNO(EPERM
),
6047 "Automatic discovery of backing block devices not permitted in --root= mode, refusing.");
6049 /* Handles CopyBlocks=auto, and finds the right source partition to copy from. We look for matching
6050 * partitions in the host, using the appropriate directory as key and ensuring that the partition
6053 if (type
.designator
== PARTITION_ROOT
)
6055 else if (type
.designator
== PARTITION_USR
)
6057 else if (type
.designator
== PARTITION_ROOT_VERITY
)
6059 else if (type
.designator
== PARTITION_USR_VERITY
)
6061 else if (type
.designator
== PARTITION_ESP
) {
6064 } else if (type
.designator
== PARTITION_XBOOTLDR
)
6067 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
6068 "Partition type " SD_ID128_FORMAT_STR
" not supported from automatic source block device discovery.",
6069 SD_ID128_FORMAT_VAL(type
.uuid
));
6071 r
= find_backing_devno(try1
, root
, &devno
);
6072 if (r
== -ENOENT
&& try2
)
6073 r
= find_backing_devno(try2
, root
, &devno
);
6075 return log_error_errno(r
, "Failed to resolve automatic CopyBlocks= path for partition type " SD_ID128_FORMAT_STR
", sorry: %m",
6076 SD_ID128_FORMAT_VAL(type
.uuid
));
6078 xsprintf_sys_block_path(p
, "/slaves", devno
);
6084 _cleanup_free_
char *q
= NULL
, *t
= NULL
;
6089 de
= readdir_no_dot(d
);
6092 return log_error_errno(errno
, "Failed to read directory '%s': %m", p
);
6097 if (!IN_SET(de
->d_type
, DT_LNK
, DT_UNKNOWN
))
6100 q
= path_join(p
, de
->d_name
, "/dev");
6104 r
= read_one_line_file(q
, &t
);
6106 return log_error_errno(r
, "Failed to read %s: %m", q
);
6108 r
= parse_devnum(t
, &sl
);
6110 log_debug_errno(r
, "Failed to parse %s, ignoring: %m", q
);
6113 if (major(sl
) == 0) {
6114 log_debug("Device backing %s is special, ignoring.", q
);
6118 r
= resolve_copy_blocks_auto_candidate(sl
, type
, restrict_devno
, &u
);
6122 /* We found a matching one! */
6124 return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ
),
6125 "Multiple matching partitions found, refusing.");
6131 } else if (errno
!= ENOENT
)
6132 return log_error_errno(errno
, "Failed open %s: %m", p
);
6134 r
= resolve_copy_blocks_auto_candidate(devno
, type
, restrict_devno
, &found_uuid
);
6142 return log_error_errno(SYNTHETIC_ERRNO(ENXIO
),
6143 "Unable to automatically discover suitable partition to copy blocks from.");
6149 *ret_uuid
= found_uuid
;
6154 static int context_open_copy_block_paths(
6156 dev_t restrict_devno
) {
6162 LIST_FOREACH(partitions
, p
, context
->partitions
) {
6163 _cleanup_close_
int source_fd
= -EBADF
;
6164 _cleanup_free_
char *opened
= NULL
;
6165 sd_id128_t uuid
= SD_ID128_NULL
;
6169 if (p
->copy_blocks_fd
>= 0)
6172 assert(p
->copy_blocks_size
== UINT64_MAX
);
6174 if (PARTITION_EXISTS(p
)) /* Never copy over partitions that already exist! */
6177 if (p
->copy_blocks_path
) {
6179 source_fd
= chase_and_open(p
->copy_blocks_path
, p
->copy_blocks_root
, CHASE_PREFIX_ROOT
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
, &opened
);
6181 return log_error_errno(source_fd
, "Failed to open '%s': %m", p
->copy_blocks_path
);
6183 if (fstat(source_fd
, &st
) < 0)
6184 return log_error_errno(errno
, "Failed to stat block copy file '%s': %m", opened
);
6186 if (!S_ISREG(st
.st_mode
) && restrict_devno
!= (dev_t
) -1)
6187 return log_error_errno(SYNTHETIC_ERRNO(EPERM
),
6188 "Copying from block device node is not permitted in --image=/--root= mode, refusing.");
6190 } else if (p
->copy_blocks_auto
) {
6191 dev_t devno
= 0; /* Fake initialization to appease gcc. */
6193 r
= resolve_copy_blocks_auto(p
->type
, p
->copy_blocks_root
, restrict_devno
, &devno
, &uuid
);
6198 source_fd
= r
= device_open_from_devnum(S_IFBLK
, devno
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
, &opened
);
6200 return log_error_errno(r
, "Failed to open automatically determined source block copy device " DEVNUM_FORMAT_STR
": %m",
6201 DEVNUM_FORMAT_VAL(devno
));
6203 if (fstat(source_fd
, &st
) < 0)
6204 return log_error_errno(errno
, "Failed to stat block copy file '%s': %m", opened
);
6208 if (S_ISDIR(st
.st_mode
)) {
6209 _cleanup_free_
char *bdev
= NULL
;
6212 /* If the file is a directory, automatically find the backing block device */
6214 if (major(st
.st_dev
) != 0)
6217 /* Special support for btrfs */
6218 r
= btrfs_get_block_device_fd(source_fd
, &devt
);
6220 return btrfs_log_dev_root(LOG_ERR
, r
, opened
);
6222 return log_error_errno(r
, "Unable to determine backing block device of '%s': %m", opened
);
6225 safe_close(source_fd
);
6227 source_fd
= r
= device_open_from_devnum(S_IFBLK
, devt
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
, &bdev
);
6229 return log_error_errno(r
, "Failed to open block device backing '%s': %m", opened
);
6231 if (fstat(source_fd
, &st
) < 0)
6232 return log_error_errno(errno
, "Failed to stat block device '%s': %m", bdev
);
6235 if (S_ISREG(st
.st_mode
))
6237 else if (S_ISBLK(st
.st_mode
)) {
6238 r
= blockdev_get_device_size(source_fd
, &size
);
6240 return log_error_errno(r
, "Failed to determine size of block device to copy from: %m");
6242 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Specified path to copy blocks from '%s' is not a regular file, block device or directory, refusing: %m", opened
);
6245 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "File to copy bytes from '%s' has zero size, refusing.", opened
);
6246 if (size
% 512 != 0)
6247 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "File to copy bytes from '%s' has size that is not multiple of 512, refusing.", opened
);
6249 p
->copy_blocks_fd
= TAKE_FD(source_fd
);
6250 p
->copy_blocks_size
= size
;
6252 free_and_replace(p
->copy_blocks_path
, opened
);
6254 /* When copying from an existing partition copy that partitions UUID if none is configured explicitly */
6255 if (!p
->new_uuid_is_set
&& !sd_id128_is_null(uuid
)) {
6257 p
->new_uuid_is_set
= true;
6264 static int fd_apparent_size(int fd
, uint64_t *ret
) {
6271 initial
= lseek(fd
, 0, SEEK_CUR
);
6273 return log_error_errno(errno
, "Failed to get file offset: %m");
6275 for (off_t off
= 0;;) {
6278 r
= lseek(fd
, off
, SEEK_DATA
);
6279 if (r
< 0 && errno
== ENXIO
)
6280 /* If errno == ENXIO, that means we've reached the final hole of the file and
6281 * that hole isn't followed by more data. */
6284 return log_error_errno(errno
, "Failed to seek data in file from offset %"PRIi64
": %m", off
);
6286 off
= r
; /* Set the offset to the start of the data segment. */
6288 /* After copying a potential hole, find the end of the data segment by looking for
6289 * the next hole. If we get ENXIO, we're at EOF. */
6290 r
= lseek(fd
, off
, SEEK_HOLE
);
6294 return log_error_errno(errno
, "Failed to seek hole in file from offset %"PRIi64
": %m", off
);
6301 if (lseek(fd
, initial
, SEEK_SET
) < 0)
6302 return log_error_errno(errno
, "Failed to reset file offset: %m");
6309 static bool need_fstab_one(const Partition
*p
) {
6318 if (p
->n_mountpoints
== 0)
6324 static bool need_fstab(const Context
*context
) {
6327 LIST_FOREACH(partitions
, p
, context
->partitions
)
6328 if (need_fstab_one(p
))
6334 static int context_fstab(Context
*context
) {
6335 _cleanup_(unlink_and_freep
) char *t
= NULL
;
6336 _cleanup_fclose_
FILE *f
= NULL
;
6337 _cleanup_free_
char *path
= NULL
;
6342 if (!arg_generate_fstab
)
6345 if (!need_fstab(context
)) {
6346 log_notice("MountPoint= is not specified for any eligible partitions, not generating %s",
6347 arg_generate_fstab
);
6351 path
= path_join(arg_copy_source
, arg_generate_fstab
);
6355 r
= fopen_tmpfile_linkable(path
, O_WRONLY
|O_CLOEXEC
, &t
, &f
);
6357 return log_error_errno(r
, "Failed to open temporary file for %s: %m", path
);
6359 fprintf(f
, "# Automatically generated by systemd-repart\n\n");
6361 LIST_FOREACH(partitions
, p
, context
->partitions
) {
6362 _cleanup_free_
char *what
= NULL
, *options
= NULL
;
6364 if (!need_fstab_one(p
))
6367 what
= strjoin("UUID=", SD_ID128_TO_UUID_STRING(p
->fs_uuid
));
6371 FOREACH_ARRAY(mountpoint
, p
->mountpoints
, p
->n_mountpoints
) {
6372 r
= partition_pick_mount_options(
6376 /* discard= */ !IN_SET(p
->type
.designator
, PARTITION_ESP
, PARTITION_XBOOTLDR
),
6382 if (!strextend_with_separator(&options
, ",", mountpoint
->options
))
6385 fprintf(f
, "%s %s %s %s 0 %i\n",
6390 p
->type
.designator
== PARTITION_ROOT
? 1 : 2);
6394 r
= flink_tmpfile(f
, t
, path
, 0);
6396 return log_error_errno(r
, "Failed to link temporary file to %s: %m", path
);
6398 log_info("%s written.", path
);
6403 static bool need_crypttab_one(const Partition
*p
) {
6409 if (p
->encrypt
== ENCRYPT_OFF
)
6412 if (!p
->encrypted_volume
)
6418 static bool need_crypttab(Context
*context
) {
6421 LIST_FOREACH(partitions
, p
, context
->partitions
)
6422 if (need_crypttab_one(p
))
6428 static int context_crypttab(Context
*context
) {
6429 _cleanup_(unlink_and_freep
) char *t
= NULL
;
6430 _cleanup_fclose_
FILE *f
= NULL
;
6431 _cleanup_free_
char *path
= NULL
;
6436 if (!arg_generate_crypttab
)
6439 if (!need_crypttab(context
)) {
6440 log_notice("EncryptedVolume= is not specified for any eligible partitions, not generating %s",
6441 arg_generate_crypttab
);
6445 path
= path_join(arg_copy_source
, arg_generate_crypttab
);
6449 r
= fopen_tmpfile_linkable(path
, O_WRONLY
|O_CLOEXEC
, &t
, &f
);
6451 return log_error_errno(r
, "Failed to open temporary file for %s: %m", path
);
6453 fprintf(f
, "# Automatically generated by systemd-repart\n\n");
6455 LIST_FOREACH(partitions
, p
, context
->partitions
) {
6456 _cleanup_free_
char *volume
= NULL
;
6458 if (!need_crypttab_one(p
))
6461 if (!p
->encrypted_volume
->name
&& asprintf(&volume
, "luks-%s", SD_ID128_TO_UUID_STRING(p
->luks_uuid
)) < 0)
6464 fprintf(f
, "%s UUID=%s %s %s\n",
6465 p
->encrypted_volume
->name
?: volume
,
6466 SD_ID128_TO_UUID_STRING(p
->luks_uuid
),
6467 isempty(p
->encrypted_volume
->keyfile
) ? "-" : p
->encrypted_volume
->keyfile
,
6468 strempty(p
->encrypted_volume
->options
));
6471 r
= flink_tmpfile(f
, t
, path
, 0);
6473 return log_error_errno(r
, "Failed to link temporary file to %s: %m", path
);
6475 log_info("%s written.", path
);
6480 static int context_minimize(Context
*context
) {
6481 const char *vt
= NULL
;
6486 LIST_FOREACH(partitions
, p
, context
->partitions
) {
6487 _cleanup_(rm_rf_physical_and_freep
) char *root
= NULL
;
6488 _cleanup_(unlink_and_freep
) char *temp
= NULL
;
6489 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
6490 _cleanup_strv_free_
char **extra_mkfs_options
= NULL
;
6491 _cleanup_close_
int fd
= -EBADF
;
6492 _cleanup_free_
char *hint
= NULL
;
6500 if (PARTITION_EXISTS(p
)) /* Never format existing partitions */
6506 if (p
->copy_blocks_fd
>= 0)
6509 if (p
->minimize
== MINIMIZE_OFF
)
6512 if (!partition_needs_populate(p
))
6515 assert(!p
->copy_blocks_path
);
6517 (void) partition_hint(p
, context
->node
, &hint
);
6519 log_info("Pre-populating %s filesystem of partition %s twice to calculate minimal partition size",
6520 p
->format
, strna(hint
));
6523 r
= var_tmp_dir(&vt
);
6525 return log_error_errno(r
, "Could not determine temporary directory: %m");
6528 r
= tempfn_random_child(vt
, "repart", &temp
);
6530 return log_error_errno(r
, "Failed to generate temporary file path: %m");
6532 if (fstype_is_ro(p
->format
))
6533 fs_uuid
= p
->fs_uuid
;
6535 fd
= open(temp
, O_CREAT
|O_EXCL
|O_CLOEXEC
|O_RDWR
|O_NOCTTY
, 0600);
6537 return log_error_errno(errno
, "Failed to open temporary file %s: %m", temp
);
6539 /* This may seem huge but it will be created sparse so it doesn't take up any space
6540 * on disk until written to. */
6541 if (ftruncate(fd
, 1024ULL * 1024ULL * 1024ULL * 1024ULL) < 0)
6542 return log_error_errno(errno
, "Failed to truncate temporary file to %s: %m",
6543 FORMAT_BYTES(1024ULL * 1024ULL * 1024ULL * 1024ULL));
6545 if (arg_offline
<= 0) {
6546 r
= loop_device_make(fd
, O_RDWR
, 0, UINT64_MAX
, context
->sector_size
, 0, LOCK_EX
, &d
);
6547 if (r
< 0 && (arg_offline
== 0 || (r
!= -ENOENT
&& !ERRNO_IS_PRIVILEGE(r
)) || !strv_isempty(p
->subvolumes
)))
6548 return log_error_errno(r
, "Failed to make loopback device of %s: %m", temp
);
6551 /* We're going to populate this filesystem twice so use a random UUID the first time
6552 * to avoid UUID conflicts. */
6553 r
= sd_id128_randomize(&fs_uuid
);
6558 if (!d
|| fstype_is_ro(p
->format
)) {
6559 if (!mkfs_supports_root_option(p
->format
))
6560 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
),
6561 "Loop device access is required to populate %s filesystems",
6564 r
= partition_populate_directory(context
, p
, &root
);
6569 r
= mkfs_options_from_env("REPART", p
->format
, &extra_mkfs_options
);
6571 return log_error_errno(r
,
6572 "Failed to determine mkfs command line options for '%s': %m",
6575 r
= make_filesystem(d
? d
->node
: temp
,
6577 strempty(p
->new_label
),
6580 arg_discard
, /* quiet = */ false,
6581 context
->fs_sector_size
,
6582 extra_mkfs_options
);
6586 /* Read-only filesystems are minimal from the first try because they create and size the
6587 * loopback file for us. */
6588 if (fstype_is_ro(p
->format
)) {
6591 fd
= open(temp
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
6593 return log_error_errno(errno
, "Failed to open temporary file %s: %m", temp
);
6595 if (fstat(fd
, &st
) < 0)
6596 return log_error_errno(errno
, "Failed to stat temporary file: %m");
6598 log_info("Minimal partition size of %s filesystem of partition %s is %s",
6599 p
->format
, strna(hint
), FORMAT_BYTES(st
.st_size
));
6601 p
->copy_blocks_path
= TAKE_PTR(temp
);
6602 p
->copy_blocks_path_is_our_file
= true;
6603 p
->copy_blocks_fd
= TAKE_FD(fd
);
6604 p
->copy_blocks_size
= st
.st_size
;
6611 r
= partition_populate_filesystem(context
, p
, d
->node
);
6616 /* Other filesystems need to be provided with a pre-sized loopback file and will adapt to
6617 * fully occupy it. Because we gave the filesystem a 1T sparse file, we need to shrink the
6618 * filesystem down to a reasonable size again to fit it in the disk image. While there are
6619 * some filesystems that support shrinking, it doesn't always work properly (e.g. shrinking
6620 * btrfs gives us a 2.0G filesystem regardless of what we put in it). Instead, let's populate
6621 * the filesystem again, but this time, instead of providing the filesystem with a 1T sparse
6622 * loopback file, let's size the loopback file based on the actual data used by the
6623 * filesystem in the sparse file after the first attempt. This should be a good guess of the
6624 * minimal amount of space needed in the filesystem to fit all the required data.
6626 r
= fd_apparent_size(fd
, &fsz
);
6630 /* Massage the size a bit because just going by actual data used in the sparse file isn't
6632 uint64_t heuristic
= streq(p
->format
, "xfs") ? fsz
: fsz
/ 2;
6633 fsz
= round_up_size(fsz
+ heuristic
, context
->grain_size
);
6634 if (minimal_size_by_fs_name(p
->format
) != UINT64_MAX
)
6635 fsz
= MAX(minimal_size_by_fs_name(p
->format
), fsz
);
6637 log_info("Minimal partition size of %s filesystem of partition %s is %s",
6638 p
->format
, strna(hint
), FORMAT_BYTES(fsz
));
6640 d
= loop_device_unref(d
);
6642 /* Erase the previous filesystem first. */
6643 if (ftruncate(fd
, 0) < 0)
6644 return log_error_errno(errno
, "Failed to erase temporary file: %m");
6646 if (ftruncate(fd
, fsz
) < 0)
6647 return log_error_errno(errno
, "Failed to truncate temporary file to %s: %m", FORMAT_BYTES(fsz
));
6649 if (arg_offline
<= 0) {
6650 r
= loop_device_make(fd
, O_RDWR
, 0, UINT64_MAX
, context
->sector_size
, 0, LOCK_EX
, &d
);
6651 if (r
< 0 && (arg_offline
== 0 || (r
!= -ENOENT
&& !ERRNO_IS_PRIVILEGE(r
)) || !strv_isempty(p
->subvolumes
)))
6652 return log_error_errno(r
, "Failed to make loopback device of %s: %m", temp
);
6655 r
= make_filesystem(d
? d
->node
: temp
,
6657 strempty(p
->new_label
),
6661 /* quiet = */ false,
6662 context
->fs_sector_size
,
6663 extra_mkfs_options
);
6670 r
= partition_populate_filesystem(context
, p
, d
->node
);
6675 if (fstat(fd
, &st
) < 0)
6676 return log_error_errno(errno
, "Failed to stat temporary file: %m");
6678 p
->copy_blocks_path
= TAKE_PTR(temp
);
6679 p
->copy_blocks_path_is_our_file
= true;
6680 p
->copy_blocks_fd
= TAKE_FD(fd
);
6681 p
->copy_blocks_size
= st
.st_size
;
6684 /* Now that we've done the data partitions, do the verity hash partitions. We do these in a separate
6685 * step because they might depend on data generated in the previous step. */
6687 LIST_FOREACH(partitions
, p
, context
->partitions
) {
6688 _cleanup_(unlink_and_freep
) char *temp
= NULL
;
6689 _cleanup_free_
char *hint
= NULL
;
6690 _cleanup_close_
int fd
= -EBADF
;
6697 if (PARTITION_EXISTS(p
)) /* Never format existing partitions */
6700 if (p
->minimize
== MINIMIZE_OFF
)
6703 if (p
->verity
!= VERITY_HASH
)
6706 assert_se(dp
= p
->siblings
[VERITY_DATA
]);
6707 assert(!dp
->dropped
);
6708 assert(dp
->copy_blocks_path
);
6710 (void) partition_hint(p
, context
->node
, &hint
);
6712 log_info("Pre-populating verity hash data of partition %s to calculate minimal partition size",
6716 r
= var_tmp_dir(&vt
);
6718 return log_error_errno(r
, "Could not determine temporary directory: %m");
6721 r
= tempfn_random_child(vt
, "repart", &temp
);
6723 return log_error_errno(r
, "Failed to generate temporary file path: %m");
6727 return log_error_errno(r
, "Failed to create temporary file: %m");
6729 r
= partition_format_verity_hash(context
, p
, temp
, dp
->copy_blocks_path
);
6733 fd
= open(temp
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
);
6735 return log_error_errno(errno
, "Failed to open temporary file %s: %m", temp
);
6737 if (fstat(fd
, &st
) < 0)
6738 return log_error_errno(errno
, "Failed to stat temporary file: %m");
6740 log_info("Minimal partition size of verity hash partition %s is %s",
6741 strna(hint
), FORMAT_BYTES(st
.st_size
));
6743 p
->copy_blocks_path
= TAKE_PTR(temp
);
6744 p
->copy_blocks_path_is_our_file
= true;
6745 p
->copy_blocks_fd
= TAKE_FD(fd
);
6746 p
->copy_blocks_size
= st
.st_size
;
6752 static int parse_partition_types(const char *p
, GptPartitionType
**partitions
, size_t *n_partitions
) {
6756 assert(n_partitions
);
6759 _cleanup_free_
char *name
= NULL
;
6760 GptPartitionType type
;
6762 r
= extract_first_word(&p
, &name
, ",", EXTRACT_CUNESCAPE
|EXTRACT_DONT_COALESCE_SEPARATORS
);
6766 return log_error_errno(r
, "Failed to extract partition type identifier or GUID: %s", p
);
6768 r
= gpt_partition_type_from_string(name
, &type
);
6770 return log_error_errno(r
, "'%s' is not a valid partition type identifier or GUID", name
);
6772 if (!GREEDY_REALLOC(*partitions
, *n_partitions
+ 1))
6775 (*partitions
)[(*n_partitions
)++] = type
;
6781 static int help(void) {
6782 _cleanup_free_
char *link
= NULL
;
6785 r
= terminal_urlify_man("systemd-repart", "8", &link
);
6789 printf("%s [OPTIONS...] [DEVICE]\n"
6790 "\n%sGrow and add partitions to partition table.%s\n\n"
6791 " -h --help Show this help\n"
6792 " --version Show package version\n"
6793 " --no-pager Do not pipe output into a pager\n"
6794 " --no-legend Do not show the headers and footers\n"
6795 " --dry-run=BOOL Whether to run dry-run operation\n"
6796 " --empty=MODE One of refuse, allow, require, force, create; controls\n"
6797 " how to handle empty disks lacking partition tables\n"
6798 " --discard=BOOL Whether to discard backing blocks for new partitions\n"
6799 " --pretty=BOOL Whether to show pretty summary before doing changes\n"
6800 " --factory-reset=BOOL Whether to remove data partitions before recreating\n"
6802 " --can-factory-reset Test whether factory reset is defined\n"
6803 " --root=PATH Operate relative to root path\n"
6804 " --image=PATH Operate relative to image file\n"
6805 " --image-policy=POLICY\n"
6806 " Specify disk image dissection policy\n"
6807 " --definitions=DIR Find partition definitions in specified directory\n"
6808 " --key-file=PATH Key to use when encrypting partitions\n"
6809 " --private-key=PATH Private key to use when generating verity roothash\n"
6811 " --certificate=PATH PEM certificate to use when generating verity\n"
6812 " roothash signatures\n"
6813 " --tpm2-device=PATH Path to TPM2 device node to use\n"
6814 " --tpm2-device-key=PATH\n"
6815 " Enroll a TPM2 device using its public key\n"
6816 " --tpm2-seal-key-handle=HANDLE\n"
6817 " Specify handle of key to use for sealing\n"
6818 " --tpm2-pcrs=PCR1+PCR2+PCR3+…\n"
6819 " TPM2 PCR indexes to use for TPM2 enrollment\n"
6820 " --tpm2-public-key=PATH\n"
6821 " Enroll signed TPM2 PCR policy against PEM public key\n"
6822 " --tpm2-public-key-pcrs=PCR1+PCR2+PCR3+…\n"
6823 " Enroll signed TPM2 PCR policy for specified TPM2 PCRs\n"
6824 " --tpm2-pcrlock=PATH\n"
6825 " Specify pcrlock policy to lock against\n"
6826 " --seed=UUID 128-bit seed UUID to derive all UUIDs from\n"
6827 " --size=BYTES Grow loopback file to specified size\n"
6828 " --json=pretty|short|off\n"
6829 " Generate JSON output\n"
6830 " --split=BOOL Whether to generate split artifacts\n"
6831 " --include-partitions=PARTITION1,PARTITION2,PARTITION3,…\n"
6832 " Ignore partitions not of the specified types\n"
6833 " --exclude-partitions=PARTITION1,PARTITION2,PARTITION3,…\n"
6834 " Ignore partitions of the specified types\n"
6835 " --defer-partitions=PARTITION1,PARTITION2,PARTITION3,…\n"
6836 " Take partitions of the specified types into account\n"
6837 " but don't populate them yet\n"
6838 " --sector-size=SIZE Set the logical sector size for the image\n"
6839 " --architecture=ARCH Set the generic architecture for the image\n"
6840 " --offline=BOOL Whether to build the image offline\n"
6841 " -s --copy-source=PATH Specify the primary source tree to copy files from\n"
6842 " --copy-from=IMAGE Copy partitions from the given image(s)\n"
6843 " -S --make-ddi=sysext Make a system extension DDI\n"
6844 " -C --make-ddi=confext Make a configuration extension DDI\n"
6845 " -P --make-ddi=portable Make a portable service DDI\n"
6846 " --generate-fstab=PATH\n"
6847 " Write fstab configuration to the given path\n"
6848 " --generate-crypttab=PATH\n"
6849 " Write crypttab configuration to the given path\n"
6850 "\nSee the %s for details.\n",
6851 program_invocation_short_name
,
6859 static int parse_argv(int argc
, char *argv
[]) {
6860 _cleanup_free_
char *private_key
= NULL
, *private_key_uri
= NULL
;
6863 ARG_VERSION
= 0x100,
6870 ARG_CAN_FACTORY_RESET
,
6881 ARG_PRIVATE_KEY_URI
,
6884 ARG_TPM2_DEVICE_KEY
,
6885 ARG_TPM2_SEAL_KEY_HANDLE
,
6887 ARG_TPM2_PUBLIC_KEY
,
6888 ARG_TPM2_PUBLIC_KEY_PCRS
,
6891 ARG_INCLUDE_PARTITIONS
,
6892 ARG_EXCLUDE_PARTITIONS
,
6893 ARG_DEFER_PARTITIONS
,
6895 ARG_SKIP_PARTITIONS
,
6901 ARG_GENERATE_CRYPTTAB
,
6904 static const struct option options
[] = {
6905 { "help", no_argument
, NULL
, 'h' },
6906 { "version", no_argument
, NULL
, ARG_VERSION
},
6907 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
6908 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
6909 { "dry-run", required_argument
, NULL
, ARG_DRY_RUN
},
6910 { "empty", required_argument
, NULL
, ARG_EMPTY
},
6911 { "discard", required_argument
, NULL
, ARG_DISCARD
},
6912 { "factory-reset", required_argument
, NULL
, ARG_FACTORY_RESET
},
6913 { "can-factory-reset", no_argument
, NULL
, ARG_CAN_FACTORY_RESET
},
6914 { "root", required_argument
, NULL
, ARG_ROOT
},
6915 { "image", required_argument
, NULL
, ARG_IMAGE
},
6916 { "image-policy", required_argument
, NULL
, ARG_IMAGE_POLICY
},
6917 { "seed", required_argument
, NULL
, ARG_SEED
},
6918 { "pretty", required_argument
, NULL
, ARG_PRETTY
},
6919 { "definitions", required_argument
, NULL
, ARG_DEFINITIONS
},
6920 { "size", required_argument
, NULL
, ARG_SIZE
},
6921 { "json", required_argument
, NULL
, ARG_JSON
},
6922 { "key-file", required_argument
, NULL
, ARG_KEY_FILE
},
6923 { "private-key", required_argument
, NULL
, ARG_PRIVATE_KEY
},
6924 { "private-key-uri", required_argument
, NULL
, ARG_PRIVATE_KEY_URI
},
6925 { "certificate", required_argument
, NULL
, ARG_CERTIFICATE
},
6926 { "tpm2-device", required_argument
, NULL
, ARG_TPM2_DEVICE
},
6927 { "tpm2-device-key", required_argument
, NULL
, ARG_TPM2_DEVICE_KEY
},
6928 { "tpm2-seal-key-handle", required_argument
, NULL
, ARG_TPM2_SEAL_KEY_HANDLE
},
6929 { "tpm2-pcrs", required_argument
, NULL
, ARG_TPM2_PCRS
},
6930 { "tpm2-public-key", required_argument
, NULL
, ARG_TPM2_PUBLIC_KEY
},
6931 { "tpm2-public-key-pcrs", required_argument
, NULL
, ARG_TPM2_PUBLIC_KEY_PCRS
},
6932 { "tpm2-pcrlock", required_argument
, NULL
, ARG_TPM2_PCRLOCK
},
6933 { "split", required_argument
, NULL
, ARG_SPLIT
},
6934 { "include-partitions", required_argument
, NULL
, ARG_INCLUDE_PARTITIONS
},
6935 { "exclude-partitions", required_argument
, NULL
, ARG_EXCLUDE_PARTITIONS
},
6936 { "defer-partitions", required_argument
, NULL
, ARG_DEFER_PARTITIONS
},
6937 { "sector-size", required_argument
, NULL
, ARG_SECTOR_SIZE
},
6938 { "architecture", required_argument
, NULL
, ARG_ARCHITECTURE
},
6939 { "offline", required_argument
, NULL
, ARG_OFFLINE
},
6940 { "copy-from", required_argument
, NULL
, ARG_COPY_FROM
},
6941 { "copy-source", required_argument
, NULL
, 's' },
6942 { "make-ddi", required_argument
, NULL
, ARG_MAKE_DDI
},
6943 { "generate-fstab", required_argument
, NULL
, ARG_GENERATE_FSTAB
},
6944 { "generate-crypttab", required_argument
, NULL
, ARG_GENERATE_CRYPTTAB
},
6948 bool auto_hash_pcr_values
= true, auto_public_key_pcr_mask
= true, auto_pcrlock
= true;
6954 while ((c
= getopt_long(argc
, argv
, "hs:SCP", options
, NULL
)) >= 0)
6965 arg_pager_flags
|= PAGER_DISABLE
;
6973 r
= parse_boolean_argument("--dry-run=", optarg
, &arg_dry_run
);
6979 if (isempty(optarg
)) {
6980 arg_empty
= EMPTY_UNSET
;
6984 arg_empty
= empty_mode_from_string(optarg
);
6986 return log_error_errno(arg_empty
, "Failed to parse --empty= parameter: %s", optarg
);
6991 r
= parse_boolean_argument("--discard=", optarg
, &arg_discard
);
6996 case ARG_FACTORY_RESET
:
6997 r
= parse_boolean_argument("--factory-reset=", optarg
, NULL
);
7000 arg_factory_reset
= r
;
7003 case ARG_CAN_FACTORY_RESET
:
7004 arg_can_factory_reset
= true;
7008 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_root
);
7014 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_image
);
7019 case ARG_IMAGE_POLICY
:
7020 r
= parse_image_policy_argument(optarg
, &arg_image_policy
);
7026 if (isempty(optarg
)) {
7027 arg_seed
= SD_ID128_NULL
;
7028 arg_randomize
= false;
7029 } else if (streq(optarg
, "random"))
7030 arg_randomize
= true;
7032 r
= sd_id128_from_string(optarg
, &arg_seed
);
7034 return log_error_errno(r
, "Failed to parse seed: %s", optarg
);
7036 arg_randomize
= false;
7042 r
= parse_boolean_argument("--pretty=", optarg
, NULL
);
7048 case ARG_DEFINITIONS
: {
7049 _cleanup_free_
char *path
= NULL
;
7050 r
= parse_path_argument(optarg
, false, &path
);
7053 if (strv_consume(&arg_definitions
, TAKE_PTR(path
)) < 0)
7059 uint64_t parsed
, rounded
;
7061 if (streq(optarg
, "auto")) {
7062 arg_size
= UINT64_MAX
;
7063 arg_size_auto
= true;
7067 r
= parse_size(optarg
, 1024, &parsed
);
7069 return log_error_errno(r
, "Failed to parse --size= parameter: %s", optarg
);
7071 rounded
= round_up_size(parsed
, 4096);
7073 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Specified image size too small, refusing.");
7074 if (rounded
== UINT64_MAX
)
7075 return log_error_errno(SYNTHETIC_ERRNO(ERANGE
), "Specified image size too large, refusing.");
7077 if (rounded
!= parsed
)
7078 log_warning("Specified size is not a multiple of 4096, rounding up automatically. (%" PRIu64
" %s %" PRIu64
")",
7079 parsed
, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
), rounded
);
7082 arg_size_auto
= false;
7087 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
7093 case ARG_KEY_FILE
: {
7094 _cleanup_(erase_and_freep
) char *k
= NULL
;
7097 r
= read_full_file_full(
7098 AT_FDCWD
, optarg
, UINT64_MAX
, SIZE_MAX
,
7099 READ_FULL_FILE_SECURE
|READ_FULL_FILE_WARN_WORLD_READABLE
|READ_FULL_FILE_CONNECT_SOCKET
,
7103 return log_error_errno(r
, "Failed to read key file '%s': %m", optarg
);
7105 erase_and_free(arg_key
);
7106 arg_key
= TAKE_PTR(k
);
7111 case ARG_PRIVATE_KEY
: {
7112 r
= free_and_strdup_warn(&private_key
, optarg
);
7118 case ARG_PRIVATE_KEY_URI
: {
7119 r
= free_and_strdup_warn(&private_key_uri
, optarg
);
7125 case ARG_CERTIFICATE
: {
7126 _cleanup_free_
char *cert
= NULL
;
7129 r
= read_full_file_full(
7130 AT_FDCWD
, optarg
, UINT64_MAX
, SIZE_MAX
,
7131 READ_FULL_FILE_CONNECT_SOCKET
,
7135 return log_error_errno(r
, "Failed to read certificate file '%s': %m", optarg
);
7137 X509_free(arg_certificate
);
7138 arg_certificate
= NULL
;
7139 r
= parse_x509_certificate(cert
, n
, &arg_certificate
);
7145 case ARG_TPM2_DEVICE
: {
7146 _cleanup_free_
char *device
= NULL
;
7148 if (streq(optarg
, "list"))
7149 return tpm2_list_devices();
7151 if (!streq(optarg
, "auto")) {
7152 device
= strdup(optarg
);
7157 free(arg_tpm2_device
);
7158 arg_tpm2_device
= TAKE_PTR(device
);
7162 case ARG_TPM2_DEVICE_KEY
:
7163 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_tpm2_device_key
);
7169 case ARG_TPM2_SEAL_KEY_HANDLE
:
7170 r
= safe_atou32_full(optarg
, 16, &arg_tpm2_seal_key_handle
);
7172 return log_error_errno(r
, "Could not parse TPM2 seal key handle index '%s': %m", optarg
);
7177 auto_hash_pcr_values
= false;
7178 r
= tpm2_parse_pcr_argument_append(optarg
, &arg_tpm2_hash_pcr_values
, &arg_tpm2_n_hash_pcr_values
);
7184 case ARG_TPM2_PUBLIC_KEY
:
7185 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_tpm2_public_key
);
7191 case ARG_TPM2_PUBLIC_KEY_PCRS
:
7192 auto_public_key_pcr_mask
= false;
7193 r
= tpm2_parse_pcr_argument_to_mask(optarg
, &arg_tpm2_public_key_pcr_mask
);
7199 case ARG_TPM2_PCRLOCK
:
7200 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_tpm2_pcrlock
);
7204 auto_pcrlock
= false;
7208 r
= parse_boolean_argument("--split=", optarg
, NULL
);
7215 case ARG_INCLUDE_PARTITIONS
:
7216 if (arg_filter_partitions_type
== FILTER_PARTITIONS_EXCLUDE
)
7217 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
7218 "Combination of --include-partitions= and --exclude-partitions= is invalid.");
7220 r
= parse_partition_types(optarg
, &arg_filter_partitions
, &arg_n_filter_partitions
);
7224 arg_filter_partitions_type
= FILTER_PARTITIONS_INCLUDE
;
7228 case ARG_EXCLUDE_PARTITIONS
:
7229 if (arg_filter_partitions_type
== FILTER_PARTITIONS_INCLUDE
)
7230 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
7231 "Combination of --include-partitions= and --exclude-partitions= is invalid.");
7233 r
= parse_partition_types(optarg
, &arg_filter_partitions
, &arg_n_filter_partitions
);
7237 arg_filter_partitions_type
= FILTER_PARTITIONS_EXCLUDE
;
7241 case ARG_DEFER_PARTITIONS
:
7242 r
= parse_partition_types(optarg
, &arg_defer_partitions
, &arg_n_defer_partitions
);
7248 case ARG_SECTOR_SIZE
:
7249 r
= parse_sector_size(optarg
, &arg_sector_size
);
7255 case ARG_ARCHITECTURE
:
7256 r
= architecture_from_string(optarg
);
7258 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid architecture '%s'", optarg
);
7260 arg_architecture
= r
;
7264 if (streq(optarg
, "auto"))
7267 r
= parse_boolean_argument("--offline=", optarg
, NULL
);
7276 case ARG_COPY_FROM
: {
7277 _cleanup_free_
char *p
= NULL
;
7279 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &p
);
7283 if (strv_consume(&arg_copy_from
, TAKE_PTR(p
)) < 0)
7290 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_copy_source
);
7296 if (!filename_is_valid(optarg
))
7297 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid DDI type: %s", optarg
);
7299 r
= free_and_strdup_warn(&arg_make_ddi
, optarg
);
7305 r
= free_and_strdup_warn(&arg_make_ddi
, "sysext");
7311 r
= free_and_strdup_warn(&arg_make_ddi
, "confext");
7317 r
= free_and_strdup_warn(&arg_make_ddi
, "portable");
7322 case ARG_GENERATE_FSTAB
:
7323 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_generate_fstab
);
7328 case ARG_GENERATE_CRYPTTAB
:
7329 r
= parse_path_argument(optarg
, /* suppress_root= */ false, &arg_generate_crypttab
);
7338 assert_not_reached();
7341 if (argc
- optind
> 1)
7342 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
7343 "Expected at most one argument, the path to the block device or image file.");
7346 if (arg_definitions
)
7347 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Combination of --make-ddi= and --definitions= is not supported.");
7348 if (!IN_SET(arg_empty
, EMPTY_UNSET
, EMPTY_CREATE
))
7349 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Combination of --make-ddi= and --empty=%s is not supported.", empty_mode_to_string(arg_empty
));
7351 /* Imply automatic sizing in DDI mode */
7352 if (arg_size
== UINT64_MAX
)
7353 arg_size_auto
= true;
7355 if (!arg_copy_source
)
7356 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "No --copy-source= specified, refusing.");
7358 r
= dir_is_empty(arg_copy_source
, /* ignore_hidden_or_backup= */ false);
7360 return log_error_errno(r
, "Failed to determine if '%s' is empty: %m", arg_copy_source
);
7362 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
), "Source directory '%s' is empty, refusing to create empty image.", arg_copy_source
);
7364 if (sd_id128_is_null(arg_seed
) && !arg_randomize
) {
7365 /* We don't want that /etc/machine-id leaks into any image built this way, hence
7366 * let's randomize the seed if not specified explicitly */
7367 log_notice("No seed value specified, randomizing generated UUIDs, resulting image will not be reproducible.");
7368 arg_randomize
= true;
7371 arg_empty
= EMPTY_CREATE
;
7374 if (arg_empty
== EMPTY_UNSET
) /* default to refuse mode, if not otherwise specified */
7375 arg_empty
= EMPTY_REFUSE
;
7377 if (arg_factory_reset
> 0 && IN_SET(arg_empty
, EMPTY_FORCE
, EMPTY_REQUIRE
, EMPTY_CREATE
))
7378 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
7379 "Combination of --factory-reset=yes and --empty=force/--empty=require/--empty=create is invalid.");
7381 if (arg_can_factory_reset
)
7382 arg_dry_run
= true; /* When --can-factory-reset is specified we don't make changes, hence
7383 * non-dry-run mode makes no sense. Thus, imply dry run mode so that we
7384 * open things strictly read-only. */
7385 else if (arg_empty
== EMPTY_CREATE
)
7386 arg_dry_run
= false; /* Imply --dry-run=no if we create the loopback file anew. After all we
7387 * cannot really break anyone's partition tables that way. */
7389 /* Disable pager once we are not just reviewing, but doing things. */
7391 arg_pager_flags
|= PAGER_DISABLE
;
7393 if (arg_empty
== EMPTY_CREATE
&& arg_size
== UINT64_MAX
&& !arg_size_auto
)
7394 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
7395 "If --empty=create is specified, --size= must be specified, too.");
7397 if (arg_image
&& arg_root
)
7398 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Please specify either --root= or --image=, the combination of both is not supported.");
7399 else if (!arg_image
&& !arg_root
&& in_initrd()) {
7401 /* By default operate on /sysusr/ or /sysroot/ when invoked in the initrd. We prefer the
7402 * former, if it is mounted, so that we have deterministic behaviour on systems where /usr/
7403 * is vendor-supplied but the root fs formatted on first boot. */
7404 r
= path_is_mount_point("/sysusr/usr");
7406 if (r
< 0 && r
!= -ENOENT
)
7407 log_debug_errno(r
, "Unable to determine whether /sysusr/usr is a mount point, assuming it is not: %m");
7409 arg_root
= strdup("/sysroot");
7411 arg_root
= strdup("/sysusr");
7416 if (argc
> optind
) {
7417 arg_node
= strdup(argv
[optind
]);
7422 if (IN_SET(arg_empty
, EMPTY_FORCE
, EMPTY_REQUIRE
, EMPTY_CREATE
) && !arg_node
&& !arg_image
)
7423 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
7424 "A path to a device node or image file must be specified when --make-ddi=, --empty=force, --empty=require or --empty=create are used.");
7426 if (arg_split
&& !arg_node
)
7427 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
7428 "A path to an image file must be specified when --split is used.");
7431 assert(!arg_tpm2_pcrlock
);
7433 r
= tpm2_pcrlock_search_file(NULL
, NULL
, &arg_tpm2_pcrlock
);
7436 log_warning_errno(r
, "Search for pcrlock.json failed, assuming it does not exist: %m");
7438 log_debug("Automatically using pcrlock policy '%s'.", arg_tpm2_pcrlock
);
7441 if (auto_public_key_pcr_mask
) {
7442 assert(arg_tpm2_public_key_pcr_mask
== 0);
7443 arg_tpm2_public_key_pcr_mask
= INDEX_TO_MASK(uint32_t, TPM2_PCR_KERNEL_BOOT
);
7446 if (auto_hash_pcr_values
&& !arg_tpm2_pcrlock
) { /* Only lock to PCR 7 if no pcr policy is specified. */
7447 assert(arg_tpm2_n_hash_pcr_values
== 0);
7449 if (!GREEDY_REALLOC_APPEND(
7450 arg_tpm2_hash_pcr_values
,
7451 arg_tpm2_n_hash_pcr_values
,
7452 &TPM2_PCR_VALUE_MAKE(TPM2_PCR_INDEX_DEFAULT
, /* hash= */ 0, /* value= */ {}),
7457 if (arg_pretty
< 0 && isatty(STDOUT_FILENO
))
7460 if (arg_architecture
>= 0) {
7461 FOREACH_ARRAY(p
, arg_filter_partitions
, arg_n_filter_partitions
)
7462 *p
= gpt_partition_type_override_architecture(*p
, arg_architecture
);
7464 FOREACH_ARRAY(p
, arg_defer_partitions
, arg_n_defer_partitions
)
7465 *p
= gpt_partition_type_override_architecture(*p
, arg_architecture
);
7468 if (private_key
&& private_key_uri
)
7469 return log_error_errno(
7470 SYNTHETIC_ERRNO(EINVAL
),
7471 "Cannot specify both --private-key= and --private-key-uri=.");
7474 _cleanup_(erase_and_freep
) char *k
= NULL
;
7477 r
= read_full_file_full(
7478 AT_FDCWD
, private_key
, UINT64_MAX
, SIZE_MAX
,
7479 READ_FULL_FILE_SECURE
|READ_FULL_FILE_WARN_WORLD_READABLE
|READ_FULL_FILE_CONNECT_SOCKET
,
7483 return log_error_errno(r
, "Failed to read key file '%s': %m", private_key
);
7485 r
= parse_private_key(k
, n
, &arg_private_key
);
7488 } else if (private_key_uri
) {
7489 /* This must happen after parse_x509_certificate() is called above, otherwise
7490 * signing later will get stuck as the parsed private key won't have the
7491 * certificate, so this block cannot be inline in ARG_PRIVATE_KEY. */
7492 r
= openssl_load_key_from_token(private_key_uri
, &arg_private_key
);
7494 return log_error_errno(
7496 "Failed to load key '%s' from OpenSSL provider: %m",
7503 static int parse_proc_cmdline_factory_reset(void) {
7507 if (arg_factory_reset
>= 0) /* Never override what is specified on the process command line */
7510 if (!in_initrd()) /* Never honour kernel command line factory reset request outside of the initrd */
7513 r
= proc_cmdline_get_bool("systemd.factory_reset", /* flags = */ 0, &b
);
7515 return log_error_errno(r
, "Failed to parse systemd.factory_reset kernel command line argument: %m");
7517 arg_factory_reset
= b
;
7520 log_notice("Honouring factory reset requested via kernel command line.");
7526 static int parse_efi_variable_factory_reset(void) {
7527 _cleanup_free_
char *value
= NULL
;
7530 if (arg_factory_reset
>= 0) /* Never override what is specified on the process command line */
7533 if (!in_initrd()) /* Never honour EFI variable factory reset request outside of the initrd */
7536 r
= efi_get_variable_string(EFI_SYSTEMD_VARIABLE(FactoryReset
), &value
);
7538 if (r
== -ENOENT
|| ERRNO_IS_NOT_SUPPORTED(r
))
7540 return log_error_errno(r
, "Failed to read EFI variable FactoryReset: %m");
7543 r
= parse_boolean(value
);
7545 return log_error_errno(r
, "Failed to parse EFI variable FactoryReset: %m");
7547 arg_factory_reset
= r
;
7549 log_notice("Factory reset requested via EFI variable FactoryReset.");
7554 static int remove_efi_variable_factory_reset(void) {
7557 r
= efi_set_variable(EFI_SYSTEMD_VARIABLE(FactoryReset
), NULL
, 0);
7559 if (r
== -ENOENT
|| ERRNO_IS_NOT_SUPPORTED(r
))
7561 return log_error_errno(r
, "Failed to remove EFI variable FactoryReset: %m");
7564 log_info("Successfully unset EFI variable FactoryReset.");
7568 static int acquire_root_devno(
7575 _cleanup_free_
char *found_path
= NULL
, *node
= NULL
;
7576 dev_t devno
, fd_devno
= MODE_INVALID
;
7577 _cleanup_close_
int fd
= -EBADF
;
7585 fd
= chase_and_open(p
, root
, CHASE_PREFIX_ROOT
, mode
, &found_path
);
7589 if (fstat(fd
, &st
) < 0)
7592 if (S_ISREG(st
.st_mode
)) {
7593 *ret
= TAKE_PTR(found_path
);
7594 *ret_fd
= TAKE_FD(fd
);
7598 if (S_ISBLK(st
.st_mode
)) {
7599 /* Refuse referencing explicit block devices if a root dir is specified, after all we should
7600 * not be able to leave the image the root path constrains us to. */
7604 fd_devno
= devno
= st
.st_rdev
;
7605 } else if (S_ISDIR(st
.st_mode
)) {
7608 if (major(devno
) == 0) {
7609 r
= btrfs_get_block_device_fd(fd
, &devno
);
7610 if (r
== -ENOTTY
) /* not btrfs */
7618 /* From dm-crypt to backing partition */
7619 r
= block_get_originating(devno
, &devno
);
7621 log_debug_errno(r
, "Device '%s' has no dm-crypt/dm-verity device, no need to look for underlying block device.", p
);
7623 log_debug_errno(r
, "Failed to find underlying block device for '%s', ignoring: %m", p
);
7625 /* From partition to whole disk containing it */
7626 r
= block_get_whole_disk(devno
, &devno
);
7628 log_debug_errno(r
, "Failed to find whole disk block device for '%s', ignoring: %m", p
);
7630 r
= devname_from_devnum(S_IFBLK
, devno
, &node
);
7632 return log_debug_errno(r
, "Failed to determine canonical path for '%s': %m", p
);
7634 /* Only if we still look at the same block device we can reuse the fd. Otherwise return an
7635 * invalidated fd. */
7636 if (fd_devno
!= MODE_INVALID
&& fd_devno
== devno
) {
7637 /* Tell udev not to interfere while we are processing the device */
7638 if (flock(fd
, arg_dry_run
? LOCK_SH
: LOCK_EX
) < 0)
7639 return log_error_errno(errno
, "Failed to lock device '%s': %m", node
);
7641 *ret_fd
= TAKE_FD(fd
);
7645 *ret
= TAKE_PTR(node
);
7649 static int find_root(Context
*context
) {
7650 _cleanup_free_
char *device
= NULL
;
7656 if (arg_empty
== EMPTY_CREATE
) {
7657 _cleanup_close_
int fd
= -EBADF
;
7658 _cleanup_free_
char *s
= NULL
;
7660 s
= strdup(arg_node
);
7664 fd
= open(arg_node
, O_RDONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
|O_NOFOLLOW
, 0666);
7666 return log_error_errno(errno
, "Failed to create '%s': %m", arg_node
);
7668 context
->node
= TAKE_PTR(s
);
7669 context
->node_is_our_file
= true;
7670 context
->backing_fd
= TAKE_FD(fd
);
7674 /* Note that we don't specify a root argument here: if the user explicitly configured a node
7675 * we'll take it relative to the host, not the image */
7676 r
= acquire_root_devno(arg_node
, NULL
, O_RDONLY
|O_CLOEXEC
, &context
->node
, &context
->backing_fd
);
7678 return btrfs_log_dev_root(LOG_ERR
, r
, arg_node
);
7680 return log_error_errno(r
, "Failed to open file or determine backing device of %s: %m", arg_node
);
7685 assert(IN_SET(arg_empty
, EMPTY_REFUSE
, EMPTY_ALLOW
));
7687 /* If the root mount has been replaced by some form of volatile file system (overlayfs), the
7688 * original root block device node is symlinked in /run/systemd/volatile-root. Let's read that
7690 r
= readlink_malloc("/run/systemd/volatile-root", &device
);
7691 if (r
== -ENOENT
) { /* volatile-root not found */
7692 /* Let's search for the root device. We look for two cases here: first in /, and then in /usr. The
7693 * latter we check for cases where / is a tmpfs and only /usr is an actual persistent block device
7694 * (think: volatile setups) */
7696 FOREACH_STRING(p
, "/", "/usr") {
7698 r
= acquire_root_devno(p
, arg_root
, O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
, &context
->node
,
7699 &context
->backing_fd
);
7702 return btrfs_log_dev_root(LOG_ERR
, r
, p
);
7704 return log_error_errno(r
, "Failed to determine backing device of %s: %m", p
);
7709 return log_error_errno(r
, "Failed to read symlink /run/systemd/volatile-root: %m");
7711 r
= acquire_root_devno(device
, NULL
, O_RDONLY
|O_CLOEXEC
, &context
->node
, &context
->backing_fd
);
7713 return btrfs_log_dev_root(LOG_ERR
, r
, device
);
7715 return log_error_errno(r
, "Failed to open file or determine backing device of %s: %m", device
);
7720 return log_error_errno(SYNTHETIC_ERRNO(ENODEV
), "Failed to discover root block device.");
7723 static int resize_pt(int fd
, uint64_t sector_size
) {
7724 _cleanup_(fdisk_unref_contextp
) struct fdisk_context
*c
= NULL
;
7727 /* After resizing the backing file we need to resize the partition table itself too, so that it takes
7728 * possession of the enlarged backing file. For this it suffices to open the device with libfdisk and
7729 * immediately write it again, with no changes. */
7731 r
= fdisk_new_context_at(fd
, /* path= */ NULL
, /* read_only= */ false, sector_size
, &c
);
7733 return log_error_errno(r
, "Failed to open device '%s': %m", FORMAT_PROC_FD_PATH(fd
));
7735 r
= fdisk_has_label(c
);
7737 return log_error_errno(r
, "Failed to determine whether disk '%s' has a disk label: %m", FORMAT_PROC_FD_PATH(fd
));
7739 log_debug("Not resizing partition table, as there currently is none.");
7743 r
= fdisk_write_disklabel(c
);
7745 return log_error_errno(r
, "Failed to write resized partition table: %m");
7747 log_info("Resized partition table.");
7751 static int resize_backing_fd(
7752 const char *node
, /* The primary way we access the disk image to operate on */
7753 int *fd
, /* An O_RDONLY fd referring to that inode */
7754 const char *backing_file
, /* If the above refers to a loopback device, the backing regular file for that, which we can grow */
7755 LoopDevice
*loop_device
,
7756 uint64_t sector_size
) {
7758 _cleanup_close_
int writable_fd
= -EBADF
;
7759 uint64_t current_size
;
7766 if (arg_size
== UINT64_MAX
) /* Nothing to do */
7770 /* Open the file if we haven't opened it yet. Note that we open it read-only here, just to
7771 * keep a reference to the file we can pass around. */
7772 *fd
= open(node
, O_RDONLY
|O_CLOEXEC
);
7774 return log_error_errno(errno
, "Failed to open '%s' in order to adjust size: %m", node
);
7777 if (fstat(*fd
, &st
) < 0)
7778 return log_error_errno(errno
, "Failed to stat '%s': %m", node
);
7780 if (S_ISBLK(st
.st_mode
)) {
7782 return log_error_errno(SYNTHETIC_ERRNO(EBADF
), "Cannot resize block device '%s'.", node
);
7784 assert(loop_device
);
7786 r
= blockdev_get_device_size(*fd
, ¤t_size
);
7788 return log_error_errno(r
, "Failed to determine size of block device %s: %m", node
);
7790 r
= stat_verify_regular(&st
);
7792 return log_error_errno(r
, "Specified path '%s' is not a regular file or loopback block device, cannot resize: %m", node
);
7794 assert(!backing_file
);
7795 assert(!loop_device
);
7796 current_size
= st
.st_size
;
7799 if (current_size
>= arg_size
) {
7800 log_info("File '%s' already is of requested size or larger, not growing. (%s >= %s)",
7801 node
, FORMAT_BYTES(current_size
), FORMAT_BYTES(arg_size
));
7805 if (S_ISBLK(st
.st_mode
)) {
7806 assert(backing_file
);
7808 /* This is a loopback device. We can't really grow those directly, but we can grow the
7809 * backing file, hence let's do that. */
7811 writable_fd
= open(backing_file
, O_WRONLY
|O_CLOEXEC
|O_NONBLOCK
);
7812 if (writable_fd
< 0)
7813 return log_error_errno(errno
, "Failed to open backing file '%s': %m", backing_file
);
7815 if (fstat(writable_fd
, &st
) < 0)
7816 return log_error_errno(errno
, "Failed to stat() backing file '%s': %m", backing_file
);
7818 r
= stat_verify_regular(&st
);
7820 return log_error_errno(r
, "Backing file '%s' of block device is not a regular file: %m", backing_file
);
7822 if ((uint64_t) st
.st_size
!= current_size
)
7823 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
7824 "Size of backing file '%s' of loopback block device '%s' don't match, refusing.",
7825 node
, backing_file
);
7827 assert(S_ISREG(st
.st_mode
));
7828 assert(!backing_file
);
7830 /* The file descriptor is read-only. In order to grow the file we need to have a writable fd. We
7831 * reopen the file for that temporarily. We keep the writable fd only open for this operation though,
7832 * as fdisk can't accept it anyway. */
7834 writable_fd
= fd_reopen(*fd
, O_WRONLY
|O_CLOEXEC
);
7835 if (writable_fd
< 0)
7836 return log_error_errno(writable_fd
, "Failed to reopen backing file '%s' writable: %m", node
);
7840 if (fallocate(writable_fd
, 0, 0, arg_size
) < 0) {
7841 if (!ERRNO_IS_NOT_SUPPORTED(errno
))
7842 return log_error_errno(errno
, "Failed to grow '%s' from %s to %s by allocation: %m",
7843 node
, FORMAT_BYTES(current_size
), FORMAT_BYTES(arg_size
));
7845 /* Fallback to truncation, if fallocate() is not supported. */
7846 log_debug("Backing file system does not support fallocate(), falling back to ftruncate().");
7848 if (current_size
== 0) /* Likely regular file just created by us */
7849 log_info("Allocated %s for '%s'.", FORMAT_BYTES(arg_size
), node
);
7851 log_info("File '%s' grown from %s to %s by allocation.",
7852 node
, FORMAT_BYTES(current_size
), FORMAT_BYTES(arg_size
));
7858 if (ftruncate(writable_fd
, arg_size
) < 0)
7859 return log_error_errno(errno
, "Failed to grow '%s' from %s to %s by truncation: %m",
7860 node
, FORMAT_BYTES(current_size
), FORMAT_BYTES(arg_size
));
7862 if (current_size
== 0) /* Likely regular file just created by us */
7863 log_info("Sized '%s' to %s.", node
, FORMAT_BYTES(arg_size
));
7865 log_info("File '%s' grown from %s to %s by truncation.",
7866 node
, FORMAT_BYTES(current_size
), FORMAT_BYTES(arg_size
));
7869 r
= resize_pt(writable_fd
, sector_size
);
7874 r
= loop_device_refresh_size(loop_device
, UINT64_MAX
, arg_size
);
7876 return log_error_errno(r
, "Failed to update loop device size: %m");
7882 static int determine_auto_size(Context
*c
) {
7887 sum
= round_up_size(GPT_METADATA_SIZE
, 4096);
7889 LIST_FOREACH(partitions
, p
, c
->partitions
) {
7895 m
= partition_min_size_with_padding(c
, p
);
7896 if (m
> UINT64_MAX
- sum
)
7897 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "Image would grow too large, refusing.");
7902 if (c
->total
!= UINT64_MAX
)
7903 /* Image already allocated? Then show its size. */
7904 log_info("Automatically determined minimal disk image size as %s, current image size is %s.",
7905 FORMAT_BYTES(sum
), FORMAT_BYTES(c
->total
));
7907 /* If the image is being created right now, then it has no previous size, suppress any comment about it hence. */
7908 log_info("Automatically determined minimal disk image size as %s.",
7915 static int run(int argc
, char *argv
[]) {
7916 _cleanup_(loop_device_unrefp
) LoopDevice
*loop_device
= NULL
;
7917 _cleanup_(umount_and_freep
) char *mounted_dir
= NULL
;
7918 _cleanup_(context_freep
) Context
* context
= NULL
;
7919 bool node_is_our_loop
= false;
7922 log_show_color(true);
7923 log_parse_environment();
7926 r
= parse_argv(argc
, argv
);
7930 r
= parse_proc_cmdline_factory_reset();
7934 r
= parse_efi_variable_factory_reset();
7938 #if HAVE_LIBCRYPTSETUP
7939 cryptsetup_enable_logging(NULL
);
7945 /* Mount this strictly read-only: we shall modify the partition table, not the file
7947 r
= mount_image_privately_interactively(
7950 DISSECT_IMAGE_MOUNT_READ_ONLY
|
7951 (arg_node
? DISSECT_IMAGE_DEVICE_READ_ONLY
: 0) | /* If a different node to make changes to is specified let's open the device in read-only mode) */
7952 DISSECT_IMAGE_GPT_ONLY
|
7953 DISSECT_IMAGE_RELAX_VAR_CHECK
|
7954 DISSECT_IMAGE_USR_NO_ROOT
|
7955 DISSECT_IMAGE_REQUIRE_ROOT
|
7956 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY
,
7958 /* ret_dir_fd= */ NULL
,
7963 arg_root
= strdup(mounted_dir
);
7968 arg_node
= strdup(loop_device
->node
);
7972 /* Remember that the device we are about to manipulate is actually the one we
7973 * allocated here, and thus to increase its backing file we know what to do */
7974 node_is_our_loop
= true;
7978 if (!arg_copy_source
&& arg_root
) {
7979 /* If no explicit copy source is specified, then use --root=/--image= */
7980 arg_copy_source
= strdup(arg_root
);
7981 if (!arg_copy_source
)
7985 context
= context_new(arg_seed
);
7989 r
= context_copy_from(context
);
7994 _cleanup_free_
char *d
= NULL
, *dp
= NULL
;
7995 assert(!arg_definitions
);
7997 d
= strjoin(arg_make_ddi
, ".repart.d/");
8001 r
= search_and_access(d
, F_OK
, NULL
, CONF_PATHS_USR_STRV("systemd/repart/definitions"), &dp
);
8003 return log_error_errno(r
, "DDI type '%s' is not defined: %m", arg_make_ddi
);
8005 if (strv_consume(&arg_definitions
, TAKE_PTR(dp
)) < 0)
8008 strv_uniq(arg_definitions
);
8010 r
= context_read_definitions(context
);
8014 r
= find_root(context
);
8016 return 76; /* Special return value which means "Root block device not found, so not doing
8017 * anything". This isn't really an error when called at boot. */
8021 if (arg_size
!= UINT64_MAX
) {
8022 r
= resize_backing_fd(
8024 &context
->backing_fd
,
8025 node_is_our_loop
? arg_image
: NULL
,
8026 node_is_our_loop
? loop_device
: NULL
,
8027 context
->sector_size
);
8032 r
= context_load_partition_table(context
);
8033 if (r
== -EHWPOISON
)
8034 return 77; /* Special return value which means "Not GPT, so not doing anything". This isn't
8035 * really an error when called at boot. */
8038 context
->from_scratch
= r
> 0; /* Starting from scratch */
8040 if (arg_can_factory_reset
) {
8041 r
= context_can_factory_reset(context
);
8045 return EXIT_FAILURE
;
8050 r
= context_factory_reset(context
);
8054 /* We actually did a factory reset! */
8055 r
= remove_efi_variable_factory_reset();
8059 /* Reload the reduced partition table */
8060 context_unload_partition_table(context
);
8061 r
= context_load_partition_table(context
);
8066 r
= context_read_seed(context
, arg_root
);
8070 /* Make sure each partition has a unique UUID and unique label */
8071 r
= context_acquire_partition_uuids_and_labels(context
);
8075 /* Open all files to copy blocks from now, since we want to take their size into consideration */
8076 r
= context_open_copy_block_paths(
8078 loop_device
? loop_device
->devno
: /* if --image= is specified, only allow partitions on the loopback device */
8079 arg_root
&& !arg_image
? 0 : /* if --root= is specified, don't accept any block device */
8080 (dev_t
) -1); /* if neither is specified, make no restrictions */
8084 r
= context_fstab(context
);
8088 r
= context_crypttab(context
);
8092 r
= context_minimize(context
);
8096 if (arg_size_auto
) {
8097 r
= determine_auto_size(context
);
8101 /* Flush out everything again, and let's grow the file first, then start fresh */
8102 context_unload_partition_table(context
);
8104 assert(arg_size
!= UINT64_MAX
);
8105 r
= resize_backing_fd(
8107 &context
->backing_fd
,
8108 node_is_our_loop
? arg_image
: NULL
,
8109 node_is_our_loop
? loop_device
: NULL
,
8110 context
->sector_size
);
8114 r
= context_load_partition_table(context
);
8119 /* First try to fit new partitions in, dropping by priority until it fits */
8121 uint64_t largest_free_area
;
8123 if (context_allocate_partitions(context
, &largest_free_area
))
8124 break; /* Success! */
8126 if (!context_drop_or_foreignize_one_priority(context
)) {
8127 r
= log_error_errno(SYNTHETIC_ERRNO(ENOSPC
),
8128 "Can't fit requested partitions into available free space (%s), refusing.",
8129 FORMAT_BYTES(largest_free_area
));
8130 determine_auto_size(context
);
8135 /* Now assign free space according to the weight logic */
8136 r
= context_grow_partitions(context
);
8140 /* Now calculate where each new partition gets placed */
8141 context_place_partitions(context
);
8143 (void) context_dump(context
, /*late=*/ false);
8145 r
= context_write_partition_table(context
);
8149 r
= context_split(context
);
8153 (void) context_dump(context
, /*late=*/ true);
8155 context
->node
= mfree(context
->node
);
8157 LIST_FOREACH(partitions
, p
, context
->partitions
)
8158 p
->split_path
= mfree(p
->split_path
);
8163 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);