]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/partition/repart.c
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[thirdparty/systemd.git] / src / partition / repart.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
e594a3b1
LP
2
3#if HAVE_VALGRIND_MEMCHECK_H
4#include <valgrind/memcheck.h>
5#endif
6
7#include <fcntl.h>
8#include <getopt.h>
e594a3b1
LP
9#include <linux/fs.h>
10#include <linux/loop.h>
11#include <sys/file.h>
12#include <sys/ioctl.h>
13#include <sys/stat.h>
14
1a037ba2 15#include "sd-device.h"
e594a3b1
LP
16#include "sd-id128.h"
17
18#include "alloc-util.h"
19#include "blkid-util.h"
20#include "blockdev-util.h"
21#include "btrfs-util.h"
d6b4d1c7 22#include "build.h"
f461a28d 23#include "chase.h"
e594a3b1
LP
24#include "conf-files.h"
25#include "conf-parser.h"
28db6fbf 26#include "constants.h"
1e2f3230 27#include "cryptsetup-util.h"
ca822829 28#include "device-util.h"
7176f06c 29#include "devnum-util.h"
5c08da58 30#include "dirent-util.h"
e594a3b1
LP
31#include "efivars.h"
32#include "errno-util.h"
33#include "fd-util.h"
4dc07c3a 34#include "fdisk-util.h"
b9df3536 35#include "fileio.h"
e594a3b1
LP
36#include "format-table.h"
37#include "format-util.h"
38#include "fs-util.h"
d8e32c47 39#include "glyph-util.h"
e594a3b1 40#include "gpt.h"
889914ef 41#include "hexdecoct.h"
ade99252 42#include "hmac.h"
e594a3b1 43#include "id128-util.h"
baa6a42d 44#include "initrd-util.h"
b456191d 45#include "io-util.h"
a015fbe7 46#include "json.h"
e594a3b1 47#include "list.h"
53171c04 48#include "loop-util.h"
e594a3b1 49#include "main-func.h"
8a794850 50#include "mkdir.h"
53171c04 51#include "mkfs-util.h"
8a794850 52#include "mount-util.h"
5c08da58 53#include "mountpoint-util.h"
c0fad2d9 54#include "nulstr-util.h"
b456191d 55#include "openssl-util.h"
614b022c 56#include "parse-argument.h"
c3eaba2d 57#include "parse-helpers.h"
e594a3b1
LP
58#include "pretty-print.h"
59#include "proc-cmdline.h"
8a794850 60#include "process-util.h"
b9df3536 61#include "random-util.h"
170c9823 62#include "resize-fs.h"
95bfd3cd 63#include "rm-rf.h"
e594a3b1 64#include "sort-util.h"
e031166e 65#include "specifier.h"
e594a3b1 66#include "stdio-util.h"
889914ef 67#include "string-table.h"
e594a3b1
LP
68#include "string-util.h"
69#include "strv.h"
bf819d3a 70#include "sync-util.h"
e594a3b1 71#include "terminal-util.h"
e0e1f4f7
LP
72#include "tmpfile-util.h"
73#include "tpm2-pcr.h"
889914ef 74#include "tpm2-util.h"
8a794850 75#include "user-util.h"
e594a3b1
LP
76#include "utf8.h"
77
fb08381c 78/* If not configured otherwise use a minimal partition size of 10M */
b262cbe8 79#define DEFAULT_MIN_SIZE (10ULL*1024ULL*1024ULL)
fb08381c
LP
80
81/* Hard lower limit for new partition sizes */
b262cbe8 82#define HARD_MIN_SIZE 4096ULL
fb08381c 83
b456191d 84/* We know up front we're never going to put more than this in a verity sig partition. */
b262cbe8 85#define VERITY_SIG_SIZE (HARD_MIN_SIZE*4ULL)
b456191d 86
69e3234d 87/* libfdisk takes off slightly more than 1M of the disk size when creating a GPT disk label */
b262cbe8 88#define GPT_METADATA_SIZE (1044ULL*1024ULL)
170c9823
LP
89
90/* LUKS2 takes off 16M of the partition size with its metadata by default */
b262cbe8 91#define LUKS2_METADATA_SIZE (16ULL*1024ULL*1024ULL)
170c9823 92
48a09a8f
DDM
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)
95
98e0456e
DDM
96/* LUKS2 volume key size. */
97#define VOLUME_KEY_SIZE (512ULL/8ULL)
98
93f125a6
DDM
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
102
e57b7020
DDM
103#define APIVFS_TMP_DIRS_NULSTR "proc\0sys\0dev\0tmp\0run\0var/tmp\0"
104
e594a3b1
LP
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. */
109
634b8471 110typedef enum EmptyMode {
243dd1e9 111 EMPTY_UNSET, /* no choice has been made yet */
e594a3b1
LP
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 */
a26f4a49 116 EMPTY_CREATE, /* create disk as loopback file, create a partition table always */
634b8471
LP
117 _EMPTY_MODE_MAX,
118 _EMPTY_MODE_INVALID = -EINVAL,
119} EmptyMode;
e594a3b1 120
53538e33 121typedef enum FilterPartitionType {
81d1098b
DDM
122 FILTER_PARTITIONS_NONE,
123 FILTER_PARTITIONS_EXCLUDE,
124 FILTER_PARTITIONS_INCLUDE,
125 _FILTER_PARTITIONS_MAX,
126 _FILTER_PARTITIONS_INVALID = -EINVAL,
127} FilterPartitionsType;
128
634b8471 129static EmptyMode arg_empty = EMPTY_UNSET;
e594a3b1
LP
130static bool arg_dry_run = true;
131static const char *arg_node = NULL;
132static char *arg_root = NULL;
252d6267 133static char *arg_image = NULL;
224c853f 134static char **arg_definitions = NULL;
e594a3b1
LP
135static bool arg_discard = true;
136static bool arg_can_factory_reset = false;
137static int arg_factory_reset = -1;
138static sd_id128_t arg_seed = SD_ID128_NULL;
139static bool arg_randomize = false;
140static int arg_pretty = -1;
a26f4a49 141static uint64_t arg_size = UINT64_MAX;
170c9823 142static bool arg_size_auto = false;
6a01ea4a 143static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
896e678b
LP
144static PagerFlags arg_pager_flags = 0;
145static bool arg_legend = true;
b9df3536
LP
146static void *arg_key = NULL;
147static size_t arg_key_size = 0;
b456191d
DDM
148static EVP_PKEY *arg_private_key = NULL;
149static X509 *arg_certificate = NULL;
889914ef 150static char *arg_tpm2_device = NULL;
78fdf0f6
LP
151static uint32_t arg_tpm2_seal_key_handle = 0;
152static char *arg_tpm2_device_key = NULL;
9e437994
DS
153static Tpm2PCRValue *arg_tpm2_hash_pcr_values = NULL;
154static size_t arg_tpm2_n_hash_pcr_values = 0;
02ef97cd 155static char *arg_tpm2_public_key = NULL;
9e437994 156static uint32_t arg_tpm2_public_key_pcr_mask = 0;
404aea78 157static char *arg_tpm2_pcrlock = NULL;
4cee8333 158static bool arg_split = false;
9786dfe6 159static GptPartitionType *arg_filter_partitions = NULL;
d989dd76 160static size_t arg_n_filter_partitions = 0;
81d1098b 161static FilterPartitionsType arg_filter_partitions_type = FILTER_PARTITIONS_NONE;
9786dfe6 162static GptPartitionType *arg_defer_partitions = NULL;
8275334b 163static size_t arg_n_defer_partitions = 0;
e1878ef7 164static uint64_t arg_sector_size = 0;
84be0c71 165static ImagePolicy *arg_image_policy = NULL;
9786dfe6 166static Architecture arg_architecture = _ARCHITECTURE_INVALID;
fc10b158 167static int arg_offline = -1;
e1536d1f 168static char **arg_copy_from = NULL;
607343a1 169static char *arg_copy_source = NULL;
a121b331 170static char *arg_make_ddi = NULL;
e594a3b1
LP
171
172STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
252d6267 173STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
224c853f 174STATIC_DESTRUCTOR_REGISTER(arg_definitions, strv_freep);
b9df3536 175STATIC_DESTRUCTOR_REGISTER(arg_key, erase_and_freep);
b456191d
DDM
176STATIC_DESTRUCTOR_REGISTER(arg_private_key, EVP_PKEY_freep);
177STATIC_DESTRUCTOR_REGISTER(arg_certificate, X509_freep);
889914ef 178STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep);
78fdf0f6 179STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device_key, freep);
9e437994 180STATIC_DESTRUCTOR_REGISTER(arg_tpm2_hash_pcr_values, freep);
02ef97cd 181STATIC_DESTRUCTOR_REGISTER(arg_tpm2_public_key, freep);
404aea78 182STATIC_DESTRUCTOR_REGISTER(arg_tpm2_pcrlock, freep);
81d1098b 183STATIC_DESTRUCTOR_REGISTER(arg_filter_partitions, freep);
84be0c71 184STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
e1536d1f 185STATIC_DESTRUCTOR_REGISTER(arg_copy_from, strv_freep);
607343a1 186STATIC_DESTRUCTOR_REGISTER(arg_copy_source, freep);
a121b331 187STATIC_DESTRUCTOR_REGISTER(arg_make_ddi, freep);
e594a3b1 188
e594a3b1 189typedef struct FreeArea FreeArea;
e594a3b1 190
889914ef
LP
191typedef enum EncryptMode {
192 ENCRYPT_OFF,
193 ENCRYPT_KEY_FILE,
194 ENCRYPT_TPM2,
195 ENCRYPT_KEY_FILE_TPM2,
196 _ENCRYPT_MODE_MAX,
2d93c20e 197 _ENCRYPT_MODE_INVALID = -EINVAL,
889914ef
LP
198} EncryptMode;
199
b5b7879a
DDM
200typedef enum VerityMode {
201 VERITY_OFF,
202 VERITY_DATA,
203 VERITY_HASH,
b456191d 204 VERITY_SIG,
b5b7879a
DDM
205 _VERITY_MODE_MAX,
206 _VERITY_MODE_INVALID = -EINVAL,
207} VerityMode;
208
5c33b686
DDM
209typedef enum MinimizeMode {
210 MINIMIZE_OFF,
211 MINIMIZE_BEST,
212 MINIMIZE_GUESS,
213 _MINIMIZE_MODE_MAX,
214 _MINIMIZE_MODE_INVALID = -EINVAL,
215} MinimizeMode;
216
448cfb7f 217typedef struct Partition {
e594a3b1 218 char *definition_path;
39fc0174 219 char **drop_in_files;
e594a3b1 220
22e932f4 221 GptPartitionType type;
e594a3b1 222 sd_id128_t current_uuid, new_uuid;
11749b61 223 bool new_uuid_is_set;
e594a3b1 224 char *current_label, *new_label;
81e04781
VH
225 sd_id128_t fs_uuid, luks_uuid, verity_uuid;
226 uint8_t verity_salt[SHA256_DIGEST_SIZE];
e594a3b1
LP
227
228 bool dropped;
229 bool factory_reset;
230 int32_t priority;
231
232 uint32_t weight, padding_weight;
233
234 uint64_t current_size, new_size;
235 uint64_t size_min, size_max;
236
237 uint64_t current_padding, new_padding;
238 uint64_t padding_min, padding_max;
239
240 uint64_t partno;
241 uint64_t offset;
242
243 struct fdisk_partition *current_partition;
244 struct fdisk_partition *new_partition;
245 FreeArea *padding_area;
246 FreeArea *allocated_to_area;
247
757bc2e4 248 char *copy_blocks_path;
7d07030e 249 bool copy_blocks_path_is_our_file;
5c08da58 250 bool copy_blocks_auto;
585c5c75 251 const char *copy_blocks_root;
757bc2e4 252 int copy_blocks_fd;
1e46985a 253 uint64_t copy_blocks_offset;
757bc2e4
LP
254 uint64_t copy_blocks_size;
255
53171c04 256 char *format;
8a794850 257 char **copy_files;
600bf76c
DDM
258 char **exclude_files_source;
259 char **exclude_files_target;
d83d8048 260 char **make_directories;
440f805c 261 char **subvolumes;
889914ef 262 EncryptMode encrypt;
b5b7879a
DDM
263 VerityMode verity;
264 char *verity_match_key;
5c33b686 265 MinimizeMode minimize;
c380047b
MC
266 uint64_t verity_data_block_size;
267 uint64_t verity_hash_block_size;
53171c04 268
e73309c5 269 uint64_t gpt_flags;
ff0771bf 270 int no_auto;
e73309c5 271 int read_only;
1c41c1dc 272 int growfs;
e73309c5 273
6b4b40f4 274 struct iovec roothash;
b5b7879a 275
4cee8333 276 char *split_name_format;
62108348 277 char *split_path;
4cee8333 278
448cfb7f 279 struct Partition *siblings[_VERITY_MODE_MAX];
b5b7879a 280
448cfb7f
DDM
281 LIST_FIELDS(struct Partition, partitions);
282} Partition;
e594a3b1
LP
283
284#define PARTITION_IS_FOREIGN(p) (!(p)->definition_path)
285#define PARTITION_EXISTS(p) (!!(p)->current_partition)
286
287struct FreeArea {
288 Partition *after;
289 uint64_t size;
290 uint64_t allocated;
291};
292
448cfb7f 293typedef struct Context {
e594a3b1
LP
294 LIST_HEAD(Partition, partitions);
295 size_t n_partitions;
296
297 FreeArea **free_areas;
319a4f4b 298 size_t n_free_areas;
e594a3b1
LP
299
300 uint64_t start, end, total;
301
302 struct fdisk_context *fdisk_context;
93f125a6 303 uint64_t sector_size, grain_size, fs_sector_size;
e594a3b1
LP
304
305 sd_id128_t seed;
cc751c75
DDM
306
307 char *node;
db1d4e6b 308 bool node_is_our_file;
cc751c75
DDM
309 int backing_fd;
310
311 bool from_scratch;
448cfb7f 312} Context;
e594a3b1 313
634b8471
LP
314static const char *empty_mode_table[_EMPTY_MODE_MAX] = {
315 [EMPTY_UNSET] = "unset",
316 [EMPTY_REFUSE] = "refuse",
317 [EMPTY_ALLOW] = "allow",
318 [EMPTY_REQUIRE] = "require",
319 [EMPTY_FORCE] = "force",
320 [EMPTY_CREATE] = "create",
321};
322
889914ef
LP
323static const char *encrypt_mode_table[_ENCRYPT_MODE_MAX] = {
324 [ENCRYPT_OFF] = "off",
325 [ENCRYPT_KEY_FILE] = "key-file",
326 [ENCRYPT_TPM2] = "tpm2",
327 [ENCRYPT_KEY_FILE_TPM2] = "key-file+tpm2",
328};
329
b5b7879a
DDM
330static const char *verity_mode_table[_VERITY_MODE_MAX] = {
331 [VERITY_OFF] = "off",
332 [VERITY_DATA] = "data",
333 [VERITY_HASH] = "hash",
b456191d 334 [VERITY_SIG] = "signature",
b5b7879a
DDM
335};
336
5c33b686
DDM
337static const char *minimize_mode_table[_MINIMIZE_MODE_MAX] = {
338 [MINIMIZE_OFF] = "off",
339 [MINIMIZE_BEST] = "best",
340 [MINIMIZE_GUESS] = "guess",
341};
342
634b8471 343DEFINE_PRIVATE_STRING_TABLE_LOOKUP(empty_mode, EmptyMode);
2709d029 344DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(encrypt_mode, EncryptMode, ENCRYPT_KEY_FILE);
86bebe38 345DEFINE_PRIVATE_STRING_TABLE_LOOKUP(verity_mode, VerityMode);
5c33b686 346DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(minimize_mode, MinimizeMode, MINIMIZE_BEST);
889914ef 347
e594a3b1
LP
348static uint64_t round_down_size(uint64_t v, uint64_t p) {
349 return (v / p) * p;
350}
351
352static uint64_t round_up_size(uint64_t v, uint64_t p) {
353
354 v = DIV_ROUND_UP(v, p);
355
356 if (v > UINT64_MAX / p)
357 return UINT64_MAX; /* overflow */
358
359 return v * p;
360}
361
362static Partition *partition_new(void) {
363 Partition *p;
364
365 p = new(Partition, 1);
366 if (!p)
367 return NULL;
368
369 *p = (Partition) {
370 .weight = 1000,
371 .padding_weight = 0,
372 .current_size = UINT64_MAX,
373 .new_size = UINT64_MAX,
374 .size_min = UINT64_MAX,
375 .size_max = UINT64_MAX,
376 .current_padding = UINT64_MAX,
377 .new_padding = UINT64_MAX,
378 .padding_min = UINT64_MAX,
379 .padding_max = UINT64_MAX,
380 .partno = UINT64_MAX,
381 .offset = UINT64_MAX,
254d1313 382 .copy_blocks_fd = -EBADF,
1e46985a 383 .copy_blocks_offset = UINT64_MAX,
757bc2e4 384 .copy_blocks_size = UINT64_MAX,
ff0771bf 385 .no_auto = -1,
e73309c5 386 .read_only = -1,
1c41c1dc 387 .growfs = -1,
c380047b
MC
388 .verity_data_block_size = UINT64_MAX,
389 .verity_hash_block_size = UINT64_MAX,
e594a3b1
LP
390 };
391
392 return p;
393}
394
395static Partition* partition_free(Partition *p) {
396 if (!p)
397 return NULL;
398
399 free(p->current_label);
400 free(p->new_label);
401 free(p->definition_path);
39fc0174 402 strv_free(p->drop_in_files);
e594a3b1
LP
403
404 if (p->current_partition)
405 fdisk_unref_partition(p->current_partition);
406 if (p->new_partition)
407 fdisk_unref_partition(p->new_partition);
408
7d07030e
DDM
409 if (p->copy_blocks_path_is_our_file)
410 unlink_and_free(p->copy_blocks_path);
411 else
412 free(p->copy_blocks_path);
757bc2e4
LP
413 safe_close(p->copy_blocks_fd);
414
53171c04 415 free(p->format);
8a794850 416 strv_free(p->copy_files);
600bf76c
DDM
417 strv_free(p->exclude_files_source);
418 strv_free(p->exclude_files_target);
d83d8048 419 strv_free(p->make_directories);
440f805c 420 strv_free(p->subvolumes);
b5b7879a
DDM
421 free(p->verity_match_key);
422
6b4b40f4 423 iovec_done(&p->roothash);
53171c04 424
4cee8333 425 free(p->split_name_format);
a2d7c42e 426 unlink_and_free(p->split_path);
4cee8333 427
e594a3b1
LP
428 return mfree(p);
429}
430
9ccceb9d
YW
431static void partition_foreignize(Partition *p) {
432 assert(p);
433 assert(PARTITION_EXISTS(p));
434
435 /* Reset several parameters set through definition file to make the partition foreign. */
436
9ccceb9d
YW
437 p->definition_path = mfree(p->definition_path);
438 p->drop_in_files = strv_free(p->drop_in_files);
439
440 p->copy_blocks_path = mfree(p->copy_blocks_path);
441 p->copy_blocks_fd = safe_close(p->copy_blocks_fd);
585c5c75 442 p->copy_blocks_root = NULL;
9ccceb9d
YW
443
444 p->format = mfree(p->format);
445 p->copy_files = strv_free(p->copy_files);
600bf76c
DDM
446 p->exclude_files_source = strv_free(p->exclude_files_source);
447 p->exclude_files_target = strv_free(p->exclude_files_target);
9ccceb9d 448 p->make_directories = strv_free(p->make_directories);
440f805c 449 p->subvolumes = strv_free(p->subvolumes);
9ccceb9d
YW
450 p->verity_match_key = mfree(p->verity_match_key);
451
9ccceb9d
YW
452 p->priority = 0;
453 p->weight = 1000;
454 p->padding_weight = 0;
455 p->size_min = UINT64_MAX;
456 p->size_max = UINT64_MAX;
457 p->padding_min = UINT64_MAX;
458 p->padding_max = UINT64_MAX;
459 p->no_auto = -1;
460 p->read_only = -1;
461 p->growfs = -1;
462 p->verity = VERITY_OFF;
463}
464
c74d50ff 465static bool partition_type_exclude(const GptPartitionType *type) {
81d1098b
DDM
466 if (arg_filter_partitions_type == FILTER_PARTITIONS_NONE)
467 return false;
468
d989dd76 469 for (size_t i = 0; i < arg_n_filter_partitions; i++)
c74d50ff 470 if (sd_id128_equal(type->uuid, arg_filter_partitions[i].uuid))
81d1098b
DDM
471 return arg_filter_partitions_type == FILTER_PARTITIONS_EXCLUDE;
472
473 return arg_filter_partitions_type == FILTER_PARTITIONS_INCLUDE;
474}
475
c74d50ff 476static bool partition_type_defer(const GptPartitionType *type) {
8275334b 477 for (size_t i = 0; i < arg_n_defer_partitions; i++)
c74d50ff 478 if (sd_id128_equal(type->uuid, arg_defer_partitions[i].uuid))
7d505753
DDM
479 return true;
480
481 return false;
482}
483
e594a3b1
LP
484static Partition* partition_unlink_and_free(Context *context, Partition *p) {
485 if (!p)
486 return NULL;
487
488 LIST_REMOVE(partitions, context->partitions, p);
489
490 assert(context->n_partitions > 0);
491 context->n_partitions--;
492
493 return partition_free(p);
494}
495
496DEFINE_TRIVIAL_CLEANUP_FUNC(Partition*, partition_free);
497
498static Context *context_new(sd_id128_t seed) {
499 Context *context;
500
501 context = new(Context, 1);
502 if (!context)
503 return NULL;
504
505 *context = (Context) {
506 .start = UINT64_MAX,
507 .end = UINT64_MAX,
508 .total = UINT64_MAX,
509 .seed = seed,
510 };
511
512 return context;
513}
514
515static void context_free_free_areas(Context *context) {
516 assert(context);
517
518 for (size_t i = 0; i < context->n_free_areas; i++)
519 free(context->free_areas[i]);
520
521 context->free_areas = mfree(context->free_areas);
522 context->n_free_areas = 0;
e594a3b1
LP
523}
524
525static Context *context_free(Context *context) {
526 if (!context)
527 return NULL;
528
529 while (context->partitions)
530 partition_unlink_and_free(context, context->partitions);
531 assert(context->n_partitions == 0);
532
533 context_free_free_areas(context);
534
535 if (context->fdisk_context)
536 fdisk_unref_context(context->fdisk_context);
537
cc751c75 538 safe_close(context->backing_fd);
db1d4e6b
DDM
539 if (context->node_is_our_file)
540 unlink_and_free(context->node);
541 else
542 free(context->node);
cc751c75 543
e594a3b1
LP
544 return mfree(context);
545}
546
547DEFINE_TRIVIAL_CLEANUP_FUNC(Context*, context_free);
548
549static int context_add_free_area(
550 Context *context,
551 uint64_t size,
552 Partition *after) {
553
554 FreeArea *a;
555
556 assert(context);
557 assert(!after || !after->padding_area);
558
319a4f4b 559 if (!GREEDY_REALLOC(context->free_areas, context->n_free_areas + 1))
e594a3b1
LP
560 return -ENOMEM;
561
562 a = new(FreeArea, 1);
563 if (!a)
564 return -ENOMEM;
565
566 *a = (FreeArea) {
567 .size = size,
568 .after = after,
569 };
570
571 context->free_areas[context->n_free_areas++] = a;
572
573 if (after)
574 after->padding_area = a;
575
576 return 0;
577}
578
9ccceb9d
YW
579static void partition_drop_or_foreignize(Partition *p) {
580 if (!p || p->dropped || PARTITION_IS_FOREIGN(p))
581 return;
582
583 if (PARTITION_EXISTS(p)) {
584 log_info("Can't grow existing partition %s of priority %" PRIi32 ", ignoring.",
585 strna(p->current_label ?: p->new_label), p->priority);
586
587 /* Handle the partition as foreign. Do not set dropped flag. */
588 partition_foreignize(p);
589 } else {
590 log_info("Can't fit partition %s of priority %" PRIi32 ", dropping.",
591 p->definition_path, p->priority);
592
593 p->dropped = true;
594 p->allocated_to_area = NULL;
595 }
596}
597
598static bool context_drop_or_foreignize_one_priority(Context *context) {
e594a3b1 599 int32_t priority = 0;
e594a3b1
LP
600
601 LIST_FOREACH(partitions, p, context->partitions) {
602 if (p->dropped)
603 continue;
e594a3b1 604
9ccceb9d 605 priority = MAX(priority, p->priority);
e594a3b1
LP
606 }
607
608 /* Refuse to drop partitions with 0 or negative priorities or partitions of priorities that have at
609 * least one existing priority */
9ccceb9d 610 if (priority <= 0)
e594a3b1
LP
611 return false;
612
613 LIST_FOREACH(partitions, p, context->partitions) {
614 if (p->priority < priority)
615 continue;
616
9ccceb9d 617 partition_drop_or_foreignize(p);
b5b7879a
DDM
618
619 /* We ensure that all verity sibling partitions have the same priority, so it's safe
620 * to drop all siblings here as well. */
621
9ccceb9d
YW
622 for (VerityMode mode = VERITY_OFF + 1; mode < _VERITY_MODE_MAX; mode++)
623 partition_drop_or_foreignize(p->siblings[mode]);
e594a3b1
LP
624 }
625
626 return true;
627}
628
a80701e6 629static uint64_t partition_min_size(const Context *context, const Partition *p) {
e594a3b1
LP
630 uint64_t sz;
631
994b3031
LP
632 assert(context);
633 assert(p);
634
e594a3b1
LP
635 /* Calculate the disk space we really need at minimum for this partition. If the partition already
636 * exists the current size is what we really need. If it doesn't exist yet refuse to allocate less
fb08381c
LP
637 * than 4K.
638 *
639 * DEFAULT_MIN_SIZE is the default SizeMin= we configure if nothing else is specified. */
e594a3b1
LP
640
641 if (PARTITION_IS_FOREIGN(p)) {
642 /* Don't allow changing size of partitions not managed by us */
643 assert(p->current_size != UINT64_MAX);
644 return p->current_size;
645 }
646
b456191d
DDM
647 if (p->verity == VERITY_SIG)
648 return VERITY_SIG_SIZE;
649
fb08381c 650 sz = p->current_size != UINT64_MAX ? p->current_size : HARD_MIN_SIZE;
757bc2e4 651
170c9823
LP
652 if (!PARTITION_EXISTS(p)) {
653 uint64_t d = 0;
654
889914ef 655 if (p->encrypt != ENCRYPT_OFF)
48a09a8f 656 d += round_up_size(LUKS2_METADATA_KEEP_FREE, context->grain_size);
170c9823
LP
657
658 if (p->copy_blocks_size != UINT64_MAX)
994b3031 659 d += round_up_size(p->copy_blocks_size, context->grain_size);
889914ef 660 else if (p->format || p->encrypt != ENCRYPT_OFF) {
170c9823
LP
661 uint64_t f;
662
663 /* If we shall synthesize a file system, take minimal fs size into account (assumed to be 4K if not known) */
994b3031
LP
664 f = p->format ? round_up_size(minimal_size_by_fs_name(p->format), context->grain_size) : UINT64_MAX;
665 d += f == UINT64_MAX ? context->grain_size : f;
170c9823
LP
666 }
667
668 if (d > sz)
669 sz = d;
670 }
757bc2e4 671
994b3031 672 return MAX(round_up_size(p->size_min != UINT64_MAX ? p->size_min : DEFAULT_MIN_SIZE, context->grain_size), sz);
e594a3b1
LP
673}
674
994b3031
LP
675static uint64_t partition_max_size(const Context *context, const Partition *p) {
676 uint64_t sm;
677
e594a3b1
LP
678 /* Calculate how large the partition may become at max. This is generally the configured maximum
679 * size, except when it already exists and is larger than that. In that case it's the existing size,
680 * since we never want to shrink partitions. */
681
994b3031
LP
682 assert(context);
683 assert(p);
684
e594a3b1
LP
685 if (PARTITION_IS_FOREIGN(p)) {
686 /* Don't allow changing size of partitions not managed by us */
687 assert(p->current_size != UINT64_MAX);
688 return p->current_size;
689 }
690
b456191d
DDM
691 if (p->verity == VERITY_SIG)
692 return VERITY_SIG_SIZE;
693
822d9b9a
YW
694 if (p->size_max == UINT64_MAX)
695 return UINT64_MAX;
696
994b3031
LP
697 sm = round_down_size(p->size_max, context->grain_size);
698
e594a3b1 699 if (p->current_size != UINT64_MAX)
b0fbf90b 700 sm = MAX(p->current_size, sm);
e594a3b1 701
b0fbf90b 702 return MAX(partition_min_size(context, p), sm);
e594a3b1
LP
703}
704
a801bb01
YW
705static uint64_t partition_min_padding(const Partition *p) {
706 assert(p);
707 return p->padding_min != UINT64_MAX ? p->padding_min : 0;
708}
709
710static uint64_t partition_max_padding(const Partition *p) {
711 assert(p);
712 return p->padding_max;
713}
714
994b3031 715static uint64_t partition_min_size_with_padding(Context *context, const Partition *p) {
e594a3b1
LP
716 uint64_t sz;
717
718 /* Calculate the disk space we need for this partition plus any free space coming after it. This
719 * takes user configured padding into account as well as any additional whitespace needed to align
720 * the next partition to 4K again. */
721
994b3031
LP
722 assert(context);
723 assert(p);
724
a801bb01 725 sz = partition_min_size(context, p) + partition_min_padding(p);
e594a3b1
LP
726
727 if (PARTITION_EXISTS(p)) {
728 /* If the partition wasn't aligned, add extra space so that any we might add will be aligned */
729 assert(p->offset != UINT64_MAX);
994b3031 730 return round_up_size(p->offset + sz, context->grain_size) - p->offset;
e594a3b1
LP
731 }
732
733 /* If this is a new partition we'll place it aligned, hence we just need to round up the required size here */
994b3031 734 return round_up_size(sz, context->grain_size);
e594a3b1
LP
735}
736
737static uint64_t free_area_available(const FreeArea *a) {
738 assert(a);
739
740 /* Determines how much of this free area is not allocated yet */
741
742 assert(a->size >= a->allocated);
743 return a->size - a->allocated;
744}
745
58b06ac1 746static uint64_t free_area_current_end(Context *context, const FreeArea *a) {
994b3031
LP
747 assert(context);
748 assert(a);
749
58b06ac1
YW
750 if (!a->after)
751 return free_area_available(a);
e594a3b1 752
58b06ac1
YW
753 assert(a->after->offset != UINT64_MAX);
754 assert(a->after->current_size != UINT64_MAX);
e594a3b1 755
58b06ac1
YW
756 /* Calculate where the free area ends, based on the offset of the partition preceding it. */
757 return round_up_size(a->after->offset + a->after->current_size, context->grain_size) + free_area_available(a);
758}
e594a3b1 759
58b06ac1
YW
760static uint64_t free_area_min_end(Context *context, const FreeArea *a) {
761 assert(context);
762 assert(a);
e594a3b1 763
58b06ac1
YW
764 if (!a->after)
765 return 0;
e594a3b1 766
58b06ac1
YW
767 assert(a->after->offset != UINT64_MAX);
768 assert(a->after->current_size != UINT64_MAX);
1052a114 769
58b06ac1
YW
770 /* Calculate where the partition would end when we give it as much as it needs. */
771 return round_up_size(a->after->offset + partition_min_size_with_padding(context, a->after), context->grain_size);
772}
773
774static uint64_t free_area_available_for_new_partitions(Context *context, const FreeArea *a) {
775 assert(context);
776 assert(a);
777
778 /* Similar to free_area_available(), but takes into account that the required size and padding of the
779 * preceding partition is honoured. */
e594a3b1 780
58b06ac1 781 return LESS_BY(free_area_current_end(context, a), free_area_min_end(context, a));
e594a3b1
LP
782}
783
994b3031
LP
784static int free_area_compare(FreeArea *const *a, FreeArea *const*b, Context *context) {
785 assert(context);
786
787 return CMP(free_area_available_for_new_partitions(context, *a),
788 free_area_available_for_new_partitions(context, *b));
e594a3b1
LP
789}
790
994b3031
LP
791static uint64_t charge_size(Context *context, uint64_t total, uint64_t amount) {
792 assert(context);
e594a3b1 793 /* Subtract the specified amount from total, rounding up to multiple of 4K if there's room */
184cf99a 794 assert(amount <= total);
994b3031 795 return LESS_BY(total, round_up_size(amount, context->grain_size));
e594a3b1
LP
796}
797
798static uint64_t charge_weight(uint64_t total, uint64_t amount) {
799 assert(amount <= total);
800 return total - amount;
801}
802
14a4c4ed 803static bool context_allocate_partitions(Context *context, uint64_t *ret_largest_free_area) {
e594a3b1
LP
804 assert(context);
805
f39cf264
YW
806 /* This may be called multiple times. Reset previous assignments. */
807 for (size_t i = 0; i < context->n_free_areas; i++)
808 context->free_areas[i]->allocated = 0;
809
14a4c4ed 810 /* Sort free areas by size, putting smallest first */
994b3031 811 typesafe_qsort_r(context->free_areas, context->n_free_areas, free_area_compare, context);
e594a3b1 812
14a4c4ed
LP
813 /* In any case return size of the largest free area (i.e. not the size of all free areas
814 * combined!) */
815 if (ret_largest_free_area)
816 *ret_largest_free_area =
817 context->n_free_areas == 0 ? 0 :
994b3031 818 free_area_available_for_new_partitions(context, context->free_areas[context->n_free_areas-1]);
14a4c4ed 819
cdbcc339
YW
820 /* Check that each existing partition can fit its area. */
821 for (size_t i = 0; i < context->n_free_areas; i++)
822 if (free_area_current_end(context, context->free_areas[i]) <
823 free_area_min_end(context, context->free_areas[i]))
824 return false;
825
14a4c4ed 826 /* A simple first-fit algorithm. We return true if we can fit the partitions in, otherwise false. */
e594a3b1
LP
827 LIST_FOREACH(partitions, p, context->partitions) {
828 bool fits = false;
829 uint64_t required;
830 FreeArea *a = NULL;
831
832 /* Skip partitions we already dropped or that already exist */
833 if (p->dropped || PARTITION_EXISTS(p))
834 continue;
835
e594a3b1 836 /* How much do we need to fit? */
994b3031
LP
837 required = partition_min_size_with_padding(context, p);
838 assert(required % context->grain_size == 0);
e594a3b1
LP
839
840 for (size_t i = 0; i < context->n_free_areas; i++) {
841 a = context->free_areas[i];
842
994b3031 843 if (free_area_available_for_new_partitions(context, a) >= required) {
e594a3b1
LP
844 fits = true;
845 break;
846 }
847 }
848
849 if (!fits)
850 return false; /* 😢 Oh no! We can't fit this partition into any free area! */
851
852 /* Assign the partition to this free area */
853 p->allocated_to_area = a;
854
855 /* Budget the minimal partition size */
856 a->allocated += required;
857 }
858
859 return true;
860}
861
862static int context_sum_weights(Context *context, FreeArea *a, uint64_t *ret) {
863 uint64_t weight_sum = 0;
e594a3b1
LP
864
865 assert(context);
866 assert(a);
867 assert(ret);
868
869 /* Determine the sum of the weights of all partitions placed in or before the specified free area */
870
871 LIST_FOREACH(partitions, p, context->partitions) {
872 if (p->padding_area != a && p->allocated_to_area != a)
873 continue;
874
875 if (p->weight > UINT64_MAX - weight_sum)
876 goto overflow_sum;
877 weight_sum += p->weight;
878
879 if (p->padding_weight > UINT64_MAX - weight_sum)
880 goto overflow_sum;
881 weight_sum += p->padding_weight;
882 }
883
884 *ret = weight_sum;
885 return 0;
886
887overflow_sum:
da890466 888 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Combined weight of partition exceeds unsigned 64-bit range, refusing.");
e594a3b1
LP
889}
890
0245e15a 891static uint64_t scale_by_weight(uint64_t value, uint64_t weight, uint64_t weight_sum) {
e594a3b1 892 assert(weight_sum >= weight);
e594a3b1 893
0245e15a
YW
894 for (;;) {
895 if (weight == 0)
896 return 0;
897 if (weight == weight_sum)
898 return value;
899 if (value <= UINT64_MAX / weight)
900 return value * weight / weight_sum;
901
902 /* Rescale weight and weight_sum to make not the calculation overflow. To satisfy the
903 * following conditions, 'weight_sum' is rounded up but 'weight' is rounded down:
904 * - the sum of scale_by_weight() for all weights must not be larger than the input value,
905 * - scale_by_weight() must not be larger than the ideal value (i.e. calculated with uint128_t). */
906 weight_sum = DIV_ROUND_UP(weight_sum, 2);
907 weight /= 2;
e594a3b1 908 }
e594a3b1
LP
909}
910
911typedef enum GrowPartitionPhase {
bf99aed6
YW
912 /* The zeroth phase: do not touch foreign partitions (i.e. those we don't manage). */
913 PHASE_FOREIGN,
914
e594a3b1
LP
915 /* The first phase: we charge partitions which need more (according to constraints) than their weight-based share. */
916 PHASE_OVERCHARGE,
917
918 /* The second phase: we charge partitions which need less (according to constraints) than their weight-based share. */
919 PHASE_UNDERCHARGE,
920
921 /* The third phase: we distribute what remains among the remaining partitions, according to the weights */
922 PHASE_DISTRIBUTE,
ae0613c6
LP
923
924 _GROW_PARTITION_PHASE_MAX,
e594a3b1
LP
925} GrowPartitionPhase;
926
0245e15a 927static bool context_grow_partitions_phase(
e594a3b1
LP
928 Context *context,
929 FreeArea *a,
930 GrowPartitionPhase phase,
931 uint64_t *span,
932 uint64_t *weight_sum) {
933
2a503ad2
YW
934 bool try_again = false;
935
e594a3b1
LP
936 assert(context);
937 assert(a);
0245e15a
YW
938 assert(span);
939 assert(weight_sum);
e594a3b1
LP
940
941 /* Now let's look at the intended weights and adjust them taking the minimum space assignments into
942 * account. i.e. if a partition has a small weight but a high minimum space value set it should not
943 * get any additional room from the left-overs. Similar, if two partitions have the same weight they
944 * should get the same space if possible, even if one has a smaller minimum size than the other. */
945 LIST_FOREACH(partitions, p, context->partitions) {
946
947 /* Look only at partitions associated with this free area, i.e. immediately
162392b7 948 * preceding it, or allocated into it */
e594a3b1
LP
949 if (p->allocated_to_area != a && p->padding_area != a)
950 continue;
951
952 if (p->new_size == UINT64_MAX) {
e594a3b1 953 uint64_t share, rsz, xsz;
2a503ad2 954 bool charge = false;
e594a3b1
LP
955
956 /* Calculate how much this space this partition needs if everyone would get
957 * the weight based share */
0245e15a 958 share = scale_by_weight(*span, p->weight, *weight_sum);
e594a3b1 959
994b3031
LP
960 rsz = partition_min_size(context, p);
961 xsz = partition_max_size(context, p);
e594a3b1 962
bf99aed6
YW
963 if (phase == PHASE_FOREIGN && PARTITION_IS_FOREIGN(p)) {
964 /* Never change of foreign partitions (i.e. those we don't manage) */
965
966 p->new_size = p->current_size;
967 charge = true;
968
969 } else if (phase == PHASE_OVERCHARGE && rsz > share) {
e594a3b1
LP
970 /* This partition needs more than its calculated share. Let's assign
971 * it that, and take this partition out of all calculations and start
972 * again. */
973
974 p->new_size = rsz;
975 charge = try_again = true;
976
822d9b9a 977 } else if (phase == PHASE_UNDERCHARGE && xsz < share) {
e594a3b1
LP
978 /* This partition accepts less than its calculated
979 * share. Let's assign it that, and take this partition out
980 * of all calculations and start again. */
981
982 p->new_size = xsz;
983 charge = try_again = true;
984
985 } else if (phase == PHASE_DISTRIBUTE) {
986 /* This partition can accept its calculated share. Let's
987 * assign it. There's no need to restart things here since
988 * assigning this shouldn't impact the shares of the other
989 * partitions. */
990
d7c46b5e
YW
991 assert(share >= rsz);
992 p->new_size = CLAMP(round_down_size(share, context->grain_size), rsz, xsz);
e594a3b1
LP
993 charge = true;
994 }
995
996 if (charge) {
994b3031 997 *span = charge_size(context, *span, p->new_size);
e594a3b1
LP
998 *weight_sum = charge_weight(*weight_sum, p->weight);
999 }
e594a3b1
LP
1000 }
1001
1002 if (p->new_padding == UINT64_MAX) {
a801bb01 1003 uint64_t share, rsz, xsz;
2a503ad2 1004 bool charge = false;
e594a3b1 1005
0245e15a 1006 share = scale_by_weight(*span, p->padding_weight, *weight_sum);
e594a3b1 1007
a801bb01
YW
1008 rsz = partition_min_padding(p);
1009 xsz = partition_max_padding(p);
1010
1011 if (phase == PHASE_OVERCHARGE && rsz > share) {
1012 p->new_padding = rsz;
e594a3b1 1013 charge = try_again = true;
a801bb01
YW
1014 } else if (phase == PHASE_UNDERCHARGE && xsz < share) {
1015 p->new_padding = xsz;
e594a3b1
LP
1016 charge = try_again = true;
1017 } else if (phase == PHASE_DISTRIBUTE) {
d7c46b5e
YW
1018 assert(share >= rsz);
1019 p->new_padding = CLAMP(round_down_size(share, context->grain_size), rsz, xsz);
e594a3b1
LP
1020 charge = true;
1021 }
1022
1023 if (charge) {
994b3031 1024 *span = charge_size(context, *span, p->new_padding);
e594a3b1
LP
1025 *weight_sum = charge_weight(*weight_sum, p->padding_weight);
1026 }
e594a3b1
LP
1027 }
1028 }
1029
2a503ad2 1030 return !try_again;
e594a3b1
LP
1031}
1032
19903a43
YW
1033static void context_grow_partition_one(Context *context, FreeArea *a, Partition *p, uint64_t *span) {
1034 uint64_t m;
1035
1036 assert(context);
1037 assert(a);
1038 assert(p);
1039 assert(span);
1040
1041 if (*span == 0)
1042 return;
1043
1044 if (p->allocated_to_area != a)
1045 return;
1046
1047 if (PARTITION_IS_FOREIGN(p))
1048 return;
1049
1050 assert(p->new_size != UINT64_MAX);
1051
1052 /* Calculate new size and align. */
1053 m = round_down_size(p->new_size + *span, context->grain_size);
1054 /* But ensure this doesn't shrink the size. */
1055 m = MAX(m, p->new_size);
1056 /* And ensure this doesn't exceed the maximum size. */
1057 m = MIN(m, partition_max_size(context, p));
1058
1059 assert(m >= p->new_size);
1060
1061 *span = charge_size(context, *span, m - p->new_size);
1062 p->new_size = m;
1063}
1064
e594a3b1
LP
1065static int context_grow_partitions_on_free_area(Context *context, FreeArea *a) {
1066 uint64_t weight_sum = 0, span;
1067 int r;
1068
1069 assert(context);
1070 assert(a);
1071
1072 r = context_sum_weights(context, a, &weight_sum);
1073 if (r < 0)
1074 return r;
1075
1076 /* Let's calculate the total area covered by this free area and the partition before it */
1077 span = a->size;
1078 if (a->after) {
1079 assert(a->after->offset != UINT64_MAX);
1080 assert(a->after->current_size != UINT64_MAX);
1081
994b3031 1082 span += round_up_size(a->after->offset + a->after->current_size, context->grain_size) - a->after->offset;
e594a3b1
LP
1083 }
1084
0245e15a
YW
1085 for (GrowPartitionPhase phase = 0; phase < _GROW_PARTITION_PHASE_MAX;)
1086 if (context_grow_partitions_phase(context, a, phase, &span, &weight_sum))
1087 phase++; /* go to the next phase */
e594a3b1 1088
162392b7 1089 /* We still have space left over? Donate to preceding partition if we have one */
19903a43
YW
1090 if (span > 0 && a->after)
1091 context_grow_partition_one(context, a, a->after, &span);
e594a3b1 1092
162392b7 1093 /* What? Even still some space left (maybe because there was no preceding partition, or it had a
e594a3b1 1094 * size limit), then let's donate it to whoever wants it. */
03677889 1095 if (span > 0)
e594a3b1 1096 LIST_FOREACH(partitions, p, context->partitions) {
19903a43 1097 context_grow_partition_one(context, a, p, &span);
e594a3b1
LP
1098 if (span == 0)
1099 break;
1100 }
e594a3b1 1101
162392b7 1102 /* Yuck, still no one? Then make it padding */
e594a3b1
LP
1103 if (span > 0 && a->after) {
1104 assert(a->after->new_padding != UINT64_MAX);
1105 a->after->new_padding += span;
1106 }
1107
1108 return 0;
1109}
1110
1111static int context_grow_partitions(Context *context) {
e594a3b1
LP
1112 int r;
1113
1114 assert(context);
1115
1116 for (size_t i = 0; i < context->n_free_areas; i++) {
1117 r = context_grow_partitions_on_free_area(context, context->free_areas[i]);
1118 if (r < 0)
1119 return r;
1120 }
1121
1122 /* All existing partitions that have no free space after them can't change size */
1123 LIST_FOREACH(partitions, p, context->partitions) {
1124 if (p->dropped)
1125 continue;
1126
1127 if (!PARTITION_EXISTS(p) || p->padding_area) {
1128 /* The algorithm above must have initialized this already */
1129 assert(p->new_size != UINT64_MAX);
1130 continue;
1131 }
1132
1133 assert(p->new_size == UINT64_MAX);
1134 p->new_size = p->current_size;
1135
1136 assert(p->new_padding == UINT64_MAX);
1137 p->new_padding = p->current_padding;
1138 }
1139
1140 return 0;
1141}
1142
00428745 1143static uint64_t find_first_unused_partno(Context *context) {
e594a3b1 1144 uint64_t partno = 0;
e594a3b1
LP
1145
1146 assert(context);
1147
5f59807d
DDM
1148 for (partno = 0;; partno++) {
1149 bool found = false;
1150 LIST_FOREACH(partitions, p, context->partitions)
1151 if (p->partno != UINT64_MAX && p->partno == partno)
1152 found = true;
1153 if (!found)
1154 break;
e594a3b1
LP
1155 }
1156
00428745
DDM
1157 return partno;
1158}
1159
1160static void context_place_partitions(Context *context) {
1161
1162 assert(context);
1163
e594a3b1
LP
1164 for (size_t i = 0; i < context->n_free_areas; i++) {
1165 FreeArea *a = context->free_areas[i];
2ea7eb00
FS
1166 _unused_ uint64_t left;
1167 uint64_t start;
e594a3b1
LP
1168
1169 if (a->after) {
1170 assert(a->after->offset != UINT64_MAX);
1171 assert(a->after->new_size != UINT64_MAX);
1172 assert(a->after->new_padding != UINT64_MAX);
1173
1174 start = a->after->offset + a->after->new_size + a->after->new_padding;
1175 } else
1176 start = context->start;
1177
994b3031 1178 start = round_up_size(start, context->grain_size);
e594a3b1
LP
1179 left = a->size;
1180
1181 LIST_FOREACH(partitions, p, context->partitions) {
1182 if (p->allocated_to_area != a)
1183 continue;
1184
1185 p->offset = start;
00428745 1186 p->partno = find_first_unused_partno(context);
e594a3b1
LP
1187
1188 assert(left >= p->new_size);
1189 start += p->new_size;
1190 left -= p->new_size;
1191
1192 assert(left >= p->new_padding);
1193 start += p->new_padding;
1194 left -= p->new_padding;
1195 }
1196 }
1197}
1198
e594a3b1
LP
1199static int config_parse_type(
1200 const char *unit,
1201 const char *filename,
1202 unsigned line,
1203 const char *section,
1204 unsigned section_line,
1205 const char *lvalue,
1206 int ltype,
1207 const char *rvalue,
1208 void *data,
1209 void *userdata) {
1210
22e932f4 1211 GptPartitionType *type = ASSERT_PTR(data);
e594a3b1
LP
1212 int r;
1213
1214 assert(rvalue);
e594a3b1 1215
22e932f4 1216 r = gpt_partition_type_from_string(rvalue, type);
e594a3b1
LP
1217 if (r < 0)
1218 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse partition type: %s", rvalue);
1219
9786dfe6
DDM
1220 if (arg_architecture >= 0)
1221 *type = gpt_partition_type_override_architecture(*type, arg_architecture);
1222
e594a3b1
LP
1223 return 0;
1224}
1225
1226static int config_parse_label(
1227 const char *unit,
1228 const char *filename,
1229 unsigned line,
1230 const char *section,
1231 unsigned section_line,
1232 const char *lvalue,
1233 int ltype,
1234 const char *rvalue,
1235 void *data,
1236 void *userdata) {
1237
e031166e 1238 _cleanup_free_ char *resolved = NULL;
99534007 1239 char **label = ASSERT_PTR(data);
e594a3b1
LP
1240 int r;
1241
1242 assert(rvalue);
e594a3b1 1243
be9ce018
LP
1244 /* Nota bene: the empty label is a totally valid one. Let's hence not follow our usual rule of
1245 * assigning the empty string to reset to default here, but really accept it as label to set. */
1246
de61a04b 1247 r = specifier_printf(rvalue, GPT_LABEL_MAX, system_and_tmp_specifier_table, arg_root, NULL, &resolved);
e031166e 1248 if (r < 0) {
e459258f 1249 log_syntax(unit, LOG_WARNING, filename, line, r,
e031166e
LP
1250 "Failed to expand specifiers in Label=, ignoring: %s", rvalue);
1251 return 0;
1252 }
1253
1254 if (!utf8_is_valid(resolved)) {
e594a3b1
LP
1255 log_syntax(unit, LOG_WARNING, filename, line, 0,
1256 "Partition label not valid UTF-8, ignoring: %s", rvalue);
1257 return 0;
1258 }
1259
22a0a36e
LP
1260 r = gpt_partition_label_valid(resolved);
1261 if (r < 0) {
1262 log_syntax(unit, LOG_WARNING, filename, line, r,
1263 "Failed to check if string is valid as GPT partition label, ignoring: \"%s\" (from \"%s\")",
1264 resolved, rvalue);
1265 return 0;
1266 }
1267 if (!r) {
e594a3b1 1268 log_syntax(unit, LOG_WARNING, filename, line, 0,
46072ae3
ZJS
1269 "Partition label too long for GPT table, ignoring: \"%s\" (from \"%s\")",
1270 resolved, rvalue);
e594a3b1
LP
1271 return 0;
1272 }
1273
e031166e 1274 free_and_replace(*label, resolved);
e594a3b1
LP
1275 return 0;
1276}
1277
1278static int config_parse_weight(
1279 const char *unit,
1280 const char *filename,
1281 unsigned line,
1282 const char *section,
1283 unsigned section_line,
1284 const char *lvalue,
1285 int ltype,
1286 const char *rvalue,
1287 void *data,
1288 void *userdata) {
1289
f126038f 1290 uint32_t *w = ASSERT_PTR(data), v;
e594a3b1
LP
1291 int r;
1292
1293 assert(rvalue);
e594a3b1
LP
1294
1295 r = safe_atou32(rvalue, &v);
1296 if (r < 0) {
1297 log_syntax(unit, LOG_WARNING, filename, line, r,
1298 "Failed to parse weight value, ignoring: %s", rvalue);
1299 return 0;
1300 }
1301
1302 if (v > 1000U*1000U) {
c8f3d767 1303 log_syntax(unit, LOG_WARNING, filename, line, 0,
e594a3b1
LP
1304 "Weight needs to be in range 0…10000000, ignoring: %" PRIu32, v);
1305 return 0;
1306 }
1307
f126038f 1308 *w = v;
e594a3b1
LP
1309 return 0;
1310}
1311
1312static int config_parse_size4096(
1313 const char *unit,
1314 const char *filename,
1315 unsigned line,
1316 const char *section,
1317 unsigned section_line,
1318 const char *lvalue,
1319 int ltype,
1320 const char *rvalue,
1321 void *data,
1322 void *userdata) {
1323
1324 uint64_t *sz = data, parsed;
1325 int r;
1326
1327 assert(rvalue);
1328 assert(data);
1329
1330 r = parse_size(rvalue, 1024, &parsed);
1331 if (r < 0)
c8f3d767 1332 return log_syntax(unit, LOG_ERR, filename, line, r,
e594a3b1
LP
1333 "Failed to parse size value: %s", rvalue);
1334
1335 if (ltype > 0)
1336 *sz = round_up_size(parsed, 4096);
1337 else if (ltype < 0)
1338 *sz = round_down_size(parsed, 4096);
1339 else
1340 *sz = parsed;
1341
1342 if (*sz != parsed)
e2341b6b
DT
1343 log_syntax(unit, LOG_NOTICE, filename, line, r, "Rounded %s= size %" PRIu64 " %s %" PRIu64 ", a multiple of 4096.",
1344 lvalue, parsed, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), *sz);
e594a3b1
LP
1345
1346 return 0;
1347}
1348
c380047b
MC
1349static int config_parse_block_size(
1350 const char *unit,
1351 const char *filename,
1352 unsigned line,
1353 const char *section,
1354 unsigned section_line,
1355 const char *lvalue,
1356 int ltype,
1357 const char *rvalue,
1358 void *data,
1359 void *userdata) {
1360
1361 uint64_t *blksz = ASSERT_PTR(data), parsed;
1362 int r;
1363
1364 assert(rvalue);
1365
1366 r = parse_size(rvalue, 1024, &parsed);
1367 if (r < 0)
1368 return log_syntax(unit, LOG_ERR, filename, line, r,
1369 "Failed to parse size value: %s", rvalue);
1370
1371 if (parsed < 512 || parsed > 4096)
1372 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
1373 "Value not between 512 and 4096: %s", rvalue);
1374
1375 if (!ISPOWEROF2(parsed))
1376 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
1377 "Value not a power of 2: %s", rvalue);
1378
1379 *blksz = parsed;
1380 return 0;
1381}
1382
53171c04
LP
1383static int config_parse_fstype(
1384 const char *unit,
1385 const char *filename,
1386 unsigned line,
1387 const char *section,
1388 unsigned section_line,
1389 const char *lvalue,
1390 int ltype,
1391 const char *rvalue,
1392 void *data,
1393 void *userdata) {
1394
99534007 1395 char **fstype = ASSERT_PTR(data);
e59049d7 1396 const char *e;
53171c04
LP
1397
1398 assert(rvalue);
53171c04 1399
e59049d7
LP
1400 /* Let's provide an easy way to override the chosen fstype for file system partitions */
1401 e = secure_getenv("SYSTEMD_REPART_OVERRIDE_FSTYPE");
1402 if (e && !streq(rvalue, e)) {
1403 log_syntax(unit, LOG_NOTICE, filename, line, 0,
1404 "Overriding defined file system type '%s' with '%s'.", rvalue, e);
1405 rvalue = e;
1406 }
1407
53171c04
LP
1408 if (!filename_is_valid(rvalue))
1409 return log_syntax(unit, LOG_ERR, filename, line, 0,
1410 "File system type is not valid, refusing: %s", rvalue);
1411
1412 return free_and_strdup_warn(fstype, rvalue);
1413}
1414
8a794850
LP
1415static int config_parse_copy_files(
1416 const char *unit,
1417 const char *filename,
1418 unsigned line,
1419 const char *section,
1420 unsigned section_line,
1421 const char *lvalue,
1422 int ltype,
1423 const char *rvalue,
1424 void *data,
1425 void *userdata) {
1426
1427 _cleanup_free_ char *source = NULL, *buffer = NULL, *resolved_source = NULL, *resolved_target = NULL;
1428 const char *p = rvalue, *target;
de98e6a7 1429 char ***copy_files = ASSERT_PTR(data);
8a794850
LP
1430 int r;
1431
1432 assert(rvalue);
8a794850
LP
1433
1434 r = extract_first_word(&p, &source, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS);
1435 if (r < 0)
1436 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract source path: %s", rvalue);
1437 if (r == 0) {
1438 log_syntax(unit, LOG_WARNING, filename, line, 0, "No argument specified: %s", rvalue);
1439 return 0;
1440 }
1441
1442 r = extract_first_word(&p, &buffer, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS);
1443 if (r < 0)
1444 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract target path: %s", rvalue);
1445 if (r == 0)
1446 target = source; /* No target, then it's the same as the source */
1447 else
1448 target = buffer;
1449
1450 if (!isempty(p))
1451 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), "Too many arguments: %s", rvalue);
1452
de61a04b 1453 r = specifier_printf(source, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_source);
8a794850
LP
1454 if (r < 0) {
1455 log_syntax(unit, LOG_WARNING, filename, line, r,
1456 "Failed to expand specifiers in CopyFiles= source, ignoring: %s", rvalue);
1457 return 0;
1458 }
1459
0ade2213
LP
1460 r = path_simplify_and_warn(resolved_source, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1461 if (r < 0)
8a794850 1462 return 0;
8a794850 1463
de61a04b 1464 r = specifier_printf(target, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_target);
8a794850
LP
1465 if (r < 0) {
1466 log_syntax(unit, LOG_WARNING, filename, line, r,
1467 "Failed to expand specifiers in CopyFiles= target, ignoring: %s", resolved_target);
1468 return 0;
1469 }
1470
0ade2213
LP
1471 r = path_simplify_and_warn(resolved_target, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1472 if (r < 0)
8a794850 1473 return 0;
8a794850 1474
de98e6a7 1475 r = strv_consume_pair(copy_files, TAKE_PTR(resolved_source), TAKE_PTR(resolved_target));
8a794850
LP
1476 if (r < 0)
1477 return log_oom();
1478
1479 return 0;
1480}
1481
a9af8276
DDM
1482static int config_parse_exclude_files(
1483 const char *unit,
1484 const char *filename,
1485 unsigned line,
1486 const char *section,
1487 unsigned section_line,
1488 const char *lvalue,
1489 int ltype,
1490 const char *rvalue,
1491 void *data,
1492 void *userdata) {
1493 _cleanup_free_ char *resolved = NULL;
1494 char ***exclude_files = ASSERT_PTR(data);
1495 int r;
1496
1497 if (isempty(rvalue)) {
1498 *exclude_files = strv_free(*exclude_files);
1499 return 0;
1500 }
1501
1502 r = specifier_printf(rvalue, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved);
1503 if (r < 0) {
1504 log_syntax(unit, LOG_WARNING, filename, line, r,
1505 "Failed to expand specifiers in ExcludeFiles= path, ignoring: %s", rvalue);
1506 return 0;
1507 }
1508
d10eccbd 1509 r = path_simplify_and_warn(resolved, PATH_CHECK_ABSOLUTE|PATH_KEEP_TRAILING_SLASH, unit, filename, line, lvalue);
8a794850 1510 if (r < 0)
a9af8276
DDM
1511 return 0;
1512
1513 if (strv_consume(exclude_files, TAKE_PTR(resolved)) < 0)
8a794850
LP
1514 return log_oom();
1515
1516 return 0;
1517}
1518
5c08da58
LP
1519static int config_parse_copy_blocks(
1520 const char *unit,
1521 const char *filename,
1522 unsigned line,
1523 const char *section,
1524 unsigned section_line,
1525 const char *lvalue,
1526 int ltype,
1527 const char *rvalue,
1528 void *data,
1529 void *userdata) {
1530
1531 _cleanup_free_ char *d = NULL;
99534007 1532 Partition *partition = ASSERT_PTR(data);
5c08da58
LP
1533 int r;
1534
1535 assert(rvalue);
5c08da58
LP
1536
1537 if (isempty(rvalue)) {
1538 partition->copy_blocks_path = mfree(partition->copy_blocks_path);
1539 partition->copy_blocks_auto = false;
1540 return 0;
1541 }
1542
1543 if (streq(rvalue, "auto")) {
1544 partition->copy_blocks_path = mfree(partition->copy_blocks_path);
1545 partition->copy_blocks_auto = true;
585c5c75 1546 partition->copy_blocks_root = arg_root;
5c08da58
LP
1547 return 0;
1548 }
1549
de61a04b 1550 r = specifier_printf(rvalue, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &d);
5c08da58
LP
1551 if (r < 0) {
1552 log_syntax(unit, LOG_WARNING, filename, line, r,
1553 "Failed to expand specifiers in CopyBlocks= source path, ignoring: %s", rvalue);
1554 return 0;
1555 }
1556
1557 r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1558 if (r < 0)
1559 return 0;
1560
1561 free_and_replace(partition->copy_blocks_path, d);
1562 partition->copy_blocks_auto = false;
585c5c75 1563 partition->copy_blocks_root = arg_root;
5c08da58
LP
1564 return 0;
1565}
1566
d83d8048
LP
1567static int config_parse_make_dirs(
1568 const char *unit,
1569 const char *filename,
1570 unsigned line,
1571 const char *section,
1572 unsigned section_line,
1573 const char *lvalue,
1574 int ltype,
1575 const char *rvalue,
1576 void *data,
1577 void *userdata) {
1578
440f805c 1579 char ***sv = ASSERT_PTR(data);
99534007 1580 const char *p = ASSERT_PTR(rvalue);
d83d8048
LP
1581 int r;
1582
d83d8048
LP
1583 for (;;) {
1584 _cleanup_free_ char *word = NULL, *d = NULL;
1585
1586 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
1587 if (r == -ENOMEM)
1588 return log_oom();
1589 if (r < 0) {
1590 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
1591 return 0;
1592 }
1593 if (r == 0)
1594 return 0;
1595
de61a04b 1596 r = specifier_printf(word, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &d);
d83d8048
LP
1597 if (r < 0) {
1598 log_syntax(unit, LOG_WARNING, filename, line, r,
1599 "Failed to expand specifiers in MakeDirectories= parameter, ignoring: %s", word);
1600 continue;
1601 }
1602
1603 r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1604 if (r < 0)
1605 continue;
1606
440f805c 1607 r = strv_consume(sv, TAKE_PTR(d));
d83d8048
LP
1608 if (r < 0)
1609 return log_oom();
1610 }
1611}
1612
889914ef
LP
1613static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_encrypt, encrypt_mode, EncryptMode, ENCRYPT_OFF, "Invalid encryption mode");
1614
e73309c5
LP
1615static int config_parse_gpt_flags(
1616 const char *unit,
1617 const char *filename,
1618 unsigned line,
1619 const char *section,
1620 unsigned section_line,
1621 const char *lvalue,
1622 int ltype,
1623 const char *rvalue,
1624 void *data,
1625 void *userdata) {
1626
99534007 1627 uint64_t *gpt_flags = ASSERT_PTR(data);
e73309c5
LP
1628 int r;
1629
1630 assert(rvalue);
e73309c5
LP
1631
1632 r = safe_atou64(rvalue, gpt_flags);
1633 if (r < 0) {
1634 log_syntax(unit, LOG_WARNING, filename, line, r,
1635 "Failed to parse Flags= value, ignoring: %s", rvalue);
1636 return 0;
1637 }
1638
1639 return 0;
1640}
1641
11749b61
DDM
1642static int config_parse_uuid(
1643 const char *unit,
1644 const char *filename,
1645 unsigned line,
1646 const char *section,
1647 unsigned section_line,
1648 const char *lvalue,
1649 int ltype,
1650 const char *rvalue,
1651 void *data,
1652 void *userdata) {
1653
1654 Partition *partition = ASSERT_PTR(data);
1655 int r;
1656
1657 if (isempty(rvalue)) {
1658 partition->new_uuid = SD_ID128_NULL;
1659 partition->new_uuid_is_set = false;
1660 return 0;
1661 }
1662
1663 if (streq(rvalue, "null")) {
1664 partition->new_uuid = SD_ID128_NULL;
1665 partition->new_uuid_is_set = true;
1666 return 0;
1667 }
1668
1669 r = sd_id128_from_string(rvalue, &partition->new_uuid);
1670 if (r < 0) {
da890466 1671 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse 128-bit ID/UUID, ignoring: %s", rvalue);
11749b61
DDM
1672 return 0;
1673 }
1674
1675 partition->new_uuid_is_set = true;
1676
1677 return 0;
1678}
1679
b5b7879a 1680static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_verity, verity_mode, VerityMode, VERITY_OFF, "Invalid verity mode");
5c33b686 1681static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_minimize, minimize_mode, MinimizeMode, MINIMIZE_OFF, "Invalid minimize mode");
b5b7879a 1682
39fc0174 1683static int partition_read_definition(Partition *p, const char *path, const char *const *conf_file_dirs) {
e594a3b1
LP
1684
1685 ConfigTableItem table[] = {
c380047b
MC
1686 { "Partition", "Type", config_parse_type, 0, &p->type },
1687 { "Partition", "Label", config_parse_label, 0, &p->new_label },
1688 { "Partition", "UUID", config_parse_uuid, 0, p },
1689 { "Partition", "Priority", config_parse_int32, 0, &p->priority },
1690 { "Partition", "Weight", config_parse_weight, 0, &p->weight },
1691 { "Partition", "PaddingWeight", config_parse_weight, 0, &p->padding_weight },
6563aede
DDM
1692 { "Partition", "SizeMinBytes", config_parse_size4096, -1, &p->size_min },
1693 { "Partition", "SizeMaxBytes", config_parse_size4096, 1, &p->size_max },
1694 { "Partition", "PaddingMinBytes", config_parse_size4096, -1, &p->padding_min },
1695 { "Partition", "PaddingMaxBytes", config_parse_size4096, 1, &p->padding_max },
c380047b
MC
1696 { "Partition", "FactoryReset", config_parse_bool, 0, &p->factory_reset },
1697 { "Partition", "CopyBlocks", config_parse_copy_blocks, 0, p },
1698 { "Partition", "Format", config_parse_fstype, 0, &p->format },
1699 { "Partition", "CopyFiles", config_parse_copy_files, 0, &p->copy_files },
1700 { "Partition", "ExcludeFiles", config_parse_exclude_files, 0, &p->exclude_files_source },
1701 { "Partition", "ExcludeFilesTarget", config_parse_exclude_files, 0, &p->exclude_files_target },
1702 { "Partition", "MakeDirectories", config_parse_make_dirs, 0, &p->make_directories },
1703 { "Partition", "Encrypt", config_parse_encrypt, 0, &p->encrypt },
1704 { "Partition", "Verity", config_parse_verity, 0, &p->verity },
1705 { "Partition", "VerityMatchKey", config_parse_string, 0, &p->verity_match_key },
1706 { "Partition", "Flags", config_parse_gpt_flags, 0, &p->gpt_flags },
1707 { "Partition", "ReadOnly", config_parse_tristate, 0, &p->read_only },
1708 { "Partition", "NoAuto", config_parse_tristate, 0, &p->no_auto },
1709 { "Partition", "GrowFileSystem", config_parse_tristate, 0, &p->growfs },
1710 { "Partition", "SplitName", config_parse_string, 0, &p->split_name_format },
1711 { "Partition", "Minimize", config_parse_minimize, 0, &p->minimize },
1712 { "Partition", "Subvolumes", config_parse_make_dirs, 0, &p->subvolumes },
1713 { "Partition", "VerityDataBlockSizeBytes", config_parse_block_size, 0, &p->verity_data_block_size },
1714 { "Partition", "VerityHashBlockSizeBytes", config_parse_block_size, 0, &p->verity_hash_block_size },
e594a3b1
LP
1715 {}
1716 };
1717 int r;
39fc0174
RP
1718 _cleanup_free_ char *filename = NULL;
1719 const char* dropin_dirname;
e594a3b1 1720
39fc0174
RP
1721 r = path_extract_filename(path, &filename);
1722 if (r < 0)
bef69ae8 1723 return log_error_errno(r, "Failed to extract filename from path '%s': %m", path);
39fc0174
RP
1724
1725 dropin_dirname = strjoina(filename, ".d");
1726
1727 r = config_parse_many(
1728 STRV_MAKE_CONST(path),
1729 conf_file_dirs,
1730 dropin_dirname,
34f2fd50 1731 arg_definitions ? NULL : arg_root,
39fc0174
RP
1732 "Partition\0",
1733 config_item_table_lookup, table,
1734 CONFIG_PARSE_WARN,
1735 p,
1736 NULL,
1737 &p->drop_in_files);
e594a3b1
LP
1738 if (r < 0)
1739 return r;
1740
c74d50ff 1741 if (partition_type_exclude(&p->type))
7d505753
DDM
1742 return 0;
1743
e594a3b1
LP
1744 if (p->size_min != UINT64_MAX && p->size_max != UINT64_MAX && p->size_min > p->size_max)
1745 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1746 "SizeMinBytes= larger than SizeMaxBytes=, refusing.");
1747
1748 if (p->padding_min != UINT64_MAX && p->padding_max != UINT64_MAX && p->padding_min > p->padding_max)
1749 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1750 "PaddingMinBytes= larger than PaddingMaxBytes=, refusing.");
1751
22e932f4 1752 if (sd_id128_is_null(p->type.uuid))
e594a3b1
LP
1753 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1754 "Type= not defined, refusing.");
1755
86320e62
DDM
1756 if ((p->copy_blocks_path || p->copy_blocks_auto) &&
1757 (p->format || !strv_isempty(p->copy_files) || !strv_isempty(p->make_directories)))
1758 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1759 "Format=/CopyFiles=/MakeDirectories= and CopyBlocks= cannot be combined, refusing.");
1760
d83d8048 1761 if ((!strv_isempty(p->copy_files) || !strv_isempty(p->make_directories)) && streq_ptr(p->format, "swap"))
8a794850
LP
1762 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1763 "Format=swap and CopyFiles= cannot be combined, refusing.");
1764
973d6be4
DDM
1765 if (!p->format) {
1766 const char *format = NULL;
1767
1768 if (!strv_isempty(p->copy_files) || !strv_isempty(p->make_directories) || (p->encrypt != ENCRYPT_OFF && !(p->copy_blocks_path || p->copy_blocks_auto)))
1769 /* Pick "vfat" as file system for esp and xbootldr partitions, otherwise default to "ext4". */
1770 format = IN_SET(p->type.designator, PARTITION_ESP, PARTITION_XBOOTLDR) ? "vfat" : "ext4";
1771 else if (p->type.designator == PARTITION_SWAP)
1772 format = "swap";
1773
1774 if (format) {
1775 p->format = strdup(format);
1776 if (!p->format)
1777 return log_oom();
1778 }
8a794850
LP
1779 }
1780
5eef7047 1781 if (p->minimize != MINIMIZE_OFF && !p->format && p->verity != VERITY_HASH)
c4a87b76 1782 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
5eef7047 1783 "Minimize= can only be enabled if Format= or Verity=hash are set");
c4a87b76 1784
5eef7047 1785 if (p->minimize == MINIMIZE_BEST && (p->format && !fstype_is_ro(p->format)) && p->verity != VERITY_HASH)
5c33b686 1786 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
5eef7047 1787 "Minimize=best can only be used with read-only filesystems or Verity=hash");
5c33b686 1788
6d6cefad
DDM
1789 if ((!strv_isempty(p->copy_files) || !strv_isempty(p->make_directories)) && !mkfs_supports_root_option(p->format) && geteuid() != 0)
1790 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EPERM),
1791 "Need to be root to populate %s filesystems with CopyFiles=/MakeDirectories=",
1792 p->format);
1793
0eb23798
DDM
1794 if (p->format && fstype_is_ro(p->format) && strv_isempty(p->copy_files) && strv_isempty(p->make_directories))
1795 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1796 "Cannot format %s filesystem without source files, refusing", p->format);
1797
b5b7879a
DDM
1798 if (p->verity != VERITY_OFF || p->encrypt != ENCRYPT_OFF) {
1799 r = dlopen_cryptsetup();
1800 if (r < 0)
1801 return log_syntax(NULL, LOG_ERR, path, 1, r,
1802 "libcryptsetup not found, Verity=/Encrypt= are not supported: %m");
1803 }
1804
1805 if (p->verity != VERITY_OFF && !p->verity_match_key)
1806 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1807 "VerityMatchKey= must be set if Verity=%s", verity_mode_to_string(p->verity));
1808
1809 if (p->verity == VERITY_OFF && p->verity_match_key)
1810 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1811 "VerityMatchKey= can only be set if Verity= is not \"%s\"",
1812 verity_mode_to_string(p->verity));
1813
b456191d
DDM
1814 if (IN_SET(p->verity, VERITY_HASH, VERITY_SIG) &&
1815 (p->copy_files || p->copy_blocks_path || p->copy_blocks_auto || p->format || p->make_directories))
b5b7879a
DDM
1816 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1817 "CopyBlocks=/CopyFiles=/Format=/MakeDirectories= cannot be used with Verity=%s",
1818 verity_mode_to_string(p->verity));
1819
1820 if (p->verity != VERITY_OFF && p->encrypt != ENCRYPT_OFF)
1821 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1822 "Encrypting verity hash/data partitions is not supported");
1823
b456191d
DDM
1824 if (p->verity == VERITY_SIG && !arg_private_key)
1825 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1826 "Verity signature partition requested but no private key provided (--private-key=)");
1827
1828 if (p->verity == VERITY_SIG && !arg_certificate)
1829 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
ba4a5eff 1830 "Verity signature partition requested but no PEM certificate provided (--certificate=)");
b456191d
DDM
1831
1832 if (p->verity == VERITY_SIG && (p->size_min != UINT64_MAX || p->size_max != UINT64_MAX))
1833 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1834 "SizeMinBytes=/SizeMaxBytes= cannot be used with Verity=%s",
1835 verity_mode_to_string(p->verity));
1836
ffd9d75c 1837 if (!strv_isempty(p->subvolumes) && arg_offline > 0)
440f805c 1838 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EOPNOTSUPP),
ffd9d75c 1839 "Subvolumes= cannot be used with --offline=yes");
440f805c 1840
e73309c5 1841 /* Verity partitions are read only, let's imply the RO flag hence, unless explicitly configured otherwise. */
2208d492
DDM
1842 if ((IN_SET(p->type.designator,
1843 PARTITION_ROOT_VERITY,
ade85cd5 1844 PARTITION_USR_VERITY) || p->verity == VERITY_DATA) && p->read_only < 0)
e73309c5
LP
1845 p->read_only = true;
1846
1c41c1dc 1847 /* Default to "growfs" on, unless read-only */
22e932f4 1848 if (gpt_partition_type_knows_growfs(p->type) &&
1c41c1dc
LP
1849 p->read_only <= 0)
1850 p->growfs = true;
1851
4cee8333
DDM
1852 if (!p->split_name_format) {
1853 char *s = strdup("%t");
1854 if (!s)
1855 return log_oom();
1856
1857 p->split_name_format = s;
1858 } else if (streq(p->split_name_format, "-"))
1859 p->split_name_format = mfree(p->split_name_format);
1860
7d505753 1861 return 1;
e594a3b1
LP
1862}
1863
b5b7879a
DDM
1864static int find_verity_sibling(Context *context, Partition *p, VerityMode mode, Partition **ret) {
1865 Partition *s = NULL;
1866
1867 assert(p);
1868 assert(p->verity != VERITY_OFF);
1869 assert(p->verity_match_key);
1870 assert(mode != VERITY_OFF);
1871 assert(p->verity != mode);
1872 assert(ret);
1873
1874 /* Try to find the matching sibling partition of the given type for a verity partition. For a data
af3d3873
YW
1875 * partition, this is the corresponding hash partition with the same verity name (and vice versa for
1876 * the hash partition). */
b5b7879a
DDM
1877
1878 LIST_FOREACH(partitions, q, context->partitions) {
1879 if (p == q)
1880 continue;
1881
1882 if (q->verity != mode)
1883 continue;
1884
1885 assert(q->verity_match_key);
1886
1887 if (!streq(p->verity_match_key, q->verity_match_key))
1888 continue;
1889
1890 if (s)
1891 return -ENOTUNIQ;
1892
1893 s = q;
1894 }
1895
1896 if (!s)
1897 return -ENXIO;
1898
1899 *ret = s;
1900
1901 return 0;
1902}
1903
3cdcf4e2 1904static int context_open_and_lock_backing_fd(const char *node, int operation, int *backing_fd) {
1e46985a
DDM
1905 _cleanup_close_ int fd = -EBADF;
1906
1907 assert(node);
1908 assert(backing_fd);
1909
1910 if (*backing_fd >= 0)
1911 return 0;
1912
1913 fd = open(node, O_RDONLY|O_CLOEXEC);
1914 if (fd < 0)
1915 return log_error_errno(errno, "Failed to open device '%s': %m", node);
1916
1917 /* Tell udev not to interfere while we are processing the device */
3cdcf4e2 1918 if (flock(fd, operation) < 0)
1e46985a
DDM
1919 return log_error_errno(errno, "Failed to lock device '%s': %m", node);
1920
1921 log_debug("Device %s opened and locked.", node);
1922 *backing_fd = TAKE_FD(fd);
1923 return 1;
1924}
1925
1926static int determine_current_padding(
1927 struct fdisk_context *c,
1928 struct fdisk_table *t,
1929 struct fdisk_partition *p,
1930 uint64_t secsz,
1931 uint64_t grainsz,
1932 uint64_t *ret) {
1933
1934 size_t n_partitions;
1935 uint64_t offset, next = UINT64_MAX;
1936
1937 assert(c);
1938 assert(t);
1939 assert(p);
1940 assert(ret);
1941
1942 if (!fdisk_partition_has_end(p))
1943 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition has no end!");
1944
1945 offset = fdisk_partition_get_end(p);
1946 assert(offset < UINT64_MAX);
1947 offset++; /* The end is one sector before the next partition or padding. */
1948 assert(offset < UINT64_MAX / secsz);
1949 offset *= secsz;
1950
1951 n_partitions = fdisk_table_get_nents(t);
1952 for (size_t i = 0; i < n_partitions; i++) {
1953 struct fdisk_partition *q;
1954 uint64_t start;
1955
1956 q = fdisk_table_get_partition(t, i);
1957 if (!q)
1958 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
1959
1960 if (fdisk_partition_is_used(q) <= 0)
1961 continue;
1962
1963 if (!fdisk_partition_has_start(q))
1964 continue;
1965
1966 start = fdisk_partition_get_start(q);
1967 assert(start < UINT64_MAX / secsz);
1968 start *= secsz;
1969
1970 if (start >= offset && (next == UINT64_MAX || next > start))
1971 next = start;
1972 }
1973
1974 if (next == UINT64_MAX) {
1975 /* No later partition? In that case check the end of the usable area */
1976 next = fdisk_get_last_lba(c);
1977 assert(next < UINT64_MAX);
1978 next++; /* The last LBA is one sector before the end */
1979
1980 assert(next < UINT64_MAX / secsz);
1981 next *= secsz;
1982
1983 if (offset > next)
1984 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end.");
1985 }
1986
1987 assert(next >= offset);
1988 offset = round_up_size(offset, grainsz);
1989 next = round_down_size(next, grainsz);
1990
1991 *ret = LESS_BY(next, offset); /* Saturated subtraction, rounding might have fucked things up */
1992 return 0;
1993}
1994
e1536d1f 1995static int context_copy_from_one(Context *context, const char *src) {
1e46985a
DDM
1996 _cleanup_close_ int fd = -EBADF;
1997 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
1998 _cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL;
1999 Partition *last = NULL;
2000 unsigned long secsz, grainsz;
2001 size_t n_partitions;
2002 int r;
2003
e1536d1f 2004 assert(src);
1e46985a 2005
e1536d1f 2006 r = context_open_and_lock_backing_fd(src, LOCK_SH, &fd);
1e46985a
DDM
2007 if (r < 0)
2008 return r;
2009
2010 r = fd_verify_regular(fd);
2011 if (r < 0)
e1536d1f 2012 return log_error_errno(r, "%s is not a file: %m", src);
1e46985a 2013
fd9fe57a 2014 r = fdisk_new_context_at(fd, /* path = */ NULL, /* read_only = */ true, /* sector_size = */ UINT32_MAX, &c);
1e46985a
DDM
2015 if (r < 0)
2016 return log_error_errno(r, "Failed to create fdisk context: %m");
2017
2018 secsz = fdisk_get_sector_size(c);
2019 grainsz = fdisk_get_grain_size(c);
2020
2021 /* Insist on a power of two, and that it's a multiple of 512, i.e. the traditional sector size. */
2022 if (secsz < 512 || !ISPOWEROF2(secsz))
2023 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Sector size %lu is not a power of two larger than 512? Refusing.", secsz);
2024
2025 if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
e1536d1f 2026 return log_error_errno(SYNTHETIC_ERRNO(EHWPOISON), "Cannot copy from disk %s with no GPT disk label.", src);
1e46985a
DDM
2027
2028 r = fdisk_get_partitions(c, &t);
2029 if (r < 0)
2030 return log_error_errno(r, "Failed to acquire partition table: %m");
2031
2032 n_partitions = fdisk_table_get_nents(t);
2033 for (size_t i = 0; i < n_partitions; i++) {
2034 _cleanup_(partition_freep) Partition *np = NULL;
2035 _cleanup_free_ char *label_copy = NULL;
2036 struct fdisk_partition *p;
2037 const char *label;
2038 uint64_t sz, start, padding;
2039 sd_id128_t ptid, id;
2040 GptPartitionType type;
2041
2042 p = fdisk_table_get_partition(t, i);
2043 if (!p)
2044 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
2045
2046 if (fdisk_partition_is_used(p) <= 0)
2047 continue;
2048
2049 if (fdisk_partition_has_start(p) <= 0 ||
2050 fdisk_partition_has_size(p) <= 0 ||
2051 fdisk_partition_has_partno(p) <= 0)
2052 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a position, size or number.");
2053
2054 r = fdisk_partition_get_type_as_id128(p, &ptid);
2055 if (r < 0)
2056 return log_error_errno(r, "Failed to query partition type UUID: %m");
2057
2058 type = gpt_partition_type_from_uuid(ptid);
2059
2060 r = fdisk_partition_get_uuid_as_id128(p, &id);
2061 if (r < 0)
2062 return log_error_errno(r, "Failed to query partition UUID: %m");
2063
2064 label = fdisk_partition_get_name(p);
2065 if (!isempty(label)) {
2066 label_copy = strdup(label);
2067 if (!label_copy)
2068 return log_oom();
2069 }
2070
2071 sz = fdisk_partition_get_size(p);
2072 assert(sz <= UINT64_MAX/secsz);
2073 sz *= secsz;
2074
2075 start = fdisk_partition_get_start(p);
2076 assert(start <= UINT64_MAX/secsz);
2077 start *= secsz;
2078
c74d50ff 2079 if (partition_type_exclude(&type))
1e46985a
DDM
2080 continue;
2081
2082 np = partition_new();
2083 if (!np)
2084 return log_oom();
2085
2086 np->type = type;
2087 np->new_uuid = id;
2088 np->new_uuid_is_set = true;
2089 np->size_min = np->size_max = sz;
2090 np->new_label = TAKE_PTR(label_copy);
2091
e1536d1f 2092 np->definition_path = strdup(src);
1e46985a
DDM
2093 if (!np->definition_path)
2094 return log_oom();
2095
2096 r = determine_current_padding(c, t, p, secsz, grainsz, &padding);
2097 if (r < 0)
2098 return r;
2099
2100 np->padding_min = np->padding_max = padding;
2101
e1536d1f 2102 np->copy_blocks_path = strdup(src);
1e46985a
DDM
2103 if (!np->copy_blocks_path)
2104 return log_oom();
2105
2106 np->copy_blocks_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
2107 if (np->copy_blocks_fd < 0)
e1536d1f 2108 return log_error_errno(r, "Failed to duplicate file descriptor of %s: %m", src);
1e46985a
DDM
2109
2110 np->copy_blocks_offset = start;
2111 np->copy_blocks_size = sz;
2112
2113 r = fdisk_partition_get_attrs_as_uint64(p, &np->gpt_flags);
2114 if (r < 0)
2115 return log_error_errno(r, "Failed to get partition flags: %m");
2116
2117 LIST_INSERT_AFTER(partitions, context->partitions, last, np);
2118 last = TAKE_PTR(np);
2119 context->n_partitions++;
2120 }
2121
2122 return 0;
2123}
2124
e1536d1f
DDM
2125static int context_copy_from(Context *context) {
2126 int r;
2127
2128 assert(context);
2129
2130 STRV_FOREACH(src, arg_copy_from) {
2131 r = context_copy_from_one(context, *src);
2132 if (r < 0)
2133 return r;
2134 }
2135
2136 return 0;
2137}
2138
4b047310 2139static int context_read_definitions(Context *context) {
e594a3b1 2140 _cleanup_strv_free_ char **files = NULL;
1e46985a 2141 Partition *last = LIST_FIND_TAIL(partitions, context->partitions);
39fc0174 2142 const char *const *dirs;
9b05a371 2143 int r;
e594a3b1
LP
2144
2145 assert(context);
2146
4b047310 2147 dirs = (const char* const*) (arg_definitions ?: CONF_PATHS_STRV("repart.d"));
39fc0174 2148
4b047310 2149 r = conf_files_list_strv(&files, ".conf", arg_definitions ? NULL : arg_root, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, dirs);
e594a3b1
LP
2150 if (r < 0)
2151 return log_error_errno(r, "Failed to enumerate *.conf files: %m");
2152
2153 STRV_FOREACH(f, files) {
2154 _cleanup_(partition_freep) Partition *p = NULL;
2155
2156 p = partition_new();
2157 if (!p)
2158 return log_oom();
2159
2160 p->definition_path = strdup(*f);
2161 if (!p->definition_path)
2162 return log_oom();
2163
39fc0174 2164 r = partition_read_definition(p, *f, dirs);
e594a3b1
LP
2165 if (r < 0)
2166 return r;
7d505753
DDM
2167 if (r == 0)
2168 continue;
e594a3b1
LP
2169
2170 LIST_INSERT_AFTER(partitions, context->partitions, last, p);
2171 last = TAKE_PTR(p);
2172 context->n_partitions++;
2173 }
2174
b5b7879a
DDM
2175 /* Check that each configured verity hash/data partition has a matching verity data/hash partition. */
2176
2177 LIST_FOREACH(partitions, p, context->partitions) {
2178 if (p->verity == VERITY_OFF)
2179 continue;
2180
2181 for (VerityMode mode = VERITY_OFF + 1; mode < _VERITY_MODE_MAX; mode++) {
b456191d 2182 Partition *q = NULL;
b5b7879a
DDM
2183
2184 if (p->verity == mode)
2185 continue;
2186
2187 if (p->siblings[mode])
2188 continue;
2189
2190 r = find_verity_sibling(context, p, mode, &q);
8e52ed02
DDM
2191 if (r == -ENXIO) {
2192 if (mode != VERITY_SIG)
2193 return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL),
2194 "Missing verity %s partition for verity %s partition with VerityMatchKey=%s",
2195 verity_mode_to_string(mode), verity_mode_to_string(p->verity), p->verity_match_key);
2196 } else if (r == -ENOTUNIQ)
b5b7879a
DDM
2197 return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL),
2198 "Multiple verity %s partitions found for verity %s partition with VerityMatchKey=%s",
2199 verity_mode_to_string(mode), verity_mode_to_string(p->verity), p->verity_match_key);
8e52ed02
DDM
2200 else if (r < 0)
2201 return log_syntax(NULL, LOG_ERR, p->definition_path, 1, r,
2202 "Failed to find verity %s partition for verity %s partition with VerityMatchKey=%s",
2203 verity_mode_to_string(mode), verity_mode_to_string(p->verity), p->verity_match_key);
b5b7879a 2204
b456191d
DDM
2205 if (q) {
2206 if (q->priority != p->priority)
2207 return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL),
9f08d7f7
MY
2208 "Priority mismatch (%i != %i) for verity sibling partitions with VerityMatchKey=%s",
2209 p->priority, q->priority, p->verity_match_key);
b5b7879a 2210
b456191d
DDM
2211 p->siblings[mode] = q;
2212 }
b5b7879a
DDM
2213 }
2214 }
2215
5eef7047
DDM
2216 LIST_FOREACH(partitions, p, context->partitions) {
2217 Partition *dp;
2218
2219 if (p->verity != VERITY_HASH)
2220 continue;
2221
2222 if (p->minimize == MINIMIZE_OFF)
2223 continue;
2224
2225 assert_se(dp = p->siblings[VERITY_DATA]);
2226
5a77fbb8 2227 if (dp->minimize == MINIMIZE_OFF && !(dp->copy_blocks_path || dp->copy_blocks_auto))
5eef7047
DDM
2228 return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL),
2229 "Minimize= set for verity hash partition but data partition does "
2230 "not set CopyBlocks= or Minimize=");
2231
2232 }
2233
e594a3b1
LP
2234 return 0;
2235}
2236
e594a3b1
LP
2237static int fdisk_ask_cb(struct fdisk_context *c, struct fdisk_ask *ask, void *data) {
2238 _cleanup_free_ char *ids = NULL;
2239 int r;
2240
2241 if (fdisk_ask_get_type(ask) != FDISK_ASKTYPE_STRING)
2242 return -EINVAL;
2243
b7416360 2244 ids = new(char, SD_ID128_UUID_STRING_MAX);
e594a3b1
LP
2245 if (!ids)
2246 return -ENOMEM;
2247
b7416360 2248 r = fdisk_ask_string_set_result(ask, sd_id128_to_uuid_string(*(sd_id128_t*) data, ids));
e594a3b1
LP
2249 if (r < 0)
2250 return r;
2251
2252 TAKE_PTR(ids);
2253 return 0;
2254}
2255
2256static int fdisk_set_disklabel_id_by_uuid(struct fdisk_context *c, sd_id128_t id) {
2257 int r;
2258
2259 r = fdisk_set_ask(c, fdisk_ask_cb, &id);
2260 if (r < 0)
2261 return r;
2262
2263 r = fdisk_set_disklabel_id(c);
2264 if (r < 0)
2265 return r;
2266
2267 return fdisk_set_ask(c, NULL, NULL);
2268}
2269
53171c04 2270static int derive_uuid(sd_id128_t base, const char *token, sd_id128_t *ret) {
e594a3b1 2271 union {
ade99252 2272 uint8_t md[SHA256_DIGEST_SIZE];
e594a3b1
LP
2273 sd_id128_t id;
2274 } result;
2275
53171c04 2276 assert(token);
e594a3b1
LP
2277 assert(ret);
2278
53171c04
LP
2279 /* Derive a new UUID from the specified UUID in a stable and reasonably safe way. Specifically, we
2280 * calculate the HMAC-SHA256 of the specified token string, keyed by the supplied base (typically the
2281 * machine ID). We use the machine ID as key (and not as cleartext!) of the HMAC operation since it's
2282 * the machine ID we don't want to leak. */
e594a3b1 2283
ade99252 2284 hmac_sha256(base.bytes, sizeof(base.bytes), token, strlen(token), result.md);
e594a3b1
LP
2285
2286 /* Take the first half, mark it as v4 UUID */
2287 assert_cc(sizeof(result.md) == sizeof(result.id) * 2);
2288 *ret = id128_make_v4_uuid(result.id);
2289 return 0;
2290}
2291
81e04781
VH
2292static void derive_salt(sd_id128_t base, const char *token, uint8_t ret[static SHA256_DIGEST_SIZE]) {
2293 assert(token);
2294
2295 hmac_sha256(base.bytes, sizeof(base.bytes), token, strlen(token), ret);
2296}
2297
cc751c75 2298static int context_load_partition_table(Context *context) {
e594a3b1
LP
2299 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
2300 _cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL;
2301 uint64_t left_boundary = UINT64_MAX, first_lba, last_lba, nsectors;
2302 _cleanup_free_ char *disk_uuid_string = NULL;
2303 bool from_scratch = false;
2304 sd_id128_t disk_uuid;
2305 size_t n_partitions;
994b3031 2306 unsigned long secsz;
93f125a6 2307 uint64_t grainsz, fs_secsz = DEFAULT_FILESYSTEM_SECTOR_SIZE;
e594a3b1
LP
2308 int r;
2309
2310 assert(context);
170c9823
LP
2311 assert(!context->fdisk_context);
2312 assert(!context->free_areas);
2313 assert(context->start == UINT64_MAX);
2314 assert(context->end == UINT64_MAX);
2315 assert(context->total == UINT64_MAX);
e594a3b1 2316
e1878ef7
DDM
2317 c = fdisk_new_context();
2318 if (!c)
2319 return log_oom();
2320
93f125a6 2321 if (arg_sector_size > 0) {
93f125a6 2322 fs_secsz = arg_sector_size;
5f13fb0a 2323 r = fdisk_save_user_sector_size(c, /* phy= */ 0, arg_sector_size);
93f125a6 2324 } else {
6c54e1d6 2325 uint32_t ssz;
93f125a6 2326 struct stat st;
6c54e1d6 2327
3cdcf4e2
DDM
2328 r = context_open_and_lock_backing_fd(context->node, arg_dry_run ? LOCK_SH : LOCK_EX,
2329 &context->backing_fd);
37734dc6
YW
2330 if (r < 0)
2331 return r;
6c54e1d6 2332
93f125a6 2333 if (fstat(context->backing_fd, &st) < 0)
9f08d7f7 2334 return log_error_errno(errno, "Failed to stat %s: %m", context->node);
93f125a6 2335
6c54e1d6
LP
2336 /* Auto-detect sector size if not specified. */
2337 r = probe_sector_size_prefer_ioctl(context->backing_fd, &ssz);
e1878ef7 2338 if (r < 0)
6c54e1d6
LP
2339 return log_error_errno(r, "Failed to probe sector size of '%s': %m", context->node);
2340
93f125a6
DDM
2341 /* If we found the sector size and we're operating on a block device, use it as the file
2342 * system sector size as well, as we know its the sector size of the actual block device and
2343 * not just the offset at which we found the GPT header. */
2344 if (r > 0 && S_ISBLK(st.st_mode))
2345 fs_secsz = ssz;
2346
6c54e1d6 2347 r = fdisk_save_user_sector_size(c, /* phy= */ 0, ssz);
e1878ef7 2348 }
6c54e1d6
LP
2349 if (r < 0)
2350 return log_error_errno(r, "Failed to set sector size: %m");
e1878ef7 2351
a26f4a49
LP
2352 /* libfdisk doesn't have an API to operate on arbitrary fds, hence reopen the fd going via the
2353 * /proc/self/fd/ magic path if we have an existing fd. Open the original file otherwise. */
e1878ef7
DDM
2354 r = fdisk_assign_device(
2355 c,
2356 context->backing_fd >= 0 ? FORMAT_PROC_FD_PATH(context->backing_fd) : context->node,
2357 arg_dry_run);
170c9823
LP
2358 if (r == -EINVAL && arg_size_auto) {
2359 struct stat st;
2360
2361 /* libfdisk returns EINVAL if opening a file of size zero. Let's check for that, and accept
2362 * it if automatic sizing is requested. */
2363
cc751c75
DDM
2364 if (context->backing_fd < 0)
2365 r = stat(context->node, &st);
170c9823 2366 else
cc751c75 2367 r = fstat(context->backing_fd, &st);
170c9823 2368 if (r < 0)
cc751c75 2369 return log_error_errno(errno, "Failed to stat block device '%s': %m", context->node);
170c9823 2370
994b3031 2371 if (S_ISREG(st.st_mode) && st.st_size == 0) {
264668a8 2372 /* Use the fallback values if we have no better idea */
93f125a6
DDM
2373 context->sector_size = fdisk_get_sector_size(c);
2374 context->fs_sector_size = fs_secsz;
994b3031 2375 context->grain_size = 4096;
170c9823 2376 return /* from_scratch = */ true;
994b3031 2377 }
170c9823
LP
2378
2379 r = -EINVAL;
2380 }
e594a3b1 2381 if (r < 0)
cc751c75 2382 return log_error_errno(r, "Failed to open device '%s': %m", context->node);
a26f4a49 2383
cc751c75 2384 if (context->backing_fd < 0) {
a26f4a49 2385 /* If we have no fd referencing the device yet, make a copy of the fd now, so that we have one */
3cdcf4e2
DDM
2386 r = context_open_and_lock_backing_fd(FORMAT_PROC_FD_PATH(fdisk_get_devfd(c)),
2387 arg_dry_run ? LOCK_SH : LOCK_EX,
2388 &context->backing_fd);
37734dc6
YW
2389 if (r < 0)
2390 return r;
25baae50 2391 }
e594a3b1 2392
994b3031
LP
2393 /* The offsets/sizes libfdisk returns to us will be in multiple of the sector size of the
2394 * device. This is typically 512, and sometimes 4096. Let's query libfdisk once for it, and then use
2395 * it for all our needs. Note that the values we use ourselves always are in bytes though, thus mean
2396 * the same thing universally. Also note that regardless what kind of sector size is in use we'll
2397 * place partitions at multiples of 4K. */
2398 secsz = fdisk_get_sector_size(c);
2399
2400 /* Insist on a power of two, and that it's a multiple of 512, i.e. the traditional sector size. */
983ce0b5
LP
2401 if (secsz < 512 || !ISPOWEROF2(secsz))
2402 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Sector size %lu is not a power of two larger than 512? Refusing.", secsz);
994b3031
LP
2403
2404 /* Use at least 4K, and ensure it's a multiple of the sector size, regardless if that is smaller or
2405 * larger */
2406 grainsz = secsz < 4096 ? 4096 : secsz;
2407
2408 log_debug("Sector size of device is %lu bytes. Using grain size of %" PRIu64 ".", secsz, grainsz);
2409
e594a3b1
LP
2410 switch (arg_empty) {
2411
2412 case EMPTY_REFUSE:
2413 /* Refuse empty disks, insist on an existing GPT partition table */
2414 if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
cc751c75 2415 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not repartitioning.", context->node);
e594a3b1
LP
2416
2417 break;
2418
2419 case EMPTY_REQUIRE:
2420 /* Require an empty disk, refuse any existing partition table */
2421 r = fdisk_has_label(c);
2422 if (r < 0)
cc751c75 2423 return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", context->node);
e594a3b1 2424 if (r > 0)
cc751c75 2425 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s already has a disk label, refusing.", context->node);
e594a3b1
LP
2426
2427 from_scratch = true;
2428 break;
2429
2430 case EMPTY_ALLOW:
2431 /* Allow both an empty disk and an existing partition table, but only GPT */
2432 r = fdisk_has_label(c);
2433 if (r < 0)
cc751c75 2434 return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", context->node);
e594a3b1
LP
2435 if (r > 0) {
2436 if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
cc751c75 2437 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has non-GPT disk label, not repartitioning.", context->node);
e594a3b1
LP
2438 } else
2439 from_scratch = true;
2440
2441 break;
2442
2443 case EMPTY_FORCE:
a26f4a49 2444 case EMPTY_CREATE:
e594a3b1
LP
2445 /* Always reinitiaize the disk, don't consider what there was on the disk before */
2446 from_scratch = true;
2447 break;
243dd1e9 2448
634b8471 2449 default:
243dd1e9 2450 assert_not_reached();
e594a3b1
LP
2451 }
2452
2453 if (from_scratch) {
e594a3b1
LP
2454 r = fdisk_create_disklabel(c, "gpt");
2455 if (r < 0)
2456 return log_error_errno(r, "Failed to create GPT disk label: %m");
2457
53171c04 2458 r = derive_uuid(context->seed, "disk-uuid", &disk_uuid);
e594a3b1
LP
2459 if (r < 0)
2460 return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
2461
2462 r = fdisk_set_disklabel_id_by_uuid(c, disk_uuid);
2463 if (r < 0)
2464 return log_error_errno(r, "Failed to set GPT disk label: %m");
2465
2466 goto add_initial_free_area;
2467 }
2468
2469 r = fdisk_get_disklabel_id(c, &disk_uuid_string);
2470 if (r < 0)
2471 return log_error_errno(r, "Failed to get current GPT disk label UUID: %m");
2472
aea3f594
ZJS
2473 r = id128_from_string_nonzero(disk_uuid_string, &disk_uuid);
2474 if (r == -ENXIO) {
53171c04 2475 r = derive_uuid(context->seed, "disk-uuid", &disk_uuid);
e594a3b1
LP
2476 if (r < 0)
2477 return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
2478
2479 r = fdisk_set_disklabel_id(c);
2480 if (r < 0)
2481 return log_error_errno(r, "Failed to set GPT disk label: %m");
aea3f594
ZJS
2482 } else if (r < 0)
2483 return log_error_errno(r, "Failed to parse current GPT disk label UUID: %m");
e594a3b1
LP
2484
2485 r = fdisk_get_partitions(c, &t);
2486 if (r < 0)
2487 return log_error_errno(r, "Failed to acquire partition table: %m");
2488
2489 n_partitions = fdisk_table_get_nents(t);
695cfd53 2490 for (size_t i = 0; i < n_partitions; i++) {
e594a3b1 2491 _cleanup_free_ char *label_copy = NULL;
03677889 2492 Partition *last = NULL;
e594a3b1 2493 struct fdisk_partition *p;
63b96eb9 2494 const char *label;
e594a3b1
LP
2495 uint64_t sz, start;
2496 bool found = false;
2497 sd_id128_t ptid, id;
2498 size_t partno;
2499
2500 p = fdisk_table_get_partition(t, i);
2501 if (!p)
2502 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
2503
2504 if (fdisk_partition_is_used(p) <= 0)
2505 continue;
2506
2507 if (fdisk_partition_has_start(p) <= 0 ||
2508 fdisk_partition_has_size(p) <= 0 ||
2509 fdisk_partition_has_partno(p) <= 0)
2510 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a position, size or number.");
2511
63b96eb9 2512 r = fdisk_partition_get_type_as_id128(p, &ptid);
e594a3b1 2513 if (r < 0)
63b96eb9 2514 return log_error_errno(r, "Failed to query partition type UUID: %m");
e594a3b1 2515
02e32aa6 2516 r = fdisk_partition_get_uuid_as_id128(p, &id);
e594a3b1 2517 if (r < 0)
02e32aa6 2518 return log_error_errno(r, "Failed to query partition UUID: %m");
e594a3b1
LP
2519
2520 label = fdisk_partition_get_name(p);
2521 if (!isempty(label)) {
2522 label_copy = strdup(label);
2523 if (!label_copy)
2524 return log_oom();
2525 }
2526
2527 sz = fdisk_partition_get_size(p);
ac33e147 2528 assert(sz <= UINT64_MAX/secsz);
994b3031 2529 sz *= secsz;
e594a3b1
LP
2530
2531 start = fdisk_partition_get_start(p);
ac33e147 2532 assert(start <= UINT64_MAX/secsz);
994b3031 2533 start *= secsz;
e594a3b1
LP
2534
2535 partno = fdisk_partition_get_partno(p);
2536
2537 if (left_boundary == UINT64_MAX || left_boundary > start)
2538 left_boundary = start;
2539
2540 /* Assign this existing partition to the first partition of the right type that doesn't have
2541 * an existing one assigned yet. */
2542 LIST_FOREACH(partitions, pp, context->partitions) {
2543 last = pp;
2544
22e932f4 2545 if (!sd_id128_equal(pp->type.uuid, ptid))
e594a3b1
LP
2546 continue;
2547
2548 if (!pp->current_partition) {
2549 pp->current_uuid = id;
2550 pp->current_size = sz;
2551 pp->offset = start;
2552 pp->partno = partno;
2553 pp->current_label = TAKE_PTR(label_copy);
2554
2555 pp->current_partition = p;
2556 fdisk_ref_partition(p);
2557
994b3031 2558 r = determine_current_padding(c, t, p, secsz, grainsz, &pp->current_padding);
e594a3b1
LP
2559 if (r < 0)
2560 return r;
2561
2562 if (pp->current_padding > 0) {
2563 r = context_add_free_area(context, pp->current_padding, pp);
2564 if (r < 0)
2565 return r;
2566 }
2567
2568 found = true;
2569 break;
2570 }
2571 }
2572
2573 /* If we have no matching definition, create a new one. */
2574 if (!found) {
2575 _cleanup_(partition_freep) Partition *np = NULL;
2576
2577 np = partition_new();
2578 if (!np)
2579 return log_oom();
2580
2581 np->current_uuid = id;
22e932f4 2582 np->type = gpt_partition_type_from_uuid(ptid);
e594a3b1
LP
2583 np->current_size = sz;
2584 np->offset = start;
2585 np->partno = partno;
2586 np->current_label = TAKE_PTR(label_copy);
2587
2588 np->current_partition = p;
2589 fdisk_ref_partition(p);
2590
994b3031 2591 r = determine_current_padding(c, t, p, secsz, grainsz, &np->current_padding);
e594a3b1
LP
2592 if (r < 0)
2593 return r;
2594
2595 if (np->current_padding > 0) {
2596 r = context_add_free_area(context, np->current_padding, np);
2597 if (r < 0)
2598 return r;
2599 }
2600
2601 LIST_INSERT_AFTER(partitions, context->partitions, last, TAKE_PTR(np));
2602 context->n_partitions++;
2603 }
2604 }
2605
2606add_initial_free_area:
2607 nsectors = fdisk_get_nsectors(c);
994b3031
LP
2608 assert(nsectors <= UINT64_MAX/secsz);
2609 nsectors *= secsz;
e594a3b1
LP
2610
2611 first_lba = fdisk_get_first_lba(c);
994b3031
LP
2612 assert(first_lba <= UINT64_MAX/secsz);
2613 first_lba *= secsz;
e594a3b1
LP
2614
2615 last_lba = fdisk_get_last_lba(c);
2616 assert(last_lba < UINT64_MAX);
2617 last_lba++;
994b3031
LP
2618 assert(last_lba <= UINT64_MAX/secsz);
2619 last_lba *= secsz;
e594a3b1
LP
2620
2621 assert(last_lba >= first_lba);
2622
2623 if (left_boundary == UINT64_MAX) {
2624 /* No partitions at all? Then the whole disk is up for grabs. */
2625
994b3031
LP
2626 first_lba = round_up_size(first_lba, grainsz);
2627 last_lba = round_down_size(last_lba, grainsz);
e594a3b1
LP
2628
2629 if (last_lba > first_lba) {
2630 r = context_add_free_area(context, last_lba - first_lba, NULL);
2631 if (r < 0)
2632 return r;
2633 }
2634 } else {
2635 /* Add space left of first partition */
2636 assert(left_boundary >= first_lba);
2637
994b3031
LP
2638 first_lba = round_up_size(first_lba, grainsz);
2639 left_boundary = round_down_size(left_boundary, grainsz);
2640 last_lba = round_down_size(last_lba, grainsz);
e594a3b1
LP
2641
2642 if (left_boundary > first_lba) {
2643 r = context_add_free_area(context, left_boundary - first_lba, NULL);
2644 if (r < 0)
2645 return r;
2646 }
2647 }
2648
2649 context->start = first_lba;
2650 context->end = last_lba;
2651 context->total = nsectors;
994b3031 2652 context->sector_size = secsz;
93f125a6 2653 context->fs_sector_size = fs_secsz;
994b3031 2654 context->grain_size = grainsz;
e594a3b1
LP
2655 context->fdisk_context = TAKE_PTR(c);
2656
2657 return from_scratch;
2658}
2659
2660static void context_unload_partition_table(Context *context) {
e594a3b1
LP
2661 assert(context);
2662
80a226b2 2663 LIST_FOREACH(partitions, p, context->partitions) {
e594a3b1
LP
2664
2665 /* Entirely remove partitions that have no configuration */
2666 if (PARTITION_IS_FOREIGN(p)) {
2667 partition_unlink_and_free(context, p);
2668 continue;
2669 }
2670
2671 /* Otherwise drop all data we read off the block device and everything we might have
2672 * calculated based on it */
2673
2674 p->dropped = false;
2675 p->current_size = UINT64_MAX;
2676 p->new_size = UINT64_MAX;
2677 p->current_padding = UINT64_MAX;
2678 p->new_padding = UINT64_MAX;
2679 p->partno = UINT64_MAX;
2680 p->offset = UINT64_MAX;
2681
2682 if (p->current_partition) {
2683 fdisk_unref_partition(p->current_partition);
2684 p->current_partition = NULL;
2685 }
2686
2687 if (p->new_partition) {
2688 fdisk_unref_partition(p->new_partition);
2689 p->new_partition = NULL;
2690 }
2691
2692 p->padding_area = NULL;
2693 p->allocated_to_area = NULL;
2694
15d43e30
LP
2695 p->current_uuid = SD_ID128_NULL;
2696 p->current_label = mfree(p->current_label);
e594a3b1
LP
2697 }
2698
2699 context->start = UINT64_MAX;
2700 context->end = UINT64_MAX;
2701 context->total = UINT64_MAX;
2702
2703 if (context->fdisk_context) {
2704 fdisk_unref_context(context->fdisk_context);
2705 context->fdisk_context = NULL;
2706 }
2707
2708 context_free_free_areas(context);
2709}
2710
2711static int format_size_change(uint64_t from, uint64_t to, char **ret) {
2b59bf51 2712 char *t;
e594a3b1
LP
2713
2714 if (from != UINT64_MAX) {
2715 if (from == to || to == UINT64_MAX)
2b59bf51 2716 t = strdup(FORMAT_BYTES(from));
e594a3b1 2717 else
fc03e80c 2718 t = strjoin(FORMAT_BYTES(from), " ", special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), " ", FORMAT_BYTES(to));
e594a3b1 2719 } else if (to != UINT64_MAX)
fc03e80c 2720 t = strjoin(special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), " ", FORMAT_BYTES(to));
e594a3b1
LP
2721 else {
2722 *ret = NULL;
2723 return 0;
2724 }
2725
2b59bf51 2726 if (!t)
e594a3b1
LP
2727 return log_oom();
2728
2b59bf51 2729 *ret = t;
e594a3b1
LP
2730 return 1;
2731}
2732
2733static const char *partition_label(const Partition *p) {
2734 assert(p);
2735
2736 if (p->new_label)
2737 return p->new_label;
2738
2739 if (p->current_label)
2740 return p->current_label;
2741
22e932f4 2742 return gpt_partition_type_uuid_to_string(p->type.uuid);
e594a3b1
LP
2743}
2744
cc751c75 2745static int context_dump_partitions(Context *context) {
e594a3b1
LP
2746 _cleanup_(table_unrefp) Table *t = NULL;
2747 uint64_t sum_padding = 0, sum_size = 0;
e594a3b1 2748 int r;
1ffa5cfb 2749 const size_t roothash_col = 14, dropin_files_col = 15, split_path_col = 16;
bf030f55 2750 bool has_roothash = false, has_dropin_files = false, has_split_path = false;
e594a3b1 2751
6a01ea4a 2752 if ((arg_json_format_flags & JSON_FORMAT_OFF) && context->n_partitions == 0) {
a015fbe7
TH
2753 log_info("Empty partition table.");
2754 return 0;
2755 }
2756
bf030f55
DDM
2757 t = table_new("type",
2758 "label",
2759 "uuid",
1ffa5cfb 2760 "partno",
bf030f55
DDM
2761 "file",
2762 "node",
2763 "offset",
2764 "old size",
2765 "raw size",
2766 "size",
2767 "old padding",
2768 "raw padding",
2769 "padding",
2770 "activity",
2771 "roothash",
2772 "drop-in files",
2773 "split path");
e594a3b1
LP
2774 if (!t)
2775 return log_oom();
2776
a015fbe7 2777 if (!DEBUG_LOGGING) {
6a01ea4a 2778 if (arg_json_format_flags & JSON_FORMAT_OFF)
a015fbe7 2779 (void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
1ffa5cfb 2780 (size_t) 8, (size_t) 9, (size_t) 12, roothash_col, dropin_files_col,
bf030f55 2781 split_path_col);
a015fbe7
TH
2782 else
2783 (void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
1ffa5cfb
DDM
2784 (size_t) 5, (size_t) 6, (size_t) 7, (size_t) 8, (size_t) 10,
2785 (size_t) 11, (size_t) 13, roothash_col, dropin_files_col,
bf030f55 2786 split_path_col);
a015fbe7 2787 }
e594a3b1 2788
e594a3b1 2789 (void) table_set_align_percent(t, table_get_cell(t, 0, 5), 100);
9c07c9ec
LP
2790 (void) table_set_align_percent(t, table_get_cell(t, 0, 6), 100);
2791 (void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
2792 (void) table_set_align_percent(t, table_get_cell(t, 0, 8), 100);
2793 (void) table_set_align_percent(t, table_get_cell(t, 0, 9), 100);
2794 (void) table_set_align_percent(t, table_get_cell(t, 0, 10), 100);
2795 (void) table_set_align_percent(t, table_get_cell(t, 0, 11), 100);
e594a3b1
LP
2796
2797 LIST_FOREACH(partitions, p, context->partitions) {
b5b7879a 2798 _cleanup_free_ char *size_change = NULL, *padding_change = NULL, *partname = NULL, *rh = NULL;
b7416360 2799 char uuid_buffer[SD_ID128_UUID_STRING_MAX];
a015fbe7 2800 const char *label, *activity = NULL;
e594a3b1
LP
2801
2802 if (p->dropped)
2803 continue;
2804
a015fbe7
TH
2805 if (p->current_size == UINT64_MAX)
2806 activity = "create";
2807 else if (p->current_size != p->new_size)
2808 activity = "resize";
2809
e594a3b1 2810 label = partition_label(p);
cc751c75 2811 partname = p->partno != UINT64_MAX ? fdisk_partname(context->node, p->partno+1) : NULL;
e594a3b1
LP
2812
2813 r = format_size_change(p->current_size, p->new_size, &size_change);
2814 if (r < 0)
2815 return r;
2816
2817 r = format_size_change(p->current_padding, p->new_padding, &padding_change);
2818 if (r < 0)
2819 return r;
2820
2821 if (p->new_size != UINT64_MAX)
2822 sum_size += p->new_size;
2823 if (p->new_padding != UINT64_MAX)
2824 sum_padding += p->new_padding;
2825
2ecc7a5b
DDM
2826 if (p->verity != VERITY_OFF) {
2827 Partition *hp = p->verity == VERITY_HASH ? p : p->siblings[VERITY_HASH];
2828
6b4b40f4 2829 rh = iovec_is_set(&hp->roothash) ? hexmem(hp->roothash.iov_base, hp->roothash.iov_len) : strdup("TBD");
b5b7879a
DDM
2830 if (!rh)
2831 return log_oom();
2832 }
2833
e594a3b1
LP
2834 r = table_add_many(
2835 t,
22e932f4 2836 TABLE_STRING, gpt_partition_type_uuid_to_string_harder(p->type.uuid, uuid_buffer),
be9ce018 2837 TABLE_STRING, empty_to_null(label) ?: "-", TABLE_SET_COLOR, empty_to_null(label) ? NULL : ansi_grey(),
11749b61 2838 TABLE_UUID, p->new_uuid_is_set ? p->new_uuid : p->current_uuid,
1ffa5cfb 2839 TABLE_UINT64, p->partno,
d0242ac9 2840 TABLE_PATH_BASENAME, p->definition_path, TABLE_SET_COLOR, p->definition_path ? NULL : ansi_grey(),
a015fbe7 2841 TABLE_STRING, partname ?: "-", TABLE_SET_COLOR, partname ? NULL : ansi_highlight(),
e594a3b1 2842 TABLE_UINT64, p->offset,
a015fbe7 2843 TABLE_UINT64, p->current_size == UINT64_MAX ? 0 : p->current_size,
e594a3b1
LP
2844 TABLE_UINT64, p->new_size,
2845 TABLE_STRING, size_change, TABLE_SET_COLOR, !p->partitions_next && sum_size > 0 ? ansi_underline() : NULL,
a015fbe7 2846 TABLE_UINT64, p->current_padding == UINT64_MAX ? 0 : p->current_padding,
e594a3b1 2847 TABLE_UINT64, p->new_padding,
a015fbe7 2848 TABLE_STRING, padding_change, TABLE_SET_COLOR, !p->partitions_next && sum_padding > 0 ? ansi_underline() : NULL,
39fc0174 2849 TABLE_STRING, activity ?: "unchanged",
b5b7879a 2850 TABLE_STRING, rh,
bf030f55
DDM
2851 TABLE_STRV, p->drop_in_files,
2852 TABLE_STRING, empty_to_null(p->split_path) ?: "-");
e594a3b1 2853 if (r < 0)
f987a261 2854 return table_log_add_error(r);
39fc0174 2855
b5b7879a 2856 has_roothash = has_roothash || !isempty(rh);
3ab44dbd 2857 has_dropin_files = has_dropin_files || !strv_isempty(p->drop_in_files);
bf030f55 2858 has_split_path = has_split_path || !isempty(p->split_path);
e594a3b1
LP
2859 }
2860
6a01ea4a 2861 if ((arg_json_format_flags & JSON_FORMAT_OFF) && (sum_padding > 0 || sum_size > 0)) {
e594a3b1
LP
2862 const char *a, *b;
2863
2b59bf51
ZJS
2864 a = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_size));
2865 b = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_padding));
e594a3b1
LP
2866
2867 r = table_add_many(
2868 t,
2869 TABLE_EMPTY,
2870 TABLE_EMPTY,
2871 TABLE_EMPTY,
2872 TABLE_EMPTY,
2873 TABLE_EMPTY,
2874 TABLE_EMPTY,
2875 TABLE_EMPTY,
1ffa5cfb 2876 TABLE_EMPTY,
a015fbe7 2877 TABLE_EMPTY,
e594a3b1
LP
2878 TABLE_STRING, a,
2879 TABLE_EMPTY,
a015fbe7
TH
2880 TABLE_EMPTY,
2881 TABLE_STRING, b,
39fc0174 2882 TABLE_EMPTY,
b5b7879a 2883 TABLE_EMPTY,
bf030f55 2884 TABLE_EMPTY,
a015fbe7 2885 TABLE_EMPTY);
e594a3b1 2886 if (r < 0)
f987a261 2887 return table_log_add_error(r);
e594a3b1
LP
2888 }
2889
b5b7879a
DDM
2890 if (!has_roothash) {
2891 r = table_hide_column_from_display(t, roothash_col);
2892 if (r < 0)
2893 return log_error_errno(r, "Failed to set columns to display: %m");
2894 }
2895
3ab44dbd 2896 if (!has_dropin_files) {
39fc0174
RP
2897 r = table_hide_column_from_display(t, dropin_files_col);
2898 if (r < 0)
2899 return log_error_errno(r, "Failed to set columns to display: %m");
2900 }
2901
bf030f55
DDM
2902 if (!has_split_path) {
2903 r = table_hide_column_from_display(t, split_path_col);
2904 if (r < 0)
2905 return log_error_errno(r, "Failed to set columns to display: %m");
2906 }
2907
896e678b 2908 return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
e594a3b1
LP
2909}
2910
2911static void context_bar_char_process_partition(
2912 Context *context,
2913 Partition *bar[],
2914 size_t n,
2915 Partition *p,
2916 size_t *ret_start) {
2917
2918 uint64_t from, to, total;
2919 size_t x, y;
2920
2921 assert(context);
2922 assert(bar);
2923 assert(n > 0);
2924 assert(p);
2925
2926 if (p->dropped)
2927 return;
2928
2929 assert(p->offset != UINT64_MAX);
2930 assert(p->new_size != UINT64_MAX);
2931
2932 from = p->offset;
2933 to = from + p->new_size;
2934
d8daed09
TY
2935 assert(context->total > 0);
2936 total = context->total;
e594a3b1 2937
d8daed09
TY
2938 assert(from <= total);
2939 x = from * n / total;
e594a3b1 2940
d8daed09
TY
2941 assert(to <= total);
2942 y = to * n / total;
e594a3b1
LP
2943
2944 assert(x <= y);
2945 assert(y <= n);
2946
2947 for (size_t i = x; i < y; i++)
2948 bar[i] = p;
2949
2950 *ret_start = x;
2951}
2952
2953static int partition_hint(const Partition *p, const char *node, char **ret) {
2954 _cleanup_free_ char *buf = NULL;
e594a3b1
LP
2955 const char *label;
2956 sd_id128_t id;
2957
2958 /* Tries really hard to find a suitable description for this partition */
2959
7b2ffb59
LP
2960 if (p->definition_path)
2961 return path_extract_filename(p->definition_path, ret);
e594a3b1
LP
2962
2963 label = partition_label(p);
2964 if (!isempty(label)) {
2965 buf = strdup(label);
2966 goto done;
2967 }
2968
2969 if (p->partno != UINT64_MAX) {
2970 buf = fdisk_partname(node, p->partno+1);
2971 goto done;
2972 }
2973
11749b61 2974 if (p->new_uuid_is_set)
e594a3b1
LP
2975 id = p->new_uuid;
2976 else if (!sd_id128_is_null(p->current_uuid))
2977 id = p->current_uuid;
2978 else
22e932f4 2979 id = p->type.uuid;
e594a3b1 2980
b7416360 2981 buf = strdup(SD_ID128_TO_UUID_STRING(id));
e594a3b1
LP
2982
2983done:
2984 if (!buf)
2985 return -ENOMEM;
2986
2987 *ret = TAKE_PTR(buf);
2988 return 0;
2989}
2990
cc751c75 2991static int context_dump_partition_bar(Context *context) {
e594a3b1
LP
2992 _cleanup_free_ Partition **bar = NULL;
2993 _cleanup_free_ size_t *start_array = NULL;
03677889 2994 Partition *last = NULL;
e594a3b1
LP
2995 bool z = false;
2996 size_t c, j = 0;
2997
f391597c 2998 assert_se((c = columns()) >= 2);
e594a3b1
LP
2999 c -= 2; /* We do not use the leftmost and rightmost character cell */
3000
3001 bar = new0(Partition*, c);
3002 if (!bar)
3003 return log_oom();
3004
3005 start_array = new(size_t, context->n_partitions);
3006 if (!start_array)
3007 return log_oom();
3008
3009 LIST_FOREACH(partitions, p, context->partitions)
3010 context_bar_char_process_partition(context, bar, c, p, start_array + j++);
3011
3012 putc(' ', stdout);
3013
3014 for (size_t i = 0; i < c; i++) {
3015 if (bar[i]) {
3016 if (last != bar[i])
3017 z = !z;
3018
3019 fputs(z ? ansi_green() : ansi_yellow(), stdout);
3020 fputs(special_glyph(SPECIAL_GLYPH_DARK_SHADE), stdout);
3021 } else {
3022 fputs(ansi_normal(), stdout);
3023 fputs(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE), stdout);
3024 }
3025
3026 last = bar[i];
3027 }
3028
3029 fputs(ansi_normal(), stdout);
3030 putc('\n', stdout);
3031
3032 for (size_t i = 0; i < context->n_partitions; i++) {
3033 _cleanup_free_ char **line = NULL;
3034
3035 line = new0(char*, c);
3036 if (!line)
3037 return log_oom();
3038
3039 j = 0;
3040 LIST_FOREACH(partitions, p, context->partitions) {
3041 _cleanup_free_ char *d = NULL;
3042 j++;
3043
3044 if (i < context->n_partitions - j) {
3045
3046 if (line[start_array[j-1]]) {
3047 const char *e;
3048
3049 /* Upgrade final corner to the right with a branch to the right */
3050 e = startswith(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_RIGHT));
3051 if (e) {
3052 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), e);
3053 if (!d)
3054 return log_oom();
3055 }
3056 }
3057
3058 if (!d) {
3059 d = strdup(special_glyph(SPECIAL_GLYPH_TREE_VERTICAL));
3060 if (!d)
3061 return log_oom();
3062 }
3063
3064 } else if (i == context->n_partitions - j) {
3065 _cleanup_free_ char *hint = NULL;
3066
cc751c75 3067 (void) partition_hint(p, context->node, &hint);
e594a3b1
LP
3068
3069 if (streq_ptr(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_VERTICAL)))
3070 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), " ", strna(hint));
3071 else
3072 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_RIGHT), " ", strna(hint));
3073
3074 if (!d)
3075 return log_oom();
3076 }
3077
3078 if (d)
3079 free_and_replace(line[start_array[j-1]], d);
3080 }
3081
3082 putc(' ', stdout);
3083
3084 j = 0;
3085 while (j < c) {
3086 if (line[j]) {
3087 fputs(line[j], stdout);
3088 j += utf8_console_width(line[j]);
3089 } else {
3090 putc(' ', stdout);
3091 j++;
3092 }
3093 }
3094
3095 putc('\n', stdout);
3096
3097 for (j = 0; j < c; j++)
3098 free(line[j]);
3099 }
3100
3101 return 0;
3102}
3103
b5b7879a
DDM
3104static bool context_has_roothash(Context *context) {
3105 LIST_FOREACH(partitions, p, context->partitions)
6b4b40f4 3106 if (iovec_is_set(&p->roothash))
b5b7879a
DDM
3107 return true;
3108
3109 return false;
3110}
3111
cc751c75 3112static int context_dump(Context *context, bool late) {
a26d463d
DDM
3113 int r;
3114
3115 assert(context);
a26d463d
DDM
3116
3117 if (arg_pretty == 0 && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
3118 return 0;
3119
b5b7879a
DDM
3120 /* If we're outputting JSON, only dump after doing all operations so we can include the roothashes
3121 * in the output. */
3122 if (!late && !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
3123 return 0;
3124
3125 /* If we're not outputting JSON, only dump again after doing all operations if there are any
3126 * roothashes that we need to communicate to the user. */
3127 if (late && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && !context_has_roothash(context))
3128 return 0;
3129
cc751c75 3130 r = context_dump_partitions(context);
a26d463d
DDM
3131 if (r < 0)
3132 return r;
3133
b5b7879a
DDM
3134 /* Make sure we only write the partition bar once, even if we're writing the partition table twice to
3135 * communicate roothashes. */
3136 if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && !late) {
a26d463d
DDM
3137 putc('\n', stdout);
3138
cc751c75 3139 r = context_dump_partition_bar(context);
a26d463d
DDM
3140 if (r < 0)
3141 return r;
3142
3143 putc('\n', stdout);
3144 }
3145
3146 fflush(stdout);
3147
3148 return 0;
3149}
3150
3151
e594a3b1 3152static bool context_changed(const Context *context) {
03677889 3153 assert(context);
e594a3b1
LP
3154
3155 LIST_FOREACH(partitions, p, context->partitions) {
3156 if (p->dropped)
3157 continue;
3158
3159 if (p->allocated_to_area)
3160 return true;
3161
3162 if (p->new_size != p->current_size)
3163 return true;
3164 }
3165
3166 return false;
3167}
3168
81873a6b 3169static int context_wipe_range(Context *context, uint64_t offset, uint64_t size) {
e594a3b1
LP
3170 _cleanup_(blkid_free_probep) blkid_probe probe = NULL;
3171 int r;
3172
3173 assert(context);
81873a6b
LP
3174 assert(offset != UINT64_MAX);
3175 assert(size != UINT64_MAX);
e594a3b1
LP
3176
3177 probe = blkid_new_probe();
3178 if (!probe)
3179 return log_oom();
3180
e594a3b1 3181 errno = 0;
81873a6b 3182 r = blkid_probe_set_device(probe, fdisk_get_devfd(context->fdisk_context), offset, size);
e594a3b1 3183 if (r < 0)
81873a6b 3184 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to allocate device probe for wiping.");
e594a3b1
LP
3185
3186 errno = 0;
3187 if (blkid_probe_enable_superblocks(probe, true) < 0 ||
3188 blkid_probe_set_superblocks_flags(probe, BLKID_SUBLKS_MAGIC|BLKID_SUBLKS_BADCSUM) < 0 ||
3189 blkid_probe_enable_partitions(probe, true) < 0 ||
3190 blkid_probe_set_partitions_flags(probe, BLKID_PARTS_MAGIC) < 0)
81873a6b 3191 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to enable superblock and partition probing.");
e594a3b1
LP
3192
3193 for (;;) {
3194 errno = 0;
3195 r = blkid_do_probe(probe);
3196 if (r < 0)
9f08d7f7 3197 return log_error_errno(errno_or_else(EIO), "Failed to probe for file systems.");
e594a3b1
LP
3198 if (r > 0)
3199 break;
3200
3201 errno = 0;
3202 if (blkid_do_wipe(probe, false) < 0)
9f08d7f7 3203 return log_error_errno(errno_or_else(EIO), "Failed to wipe file system signature.");
e594a3b1
LP
3204 }
3205
e594a3b1
LP
3206 return 0;
3207}
3208
81873a6b
LP
3209static int context_wipe_partition(Context *context, Partition *p) {
3210 int r;
3211
3212 assert(context);
3213 assert(p);
3214 assert(!PARTITION_EXISTS(p)); /* Safety check: never wipe existing partitions */
3215
3216 assert(p->offset != UINT64_MAX);
3217 assert(p->new_size != UINT64_MAX);
3218
3219 r = context_wipe_range(context, p->offset, p->new_size);
3220 if (r < 0)
3221 return r;
3222
3223 log_info("Successfully wiped file system signatures from future partition %" PRIu64 ".", p->partno);
3224 return 0;
3225}
3226
3227static int context_discard_range(
3228 Context *context,
3229 uint64_t offset,
3230 uint64_t size) {
3231
e594a3b1
LP
3232 struct stat st;
3233 int fd;
3234
3235 assert(context);
3236 assert(offset != UINT64_MAX);
3237 assert(size != UINT64_MAX);
3238
3239 if (size <= 0)
3240 return 0;
3241
a26f4a49 3242 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
e594a3b1
LP
3243
3244 if (fstat(fd, &st) < 0)
3245 return -errno;
3246
3247 if (S_ISREG(st.st_mode)) {
3248 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, offset, size) < 0) {
3249 if (ERRNO_IS_NOT_SUPPORTED(errno))
3250 return -EOPNOTSUPP;
3251
3252 return -errno;
3253 }
3254
3255 return 1;
3256 }
3257
3258 if (S_ISBLK(st.st_mode)) {
3259 uint64_t range[2], end;
3260
994b3031 3261 range[0] = round_up_size(offset, context->sector_size);
e594a3b1 3262
55d38014
LP
3263 if (offset > UINT64_MAX - size)
3264 return -ERANGE;
3265
e594a3b1
LP
3266 end = offset + size;
3267 if (end <= range[0])
3268 return 0;
3269
994b3031 3270 range[1] = round_down_size(end - range[0], context->sector_size);
e594a3b1
LP
3271 if (range[1] <= 0)
3272 return 0;
3273
3274 if (ioctl(fd, BLKDISCARD, range) < 0) {
3275 if (ERRNO_IS_NOT_SUPPORTED(errno))
3276 return -EOPNOTSUPP;
3277
3278 return -errno;
3279 }
3280
3281 return 1;
3282 }
3283
3284 return -EOPNOTSUPP;
3285}
3286
3287static int context_discard_partition(Context *context, Partition *p) {
3288 int r;
3289
3290 assert(context);
3291 assert(p);
3292
3293 assert(p->offset != UINT64_MAX);
3294 assert(p->new_size != UINT64_MAX);
3295 assert(!PARTITION_EXISTS(p)); /* Safety check: never discard existing partitions */
3296
3297 if (!arg_discard)
3298 return 0;
3299
3300 r = context_discard_range(context, p->offset, p->new_size);
3301 if (r == -EOPNOTSUPP) {
5b5109e2 3302 log_info("Storage does not support discard, not discarding data in future partition %" PRIu64 ".", p->partno);
e594a3b1
LP
3303 return 0;
3304 }
22163eb5
LP
3305 if (r == -EBUSY) {
3306 /* Let's handle this gracefully: https://bugzilla.kernel.org/show_bug.cgi?id=211167 */
3307 log_info("Block device is busy, not discarding partition %" PRIu64 " because it probably is mounted.", p->partno);
3308 return 0;
3309 }
e594a3b1
LP
3310 if (r == 0) {
3311 log_info("Partition %" PRIu64 " too short for discard, skipping.", p->partno);
3312 return 0;
3313 }
3314 if (r < 0)
5b5109e2 3315 return log_error_errno(r, "Failed to discard data for future partition %" PRIu64 ".", p->partno);
e594a3b1 3316
5b5109e2 3317 log_info("Successfully discarded data from future partition %" PRIu64 ".", p->partno);
e594a3b1
LP
3318 return 1;
3319}
3320
3321static int context_discard_gap_after(Context *context, Partition *p) {
3322 uint64_t gap, next = UINT64_MAX;
e594a3b1
LP
3323 int r;
3324
3325 assert(context);
3326 assert(!p || (p->offset != UINT64_MAX && p->new_size != UINT64_MAX));
3327
5113436b
AF
3328 if (!arg_discard)
3329 return 0;
3330
e594a3b1
LP
3331 if (p)
3332 gap = p->offset + p->new_size;
3333 else
771805eb
SS
3334 /* The context start gets rounded up to grain_size, however
3335 * existing partitions may be before that so ensure the gap
3336 * starts at the first actually usable lba
3337 */
3338 gap = fdisk_get_first_lba(context->fdisk_context) * context->sector_size;
e594a3b1
LP
3339
3340 LIST_FOREACH(partitions, q, context->partitions) {
3341 if (q->dropped)
3342 continue;
3343
3344 assert(q->offset != UINT64_MAX);
3345 assert(q->new_size != UINT64_MAX);
3346
3347 if (q->offset < gap)
3348 continue;
3349
3350 if (next == UINT64_MAX || q->offset < next)
3351 next = q->offset;
3352 }
3353
3354 if (next == UINT64_MAX) {
771805eb 3355 next = (fdisk_get_last_lba(context->fdisk_context) + 1) * context->sector_size;
e594a3b1
LP
3356 if (gap > next)
3357 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end.");
3358 }
3359
3360 assert(next >= gap);
3361 r = context_discard_range(context, gap, next - gap);
3362 if (r == -EOPNOTSUPP) {
3363 if (p)
5b5109e2 3364 log_info("Storage does not support discard, not discarding gap after partition %" PRIu64 ".", p->partno);
e594a3b1 3365 else
5b5109e2 3366 log_info("Storage does not support discard, not discarding gap at beginning of disk.");
e594a3b1
LP
3367 return 0;
3368 }
3369 if (r == 0) /* Too short */
3370 return 0;
3371 if (r < 0) {
3372 if (p)
3373 return log_error_errno(r, "Failed to discard gap after partition %" PRIu64 ".", p->partno);
3374 else
3375 return log_error_errno(r, "Failed to discard gap at beginning of disk.");
3376 }
3377
3378 if (p)
3379 log_info("Successfully discarded gap after partition %" PRIu64 ".", p->partno);
3380 else
3381 log_info("Successfully discarded gap at beginning of disk.");
3382
3383 return 0;
3384}
3385
cc751c75 3386static int context_wipe_and_discard(Context *context) {
e594a3b1
LP
3387 int r;
3388
3389 assert(context);
3390
19c58989
LP
3391 if (arg_empty == EMPTY_CREATE) /* If we just created the image, no need to wipe */
3392 return 0;
3393
e594a3b1
LP
3394 /* Wipe and discard the contents of all partitions we are about to create. We skip the discarding if
3395 * we were supposed to start from scratch anyway, as in that case we just discard the whole block
3396 * device in one go early on. */
3397
3398 LIST_FOREACH(partitions, p, context->partitions) {
3399
3400 if (!p->allocated_to_area)
3401 continue;
3402
c74d50ff 3403 if (partition_type_defer(&p->type))
81d1098b
DDM
3404 continue;
3405
e594a3b1
LP
3406 r = context_wipe_partition(context, p);
3407 if (r < 0)
3408 return r;
3409
cc751c75 3410 if (!context->from_scratch) {
f0cb1b95
LP
3411 r = context_discard_partition(context, p);
3412 if (r < 0)
3413 return r;
3414
e594a3b1
LP
3415 r = context_discard_gap_after(context, p);
3416 if (r < 0)
3417 return r;
3418 }
3419 }
3420
cc751c75 3421 if (!context->from_scratch) {
e594a3b1
LP
3422 r = context_discard_gap_after(context, NULL);
3423 if (r < 0)
3424 return r;
3425 }
3426
3427 return 0;
3428}
3429
a09ae915
DDM
3430typedef struct DecryptedPartitionTarget {
3431 int fd;
fd9b68d9 3432 char *dm_name;
a09ae915
DDM
3433 char *volume;
3434 struct crypt_device *device;
3435} DecryptedPartitionTarget;
3436
3437static DecryptedPartitionTarget* decrypted_partition_target_free(DecryptedPartitionTarget *t) {
465261bf 3438#if HAVE_LIBCRYPTSETUP
a09ae915
DDM
3439 int r;
3440
3441 if (!t)
3442 return NULL;
3443
a09ae915
DDM
3444 safe_close(t->fd);
3445
fd9b68d9
DDM
3446 /* udev or so might access out block device in the background while we are done. Let's hence
3447 * force detach the volume. We sync'ed before, hence this should be safe. */
3448 r = sym_crypt_deactivate_by_name(t->device, t->dm_name, CRYPT_DEACTIVATE_FORCE);
3449 if (r < 0)
3450 log_warning_errno(r, "Failed to deactivate LUKS device, ignoring: %m");
a09ae915
DDM
3451
3452 sym_crypt_free(t->device);
fd9b68d9 3453 free(t->dm_name);
a09ae915
DDM
3454 free(t->volume);
3455 free(t);
3456#endif
3457 return NULL;
3458}
3459
a64769d6
DDM
3460typedef struct {
3461 LoopDevice *loop;
3462 int fd;
3463 char *path;
3464 int whole_fd;
a09ae915 3465 DecryptedPartitionTarget *decrypted;
a64769d6
DDM
3466} PartitionTarget;
3467
3468static int partition_target_fd(PartitionTarget *t) {
3469 assert(t);
3470 assert(t->loop || t->fd >= 0 || t->whole_fd >= 0);
a09ae915
DDM
3471
3472 if (t->decrypted)
3473 return t->decrypted->fd;
3474
3475 if (t->loop)
3476 return t->loop->fd;
3477
3478 if (t->fd >= 0)
3479 return t->fd;
3480
3481 return t->whole_fd;
a64769d6
DDM
3482}
3483
3484static const char* partition_target_path(PartitionTarget *t) {
3485 assert(t);
3486 assert(t->loop || t->path);
a09ae915
DDM
3487
3488 if (t->decrypted)
3489 return t->decrypted->volume;
3490
3491 if (t->loop)
3492 return t->loop->node;
3493
3494 return t->path;
a64769d6
DDM
3495}
3496
3497static PartitionTarget *partition_target_free(PartitionTarget *t) {
3498 if (!t)
3499 return NULL;
3500
a09ae915 3501 decrypted_partition_target_free(t->decrypted);
a64769d6
DDM
3502 loop_device_unref(t->loop);
3503 safe_close(t->fd);
3504 unlink_and_free(t->path);
3505
3506 return mfree(t);
3507}
3508
3509DEFINE_TRIVIAL_CLEANUP_FUNC(PartitionTarget*, partition_target_free);
3510
d3201eb4
DDM
3511static int prepare_temporary_file(PartitionTarget *t, uint64_t size) {
3512 _cleanup_(unlink_and_freep) char *temp = NULL;
254d1313 3513 _cleanup_close_ int fd = -EBADF;
d3201eb4
DDM
3514 const char *vt;
3515 int r;
3516
3517 assert(t);
3518
3519 r = var_tmp_dir(&vt);
3520 if (r < 0)
3521 return log_error_errno(r, "Could not determine temporary directory: %m");
3522
3523 temp = path_join(vt, "repart-XXXXXX");
3524 if (!temp)
3525 return log_oom();
3526
3527 fd = mkostemp_safe(temp);
3528 if (fd < 0)
3529 return log_error_errno(fd, "Failed to create temporary file: %m");
3530
3531 if (ftruncate(fd, size) < 0)
3532 return log_error_errno(errno, "Failed to truncate temporary file to %s: %m",
9f08d7f7 3533 FORMAT_BYTES(size));
d3201eb4
DDM
3534
3535 t->fd = TAKE_FD(fd);
3536 t->path = TAKE_PTR(temp);
3537
3538 return 0;
3539}
3540
a64769d6
DDM
3541static int partition_target_prepare(
3542 Context *context,
3543 Partition *p,
3544 uint64_t size,
3545 bool need_path,
3546 PartitionTarget **ret) {
3547
3548 _cleanup_(partition_target_freep) PartitionTarget *t = NULL;
d3201eb4
DDM
3549 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
3550 int whole_fd, r;
a64769d6
DDM
3551
3552 assert(context);
3553 assert(p);
3554 assert(ret);
3555
3556 assert_se((whole_fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
3557
6b1ea55e 3558 t = new(PartitionTarget, 1);
a64769d6
DDM
3559 if (!t)
3560 return log_oom();
6b1ea55e 3561 *t = (PartitionTarget) {
254d1313
ZJS
3562 .fd = -EBADF,
3563 .whole_fd = -EBADF,
6b1ea55e 3564 };
a64769d6 3565
d3201eb4 3566 if (!need_path) {
86cbbc6d 3567 if (lseek(whole_fd, p->offset, SEEK_SET) < 0)
d3201eb4 3568 return log_error_errno(errno, "Failed to seek to partition offset: %m");
a64769d6 3569
d3201eb4
DDM
3570 t->whole_fd = whole_fd;
3571 *ret = TAKE_PTR(t);
3572 return 0;
3573 }
a64769d6 3574
d3201eb4
DDM
3575 /* Loopback block devices are not only useful to turn regular files into block devices, but
3576 * also to cut out sections of block devices into new block devices. */
a64769d6 3577
fc10b158 3578 if (arg_offline <= 0) {
66a8c74c 3579 r = loop_device_make(whole_fd, O_RDWR, p->offset, size, context->sector_size, 0, LOCK_EX, &d);
ffd9d75c 3580 if (r < 0 && (arg_offline == 0 || (r != -ENOENT && !ERRNO_IS_PRIVILEGE(r)) || !strv_isempty(p->subvolumes)))
fc10b158
DDM
3581 return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno);
3582 if (r >= 0) {
3583 t->loop = TAKE_PTR(d);
3584 *ret = TAKE_PTR(t);
3585 return 0;
3586 }
3587
3588 log_debug_errno(r, "No access to loop devices, falling back to a regular file");
d3201eb4 3589 }
a64769d6 3590
d3201eb4
DDM
3591 /* If we can't allocate a loop device, let's write to a regular file that we copy into the final
3592 * image so we can run in containers and without needing root privileges. On filesystems with
3593 * reflinking support, we can take advantage of this and just reflink the result into the image.
3594 */
a64769d6 3595
d3201eb4
DDM
3596 r = prepare_temporary_file(t, size);
3597 if (r < 0)
3598 return r;
a64769d6
DDM
3599
3600 *ret = TAKE_PTR(t);
3601
3602 return 0;
3603}
3604
3605static int partition_target_grow(PartitionTarget *t, uint64_t size) {
3606 int r;
3607
3608 assert(t);
a09ae915 3609 assert(!t->decrypted);
a64769d6
DDM
3610
3611 if (t->loop) {
3612 r = loop_device_refresh_size(t->loop, UINT64_MAX, size);
3613 if (r < 0)
3614 return log_error_errno(r, "Failed to refresh loopback device size: %m");
3615 } else if (t->fd >= 0) {
3616 if (ftruncate(t->fd, size) < 0)
3617 return log_error_errno(errno, "Failed to grow '%s' to %s by truncation: %m",
3618 t->path, FORMAT_BYTES(size));
3619 }
3620
3621 return 0;
3622}
3623
3624static int partition_target_sync(Context *context, Partition *p, PartitionTarget *t) {
3625 int whole_fd, r;
3626
3627 assert(context);
3628 assert(p);
3629 assert(t);
3630
3631 assert_se((whole_fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
3632
a09ae915
DDM
3633 if (t->decrypted && fsync(t->decrypted->fd) < 0)
3634 return log_error_errno(errno, "Failed to sync changes to '%s': %m", t->decrypted->volume);
3635
a64769d6
DDM
3636 if (t->loop) {
3637 r = loop_device_sync(t->loop);
3638 if (r < 0)
3639 return log_error_errno(r, "Failed to sync loopback device: %m");
3640 } else if (t->fd >= 0) {
e21be797
DDM
3641 struct stat st;
3642
86cbbc6d 3643 if (lseek(whole_fd, p->offset, SEEK_SET) < 0)
a64769d6
DDM
3644 return log_error_errno(errno, "Failed to seek to partition offset: %m");
3645
86cbbc6d 3646 if (lseek(t->fd, 0, SEEK_SET) < 0)
6eccec00
DDM
3647 return log_error_errno(errno, "Failed to seek to start of temporary file: %m");
3648
e21be797
DDM
3649 if (fstat(t->fd, &st) < 0)
3650 return log_error_errno(errno, "Failed to stat temporary file: %m");
3651
3652 if (st.st_size > (off_t) p->new_size)
3653 return log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
3654 "Partition %" PRIu64 "'s contents (%s) don't fit in the partition (%s)",
3655 p->partno, FORMAT_BYTES(st.st_size), FORMAT_BYTES(p->new_size));
3656
a64769d6
DDM
3657 r = copy_bytes(t->fd, whole_fd, UINT64_MAX, COPY_REFLINK|COPY_HOLES|COPY_FSYNC);
3658 if (r < 0)
3659 return log_error_errno(r, "Failed to copy bytes to partition: %m");
3660 } else {
3661 if (fsync(t->whole_fd) < 0)
3662 return log_error_errno(errno, "Failed to sync changes: %m");
3663 }
3664
3665 return 0;
3666}
3667
a09ae915 3668static int partition_encrypt(Context *context, Partition *p, PartitionTarget *target, bool offline) {
48a09a8f 3669#if HAVE_LIBCRYPTSETUP && HAVE_CRYPT_SET_DATA_OFFSET && HAVE_CRYPT_REENCRYPT_INIT_BY_PASSPHRASE && HAVE_CRYPT_REENCRYPT
a09ae915 3670 const char *node = partition_target_path(target);
48a09a8f 3671 struct crypt_params_luks2 luks_params = {
922576e4 3672 .label = strempty(ASSERT_PTR(p)->new_label),
93f125a6 3673 .sector_size = ASSERT_PTR(context)->fs_sector_size,
a09ae915 3674 .data_device = offline ? node : NULL,
48a09a8f
DDM
3675 };
3676 struct crypt_params_reencrypt reencrypt_params = {
3677 .mode = CRYPT_REENCRYPT_ENCRYPT,
3678 .direction = CRYPT_REENCRYPT_BACKWARD,
3679 .resilience = "datashift",
3680 .data_shift = LUKS2_METADATA_SIZE / 512,
3681 .luks2 = &luks_params,
3682 .flags = CRYPT_REENCRYPT_INITIALIZE_ONLY|CRYPT_REENCRYPT_MOVE_FIRST_SEGMENT,
3683 };
0d12936d 3684 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
48a09a8f
DDM
3685 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
3686 _cleanup_fclose_ FILE *h = NULL;
a09ae915 3687 _cleanup_free_ char *hp = NULL, *vol = NULL, *dm_name = NULL;
48a09a8f
DDM
3688 const char *passphrase = NULL;
3689 size_t passphrase_size = 0;
0b75744d 3690 const char *vt;
b9df3536
LP
3691 int r;
3692
994b3031 3693 assert(context);
b9df3536 3694 assert(p);
889914ef
LP
3695 assert(p->encrypt != ENCRYPT_OFF);
3696
0d12936d
LP
3697 r = dlopen_cryptsetup();
3698 if (r < 0)
3699 return log_error_errno(r, "libcryptsetup not found, cannot encrypt: %m");
3700
b9df3536
LP
3701 log_info("Encrypting future partition %" PRIu64 "...", p->partno);
3702
a09ae915
DDM
3703 if (offline) {
3704 r = var_tmp_dir(&vt);
3705 if (r < 0)
3706 return log_error_errno(r, "Failed to determine temporary files directory: %m");
0b75744d 3707
a09ae915
DDM
3708 r = fopen_temporary_child(vt, &h, &hp);
3709 if (r < 0)
3710 return log_error_errno(r, "Failed to create temporary LUKS header file: %m");
48a09a8f 3711
a09ae915
DDM
3712 /* Weird cryptsetup requirement which requires the header file to be the size of at least one
3713 * sector. */
9f08d7f7
MY
3714 if (ftruncate(fileno(h), luks_params.sector_size) < 0)
3715 return log_error_errno(errno, "Failed to grow temporary LUKS header file: %m");
a09ae915
DDM
3716 } else {
3717 if (asprintf(&dm_name, "luks-repart-%08" PRIx64, random_u64()) < 0)
3718 return log_oom();
3719
3720 vol = path_join("/dev/mapper/", dm_name);
3721 if (!vol)
3722 return log_oom();
3723 }
48a09a8f 3724
a09ae915 3725 r = sym_crypt_init(&cd, offline ? hp : node);
48a09a8f
DDM
3726 if (r < 0)
3727 return log_error_errno(r, "Failed to allocate libcryptsetup context for %s: %m", hp);
b9df3536
LP
3728
3729 cryptsetup_enable_logging(cd);
3730
a09ae915
DDM
3731 if (offline) {
3732 /* Disable kernel keyring usage by libcryptsetup as a workaround for
3733 * https://gitlab.com/cryptsetup/cryptsetup/-/merge_requests/273. This makes sure that we can
3734 * do offline encryption even when repart is running in a container. */
3735 r = sym_crypt_volume_key_keyring(cd, false);
3736 if (r < 0)
3737 return log_error_errno(r, "Failed to disable kernel keyring: %m");
48a09a8f 3738
a09ae915
DDM
3739 r = sym_crypt_metadata_locking(cd, false);
3740 if (r < 0)
3741 return log_error_errno(r, "Failed to disable metadata locking: %m");
48a09a8f 3742
a09ae915
DDM
3743 r = sym_crypt_set_data_offset(cd, LUKS2_METADATA_SIZE / 512);
3744 if (r < 0)
3745 return log_error_errno(r, "Failed to set data offset: %m");
3746 }
48a09a8f 3747
9f08d7f7
MY
3748 r = sym_crypt_format(
3749 cd,
3750 CRYPT_LUKS2,
3751 "aes",
3752 "xts-plain64",
3753 SD_ID128_TO_UUID_STRING(p->luks_uuid),
3754 NULL,
3755 VOLUME_KEY_SIZE,
3756 &luks_params);
b9df3536
LP
3757 if (r < 0)
3758 return log_error_errno(r, "Failed to LUKS2 format future partition: %m");
3759
889914ef
LP
3760 if (IN_SET(p->encrypt, ENCRYPT_KEY_FILE, ENCRYPT_KEY_FILE_TPM2)) {
3761 r = sym_crypt_keyslot_add_by_volume_key(
3762 cd,
3763 CRYPT_ANY_SLOT,
98e0456e
DDM
3764 NULL,
3765 VOLUME_KEY_SIZE,
889914ef
LP
3766 strempty(arg_key),
3767 arg_key_size);
3768 if (r < 0)
3769 return log_error_errno(r, "Failed to add LUKS2 key: %m");
48a09a8f
DDM
3770
3771 passphrase = strempty(arg_key);
3772 passphrase_size = arg_key_size;
889914ef
LP
3773 }
3774
3775 if (IN_SET(p->encrypt, ENCRYPT_TPM2, ENCRYPT_KEY_FILE_TPM2)) {
3776#if HAVE_TPM2
889914ef
LP
3777 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
3778 _cleanup_(erase_and_freep) void *secret = NULL;
02ef97cd 3779 _cleanup_free_ void *pubkey = NULL;
9e437994
DS
3780 _cleanup_free_ void *blob = NULL, *srk_buf = NULL;
3781 size_t secret_size, blob_size, pubkey_size = 0, srk_buf_size = 0;
5e476b85 3782 ssize_t base64_encoded_size;
889914ef 3783 int keyslot;
59c8afbc 3784 TPM2Flags flags = 0;
889914ef 3785
02ef97cd
LP
3786 if (arg_tpm2_public_key_pcr_mask != 0) {
3787 r = tpm2_load_pcr_public_key(arg_tpm2_public_key, &pubkey, &pubkey_size);
3788 if (r < 0) {
3789 if (arg_tpm2_public_key || r != -ENOENT)
2e9f6072 3790 return log_error_errno(r, "Failed to read TPM PCR public key: %m");
02ef97cd
LP
3791
3792 log_debug_errno(r, "Failed to read TPM2 PCR public key, proceeding without: %m");
3793 arg_tpm2_public_key_pcr_mask = 0;
3794 }
3795 }
3796
9e437994
DS
3797 TPM2B_PUBLIC public;
3798 if (pubkey) {
3799 r = tpm2_tpm2b_public_from_pem(pubkey, pubkey_size, &public);
3800 if (r < 0)
3801 return log_error_errno(r, "Could not convert public key to TPM2B_PUBLIC: %m");
3802 }
3803
78fdf0f6
LP
3804 _cleanup_(tpm2_pcrlock_policy_done) Tpm2PCRLockPolicy pcrlock_policy = {};
3805 if (arg_tpm2_pcrlock) {
3806 r = tpm2_pcrlock_policy_load(arg_tpm2_pcrlock, &pcrlock_policy);
3807 if (r < 0)
3808 return r;
3809
3810 flags |= TPM2_FLAGS_USE_PCRLOCK;
3811 }
3812
3813 _cleanup_(tpm2_context_unrefp) Tpm2Context *tpm2_context = NULL;
3814 TPM2B_PUBLIC device_key_public = {};
3815 if (arg_tpm2_device_key) {
3816 r = tpm2_load_public_key_file(arg_tpm2_device_key, &device_key_public);
3817 if (r < 0)
3818 return r;
3819
3820 if (!tpm2_pcr_values_has_all_values(arg_tpm2_hash_pcr_values, arg_tpm2_n_hash_pcr_values))
3821 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
3822 "Must provide all PCR values when using TPM2 device key.");
3823 } else {
3824 r = tpm2_context_new(arg_tpm2_device, &tpm2_context);
3825 if (r < 0)
3826 return log_error_errno(r, "Failed to create TPM2 context: %m");
3827
3828 if (!tpm2_pcr_values_has_all_values(arg_tpm2_hash_pcr_values, arg_tpm2_n_hash_pcr_values)) {
3829 r = tpm2_pcr_read_missing_values(tpm2_context, arg_tpm2_hash_pcr_values, arg_tpm2_n_hash_pcr_values);
3830 if (r < 0)
3831 return log_error_errno(r, "Could not read pcr values: %m");
3832 }
3833 }
9e437994
DS
3834
3835 uint16_t hash_pcr_bank = 0;
3836 uint32_t hash_pcr_mask = 0;
3837 if (arg_tpm2_n_hash_pcr_values > 0) {
3838 size_t hash_count;
3839 r = tpm2_pcr_values_hash_count(arg_tpm2_hash_pcr_values, arg_tpm2_n_hash_pcr_values, &hash_count);
3840 if (r < 0)
3841 return log_error_errno(r, "Could not get hash count: %m");
3842
3843 if (hash_count > 1)
3844 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Multiple PCR banks selected.");
3845
3846 hash_pcr_bank = arg_tpm2_hash_pcr_values[0].hash;
3847 r = tpm2_pcr_values_to_mask(arg_tpm2_hash_pcr_values, arg_tpm2_n_hash_pcr_values, hash_pcr_bank, &hash_pcr_mask);
3848 if (r < 0)
3849 return log_error_errno(r, "Could not get hash mask: %m");
3850 }
3851
3852 TPM2B_DIGEST policy = TPM2B_DIGEST_MAKE(NULL, TPM2_SHA256_DIGEST_SIZE);
a4342701
LP
3853 r = tpm2_calculate_sealing_policy(
3854 arg_tpm2_hash_pcr_values,
3855 arg_tpm2_n_hash_pcr_values,
3856 pubkey ? &public : NULL,
3857 /* use_pin= */ false,
404aea78 3858 arg_tpm2_pcrlock ? &pcrlock_policy : NULL,
a4342701 3859 &policy);
9e437994 3860 if (r < 0)
f9a0ee75 3861 return log_error_errno(r, "Could not calculate sealing policy digest: %m");
9e437994 3862
78fdf0f6
LP
3863 if (arg_tpm2_device_key)
3864 r = tpm2_calculate_seal(
3865 arg_tpm2_seal_key_handle,
3866 &device_key_public,
3867 /* attributes= */ NULL,
3868 /* secret= */ NULL, /* secret_size= */ 0,
3869 &policy,
3870 /* pin= */ NULL,
3871 &secret, &secret_size,
3872 &blob, &blob_size,
3873 &srk_buf, &srk_buf_size);
3874 else
3875 r = tpm2_seal(tpm2_context,
3876 arg_tpm2_seal_key_handle,
3877 &policy,
3878 /* pin= */ NULL,
3879 &secret, &secret_size,
3880 &blob, &blob_size,
3881 /* ret_primary_alg= */ NULL,
3882 &srk_buf, &srk_buf_size);
889914ef
LP
3883 if (r < 0)
3884 return log_error_errno(r, "Failed to seal to TPM2: %m");
3885
5e476b85
LP
3886 base64_encoded_size = base64mem(secret, secret_size, &base64_encoded);
3887 if (base64_encoded_size < 0)
3888 return log_error_errno(base64_encoded_size, "Failed to base64 encode secret key: %m");
889914ef
LP
3889
3890 r = cryptsetup_set_minimal_pbkdf(cd);
3891 if (r < 0)
3892 return log_error_errno(r, "Failed to set minimal PBKDF: %m");
3893
3894 keyslot = sym_crypt_keyslot_add_by_volume_key(
3895 cd,
3896 CRYPT_ANY_SLOT,
78fdf0f6
LP
3897 /* volume_key= */ NULL,
3898 /* volume_key_size= */ VOLUME_KEY_SIZE,
889914ef 3899 base64_encoded,
5e476b85 3900 base64_encoded_size);
889914ef 3901 if (keyslot < 0)
48a09a8f 3902 return log_error_errno(keyslot, "Failed to add new TPM2 key: %m");
889914ef 3903
f0f4fcae
LP
3904 r = tpm2_make_luks2_json(
3905 keyslot,
9e437994
DS
3906 hash_pcr_mask,
3907 hash_pcr_bank,
02ef97cd
LP
3908 pubkey, pubkey_size,
3909 arg_tpm2_public_key_pcr_mask,
9e437994 3910 /* primary_alg= */ 0,
f0f4fcae 3911 blob, blob_size,
9e437994 3912 policy.buffer, policy.size,
aae6eb96 3913 NULL, 0, /* no salt because tpm2_seal has no pin */
acbb504e 3914 srk_buf, srk_buf_size,
78fdf0f6 3915 flags,
f0f4fcae 3916 &v);
889914ef
LP
3917 if (r < 0)
3918 return log_error_errno(r, "Failed to prepare TPM2 JSON token object: %m");
3919
3920 r = cryptsetup_add_token_json(cd, v);
3921 if (r < 0)
3922 return log_error_errno(r, "Failed to add TPM2 JSON token to LUKS2 header: %m");
48a09a8f
DDM
3923
3924 passphrase = base64_encoded;
3925 passphrase_size = strlen(base64_encoded);
889914ef
LP
3926#else
3927 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
3928 "Support for TPM2 enrollment not enabled.");
3929#endif
3930 }
b9df3536 3931
a09ae915
DDM
3932 if (offline) {
3933 r = sym_crypt_reencrypt_init_by_passphrase(
3934 cd,
3935 NULL,
3936 passphrase,
3937 passphrase_size,
3938 CRYPT_ANY_SLOT,
3939 0,
3940 sym_crypt_get_cipher(cd),
3941 sym_crypt_get_cipher_mode(cd),
3942 &reencrypt_params);
3943 if (r < 0)
3944 return log_error_errno(r, "Failed to prepare for reencryption: %m");
b9df3536 3945
a09ae915
DDM
3946 /* crypt_reencrypt_init_by_passphrase() doesn't actually put the LUKS header at the front, we
3947 * have to do that ourselves. */
b9df3536 3948
a09ae915
DDM
3949 sym_crypt_free(cd);
3950 cd = NULL;
b9df3536 3951
a09ae915
DDM
3952 r = sym_crypt_init(&cd, node);
3953 if (r < 0)
3954 return log_error_errno(r, "Failed to allocate libcryptsetup context for %s: %m", node);
b9df3536 3955
a09ae915
DDM
3956 r = sym_crypt_header_restore(cd, CRYPT_LUKS2, hp);
3957 if (r < 0)
3958 return log_error_errno(r, "Failed to place new LUKS header at head of %s: %m", node);
b9df3536 3959
a09ae915 3960 reencrypt_params.flags &= ~CRYPT_REENCRYPT_INITIALIZE_ONLY;
b9df3536 3961
a09ae915
DDM
3962 r = sym_crypt_reencrypt_init_by_passphrase(
3963 cd,
3964 NULL,
3965 passphrase,
3966 passphrase_size,
3967 CRYPT_ANY_SLOT,
3968 0,
3969 NULL,
3970 NULL,
3971 &reencrypt_params);
3972 if (r < 0)
3973 return log_error_errno(r, "Failed to load reencryption context: %m");
b9df3536 3974
a09ae915
DDM
3975 r = sym_crypt_reencrypt(cd, NULL);
3976 if (r < 0)
3977 return log_error_errno(r, "Failed to encrypt %s: %m", node);
3978 } else {
3979 _cleanup_free_ DecryptedPartitionTarget *t = NULL;
3980 _cleanup_close_ int dev_fd = -1;
3981
3982 r = sym_crypt_activate_by_volume_key(
3983 cd,
3984 dm_name,
3985 NULL,
3986 VOLUME_KEY_SIZE,
3987 arg_discard ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0);
3988 if (r < 0)
3989 return log_error_errno(r, "Failed to activate LUKS superblock: %m");
3990
3991 dev_fd = open(vol, O_RDWR|O_CLOEXEC|O_NOCTTY);
3992 if (dev_fd < 0)
3993 return log_error_errno(errno, "Failed to open LUKS volume '%s': %m", vol);
3994
3995 if (flock(dev_fd, LOCK_EX) < 0)
3996 return log_error_errno(errno, "Failed to lock '%s': %m", vol);
3997
3998 t = new(DecryptedPartitionTarget, 1);
3999 if (!t)
4000 return log_oom();
4001
4002 *t = (DecryptedPartitionTarget) {
4003 .fd = TAKE_FD(dev_fd),
fd9b68d9 4004 .dm_name = TAKE_PTR(dm_name),
a09ae915
DDM
4005 .volume = TAKE_PTR(vol),
4006 .device = TAKE_PTR(cd),
4007 };
4008
4009 target->decrypted = TAKE_PTR(t);
4010 }
48a09a8f
DDM
4011
4012 log_info("Successfully encrypted future partition %" PRIu64 ".", p->partno);
b9df3536 4013
3dd8ae5c 4014 return 0;
48a09a8f
DDM
4015#else
4016 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
4017 "libcryptsetup is not supported or is missing required symbols, cannot encrypt: %m");
3dd8ae5c 4018#endif
b9df3536
LP
4019}
4020
2b392d86
DDM
4021static int partition_format_verity_hash(
4022 Context *context,
4023 Partition *p,
13bde177 4024 const char *node,
2b392d86
DDM
4025 const char *data_node) {
4026
4027#if HAVE_LIBCRYPTSETUP
4028 Partition *dp;
a64769d6 4029 _cleanup_(partition_target_freep) PartitionTarget *t = NULL;
2b392d86 4030 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
cf18d96f 4031 _cleanup_free_ char *hint = NULL;
a64769d6 4032 int r;
2b392d86
DDM
4033
4034 assert(context);
4035 assert(p);
deeae10e 4036 assert(p->verity == VERITY_HASH);
2b392d86
DDM
4037 assert(data_node);
4038
4039 if (p->dropped)
4040 return 0;
4041
4042 if (PARTITION_EXISTS(p)) /* Never format existing partitions */
4043 return 0;
4044
5eef7047
DDM
4045 /* Minimized partitions will use the copy blocks logic so let's make sure to skip those here. */
4046 if (p->copy_blocks_fd >= 0)
4047 return 0;
4048
2b392d86
DDM
4049 assert_se(dp = p->siblings[VERITY_DATA]);
4050 assert(!dp->dropped);
4051
cf18d96f
DDM
4052 (void) partition_hint(p, node, &hint);
4053
2b392d86
DDM
4054 r = dlopen_cryptsetup();
4055 if (r < 0)
4056 return log_error_errno(r, "libcryptsetup not found, cannot setup verity: %m");
4057
13bde177
DDM
4058 if (!node) {
4059 r = partition_target_prepare(context, p, p->new_size, /*need_path=*/ true, &t);
4060 if (r < 0)
4061 return r;
2b392d86 4062
13bde177
DDM
4063 node = partition_target_path(t);
4064 }
4065
c380047b
MC
4066 if (p->verity_data_block_size == UINT64_MAX)
4067 p->verity_data_block_size = context->fs_sector_size;
4068 if (p->verity_hash_block_size == UINT64_MAX)
4069 p->verity_hash_block_size = context->fs_sector_size;
4070
13bde177 4071 r = sym_crypt_init(&cd, node);
2b392d86 4072 if (r < 0)
cf18d96f 4073 return log_error_errno(r, "Failed to allocate libcryptsetup context for %s: %m", node);
2b392d86 4074
e21be797
DDM
4075 cryptsetup_enable_logging(cd);
4076
2b392d86 4077 r = sym_crypt_format(
81e04781 4078 cd, CRYPT_VERITY, NULL, NULL, SD_ID128_TO_UUID_STRING(p->verity_uuid), NULL, 0,
2b392d86
DDM
4079 &(struct crypt_params_verity){
4080 .data_device = data_node,
4081 .flags = CRYPT_VERITY_CREATE_HASH,
4082 .hash_name = "sha256",
4083 .hash_type = 1,
c380047b
MC
4084 .data_block_size = p->verity_data_block_size,
4085 .hash_block_size = p->verity_hash_block_size,
81e04781
VH
4086 .salt_size = sizeof(p->verity_salt),
4087 .salt = (const char*)p->verity_salt,
2b392d86 4088 });
e21be797
DDM
4089 if (r < 0) {
4090 /* libcryptsetup reports non-descriptive EIO errors for every I/O failure. Luckily, it
4091 * doesn't clobber errno so let's check for ENOSPC so we can report a better error if the
4092 * partition is too small. */
4093 if (r == -EIO && errno == ENOSPC)
4094 return log_error_errno(errno,
cf18d96f
DDM
4095 "Verity hash data does not fit in partition %s with size %s",
4096 strna(hint), FORMAT_BYTES(p->new_size));
e21be797 4097
cf18d96f 4098 return log_error_errno(r, "Failed to setup verity hash data of partition %s: %m", strna(hint));
e21be797 4099 }
2b392d86 4100
13bde177
DDM
4101 if (t) {
4102 r = partition_target_sync(context, p, t);
4103 if (r < 0)
4104 return r;
4105 }
a64769d6 4106
2b392d86
DDM
4107 r = sym_crypt_get_volume_key_size(cd);
4108 if (r < 0)
cf18d96f 4109 return log_error_errno(r, "Failed to determine verity root hash size of partition %s: %m", strna(hint));
2b392d86 4110
6b4b40f4
LP
4111 _cleanup_(iovec_done) struct iovec rh = {
4112 .iov_base = malloc(r),
4113 .iov_len = r,
4114 };
4115 if (!rh.iov_base)
2b392d86
DDM
4116 return log_oom();
4117
6b4b40f4 4118 r = sym_crypt_volume_key_get(cd, CRYPT_ANY_SLOT, (char *) rh.iov_base, &rh.iov_len, NULL, 0);
2b392d86 4119 if (r < 0)
cf18d96f 4120 return log_error_errno(r, "Failed to get verity root hash of partition %s: %m", strna(hint));
2b392d86 4121
6b4b40f4 4122 assert(rh.iov_len >= sizeof(sd_id128_t) * 2);
2b392d86
DDM
4123
4124 if (!dp->new_uuid_is_set) {
6b4b40f4 4125 memcpy_safe(dp->new_uuid.bytes, rh.iov_base, sizeof(sd_id128_t));
2b392d86
DDM
4126 dp->new_uuid_is_set = true;
4127 }
4128
4129 if (!p->new_uuid_is_set) {
6b4b40f4 4130 memcpy_safe(p->new_uuid.bytes, (uint8_t*) rh.iov_base + (rh.iov_len - sizeof(sd_id128_t)), sizeof(sd_id128_t));
2b392d86
DDM
4131 p->new_uuid_is_set = true;
4132 }
4133
3c9783c7 4134 p->roothash = TAKE_STRUCT(rh);
2b392d86
DDM
4135
4136 return 0;
4137#else
4138 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libcryptsetup is not supported, cannot setup verity hashes: %m");
4139#endif
4140}
4141
4ecd39c5 4142static int sign_verity_roothash(
6b4b40f4
LP
4143 const struct iovec *roothash,
4144 struct iovec *ret_signature) {
4ecd39c5
DDM
4145
4146#if HAVE_OPENSSL
4147 _cleanup_(BIO_freep) BIO *rb = NULL;
4148 _cleanup_(PKCS7_freep) PKCS7 *p7 = NULL;
4149 _cleanup_free_ char *hex = NULL;
4150 _cleanup_free_ uint8_t *sig = NULL;
4151 int sigsz;
4152
4153 assert(roothash);
6b4b40f4 4154 assert(iovec_is_set(roothash));
4ecd39c5 4155 assert(ret_signature);
4ecd39c5 4156
6b4b40f4 4157 hex = hexmem(roothash->iov_base, roothash->iov_len);
4ecd39c5
DDM
4158 if (!hex)
4159 return log_oom();
4160
4161 rb = BIO_new_mem_buf(hex, -1);
4162 if (!rb)
4163 return log_oom();
4164
4165 p7 = PKCS7_sign(arg_certificate, arg_private_key, NULL, rb, PKCS7_DETACHED|PKCS7_NOATTR|PKCS7_BINARY);
4166 if (!p7)
4167 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to calculate PKCS7 signature: %s",
4168 ERR_error_string(ERR_get_error(), NULL));
4169
4170 sigsz = i2d_PKCS7(p7, &sig);
4171 if (sigsz < 0)
4172 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to convert PKCS7 signature to DER: %s",
4173 ERR_error_string(ERR_get_error(), NULL));
4174
6b4b40f4
LP
4175 ret_signature->iov_base = TAKE_PTR(sig);
4176 ret_signature->iov_len = sigsz;
4ecd39c5
DDM
4177
4178 return 0;
4179#else
968d232d 4180 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "OpenSSL is not supported, cannot setup verity signature: %m");
4ecd39c5
DDM
4181#endif
4182}
4183
4184static int partition_format_verity_sig(Context *context, Partition *p) {
4185 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
7dc951ef 4186 _cleanup_(iovec_done) struct iovec sig = {};
cf18d96f 4187 _cleanup_free_ char *text = NULL, *hint = NULL;
4ecd39c5
DDM
4188 Partition *hp;
4189 uint8_t fp[X509_FINGERPRINT_SIZE];
4ecd39c5
DDM
4190 int whole_fd, r;
4191
4192 assert(p->verity == VERITY_SIG);
4193
4194 if (p->dropped)
4195 return 0;
4196
4197 if (PARTITION_EXISTS(p))
4198 return 0;
4199
cf18d96f
DDM
4200 (void) partition_hint(p, context->node, &hint);
4201
4ecd39c5
DDM
4202 assert_se(hp = p->siblings[VERITY_HASH]);
4203 assert(!hp->dropped);
4204
4205 assert(arg_certificate);
4206
4207 assert_se((whole_fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
4208
6b4b40f4 4209 r = sign_verity_roothash(&hp->roothash, &sig);
4ecd39c5
DDM
4210 if (r < 0)
4211 return r;
4212
4213 r = x509_fingerprint(arg_certificate, fp);
4214 if (r < 0)
4215 return log_error_errno(r, "Unable to calculate X509 certificate fingerprint: %m");
4216
4217 r = json_build(&v,
4218 JSON_BUILD_OBJECT(
6b4b40f4 4219 JSON_BUILD_PAIR("rootHash", JSON_BUILD_HEX(hp->roothash.iov_base, hp->roothash.iov_len)),
4ecd39c5
DDM
4220 JSON_BUILD_PAIR(
4221 "certificateFingerprint",
4222 JSON_BUILD_HEX(fp, sizeof(fp))
4223 ),
6b4b40f4 4224 JSON_BUILD_PAIR("signature", JSON_BUILD_IOVEC_BASE64(&sig))
4ecd39c5
DDM
4225 )
4226 );
4227 if (r < 0)
cf18d96f 4228 return log_error_errno(r, "Failed to build verity signature JSON object: %m");
4ecd39c5
DDM
4229
4230 r = json_variant_format(v, 0, &text);
4231 if (r < 0)
cf18d96f 4232 return log_error_errno(r, "Failed to format verity signature JSON object: %m");
4ecd39c5 4233
3510df0a
LP
4234 if (strlen(text)+1 > p->new_size)
4235 return log_error_errno(SYNTHETIC_ERRNO(E2BIG), "Verity signature too long for partition: %m");
4236
d28c6ce6 4237 r = strgrowpad0(&text, p->new_size);
4ecd39c5 4238 if (r < 0)
d28c6ce6 4239 return log_error_errno(r, "Failed to pad string to %s", FORMAT_BYTES(p->new_size));
4ecd39c5 4240
86cbbc6d 4241 if (lseek(whole_fd, p->offset, SEEK_SET) < 0)
cf18d96f 4242 return log_error_errno(errno, "Failed to seek to partition %s offset: %m", strna(hint));
4ecd39c5 4243
e22c60a9 4244 r = loop_write(whole_fd, text, p->new_size);
4ecd39c5 4245 if (r < 0)
cf18d96f 4246 return log_error_errno(r, "Failed to write verity signature to partition %s: %m", strna(hint));
4ecd39c5
DDM
4247
4248 if (fsync(whole_fd) < 0)
cf18d96f 4249 return log_error_errno(errno, "Failed to synchronize partition %s: %m", strna(hint));
4ecd39c5
DDM
4250
4251 return 0;
4252}
4253
757bc2e4 4254static int context_copy_blocks(Context *context) {
a64769d6 4255 int r;
757bc2e4
LP
4256
4257 assert(context);
4258
4259 /* Copy in file systems on the block level */
4260
4261 LIST_FOREACH(partitions, p, context->partitions) {
a64769d6 4262 _cleanup_(partition_target_freep) PartitionTarget *t = NULL;
757bc2e4
LP
4263
4264 if (p->copy_blocks_fd < 0)
4265 continue;
4266
4267 if (p->dropped)
4268 continue;
4269
4270 if (PARTITION_EXISTS(p)) /* Never copy over existing partitions */
4271 continue;
4272
c74d50ff 4273 if (partition_type_defer(&p->type))
81d1098b
DDM
4274 continue;
4275
757bc2e4
LP
4276 assert(p->new_size != UINT64_MAX);
4277 assert(p->copy_blocks_size != UINT64_MAX);
48a09a8f 4278 assert(p->new_size >= p->copy_blocks_size + (p->encrypt != ENCRYPT_OFF ? LUKS2_METADATA_KEEP_FREE : 0));
757bc2e4 4279
b7786df0
LP
4280 usec_t start_timestamp = now(CLOCK_MONOTONIC);
4281
a64769d6
DDM
4282 r = partition_target_prepare(context, p, p->new_size,
4283 /*need_path=*/ p->encrypt != ENCRYPT_OFF || p->siblings[VERITY_HASH],
4284 &t);
4285 if (r < 0)
4286 return r;
757bc2e4 4287
a09ae915
DDM
4288 if (p->encrypt != ENCRYPT_OFF && t->loop) {
4289 r = partition_encrypt(context, p, t, /* offline = */ false);
4290 if (r < 0)
4291 return r;
4292 }
4293
a053f801
LP
4294 if (p->copy_blocks_offset == UINT64_MAX)
4295 log_info("Copying in '%s' (%s) on block level into future partition %" PRIu64 ".",
4296 p->copy_blocks_path, FORMAT_BYTES(p->copy_blocks_size), p->partno);
4297 else {
4298 log_info("Copying in '%s' @ %" PRIu64 " (%s) on block level into future partition %" PRIu64 ".",
4299 p->copy_blocks_path, p->copy_blocks_offset, FORMAT_BYTES(p->copy_blocks_size), p->partno);
4300
4301 if (lseek(p->copy_blocks_fd, p->copy_blocks_offset, SEEK_SET) < 0)
4302 return log_error_errno(errno, "Failed to seek to copy blocks offset in %s: %m", p->copy_blocks_path);
4303 }
1e46985a 4304
a64769d6 4305 r = copy_bytes(p->copy_blocks_fd, partition_target_fd(t), p->copy_blocks_size, COPY_REFLINK);
757bc2e4
LP
4306 if (r < 0)
4307 return log_error_errno(r, "Failed to copy in data from '%s': %m", p->copy_blocks_path);
4308
a09ae915
DDM
4309 log_info("Copying in of '%s' on block level completed.", p->copy_blocks_path);
4310
4311 if (p->encrypt != ENCRYPT_OFF && !t->loop) {
4312 r = partition_encrypt(context, p, t, /* offline = */ true);
b9df3536
LP
4313 if (r < 0)
4314 return r;
b9df3536
LP
4315 }
4316
a64769d6
DDM
4317 r = partition_target_sync(context, p, t);
4318 if (r < 0)
4319 return r;
48a09a8f 4320
b7786df0
LP
4321 usec_t time_spent = usec_sub_unsigned(now(CLOCK_MONOTONIC), start_timestamp);
4322 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 */
4323 log_info("Block level copying and synchronization of partition %" PRIu64 " complete in %s (%s/s).",
4324 p->partno, FORMAT_TIMESPAN(time_spent, 0), FORMAT_BYTES((uint64_t) ((double) p->copy_blocks_size / time_spent * USEC_PER_SEC)));
4325 else
4326 log_info("Block level copying and synchronization of partition %" PRIu64 " complete in %s.",
4327 p->partno, FORMAT_TIMESPAN(time_spent, 0));
4328
c74d50ff 4329 if (p->siblings[VERITY_HASH] && !partition_type_defer(&p->siblings[VERITY_HASH]->type)) {
a64769d6 4330 r = partition_format_verity_hash(context, p->siblings[VERITY_HASH],
13bde177 4331 /* node = */ NULL, partition_target_path(t));
2b392d86
DDM
4332 if (r < 0)
4333 return r;
4334 }
4ecd39c5 4335
c74d50ff 4336 if (p->siblings[VERITY_SIG] && !partition_type_defer(&p->siblings[VERITY_SIG]->type)) {
4ecd39c5
DDM
4337 r = partition_format_verity_sig(context, p->siblings[VERITY_SIG]);
4338 if (r < 0)
4339 return r;
4340 }
757bc2e4
LP
4341 }
4342
4343 return 0;
4344}
4345
e57b7020
DDM
4346static int add_exclude_path(const char *path, Hashmap **denylist, DenyType type) {
4347 _cleanup_free_ struct stat *st = NULL;
4348 int r;
4349
4350 assert(path);
4351 assert(denylist);
4352
4353 st = new(struct stat, 1);
4354 if (!st)
4355 return log_oom();
4356
607343a1 4357 r = chase_and_stat(path, arg_copy_source, CHASE_PREFIX_ROOT, NULL, st);
e57b7020
DDM
4358 if (r == -ENOENT)
4359 return 0;
4360 if (r < 0)
607343a1 4361 return log_error_errno(r, "Failed to stat source file '%s/%s': %m", strempty(arg_copy_source), path);
e57b7020
DDM
4362
4363 r = hashmap_ensure_put(denylist, &inode_hash_ops, st, INT_TO_PTR(type));
4364 if (r == -EEXIST)
4365 return 0;
4366 if (r < 0)
4367 return log_oom();
4368 if (r > 0)
4369 TAKE_PTR(st);
4370
4371 return 0;
4372}
4373
4374static int make_copy_files_denylist(
4375 Context *context,
4376 const Partition *p,
4377 const char *source,
4378 const char *target,
4379 Hashmap **ret) {
4380
4381 _cleanup_hashmap_free_ Hashmap *denylist = NULL;
4382 int r;
4383
4384 assert(context);
4385 assert(p);
4386 assert(source);
4387 assert(target);
4388 assert(ret);
4389
4390 /* Always exclude the top level APIVFS and temporary directories since the contents of these
4391 * directories are almost certainly not intended to end up in an image. */
4392
4393 NULSTR_FOREACH(s, APIVFS_TMP_DIRS_NULSTR) {
4394 r = add_exclude_path(s, &denylist, DENY_CONTENTS);
4395 if (r < 0)
4396 return r;
4397 }
4398
4399 /* Add the user configured excludes. */
4400
600bf76c 4401 STRV_FOREACH(e, p->exclude_files_source) {
e57b7020
DDM
4402 r = add_exclude_path(*e, &denylist, endswith(*e, "/") ? DENY_CONTENTS : DENY_INODE);
4403 if (r < 0)
4404 return r;
4405 }
4406
600bf76c
DDM
4407 STRV_FOREACH(e, p->exclude_files_target) {
4408 _cleanup_free_ char *path = NULL;
4409
4410 const char *s = path_startswith(*e, target);
4411 if (!s)
4412 continue;
4413
4414 path = path_join(source, s);
4415 if (!path)
4416 return log_oom();
4417
4418 r = add_exclude_path(path, &denylist, endswith(*e, "/") ? DENY_CONTENTS : DENY_INODE);
4419 if (r < 0)
4420 return r;
4421 }
4422
e57b7020
DDM
4423 /* If we're populating a root partition, we don't want any files to end up under the APIVFS mount
4424 * points. While we already exclude <source>/proc, users could still do something such as
4425 * "CopyFiles=/abc:/". Now, if /abc has a proc subdirectory with files in it, those will end up in
4426 * the top level proc directory in the root partition, which we want to avoid. To deal with these
4427 * cases, whenever we're populating a root partition and the target of CopyFiles= is the root
4428 * directory of the root partition, we exclude all directories under the source that are named after
4429 * APIVFS directories or named after mount points of other partitions that are also going to be part
4430 * of the image. */
4431
4432 if (p->type.designator == PARTITION_ROOT && empty_or_root(target)) {
4433 LIST_FOREACH(partitions, q, context->partitions) {
4434 if (q->type.designator == PARTITION_ROOT)
4435 continue;
4436
4437 const char *sources = gpt_partition_type_mountpoint_nulstr(q->type);
4438 if (!sources)
4439 continue;
4440
4441 NULSTR_FOREACH(s, sources) {
4442 _cleanup_free_ char *path = NULL;
4443
4444 /* Exclude only the children of partition mount points so that the nested
4445 * partition mount point itself still ends up in the upper partition. */
4446
4447 path = path_join(source, s);
4448 if (!path)
4449 return -ENOMEM;
4450
4451 r = add_exclude_path(path, &denylist, DENY_CONTENTS);
4452 if (r < 0)
4453 return r;
4454 }
4455 }
4456
4457 NULSTR_FOREACH(s, APIVFS_TMP_DIRS_NULSTR) {
4458 _cleanup_free_ char *path = NULL;
4459
4460 path = path_join(source, s);
4461 if (!path)
4462 return -ENOMEM;
4463
4464 r = add_exclude_path(path, &denylist, DENY_CONTENTS);
4465 if (r < 0)
4466 return r;
4467 }
4468 }
4469
4470 *ret = TAKE_PTR(denylist);
4471 return 0;
4472}
4473
440f805c
DDM
4474static int add_subvolume_path(const char *path, Set **subvolumes) {
4475 _cleanup_free_ struct stat *st = NULL;
4476 int r;
4477
4478 assert(path);
4479 assert(subvolumes);
4480
4481 st = new(struct stat, 1);
4482 if (!st)
4483 return log_oom();
4484
607343a1 4485 r = chase_and_stat(path, arg_copy_source, CHASE_PREFIX_ROOT, NULL, st);
440f805c
DDM
4486 if (r == -ENOENT)
4487 return 0;
4488 if (r < 0)
607343a1 4489 return log_error_errno(r, "Failed to stat source file '%s/%s': %m", strempty(arg_copy_source), path);
440f805c 4490
fc159b2f 4491 r = set_ensure_consume(subvolumes, &inode_hash_ops, TAKE_PTR(st));
440f805c
DDM
4492 if (r < 0)
4493 return log_oom();
440f805c
DDM
4494
4495 return 0;
4496}
4497
4498static int make_subvolumes_set(
4499 Context *context,
4500 const Partition *p,
4501 const char *source,
4502 const char *target,
4503 Set **ret) {
4504 _cleanup_set_free_ Set *subvolumes = NULL;
4505 int r;
4506
4507 assert(context);
4508 assert(p);
4509 assert(target);
4510 assert(ret);
4511
4512 STRV_FOREACH(subvolume, p->subvolumes) {
4513 _cleanup_free_ char *path = NULL;
4514
4515 const char *s = path_startswith(*subvolume, target);
4516 if (!s)
4517 continue;
4518
4519 path = path_join(source, s);
4520 if (!path)
4521 return log_oom();
4522
4523 r = add_subvolume_path(path, &subvolumes);
4524 if (r < 0)
4525 return r;
4526 }
4527
4528 *ret = TAKE_PTR(subvolumes);
4529 return 0;
4530}
4531
e57b7020 4532static int do_copy_files(Context *context, Partition *p, const char *root) {
8a794850
LP
4533 int r;
4534
4535 assert(p);
92cd7e7c 4536 assert(root);
8a794850 4537
3e451460
DDM
4538 /* copy_tree_at() automatically copies the permissions of source directories to target directories if
4539 * it created them. However, the root directory is created by us, so we have to manually take care
4540 * that it is initialized. We use the first source directory targeting "/" as the metadata source for
4541 * the root directory. */
4542 STRV_FOREACH_PAIR(source, target, p->copy_files) {
4543 _cleanup_close_ int rfd = -EBADF, sfd = -EBADF;
4544
4545 if (!path_equal(*target, "/"))
4546 continue;
4547
4548 rfd = open(root, O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
4549 if (rfd < 0)
9f08d7f7 4550 return -errno;
3e451460 4551
607343a1 4552 sfd = chase_and_open(*source, arg_copy_source, CHASE_PREFIX_ROOT, O_PATH|O_DIRECTORY|O_CLOEXEC|O_NOCTTY, NULL);
3e451460 4553 if (sfd < 0)
607343a1 4554 return log_error_errno(sfd, "Failed to open source file '%s%s': %m", strempty(arg_copy_source), *source);
3e451460
DDM
4555
4556 (void) copy_xattr(sfd, NULL, rfd, NULL, COPY_ALL_XATTRS);
4557 (void) copy_access(sfd, rfd);
4558 (void) copy_times(sfd, rfd, 0);
4559
4560 break;
4561 }
4562
8a794850 4563 STRV_FOREACH_PAIR(source, target, p->copy_files) {
e57b7020 4564 _cleanup_hashmap_free_ Hashmap *denylist = NULL;
440f805c 4565 _cleanup_set_free_ Set *subvolumes_by_source_inode = NULL;
254d1313 4566 _cleanup_close_ int sfd = -EBADF, pfd = -EBADF, tfd = -EBADF;
8a794850 4567
e57b7020
DDM
4568 r = make_copy_files_denylist(context, p, *source, *target, &denylist);
4569 if (r < 0)
4570 return r;
4571
440f805c
DDM
4572 r = make_subvolumes_set(context, p, *source, *target, &subvolumes_by_source_inode);
4573 if (r < 0)
4574 return r;
4575
607343a1 4576 sfd = chase_and_open(*source, arg_copy_source, CHASE_PREFIX_ROOT, O_CLOEXEC|O_NOCTTY, NULL);
489b0f51
LP
4577 if (sfd == -ENOENT) {
4578 log_notice_errno(sfd, "Failed to open source file '%s%s', skipping: %m", strempty(arg_copy_source), *source);
4579 continue;
4580 }
8a794850 4581 if (sfd < 0)
607343a1 4582 return log_error_errno(sfd, "Failed to open source file '%s%s': %m", strempty(arg_copy_source), *source);
8a794850
LP
4583
4584 r = fd_verify_regular(sfd);
4585 if (r < 0) {
4586 if (r != -EISDIR)
4587 return log_error_errno(r, "Failed to check type of source file '%s': %m", *source);
4588
4589 /* We are looking at a directory */
f461a28d 4590 tfd = chase_and_open(*target, root, CHASE_PREFIX_ROOT, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
8a794850 4591 if (tfd < 0) {
f21a3a82
LP
4592 _cleanup_free_ char *dn = NULL, *fn = NULL;
4593
8a794850
LP
4594 if (tfd != -ENOENT)
4595 return log_error_errno(tfd, "Failed to open target directory '%s': %m", *target);
4596
f21a3a82
LP
4597 r = path_extract_filename(*target, &fn);
4598 if (r < 0)
4599 return log_error_errno(r, "Failed to extract filename from '%s': %m", *target);
4600
4601 r = path_extract_directory(*target, &dn);
4602 if (r < 0)
4603 return log_error_errno(r, "Failed to extract directory from '%s': %m", *target);
4604
440f805c 4605 r = mkdir_p_root(root, dn, UID_INVALID, GID_INVALID, 0755, p->subvolumes);
8a794850
LP
4606 if (r < 0)
4607 return log_error_errno(r, "Failed to create parent directory '%s': %m", dn);
4608
f461a28d 4609 pfd = chase_and_open(dn, root, CHASE_PREFIX_ROOT, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
8a794850
LP
4610 if (pfd < 0)
4611 return log_error_errno(pfd, "Failed to open parent directory of target: %m");
4612
652d9040
LP
4613 r = copy_tree_at(
4614 sfd, ".",
6020d00d 4615 pfd, fn,
ff1b55ff 4616 UID_INVALID, GID_INVALID,
ab645e0a 4617 COPY_REFLINK|COPY_HOLES|COPY_MERGE|COPY_REPLACE|COPY_SIGINT|COPY_HARDLINKS|COPY_ALL_XATTRS|COPY_GRACEFUL_WARN|COPY_TRUNCATE,
440f805c 4618 denylist, subvolumes_by_source_inode);
8a794850 4619 } else
652d9040
LP
4620 r = copy_tree_at(
4621 sfd, ".",
4622 tfd, ".",
ff1b55ff 4623 UID_INVALID, GID_INVALID,
ab645e0a 4624 COPY_REFLINK|COPY_HOLES|COPY_MERGE|COPY_REPLACE|COPY_SIGINT|COPY_HARDLINKS|COPY_ALL_XATTRS|COPY_GRACEFUL_WARN|COPY_TRUNCATE,
440f805c 4625 denylist, subvolumes_by_source_inode);
8a794850 4626 if (r < 0)
cf2ed23c 4627 return log_error_errno(r, "Failed to copy '%s%s' to '%s%s': %m",
607343a1 4628 strempty(arg_copy_source), *source, strempty(root), *target);
8a794850 4629 } else {
f21a3a82
LP
4630 _cleanup_free_ char *dn = NULL, *fn = NULL;
4631
8a794850
LP
4632 /* We are looking at a regular file */
4633
f21a3a82
LP
4634 r = path_extract_filename(*target, &fn);
4635 if (r == -EADDRNOTAVAIL || r == O_DIRECTORY)
4636 return log_error_errno(SYNTHETIC_ERRNO(EISDIR),
4637 "Target path '%s' refers to a directory, but source path '%s' refers to regular file, can't copy.", *target, *source);
4638 if (r < 0)
4639 return log_error_errno(r, "Failed to extract filename from '%s': %m", *target);
4640
4641 r = path_extract_directory(*target, &dn);
4642 if (r < 0)
4643 return log_error_errno(r, "Failed to extract directory from '%s': %m", *target);
4644
440f805c 4645 r = mkdir_p_root(root, dn, UID_INVALID, GID_INVALID, 0755, p->subvolumes);
8a794850
LP
4646 if (r < 0)
4647 return log_error_errno(r, "Failed to create parent directory: %m");
4648
f461a28d 4649 pfd = chase_and_open(dn, root, CHASE_PREFIX_ROOT, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
8a794850 4650 if (pfd < 0)
a0ff9971 4651 return log_error_errno(pfd, "Failed to open parent directory of target: %m");
8a794850 4652
e2819067 4653 tfd = openat(pfd, fn, O_CREAT|O_EXCL|O_WRONLY|O_CLOEXEC, 0700);
8a794850
LP
4654 if (tfd < 0)
4655 return log_error_errno(errno, "Failed to create target file '%s': %m", *target);
4656
ab645e0a 4657 r = copy_bytes(sfd, tfd, UINT64_MAX, COPY_REFLINK|COPY_HOLES|COPY_SIGINT|COPY_TRUNCATE);
8a794850 4658 if (r < 0)
607343a1 4659 return log_error_errno(r, "Failed to copy '%s' to '%s%s': %m", *source, strempty(arg_copy_source), *target);
8a794850 4660
c17cfe6e 4661 (void) copy_xattr(sfd, NULL, tfd, NULL, COPY_ALL_XATTRS);
8a794850
LP
4662 (void) copy_access(sfd, tfd);
4663 (void) copy_times(sfd, tfd, 0);
4664 }
4665 }
4666
4667 return 0;
4668}
4669
49fb6e97 4670static int do_make_directories(Partition *p, const char *root) {
d83d8048
LP
4671 int r;
4672
4673 assert(p);
92cd7e7c 4674 assert(root);
d83d8048
LP
4675
4676 STRV_FOREACH(d, p->make_directories) {
440f805c 4677 r = mkdir_p_root(root, *d, UID_INVALID, GID_INVALID, 0755, p->subvolumes);
d83d8048
LP
4678 if (r < 0)
4679 return log_error_errno(r, "Failed to create directory '%s' in file system: %m", *d);
4680 }
4681
4682 return 0;
4683}
4684
d3201eb4
DDM
4685static bool partition_needs_populate(Partition *p) {
4686 assert(p);
4687 return !strv_isempty(p->copy_files) || !strv_isempty(p->make_directories);
4688}
4689
e57b7020 4690static int partition_populate_directory(Context *context, Partition *p, char **ret) {
95bfd3cd 4691 _cleanup_(rm_rf_physical_and_freep) char *root = NULL;
1eb86ddd 4692 const char *vt;
95bfd3cd
DDM
4693 int r;
4694
e59678b2 4695 assert(ret);
95bfd3cd 4696
b24bfd6e
DDM
4697 log_info("Populating %s filesystem.", p->format);
4698
1eb86ddd
DDM
4699 r = var_tmp_dir(&vt);
4700 if (r < 0)
4701 return log_error_errno(r, "Could not determine temporary directory: %m");
95bfd3cd 4702
1eb86ddd
DDM
4703 r = tempfn_random_child(vt, "repart", &root);
4704 if (r < 0)
4705 return log_error_errno(r, "Failed to generate temporary directory: %m");
4706
4707 r = mkdir(root, 0755);
4708 if (r < 0)
4709 return log_error_errno(errno, "Failed to create temporary directory: %m");
0b34f351 4710
e57b7020 4711 r = do_copy_files(context, p, root);
95bfd3cd
DDM
4712 if (r < 0)
4713 return r;
4714
49fb6e97 4715 r = do_make_directories(p, root);
95bfd3cd
DDM
4716 if (r < 0)
4717 return r;
4718
b24bfd6e
DDM
4719 log_info("Successfully populated %s filesystem.", p->format);
4720
e59678b2 4721 *ret = TAKE_PTR(root);
95bfd3cd
DDM
4722 return 0;
4723}
4724
e57b7020 4725static int partition_populate_filesystem(Context *context, Partition *p, const char *node) {
8a794850
LP
4726 int r;
4727
4728 assert(p);
4729 assert(node);
4730
b24bfd6e 4731 log_info("Populating %s filesystem.", p->format);
8a794850
LP
4732
4733 /* We copy in a child process, since we have to mount the fs for that, and we don't want that fs to
4734 * appear in the host namespace. Hence we fork a child that has its own file system namespace and
4735 * detached mount propagation. */
4736
e9ccae31 4737 r = safe_fork("(sd-copy)", FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, NULL);
8a794850
LP
4738 if (r < 0)
4739 return r;
4740 if (r == 0) {
4741 static const char fs[] = "/run/systemd/mount-root";
4742 /* This is a child process with its own mount namespace and propagation to host turned off */
4743
4744 r = mkdir_p(fs, 0700);
4745 if (r < 0) {
4746 log_error_errno(r, "Failed to create mount point: %m");
4747 _exit(EXIT_FAILURE);
4748 }
4749
511a8cfe 4750 if (mount_nofollow_verbose(LOG_ERR, node, fs, p->format, MS_NOATIME|MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL) < 0)
8a794850
LP
4751 _exit(EXIT_FAILURE);
4752
e57b7020 4753 if (do_copy_files(context, p, fs) < 0)
8a794850
LP
4754 _exit(EXIT_FAILURE);
4755
49fb6e97 4756 if (do_make_directories(p, fs) < 0)
d83d8048
LP
4757 _exit(EXIT_FAILURE);
4758
8a794850
LP
4759 r = syncfs_path(AT_FDCWD, fs);
4760 if (r < 0) {
4761 log_error_errno(r, "Failed to synchronize written files: %m");
4762 _exit(EXIT_FAILURE);
4763 }
4764
4765 _exit(EXIT_SUCCESS);
4766 }
4767
b24bfd6e 4768 log_info("Successfully populated %s filesystem.", p->format);
8a794850
LP
4769 return 0;
4770}
4771
53171c04 4772static int context_mkfs(Context *context) {
a64769d6 4773 int r;
53171c04
LP
4774
4775 assert(context);
4776
4777 /* Make a file system */
4778
4779 LIST_FOREACH(partitions, p, context->partitions) {
e59678b2 4780 _cleanup_(rm_rf_physical_and_freep) char *root = NULL;
a64769d6 4781 _cleanup_(partition_target_freep) PartitionTarget *t = NULL;
4b8ce14f 4782 _cleanup_strv_free_ char **extra_mkfs_options = NULL;
53171c04
LP
4783
4784 if (p->dropped)
4785 continue;
4786
4787 if (PARTITION_EXISTS(p)) /* Never format existing partitions */
4788 continue;
4789
4790 if (!p->format)
4791 continue;
4792
c4a87b76
DDM
4793 /* Minimized partitions will use the copy blocks logic so let's make sure to skip those here. */
4794 if (p->copy_blocks_fd >= 0)
4795 continue;
4796
c74d50ff 4797 if (partition_type_defer(&p->type))
81d1098b
DDM
4798 continue;
4799
53171c04
LP
4800 assert(p->offset != UINT64_MAX);
4801 assert(p->new_size != UINT64_MAX);
48a09a8f 4802 assert(p->new_size >= (p->encrypt != ENCRYPT_OFF ? LUKS2_METADATA_KEEP_FREE : 0));
53171c04 4803
a64769d6
DDM
4804 /* If we're doing encryption, we make sure we keep free space at the end which is required
4805 * for cryptsetup's offline encryption. */
4806 r = partition_target_prepare(context, p,
4807 p->new_size - (p->encrypt != ENCRYPT_OFF ? LUKS2_METADATA_KEEP_FREE : 0),
4808 /*need_path=*/ true,
4809 &t);
53171c04 4810 if (r < 0)
a64769d6 4811 return r;
53171c04 4812
a09ae915
DDM
4813 if (p->encrypt != ENCRYPT_OFF && t->loop) {
4814 r = partition_target_grow(t, p->new_size);
4815 if (r < 0)
4816 return r;
4817
4818 r = partition_encrypt(context, p, t, /* offline = */ false);
4819 if (r < 0)
4820 return log_error_errno(r, "Failed to encrypt device: %m");
4821 }
4822
53171c04
LP
4823 log_info("Formatting future partition %" PRIu64 ".", p->partno);
4824
d3201eb4
DDM
4825 /* If we're not writing to a loop device or if we're populating a read-only filesystem, we
4826 * have to populate using the filesystem's mkfs's --root (or equivalent) option. To do that,
4827 * we need to set up the final directory tree beforehand. */
4828
4829 if (partition_needs_populate(p) && (!t->loop || fstype_is_ro(p->format))) {
4830 if (!mkfs_supports_root_option(p->format))
4831 return log_error_errno(SYNTHETIC_ERRNO(ENODEV),
4832 "Loop device access is required to populate %s filesystems.",
4833 p->format);
95bfd3cd 4834
e57b7020 4835 r = partition_populate_directory(context, p, &root);
143c3c08
DDM
4836 if (r < 0)
4837 return r;
4838 }
95bfd3cd 4839
4b8ce14f
DDM
4840 r = mkfs_options_from_env("REPART", p->format, &extra_mkfs_options);
4841 if (r < 0)
4842 return log_error_errno(r,
4843 "Failed to determine mkfs command line options for '%s': %m",
4844 p->format);
4845
a64769d6 4846 r = make_filesystem(partition_target_path(t), p->format, strempty(p->new_label), root,
93f125a6
DDM
4847 p->fs_uuid, arg_discard, /* quiet = */ false,
4848 context->fs_sector_size, extra_mkfs_options);
48a09a8f 4849 if (r < 0)
53171c04
LP
4850 return r;
4851
0122c7d0
DDM
4852 /* The mkfs binary we invoked might have removed our temporary file when we're not operating
4853 * on a loop device, so let's make sure we open the file again to make sure our file
4854 * descriptor points to any potential new file. */
4855
4856 if (t->fd >= 0 && t->path && !t->loop) {
4857 safe_close(t->fd);
4858 t->fd = open(t->path, O_RDWR|O_CLOEXEC);
4859 if (t->fd < 0)
4860 return log_error_errno(errno, "Failed to reopen temporary file: %m");
4861 }
4862
53171c04
LP
4863 log_info("Successfully formatted future partition %" PRIu64 ".", p->partno);
4864
d3201eb4
DDM
4865 /* If we're writing to a loop device, we can now mount the empty filesystem and populate it. */
4866 if (partition_needs_populate(p) && !root) {
4867 assert(t->loop);
4868
a09ae915 4869 r = partition_populate_filesystem(context, p, partition_target_path(t));
48a09a8f 4870 if (r < 0)
143c3c08 4871 return r;
b9df3536
LP
4872 }
4873
a09ae915 4874 if (p->encrypt != ENCRYPT_OFF && !t->loop) {
a64769d6 4875 r = partition_target_grow(t, p->new_size);
b9df3536 4876 if (r < 0)
a64769d6 4877 return r;
b9df3536 4878
a09ae915 4879 r = partition_encrypt(context, p, t, /* offline = */ true);
48a09a8f
DDM
4880 if (r < 0)
4881 return log_error_errno(r, "Failed to encrypt device: %m");
b9df3536 4882 }
8a794850 4883
48a09a8f
DDM
4884 /* Note that we always sync explicitly here, since mkfs.fat doesn't do that on its own, and
4885 * if we don't sync before detaching a block device the in-flight sectors possibly won't hit
4886 * the disk. */
4887
a64769d6 4888 r = partition_target_sync(context, p, t);
53171c04 4889 if (r < 0)
a64769d6 4890 return r;
b5b7879a 4891
c74d50ff 4892 if (p->siblings[VERITY_HASH] && !partition_type_defer(&p->siblings[VERITY_HASH]->type)) {
a64769d6 4893 r = partition_format_verity_hash(context, p->siblings[VERITY_HASH],
13bde177 4894 /* node = */ NULL, partition_target_path(t));
2b392d86
DDM
4895 if (r < 0)
4896 return r;
b5b7879a 4897 }
4ecd39c5 4898
c74d50ff 4899 if (p->siblings[VERITY_SIG] && !partition_type_defer(&p->siblings[VERITY_SIG]->type)) {
4ecd39c5
DDM
4900 r = partition_format_verity_sig(context, p->siblings[VERITY_SIG]);
4901 if (r < 0)
4902 return r;
4903 }
b5b7879a
DDM
4904 }
4905
4906 return 0;
4907}
4908
b456191d
DDM
4909static int parse_x509_certificate(const char *certificate, size_t certificate_size, X509 **ret) {
4910#if HAVE_OPENSSL
4911 _cleanup_(X509_freep) X509 *cert = NULL;
4912 _cleanup_(BIO_freep) BIO *cb = NULL;
4913
4914 assert(certificate);
4915 assert(certificate_size > 0);
4916 assert(ret);
4917
4918 cb = BIO_new_mem_buf(certificate, certificate_size);
4919 if (!cb)
4920 return log_oom();
4921
4922 cert = PEM_read_bio_X509(cb, NULL, NULL, NULL);
4923 if (!cert)
4924 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Failed to parse X.509 certificate: %s",
4925 ERR_error_string(ERR_get_error(), NULL));
4926
4927 if (ret)
4928 *ret = TAKE_PTR(cert);
4929
4930 return 0;
4931#else
968d232d 4932 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "OpenSSL is not supported, cannot parse X509 certificate.");
b456191d
DDM
4933#endif
4934}
4935
4936static int parse_private_key(const char *key, size_t key_size, EVP_PKEY **ret) {
4937#if HAVE_OPENSSL
4938 _cleanup_(BIO_freep) BIO *kb = NULL;
4939 _cleanup_(EVP_PKEY_freep) EVP_PKEY *pk = NULL;
4940
4941 assert(key);
4942 assert(key_size > 0);
4943 assert(ret);
4944
4945 kb = BIO_new_mem_buf(key, key_size);
4946 if (!kb)
4947 return log_oom();
4948
4949 pk = PEM_read_bio_PrivateKey(kb, NULL, NULL, NULL);
4950 if (!pk)
4951 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to parse PEM private key: %s",
4952 ERR_error_string(ERR_get_error(), NULL));
4953
4954 if (ret)
4955 *ret = TAKE_PTR(pk);
4956
4957 return 0;
4958#else
968d232d 4959 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "OpenSSL is not supported, cannot parse private key.");
b456191d
DDM
4960#endif
4961}
4962
e594a3b1
LP
4963static int partition_acquire_uuid(Context *context, Partition *p, sd_id128_t *ret) {
4964 struct {
4965 sd_id128_t type_uuid;
4966 uint64_t counter;
695cfd53 4967 } _packed_ plaintext = {};
e594a3b1 4968 union {
ade99252 4969 uint8_t md[SHA256_DIGEST_SIZE];
e594a3b1
LP
4970 sd_id128_t id;
4971 } result;
4972
4973 uint64_t k = 0;
e594a3b1
LP
4974 int r;
4975
4976 assert(context);
4977 assert(p);
4978 assert(ret);
4979
4980 /* Calculate a good UUID for the indicated partition. We want a certain degree of reproducibility,
4981 * hence we won't generate the UUIDs randomly. Instead we use a cryptographic hash (precisely:
4982 * HMAC-SHA256) to derive them from a single seed. The seed is generally the machine ID of the
4983 * installation we are processing, but if random behaviour is desired can be random, too. We use the
4984 * seed value as key for the HMAC (since the machine ID is something we generally don't want to leak)
4985 * and the partition type as plaintext. The partition type is suffixed with a counter (only for the
4986 * second and later partition of the same type) if we have more than one partition of the same
4987 * time. Or in other words:
4988 *
4989 * With:
4990 * SEED := /etc/machine-id
4991 *
4992 * If first partition instance of type TYPE_UUID:
4993 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID)
4994 *
4995 * For all later partition instances of type TYPE_UUID with INSTANCE being the LE64 encoded instance number:
4996 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID || INSTANCE)
4997 */
4998
4999 LIST_FOREACH(partitions, q, context->partitions) {
5000 if (p == q)
5001 break;
5002
22e932f4 5003 if (!sd_id128_equal(p->type.uuid, q->type.uuid))
e594a3b1
LP
5004 continue;
5005
5006 k++;
5007 }
5008
22e932f4 5009 plaintext.type_uuid = p->type.uuid;
e594a3b1
LP
5010 plaintext.counter = htole64(k);
5011
ade99252
KK
5012 hmac_sha256(context->seed.bytes, sizeof(context->seed.bytes),
5013 &plaintext,
5014 k == 0 ? sizeof(sd_id128_t) : sizeof(plaintext),
5015 result.md);
e594a3b1
LP
5016
5017 /* Take the first half, mark it as v4 UUID */
5018 assert_cc(sizeof(result.md) == sizeof(result.id) * 2);
5019 result.id = id128_make_v4_uuid(result.id);
5020
5021 /* Ensure this partition UUID is actually unique, and there's no remaining partition from an earlier run? */
5022 LIST_FOREACH(partitions, q, context->partitions) {
5023 if (p == q)
5024 continue;
5025
580f48cc 5026 if (sd_id128_in_set(result.id, q->current_uuid, q->new_uuid)) {
da1af43d 5027 log_warning("Partition UUID calculated from seed for partition %" PRIu64 " already used, reverting to randomized UUID.", p->partno);
e594a3b1
LP
5028
5029 r = sd_id128_randomize(&result.id);
5030 if (r < 0)
5031 return log_error_errno(r, "Failed to generate randomized UUID: %m");
5032
5033 break;
5034 }
5035 }
5036
5037 *ret = result.id;
5038 return 0;
5039}
5040
5041static int partition_acquire_label(Context *context, Partition *p, char **ret) {
5042 _cleanup_free_ char *label = NULL;
5043 const char *prefix;
5044 unsigned k = 1;
5045
5046 assert(context);
5047 assert(p);
5048 assert(ret);
5049
22e932f4 5050 prefix = gpt_partition_type_uuid_to_string(p->type.uuid);
e594a3b1
LP
5051 if (!prefix)
5052 prefix = "linux";
5053
5054 for (;;) {
5055 const char *ll = label ?: prefix;
5056 bool retry = false;
e594a3b1
LP
5057
5058 LIST_FOREACH(partitions, q, context->partitions) {
5059 if (p == q)
5060 break;
5061
5062 if (streq_ptr(ll, q->current_label) ||
5063 streq_ptr(ll, q->new_label)) {
5064 retry = true;
5065 break;
5066 }
5067 }
5068
5069 if (!retry)
5070 break;
5071
5072 label = mfree(label);
e594a3b1
LP
5073 if (asprintf(&label, "%s-%u", prefix, ++k) < 0)
5074 return log_oom();
5075 }
5076
5077 if (!label) {
5078 label = strdup(prefix);
5079 if (!label)
5080 return log_oom();
5081 }
5082
5083 *ret = TAKE_PTR(label);
5084 return 0;
5085}
5086
5087static int context_acquire_partition_uuids_and_labels(Context *context) {
e594a3b1
LP
5088 int r;
5089
5090 assert(context);
5091
5092 LIST_FOREACH(partitions, p, context->partitions) {
15cad3a2
DDM
5093 sd_id128_t uuid;
5094
e594a3b1
LP
5095 /* Never touch foreign partitions */
5096 if (PARTITION_IS_FOREIGN(p)) {
5097 p->new_uuid = p->current_uuid;
5098
5099 if (p->current_label) {
78eee6ce
LP
5100 r = free_and_strdup_warn(&p->new_label, strempty(p->current_label));
5101 if (r < 0)
5102 return r;
e594a3b1
LP
5103 }
5104
5105 continue;
5106 }
5107
5108 if (!sd_id128_is_null(p->current_uuid))
15cad3a2
DDM
5109 p->new_uuid = uuid = p->current_uuid; /* Never change initialized UUIDs */
5110 else if (p->new_uuid_is_set)
5111 uuid = p->new_uuid;
5112 else {
12963533 5113 /* Not explicitly set by user! */
15cad3a2 5114 r = partition_acquire_uuid(context, p, &uuid);
e594a3b1
LP
5115 if (r < 0)
5116 return r;
11749b61 5117
15cad3a2
DDM
5118 /* The final verity hash/data UUIDs can only be determined after formatting the
5119 * verity hash partition. However, we still want to use the generated partition UUID
5120 * to derive other UUIDs to keep things unique and reproducible, so we always
5121 * generate a UUID if none is set, but we only use it as the actual partition UUID if
5122 * verity is not configured. */
5123 if (!IN_SET(p->verity, VERITY_DATA, VERITY_HASH)) {
5124 p->new_uuid = uuid;
5125 p->new_uuid_is_set = true;
5126 }
e594a3b1
LP
5127 }
5128
8bbbdfd7
DDM
5129 /* Calculate the UUID for the file system as HMAC-SHA256 of the string "file-system-uuid",
5130 * keyed off the partition UUID. */
15cad3a2 5131 r = derive_uuid(uuid, "file-system-uuid", &p->fs_uuid);
8bbbdfd7
DDM
5132 if (r < 0)
5133 return r;
5134
15cad3a2
DDM
5135 if (p->encrypt != ENCRYPT_OFF) {
5136 r = derive_uuid(uuid, "luks-uuid", &p->luks_uuid);
5137 if (r < 0)
5138 return r;
5139 }
5140
81e04781
VH
5141 /* Derive the verity salt and verity superblock UUID from the seed to keep them reproducible */
5142 if (p->verity == VERITY_HASH) {
5143 derive_salt(context->seed, "verity-salt", p->verity_salt);
5144
5145 r = derive_uuid(context->seed, "verity-uuid", &p->verity_uuid);
5146 if (r < 0)
5147 return log_error_errno(r, "Failed to acquire verity uuid: %m");
5148 }
5149
e594a3b1 5150 if (!isempty(p->current_label)) {
78eee6ce
LP
5151 /* never change initialized labels */
5152 r = free_and_strdup_warn(&p->new_label, p->current_label);
5153 if (r < 0)
5154 return r;
12963533
TH
5155 } else if (!p->new_label) {
5156 /* Not explicitly set by user! */
5157
e594a3b1
LP
5158 r = partition_acquire_label(context, p, &p->new_label);
5159 if (r < 0)
5160 return r;
5161 }
5162 }
5163
5164 return 0;
5165}
5166
e73309c5
LP
5167static int set_gpt_flags(struct fdisk_partition *q, uint64_t flags) {
5168 _cleanup_free_ char *a = NULL;
5169
5170 for (unsigned i = 0; i < sizeof(flags) * 8; i++) {
5171 uint64_t bit = UINT64_C(1) << i;
5172 char buf[DECIMAL_STR_MAX(unsigned)+1];
5173
5174 if (!FLAGS_SET(flags, bit))
5175 continue;
5176
5177 xsprintf(buf, "%u", i);
5178 if (!strextend_with_separator(&a, ",", buf))
5179 return -ENOMEM;
5180 }
5181
5182 return fdisk_partition_set_attrs(q, a);
5183}
5184
1c41c1dc
LP
5185static uint64_t partition_merge_flags(Partition *p) {
5186 uint64_t f;
5187
5188 assert(p);
5189
5190 f = p->gpt_flags;
5191
ff0771bf 5192 if (p->no_auto >= 0) {
22e932f4 5193 if (gpt_partition_type_knows_no_auto(p->type))
92e72028 5194 SET_FLAG(f, SD_GPT_FLAG_NO_AUTO, p->no_auto);
ff0771bf 5195 else {
b7416360 5196 char buffer[SD_ID128_UUID_STRING_MAX];
ff0771bf
LP
5197 log_warning("Configured NoAuto=%s for partition type '%s' that doesn't support it, ignoring.",
5198 yes_no(p->no_auto),
22e932f4 5199 gpt_partition_type_uuid_to_string_harder(p->type.uuid, buffer));
ff0771bf
LP
5200 }
5201 }
5202
1c41c1dc 5203 if (p->read_only >= 0) {
22e932f4 5204 if (gpt_partition_type_knows_read_only(p->type))
92e72028 5205 SET_FLAG(f, SD_GPT_FLAG_READ_ONLY, p->read_only);
1c41c1dc 5206 else {
b7416360 5207 char buffer[SD_ID128_UUID_STRING_MAX];
1c41c1dc
LP
5208 log_warning("Configured ReadOnly=%s for partition type '%s' that doesn't support it, ignoring.",
5209 yes_no(p->read_only),
22e932f4 5210 gpt_partition_type_uuid_to_string_harder(p->type.uuid, buffer));
1c41c1dc
LP
5211 }
5212 }
5213
5214 if (p->growfs >= 0) {
22e932f4 5215 if (gpt_partition_type_knows_growfs(p->type))
92e72028 5216 SET_FLAG(f, SD_GPT_FLAG_GROWFS, p->growfs);
1c41c1dc 5217 else {
b7416360 5218 char buffer[SD_ID128_UUID_STRING_MAX];
1c41c1dc
LP
5219 log_warning("Configured GrowFileSystem=%s for partition type '%s' that doesn't support it, ignoring.",
5220 yes_no(p->growfs),
22e932f4 5221 gpt_partition_type_uuid_to_string_harder(p->type.uuid, buffer));
1c41c1dc
LP
5222 }
5223 }
5224
5225 return f;
5226}
5227
f28d4f42 5228static int context_mangle_partitions(Context *context) {
f28d4f42 5229 int r;
e594a3b1
LP
5230
5231 assert(context);
5232
e594a3b1
LP
5233 LIST_FOREACH(partitions, p, context->partitions) {
5234 if (p->dropped)
5235 continue;
5236
c74d50ff 5237 if (partition_type_defer(&p->type))
81d1098b
DDM
5238 continue;
5239
e594a3b1
LP
5240 assert(p->new_size != UINT64_MAX);
5241 assert(p->offset != UINT64_MAX);
5242 assert(p->partno != UINT64_MAX);
5243
5244 if (PARTITION_EXISTS(p)) {
5245 bool changed = false;
5246
5247 assert(p->current_partition);
5248
5249 if (p->new_size != p->current_size) {
5250 assert(p->new_size >= p->current_size);
994b3031 5251 assert(p->new_size % context->sector_size == 0);
e594a3b1
LP
5252
5253 r = fdisk_partition_size_explicit(p->current_partition, true);
5254 if (r < 0)
5255 return log_error_errno(r, "Failed to enable explicit sizing: %m");
5256
994b3031 5257 r = fdisk_partition_set_size(p->current_partition, p->new_size / context->sector_size);
e594a3b1
LP
5258 if (r < 0)
5259 return log_error_errno(r, "Failed to grow partition: %m");
5260
5261 log_info("Growing existing partition %" PRIu64 ".", p->partno);
5262 changed = true;
5263 }
5264
5265 if (!sd_id128_equal(p->new_uuid, p->current_uuid)) {
b7416360 5266 r = fdisk_partition_set_uuid(p->current_partition, SD_ID128_TO_UUID_STRING(p->new_uuid));
e594a3b1
LP
5267 if (r < 0)
5268 return log_error_errno(r, "Failed to set partition UUID: %m");
5269
5270 log_info("Initializing UUID of existing partition %" PRIu64 ".", p->partno);
5271 changed = true;
5272 }
5273
5274 if (!streq_ptr(p->new_label, p->current_label)) {
be9ce018 5275 r = fdisk_partition_set_name(p->current_partition, strempty(p->new_label));
e594a3b1
LP
5276 if (r < 0)
5277 return log_error_errno(r, "Failed to set partition label: %m");
5278
5279 log_info("Setting partition label of existing partition %" PRIu64 ".", p->partno);
5280 changed = true;
5281 }
5282
5283 if (changed) {
5284 assert(!PARTITION_IS_FOREIGN(p)); /* never touch foreign partitions */
5285
5286 r = fdisk_set_partition(context->fdisk_context, p->partno, p->current_partition);
5287 if (r < 0)
5288 return log_error_errno(r, "Failed to update partition: %m");
5289 }
5290 } else {
5291 _cleanup_(fdisk_unref_partitionp) struct fdisk_partition *q = NULL;
5292 _cleanup_(fdisk_unref_parttypep) struct fdisk_parttype *t = NULL;
e594a3b1
LP
5293
5294 assert(!p->new_partition);
994b3031
LP
5295 assert(p->offset % context->sector_size == 0);
5296 assert(p->new_size % context->sector_size == 0);
be9ce018 5297 assert(p->new_label);
e594a3b1
LP
5298
5299 t = fdisk_new_parttype();
5300 if (!t)
5301 return log_oom();
5302
22e932f4 5303 r = fdisk_parttype_set_typestr(t, SD_ID128_TO_UUID_STRING(p->type.uuid));
e594a3b1
LP
5304 if (r < 0)
5305 return log_error_errno(r, "Failed to initialize partition type: %m");
5306
5307 q = fdisk_new_partition();
5308 if (!q)
5309 return log_oom();
5310
5311 r = fdisk_partition_set_type(q, t);
5312 if (r < 0)
5313 return log_error_errno(r, "Failed to set partition type: %m");
5314
5315 r = fdisk_partition_size_explicit(q, true);
5316 if (r < 0)
5317 return log_error_errno(r, "Failed to enable explicit sizing: %m");
5318
994b3031 5319 r = fdisk_partition_set_start(q, p->offset / context->sector_size);
e594a3b1
LP
5320 if (r < 0)
5321 return log_error_errno(r, "Failed to position partition: %m");
5322
994b3031 5323 r = fdisk_partition_set_size(q, p->new_size / context->sector_size);
e594a3b1
LP
5324 if (r < 0)
5325 return log_error_errno(r, "Failed to grow partition: %m");
5326
5327 r = fdisk_partition_set_partno(q, p->partno);
5328 if (r < 0)
5329 return log_error_errno(r, "Failed to set partition number: %m");
5330
b7416360 5331 r = fdisk_partition_set_uuid(q, SD_ID128_TO_UUID_STRING(p->new_uuid));
e594a3b1
LP
5332 if (r < 0)
5333 return log_error_errno(r, "Failed to set partition UUID: %m");
5334
be9ce018 5335 r = fdisk_partition_set_name(q, strempty(p->new_label));
e594a3b1
LP
5336 if (r < 0)
5337 return log_error_errno(r, "Failed to set partition label: %m");
5338
ff0771bf 5339 /* Merge the no auto + read only + growfs setting with the literal flags, and set them for the partition */
1c41c1dc 5340 r = set_gpt_flags(q, partition_merge_flags(p));
e73309c5
LP
5341 if (r < 0)
5342 return log_error_errno(r, "Failed to set GPT partition flags: %m");
5343
5b5109e2 5344 log_info("Adding new partition %" PRIu64 " to partition table.", p->partno);
e594a3b1
LP
5345
5346 r = fdisk_add_partition(context->fdisk_context, q, NULL);
5347 if (r < 0)
5348 return log_error_errno(r, "Failed to add partition: %m");
5349
5350 assert(!p->new_partition);
5351 p->new_partition = TAKE_PTR(q);
5352 }
5353 }
5354
f28d4f42
LP
5355 return 0;
5356}
5357
62108348 5358static int split_name_printf(Partition *p, char **ret) {
4cee8333
DDM
5359 assert(p);
5360
5361 const Specifier table[] = {
22e932f4
DDM
5362 { 't', specifier_string, GPT_PARTITION_TYPE_UUID_TO_STRING_HARDER(p->type.uuid) },
5363 { 'T', specifier_id128, &p->type.uuid },
4cee8333
DDM
5364 { 'U', specifier_id128, &p->new_uuid },
5365 { 'n', specifier_uint64, &p->partno },
5366
5367 COMMON_SYSTEM_SPECIFIERS,
5368 {}
5369 };
5370
62108348 5371 return specifier_printf(p->split_name_format, NAME_MAX, table, arg_root, p, ret);
4cee8333
DDM
5372}
5373
62108348
DDM
5374static int split_node(const char *node, char **ret_base, char **ret_ext) {
5375 _cleanup_free_ char *base = NULL, *ext = NULL;
5376 char *e;
5377 int r;
5378
5379 assert(node);
5380 assert(ret_base);
5381 assert(ret_ext);
5382
5383 r = path_extract_filename(node, &base);
5384 if (r == O_DIRECTORY || r == -EADDRNOTAVAIL)
5385 return log_error_errno(r, "Device node %s cannot be a directory", node);
5386 if (r < 0)
5387 return log_error_errno(r, "Failed to extract filename from %s: %m", node);
5388
5389 e = endswith(base, ".raw");
5390 if (e) {
5391 ext = strdup(e);
5392 if (!ext)
5393 return log_oom();
5394
5395 *e = 0;
5396 }
5397
5398 *ret_base = TAKE_PTR(base);
5399 *ret_ext = TAKE_PTR(ext);
5400
5401 return 0;
4cee8333
DDM
5402}
5403
5404static int split_name_resolve(Context *context) {
62108348 5405 _cleanup_free_ char *parent = NULL, *base = NULL, *ext = NULL;
4cee8333
DDM
5406 int r;
5407
62108348 5408 assert(context);
62108348 5409
cc751c75 5410 r = path_extract_directory(context->node, &parent);
62108348 5411 if (r < 0 && r != -EDESTADDRREQ)
cc751c75 5412 return log_error_errno(r, "Failed to extract directory from %s: %m", context->node);
62108348 5413
cc751c75 5414 r = split_node(context->node, &base, &ext);
62108348
DDM
5415 if (r < 0)
5416 return r;
5417
4cee8333 5418 LIST_FOREACH(partitions, p, context->partitions) {
62108348
DDM
5419 _cleanup_free_ char *resolved = NULL;
5420
4cee8333
DDM
5421 if (p->dropped)
5422 continue;
5423
5424 if (!p->split_name_format)
5425 continue;
5426
62108348 5427 r = split_name_printf(p, &resolved);
4cee8333
DDM
5428 if (r < 0)
5429 return log_error_errno(r, "Failed to resolve specifiers in %s: %m", p->split_name_format);
62108348
DDM
5430
5431 if (parent)
5432 p->split_path = strjoin(parent, "/", base, ".", resolved, ext);
5433 else
5434 p->split_path = strjoin(base, ".", resolved, ext);
5435 if (!p->split_path)
5436 return log_oom();
4cee8333
DDM
5437 }
5438
5439 LIST_FOREACH(partitions, p, context->partitions) {
62108348 5440 if (!p->split_path)
4cee8333
DDM
5441 continue;
5442
5443 LIST_FOREACH(partitions, q, context->partitions) {
5444 if (p == q)
5445 continue;
5446
62108348 5447 if (!q->split_path)
4cee8333
DDM
5448 continue;
5449
62108348 5450 if (!streq(p->split_path, q->split_path))
4cee8333
DDM
5451 continue;
5452
5453 return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ),
5454 "%s and %s have the same resolved split name \"%s\", refusing",
62108348 5455 p->definition_path, q->definition_path, p->split_path);
4cee8333
DDM
5456 }
5457 }
5458
5459 return 0;
5460}
5461
4cee8333 5462static int context_split(Context *context) {
254d1313 5463 int fd = -EBADF, r;
4cee8333
DDM
5464
5465 if (!arg_split)
5466 return 0;
5467
5468 assert(context);
4cee8333
DDM
5469
5470 /* We can't do resolution earlier because the partition UUIDs for verity partitions are only filled
5471 * in after they've been generated. */
5472
5473 r = split_name_resolve(context);
5474 if (r < 0)
5475 return r;
5476
4cee8333 5477 LIST_FOREACH(partitions, p, context->partitions) {
254d1313 5478 _cleanup_close_ int fdt = -EBADF;
4cee8333
DDM
5479
5480 if (p->dropped)
5481 continue;
5482
62108348 5483 if (!p->split_path)
4cee8333
DDM
5484 continue;
5485
c74d50ff 5486 if (partition_type_defer(&p->type))
81d1098b
DDM
5487 continue;
5488
62108348 5489 fdt = open(p->split_path, O_WRONLY|O_NOCTTY|O_CLOEXEC|O_NOFOLLOW|O_CREAT|O_EXCL, 0666);
4cee8333 5490 if (fdt < 0)
62108348 5491 return log_error_errno(fdt, "Failed to open split partition file %s: %m", p->split_path);
4cee8333
DDM
5492
5493 if (fd < 0)
5494 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
5495
5496 if (lseek(fd, p->offset, SEEK_SET) < 0)
5497 return log_error_errno(errno, "Failed to seek to partition offset: %m");
5498
ab645e0a 5499 r = copy_bytes(fd, fdt, p->new_size, COPY_REFLINK|COPY_HOLES|COPY_TRUNCATE);
4cee8333 5500 if (r < 0)
62108348 5501 return log_error_errno(r, "Failed to copy to split partition %s: %m", p->split_path);
4cee8333
DDM
5502 }
5503
5504 return 0;
5505}
5506
cc751c75 5507static int context_write_partition_table(Context *context) {
f28d4f42
LP
5508 _cleanup_(fdisk_unref_tablep) struct fdisk_table *original_table = NULL;
5509 int capable, r;
5510
5511 assert(context);
5512
cc751c75 5513 if (!context->from_scratch && !context_changed(context)) {
f28d4f42
LP
5514 log_info("No changes.");
5515 return 0;
5516 }
5517
5518 if (arg_dry_run) {
5519 log_notice("Refusing to repartition, please re-run with --dry-run=no.");
5520 return 0;
5521 }
5522
acdf6bcf 5523 log_info("Applying changes to %s.", context->node);
f28d4f42 5524
19c58989
LP
5525 if (context->from_scratch && arg_empty != EMPTY_CREATE) {
5526 /* Erase everything if we operate from scratch, except if the image was just created anyway, and thus is definitely empty. */
81873a6b
LP
5527 r = context_wipe_range(context, 0, context->total);
5528 if (r < 0)
5529 return r;
5530
5531 log_info("Wiped block device.");
5532
0dce448b
LB
5533 if (arg_discard) {
5534 r = context_discard_range(context, 0, context->total);
5535 if (r == -EOPNOTSUPP)
5536 log_info("Storage does not support discard, not discarding entire block device data.");
5537 else if (r < 0)
5538 return log_error_errno(r, "Failed to discard entire block device: %m");
5539 else if (r > 0)
5540 log_info("Discarded entire block device.");
5541 }
f28d4f42
LP
5542 }
5543
5544 r = fdisk_get_partitions(context->fdisk_context, &original_table);
5545 if (r < 0)
5546 return log_error_errno(r, "Failed to acquire partition table: %m");
5547
5548 /* Wipe fs signatures and discard sectors where the new partitions are going to be placed and in the
5549 * gaps between partitions, just to be sure. */
cc751c75 5550 r = context_wipe_and_discard(context);
f28d4f42
LP
5551 if (r < 0)
5552 return r;
5553
5554 r = context_copy_blocks(context);
5555 if (r < 0)
5556 return r;
5557
5558 r = context_mkfs(context);
5559 if (r < 0)
5560 return r;
5561
5562 r = context_mangle_partitions(context);
5563 if (r < 0)
5564 return r;
5565
e594a3b1
LP
5566 log_info("Writing new partition table.");
5567
5568 r = fdisk_write_disklabel(context->fdisk_context);
5569 if (r < 0)
5570 return log_error_errno(r, "Failed to write partition table: %m");
5571
911ba624 5572 capable = blockdev_partscan_enabled(fdisk_get_devfd(context->fdisk_context));
9a1deb85
LP
5573 if (capable == -ENOTBLK)
5574 log_debug("Not telling kernel to reread partition table, since we are not operating on a block device.");
5575 else if (capable < 0)
911ba624 5576 return log_error_errno(capable, "Failed to check if block device supports partition scanning: %m");
9a1deb85 5577 else if (capable > 0) {
e594a3b1
LP
5578 log_info("Telling kernel to reread partition table.");
5579
cc751c75 5580 if (context->from_scratch)
e594a3b1
LP
5581 r = fdisk_reread_partition_table(context->fdisk_context);
5582 else
5583 r = fdisk_reread_changes(context->fdisk_context, original_table);
5584 if (r < 0)
5585 return log_error_errno(r, "Failed to reread partition table: %m");
5586 } else
5587 log_notice("Not telling kernel to reread partition table, because selected image does not support kernel partition block devices.");
5588
5589 log_info("All done.");
5590
5591 return 0;
5592}
5593
5594static int context_read_seed(Context *context, const char *root) {
5595 int r;
5596
5597 assert(context);
5598
5599 if (!sd_id128_is_null(context->seed))
5600 return 0;
5601
5602 if (!arg_randomize) {
5ee37b70
YW
5603 r = id128_get_machine(root, &context->seed);
5604 if (r >= 0)
5605 return 0;
e594a3b1 5606
5ee37b70
YW
5607 if (!ERRNO_IS_MACHINE_ID_UNSET(r))
5608 return log_error_errno(r, "Failed to parse machine ID of image: %m");
74e795ee 5609
5ee37b70 5610 log_info("No machine ID set, using randomized partition UUIDs.");
e594a3b1
LP
5611 }
5612
5613 r = sd_id128_randomize(&context->seed);
5614 if (r < 0)
5615 return log_error_errno(r, "Failed to generate randomized seed: %m");
5616
5617 return 0;
5618}
5619
cc751c75 5620static int context_factory_reset(Context *context) {
e594a3b1
LP
5621 size_t n = 0;
5622 int r;
5623
5624 assert(context);
5625
5626 if (arg_factory_reset <= 0)
5627 return 0;
5628
cc751c75 5629 if (context->from_scratch) /* Nothing to reset if we start from scratch */
e594a3b1
LP
5630 return 0;
5631
5632 if (arg_dry_run) {
5633 log_notice("Refusing to factory reset, please re-run with --dry-run=no.");
5634 return 0;
5635 }
5636
5637 log_info("Applying factory reset.");
5638
5639 LIST_FOREACH(partitions, p, context->partitions) {
5640
5641 if (!p->factory_reset || !PARTITION_EXISTS(p))
5642 continue;
5643
5644 assert(p->partno != UINT64_MAX);
5645
5646 log_info("Removing partition %" PRIu64 " for factory reset.", p->partno);
5647
5648 r = fdisk_delete_partition(context->fdisk_context, p->partno);
5649 if (r < 0)
5650 return log_error_errno(r, "Failed to remove partition %" PRIu64 ": %m", p->partno);
5651
5652 n++;
5653 }
5654
5655 if (n == 0) {
5656 log_info("Factory reset requested, but no partitions to delete found.");
5657 return 0;
5658 }
5659
5660 r = fdisk_write_disklabel(context->fdisk_context);
5661 if (r < 0)
5662 return log_error_errno(r, "Failed to write disk label: %m");
5663
5664 log_info("Successfully deleted %zu partitions.", n);
5665 return 1;
5666}
5667
5668static int context_can_factory_reset(Context *context) {
e594a3b1
LP
5669 assert(context);
5670
5671 LIST_FOREACH(partitions, p, context->partitions)
5672 if (p->factory_reset && PARTITION_EXISTS(p))
5673 return true;
5674
5675 return false;
5676}
5677
5c08da58
LP
5678static int resolve_copy_blocks_auto_candidate(
5679 dev_t partition_devno,
22e932f4 5680 GptPartitionType partition_type,
5c08da58
LP
5681 dev_t restrict_devno,
5682 sd_id128_t *ret_uuid) {
5683
5684 _cleanup_(blkid_free_probep) blkid_probe b = NULL;
254d1313 5685 _cleanup_close_ int fd = -EBADF;
ca822829
YW
5686 _cleanup_free_ char *p = NULL;
5687 const char *pttype, *t;
5c08da58
LP
5688 sd_id128_t pt_parsed, u;
5689 blkid_partition pp;
5690 dev_t whole_devno;
5691 blkid_partlist pl;
5c08da58
LP
5692 int r;
5693
5694 /* Checks if the specified partition has the specified GPT type UUID, and is located on the specified
5695 * 'restrict_devno' device. The type check is particularly relevant if we have Verity volume which is
5696 * backed by two separate partitions: the data and the hash partitions, and we need to find the right
5697 * one of the two. */
5698
5699 r = block_get_whole_disk(partition_devno, &whole_devno);
5700 if (r < 0)
5701 return log_error_errno(
5702 r,
5703 "Unable to determine containing block device of partition %u:%u: %m",
5704 major(partition_devno), minor(partition_devno));
5705
5706 if (restrict_devno != (dev_t) -1 &&
5707 restrict_devno != whole_devno)
5708 return log_error_errno(
5709 SYNTHETIC_ERRNO(EPERM),
5710 "Partition %u:%u is located outside of block device %u:%u, refusing.",
5711 major(partition_devno), minor(partition_devno),
5712 major(restrict_devno), minor(restrict_devno));
5713
ca822829 5714 fd = r = device_open_from_devnum(S_IFBLK, whole_devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &p);
5c08da58 5715 if (r < 0)
ca822829
YW
5716 return log_error_errno(r, "Failed to open block device " DEVNUM_FORMAT_STR ": %m",
5717 DEVNUM_FORMAT_VAL(whole_devno));
5c08da58
LP
5718
5719 b = blkid_new_probe();
5720 if (!b)
5721 return log_oom();
5722
5723 errno = 0;
5724 r = blkid_probe_set_device(b, fd, 0, 0);
5725 if (r != 0)
5726 return log_error_errno(errno_or_else(ENOMEM), "Failed to open block device '%s': %m", p);
5727
5728 (void) blkid_probe_enable_partitions(b, 1);
5729 (void) blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
5730
5731 errno = 0;
5732 r = blkid_do_safeprobe(b);
2e3944b8
LP
5733 if (r == _BLKID_SAFEPROBE_ERROR)
5734 return log_error_errno(errno_or_else(EIO), "Unable to probe for partition table of '%s': %m", p);
5735 if (IN_SET(r, _BLKID_SAFEPROBE_AMBIGUOUS, _BLKID_SAFEPROBE_NOT_FOUND)) {
5c08da58
LP
5736 log_debug("Didn't find partition table on block device '%s'.", p);
5737 return false;
5738 }
2e3944b8
LP
5739
5740 assert(r == _BLKID_SAFEPROBE_FOUND);
5c08da58
LP
5741
5742 (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL);
5743 if (!streq_ptr(pttype, "gpt")) {
5744 log_debug("Didn't find a GPT partition table on '%s'.", p);
5745 return false;
5746 }
5747
5748 errno = 0;
5749 pl = blkid_probe_get_partitions(b);
5750 if (!pl)
5751 return log_error_errno(errno_or_else(EIO), "Unable read partition table of '%s': %m", p);
5c08da58
LP
5752
5753 pp = blkid_partlist_devno_to_partition(pl, partition_devno);
5754 if (!pp) {
5755 log_debug("Partition %u:%u has no matching partition table entry on '%s'.",
5756 major(partition_devno), minor(partition_devno), p);
5757 return false;
5758 }
5759
5760 t = blkid_partition_get_type_string(pp);
5761 if (isempty(t)) {
5762 log_debug("Partition %u:%u has no type on '%s'.",
5763 major(partition_devno), minor(partition_devno), p);
5764 return false;
5765 }
5766
5767 r = sd_id128_from_string(t, &pt_parsed);
5768 if (r < 0) {
5769 log_debug_errno(r, "Failed to parse partition type \"%s\": %m", t);
5770 return false;
5771 }
5772
22e932f4 5773 if (!sd_id128_equal(pt_parsed, partition_type.uuid)) {
5c08da58
LP
5774 log_debug("Partition %u:%u has non-matching partition type " SD_ID128_FORMAT_STR " (needed: " SD_ID128_FORMAT_STR "), ignoring.",
5775 major(partition_devno), minor(partition_devno),
22e932f4 5776 SD_ID128_FORMAT_VAL(pt_parsed), SD_ID128_FORMAT_VAL(partition_type.uuid));
5c08da58
LP
5777 return false;
5778 }
5779
e3b9a5ff
LP
5780 r = blkid_partition_get_uuid_id128(pp, &u);
5781 if (r == -ENXIO) {
5782 log_debug_errno(r, "Partition " DEVNUM_FORMAT_STR " has no UUID.", DEVNUM_FORMAT_VAL(partition_devno));
5c08da58
LP
5783 return false;
5784 }
5c08da58 5785 if (r < 0) {
e3b9a5ff 5786 log_debug_errno(r, "Failed to read partition UUID of " DEVNUM_FORMAT_STR ": %m", DEVNUM_FORMAT_VAL(partition_devno));
5c08da58
LP
5787 return false;
5788 }
5789
e3b9a5ff
LP
5790 log_debug("Automatically found partition " DEVNUM_FORMAT_STR " of right type " SD_ID128_FORMAT_STR ".",
5791 DEVNUM_FORMAT_VAL(partition_devno),
5c08da58
LP
5792 SD_ID128_FORMAT_VAL(pt_parsed));
5793
5794 if (ret_uuid)
5795 *ret_uuid = u;
5796
5797 return true;
5798}
5799
5800static int find_backing_devno(
5801 const char *path,
5802 const char *root,
5803 dev_t *ret) {
5804
5805 _cleanup_free_ char *resolved = NULL;
5806 int r;
5807
5808 assert(path);
5809
f461a28d 5810 r = chase(path, root, CHASE_PREFIX_ROOT, &resolved, NULL);
5c08da58
LP
5811 if (r < 0)
5812 return r;
5813
5814 r = path_is_mount_point(resolved, NULL, 0);
5815 if (r < 0)
5816 return r;
5817 if (r == 0) /* Not a mount point, then it's not a partition of its own, let's not automatically use it. */
5818 return -ENOENT;
5819
5820 r = get_block_device(resolved, ret);
5821 if (r < 0)
5822 return r;
5823 if (r == 0) /* Not backed by physical file system, we can't use this */
5824 return -ENOENT;
5825
5826 return 0;
5827}
5828
5829static int resolve_copy_blocks_auto(
22e932f4 5830 GptPartitionType type,
5c08da58
LP
5831 const char *root,
5832 dev_t restrict_devno,
1a037ba2 5833 dev_t *ret_devno,
5c08da58
LP
5834 sd_id128_t *ret_uuid) {
5835
5836 const char *try1 = NULL, *try2 = NULL;
5837 char p[SYS_BLOCK_PATH_MAX("/slaves")];
5d2a48da 5838 _cleanup_closedir_ DIR *d = NULL;
5c08da58
LP
5839 sd_id128_t found_uuid = SD_ID128_NULL;
5840 dev_t devno, found = 0;
5841 int r;
5842
86320e62
DDM
5843 /* Enforce some security restrictions: CopyBlocks=auto should not be an avenue to get outside of the
5844 * --root=/--image= confinement. Specifically, refuse CopyBlocks= in combination with --root= at all,
5845 * and restrict block device references in the --image= case to loopback block device we set up.
5846 *
5847 * restrict_devno contain the dev_t of the loop back device we operate on in case of --image=, and
5848 * thus declares which device (and its partition subdevices) we shall limit access to. If
5849 * restrict_devno is zero no device probing access shall be allowed at all (used for --root=) and if
5850 * it is (dev_t) -1 then free access shall be allowed (if neither switch is used). */
5851
5852 if (restrict_devno == 0)
5853 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
5854 "Automatic discovery of backing block devices not permitted in --root= mode, refusing.");
5855
5c08da58
LP
5856 /* Handles CopyBlocks=auto, and finds the right source partition to copy from. We look for matching
5857 * partitions in the host, using the appropriate directory as key and ensuring that the partition
5858 * type matches. */
5859
22e932f4 5860 if (type.designator == PARTITION_ROOT)
5c08da58 5861 try1 = "/";
22e932f4 5862 else if (type.designator == PARTITION_USR)
5c08da58 5863 try1 = "/usr/";
22e932f4 5864 else if (type.designator == PARTITION_ROOT_VERITY)
5c08da58 5865 try1 = "/";
22e932f4 5866 else if (type.designator == PARTITION_USR_VERITY)
5c08da58 5867 try1 = "/usr/";
22e932f4 5868 else if (type.designator == PARTITION_ESP) {
5c08da58
LP
5869 try1 = "/efi/";
5870 try2 = "/boot/";
22e932f4 5871 } else if (type.designator == PARTITION_XBOOTLDR)
5c08da58
LP
5872 try1 = "/boot/";
5873 else
5874 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
5875 "Partition type " SD_ID128_FORMAT_STR " not supported from automatic source block device discovery.",
22e932f4 5876 SD_ID128_FORMAT_VAL(type.uuid));
5c08da58
LP
5877
5878 r = find_backing_devno(try1, root, &devno);
5879 if (r == -ENOENT && try2)
5880 r = find_backing_devno(try2, root, &devno);
5881 if (r < 0)
5882 return log_error_errno(r, "Failed to resolve automatic CopyBlocks= path for partition type " SD_ID128_FORMAT_STR ", sorry: %m",
22e932f4 5883 SD_ID128_FORMAT_VAL(type.uuid));
5c08da58
LP
5884
5885 xsprintf_sys_block_path(p, "/slaves", devno);
5886 d = opendir(p);
5887 if (d) {
5888 struct dirent *de;
5889
5890 for (;;) {
5891 _cleanup_free_ char *q = NULL, *t = NULL;
5892 sd_id128_t u;
5893 dev_t sl;
5894
5895 errno = 0;
5896 de = readdir_no_dot(d);
5897 if (!de) {
5898 if (errno != 0)
5899 return log_error_errno(errno, "Failed to read directory '%s': %m", p);
5900
5901 break;
5902 }
5903
5904 if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
5905 continue;
5906
5907 q = path_join(p, de->d_name, "/dev");
5908 if (!q)
5909 return log_oom();
5910
5911 r = read_one_line_file(q, &t);
5912 if (r < 0)
5913 return log_error_errno(r, "Failed to read %s: %m", q);
5914
7176f06c 5915 r = parse_devnum(t, &sl);
5c08da58
LP
5916 if (r < 0) {
5917 log_debug_errno(r, "Failed to parse %s, ignoring: %m", q);
5918 continue;
5919 }
5920 if (major(sl) == 0) {
375ffdba 5921 log_debug("Device backing %s is special, ignoring.", q);
5c08da58
LP
5922 continue;
5923 }
5924
22e932f4 5925 r = resolve_copy_blocks_auto_candidate(sl, type, restrict_devno, &u);
5c08da58
LP
5926 if (r < 0)
5927 return r;
5928 if (r > 0) {
5929 /* We found a matching one! */
5930 if (found != 0)
5931 return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ),
5932 "Multiple matching partitions found, refusing.");
5933
5934 found = sl;
5935 found_uuid = u;
5936 }
5937 }
5938 } else if (errno != ENOENT)
5939 return log_error_errno(errno, "Failed open %s: %m", p);
5940 else {
22e932f4 5941 r = resolve_copy_blocks_auto_candidate(devno, type, restrict_devno, &found_uuid);
5c08da58
LP
5942 if (r < 0)
5943 return r;
5944 if (r > 0)
5945 found = devno;
5946 }
5947
86320e62
DDM
5948 if (found == 0)
5949 return log_error_errno(SYNTHETIC_ERRNO(ENXIO),
5950 "Unable to automatically discover suitable partition to copy blocks from.");
5951
1a037ba2
YW
5952 if (ret_devno)
5953 *ret_devno = found;
5c08da58
LP
5954
5955 if (ret_uuid)
5956 *ret_uuid = found_uuid;
5957
86320e62 5958 return 0;
5c08da58
LP
5959}
5960
5961static int context_open_copy_block_paths(
5962 Context *context,
5c08da58
LP
5963 dev_t restrict_devno) {
5964
757bc2e4
LP
5965 int r;
5966
5967 assert(context);
5968
5969 LIST_FOREACH(partitions, p, context->partitions) {
254d1313 5970 _cleanup_close_ int source_fd = -EBADF;
5c08da58
LP
5971 _cleanup_free_ char *opened = NULL;
5972 sd_id128_t uuid = SD_ID128_NULL;
757bc2e4
LP
5973 uint64_t size;
5974 struct stat st;
5975
5eef7047
DDM
5976 if (p->copy_blocks_fd >= 0)
5977 continue;
5978
757bc2e4
LP
5979 assert(p->copy_blocks_size == UINT64_MAX);
5980
5981 if (PARTITION_EXISTS(p)) /* Never copy over partitions that already exist! */
5982 continue;
5983
5c08da58 5984 if (p->copy_blocks_path) {
757bc2e4 5985
f461a28d 5986 source_fd = chase_and_open(p->copy_blocks_path, p->copy_blocks_root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened);
5c08da58
LP
5987 if (source_fd < 0)
5988 return log_error_errno(source_fd, "Failed to open '%s': %m", p->copy_blocks_path);
757bc2e4 5989
5c08da58
LP
5990 if (fstat(source_fd, &st) < 0)
5991 return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened);
5992
5993 if (!S_ISREG(st.st_mode) && restrict_devno != (dev_t) -1)
5994 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
5995 "Copying from block device node is not permitted in --image=/--root= mode, refusing.");
5996
5997 } else if (p->copy_blocks_auto) {
03f5e501 5998 dev_t devno = 0; /* Fake initialization to appease gcc. */
5c08da58 5999
22e932f4 6000 r = resolve_copy_blocks_auto(p->type, p->copy_blocks_root, restrict_devno, &devno, &uuid);
5c08da58
LP
6001 if (r < 0)
6002 return r;
03f5e501 6003 assert(devno != 0);
5c08da58 6004
ca822829 6005 source_fd = r = device_open_from_devnum(S_IFBLK, devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened);
1a037ba2 6006 if (r < 0)
ca822829
YW
6007 return log_error_errno(r, "Failed to open automatically determined source block copy device " DEVNUM_FORMAT_STR ": %m",
6008 DEVNUM_FORMAT_VAL(devno));
5c08da58
LP
6009
6010 if (fstat(source_fd, &st) < 0)
6011 return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened);
1a037ba2 6012 } else
5c08da58 6013 continue;
757bc2e4
LP
6014
6015 if (S_ISDIR(st.st_mode)) {
ca822829
YW
6016 _cleanup_free_ char *bdev = NULL;
6017 dev_t devt;
757bc2e4
LP
6018
6019 /* If the file is a directory, automatically find the backing block device */
6020
6021 if (major(st.st_dev) != 0)
ca822829 6022 devt = st.st_dev;
757bc2e4 6023 else {
757bc2e4 6024 /* Special support for btrfs */
757bc2e4 6025 r = btrfs_get_block_device_fd(source_fd, &devt);
67f0ac8c 6026 if (r == -EUCLEAN)
5c08da58 6027 return btrfs_log_dev_root(LOG_ERR, r, opened);
757bc2e4 6028 if (r < 0)
5c08da58 6029 return log_error_errno(r, "Unable to determine backing block device of '%s': %m", opened);
757bc2e4 6030 }
757bc2e4
LP
6031
6032 safe_close(source_fd);
6033
ca822829
YW
6034 source_fd = r = device_open_from_devnum(S_IFBLK, devt, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &bdev);
6035 if (r < 0)
6036 return log_error_errno(r, "Failed to open block device backing '%s': %m", opened);
757bc2e4
LP
6037
6038 if (fstat(source_fd, &st) < 0)
6039 return log_error_errno(errno, "Failed to stat block device '%s': %m", bdev);
757bc2e4
LP
6040 }
6041
6042 if (S_ISREG(st.st_mode))
6043 size = st.st_size;
6044 else if (S_ISBLK(st.st_mode)) {
01db9c85
LP
6045 r = blockdev_get_device_size(source_fd, &size);
6046 if (r < 0)
6047 return log_error_errno(r, "Failed to determine size of block device to copy from: %m");
757bc2e4 6048 } else
5c08da58 6049 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);
757bc2e4
LP
6050
6051 if (size <= 0)
5c08da58 6052 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File to copy bytes from '%s' has zero size, refusing.", opened);
757bc2e4 6053 if (size % 512 != 0)
5c08da58 6054 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File to copy bytes from '%s' has size that is not multiple of 512, refusing.", opened);
757bc2e4
LP
6055
6056 p->copy_blocks_fd = TAKE_FD(source_fd);
6057 p->copy_blocks_size = size;
5c08da58
LP
6058
6059 free_and_replace(p->copy_blocks_path, opened);
6060
6061 /* When copying from an existing partition copy that partitions UUID if none is configured explicitly */
11749b61 6062 if (!p->new_uuid_is_set && !sd_id128_is_null(uuid)) {
5c08da58 6063 p->new_uuid = uuid;
11749b61
DDM
6064 p->new_uuid_is_set = true;
6065 }
757bc2e4
LP
6066 }
6067
6068 return 0;
6069}
6070
c4a87b76
DDM
6071static int fd_apparent_size(int fd, uint64_t *ret) {
6072 off_t initial = 0;
6073 uint64_t size = 0;
6074
6075 assert(fd >= 0);
6076 assert(ret);
6077
6078 initial = lseek(fd, 0, SEEK_CUR);
6079 if (initial < 0)
6080 return log_error_errno(errno, "Failed to get file offset: %m");
6081
6082 for (off_t off = 0;;) {
6083 off_t r;
6084
6085 r = lseek(fd, off, SEEK_DATA);
6086 if (r < 0 && errno == ENXIO)
6087 /* If errno == ENXIO, that means we've reached the final hole of the file and
6088 * that hole isn't followed by more data. */
6089 break;
6090 if (r < 0)
6091 return log_error_errno(errno, "Failed to seek data in file from offset %"PRIi64": %m", off);
6092
6093 off = r; /* Set the offset to the start of the data segment. */
6094
6095 /* After copying a potential hole, find the end of the data segment by looking for
6096 * the next hole. If we get ENXIO, we're at EOF. */
6097 r = lseek(fd, off, SEEK_HOLE);
6098 if (r < 0) {
6099 if (errno == ENXIO)
6100 break;
6101 return log_error_errno(errno, "Failed to seek hole in file from offset %"PRIi64": %m", off);
6102 }
6103
6104 size += r - off;
6105 off = r;
6106 }
6107
6108 if (lseek(fd, initial, SEEK_SET) < 0)
6109 return log_error_errno(errno, "Failed to reset file offset: %m");
6110
6111 *ret = size;
6112
6113 return 0;
6114}
6115
6116static int context_minimize(Context *context) {
c264ec5f 6117 const char *vt = NULL;
c4a87b76
DDM
6118 int r;
6119
6120 assert(context);
6121
c4a87b76 6122 LIST_FOREACH(partitions, p, context->partitions) {
e59678b2 6123 _cleanup_(rm_rf_physical_and_freep) char *root = NULL;
c4a87b76 6124 _cleanup_(unlink_and_freep) char *temp = NULL;
d3201eb4 6125 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
4b8ce14f 6126 _cleanup_strv_free_ char **extra_mkfs_options = NULL;
254d1313 6127 _cleanup_close_ int fd = -EBADF;
b24bfd6e 6128 _cleanup_free_ char *hint = NULL;
c4a87b76 6129 sd_id128_t fs_uuid;
6ac299e3 6130 struct stat st;
c4a87b76
DDM
6131 uint64_t fsz;
6132
6133 if (p->dropped)
6134 continue;
6135
6136 if (PARTITION_EXISTS(p)) /* Never format existing partitions */
6137 continue;
6138
6139 if (!p->format)
6140 continue;
6141
dea0dc7b
DDM
6142 if (p->copy_blocks_fd >= 0)
6143 continue;
6144
5c33b686 6145 if (p->minimize == MINIMIZE_OFF)
c4a87b76
DDM
6146 continue;
6147
d3201eb4
DDM
6148 if (!partition_needs_populate(p))
6149 continue;
6150
c4a87b76
DDM
6151 assert(!p->copy_blocks_path);
6152
b24bfd6e
DDM
6153 (void) partition_hint(p, context->node, &hint);
6154
6155 log_info("Pre-populating %s filesystem of partition %s twice to calculate minimal partition size",
6156 p->format, strna(hint));
6157
c264ec5f
ZJS
6158 if (!vt) {
6159 r = var_tmp_dir(&vt);
6160 if (r < 0)
6161 return log_error_errno(r, "Could not determine temporary directory: %m");
6162 }
6163
c4a87b76
DDM
6164 r = tempfn_random_child(vt, "repart", &temp);
6165 if (r < 0)
6166 return log_error_errno(r, "Failed to generate temporary file path: %m");
6167
59e2be46
DDM
6168 if (fstype_is_ro(p->format))
6169 fs_uuid = p->fs_uuid;
6170 else {
c4a87b76
DDM
6171 fd = open(temp, O_CREAT|O_EXCL|O_CLOEXEC|O_RDWR|O_NOCTTY, 0600);
6172 if (fd < 0)
6173 return log_error_errno(errno, "Failed to open temporary file %s: %m", temp);
6174
6175 /* This may seem huge but it will be created sparse so it doesn't take up any space
d3201eb4 6176 * on disk until written to. */
c4a87b76
DDM
6177 if (ftruncate(fd, 1024ULL * 1024ULL * 1024ULL * 1024ULL) < 0)
6178 return log_error_errno(errno, "Failed to truncate temporary file to %s: %m",
6179 FORMAT_BYTES(1024ULL * 1024ULL * 1024ULL * 1024ULL));
6180
e30c6e91 6181 if (arg_offline <= 0) {
66a8c74c 6182 r = loop_device_make(fd, O_RDWR, 0, UINT64_MAX, context->sector_size, 0, LOCK_EX, &d);
ffd9d75c 6183 if (r < 0 && (arg_offline == 0 || (r != -ENOENT && !ERRNO_IS_PRIVILEGE(r)) || !strv_isempty(p->subvolumes)))
e30c6e91
DDM
6184 return log_error_errno(r, "Failed to make loopback device of %s: %m", temp);
6185 }
d3201eb4 6186
c4a87b76
DDM
6187 /* We're going to populate this filesystem twice so use a random UUID the first time
6188 * to avoid UUID conflicts. */
6189 r = sd_id128_randomize(&fs_uuid);
6190 if (r < 0)
6191 return r;
59e2be46
DDM
6192 }
6193
d3201eb4
DDM
6194 if (!d || fstype_is_ro(p->format)) {
6195 if (!mkfs_supports_root_option(p->format))
6196 return log_error_errno(SYNTHETIC_ERRNO(ENODEV),
6197 "Loop device access is required to populate %s filesystems",
6198 p->format);
6199
e57b7020 6200 r = partition_populate_directory(context, p, &root);
c4a87b76
DDM
6201 if (r < 0)
6202 return r;
c4a87b76
DDM
6203 }
6204
4b8ce14f
DDM
6205 r = mkfs_options_from_env("REPART", p->format, &extra_mkfs_options);
6206 if (r < 0)
6207 return log_error_errno(r,
6208 "Failed to determine mkfs command line options for '%s': %m",
6209 p->format);
6210
93f125a6
DDM
6211 r = make_filesystem(d ? d->node : temp,
6212 p->format,
6213 strempty(p->new_label),
6214 root,
6215 fs_uuid,
6216 arg_discard, /* quiet = */ false,
6217 context->fs_sector_size,
6218 extra_mkfs_options);
c4a87b76
DDM
6219 if (r < 0)
6220 return r;
6221
6222 /* Read-only filesystems are minimal from the first try because they create and size the
6223 * loopback file for us. */
6224 if (fstype_is_ro(p->format)) {
dea0dc7b 6225 assert(fd < 0);
2843df3e 6226
dea0dc7b
DDM
6227 fd = open(temp, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
6228 if (fd < 0)
6229 return log_error_errno(errno, "Failed to open temporary file %s: %m", temp);
6230
6231 if (fstat(fd, &st) < 0)
b24bfd6e
DDM
6232 return log_error_errno(errno, "Failed to stat temporary file: %m");
6233
6234 log_info("Minimal partition size of %s filesystem of partition %s is %s",
6235 p->format, strna(hint), FORMAT_BYTES(st.st_size));
6236
c4a87b76 6237 p->copy_blocks_path = TAKE_PTR(temp);
7d07030e 6238 p->copy_blocks_path_is_our_file = true;
2843df3e
DDM
6239 p->copy_blocks_fd = TAKE_FD(fd);
6240 p->copy_blocks_size = st.st_size;
c4a87b76
DDM
6241 continue;
6242 }
6243
d3201eb4
DDM
6244 if (!root) {
6245 assert(d);
6246
e57b7020 6247 r = partition_populate_filesystem(context, p, d->node);
59e2be46
DDM
6248 if (r < 0)
6249 return r;
6250 }
c4a87b76
DDM
6251
6252 /* Other filesystems need to be provided with a pre-sized loopback file and will adapt to
6253 * fully occupy it. Because we gave the filesystem a 1T sparse file, we need to shrink the
6254 * filesystem down to a reasonable size again to fit it in the disk image. While there are
6255 * some filesystems that support shrinking, it doesn't always work properly (e.g. shrinking
6256 * btrfs gives us a 2.0G filesystem regardless of what we put in it). Instead, let's populate
6257 * the filesystem again, but this time, instead of providing the filesystem with a 1T sparse
6258 * loopback file, let's size the loopback file based on the actual data used by the
6259 * filesystem in the sparse file after the first attempt. This should be a good guess of the
6260 * minimal amount of space needed in the filesystem to fit all the required data.
6261 */
6262 r = fd_apparent_size(fd, &fsz);
6263 if (r < 0)
6264 return r;
6265
6266 /* Massage the size a bit because just going by actual data used in the sparse file isn't
6267 * fool-proof. */
7bc6c028
DDM
6268 uint64_t heuristic = streq(p->format, "xfs") ? fsz : fsz / 2;
6269 fsz = round_up_size(fsz + heuristic, context->grain_size);
c4a87b76
DDM
6270 if (minimal_size_by_fs_name(p->format) != UINT64_MAX)
6271 fsz = MAX(minimal_size_by_fs_name(p->format), fsz);
6272
b24bfd6e
DDM
6273 log_info("Minimal partition size of %s filesystem of partition %s is %s",
6274 p->format, strna(hint), FORMAT_BYTES(fsz));
6275
d3201eb4
DDM
6276 d = loop_device_unref(d);
6277
c4a87b76 6278 /* Erase the previous filesystem first. */
6c7f4bad 6279 if (ftruncate(fd, 0) < 0)
c4a87b76
DDM
6280 return log_error_errno(errno, "Failed to erase temporary file: %m");
6281
6c7f4bad 6282 if (ftruncate(fd, fsz) < 0)
c4a87b76
DDM
6283 return log_error_errno(errno, "Failed to truncate temporary file to %s: %m", FORMAT_BYTES(fsz));
6284
e30c6e91 6285 if (arg_offline <= 0) {
66a8c74c 6286 r = loop_device_make(fd, O_RDWR, 0, UINT64_MAX, context->sector_size, 0, LOCK_EX, &d);
ffd9d75c 6287 if (r < 0 && (arg_offline == 0 || (r != -ENOENT && !ERRNO_IS_PRIVILEGE(r)) || !strv_isempty(p->subvolumes)))
e30c6e91
DDM
6288 return log_error_errno(r, "Failed to make loopback device of %s: %m", temp);
6289 }
d3201eb4 6290
93f125a6
DDM
6291 r = make_filesystem(d ? d->node : temp,
6292 p->format,
6293 strempty(p->new_label),
6294 root,
6295 p->fs_uuid,
6296 arg_discard,
6297 /* quiet = */ false,
6298 context->fs_sector_size,
6299 extra_mkfs_options);
c4a87b76
DDM
6300 if (r < 0)
6301 return r;
6302
d3201eb4
DDM
6303 if (!root) {
6304 assert(d);
6305
e57b7020 6306 r = partition_populate_filesystem(context, p, d->node);
59e2be46
DDM
6307 if (r < 0)
6308 return r;
6309 }
c4a87b76 6310
6ac299e3
DDM
6311 if (fstat(fd, &st) < 0)
6312 return log_error_errno(errno, "Failed to stat temporary file: %m");
dea0dc7b 6313
c4a87b76 6314 p->copy_blocks_path = TAKE_PTR(temp);
7d07030e 6315 p->copy_blocks_path_is_our_file = true;
2843df3e 6316 p->copy_blocks_fd = TAKE_FD(fd);
6ac299e3 6317 p->copy_blocks_size = st.st_size;
c4a87b76
DDM
6318 }
6319
5eef7047
DDM
6320 /* Now that we've done the data partitions, do the verity hash partitions. We do these in a separate
6321 * step because they might depend on data generated in the previous step. */
6322
6323 LIST_FOREACH(partitions, p, context->partitions) {
6324 _cleanup_(unlink_and_freep) char *temp = NULL;
6325 _cleanup_free_ char *hint = NULL;
2843df3e 6326 _cleanup_close_ int fd = -EBADF;
5eef7047
DDM
6327 struct stat st;
6328 Partition *dp;
6329
6330 if (p->dropped)
6331 continue;
6332
6333 if (PARTITION_EXISTS(p)) /* Never format existing partitions */
6334 continue;
6335
6336 if (p->minimize == MINIMIZE_OFF)
6337 continue;
6338
6339 if (p->verity != VERITY_HASH)
6340 continue;
6341
6342 assert_se(dp = p->siblings[VERITY_DATA]);
6343 assert(!dp->dropped);
6344 assert(dp->copy_blocks_path);
6345
6346 (void) partition_hint(p, context->node, &hint);
6347
6348 log_info("Pre-populating verity hash data of partition %s to calculate minimal partition size",
6349 strna(hint));
6350
c264ec5f
ZJS
6351 if (!vt) {
6352 r = var_tmp_dir(&vt);
6353 if (r < 0)
6354 return log_error_errno(r, "Could not determine temporary directory: %m");
6355 }
6356
5eef7047
DDM
6357 r = tempfn_random_child(vt, "repart", &temp);
6358 if (r < 0)
6359 return log_error_errno(r, "Failed to generate temporary file path: %m");
6360
6361 r = touch(temp);
6362 if (r < 0)
6363 return log_error_errno(r, "Failed to create temporary file: %m");
6364
6365 r = partition_format_verity_hash(context, p, temp, dp->copy_blocks_path);
6366 if (r < 0)
6367 return r;
6368
2843df3e
DDM
6369 fd = open(temp, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
6370 if (fd < 0)
6371 return log_error_errno(errno, "Failed to open temporary file %s: %m", temp);
6372
6373 if (fstat(fd, &st) < 0)
9f08d7f7 6374 return log_error_errno(errno, "Failed to stat temporary file: %m");
5eef7047
DDM
6375
6376 log_info("Minimal partition size of verity hash partition %s is %s",
6377 strna(hint), FORMAT_BYTES(st.st_size));
6378
6379 p->copy_blocks_path = TAKE_PTR(temp);
6380 p->copy_blocks_path_is_our_file = true;
2843df3e
DDM
6381 p->copy_blocks_fd = TAKE_FD(fd);
6382 p->copy_blocks_size = st.st_size;
5eef7047
DDM
6383 }
6384
c4a87b76
DDM
6385 return 0;
6386}
6387
9786dfe6 6388static int parse_partition_types(const char *p, GptPartitionType **partitions, size_t *n_partitions) {
81d1098b
DDM
6389 int r;
6390
220780db
DDM
6391 assert(partitions);
6392 assert(n_partitions);
6393
81d1098b
DDM
6394 for (;;) {
6395 _cleanup_free_ char *name = NULL;
6396 GptPartitionType type;
6397
6398 r = extract_first_word(&p, &name, ",", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS);
6399 if (r == 0)
6400 break;
6401 if (r < 0)
766f52f2 6402 return log_error_errno(r, "Failed to extract partition type identifier or GUID: %s", p);
81d1098b
DDM
6403
6404 r = gpt_partition_type_from_string(name, &type);
6405 if (r < 0)
766f52f2 6406 return log_error_errno(r, "'%s' is not a valid partition type identifier or GUID", name);
81d1098b 6407
220780db 6408 if (!GREEDY_REALLOC(*partitions, *n_partitions + 1))
81d1098b
DDM
6409 return log_oom();
6410
9786dfe6 6411 (*partitions)[(*n_partitions)++] = type;
81d1098b
DDM
6412 }
6413
6414 return 0;
6415}
6416
e594a3b1
LP
6417static int help(void) {
6418 _cleanup_free_ char *link = NULL;
6419 int r;
6420
6dadf31d 6421 r = terminal_urlify_man("systemd-repart", "8", &link);
e594a3b1
LP
6422 if (r < 0)
6423 return log_oom();
6424
6425 printf("%s [OPTIONS...] [DEVICE]\n"
6426 "\n%sGrow and add partitions to partition table.%s\n\n"
6427 " -h --help Show this help\n"
6428 " --version Show package version\n"
896e678b
LP
6429 " --no-pager Do not pipe output into a pager\n"
6430 " --no-legend Do not show the headers and footers\n"
e594a3b1 6431 " --dry-run=BOOL Whether to run dry-run operation\n"
a26f4a49
LP
6432 " --empty=MODE One of refuse, allow, require, force, create; controls\n"
6433 " how to handle empty disks lacking partition tables\n"
e594a3b1 6434 " --discard=BOOL Whether to discard backing blocks for new partitions\n"
2d2d0a57 6435 " --pretty=BOOL Whether to show pretty summary before doing changes\n"
e594a3b1
LP
6436 " --factory-reset=BOOL Whether to remove data partitions before recreating\n"
6437 " them\n"
6438 " --can-factory-reset Test whether factory reset is defined\n"
6439 " --root=PATH Operate relative to root path\n"
252d6267 6440 " --image=PATH Operate relative to image file\n"
84be0c71
LP
6441 " --image-policy=POLICY\n"
6442 " Specify disk image dissection policy\n"
9d252fbb 6443 " --definitions=DIR Find partition definitions in specified directory\n"
b9df3536 6444 " --key-file=PATH Key to use when encrypting partitions\n"
b456191d
DDM
6445 " --private-key=PATH Private key to use when generating verity roothash\n"
6446 " signatures\n"
6447 " --certificate=PATH PEM certificate to use when generating verity\n"
6448 " roothash signatures\n"
889914ef 6449 " --tpm2-device=PATH Path to TPM2 device node to use\n"
78fdf0f6
LP
6450 " --tpm2-device-key=PATH\n"
6451 " Enroll a TPM2 device using its public key\n"
6452 " --tpm2-seal-key-handle=HANDLE\n"
6453 " Specify handle of key to use for sealing\n"
a1788a69 6454 " --tpm2-pcrs=PCR1+PCR2+PCR3+…\n"
889914ef 6455 " TPM2 PCR indexes to use for TPM2 enrollment\n"
02ef97cd
LP
6456 " --tpm2-public-key=PATH\n"
6457 " Enroll signed TPM2 PCR policy against PEM public key\n"
6458 " --tpm2-public-key-pcrs=PCR1+PCR2+PCR3+…\n"
6459 " Enroll signed TPM2 PCR policy for specified TPM2 PCRs\n"
fadc7d8c
LP
6460 " --tpm2-pcrlock=PATH\n"
6461 " Specify pcrlock policy to lock against\n"
da890466 6462 " --seed=UUID 128-bit seed UUID to derive all UUIDs from\n"
a26f4a49 6463 " --size=BYTES Grow loopback file to specified size\n"
2d2d0a57 6464 " --json=pretty|short|off\n"
de8231b0 6465 " Generate JSON output\n"
4cee8333 6466 " --split=BOOL Whether to generate split artifacts\n"
81d1098b 6467 " --include-partitions=PARTITION1,PARTITION2,PARTITION3,…\n"
7d505753 6468 " Ignore partitions not of the specified types\n"
81d1098b 6469 " --exclude-partitions=PARTITION1,PARTITION2,PARTITION3,…\n"
7d505753 6470 " Ignore partitions of the specified types\n"
8275334b 6471 " --defer-partitions=PARTITION1,PARTITION2,PARTITION3,…\n"
7d505753
DDM
6472 " Take partitions of the specified types into account\n"
6473 " but don't populate them yet\n"
e1878ef7 6474 " --sector-size=SIZE Set the logical sector size for the image\n"
9786dfe6 6475 " --architecture=ARCH Set the generic architecture for the image\n"
fc10b158 6476 " --offline=BOOL Whether to build the image offline\n"
607343a1 6477 " -s --copy-source=PATH Specify the primary source tree to copy files from\n"
e1536d1f 6478 " --copy-from=IMAGE Copy partitions from the given image(s)\n"
a121b331
LP
6479 " -S --make-ddi=sysext Make a system extension DDI\n"
6480 " -C --make-ddi=confext Make a configuration extension DDI\n"
6481 " -P --make-ddi=portable Make a portable service DDI\n"
bc556335
DDM
6482 "\nSee the %s for details.\n",
6483 program_invocation_short_name,
6484 ansi_highlight(),
6485 ansi_normal(),
6486 link);
e594a3b1
LP
6487
6488 return 0;
6489}
6490
6491static int parse_argv(int argc, char *argv[]) {
6492
6493 enum {
6494 ARG_VERSION = 0x100,
896e678b
LP
6495 ARG_NO_PAGER,
6496 ARG_NO_LEGEND,
e594a3b1
LP
6497 ARG_DRY_RUN,
6498 ARG_EMPTY,
6499 ARG_DISCARD,
6500 ARG_FACTORY_RESET,
6501 ARG_CAN_FACTORY_RESET,
6502 ARG_ROOT,
252d6267 6503 ARG_IMAGE,
06e78680 6504 ARG_IMAGE_POLICY,
e594a3b1
LP
6505 ARG_SEED,
6506 ARG_PRETTY,
6507 ARG_DEFINITIONS,
a26f4a49 6508 ARG_SIZE,
a015fbe7 6509 ARG_JSON,
b9df3536 6510 ARG_KEY_FILE,
b456191d
DDM
6511 ARG_PRIVATE_KEY,
6512 ARG_CERTIFICATE,
889914ef 6513 ARG_TPM2_DEVICE,
78fdf0f6
LP
6514 ARG_TPM2_DEVICE_KEY,
6515 ARG_TPM2_SEAL_KEY_HANDLE,
889914ef 6516 ARG_TPM2_PCRS,
02ef97cd
LP
6517 ARG_TPM2_PUBLIC_KEY,
6518 ARG_TPM2_PUBLIC_KEY_PCRS,
404aea78 6519 ARG_TPM2_PCRLOCK,
4cee8333 6520 ARG_SPLIT,
81d1098b
DDM
6521 ARG_INCLUDE_PARTITIONS,
6522 ARG_EXCLUDE_PARTITIONS,
8275334b 6523 ARG_DEFER_PARTITIONS,
e1878ef7 6524 ARG_SECTOR_SIZE,
84be0c71 6525 ARG_SKIP_PARTITIONS,
9786dfe6 6526 ARG_ARCHITECTURE,
fc10b158 6527 ARG_OFFLINE,
1e46985a 6528 ARG_COPY_FROM,
a121b331 6529 ARG_MAKE_DDI,
e594a3b1
LP
6530 };
6531
6532 static const struct option options[] = {
02ef97cd
LP
6533 { "help", no_argument, NULL, 'h' },
6534 { "version", no_argument, NULL, ARG_VERSION },
6535 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
6536 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
6537 { "dry-run", required_argument, NULL, ARG_DRY_RUN },
6538 { "empty", required_argument, NULL, ARG_EMPTY },
6539 { "discard", required_argument, NULL, ARG_DISCARD },
6540 { "factory-reset", required_argument, NULL, ARG_FACTORY_RESET },
6541 { "can-factory-reset", no_argument, NULL, ARG_CAN_FACTORY_RESET },
6542 { "root", required_argument, NULL, ARG_ROOT },
6543 { "image", required_argument, NULL, ARG_IMAGE },
06e78680 6544 { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY },
02ef97cd
LP
6545 { "seed", required_argument, NULL, ARG_SEED },
6546 { "pretty", required_argument, NULL, ARG_PRETTY },
6547 { "definitions", required_argument, NULL, ARG_DEFINITIONS },
6548 { "size", required_argument, NULL, ARG_SIZE },
6549 { "json", required_argument, NULL, ARG_JSON },
6550 { "key-file", required_argument, NULL, ARG_KEY_FILE },
b456191d
DDM
6551 { "private-key", required_argument, NULL, ARG_PRIVATE_KEY },
6552 { "certificate", required_argument, NULL, ARG_CERTIFICATE },
02ef97cd 6553 { "tpm2-device", required_argument, NULL, ARG_TPM2_DEVICE },
78fdf0f6
LP
6554 { "tpm2-device-key", required_argument, NULL, ARG_TPM2_DEVICE_KEY },
6555 { "tpm2-seal-key-handle", required_argument, NULL, ARG_TPM2_SEAL_KEY_HANDLE },
02ef97cd
LP
6556 { "tpm2-pcrs", required_argument, NULL, ARG_TPM2_PCRS },
6557 { "tpm2-public-key", required_argument, NULL, ARG_TPM2_PUBLIC_KEY },
6558 { "tpm2-public-key-pcrs", required_argument, NULL, ARG_TPM2_PUBLIC_KEY_PCRS },
404aea78 6559 { "tpm2-pcrlock", required_argument, NULL, ARG_TPM2_PCRLOCK },
4cee8333 6560 { "split", required_argument, NULL, ARG_SPLIT },
81d1098b
DDM
6561 { "include-partitions", required_argument, NULL, ARG_INCLUDE_PARTITIONS },
6562 { "exclude-partitions", required_argument, NULL, ARG_EXCLUDE_PARTITIONS },
8275334b 6563 { "defer-partitions", required_argument, NULL, ARG_DEFER_PARTITIONS },
e1878ef7 6564 { "sector-size", required_argument, NULL, ARG_SECTOR_SIZE },
9786dfe6 6565 { "architecture", required_argument, NULL, ARG_ARCHITECTURE },
fc10b158 6566 { "offline", required_argument, NULL, ARG_OFFLINE },
1e46985a 6567 { "copy-from", required_argument, NULL, ARG_COPY_FROM },
607343a1 6568 { "copy-source", required_argument, NULL, 's' },
a121b331 6569 { "make-ddi", required_argument, NULL, ARG_MAKE_DDI },
e594a3b1
LP
6570 {}
6571 };
6572
404aea78 6573 bool auto_hash_pcr_values = true, auto_public_key_pcr_mask = true, auto_pcrlock = true;
ba6bd342 6574 int c, r;
e594a3b1
LP
6575
6576 assert(argc >= 0);
6577 assert(argv);
6578
a121b331 6579 while ((c = getopt_long(argc, argv, "hs:SCP", options, NULL)) >= 0)
e594a3b1
LP
6580
6581 switch (c) {
6582
6583 case 'h':
6584 return help();
6585
6586 case ARG_VERSION:
6587 return version();
6588
896e678b
LP
6589 case ARG_NO_PAGER:
6590 arg_pager_flags |= PAGER_DISABLE;
6591 break;
6592
6593 case ARG_NO_LEGEND:
6594 arg_legend = false;
6595 break;
6596
e594a3b1 6597 case ARG_DRY_RUN:
599c7c54 6598 r = parse_boolean_argument("--dry-run=", optarg, &arg_dry_run);
e594a3b1 6599 if (r < 0)
599c7c54 6600 return r;
e594a3b1
LP
6601 break;
6602
6603 case ARG_EMPTY:
634b8471 6604 if (isempty(optarg)) {
243dd1e9 6605 arg_empty = EMPTY_UNSET;
634b8471
LP
6606 break;
6607 }
6608
6609 arg_empty = empty_mode_from_string(optarg);
6610 if (arg_empty < 0)
6611 return log_error_errno(arg_empty, "Failed to parse --empty= parameter: %s", optarg);
6612
e594a3b1
LP
6613 break;
6614
6615 case ARG_DISCARD:
599c7c54 6616 r = parse_boolean_argument("--discard=", optarg, &arg_discard);
e594a3b1 6617 if (r < 0)
599c7c54 6618 return r;
e594a3b1
LP
6619 break;
6620
6621 case ARG_FACTORY_RESET:
c3470872 6622 r = parse_boolean_argument("--factory-reset=", optarg, NULL);
e594a3b1 6623 if (r < 0)
c3470872 6624 return r;
e594a3b1
LP
6625 arg_factory_reset = r;
6626 break;
6627
6628 case ARG_CAN_FACTORY_RESET:
6629 arg_can_factory_reset = true;
6630 break;
6631
6632 case ARG_ROOT:
252d6267
LP
6633 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root);
6634 if (r < 0)
6635 return r;
6636 break;
6637
6638 case ARG_IMAGE:
6639 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
e594a3b1
LP
6640 if (r < 0)
6641 return r;
6642 break;
6643
06e78680
YW
6644 case ARG_IMAGE_POLICY:
6645 r = parse_image_policy_argument(optarg, &arg_image_policy);
6646 if (r < 0)
6647 return r;
6648 break;
6649
e594a3b1
LP
6650 case ARG_SEED:
6651 if (isempty(optarg)) {
6652 arg_seed = SD_ID128_NULL;
6653 arg_randomize = false;
6654 } else if (streq(optarg, "random"))
6655 arg_randomize = true;
6656 else {
6657 r = sd_id128_from_string(optarg, &arg_seed);
6658 if (r < 0)
6659 return log_error_errno(r, "Failed to parse seed: %s", optarg);
6660
6661 arg_randomize = false;
6662 }
6663
6664 break;
6665
6666 case ARG_PRETTY:
c3470872 6667 r = parse_boolean_argument("--pretty=", optarg, NULL);
e594a3b1 6668 if (r < 0)
c3470872 6669 return r;
e594a3b1
LP
6670 arg_pretty = r;
6671 break;
6672
224c853f
RP
6673 case ARG_DEFINITIONS: {
6674 _cleanup_free_ char *path = NULL;
6675 r = parse_path_argument(optarg, false, &path);
e594a3b1
LP
6676 if (r < 0)
6677 return r;
224c853f
RP
6678 if (strv_consume(&arg_definitions, TAKE_PTR(path)) < 0)
6679 return log_oom();
e594a3b1 6680 break;
224c853f 6681 }
e594a3b1 6682
a26f4a49
LP
6683 case ARG_SIZE: {
6684 uint64_t parsed, rounded;
6685
170c9823
LP
6686 if (streq(optarg, "auto")) {
6687 arg_size = UINT64_MAX;
6688 arg_size_auto = true;
6689 break;
6690 }
6691
a26f4a49
LP
6692 r = parse_size(optarg, 1024, &parsed);
6693 if (r < 0)
6694 return log_error_errno(r, "Failed to parse --size= parameter: %s", optarg);
6695
6696 rounded = round_up_size(parsed, 4096);
6697 if (rounded == 0)
6698 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too small, refusing.");
6699 if (rounded == UINT64_MAX)
6700 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too large, refusing.");
6701
6702 if (rounded != parsed)
e2341b6b
DT
6703 log_warning("Specified size is not a multiple of 4096, rounding up automatically. (%" PRIu64 " %s %" PRIu64 ")",
6704 parsed, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), rounded);
a26f4a49
LP
6705
6706 arg_size = rounded;
170c9823 6707 arg_size_auto = false;
a26f4a49
LP
6708 break;
6709 }
b9df3536 6710
a015fbe7 6711 case ARG_JSON:
b1e8f46c 6712 r = parse_json_argument(optarg, &arg_json_format_flags);
6a01ea4a
LP
6713 if (r <= 0)
6714 return r;
a015fbe7
TH
6715
6716 break;
6717
b9df3536
LP
6718 case ARG_KEY_FILE: {
6719 _cleanup_(erase_and_freep) char *k = NULL;
6720 size_t n = 0;
6721
8b3c3a49 6722 r = read_full_file_full(
986311c2 6723 AT_FDCWD, optarg, UINT64_MAX, SIZE_MAX,
8b3c3a49
LP
6724 READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
6725 NULL,
6726 &k, &n);
b9df3536
LP
6727 if (r < 0)
6728 return log_error_errno(r, "Failed to read key file '%s': %m", optarg);
6729
6730 erase_and_free(arg_key);
6731 arg_key = TAKE_PTR(k);
6732 arg_key_size = n;
6733 break;
6734 }
a26f4a49 6735
b456191d
DDM
6736 case ARG_PRIVATE_KEY: {
6737 _cleanup_(erase_and_freep) char *k = NULL;
6738 size_t n = 0;
6739
6740 r = read_full_file_full(
6741 AT_FDCWD, optarg, UINT64_MAX, SIZE_MAX,
6742 READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
6743 NULL,
6744 &k, &n);
6745 if (r < 0)
6746 return log_error_errno(r, "Failed to read key file '%s': %m", optarg);
6747
6748 EVP_PKEY_free(arg_private_key);
6749 arg_private_key = NULL;
6750 r = parse_private_key(k, n, &arg_private_key);
6751 if (r < 0)
6752 return r;
6753 break;
6754 }
6755
6756 case ARG_CERTIFICATE: {
6757 _cleanup_free_ char *cert = NULL;
6758 size_t n = 0;
6759
6760 r = read_full_file_full(
6761 AT_FDCWD, optarg, UINT64_MAX, SIZE_MAX,
6762 READ_FULL_FILE_CONNECT_SOCKET,
6763 NULL,
6764 &cert, &n);
6765 if (r < 0)
6766 return log_error_errno(r, "Failed to read certificate file '%s': %m", optarg);
6767
6768 X509_free(arg_certificate);
6769 arg_certificate = NULL;
6770 r = parse_x509_certificate(cert, n, &arg_certificate);
6771 if (r < 0)
6772 return r;
6773 break;
6774 }
6775
889914ef
LP
6776 case ARG_TPM2_DEVICE: {
6777 _cleanup_free_ char *device = NULL;
6778
6779 if (streq(optarg, "list"))
6780 return tpm2_list_devices();
6781
6782 if (!streq(optarg, "auto")) {
6783 device = strdup(optarg);
6784 if (!device)
6785 return log_oom();
6786 }
6787
6788 free(arg_tpm2_device);
6789 arg_tpm2_device = TAKE_PTR(device);
6790 break;
6791 }
6792
78fdf0f6
LP
6793 case ARG_TPM2_DEVICE_KEY:
6794 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_tpm2_device_key);
6795 if (r < 0)
6796 return r;
6797
6798 break;
6799
6800 case ARG_TPM2_SEAL_KEY_HANDLE:
6801 r = safe_atou32_full(optarg, 16, &arg_tpm2_seal_key_handle);
6802 if (r < 0)
6803 return log_error_errno(r, "Could not parse TPM2 seal key handle index '%s': %m", optarg);
6804
6805 break;
6806
222a951f 6807 case ARG_TPM2_PCRS:
c588cf97 6808 auto_hash_pcr_values = false;
9e437994 6809 r = tpm2_parse_pcr_argument_append(optarg, &arg_tpm2_hash_pcr_values, &arg_tpm2_n_hash_pcr_values);
889914ef
LP
6810 if (r < 0)
6811 return r;
6812
889914ef 6813 break;
889914ef 6814
02ef97cd
LP
6815 case ARG_TPM2_PUBLIC_KEY:
6816 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_tpm2_public_key);
6817 if (r < 0)
6818 return r;
6819
6820 break;
6821
6822 case ARG_TPM2_PUBLIC_KEY_PCRS:
c588cf97 6823 auto_public_key_pcr_mask = false;
07c04061 6824 r = tpm2_parse_pcr_argument_to_mask(optarg, &arg_tpm2_public_key_pcr_mask);
02ef97cd
LP
6825 if (r < 0)
6826 return r;
6827
6828 break;
6829
404aea78
LP
6830 case ARG_TPM2_PCRLOCK:
6831 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_tpm2_pcrlock);
6832 if (r < 0)
6833 return r;
6834
6835 auto_pcrlock = false;
6836 break;
6837
4cee8333
DDM
6838 case ARG_SPLIT:
6839 r = parse_boolean_argument("--split=", optarg, NULL);
6840 if (r < 0)
6841 return r;
6842
6843 arg_split = r;
6844 break;
6845
81d1098b
DDM
6846 case ARG_INCLUDE_PARTITIONS:
6847 if (arg_filter_partitions_type == FILTER_PARTITIONS_EXCLUDE)
6848 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
6849 "Combination of --include-partitions= and --exclude-partitions= is invalid.");
6850
220780db 6851 r = parse_partition_types(optarg, &arg_filter_partitions, &arg_n_filter_partitions);
81d1098b
DDM
6852 if (r < 0)
6853 return r;
6854
6855 arg_filter_partitions_type = FILTER_PARTITIONS_INCLUDE;
6856
6857 break;
6858
6859 case ARG_EXCLUDE_PARTITIONS:
6860 if (arg_filter_partitions_type == FILTER_PARTITIONS_INCLUDE)
6861 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
6862 "Combination of --include-partitions= and --exclude-partitions= is invalid.");
6863
220780db 6864 r = parse_partition_types(optarg, &arg_filter_partitions, &arg_n_filter_partitions);
81d1098b
DDM
6865 if (r < 0)
6866 return r;
6867
6868 arg_filter_partitions_type = FILTER_PARTITIONS_EXCLUDE;
6869
6870 break;
6871
8275334b
DDM
6872 case ARG_DEFER_PARTITIONS:
6873 r = parse_partition_types(optarg, &arg_defer_partitions, &arg_n_defer_partitions);
7d505753
DDM
6874 if (r < 0)
6875 return r;
6876
6877 break;
6878
e1878ef7
DDM
6879 case ARG_SECTOR_SIZE:
6880 r = parse_sector_size(optarg, &arg_sector_size);
6881 if (r < 0)
6882 return r;
6883
6884 break;
6885
9786dfe6
DDM
6886 case ARG_ARCHITECTURE:
6887 r = architecture_from_string(optarg);
6888 if (r < 0)
6889 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid architecture '%s'", optarg);
6890
6891 arg_architecture = r;
6892 break;
6893
fc10b158
DDM
6894 case ARG_OFFLINE:
6895 if (streq(optarg, "auto"))
6896 arg_offline = -1;
6897 else {
6898 r = parse_boolean_argument("--offline=", optarg, NULL);
6899 if (r < 0)
6900 return r;
6901
6902 arg_offline = r;
6903 }
6904
6905 break;
6906
e1536d1f
DDM
6907 case ARG_COPY_FROM: {
6908 _cleanup_free_ char *p = NULL;
6909
6910 r = parse_path_argument(optarg, /* suppress_root= */ false, &p);
1e46985a
DDM
6911 if (r < 0)
6912 return r;
e1536d1f
DDM
6913
6914 if (strv_consume(&arg_copy_from, TAKE_PTR(p)) < 0)
6915 return log_oom();
6916
1e46985a 6917 break;
e1536d1f 6918 }
1e46985a 6919
607343a1
LP
6920 case 's':
6921 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_copy_source);
6922 if (r < 0)
6923 return r;
6924 break;
6925
a121b331
LP
6926 case ARG_MAKE_DDI:
6927 if (!filename_is_valid(optarg))
6928 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid DDI type: %s", optarg);
6929
6930 r = free_and_strdup_warn(&arg_make_ddi, optarg);
6931 if (r < 0)
6932 return r;
6933 break;
6934
6935 case 'S':
6936 r = free_and_strdup_warn(&arg_make_ddi, "sysext");
6937 if (r < 0)
6938 return r;
6939 break;
6940
6941 case 'C':
6942 r = free_and_strdup_warn(&arg_make_ddi, "confext");
6943 if (r < 0)
6944 return r;
6945 break;
6946
6947 case 'P':
6948 r = free_and_strdup_warn(&arg_make_ddi, "portable");
6949 if (r < 0)
6950 return r;
6951 break;
6952
e594a3b1
LP
6953 case '?':
6954 return -EINVAL;
6955
6956 default:
04499a70 6957 assert_not_reached();
e594a3b1
LP
6958 }
6959
6960 if (argc - optind > 1)
6961 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
37a38788 6962 "Expected at most one argument, the path to the block device or image file.");
e594a3b1 6963
a121b331
LP
6964 if (arg_make_ddi) {
6965 if (arg_definitions)
6966 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Combination of --make-ddi= and --definitions= is not supported.");
6967 if (!IN_SET(arg_empty, EMPTY_UNSET, EMPTY_CREATE))
634b8471 6968 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Combination of --make-ddi= and --empty=%s is not supported.", empty_mode_to_string(arg_empty));
a121b331
LP
6969
6970 /* Imply automatic sizing in DDI mode */
6971 if (arg_size == UINT64_MAX)
6972 arg_size_auto = true;
6973
6974 if (!arg_copy_source)
6975 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No --copy-source= specified, refusing.");
6976
6977 r = dir_is_empty(arg_copy_source, /* ignore_hidden_or_backup= */ false);
6978 if (r < 0)
6979 return log_error_errno(r, "Failed to determine if '%s' is empty: %m", arg_copy_source);
6980 if (r > 0)
6981 return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Source directory '%s' is empty, refusing to create empty image.", arg_copy_source);
6982
6983 if (sd_id128_is_null(arg_seed) && !arg_randomize) {
6984 /* We don't want that /etc/machine-id leaks into any image built this way, hence
6985 * let's randomize the seed if not specified explicitly */
6986 log_notice("No seed value specified, randomizing generated UUIDs, resulting image will not be reproducible.");
6987 arg_randomize = true;
6988 }
6989
6990 arg_empty = EMPTY_CREATE;
6991 }
6992
243dd1e9
LP
6993 if (arg_empty == EMPTY_UNSET) /* default to refuse mode, if not otherwise specified */
6994 arg_empty = EMPTY_REFUSE;
6995
a26f4a49 6996 if (arg_factory_reset > 0 && IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE))
e594a3b1 6997 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
a26f4a49 6998 "Combination of --factory-reset=yes and --empty=force/--empty=require/--empty=create is invalid.");
e594a3b1
LP
6999
7000 if (arg_can_factory_reset)
a26f4a49
LP
7001 arg_dry_run = true; /* When --can-factory-reset is specified we don't make changes, hence
7002 * non-dry-run mode makes no sense. Thus, imply dry run mode so that we
7003 * open things strictly read-only. */
ba6bd342
LP
7004 else if (arg_empty == EMPTY_CREATE)
7005 arg_dry_run = false; /* Imply --dry-run=no if we create the loopback file anew. After all we
7006 * cannot really break anyone's partition tables that way. */
a26f4a49 7007
6c05395e
LP
7008 /* Disable pager once we are not just reviewing, but doing things. */
7009 if (!arg_dry_run)
7010 arg_pager_flags |= PAGER_DISABLE;
7011
248f0186 7012 if (arg_empty == EMPTY_CREATE && arg_size == UINT64_MAX && !arg_size_auto)
a26f4a49
LP
7013 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
7014 "If --empty=create is specified, --size= must be specified, too.");
e594a3b1 7015
252d6267
LP
7016 if (arg_image && arg_root)
7017 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
7018 else if (!arg_image && !arg_root && in_initrd()) {
8f47e32a
LP
7019
7020 /* By default operate on /sysusr/ or /sysroot/ when invoked in the initrd. We prefer the
7021 * former, if it is mounted, so that we have deterministic behaviour on systems where /usr/
7022 * is vendor-supplied but the root fs formatted on first boot. */
7023 r = path_is_mount_point("/sysusr/usr", NULL, 0);
7024 if (r <= 0) {
7025 if (r < 0 && r != -ENOENT)
7026 log_debug_errno(r, "Unable to determine whether /sysusr/usr is a mount point, assuming it is not: %m");
7027
7028 arg_root = strdup("/sysroot");
7029 } else
7030 arg_root = strdup("/sysusr");
252d6267
LP
7031 if (!arg_root)
7032 return log_oom();
7033 }
7034
e594a3b1 7035 arg_node = argc > optind ? argv[optind] : NULL;
a26f4a49 7036
252d6267 7037 if (IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE) && !arg_node && !arg_image)
a26f4a49 7038 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
37a38788 7039 "A path to a device node or image file must be specified when --make-ddi=, --empty=force, --empty=require or --empty=create are used.");
a26f4a49 7040
4cee8333
DDM
7041 if (arg_split && !arg_node)
7042 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
37a38788 7043 "A path to an image file must be specified when --split is used.");
4cee8333 7044
404aea78
LP
7045 if (auto_pcrlock) {
7046 assert(!arg_tpm2_pcrlock);
7047
7048 r = tpm2_pcrlock_search_file(NULL, NULL, &arg_tpm2_pcrlock);
7049 if (r < 0) {
7050 if (r != -ENOENT)
7051 log_warning_errno(r, "Search for pcrlock.json failed, assuming it does not exist: %m");
7052 } else
dc7e34b3 7053 log_debug("Automatically using pcrlock policy '%s'.", arg_tpm2_pcrlock);
404aea78
LP
7054 }
7055
2e9f6072 7056 if (auto_public_key_pcr_mask) {
c588cf97 7057 assert(arg_tpm2_public_key_pcr_mask == 0);
2099cd62 7058 arg_tpm2_public_key_pcr_mask = INDEX_TO_MASK(uint32_t, TPM2_PCR_KERNEL_BOOT);
c588cf97 7059 }
9e437994 7060
404aea78 7061 if (auto_hash_pcr_values && !arg_tpm2_pcrlock) { /* Only lock to PCR 7 if no pcr policy is specified. */
c588cf97
LP
7062 assert(arg_tpm2_n_hash_pcr_values == 0);
7063
7064 if (!GREEDY_REALLOC_APPEND(
7065 arg_tpm2_hash_pcr_values,
7066 arg_tpm2_n_hash_pcr_values,
7067 &TPM2_PCR_VALUE_MAKE(TPM2_PCR_INDEX_DEFAULT, /* hash= */ 0, /* value= */ {}),
7068 1))
7069 return log_oom();
7070 }
889914ef 7071
a26d463d
DDM
7072 if (arg_pretty < 0 && isatty(STDOUT_FILENO))
7073 arg_pretty = true;
7074
9786dfe6
DDM
7075 if (arg_architecture >= 0) {
7076 FOREACH_ARRAY(p, arg_filter_partitions, arg_n_filter_partitions)
7077 *p = gpt_partition_type_override_architecture(*p, arg_architecture);
7078
7079 FOREACH_ARRAY(p, arg_defer_partitions, arg_n_defer_partitions)
7080 *p = gpt_partition_type_override_architecture(*p, arg_architecture);
7081 }
7082
e594a3b1
LP
7083 return 1;
7084}
7085
7086static int parse_proc_cmdline_factory_reset(void) {
7087 bool b;
7088 int r;
7089
7090 if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */
7091 return 0;
7092
7093 if (!in_initrd()) /* Never honour kernel command line factory reset request outside of the initrd */
7094 return 0;
7095
3787934b 7096 r = proc_cmdline_get_bool("systemd.factory_reset", /* flags = */ 0, &b);
e594a3b1
LP
7097 if (r < 0)
7098 return log_error_errno(r, "Failed to parse systemd.factory_reset kernel command line argument: %m");
7099 if (r > 0) {
7100 arg_factory_reset = b;
7101
7102 if (b)
7103 log_notice("Honouring factory reset requested via kernel command line.");
7104 }
7105
7106 return 0;
7107}
7108
7109static int parse_efi_variable_factory_reset(void) {
7110 _cleanup_free_ char *value = NULL;
7111 int r;
7112
7113 if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */
7114 return 0;
7115
7116 if (!in_initrd()) /* Never honour EFI variable factory reset request outside of the initrd */
7117 return 0;
7118
e6f055cb 7119 r = efi_get_variable_string(EFI_SYSTEMD_VARIABLE(FactoryReset), &value);
6ce691c3
DL
7120 if (r < 0) {
7121 if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
7122 return 0;
e594a3b1 7123 return log_error_errno(r, "Failed to read EFI variable FactoryReset: %m");
6ce691c3 7124 }
e594a3b1
LP
7125
7126 r = parse_boolean(value);
7127 if (r < 0)
7128 return log_error_errno(r, "Failed to parse EFI variable FactoryReset: %m");
7129
7130 arg_factory_reset = r;
7131 if (r)
111a3aae 7132 log_notice("Factory reset requested via EFI variable FactoryReset.");
e594a3b1
LP
7133
7134 return 0;
7135}
7136
7137static int remove_efi_variable_factory_reset(void) {
7138 int r;
7139
e6f055cb 7140 r = efi_set_variable(EFI_SYSTEMD_VARIABLE(FactoryReset), NULL, 0);
6ce691c3
DL
7141 if (r < 0) {
7142 if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
7143 return 0;
e594a3b1 7144 return log_error_errno(r, "Failed to remove EFI variable FactoryReset: %m");
6ce691c3 7145 }
e594a3b1
LP
7146
7147 log_info("Successfully unset EFI variable FactoryReset.");
7148 return 0;
7149}
7150
252d6267
LP
7151static int acquire_root_devno(
7152 const char *p,
7153 const char *root,
7154 int mode,
7155 char **ret,
7156 int *ret_fd) {
7157
37734dc6 7158 _cleanup_free_ char *found_path = NULL, *node = NULL;
252d6267 7159 dev_t devno, fd_devno = MODE_INVALID;
254d1313 7160 _cleanup_close_ int fd = -EBADF;
e594a3b1 7161 struct stat st;
e594a3b1
LP
7162 int r;
7163
a26f4a49
LP
7164 assert(p);
7165 assert(ret);
7166 assert(ret_fd);
7167
f461a28d 7168 fd = chase_and_open(p, root, CHASE_PREFIX_ROOT, mode, &found_path);
e594a3b1 7169 if (fd < 0)
252d6267 7170 return fd;
e594a3b1
LP
7171
7172 if (fstat(fd, &st) < 0)
7173 return -errno;
7174
7175 if (S_ISREG(st.st_mode)) {
252d6267 7176 *ret = TAKE_PTR(found_path);
a26f4a49 7177 *ret_fd = TAKE_FD(fd);
e594a3b1
LP
7178 return 0;
7179 }
7180
252d6267
LP
7181 if (S_ISBLK(st.st_mode)) {
7182 /* Refuse referencing explicit block devices if a root dir is specified, after all we should
5c08da58 7183 * not be able to leave the image the root path constrains us to. */
252d6267
LP
7184 if (root)
7185 return -EPERM;
7186
a26f4a49 7187 fd_devno = devno = st.st_rdev;
252d6267 7188 } else if (S_ISDIR(st.st_mode)) {
e594a3b1
LP
7189
7190 devno = st.st_dev;
a26f4a49 7191 if (major(devno) == 0) {
e594a3b1
LP
7192 r = btrfs_get_block_device_fd(fd, &devno);
7193 if (r == -ENOTTY) /* not btrfs */
7194 return -ENODEV;
7195 if (r < 0)
7196 return r;
7197 }
e594a3b1
LP
7198 } else
7199 return -ENOTBLK;
7200
7201 /* From dm-crypt to backing partition */
7202 r = block_get_originating(devno, &devno);
8e5f3cec
LP
7203 if (r == -ENOENT)
7204 log_debug_errno(r, "Device '%s' has no dm-crypt/dm-verity device, no need to look for underlying block device.", p);
7205 else if (r < 0)
e594a3b1
LP
7206 log_debug_errno(r, "Failed to find underlying block device for '%s', ignoring: %m", p);
7207
7208 /* From partition to whole disk containing it */
7209 r = block_get_whole_disk(devno, &devno);
7210 if (r < 0)
162392b7 7211 log_debug_errno(r, "Failed to find whole disk block device for '%s', ignoring: %m", p);
e594a3b1 7212
37734dc6 7213 r = devname_from_devnum(S_IFBLK, devno, &node);
a26f4a49
LP
7214 if (r < 0)
7215 return log_debug_errno(r, "Failed to determine canonical path for '%s': %m", p);
7216
6bbae9f8 7217 /* Only if we still look at the same block device we can reuse the fd. Otherwise return an
a26f4a49 7218 * invalidated fd. */
37734dc6
YW
7219 if (fd_devno != MODE_INVALID && fd_devno == devno) {
7220 /* Tell udev not to interfere while we are processing the device */
7221 if (flock(fd, arg_dry_run ? LOCK_SH : LOCK_EX) < 0)
7222 return log_error_errno(errno, "Failed to lock device '%s': %m", node);
7223
7224 *ret_fd = TAKE_FD(fd);
7225 } else
7226 *ret_fd = -EBADF;
7227
7228 *ret = TAKE_PTR(node);
a26f4a49 7229 return 0;
e594a3b1
LP
7230}
7231
cc751c75 7232static int find_root(Context *context) {
54632d2e 7233 _cleanup_free_ char *device = NULL;
5980d463 7234 int r;
e594a3b1 7235
cc751c75 7236 assert(context);
a26f4a49 7237
e594a3b1 7238 if (arg_node) {
a26f4a49 7239 if (arg_empty == EMPTY_CREATE) {
254d1313 7240 _cleanup_close_ int fd = -EBADF;
a26f4a49
LP
7241 _cleanup_free_ char *s = NULL;
7242
7243 s = strdup(arg_node);
7244 if (!s)
7245 return log_oom();
7246
5332d7c6 7247 fd = open(arg_node, O_RDONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOFOLLOW, 0666);
a26f4a49
LP
7248 if (fd < 0)
7249 return log_error_errno(errno, "Failed to create '%s': %m", arg_node);
7250
cc751c75 7251 context->node = TAKE_PTR(s);
db1d4e6b 7252 context->node_is_our_file = true;
cc751c75 7253 context->backing_fd = TAKE_FD(fd);
a26f4a49
LP
7254 return 0;
7255 }
7256
252d6267
LP
7257 /* Note that we don't specify a root argument here: if the user explicitly configured a node
7258 * we'll take it relative to the host, not the image */
cc751c75 7259 r = acquire_root_devno(arg_node, NULL, O_RDONLY|O_CLOEXEC, &context->node, &context->backing_fd);
67f0ac8c
LP
7260 if (r == -EUCLEAN)
7261 return btrfs_log_dev_root(LOG_ERR, r, arg_node);
e594a3b1 7262 if (r < 0)
aa2a74ad 7263 return log_error_errno(r, "Failed to open file or determine backing device of %s: %m", arg_node);
e594a3b1
LP
7264
7265 return 0;
7266 }
7267
a26f4a49
LP
7268 assert(IN_SET(arg_empty, EMPTY_REFUSE, EMPTY_ALLOW));
7269
54632d2e
KK
7270 /* If the root mount has been replaced by some form of volatile file system (overlayfs), the
7271 * original root block device node is symlinked in /run/systemd/volatile-root. Let's read that
7272 * here. */
7273 r = readlink_malloc("/run/systemd/volatile-root", &device);
7274 if (r == -ENOENT) { /* volatile-root not found */
7275 /* Let's search for the root device. We look for two cases here: first in /, and then in /usr. The
7276 * latter we check for cases where / is a tmpfs and only /usr is an actual persistent block device
7277 * (think: volatile setups) */
e594a3b1 7278
54632d2e 7279 FOREACH_STRING(p, "/", "/usr") {
e594a3b1 7280
cc751c75
DDM
7281 r = acquire_root_devno(p, arg_root, O_RDONLY|O_DIRECTORY|O_CLOEXEC, &context->node,
7282 &context->backing_fd);
54632d2e
KK
7283 if (r < 0) {
7284 if (r == -EUCLEAN)
7285 return btrfs_log_dev_root(LOG_ERR, r, p);
7286 if (r != -ENODEV)
7287 return log_error_errno(r, "Failed to determine backing device of %s: %m", p);
7288 } else
7289 return 0;
7290 }
7291 } else if (r < 0)
7292 return log_error_errno(r, "Failed to read symlink /run/systemd/volatile-root: %m");
7293 else {
cc751c75 7294 r = acquire_root_devno(device, NULL, O_RDONLY|O_CLOEXEC, &context->node, &context->backing_fd);
54632d2e
KK
7295 if (r == -EUCLEAN)
7296 return btrfs_log_dev_root(LOG_ERR, r, device);
7297 if (r < 0)
7298 return log_error_errno(r, "Failed to open file or determine backing device of %s: %m", device);
7299
7300 return 0;
e594a3b1
LP
7301 }
7302
7303 return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "Failed to discover root block device.");
7304}
7305
81dde3d8 7306static int resize_pt(int fd, uint64_t sector_size) {
f9b3afae
LP
7307 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
7308 int r;
7309
7310 /* After resizing the backing file we need to resize the partition table itself too, so that it takes
7311 * possession of the enlarged backing file. For this it suffices to open the device with libfdisk and
7312 * immediately write it again, with no changes. */
7313
fd9fe57a 7314 r = fdisk_new_context_at(fd, /* path= */ NULL, /* read_only= */ false, sector_size, &c);
f9b3afae 7315 if (r < 0)
ddb6eeaf 7316 return log_error_errno(r, "Failed to open device '%s': %m", FORMAT_PROC_FD_PATH(fd));
f9b3afae
LP
7317
7318 r = fdisk_has_label(c);
7319 if (r < 0)
ddb6eeaf 7320 return log_error_errno(r, "Failed to determine whether disk '%s' has a disk label: %m", FORMAT_PROC_FD_PATH(fd));
f9b3afae
LP
7321 if (r == 0) {
7322 log_debug("Not resizing partition table, as there currently is none.");
7323 return 0;
7324 }
7325
7326 r = fdisk_write_disklabel(c);
7327 if (r < 0)
7328 return log_error_errno(r, "Failed to write resized partition table: %m");
7329
7330 log_info("Resized partition table.");
7331 return 1;
7332}
7333
252d6267
LP
7334static int resize_backing_fd(
7335 const char *node, /* The primary way we access the disk image to operate on */
7336 int *fd, /* An O_RDONLY fd referring to that inode */
7337 const char *backing_file, /* If the above refers to a loopback device, the backing regular file for that, which we can grow */
81dde3d8
LP
7338 LoopDevice *loop_device,
7339 uint64_t sector_size) {
252d6267 7340
254d1313 7341 _cleanup_close_ int writable_fd = -EBADF;
252d6267 7342 uint64_t current_size;
a26f4a49
LP
7343 struct stat st;
7344 int r;
7345
7346 assert(node);
7347 assert(fd);
7348
7349 if (arg_size == UINT64_MAX) /* Nothing to do */
7350 return 0;
7351
7352 if (*fd < 0) {
7353 /* Open the file if we haven't opened it yet. Note that we open it read-only here, just to
7354 * keep a reference to the file we can pass around. */
7355 *fd = open(node, O_RDONLY|O_CLOEXEC);
7356 if (*fd < 0)
7357 return log_error_errno(errno, "Failed to open '%s' in order to adjust size: %m", node);
7358 }
7359
7360 if (fstat(*fd, &st) < 0)
7361 return log_error_errno(errno, "Failed to stat '%s': %m", node);
7362
252d6267
LP
7363 if (S_ISBLK(st.st_mode)) {
7364 if (!backing_file)
7365 return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Cannot resize block device '%s'.", node);
7366
7367 assert(loop_device);
a26f4a49 7368
01db9c85
LP
7369 r = blockdev_get_device_size(*fd, &current_size);
7370 if (r < 0)
7371 return log_error_errno(r, "Failed to determine size of block device %s: %m", node);
252d6267
LP
7372 } else {
7373 r = stat_verify_regular(&st);
7374 if (r < 0)
7375 return log_error_errno(r, "Specified path '%s' is not a regular file or loopback block device, cannot resize: %m", node);
7376
7377 assert(!backing_file);
7378 assert(!loop_device);
7379 current_size = st.st_size;
7380 }
7381
252d6267 7382 if (current_size >= arg_size) {
2b59bf51
ZJS
7383 log_info("File '%s' already is of requested size or larger, not growing. (%s >= %s)",
7384 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49
LP
7385 return 0;
7386 }
7387
252d6267
LP
7388 if (S_ISBLK(st.st_mode)) {
7389 assert(backing_file);
7390
7391 /* This is a loopback device. We can't really grow those directly, but we can grow the
7392 * backing file, hence let's do that. */
7393
7394 writable_fd = open(backing_file, O_WRONLY|O_CLOEXEC|O_NONBLOCK);
7395 if (writable_fd < 0)
7396 return log_error_errno(errno, "Failed to open backing file '%s': %m", backing_file);
7397
7398 if (fstat(writable_fd, &st) < 0)
7399 return log_error_errno(errno, "Failed to stat() backing file '%s': %m", backing_file);
7400
7401 r = stat_verify_regular(&st);
7402 if (r < 0)
7403 return log_error_errno(r, "Backing file '%s' of block device is not a regular file: %m", backing_file);
7404
7405 if ((uint64_t) st.st_size != current_size)
7406 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2b59bf51
ZJS
7407 "Size of backing file '%s' of loopback block device '%s' don't match, refusing.",
7408 node, backing_file);
252d6267
LP
7409 } else {
7410 assert(S_ISREG(st.st_mode));
7411 assert(!backing_file);
a26f4a49 7412
252d6267
LP
7413 /* The file descriptor is read-only. In order to grow the file we need to have a writable fd. We
7414 * reopen the file for that temporarily. We keep the writable fd only open for this operation though,
7415 * as fdisk can't accept it anyway. */
7416
7417 writable_fd = fd_reopen(*fd, O_WRONLY|O_CLOEXEC);
7418 if (writable_fd < 0)
7419 return log_error_errno(writable_fd, "Failed to reopen backing file '%s' writable: %m", node);
7420 }
a26f4a49
LP
7421
7422 if (!arg_discard) {
7423 if (fallocate(writable_fd, 0, 0, arg_size) < 0) {
7424 if (!ERRNO_IS_NOT_SUPPORTED(errno))
7425 return log_error_errno(errno, "Failed to grow '%s' from %s to %s by allocation: %m",
2b59bf51 7426 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49
LP
7427
7428 /* Fallback to truncation, if fallocate() is not supported. */
7429 log_debug("Backing file system does not support fallocate(), falling back to ftruncate().");
7430 } else {
252d6267 7431 if (current_size == 0) /* Likely regular file just created by us */
2b59bf51 7432 log_info("Allocated %s for '%s'.", FORMAT_BYTES(arg_size), node);
a26f4a49 7433 else
2b59bf51
ZJS
7434 log_info("File '%s' grown from %s to %s by allocation.",
7435 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49 7436
252d6267 7437 goto done;
a26f4a49
LP
7438 }
7439 }
7440
7441 if (ftruncate(writable_fd, arg_size) < 0)
7442 return log_error_errno(errno, "Failed to grow '%s' from %s to %s by truncation: %m",
2b59bf51 7443 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49 7444
252d6267 7445 if (current_size == 0) /* Likely regular file just created by us */
2b59bf51 7446 log_info("Sized '%s' to %s.", node, FORMAT_BYTES(arg_size));
252d6267 7447 else
2b59bf51
ZJS
7448 log_info("File '%s' grown from %s to %s by truncation.",
7449 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
252d6267
LP
7450
7451done:
81dde3d8 7452 r = resize_pt(writable_fd, sector_size);
f9b3afae
LP
7453 if (r < 0)
7454 return r;
7455
252d6267
LP
7456 if (loop_device) {
7457 r = loop_device_refresh_size(loop_device, UINT64_MAX, arg_size);
7458 if (r < 0)
7459 return log_error_errno(r, "Failed to update loop device size: %m");
7460 }
a26f4a49
LP
7461
7462 return 1;
7463}
7464
170c9823 7465static int determine_auto_size(Context *c) {
994b3031 7466 uint64_t sum;
170c9823 7467
ac33e147 7468 assert(c);
170c9823 7469
994b3031
LP
7470 sum = round_up_size(GPT_METADATA_SIZE, 4096);
7471
170c9823
LP
7472 LIST_FOREACH(partitions, p, c->partitions) {
7473 uint64_t m;
7474
7475 if (p->dropped)
7476 continue;
7477
994b3031 7478 m = partition_min_size_with_padding(c, p);
170c9823
LP
7479 if (m > UINT64_MAX - sum)
7480 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Image would grow too large, refusing.");
7481
7482 sum += m;
7483 }
7484
2b59bf51
ZJS
7485 if (c->total != UINT64_MAX)
7486 /* Image already allocated? Then show its size. */
7487 log_info("Automatically determined minimal disk image size as %s, current image size is %s.",
7488 FORMAT_BYTES(sum), FORMAT_BYTES(c->total));
7489 else
7490 /* If the image is being created right now, then it has no previous size, suppress any comment about it hence. */
7491 log_info("Automatically determined minimal disk image size as %s.",
7492 FORMAT_BYTES(sum));
170c9823
LP
7493
7494 arg_size = sum;
7495 return 0;
7496}
7497
e594a3b1 7498static int run(int argc, char *argv[]) {
252d6267 7499 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
a4b3e942 7500 _cleanup_(umount_and_freep) char *mounted_dir = NULL;
e594a3b1 7501 _cleanup_(context_freep) Context* context = NULL;
cc751c75 7502 bool node_is_our_loop = false;
e594a3b1
LP
7503 int r;
7504
7505 log_show_color(true);
7506 log_parse_environment();
7507 log_open();
7508
e594a3b1
LP
7509 r = parse_argv(argc, argv);
7510 if (r <= 0)
7511 return r;
7512
7513 r = parse_proc_cmdline_factory_reset();
7514 if (r < 0)
7515 return r;
7516
7517 r = parse_efi_variable_factory_reset();
7518 if (r < 0)
7519 return r;
7520
30f19400
LP
7521#if HAVE_LIBCRYPTSETUP
7522 cryptsetup_enable_logging(NULL);
7523#endif
7524
252d6267
LP
7525 if (arg_image) {
7526 assert(!arg_root);
7527
7528 /* Mount this strictly read-only: we shall modify the partition table, not the file
7529 * systems */
7530 r = mount_image_privately_interactively(
7531 arg_image,
84be0c71 7532 arg_image_policy,
252d6267
LP
7533 DISSECT_IMAGE_MOUNT_READ_ONLY |
7534 (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) */
7535 DISSECT_IMAGE_GPT_ONLY |
7536 DISSECT_IMAGE_RELAX_VAR_CHECK |
7537 DISSECT_IMAGE_USR_NO_ROOT |
7538 DISSECT_IMAGE_REQUIRE_ROOT,
7539 &mounted_dir,
a133d2c3 7540 /* ret_dir_fd= */ NULL,
e330f97a 7541 &loop_device);
252d6267
LP
7542 if (r < 0)
7543 return r;
7544
7545 arg_root = strdup(mounted_dir);
7546 if (!arg_root)
7547 return log_oom();
7548
7549 if (!arg_node) {
7550 arg_node = strdup(loop_device->node);
7551 if (!arg_node)
7552 return log_oom();
7553
3d62af7d 7554 /* Remember that the device we are about to manipulate is actually the one we
252d6267
LP
7555 * allocated here, and thus to increase its backing file we know what to do */
7556 node_is_our_loop = true;
7557 }
7558 }
7559
607343a1
LP
7560 if (!arg_copy_source && arg_root) {
7561 /* If no explicit copy source is specified, then use --root=/--image= */
7562 arg_copy_source = strdup(arg_root);
7563 if (!arg_copy_source)
7564 return log_oom();
7565 }
7566
e594a3b1
LP
7567 context = context_new(arg_seed);
7568 if (!context)
7569 return log_oom();
7570
1e46985a
DDM
7571 r = context_copy_from(context);
7572 if (r < 0)
7573 return r;
7574
a121b331
LP
7575 if (arg_make_ddi) {
7576 _cleanup_free_ char *d = NULL, *dp = NULL;
7577 assert(!arg_definitions);
7578
7579 d = strjoin(arg_make_ddi, ".repart.d/");
7580 if (!d)
7581 return log_oom();
7582
4ae703b2 7583 r = search_and_access(d, F_OK, NULL, CONF_PATHS_USR_STRV("systemd/repart/definitions"), &dp);
a121b331 7584 if (r < 0)
9f08d7f7 7585 return log_error_errno(r, "DDI type '%s' is not defined: %m", arg_make_ddi);
a121b331
LP
7586
7587 if (strv_consume(&arg_definitions, TAKE_PTR(dp)) < 0)
7588 return log_oom();
7589 } else
7590 strv_uniq(arg_definitions);
224c853f 7591
4b047310 7592 r = context_read_definitions(context);
e594a3b1
LP
7593 if (r < 0)
7594 return r;
7595
cc751c75 7596 r = find_root(context);
021b0ff4
DDM
7597 if (r == -ENODEV)
7598 return 76; /* Special return value which means "Root block device not found, so not doing
7599 * anything". This isn't really an error when called at boot. */
0ae5ffe0
YW
7600 if (r < 0)
7601 return r;
7602
a26f4a49 7603 if (arg_size != UINT64_MAX) {
252d6267 7604 r = resize_backing_fd(
cc751c75
DDM
7605 context->node,
7606 &context->backing_fd,
252d6267 7607 node_is_our_loop ? arg_image : NULL,
81dde3d8
LP
7608 node_is_our_loop ? loop_device : NULL,
7609 context->sector_size);
a26f4a49
LP
7610 if (r < 0)
7611 return r;
7612 }
7613
cc751c75 7614 r = context_load_partition_table(context);
e594a3b1
LP
7615 if (r == -EHWPOISON)
7616 return 77; /* Special return value which means "Not GPT, so not doing anything". This isn't
7617 * really an error when called at boot. */
7618 if (r < 0)
7619 return r;
cc751c75 7620 context->from_scratch = r > 0; /* Starting from scratch */
e594a3b1
LP
7621
7622 if (arg_can_factory_reset) {
7623 r = context_can_factory_reset(context);
7624 if (r < 0)
7625 return r;
7626 if (r == 0)
7627 return EXIT_FAILURE;
7628
7629 return 0;
7630 }
7631
cc751c75 7632 r = context_factory_reset(context);
e594a3b1
LP
7633 if (r < 0)
7634 return r;
7635 if (r > 0) {
7636 /* We actually did a factory reset! */
7637 r = remove_efi_variable_factory_reset();
7638 if (r < 0)
7639 return r;
7640
7641 /* Reload the reduced partition table */
7642 context_unload_partition_table(context);
cc751c75 7643 r = context_load_partition_table(context);
e594a3b1
LP
7644 if (r < 0)
7645 return r;
7646 }
7647
e594a3b1
LP
7648 r = context_read_seed(context, arg_root);
7649 if (r < 0)
7650 return r;
7651
8bbbdfd7
DDM
7652 /* Make sure each partition has a unique UUID and unique label */
7653 r = context_acquire_partition_uuids_and_labels(context);
7654 if (r < 0)
7655 return r;
7656
757bc2e4 7657 /* Open all files to copy blocks from now, since we want to take their size into consideration */
5c08da58
LP
7658 r = context_open_copy_block_paths(
7659 context,
7802194a 7660 loop_device ? loop_device->devno : /* if --image= is specified, only allow partitions on the loopback device */
5c08da58
LP
7661 arg_root && !arg_image ? 0 : /* if --root= is specified, don't accept any block device */
7662 (dev_t) -1); /* if neither is specified, make no restrictions */
757bc2e4
LP
7663 if (r < 0)
7664 return r;
7665
5eef7047
DDM
7666 r = context_minimize(context);
7667 if (r < 0)
7668 return r;
7669
170c9823
LP
7670 if (arg_size_auto) {
7671 r = determine_auto_size(context);
7672 if (r < 0)
7673 return r;
7674
7675 /* Flush out everything again, and let's grow the file first, then start fresh */
7676 context_unload_partition_table(context);
7677
ac33e147 7678 assert(arg_size != UINT64_MAX);
252d6267 7679 r = resize_backing_fd(
cc751c75
DDM
7680 context->node,
7681 &context->backing_fd,
252d6267 7682 node_is_our_loop ? arg_image : NULL,
81dde3d8
LP
7683 node_is_our_loop ? loop_device : NULL,
7684 context->sector_size);
170c9823
LP
7685 if (r < 0)
7686 return r;
7687
cc751c75 7688 r = context_load_partition_table(context);
170c9823
LP
7689 if (r < 0)
7690 return r;
7691 }
7692
e594a3b1
LP
7693 /* First try to fit new partitions in, dropping by priority until it fits */
7694 for (;;) {
14a4c4ed
LP
7695 uint64_t largest_free_area;
7696
7697 if (context_allocate_partitions(context, &largest_free_area))
e594a3b1
LP
7698 break; /* Success! */
7699
9ccceb9d 7700 if (!context_drop_or_foreignize_one_priority(context)) {
d17db7b2 7701 r = log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
14a4c4ed 7702 "Can't fit requested partitions into available free space (%s), refusing.",
2b59bf51 7703 FORMAT_BYTES(largest_free_area));
d17db7b2
LP
7704 determine_auto_size(context);
7705 return r;
7706 }
e594a3b1
LP
7707 }
7708
7709 /* Now assign free space according to the weight logic */
7710 r = context_grow_partitions(context);
7711 if (r < 0)
7712 return r;
7713
0b7f574f 7714 /* Now calculate where each new partition gets placed */
e594a3b1
LP
7715 context_place_partitions(context);
7716
cc751c75 7717 (void) context_dump(context, /*late=*/ false);
a26d463d 7718
cc751c75 7719 r = context_write_partition_table(context);
e594a3b1
LP
7720 if (r < 0)
7721 return r;
7722
4cee8333
DDM
7723 r = context_split(context);
7724 if (r < 0)
7725 return r;
7726
cc751c75 7727 (void) context_dump(context, /*late=*/ true);
b5b7879a 7728
db1d4e6b
DDM
7729 context->node = mfree(context->node);
7730
a2d7c42e
DDM
7731 LIST_FOREACH(partitions, p, context->partitions)
7732 p->split_path = mfree(p->split_path);
b5b7879a 7733
e594a3b1
LP
7734 return 0;
7735}
7736
7737DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);