]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/partition/repart.c
mkfs-util: Add support to populate vfat without mounting using mcopy
[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"
f4351959 23#include "chase-symlinks.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"
95bfd3cd 71#include "tmpfile-util.h"
e594a3b1 72#include "terminal-util.h"
02ef97cd 73#include "tpm-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
e594a3b1
LP
93/* Note: When growing and placing new partitions we always align to 4K sector size. It's how newer hard disks
94 * are designed, and if everything is aligned to that performance is best. And for older hard disks with 512B
95 * sector size devices were generally assumed to have an even number of sectors, hence at the worst we'll
96 * waste 3K per partition, which is probably fine. */
97
98static enum {
99 EMPTY_REFUSE, /* refuse empty disks, never create a partition table */
100 EMPTY_ALLOW, /* allow empty disks, create partition table if necessary */
101 EMPTY_REQUIRE, /* require an empty disk, create a partition table */
102 EMPTY_FORCE, /* make disk empty, erase everything, create a partition table always */
a26f4a49 103 EMPTY_CREATE, /* create disk as loopback file, create a partition table always */
e594a3b1
LP
104} arg_empty = EMPTY_REFUSE;
105
81d1098b
DDM
106typedef enum {
107 FILTER_PARTITIONS_NONE,
108 FILTER_PARTITIONS_EXCLUDE,
109 FILTER_PARTITIONS_INCLUDE,
110 _FILTER_PARTITIONS_MAX,
111 _FILTER_PARTITIONS_INVALID = -EINVAL,
112} FilterPartitionsType;
113
e594a3b1
LP
114static bool arg_dry_run = true;
115static const char *arg_node = NULL;
116static char *arg_root = NULL;
252d6267 117static char *arg_image = NULL;
224c853f 118static char **arg_definitions = NULL;
e594a3b1
LP
119static bool arg_discard = true;
120static bool arg_can_factory_reset = false;
121static int arg_factory_reset = -1;
122static sd_id128_t arg_seed = SD_ID128_NULL;
123static bool arg_randomize = false;
124static int arg_pretty = -1;
a26f4a49 125static uint64_t arg_size = UINT64_MAX;
170c9823 126static bool arg_size_auto = false;
6a01ea4a 127static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
896e678b
LP
128static PagerFlags arg_pager_flags = 0;
129static bool arg_legend = true;
b9df3536
LP
130static void *arg_key = NULL;
131static size_t arg_key_size = 0;
b456191d
DDM
132static EVP_PKEY *arg_private_key = NULL;
133static X509 *arg_certificate = NULL;
889914ef
LP
134static char *arg_tpm2_device = NULL;
135static uint32_t arg_tpm2_pcr_mask = UINT32_MAX;
02ef97cd
LP
136static char *arg_tpm2_public_key = NULL;
137static uint32_t arg_tpm2_public_key_pcr_mask = UINT32_MAX;
4cee8333 138static bool arg_split = false;
81d1098b
DDM
139static sd_id128_t *arg_filter_partitions = NULL;
140static size_t arg_filter_partitions_size = 0;
141static FilterPartitionsType arg_filter_partitions_type = FILTER_PARTITIONS_NONE;
e594a3b1
LP
142
143STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
252d6267 144STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
224c853f 145STATIC_DESTRUCTOR_REGISTER(arg_definitions, strv_freep);
b9df3536 146STATIC_DESTRUCTOR_REGISTER(arg_key, erase_and_freep);
b456191d
DDM
147STATIC_DESTRUCTOR_REGISTER(arg_private_key, EVP_PKEY_freep);
148STATIC_DESTRUCTOR_REGISTER(arg_certificate, X509_freep);
889914ef 149STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep);
02ef97cd 150STATIC_DESTRUCTOR_REGISTER(arg_tpm2_public_key, freep);
81d1098b 151STATIC_DESTRUCTOR_REGISTER(arg_filter_partitions, freep);
e594a3b1
LP
152
153typedef struct Partition Partition;
154typedef struct FreeArea FreeArea;
155typedef struct Context Context;
156
889914ef
LP
157typedef enum EncryptMode {
158 ENCRYPT_OFF,
159 ENCRYPT_KEY_FILE,
160 ENCRYPT_TPM2,
161 ENCRYPT_KEY_FILE_TPM2,
162 _ENCRYPT_MODE_MAX,
2d93c20e 163 _ENCRYPT_MODE_INVALID = -EINVAL,
889914ef
LP
164} EncryptMode;
165
b5b7879a
DDM
166typedef enum VerityMode {
167 VERITY_OFF,
168 VERITY_DATA,
169 VERITY_HASH,
b456191d 170 VERITY_SIG,
b5b7879a
DDM
171 _VERITY_MODE_MAX,
172 _VERITY_MODE_INVALID = -EINVAL,
173} VerityMode;
174
e594a3b1
LP
175struct Partition {
176 char *definition_path;
39fc0174 177 char **drop_in_files;
e594a3b1 178
22e932f4 179 GptPartitionType type;
e594a3b1 180 sd_id128_t current_uuid, new_uuid;
11749b61 181 bool new_uuid_is_set;
e594a3b1 182 char *current_label, *new_label;
8bbbdfd7 183 sd_id128_t fs_uuid;
e594a3b1
LP
184
185 bool dropped;
186 bool factory_reset;
187 int32_t priority;
188
189 uint32_t weight, padding_weight;
190
191 uint64_t current_size, new_size;
192 uint64_t size_min, size_max;
193
194 uint64_t current_padding, new_padding;
195 uint64_t padding_min, padding_max;
196
197 uint64_t partno;
198 uint64_t offset;
199
200 struct fdisk_partition *current_partition;
201 struct fdisk_partition *new_partition;
202 FreeArea *padding_area;
203 FreeArea *allocated_to_area;
204
757bc2e4 205 char *copy_blocks_path;
5c08da58 206 bool copy_blocks_auto;
585c5c75 207 const char *copy_blocks_root;
757bc2e4
LP
208 int copy_blocks_fd;
209 uint64_t copy_blocks_size;
210
53171c04 211 char *format;
8a794850 212 char **copy_files;
d83d8048 213 char **make_directories;
889914ef 214 EncryptMode encrypt;
b5b7879a
DDM
215 VerityMode verity;
216 char *verity_match_key;
c4a87b76 217 bool minimize;
53171c04 218
e73309c5 219 uint64_t gpt_flags;
ff0771bf 220 int no_auto;
e73309c5 221 int read_only;
1c41c1dc 222 int growfs;
e73309c5 223
b5b7879a
DDM
224 uint8_t *roothash;
225 size_t roothash_size;
226
4cee8333
DDM
227 char *split_name_format;
228 char *split_name_resolved;
229
b5b7879a
DDM
230 Partition *siblings[_VERITY_MODE_MAX];
231
e594a3b1
LP
232 LIST_FIELDS(Partition, partitions);
233};
234
235#define PARTITION_IS_FOREIGN(p) (!(p)->definition_path)
236#define PARTITION_EXISTS(p) (!!(p)->current_partition)
237
238struct FreeArea {
239 Partition *after;
240 uint64_t size;
241 uint64_t allocated;
242};
243
244struct Context {
245 LIST_HEAD(Partition, partitions);
246 size_t n_partitions;
247
248 FreeArea **free_areas;
319a4f4b 249 size_t n_free_areas;
e594a3b1
LP
250
251 uint64_t start, end, total;
252
253 struct fdisk_context *fdisk_context;
994b3031
LP
254 uint64_t sector_size;
255 uint64_t grain_size;
e594a3b1
LP
256
257 sd_id128_t seed;
258};
259
889914ef
LP
260static const char *encrypt_mode_table[_ENCRYPT_MODE_MAX] = {
261 [ENCRYPT_OFF] = "off",
262 [ENCRYPT_KEY_FILE] = "key-file",
263 [ENCRYPT_TPM2] = "tpm2",
264 [ENCRYPT_KEY_FILE_TPM2] = "key-file+tpm2",
265};
266
b5b7879a
DDM
267static const char *verity_mode_table[_VERITY_MODE_MAX] = {
268 [VERITY_OFF] = "off",
269 [VERITY_DATA] = "data",
270 [VERITY_HASH] = "hash",
b456191d 271 [VERITY_SIG] = "signature",
b5b7879a
DDM
272};
273
2709d029 274#if HAVE_LIBCRYPTSETUP
889914ef 275DEFINE_PRIVATE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(encrypt_mode, EncryptMode, ENCRYPT_KEY_FILE);
2709d029
MH
276#else
277DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(encrypt_mode, EncryptMode, ENCRYPT_KEY_FILE);
278#endif
279
86bebe38 280DEFINE_PRIVATE_STRING_TABLE_LOOKUP(verity_mode, VerityMode);
889914ef 281
e594a3b1
LP
282static uint64_t round_down_size(uint64_t v, uint64_t p) {
283 return (v / p) * p;
284}
285
286static uint64_t round_up_size(uint64_t v, uint64_t p) {
287
288 v = DIV_ROUND_UP(v, p);
289
290 if (v > UINT64_MAX / p)
291 return UINT64_MAX; /* overflow */
292
293 return v * p;
294}
295
296static Partition *partition_new(void) {
297 Partition *p;
298
299 p = new(Partition, 1);
300 if (!p)
301 return NULL;
302
303 *p = (Partition) {
304 .weight = 1000,
305 .padding_weight = 0,
306 .current_size = UINT64_MAX,
307 .new_size = UINT64_MAX,
308 .size_min = UINT64_MAX,
309 .size_max = UINT64_MAX,
310 .current_padding = UINT64_MAX,
311 .new_padding = UINT64_MAX,
312 .padding_min = UINT64_MAX,
313 .padding_max = UINT64_MAX,
314 .partno = UINT64_MAX,
315 .offset = UINT64_MAX,
757bc2e4
LP
316 .copy_blocks_fd = -1,
317 .copy_blocks_size = UINT64_MAX,
ff0771bf 318 .no_auto = -1,
e73309c5 319 .read_only = -1,
1c41c1dc 320 .growfs = -1,
e594a3b1
LP
321 };
322
323 return p;
324}
325
326static Partition* partition_free(Partition *p) {
327 if (!p)
328 return NULL;
329
330 free(p->current_label);
331 free(p->new_label);
332 free(p->definition_path);
39fc0174 333 strv_free(p->drop_in_files);
e594a3b1
LP
334
335 if (p->current_partition)
336 fdisk_unref_partition(p->current_partition);
337 if (p->new_partition)
338 fdisk_unref_partition(p->new_partition);
339
757bc2e4
LP
340 free(p->copy_blocks_path);
341 safe_close(p->copy_blocks_fd);
342
53171c04 343 free(p->format);
8a794850 344 strv_free(p->copy_files);
d83d8048 345 strv_free(p->make_directories);
b5b7879a
DDM
346 free(p->verity_match_key);
347
348 free(p->roothash);
53171c04 349
4cee8333
DDM
350 free(p->split_name_format);
351 free(p->split_name_resolved);
352
e594a3b1
LP
353 return mfree(p);
354}
355
9ccceb9d
YW
356static void partition_foreignize(Partition *p) {
357 assert(p);
358 assert(PARTITION_EXISTS(p));
359
360 /* Reset several parameters set through definition file to make the partition foreign. */
361
9ccceb9d
YW
362 p->definition_path = mfree(p->definition_path);
363 p->drop_in_files = strv_free(p->drop_in_files);
364
365 p->copy_blocks_path = mfree(p->copy_blocks_path);
366 p->copy_blocks_fd = safe_close(p->copy_blocks_fd);
585c5c75 367 p->copy_blocks_root = NULL;
9ccceb9d
YW
368
369 p->format = mfree(p->format);
370 p->copy_files = strv_free(p->copy_files);
371 p->make_directories = strv_free(p->make_directories);
372 p->verity_match_key = mfree(p->verity_match_key);
373
9ccceb9d
YW
374 p->priority = 0;
375 p->weight = 1000;
376 p->padding_weight = 0;
377 p->size_min = UINT64_MAX;
378 p->size_max = UINT64_MAX;
379 p->padding_min = UINT64_MAX;
380 p->padding_max = UINT64_MAX;
381 p->no_auto = -1;
382 p->read_only = -1;
383 p->growfs = -1;
384 p->verity = VERITY_OFF;
385}
386
81d1098b
DDM
387static bool partition_skip(const Partition *p) {
388 assert(p);
389
390 if (arg_filter_partitions_type == FILTER_PARTITIONS_NONE)
391 return false;
392
393 for (size_t i = 0; i < arg_filter_partitions_size; i++)
394 if (sd_id128_equal(p->type.uuid, arg_filter_partitions[i]))
395 return arg_filter_partitions_type == FILTER_PARTITIONS_EXCLUDE;
396
397 return arg_filter_partitions_type == FILTER_PARTITIONS_INCLUDE;
398}
399
e594a3b1
LP
400static Partition* partition_unlink_and_free(Context *context, Partition *p) {
401 if (!p)
402 return NULL;
403
404 LIST_REMOVE(partitions, context->partitions, p);
405
406 assert(context->n_partitions > 0);
407 context->n_partitions--;
408
409 return partition_free(p);
410}
411
412DEFINE_TRIVIAL_CLEANUP_FUNC(Partition*, partition_free);
413
414static Context *context_new(sd_id128_t seed) {
415 Context *context;
416
417 context = new(Context, 1);
418 if (!context)
419 return NULL;
420
421 *context = (Context) {
422 .start = UINT64_MAX,
423 .end = UINT64_MAX,
424 .total = UINT64_MAX,
425 .seed = seed,
426 };
427
428 return context;
429}
430
431static void context_free_free_areas(Context *context) {
432 assert(context);
433
434 for (size_t i = 0; i < context->n_free_areas; i++)
435 free(context->free_areas[i]);
436
437 context->free_areas = mfree(context->free_areas);
438 context->n_free_areas = 0;
e594a3b1
LP
439}
440
441static Context *context_free(Context *context) {
442 if (!context)
443 return NULL;
444
445 while (context->partitions)
446 partition_unlink_and_free(context, context->partitions);
447 assert(context->n_partitions == 0);
448
449 context_free_free_areas(context);
450
451 if (context->fdisk_context)
452 fdisk_unref_context(context->fdisk_context);
453
454 return mfree(context);
455}
456
457DEFINE_TRIVIAL_CLEANUP_FUNC(Context*, context_free);
458
459static int context_add_free_area(
460 Context *context,
461 uint64_t size,
462 Partition *after) {
463
464 FreeArea *a;
465
466 assert(context);
467 assert(!after || !after->padding_area);
468
319a4f4b 469 if (!GREEDY_REALLOC(context->free_areas, context->n_free_areas + 1))
e594a3b1
LP
470 return -ENOMEM;
471
472 a = new(FreeArea, 1);
473 if (!a)
474 return -ENOMEM;
475
476 *a = (FreeArea) {
477 .size = size,
478 .after = after,
479 };
480
481 context->free_areas[context->n_free_areas++] = a;
482
483 if (after)
484 after->padding_area = a;
485
486 return 0;
487}
488
9ccceb9d
YW
489static void partition_drop_or_foreignize(Partition *p) {
490 if (!p || p->dropped || PARTITION_IS_FOREIGN(p))
491 return;
492
493 if (PARTITION_EXISTS(p)) {
494 log_info("Can't grow existing partition %s of priority %" PRIi32 ", ignoring.",
495 strna(p->current_label ?: p->new_label), p->priority);
496
497 /* Handle the partition as foreign. Do not set dropped flag. */
498 partition_foreignize(p);
499 } else {
500 log_info("Can't fit partition %s of priority %" PRIi32 ", dropping.",
501 p->definition_path, p->priority);
502
503 p->dropped = true;
504 p->allocated_to_area = NULL;
505 }
506}
507
508static bool context_drop_or_foreignize_one_priority(Context *context) {
e594a3b1 509 int32_t priority = 0;
e594a3b1
LP
510
511 LIST_FOREACH(partitions, p, context->partitions) {
512 if (p->dropped)
513 continue;
e594a3b1 514
9ccceb9d 515 priority = MAX(priority, p->priority);
e594a3b1
LP
516 }
517
518 /* Refuse to drop partitions with 0 or negative priorities or partitions of priorities that have at
519 * least one existing priority */
9ccceb9d 520 if (priority <= 0)
e594a3b1
LP
521 return false;
522
523 LIST_FOREACH(partitions, p, context->partitions) {
524 if (p->priority < priority)
525 continue;
526
9ccceb9d 527 partition_drop_or_foreignize(p);
b5b7879a
DDM
528
529 /* We ensure that all verity sibling partitions have the same priority, so it's safe
530 * to drop all siblings here as well. */
531
9ccceb9d
YW
532 for (VerityMode mode = VERITY_OFF + 1; mode < _VERITY_MODE_MAX; mode++)
533 partition_drop_or_foreignize(p->siblings[mode]);
e594a3b1
LP
534 }
535
536 return true;
537}
538
a80701e6 539static uint64_t partition_min_size(const Context *context, const Partition *p) {
e594a3b1
LP
540 uint64_t sz;
541
994b3031
LP
542 assert(context);
543 assert(p);
544
e594a3b1
LP
545 /* Calculate the disk space we really need at minimum for this partition. If the partition already
546 * exists the current size is what we really need. If it doesn't exist yet refuse to allocate less
fb08381c
LP
547 * than 4K.
548 *
549 * DEFAULT_MIN_SIZE is the default SizeMin= we configure if nothing else is specified. */
e594a3b1
LP
550
551 if (PARTITION_IS_FOREIGN(p)) {
552 /* Don't allow changing size of partitions not managed by us */
553 assert(p->current_size != UINT64_MAX);
554 return p->current_size;
555 }
556
b456191d
DDM
557 if (p->verity == VERITY_SIG)
558 return VERITY_SIG_SIZE;
559
fb08381c 560 sz = p->current_size != UINT64_MAX ? p->current_size : HARD_MIN_SIZE;
757bc2e4 561
170c9823
LP
562 if (!PARTITION_EXISTS(p)) {
563 uint64_t d = 0;
564
889914ef 565 if (p->encrypt != ENCRYPT_OFF)
994b3031 566 d += round_up_size(LUKS2_METADATA_SIZE, context->grain_size);
170c9823
LP
567
568 if (p->copy_blocks_size != UINT64_MAX)
994b3031 569 d += round_up_size(p->copy_blocks_size, context->grain_size);
889914ef 570 else if (p->format || p->encrypt != ENCRYPT_OFF) {
170c9823
LP
571 uint64_t f;
572
573 /* If we shall synthesize a file system, take minimal fs size into account (assumed to be 4K if not known) */
994b3031
LP
574 f = p->format ? round_up_size(minimal_size_by_fs_name(p->format), context->grain_size) : UINT64_MAX;
575 d += f == UINT64_MAX ? context->grain_size : f;
170c9823
LP
576 }
577
578 if (d > sz)
579 sz = d;
580 }
757bc2e4 581
994b3031 582 return MAX(round_up_size(p->size_min != UINT64_MAX ? p->size_min : DEFAULT_MIN_SIZE, context->grain_size), sz);
e594a3b1
LP
583}
584
994b3031
LP
585static uint64_t partition_max_size(const Context *context, const Partition *p) {
586 uint64_t sm;
587
e594a3b1
LP
588 /* Calculate how large the partition may become at max. This is generally the configured maximum
589 * size, except when it already exists and is larger than that. In that case it's the existing size,
590 * since we never want to shrink partitions. */
591
994b3031
LP
592 assert(context);
593 assert(p);
594
e594a3b1
LP
595 if (PARTITION_IS_FOREIGN(p)) {
596 /* Don't allow changing size of partitions not managed by us */
597 assert(p->current_size != UINT64_MAX);
598 return p->current_size;
599 }
600
b456191d
DDM
601 if (p->verity == VERITY_SIG)
602 return VERITY_SIG_SIZE;
603
822d9b9a
YW
604 if (p->size_max == UINT64_MAX)
605 return UINT64_MAX;
606
994b3031
LP
607 sm = round_down_size(p->size_max, context->grain_size);
608
e594a3b1 609 if (p->current_size != UINT64_MAX)
b0fbf90b 610 sm = MAX(p->current_size, sm);
e594a3b1 611
b0fbf90b 612 return MAX(partition_min_size(context, p), sm);
e594a3b1
LP
613}
614
a801bb01
YW
615static uint64_t partition_min_padding(const Partition *p) {
616 assert(p);
617 return p->padding_min != UINT64_MAX ? p->padding_min : 0;
618}
619
620static uint64_t partition_max_padding(const Partition *p) {
621 assert(p);
622 return p->padding_max;
623}
624
994b3031 625static uint64_t partition_min_size_with_padding(Context *context, const Partition *p) {
e594a3b1
LP
626 uint64_t sz;
627
628 /* Calculate the disk space we need for this partition plus any free space coming after it. This
629 * takes user configured padding into account as well as any additional whitespace needed to align
630 * the next partition to 4K again. */
631
994b3031
LP
632 assert(context);
633 assert(p);
634
a801bb01 635 sz = partition_min_size(context, p) + partition_min_padding(p);
e594a3b1
LP
636
637 if (PARTITION_EXISTS(p)) {
638 /* If the partition wasn't aligned, add extra space so that any we might add will be aligned */
639 assert(p->offset != UINT64_MAX);
994b3031 640 return round_up_size(p->offset + sz, context->grain_size) - p->offset;
e594a3b1
LP
641 }
642
643 /* If this is a new partition we'll place it aligned, hence we just need to round up the required size here */
994b3031 644 return round_up_size(sz, context->grain_size);
e594a3b1
LP
645}
646
647static uint64_t free_area_available(const FreeArea *a) {
648 assert(a);
649
650 /* Determines how much of this free area is not allocated yet */
651
652 assert(a->size >= a->allocated);
653 return a->size - a->allocated;
654}
655
58b06ac1 656static uint64_t free_area_current_end(Context *context, const FreeArea *a) {
994b3031
LP
657 assert(context);
658 assert(a);
659
58b06ac1
YW
660 if (!a->after)
661 return free_area_available(a);
e594a3b1 662
58b06ac1
YW
663 assert(a->after->offset != UINT64_MAX);
664 assert(a->after->current_size != UINT64_MAX);
e594a3b1 665
58b06ac1
YW
666 /* Calculate where the free area ends, based on the offset of the partition preceding it. */
667 return round_up_size(a->after->offset + a->after->current_size, context->grain_size) + free_area_available(a);
668}
e594a3b1 669
58b06ac1
YW
670static uint64_t free_area_min_end(Context *context, const FreeArea *a) {
671 assert(context);
672 assert(a);
e594a3b1 673
58b06ac1
YW
674 if (!a->after)
675 return 0;
e594a3b1 676
58b06ac1
YW
677 assert(a->after->offset != UINT64_MAX);
678 assert(a->after->current_size != UINT64_MAX);
1052a114 679
58b06ac1
YW
680 /* Calculate where the partition would end when we give it as much as it needs. */
681 return round_up_size(a->after->offset + partition_min_size_with_padding(context, a->after), context->grain_size);
682}
683
684static uint64_t free_area_available_for_new_partitions(Context *context, const FreeArea *a) {
685 assert(context);
686 assert(a);
687
688 /* Similar to free_area_available(), but takes into account that the required size and padding of the
689 * preceding partition is honoured. */
e594a3b1 690
58b06ac1 691 return LESS_BY(free_area_current_end(context, a), free_area_min_end(context, a));
e594a3b1
LP
692}
693
994b3031
LP
694static int free_area_compare(FreeArea *const *a, FreeArea *const*b, Context *context) {
695 assert(context);
696
697 return CMP(free_area_available_for_new_partitions(context, *a),
698 free_area_available_for_new_partitions(context, *b));
e594a3b1
LP
699}
700
994b3031
LP
701static uint64_t charge_size(Context *context, uint64_t total, uint64_t amount) {
702 assert(context);
e594a3b1 703 /* Subtract the specified amount from total, rounding up to multiple of 4K if there's room */
184cf99a 704 assert(amount <= total);
994b3031 705 return LESS_BY(total, round_up_size(amount, context->grain_size));
e594a3b1
LP
706}
707
708static uint64_t charge_weight(uint64_t total, uint64_t amount) {
709 assert(amount <= total);
710 return total - amount;
711}
712
14a4c4ed 713static bool context_allocate_partitions(Context *context, uint64_t *ret_largest_free_area) {
e594a3b1
LP
714 assert(context);
715
f39cf264
YW
716 /* This may be called multiple times. Reset previous assignments. */
717 for (size_t i = 0; i < context->n_free_areas; i++)
718 context->free_areas[i]->allocated = 0;
719
14a4c4ed 720 /* Sort free areas by size, putting smallest first */
994b3031 721 typesafe_qsort_r(context->free_areas, context->n_free_areas, free_area_compare, context);
e594a3b1 722
14a4c4ed
LP
723 /* In any case return size of the largest free area (i.e. not the size of all free areas
724 * combined!) */
725 if (ret_largest_free_area)
726 *ret_largest_free_area =
727 context->n_free_areas == 0 ? 0 :
994b3031 728 free_area_available_for_new_partitions(context, context->free_areas[context->n_free_areas-1]);
14a4c4ed 729
cdbcc339
YW
730 /* Check that each existing partition can fit its area. */
731 for (size_t i = 0; i < context->n_free_areas; i++)
732 if (free_area_current_end(context, context->free_areas[i]) <
733 free_area_min_end(context, context->free_areas[i]))
734 return false;
735
14a4c4ed 736 /* A simple first-fit algorithm. We return true if we can fit the partitions in, otherwise false. */
e594a3b1
LP
737 LIST_FOREACH(partitions, p, context->partitions) {
738 bool fits = false;
739 uint64_t required;
740 FreeArea *a = NULL;
741
742 /* Skip partitions we already dropped or that already exist */
743 if (p->dropped || PARTITION_EXISTS(p))
744 continue;
745
e594a3b1 746 /* How much do we need to fit? */
994b3031
LP
747 required = partition_min_size_with_padding(context, p);
748 assert(required % context->grain_size == 0);
e594a3b1
LP
749
750 for (size_t i = 0; i < context->n_free_areas; i++) {
751 a = context->free_areas[i];
752
994b3031 753 if (free_area_available_for_new_partitions(context, a) >= required) {
e594a3b1
LP
754 fits = true;
755 break;
756 }
757 }
758
759 if (!fits)
760 return false; /* 😢 Oh no! We can't fit this partition into any free area! */
761
762 /* Assign the partition to this free area */
763 p->allocated_to_area = a;
764
765 /* Budget the minimal partition size */
766 a->allocated += required;
767 }
768
769 return true;
770}
771
772static int context_sum_weights(Context *context, FreeArea *a, uint64_t *ret) {
773 uint64_t weight_sum = 0;
e594a3b1
LP
774
775 assert(context);
776 assert(a);
777 assert(ret);
778
779 /* Determine the sum of the weights of all partitions placed in or before the specified free area */
780
781 LIST_FOREACH(partitions, p, context->partitions) {
782 if (p->padding_area != a && p->allocated_to_area != a)
783 continue;
784
785 if (p->weight > UINT64_MAX - weight_sum)
786 goto overflow_sum;
787 weight_sum += p->weight;
788
789 if (p->padding_weight > UINT64_MAX - weight_sum)
790 goto overflow_sum;
791 weight_sum += p->padding_weight;
792 }
793
794 *ret = weight_sum;
795 return 0;
796
797overflow_sum:
798 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Combined weight of partition exceeds unsigned 64bit range, refusing.");
799}
800
0245e15a 801static uint64_t scale_by_weight(uint64_t value, uint64_t weight, uint64_t weight_sum) {
e594a3b1 802 assert(weight_sum >= weight);
e594a3b1 803
0245e15a
YW
804 for (;;) {
805 if (weight == 0)
806 return 0;
807 if (weight == weight_sum)
808 return value;
809 if (value <= UINT64_MAX / weight)
810 return value * weight / weight_sum;
811
812 /* Rescale weight and weight_sum to make not the calculation overflow. To satisfy the
813 * following conditions, 'weight_sum' is rounded up but 'weight' is rounded down:
814 * - the sum of scale_by_weight() for all weights must not be larger than the input value,
815 * - scale_by_weight() must not be larger than the ideal value (i.e. calculated with uint128_t). */
816 weight_sum = DIV_ROUND_UP(weight_sum, 2);
817 weight /= 2;
e594a3b1 818 }
e594a3b1
LP
819}
820
821typedef enum GrowPartitionPhase {
bf99aed6
YW
822 /* The zeroth phase: do not touch foreign partitions (i.e. those we don't manage). */
823 PHASE_FOREIGN,
824
e594a3b1
LP
825 /* The first phase: we charge partitions which need more (according to constraints) than their weight-based share. */
826 PHASE_OVERCHARGE,
827
828 /* The second phase: we charge partitions which need less (according to constraints) than their weight-based share. */
829 PHASE_UNDERCHARGE,
830
831 /* The third phase: we distribute what remains among the remaining partitions, according to the weights */
832 PHASE_DISTRIBUTE,
ae0613c6
LP
833
834 _GROW_PARTITION_PHASE_MAX,
e594a3b1
LP
835} GrowPartitionPhase;
836
0245e15a 837static bool context_grow_partitions_phase(
e594a3b1
LP
838 Context *context,
839 FreeArea *a,
840 GrowPartitionPhase phase,
841 uint64_t *span,
842 uint64_t *weight_sum) {
843
2a503ad2
YW
844 bool try_again = false;
845
e594a3b1
LP
846 assert(context);
847 assert(a);
0245e15a
YW
848 assert(span);
849 assert(weight_sum);
e594a3b1
LP
850
851 /* Now let's look at the intended weights and adjust them taking the minimum space assignments into
852 * account. i.e. if a partition has a small weight but a high minimum space value set it should not
853 * get any additional room from the left-overs. Similar, if two partitions have the same weight they
854 * should get the same space if possible, even if one has a smaller minimum size than the other. */
855 LIST_FOREACH(partitions, p, context->partitions) {
856
857 /* Look only at partitions associated with this free area, i.e. immediately
162392b7 858 * preceding it, or allocated into it */
e594a3b1
LP
859 if (p->allocated_to_area != a && p->padding_area != a)
860 continue;
861
862 if (p->new_size == UINT64_MAX) {
e594a3b1 863 uint64_t share, rsz, xsz;
2a503ad2 864 bool charge = false;
e594a3b1
LP
865
866 /* Calculate how much this space this partition needs if everyone would get
867 * the weight based share */
0245e15a 868 share = scale_by_weight(*span, p->weight, *weight_sum);
e594a3b1 869
994b3031
LP
870 rsz = partition_min_size(context, p);
871 xsz = partition_max_size(context, p);
e594a3b1 872
bf99aed6
YW
873 if (phase == PHASE_FOREIGN && PARTITION_IS_FOREIGN(p)) {
874 /* Never change of foreign partitions (i.e. those we don't manage) */
875
876 p->new_size = p->current_size;
877 charge = true;
878
879 } else if (phase == PHASE_OVERCHARGE && rsz > share) {
e594a3b1
LP
880 /* This partition needs more than its calculated share. Let's assign
881 * it that, and take this partition out of all calculations and start
882 * again. */
883
884 p->new_size = rsz;
885 charge = try_again = true;
886
822d9b9a 887 } else if (phase == PHASE_UNDERCHARGE && xsz < share) {
e594a3b1
LP
888 /* This partition accepts less than its calculated
889 * share. Let's assign it that, and take this partition out
890 * of all calculations and start again. */
891
892 p->new_size = xsz;
893 charge = try_again = true;
894
895 } else if (phase == PHASE_DISTRIBUTE) {
896 /* This partition can accept its calculated share. Let's
897 * assign it. There's no need to restart things here since
898 * assigning this shouldn't impact the shares of the other
899 * partitions. */
900
d7c46b5e
YW
901 assert(share >= rsz);
902 p->new_size = CLAMP(round_down_size(share, context->grain_size), rsz, xsz);
e594a3b1
LP
903 charge = true;
904 }
905
906 if (charge) {
994b3031 907 *span = charge_size(context, *span, p->new_size);
e594a3b1
LP
908 *weight_sum = charge_weight(*weight_sum, p->weight);
909 }
e594a3b1
LP
910 }
911
912 if (p->new_padding == UINT64_MAX) {
a801bb01 913 uint64_t share, rsz, xsz;
2a503ad2 914 bool charge = false;
e594a3b1 915
0245e15a 916 share = scale_by_weight(*span, p->padding_weight, *weight_sum);
e594a3b1 917
a801bb01
YW
918 rsz = partition_min_padding(p);
919 xsz = partition_max_padding(p);
920
921 if (phase == PHASE_OVERCHARGE && rsz > share) {
922 p->new_padding = rsz;
e594a3b1 923 charge = try_again = true;
a801bb01
YW
924 } else if (phase == PHASE_UNDERCHARGE && xsz < share) {
925 p->new_padding = xsz;
e594a3b1
LP
926 charge = try_again = true;
927 } else if (phase == PHASE_DISTRIBUTE) {
d7c46b5e
YW
928 assert(share >= rsz);
929 p->new_padding = CLAMP(round_down_size(share, context->grain_size), rsz, xsz);
e594a3b1
LP
930 charge = true;
931 }
932
933 if (charge) {
994b3031 934 *span = charge_size(context, *span, p->new_padding);
e594a3b1
LP
935 *weight_sum = charge_weight(*weight_sum, p->padding_weight);
936 }
e594a3b1
LP
937 }
938 }
939
2a503ad2 940 return !try_again;
e594a3b1
LP
941}
942
19903a43
YW
943static void context_grow_partition_one(Context *context, FreeArea *a, Partition *p, uint64_t *span) {
944 uint64_t m;
945
946 assert(context);
947 assert(a);
948 assert(p);
949 assert(span);
950
951 if (*span == 0)
952 return;
953
954 if (p->allocated_to_area != a)
955 return;
956
957 if (PARTITION_IS_FOREIGN(p))
958 return;
959
960 assert(p->new_size != UINT64_MAX);
961
962 /* Calculate new size and align. */
963 m = round_down_size(p->new_size + *span, context->grain_size);
964 /* But ensure this doesn't shrink the size. */
965 m = MAX(m, p->new_size);
966 /* And ensure this doesn't exceed the maximum size. */
967 m = MIN(m, partition_max_size(context, p));
968
969 assert(m >= p->new_size);
970
971 *span = charge_size(context, *span, m - p->new_size);
972 p->new_size = m;
973}
974
e594a3b1
LP
975static int context_grow_partitions_on_free_area(Context *context, FreeArea *a) {
976 uint64_t weight_sum = 0, span;
977 int r;
978
979 assert(context);
980 assert(a);
981
982 r = context_sum_weights(context, a, &weight_sum);
983 if (r < 0)
984 return r;
985
986 /* Let's calculate the total area covered by this free area and the partition before it */
987 span = a->size;
988 if (a->after) {
989 assert(a->after->offset != UINT64_MAX);
990 assert(a->after->current_size != UINT64_MAX);
991
994b3031 992 span += round_up_size(a->after->offset + a->after->current_size, context->grain_size) - a->after->offset;
e594a3b1
LP
993 }
994
0245e15a
YW
995 for (GrowPartitionPhase phase = 0; phase < _GROW_PARTITION_PHASE_MAX;)
996 if (context_grow_partitions_phase(context, a, phase, &span, &weight_sum))
997 phase++; /* go to the next phase */
e594a3b1 998
162392b7 999 /* We still have space left over? Donate to preceding partition if we have one */
19903a43
YW
1000 if (span > 0 && a->after)
1001 context_grow_partition_one(context, a, a->after, &span);
e594a3b1 1002
162392b7 1003 /* What? Even still some space left (maybe because there was no preceding partition, or it had a
e594a3b1 1004 * size limit), then let's donate it to whoever wants it. */
03677889 1005 if (span > 0)
e594a3b1 1006 LIST_FOREACH(partitions, p, context->partitions) {
19903a43 1007 context_grow_partition_one(context, a, p, &span);
e594a3b1
LP
1008 if (span == 0)
1009 break;
1010 }
e594a3b1 1011
162392b7 1012 /* Yuck, still no one? Then make it padding */
e594a3b1
LP
1013 if (span > 0 && a->after) {
1014 assert(a->after->new_padding != UINT64_MAX);
1015 a->after->new_padding += span;
1016 }
1017
1018 return 0;
1019}
1020
1021static int context_grow_partitions(Context *context) {
e594a3b1
LP
1022 int r;
1023
1024 assert(context);
1025
1026 for (size_t i = 0; i < context->n_free_areas; i++) {
1027 r = context_grow_partitions_on_free_area(context, context->free_areas[i]);
1028 if (r < 0)
1029 return r;
1030 }
1031
1032 /* All existing partitions that have no free space after them can't change size */
1033 LIST_FOREACH(partitions, p, context->partitions) {
1034 if (p->dropped)
1035 continue;
1036
1037 if (!PARTITION_EXISTS(p) || p->padding_area) {
1038 /* The algorithm above must have initialized this already */
1039 assert(p->new_size != UINT64_MAX);
1040 continue;
1041 }
1042
1043 assert(p->new_size == UINT64_MAX);
1044 p->new_size = p->current_size;
1045
1046 assert(p->new_padding == UINT64_MAX);
1047 p->new_padding = p->current_padding;
1048 }
1049
1050 return 0;
1051}
1052
00428745 1053static uint64_t find_first_unused_partno(Context *context) {
e594a3b1 1054 uint64_t partno = 0;
e594a3b1
LP
1055
1056 assert(context);
1057
00428745
DDM
1058 for (bool changed = true; changed;) {
1059 changed = false;
e594a3b1 1060
00428745
DDM
1061 LIST_FOREACH(partitions, p, context->partitions) {
1062 if (p->partno != UINT64_MAX && p->partno == partno) {
1063 partno++;
1064 changed = true;
1065 break;
1066 }
1067 }
e594a3b1
LP
1068 }
1069
00428745
DDM
1070 return partno;
1071}
1072
1073static void context_place_partitions(Context *context) {
1074
1075 assert(context);
1076
e594a3b1
LP
1077 for (size_t i = 0; i < context->n_free_areas; i++) {
1078 FreeArea *a = context->free_areas[i];
2ea7eb00
FS
1079 _unused_ uint64_t left;
1080 uint64_t start;
e594a3b1
LP
1081
1082 if (a->after) {
1083 assert(a->after->offset != UINT64_MAX);
1084 assert(a->after->new_size != UINT64_MAX);
1085 assert(a->after->new_padding != UINT64_MAX);
1086
1087 start = a->after->offset + a->after->new_size + a->after->new_padding;
1088 } else
1089 start = context->start;
1090
994b3031 1091 start = round_up_size(start, context->grain_size);
e594a3b1
LP
1092 left = a->size;
1093
1094 LIST_FOREACH(partitions, p, context->partitions) {
1095 if (p->allocated_to_area != a)
1096 continue;
1097
1098 p->offset = start;
00428745 1099 p->partno = find_first_unused_partno(context);
e594a3b1
LP
1100
1101 assert(left >= p->new_size);
1102 start += p->new_size;
1103 left -= p->new_size;
1104
1105 assert(left >= p->new_padding);
1106 start += p->new_padding;
1107 left -= p->new_padding;
1108 }
1109 }
1110}
1111
e594a3b1
LP
1112static int config_parse_type(
1113 const char *unit,
1114 const char *filename,
1115 unsigned line,
1116 const char *section,
1117 unsigned section_line,
1118 const char *lvalue,
1119 int ltype,
1120 const char *rvalue,
1121 void *data,
1122 void *userdata) {
1123
22e932f4 1124 GptPartitionType *type = ASSERT_PTR(data);
e594a3b1
LP
1125 int r;
1126
1127 assert(rvalue);
e594a3b1 1128
22e932f4 1129 r = gpt_partition_type_from_string(rvalue, type);
e594a3b1
LP
1130 if (r < 0)
1131 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse partition type: %s", rvalue);
1132
1133 return 0;
1134}
1135
1136static int config_parse_label(
1137 const char *unit,
1138 const char *filename,
1139 unsigned line,
1140 const char *section,
1141 unsigned section_line,
1142 const char *lvalue,
1143 int ltype,
1144 const char *rvalue,
1145 void *data,
1146 void *userdata) {
1147
e031166e 1148 _cleanup_free_ char *resolved = NULL;
99534007 1149 char **label = ASSERT_PTR(data);
e594a3b1
LP
1150 int r;
1151
1152 assert(rvalue);
e594a3b1 1153
be9ce018
LP
1154 /* Nota bene: the empty label is a totally valid one. Let's hence not follow our usual rule of
1155 * assigning the empty string to reset to default here, but really accept it as label to set. */
1156
de61a04b 1157 r = specifier_printf(rvalue, GPT_LABEL_MAX, system_and_tmp_specifier_table, arg_root, NULL, &resolved);
e031166e 1158 if (r < 0) {
e459258f 1159 log_syntax(unit, LOG_WARNING, filename, line, r,
e031166e
LP
1160 "Failed to expand specifiers in Label=, ignoring: %s", rvalue);
1161 return 0;
1162 }
1163
1164 if (!utf8_is_valid(resolved)) {
e594a3b1
LP
1165 log_syntax(unit, LOG_WARNING, filename, line, 0,
1166 "Partition label not valid UTF-8, ignoring: %s", rvalue);
1167 return 0;
1168 }
1169
22a0a36e
LP
1170 r = gpt_partition_label_valid(resolved);
1171 if (r < 0) {
1172 log_syntax(unit, LOG_WARNING, filename, line, r,
1173 "Failed to check if string is valid as GPT partition label, ignoring: \"%s\" (from \"%s\")",
1174 resolved, rvalue);
1175 return 0;
1176 }
1177 if (!r) {
e594a3b1 1178 log_syntax(unit, LOG_WARNING, filename, line, 0,
46072ae3
ZJS
1179 "Partition label too long for GPT table, ignoring: \"%s\" (from \"%s\")",
1180 resolved, rvalue);
e594a3b1
LP
1181 return 0;
1182 }
1183
e031166e 1184 free_and_replace(*label, resolved);
e594a3b1
LP
1185 return 0;
1186}
1187
1188static int config_parse_weight(
1189 const char *unit,
1190 const char *filename,
1191 unsigned line,
1192 const char *section,
1193 unsigned section_line,
1194 const char *lvalue,
1195 int ltype,
1196 const char *rvalue,
1197 void *data,
1198 void *userdata) {
1199
f126038f 1200 uint32_t *w = ASSERT_PTR(data), v;
e594a3b1
LP
1201 int r;
1202
1203 assert(rvalue);
e594a3b1
LP
1204
1205 r = safe_atou32(rvalue, &v);
1206 if (r < 0) {
1207 log_syntax(unit, LOG_WARNING, filename, line, r,
1208 "Failed to parse weight value, ignoring: %s", rvalue);
1209 return 0;
1210 }
1211
1212 if (v > 1000U*1000U) {
c8f3d767 1213 log_syntax(unit, LOG_WARNING, filename, line, 0,
e594a3b1
LP
1214 "Weight needs to be in range 0…10000000, ignoring: %" PRIu32, v);
1215 return 0;
1216 }
1217
f126038f 1218 *w = v;
e594a3b1
LP
1219 return 0;
1220}
1221
1222static int config_parse_size4096(
1223 const char *unit,
1224 const char *filename,
1225 unsigned line,
1226 const char *section,
1227 unsigned section_line,
1228 const char *lvalue,
1229 int ltype,
1230 const char *rvalue,
1231 void *data,
1232 void *userdata) {
1233
1234 uint64_t *sz = data, parsed;
1235 int r;
1236
1237 assert(rvalue);
1238 assert(data);
1239
1240 r = parse_size(rvalue, 1024, &parsed);
1241 if (r < 0)
c8f3d767 1242 return log_syntax(unit, LOG_ERR, filename, line, r,
e594a3b1
LP
1243 "Failed to parse size value: %s", rvalue);
1244
1245 if (ltype > 0)
1246 *sz = round_up_size(parsed, 4096);
1247 else if (ltype < 0)
1248 *sz = round_down_size(parsed, 4096);
1249 else
1250 *sz = parsed;
1251
1252 if (*sz != parsed)
e2341b6b
DT
1253 log_syntax(unit, LOG_NOTICE, filename, line, r, "Rounded %s= size %" PRIu64 " %s %" PRIu64 ", a multiple of 4096.",
1254 lvalue, parsed, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), *sz);
e594a3b1
LP
1255
1256 return 0;
1257}
1258
53171c04
LP
1259static int config_parse_fstype(
1260 const char *unit,
1261 const char *filename,
1262 unsigned line,
1263 const char *section,
1264 unsigned section_line,
1265 const char *lvalue,
1266 int ltype,
1267 const char *rvalue,
1268 void *data,
1269 void *userdata) {
1270
99534007 1271 char **fstype = ASSERT_PTR(data);
53171c04
LP
1272
1273 assert(rvalue);
53171c04
LP
1274
1275 if (!filename_is_valid(rvalue))
1276 return log_syntax(unit, LOG_ERR, filename, line, 0,
1277 "File system type is not valid, refusing: %s", rvalue);
1278
1279 return free_and_strdup_warn(fstype, rvalue);
1280}
1281
8a794850
LP
1282static int config_parse_copy_files(
1283 const char *unit,
1284 const char *filename,
1285 unsigned line,
1286 const char *section,
1287 unsigned section_line,
1288 const char *lvalue,
1289 int ltype,
1290 const char *rvalue,
1291 void *data,
1292 void *userdata) {
1293
1294 _cleanup_free_ char *source = NULL, *buffer = NULL, *resolved_source = NULL, *resolved_target = NULL;
1295 const char *p = rvalue, *target;
99534007 1296 Partition *partition = ASSERT_PTR(data);
8a794850
LP
1297 int r;
1298
1299 assert(rvalue);
8a794850
LP
1300
1301 r = extract_first_word(&p, &source, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS);
1302 if (r < 0)
1303 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract source path: %s", rvalue);
1304 if (r == 0) {
1305 log_syntax(unit, LOG_WARNING, filename, line, 0, "No argument specified: %s", rvalue);
1306 return 0;
1307 }
1308
1309 r = extract_first_word(&p, &buffer, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS);
1310 if (r < 0)
1311 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract target path: %s", rvalue);
1312 if (r == 0)
1313 target = source; /* No target, then it's the same as the source */
1314 else
1315 target = buffer;
1316
1317 if (!isempty(p))
1318 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), "Too many arguments: %s", rvalue);
1319
de61a04b 1320 r = specifier_printf(source, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_source);
8a794850
LP
1321 if (r < 0) {
1322 log_syntax(unit, LOG_WARNING, filename, line, r,
1323 "Failed to expand specifiers in CopyFiles= source, ignoring: %s", rvalue);
1324 return 0;
1325 }
1326
0ade2213
LP
1327 r = path_simplify_and_warn(resolved_source, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1328 if (r < 0)
8a794850 1329 return 0;
8a794850 1330
de61a04b 1331 r = specifier_printf(target, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_target);
8a794850
LP
1332 if (r < 0) {
1333 log_syntax(unit, LOG_WARNING, filename, line, r,
1334 "Failed to expand specifiers in CopyFiles= target, ignoring: %s", resolved_target);
1335 return 0;
1336 }
1337
0ade2213
LP
1338 r = path_simplify_and_warn(resolved_target, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1339 if (r < 0)
8a794850 1340 return 0;
8a794850
LP
1341
1342 r = strv_consume_pair(&partition->copy_files, TAKE_PTR(resolved_source), TAKE_PTR(resolved_target));
1343 if (r < 0)
1344 return log_oom();
1345
1346 return 0;
1347}
1348
5c08da58
LP
1349static int config_parse_copy_blocks(
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 _cleanup_free_ char *d = NULL;
99534007 1362 Partition *partition = ASSERT_PTR(data);
5c08da58
LP
1363 int r;
1364
1365 assert(rvalue);
5c08da58
LP
1366
1367 if (isempty(rvalue)) {
1368 partition->copy_blocks_path = mfree(partition->copy_blocks_path);
1369 partition->copy_blocks_auto = false;
1370 return 0;
1371 }
1372
1373 if (streq(rvalue, "auto")) {
1374 partition->copy_blocks_path = mfree(partition->copy_blocks_path);
1375 partition->copy_blocks_auto = true;
585c5c75 1376 partition->copy_blocks_root = arg_root;
5c08da58
LP
1377 return 0;
1378 }
1379
de61a04b 1380 r = specifier_printf(rvalue, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &d);
5c08da58
LP
1381 if (r < 0) {
1382 log_syntax(unit, LOG_WARNING, filename, line, r,
1383 "Failed to expand specifiers in CopyBlocks= source path, ignoring: %s", rvalue);
1384 return 0;
1385 }
1386
1387 r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1388 if (r < 0)
1389 return 0;
1390
1391 free_and_replace(partition->copy_blocks_path, d);
1392 partition->copy_blocks_auto = false;
585c5c75 1393 partition->copy_blocks_root = arg_root;
5c08da58
LP
1394 return 0;
1395}
1396
d83d8048
LP
1397static int config_parse_make_dirs(
1398 const char *unit,
1399 const char *filename,
1400 unsigned line,
1401 const char *section,
1402 unsigned section_line,
1403 const char *lvalue,
1404 int ltype,
1405 const char *rvalue,
1406 void *data,
1407 void *userdata) {
1408
99534007
DT
1409 Partition *partition = ASSERT_PTR(data);
1410 const char *p = ASSERT_PTR(rvalue);
d83d8048
LP
1411 int r;
1412
d83d8048
LP
1413 for (;;) {
1414 _cleanup_free_ char *word = NULL, *d = NULL;
1415
1416 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
1417 if (r == -ENOMEM)
1418 return log_oom();
1419 if (r < 0) {
1420 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
1421 return 0;
1422 }
1423 if (r == 0)
1424 return 0;
1425
de61a04b 1426 r = specifier_printf(word, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &d);
d83d8048
LP
1427 if (r < 0) {
1428 log_syntax(unit, LOG_WARNING, filename, line, r,
1429 "Failed to expand specifiers in MakeDirectories= parameter, ignoring: %s", word);
1430 continue;
1431 }
1432
1433 r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1434 if (r < 0)
1435 continue;
1436
1437 r = strv_consume(&partition->make_directories, TAKE_PTR(d));
1438 if (r < 0)
1439 return log_oom();
1440 }
1441}
1442
889914ef
LP
1443static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_encrypt, encrypt_mode, EncryptMode, ENCRYPT_OFF, "Invalid encryption mode");
1444
e73309c5
LP
1445static int config_parse_gpt_flags(
1446 const char *unit,
1447 const char *filename,
1448 unsigned line,
1449 const char *section,
1450 unsigned section_line,
1451 const char *lvalue,
1452 int ltype,
1453 const char *rvalue,
1454 void *data,
1455 void *userdata) {
1456
99534007 1457 uint64_t *gpt_flags = ASSERT_PTR(data);
e73309c5
LP
1458 int r;
1459
1460 assert(rvalue);
e73309c5
LP
1461
1462 r = safe_atou64(rvalue, gpt_flags);
1463 if (r < 0) {
1464 log_syntax(unit, LOG_WARNING, filename, line, r,
1465 "Failed to parse Flags= value, ignoring: %s", rvalue);
1466 return 0;
1467 }
1468
1469 return 0;
1470}
1471
11749b61
DDM
1472static int config_parse_uuid(
1473 const char *unit,
1474 const char *filename,
1475 unsigned line,
1476 const char *section,
1477 unsigned section_line,
1478 const char *lvalue,
1479 int ltype,
1480 const char *rvalue,
1481 void *data,
1482 void *userdata) {
1483
1484 Partition *partition = ASSERT_PTR(data);
1485 int r;
1486
1487 if (isempty(rvalue)) {
1488 partition->new_uuid = SD_ID128_NULL;
1489 partition->new_uuid_is_set = false;
1490 return 0;
1491 }
1492
1493 if (streq(rvalue, "null")) {
1494 partition->new_uuid = SD_ID128_NULL;
1495 partition->new_uuid_is_set = true;
1496 return 0;
1497 }
1498
1499 r = sd_id128_from_string(rvalue, &partition->new_uuid);
1500 if (r < 0) {
1501 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse 128bit ID/UUID, ignoring: %s", rvalue);
1502 return 0;
1503 }
1504
1505 partition->new_uuid_is_set = true;
1506
1507 return 0;
1508}
1509
b5b7879a
DDM
1510static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_verity, verity_mode, VerityMode, VERITY_OFF, "Invalid verity mode");
1511
39fc0174 1512static int partition_read_definition(Partition *p, const char *path, const char *const *conf_file_dirs) {
e594a3b1
LP
1513
1514 ConfigTableItem table[] = {
22e932f4 1515 { "Partition", "Type", config_parse_type, 0, &p->type },
4cee8333
DDM
1516 { "Partition", "Label", config_parse_label, 0, &p->new_label },
1517 { "Partition", "UUID", config_parse_uuid, 0, p },
1518 { "Partition", "Priority", config_parse_int32, 0, &p->priority },
1519 { "Partition", "Weight", config_parse_weight, 0, &p->weight },
1520 { "Partition", "PaddingWeight", config_parse_weight, 0, &p->padding_weight },
1521 { "Partition", "SizeMinBytes", config_parse_size4096, 1, &p->size_min },
1522 { "Partition", "SizeMaxBytes", config_parse_size4096, -1, &p->size_max },
1523 { "Partition", "PaddingMinBytes", config_parse_size4096, 1, &p->padding_min },
1524 { "Partition", "PaddingMaxBytes", config_parse_size4096, -1, &p->padding_max },
1525 { "Partition", "FactoryReset", config_parse_bool, 0, &p->factory_reset },
1526 { "Partition", "CopyBlocks", config_parse_copy_blocks, 0, p },
1527 { "Partition", "Format", config_parse_fstype, 0, &p->format },
1528 { "Partition", "CopyFiles", config_parse_copy_files, 0, p },
1529 { "Partition", "MakeDirectories", config_parse_make_dirs, 0, p },
1530 { "Partition", "Encrypt", config_parse_encrypt, 0, &p->encrypt },
1531 { "Partition", "Verity", config_parse_verity, 0, &p->verity },
1532 { "Partition", "VerityMatchKey", config_parse_string, 0, &p->verity_match_key },
1533 { "Partition", "Flags", config_parse_gpt_flags, 0, &p->gpt_flags },
1534 { "Partition", "ReadOnly", config_parse_tristate, 0, &p->read_only },
1535 { "Partition", "NoAuto", config_parse_tristate, 0, &p->no_auto },
1536 { "Partition", "GrowFileSystem", config_parse_tristate, 0, &p->growfs },
1537 { "Partition", "SplitName", config_parse_string, 0, &p->split_name_format },
c4a87b76 1538 { "Partition", "Minimize", config_parse_bool, 0, &p->minimize },
e594a3b1
LP
1539 {}
1540 };
1541 int r;
39fc0174
RP
1542 _cleanup_free_ char *filename = NULL;
1543 const char* dropin_dirname;
e594a3b1 1544
39fc0174
RP
1545 r = path_extract_filename(path, &filename);
1546 if (r < 0)
bef69ae8 1547 return log_error_errno(r, "Failed to extract filename from path '%s': %m", path);
39fc0174
RP
1548
1549 dropin_dirname = strjoina(filename, ".d");
1550
1551 r = config_parse_many(
1552 STRV_MAKE_CONST(path),
1553 conf_file_dirs,
1554 dropin_dirname,
1555 "Partition\0",
1556 config_item_table_lookup, table,
1557 CONFIG_PARSE_WARN,
1558 p,
1559 NULL,
1560 &p->drop_in_files);
e594a3b1
LP
1561 if (r < 0)
1562 return r;
1563
1564 if (p->size_min != UINT64_MAX && p->size_max != UINT64_MAX && p->size_min > p->size_max)
1565 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1566 "SizeMinBytes= larger than SizeMaxBytes=, refusing.");
1567
1568 if (p->padding_min != UINT64_MAX && p->padding_max != UINT64_MAX && p->padding_min > p->padding_max)
1569 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1570 "PaddingMinBytes= larger than PaddingMaxBytes=, refusing.");
1571
22e932f4 1572 if (sd_id128_is_null(p->type.uuid))
e594a3b1
LP
1573 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1574 "Type= not defined, refusing.");
1575
5c08da58
LP
1576 if ((p->copy_blocks_path || p->copy_blocks_auto) &&
1577 (p->format || !strv_isempty(p->copy_files) || !strv_isempty(p->make_directories)))
53171c04 1578 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
5c08da58 1579 "Format=/CopyFiles=/MakeDirectories= and CopyBlocks= cannot be combined, refusing.");
53171c04 1580
d83d8048 1581 if ((!strv_isempty(p->copy_files) || !strv_isempty(p->make_directories)) && streq_ptr(p->format, "swap"))
8a794850
LP
1582 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1583 "Format=swap and CopyFiles= cannot be combined, refusing.");
1584
5c08da58 1585 if (!p->format && (!strv_isempty(p->copy_files) || !strv_isempty(p->make_directories) || (p->encrypt != ENCRYPT_OFF && !(p->copy_blocks_path || p->copy_blocks_auto)))) {
b9df3536 1586 /* Pick "ext4" as file system if we are configured to copy files or encrypt the device */
8a794850
LP
1587 p->format = strdup("ext4");
1588 if (!p->format)
1589 return log_oom();
1590 }
1591
c4a87b76
DDM
1592 if (p->minimize && !p->format)
1593 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1594 "Minimize= can only be enabled if Format= is set");
1595
b5b7879a
DDM
1596 if (p->verity != VERITY_OFF || p->encrypt != ENCRYPT_OFF) {
1597 r = dlopen_cryptsetup();
1598 if (r < 0)
1599 return log_syntax(NULL, LOG_ERR, path, 1, r,
1600 "libcryptsetup not found, Verity=/Encrypt= are not supported: %m");
1601 }
1602
1603 if (p->verity != VERITY_OFF && !p->verity_match_key)
1604 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1605 "VerityMatchKey= must be set if Verity=%s", verity_mode_to_string(p->verity));
1606
1607 if (p->verity == VERITY_OFF && p->verity_match_key)
1608 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1609 "VerityMatchKey= can only be set if Verity= is not \"%s\"",
1610 verity_mode_to_string(p->verity));
1611
b456191d
DDM
1612 if (IN_SET(p->verity, VERITY_HASH, VERITY_SIG) &&
1613 (p->copy_files || p->copy_blocks_path || p->copy_blocks_auto || p->format || p->make_directories))
b5b7879a
DDM
1614 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1615 "CopyBlocks=/CopyFiles=/Format=/MakeDirectories= cannot be used with Verity=%s",
1616 verity_mode_to_string(p->verity));
1617
1618 if (p->verity != VERITY_OFF && p->encrypt != ENCRYPT_OFF)
1619 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1620 "Encrypting verity hash/data partitions is not supported");
1621
b456191d
DDM
1622 if (p->verity == VERITY_SIG && !arg_private_key)
1623 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1624 "Verity signature partition requested but no private key provided (--private-key=)");
1625
1626 if (p->verity == VERITY_SIG && !arg_certificate)
1627 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
ba4a5eff 1628 "Verity signature partition requested but no PEM certificate provided (--certificate=)");
b456191d
DDM
1629
1630 if (p->verity == VERITY_SIG && (p->size_min != UINT64_MAX || p->size_max != UINT64_MAX))
1631 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1632 "SizeMinBytes=/SizeMaxBytes= cannot be used with Verity=%s",
1633 verity_mode_to_string(p->verity));
1634
e73309c5 1635 /* Verity partitions are read only, let's imply the RO flag hence, unless explicitly configured otherwise. */
22e932f4
DDM
1636 if ((p->type.designator == PARTITION_ROOT_VERITY ||
1637 p->type.designator == PARTITION_USR_VERITY) &&
e73309c5
LP
1638 p->read_only < 0)
1639 p->read_only = true;
1640
1c41c1dc 1641 /* Default to "growfs" on, unless read-only */
22e932f4 1642 if (gpt_partition_type_knows_growfs(p->type) &&
1c41c1dc
LP
1643 p->read_only <= 0)
1644 p->growfs = true;
1645
4cee8333
DDM
1646 if (!p->split_name_format) {
1647 char *s = strdup("%t");
1648 if (!s)
1649 return log_oom();
1650
1651 p->split_name_format = s;
1652 } else if (streq(p->split_name_format, "-"))
1653 p->split_name_format = mfree(p->split_name_format);
1654
e594a3b1
LP
1655 return 0;
1656}
1657
b5b7879a
DDM
1658static int find_verity_sibling(Context *context, Partition *p, VerityMode mode, Partition **ret) {
1659 Partition *s = NULL;
1660
1661 assert(p);
1662 assert(p->verity != VERITY_OFF);
1663 assert(p->verity_match_key);
1664 assert(mode != VERITY_OFF);
1665 assert(p->verity != mode);
1666 assert(ret);
1667
1668 /* Try to find the matching sibling partition of the given type for a verity partition. For a data
af3d3873
YW
1669 * partition, this is the corresponding hash partition with the same verity name (and vice versa for
1670 * the hash partition). */
b5b7879a
DDM
1671
1672 LIST_FOREACH(partitions, q, context->partitions) {
1673 if (p == q)
1674 continue;
1675
1676 if (q->verity != mode)
1677 continue;
1678
1679 assert(q->verity_match_key);
1680
1681 if (!streq(p->verity_match_key, q->verity_match_key))
1682 continue;
1683
1684 if (s)
1685 return -ENOTUNIQ;
1686
1687 s = q;
1688 }
1689
1690 if (!s)
1691 return -ENXIO;
1692
1693 *ret = s;
1694
1695 return 0;
1696}
1697
e594a3b1
LP
1698static int context_read_definitions(
1699 Context *context,
224c853f 1700 char **directories,
e594a3b1
LP
1701 const char *root) {
1702
1703 _cleanup_strv_free_ char **files = NULL;
1704 Partition *last = NULL;
e594a3b1 1705 int r;
39fc0174 1706 const char *const *dirs;
e594a3b1
LP
1707
1708 assert(context);
1709
224c853f 1710 dirs = (const char* const*) (directories ?: CONF_PATHS_STRV("repart.d"));
39fc0174 1711
224c853f 1712 r = conf_files_list_strv(&files, ".conf", directories ? NULL : root, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, dirs);
e594a3b1
LP
1713 if (r < 0)
1714 return log_error_errno(r, "Failed to enumerate *.conf files: %m");
1715
1716 STRV_FOREACH(f, files) {
1717 _cleanup_(partition_freep) Partition *p = NULL;
1718
1719 p = partition_new();
1720 if (!p)
1721 return log_oom();
1722
1723 p->definition_path = strdup(*f);
1724 if (!p->definition_path)
1725 return log_oom();
1726
39fc0174 1727 r = partition_read_definition(p, *f, dirs);
e594a3b1
LP
1728 if (r < 0)
1729 return r;
1730
1731 LIST_INSERT_AFTER(partitions, context->partitions, last, p);
1732 last = TAKE_PTR(p);
1733 context->n_partitions++;
1734 }
1735
b5b7879a
DDM
1736 /* Check that each configured verity hash/data partition has a matching verity data/hash partition. */
1737
1738 LIST_FOREACH(partitions, p, context->partitions) {
1739 if (p->verity == VERITY_OFF)
1740 continue;
1741
1742 for (VerityMode mode = VERITY_OFF + 1; mode < _VERITY_MODE_MAX; mode++) {
b456191d 1743 Partition *q = NULL;
b5b7879a
DDM
1744
1745 if (p->verity == mode)
1746 continue;
1747
1748 if (p->siblings[mode])
1749 continue;
1750
1751 r = find_verity_sibling(context, p, mode, &q);
8e52ed02
DDM
1752 if (r == -ENXIO) {
1753 if (mode != VERITY_SIG)
1754 return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL),
1755 "Missing verity %s partition for verity %s partition with VerityMatchKey=%s",
1756 verity_mode_to_string(mode), verity_mode_to_string(p->verity), p->verity_match_key);
1757 } else if (r == -ENOTUNIQ)
b5b7879a
DDM
1758 return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL),
1759 "Multiple verity %s partitions found for verity %s partition with VerityMatchKey=%s",
1760 verity_mode_to_string(mode), verity_mode_to_string(p->verity), p->verity_match_key);
8e52ed02
DDM
1761 else if (r < 0)
1762 return log_syntax(NULL, LOG_ERR, p->definition_path, 1, r,
1763 "Failed to find verity %s partition for verity %s partition with VerityMatchKey=%s",
1764 verity_mode_to_string(mode), verity_mode_to_string(p->verity), p->verity_match_key);
b5b7879a 1765
b456191d
DDM
1766 if (q) {
1767 if (q->priority != p->priority)
1768 return log_syntax(NULL, LOG_ERR, p->definition_path, 1, SYNTHETIC_ERRNO(EINVAL),
1769 "Priority mismatch (%i != %i) for verity sibling partitions with VerityMatchKey=%s",
1770 p->priority, q->priority, p->verity_match_key);
b5b7879a 1771
b456191d
DDM
1772 p->siblings[mode] = q;
1773 }
b5b7879a
DDM
1774 }
1775 }
1776
e594a3b1
LP
1777 return 0;
1778}
1779
e594a3b1
LP
1780static int determine_current_padding(
1781 struct fdisk_context *c,
1782 struct fdisk_table *t,
1783 struct fdisk_partition *p,
994b3031
LP
1784 uint64_t secsz,
1785 uint64_t grainsz,
e594a3b1
LP
1786 uint64_t *ret) {
1787
1788 size_t n_partitions;
1789 uint64_t offset, next = UINT64_MAX;
1790
1791 assert(c);
1792 assert(t);
1793 assert(p);
1794
1795 if (!fdisk_partition_has_end(p))
1796 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition has no end!");
1797
1798 offset = fdisk_partition_get_end(p);
994b3031
LP
1799 assert(offset < UINT64_MAX / secsz);
1800 offset *= secsz;
e594a3b1
LP
1801
1802 n_partitions = fdisk_table_get_nents(t);
695cfd53 1803 for (size_t i = 0; i < n_partitions; i++) {
e594a3b1
LP
1804 struct fdisk_partition *q;
1805 uint64_t start;
1806
1807 q = fdisk_table_get_partition(t, i);
1808 if (!q)
1809 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
1810
1811 if (fdisk_partition_is_used(q) <= 0)
1812 continue;
1813
1814 if (!fdisk_partition_has_start(q))
1815 continue;
1816
1817 start = fdisk_partition_get_start(q);
994b3031
LP
1818 assert(start < UINT64_MAX / secsz);
1819 start *= secsz;
e594a3b1
LP
1820
1821 if (start >= offset && (next == UINT64_MAX || next > start))
1822 next = start;
1823 }
1824
1825 if (next == UINT64_MAX) {
1826 /* No later partition? In that case check the end of the usable area */
1827 next = fdisk_get_last_lba(c);
1828 assert(next < UINT64_MAX);
1829 next++; /* The last LBA is one sector before the end */
1830
994b3031
LP
1831 assert(next < UINT64_MAX / secsz);
1832 next *= secsz;
e594a3b1
LP
1833
1834 if (offset > next)
1835 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end.");
1836 }
1837
1838 assert(next >= offset);
994b3031
LP
1839 offset = round_up_size(offset, grainsz);
1840 next = round_down_size(next, grainsz);
e594a3b1 1841
a6f44d61 1842 *ret = LESS_BY(next, offset); /* Saturated subtraction, rounding might have fucked things up */
e594a3b1
LP
1843 return 0;
1844}
1845
1846static int fdisk_ask_cb(struct fdisk_context *c, struct fdisk_ask *ask, void *data) {
1847 _cleanup_free_ char *ids = NULL;
1848 int r;
1849
1850 if (fdisk_ask_get_type(ask) != FDISK_ASKTYPE_STRING)
1851 return -EINVAL;
1852
b7416360 1853 ids = new(char, SD_ID128_UUID_STRING_MAX);
e594a3b1
LP
1854 if (!ids)
1855 return -ENOMEM;
1856
b7416360 1857 r = fdisk_ask_string_set_result(ask, sd_id128_to_uuid_string(*(sd_id128_t*) data, ids));
e594a3b1
LP
1858 if (r < 0)
1859 return r;
1860
1861 TAKE_PTR(ids);
1862 return 0;
1863}
1864
1865static int fdisk_set_disklabel_id_by_uuid(struct fdisk_context *c, sd_id128_t id) {
1866 int r;
1867
1868 r = fdisk_set_ask(c, fdisk_ask_cb, &id);
1869 if (r < 0)
1870 return r;
1871
1872 r = fdisk_set_disklabel_id(c);
1873 if (r < 0)
1874 return r;
1875
1876 return fdisk_set_ask(c, NULL, NULL);
1877}
1878
53171c04 1879static int derive_uuid(sd_id128_t base, const char *token, sd_id128_t *ret) {
e594a3b1 1880 union {
ade99252 1881 uint8_t md[SHA256_DIGEST_SIZE];
e594a3b1
LP
1882 sd_id128_t id;
1883 } result;
1884
53171c04 1885 assert(token);
e594a3b1
LP
1886 assert(ret);
1887
53171c04
LP
1888 /* Derive a new UUID from the specified UUID in a stable and reasonably safe way. Specifically, we
1889 * calculate the HMAC-SHA256 of the specified token string, keyed by the supplied base (typically the
1890 * machine ID). We use the machine ID as key (and not as cleartext!) of the HMAC operation since it's
1891 * the machine ID we don't want to leak. */
e594a3b1 1892
ade99252 1893 hmac_sha256(base.bytes, sizeof(base.bytes), token, strlen(token), result.md);
e594a3b1
LP
1894
1895 /* Take the first half, mark it as v4 UUID */
1896 assert_cc(sizeof(result.md) == sizeof(result.id) * 2);
1897 *ret = id128_make_v4_uuid(result.id);
1898 return 0;
1899}
1900
a26f4a49
LP
1901static int context_load_partition_table(
1902 Context *context,
1903 const char *node,
1904 int *backing_fd) {
1905
e594a3b1
LP
1906 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
1907 _cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL;
1908 uint64_t left_boundary = UINT64_MAX, first_lba, last_lba, nsectors;
1909 _cleanup_free_ char *disk_uuid_string = NULL;
1910 bool from_scratch = false;
1911 sd_id128_t disk_uuid;
1912 size_t n_partitions;
994b3031
LP
1913 unsigned long secsz;
1914 uint64_t grainsz;
e594a3b1
LP
1915 int r;
1916
1917 assert(context);
1918 assert(node);
a26f4a49 1919 assert(backing_fd);
170c9823
LP
1920 assert(!context->fdisk_context);
1921 assert(!context->free_areas);
1922 assert(context->start == UINT64_MAX);
1923 assert(context->end == UINT64_MAX);
1924 assert(context->total == UINT64_MAX);
e594a3b1
LP
1925
1926 c = fdisk_new_context();
1927 if (!c)
1928 return log_oom();
1929
a26f4a49
LP
1930 /* libfdisk doesn't have an API to operate on arbitrary fds, hence reopen the fd going via the
1931 * /proc/self/fd/ magic path if we have an existing fd. Open the original file otherwise. */
1932 if (*backing_fd < 0)
1933 r = fdisk_assign_device(c, node, arg_dry_run);
ddb6eeaf
LP
1934 else
1935 r = fdisk_assign_device(c, FORMAT_PROC_FD_PATH(*backing_fd), arg_dry_run);
170c9823
LP
1936 if (r == -EINVAL && arg_size_auto) {
1937 struct stat st;
1938
1939 /* libfdisk returns EINVAL if opening a file of size zero. Let's check for that, and accept
1940 * it if automatic sizing is requested. */
1941
1942 if (*backing_fd < 0)
1943 r = stat(node, &st);
1944 else
1945 r = fstat(*backing_fd, &st);
1946 if (r < 0)
1947 return log_error_errno(errno, "Failed to stat block device '%s': %m", node);
1948
994b3031
LP
1949 if (S_ISREG(st.st_mode) && st.st_size == 0) {
1950 /* User the fallback values if we have no better idea */
1951 context->sector_size = 512;
1952 context->grain_size = 4096;
170c9823 1953 return /* from_scratch = */ true;
994b3031 1954 }
170c9823
LP
1955
1956 r = -EINVAL;
1957 }
e594a3b1 1958 if (r < 0)
a26f4a49
LP
1959 return log_error_errno(r, "Failed to open device '%s': %m", node);
1960
1961 if (*backing_fd < 0) {
1962 /* If we have no fd referencing the device yet, make a copy of the fd now, so that we have one */
38f81e93 1963 *backing_fd = fd_reopen(fdisk_get_devfd(c), O_RDONLY|O_CLOEXEC);
a26f4a49 1964 if (*backing_fd < 0)
38f81e93 1965 return log_error_errno(*backing_fd, "Failed to duplicate fdisk fd: %m");
e594a3b1 1966
25baae50
DDM
1967 /* Tell udev not to interfere while we are processing the device */
1968 if (flock(*backing_fd, arg_dry_run ? LOCK_SH : LOCK_EX) < 0)
1969 return log_error_errno(errno, "Failed to lock block device: %m");
1970 }
e594a3b1 1971
994b3031
LP
1972 /* The offsets/sizes libfdisk returns to us will be in multiple of the sector size of the
1973 * device. This is typically 512, and sometimes 4096. Let's query libfdisk once for it, and then use
1974 * it for all our needs. Note that the values we use ourselves always are in bytes though, thus mean
1975 * the same thing universally. Also note that regardless what kind of sector size is in use we'll
1976 * place partitions at multiples of 4K. */
1977 secsz = fdisk_get_sector_size(c);
1978
1979 /* Insist on a power of two, and that it's a multiple of 512, i.e. the traditional sector size. */
983ce0b5
LP
1980 if (secsz < 512 || !ISPOWEROF2(secsz))
1981 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Sector size %lu is not a power of two larger than 512? Refusing.", secsz);
994b3031
LP
1982
1983 /* Use at least 4K, and ensure it's a multiple of the sector size, regardless if that is smaller or
1984 * larger */
1985 grainsz = secsz < 4096 ? 4096 : secsz;
1986
1987 log_debug("Sector size of device is %lu bytes. Using grain size of %" PRIu64 ".", secsz, grainsz);
1988
e594a3b1
LP
1989 switch (arg_empty) {
1990
1991 case EMPTY_REFUSE:
1992 /* Refuse empty disks, insist on an existing GPT partition table */
1993 if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
1994 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not repartitioning.", node);
1995
1996 break;
1997
1998 case EMPTY_REQUIRE:
1999 /* Require an empty disk, refuse any existing partition table */
2000 r = fdisk_has_label(c);
2001 if (r < 0)
2002 return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", node);
2003 if (r > 0)
2004 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s already has a disk label, refusing.", node);
2005
2006 from_scratch = true;
2007 break;
2008
2009 case EMPTY_ALLOW:
2010 /* Allow both an empty disk and an existing partition table, but only GPT */
2011 r = fdisk_has_label(c);
2012 if (r < 0)
2013 return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", node);
2014 if (r > 0) {
2015 if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
2016 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has non-GPT disk label, not repartitioning.", node);
2017 } else
2018 from_scratch = true;
2019
2020 break;
2021
2022 case EMPTY_FORCE:
a26f4a49 2023 case EMPTY_CREATE:
e594a3b1
LP
2024 /* Always reinitiaize the disk, don't consider what there was on the disk before */
2025 from_scratch = true;
2026 break;
2027 }
2028
2029 if (from_scratch) {
e594a3b1
LP
2030 r = fdisk_create_disklabel(c, "gpt");
2031 if (r < 0)
2032 return log_error_errno(r, "Failed to create GPT disk label: %m");
2033
53171c04 2034 r = derive_uuid(context->seed, "disk-uuid", &disk_uuid);
e594a3b1
LP
2035 if (r < 0)
2036 return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
2037
2038 r = fdisk_set_disklabel_id_by_uuid(c, disk_uuid);
2039 if (r < 0)
2040 return log_error_errno(r, "Failed to set GPT disk label: %m");
2041
2042 goto add_initial_free_area;
2043 }
2044
2045 r = fdisk_get_disklabel_id(c, &disk_uuid_string);
2046 if (r < 0)
2047 return log_error_errno(r, "Failed to get current GPT disk label UUID: %m");
2048
2049 r = sd_id128_from_string(disk_uuid_string, &disk_uuid);
2050 if (r < 0)
2051 return log_error_errno(r, "Failed to parse current GPT disk label UUID: %m");
2052
2053 if (sd_id128_is_null(disk_uuid)) {
53171c04 2054 r = derive_uuid(context->seed, "disk-uuid", &disk_uuid);
e594a3b1
LP
2055 if (r < 0)
2056 return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
2057
2058 r = fdisk_set_disklabel_id(c);
2059 if (r < 0)
2060 return log_error_errno(r, "Failed to set GPT disk label: %m");
2061 }
2062
2063 r = fdisk_get_partitions(c, &t);
2064 if (r < 0)
2065 return log_error_errno(r, "Failed to acquire partition table: %m");
2066
2067 n_partitions = fdisk_table_get_nents(t);
695cfd53 2068 for (size_t i = 0; i < n_partitions; i++) {
e594a3b1 2069 _cleanup_free_ char *label_copy = NULL;
03677889 2070 Partition *last = NULL;
e594a3b1
LP
2071 struct fdisk_partition *p;
2072 struct fdisk_parttype *pt;
2073 const char *pts, *ids, *label;
2074 uint64_t sz, start;
2075 bool found = false;
2076 sd_id128_t ptid, id;
2077 size_t partno;
2078
2079 p = fdisk_table_get_partition(t, i);
2080 if (!p)
2081 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
2082
2083 if (fdisk_partition_is_used(p) <= 0)
2084 continue;
2085
2086 if (fdisk_partition_has_start(p) <= 0 ||
2087 fdisk_partition_has_size(p) <= 0 ||
2088 fdisk_partition_has_partno(p) <= 0)
2089 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a position, size or number.");
2090
2091 pt = fdisk_partition_get_type(p);
2092 if (!pt)
2093 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition: %m");
2094
2095 pts = fdisk_parttype_get_string(pt);
2096 if (!pts)
2097 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition as string: %m");
2098
2099 r = sd_id128_from_string(pts, &ptid);
2100 if (r < 0)
2101 return log_error_errno(r, "Failed to parse partition type UUID %s: %m", pts);
2102
2103 ids = fdisk_partition_get_uuid(p);
2104 if (!ids)
2105 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a UUID.");
2106
2107 r = sd_id128_from_string(ids, &id);
2108 if (r < 0)
2109 return log_error_errno(r, "Failed to parse partition UUID %s: %m", ids);
2110
2111 label = fdisk_partition_get_name(p);
2112 if (!isempty(label)) {
2113 label_copy = strdup(label);
2114 if (!label_copy)
2115 return log_oom();
2116 }
2117
2118 sz = fdisk_partition_get_size(p);
ac33e147 2119 assert(sz <= UINT64_MAX/secsz);
994b3031 2120 sz *= secsz;
e594a3b1
LP
2121
2122 start = fdisk_partition_get_start(p);
ac33e147 2123 assert(start <= UINT64_MAX/secsz);
994b3031 2124 start *= secsz;
e594a3b1
LP
2125
2126 partno = fdisk_partition_get_partno(p);
2127
2128 if (left_boundary == UINT64_MAX || left_boundary > start)
2129 left_boundary = start;
2130
2131 /* Assign this existing partition to the first partition of the right type that doesn't have
2132 * an existing one assigned yet. */
2133 LIST_FOREACH(partitions, pp, context->partitions) {
2134 last = pp;
2135
22e932f4 2136 if (!sd_id128_equal(pp->type.uuid, ptid))
e594a3b1
LP
2137 continue;
2138
2139 if (!pp->current_partition) {
2140 pp->current_uuid = id;
2141 pp->current_size = sz;
2142 pp->offset = start;
2143 pp->partno = partno;
2144 pp->current_label = TAKE_PTR(label_copy);
2145
2146 pp->current_partition = p;
2147 fdisk_ref_partition(p);
2148
994b3031 2149 r = determine_current_padding(c, t, p, secsz, grainsz, &pp->current_padding);
e594a3b1
LP
2150 if (r < 0)
2151 return r;
2152
2153 if (pp->current_padding > 0) {
2154 r = context_add_free_area(context, pp->current_padding, pp);
2155 if (r < 0)
2156 return r;
2157 }
2158
2159 found = true;
2160 break;
2161 }
2162 }
2163
2164 /* If we have no matching definition, create a new one. */
2165 if (!found) {
2166 _cleanup_(partition_freep) Partition *np = NULL;
2167
2168 np = partition_new();
2169 if (!np)
2170 return log_oom();
2171
2172 np->current_uuid = id;
22e932f4 2173 np->type = gpt_partition_type_from_uuid(ptid);
e594a3b1
LP
2174 np->current_size = sz;
2175 np->offset = start;
2176 np->partno = partno;
2177 np->current_label = TAKE_PTR(label_copy);
2178
2179 np->current_partition = p;
2180 fdisk_ref_partition(p);
2181
994b3031 2182 r = determine_current_padding(c, t, p, secsz, grainsz, &np->current_padding);
e594a3b1
LP
2183 if (r < 0)
2184 return r;
2185
2186 if (np->current_padding > 0) {
2187 r = context_add_free_area(context, np->current_padding, np);
2188 if (r < 0)
2189 return r;
2190 }
2191
2192 LIST_INSERT_AFTER(partitions, context->partitions, last, TAKE_PTR(np));
2193 context->n_partitions++;
2194 }
2195 }
2196
2197add_initial_free_area:
2198 nsectors = fdisk_get_nsectors(c);
994b3031
LP
2199 assert(nsectors <= UINT64_MAX/secsz);
2200 nsectors *= secsz;
e594a3b1
LP
2201
2202 first_lba = fdisk_get_first_lba(c);
994b3031
LP
2203 assert(first_lba <= UINT64_MAX/secsz);
2204 first_lba *= secsz;
e594a3b1
LP
2205
2206 last_lba = fdisk_get_last_lba(c);
2207 assert(last_lba < UINT64_MAX);
2208 last_lba++;
994b3031
LP
2209 assert(last_lba <= UINT64_MAX/secsz);
2210 last_lba *= secsz;
e594a3b1
LP
2211
2212 assert(last_lba >= first_lba);
2213
2214 if (left_boundary == UINT64_MAX) {
2215 /* No partitions at all? Then the whole disk is up for grabs. */
2216
994b3031
LP
2217 first_lba = round_up_size(first_lba, grainsz);
2218 last_lba = round_down_size(last_lba, grainsz);
e594a3b1
LP
2219
2220 if (last_lba > first_lba) {
2221 r = context_add_free_area(context, last_lba - first_lba, NULL);
2222 if (r < 0)
2223 return r;
2224 }
2225 } else {
2226 /* Add space left of first partition */
2227 assert(left_boundary >= first_lba);
2228
994b3031
LP
2229 first_lba = round_up_size(first_lba, grainsz);
2230 left_boundary = round_down_size(left_boundary, grainsz);
2231 last_lba = round_down_size(last_lba, grainsz);
e594a3b1
LP
2232
2233 if (left_boundary > first_lba) {
2234 r = context_add_free_area(context, left_boundary - first_lba, NULL);
2235 if (r < 0)
2236 return r;
2237 }
2238 }
2239
2240 context->start = first_lba;
2241 context->end = last_lba;
2242 context->total = nsectors;
994b3031
LP
2243 context->sector_size = secsz;
2244 context->grain_size = grainsz;
e594a3b1
LP
2245 context->fdisk_context = TAKE_PTR(c);
2246
2247 return from_scratch;
2248}
2249
2250static void context_unload_partition_table(Context *context) {
e594a3b1
LP
2251 assert(context);
2252
80a226b2 2253 LIST_FOREACH(partitions, p, context->partitions) {
e594a3b1
LP
2254
2255 /* Entirely remove partitions that have no configuration */
2256 if (PARTITION_IS_FOREIGN(p)) {
2257 partition_unlink_and_free(context, p);
2258 continue;
2259 }
2260
2261 /* Otherwise drop all data we read off the block device and everything we might have
2262 * calculated based on it */
2263
2264 p->dropped = false;
2265 p->current_size = UINT64_MAX;
2266 p->new_size = UINT64_MAX;
2267 p->current_padding = UINT64_MAX;
2268 p->new_padding = UINT64_MAX;
2269 p->partno = UINT64_MAX;
2270 p->offset = UINT64_MAX;
2271
2272 if (p->current_partition) {
2273 fdisk_unref_partition(p->current_partition);
2274 p->current_partition = NULL;
2275 }
2276
2277 if (p->new_partition) {
2278 fdisk_unref_partition(p->new_partition);
2279 p->new_partition = NULL;
2280 }
2281
2282 p->padding_area = NULL;
2283 p->allocated_to_area = NULL;
2284
15d43e30
LP
2285 p->current_uuid = SD_ID128_NULL;
2286 p->current_label = mfree(p->current_label);
e594a3b1
LP
2287 }
2288
2289 context->start = UINT64_MAX;
2290 context->end = UINT64_MAX;
2291 context->total = UINT64_MAX;
2292
2293 if (context->fdisk_context) {
2294 fdisk_unref_context(context->fdisk_context);
2295 context->fdisk_context = NULL;
2296 }
2297
2298 context_free_free_areas(context);
2299}
2300
2301static int format_size_change(uint64_t from, uint64_t to, char **ret) {
2b59bf51 2302 char *t;
e594a3b1
LP
2303
2304 if (from != UINT64_MAX) {
2305 if (from == to || to == UINT64_MAX)
2b59bf51 2306 t = strdup(FORMAT_BYTES(from));
e594a3b1 2307 else
fc03e80c 2308 t = strjoin(FORMAT_BYTES(from), " ", special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), " ", FORMAT_BYTES(to));
e594a3b1 2309 } else if (to != UINT64_MAX)
fc03e80c 2310 t = strjoin(special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), " ", FORMAT_BYTES(to));
e594a3b1
LP
2311 else {
2312 *ret = NULL;
2313 return 0;
2314 }
2315
2b59bf51 2316 if (!t)
e594a3b1
LP
2317 return log_oom();
2318
2b59bf51 2319 *ret = t;
e594a3b1
LP
2320 return 1;
2321}
2322
2323static const char *partition_label(const Partition *p) {
2324 assert(p);
2325
2326 if (p->new_label)
2327 return p->new_label;
2328
2329 if (p->current_label)
2330 return p->current_label;
2331
22e932f4 2332 return gpt_partition_type_uuid_to_string(p->type.uuid);
e594a3b1
LP
2333}
2334
2335static int context_dump_partitions(Context *context, const char *node) {
2336 _cleanup_(table_unrefp) Table *t = NULL;
2337 uint64_t sum_padding = 0, sum_size = 0;
e594a3b1 2338 int r;
b5b7879a
DDM
2339 const size_t roothash_col = 13, dropin_files_col = 14;
2340 bool has_roothash = false, has_dropin_files = false;
e594a3b1 2341
6a01ea4a 2342 if ((arg_json_format_flags & JSON_FORMAT_OFF) && context->n_partitions == 0) {
a015fbe7
TH
2343 log_info("Empty partition table.");
2344 return 0;
2345 }
2346
b5b7879a 2347 t = table_new("type", "label", "uuid", "file", "node", "offset", "old size", "raw size", "size", "old padding", "raw padding", "padding", "activity", "roothash", "drop-in files");
e594a3b1
LP
2348 if (!t)
2349 return log_oom();
2350
a015fbe7 2351 if (!DEBUG_LOGGING) {
6a01ea4a 2352 if (arg_json_format_flags & JSON_FORMAT_OFF)
a015fbe7 2353 (void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
b5b7879a 2354 (size_t) 8, (size_t) 11, roothash_col, dropin_files_col);
a015fbe7
TH
2355 else
2356 (void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
b5b7879a
DDM
2357 (size_t) 5, (size_t) 6, (size_t) 7, (size_t) 9, (size_t) 10,
2358 (size_t) 12, roothash_col, dropin_files_col);
a015fbe7 2359 }
e594a3b1 2360
e594a3b1 2361 (void) table_set_align_percent(t, table_get_cell(t, 0, 5), 100);
9c07c9ec
LP
2362 (void) table_set_align_percent(t, table_get_cell(t, 0, 6), 100);
2363 (void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
2364 (void) table_set_align_percent(t, table_get_cell(t, 0, 8), 100);
2365 (void) table_set_align_percent(t, table_get_cell(t, 0, 9), 100);
2366 (void) table_set_align_percent(t, table_get_cell(t, 0, 10), 100);
2367 (void) table_set_align_percent(t, table_get_cell(t, 0, 11), 100);
e594a3b1
LP
2368
2369 LIST_FOREACH(partitions, p, context->partitions) {
b5b7879a 2370 _cleanup_free_ char *size_change = NULL, *padding_change = NULL, *partname = NULL, *rh = NULL;
b7416360 2371 char uuid_buffer[SD_ID128_UUID_STRING_MAX];
a015fbe7 2372 const char *label, *activity = NULL;
e594a3b1
LP
2373
2374 if (p->dropped)
2375 continue;
2376
a015fbe7
TH
2377 if (p->current_size == UINT64_MAX)
2378 activity = "create";
2379 else if (p->current_size != p->new_size)
2380 activity = "resize";
2381
e594a3b1
LP
2382 label = partition_label(p);
2383 partname = p->partno != UINT64_MAX ? fdisk_partname(node, p->partno+1) : NULL;
2384
2385 r = format_size_change(p->current_size, p->new_size, &size_change);
2386 if (r < 0)
2387 return r;
2388
2389 r = format_size_change(p->current_padding, p->new_padding, &padding_change);
2390 if (r < 0)
2391 return r;
2392
2393 if (p->new_size != UINT64_MAX)
2394 sum_size += p->new_size;
2395 if (p->new_padding != UINT64_MAX)
2396 sum_padding += p->new_padding;
2397
b5b7879a
DDM
2398 if (p->verity == VERITY_HASH) {
2399 rh = p->roothash ? hexmem(p->roothash, p->roothash_size) : strdup("TBD");
2400 if (!rh)
2401 return log_oom();
2402 }
2403
e594a3b1
LP
2404 r = table_add_many(
2405 t,
22e932f4 2406 TABLE_STRING, gpt_partition_type_uuid_to_string_harder(p->type.uuid, uuid_buffer),
be9ce018 2407 TABLE_STRING, empty_to_null(label) ?: "-", TABLE_SET_COLOR, empty_to_null(label) ? NULL : ansi_grey(),
11749b61 2408 TABLE_UUID, p->new_uuid_is_set ? p->new_uuid : p->current_uuid,
e594a3b1 2409 TABLE_STRING, p->definition_path ? basename(p->definition_path) : "-", TABLE_SET_COLOR, p->definition_path ? NULL : ansi_grey(),
a015fbe7 2410 TABLE_STRING, partname ?: "-", TABLE_SET_COLOR, partname ? NULL : ansi_highlight(),
e594a3b1 2411 TABLE_UINT64, p->offset,
a015fbe7 2412 TABLE_UINT64, p->current_size == UINT64_MAX ? 0 : p->current_size,
e594a3b1
LP
2413 TABLE_UINT64, p->new_size,
2414 TABLE_STRING, size_change, TABLE_SET_COLOR, !p->partitions_next && sum_size > 0 ? ansi_underline() : NULL,
a015fbe7 2415 TABLE_UINT64, p->current_padding == UINT64_MAX ? 0 : p->current_padding,
e594a3b1 2416 TABLE_UINT64, p->new_padding,
a015fbe7 2417 TABLE_STRING, padding_change, TABLE_SET_COLOR, !p->partitions_next && sum_padding > 0 ? ansi_underline() : NULL,
39fc0174 2418 TABLE_STRING, activity ?: "unchanged",
b5b7879a 2419 TABLE_STRING, rh,
39fc0174 2420 TABLE_STRV, p->drop_in_files);
e594a3b1 2421 if (r < 0)
f987a261 2422 return table_log_add_error(r);
39fc0174 2423
b5b7879a 2424 has_roothash = has_roothash || !isempty(rh);
3ab44dbd 2425 has_dropin_files = has_dropin_files || !strv_isempty(p->drop_in_files);
e594a3b1
LP
2426 }
2427
6a01ea4a 2428 if ((arg_json_format_flags & JSON_FORMAT_OFF) && (sum_padding > 0 || sum_size > 0)) {
e594a3b1
LP
2429 const char *a, *b;
2430
2b59bf51
ZJS
2431 a = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_size));
2432 b = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_padding));
e594a3b1
LP
2433
2434 r = table_add_many(
2435 t,
2436 TABLE_EMPTY,
2437 TABLE_EMPTY,
2438 TABLE_EMPTY,
2439 TABLE_EMPTY,
2440 TABLE_EMPTY,
2441 TABLE_EMPTY,
2442 TABLE_EMPTY,
a015fbe7 2443 TABLE_EMPTY,
e594a3b1
LP
2444 TABLE_STRING, a,
2445 TABLE_EMPTY,
a015fbe7
TH
2446 TABLE_EMPTY,
2447 TABLE_STRING, b,
39fc0174 2448 TABLE_EMPTY,
b5b7879a 2449 TABLE_EMPTY,
a015fbe7 2450 TABLE_EMPTY);
e594a3b1 2451 if (r < 0)
f987a261 2452 return table_log_add_error(r);
e594a3b1
LP
2453 }
2454
b5b7879a
DDM
2455 if (!has_roothash) {
2456 r = table_hide_column_from_display(t, roothash_col);
2457 if (r < 0)
2458 return log_error_errno(r, "Failed to set columns to display: %m");
2459 }
2460
3ab44dbd 2461 if (!has_dropin_files) {
39fc0174
RP
2462 r = table_hide_column_from_display(t, dropin_files_col);
2463 if (r < 0)
2464 return log_error_errno(r, "Failed to set columns to display: %m");
2465 }
2466
896e678b 2467 return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
e594a3b1
LP
2468}
2469
2470static void context_bar_char_process_partition(
2471 Context *context,
2472 Partition *bar[],
2473 size_t n,
2474 Partition *p,
2475 size_t *ret_start) {
2476
2477 uint64_t from, to, total;
2478 size_t x, y;
2479
2480 assert(context);
2481 assert(bar);
2482 assert(n > 0);
2483 assert(p);
2484
2485 if (p->dropped)
2486 return;
2487
2488 assert(p->offset != UINT64_MAX);
2489 assert(p->new_size != UINT64_MAX);
2490
2491 from = p->offset;
2492 to = from + p->new_size;
2493
d8daed09
TY
2494 assert(context->total > 0);
2495 total = context->total;
e594a3b1 2496
d8daed09
TY
2497 assert(from <= total);
2498 x = from * n / total;
e594a3b1 2499
d8daed09
TY
2500 assert(to <= total);
2501 y = to * n / total;
e594a3b1
LP
2502
2503 assert(x <= y);
2504 assert(y <= n);
2505
2506 for (size_t i = x; i < y; i++)
2507 bar[i] = p;
2508
2509 *ret_start = x;
2510}
2511
2512static int partition_hint(const Partition *p, const char *node, char **ret) {
2513 _cleanup_free_ char *buf = NULL;
e594a3b1
LP
2514 const char *label;
2515 sd_id128_t id;
2516
2517 /* Tries really hard to find a suitable description for this partition */
2518
2519 if (p->definition_path) {
2520 buf = strdup(basename(p->definition_path));
2521 goto done;
2522 }
2523
2524 label = partition_label(p);
2525 if (!isempty(label)) {
2526 buf = strdup(label);
2527 goto done;
2528 }
2529
2530 if (p->partno != UINT64_MAX) {
2531 buf = fdisk_partname(node, p->partno+1);
2532 goto done;
2533 }
2534
11749b61 2535 if (p->new_uuid_is_set)
e594a3b1
LP
2536 id = p->new_uuid;
2537 else if (!sd_id128_is_null(p->current_uuid))
2538 id = p->current_uuid;
2539 else
22e932f4 2540 id = p->type.uuid;
e594a3b1 2541
b7416360 2542 buf = strdup(SD_ID128_TO_UUID_STRING(id));
e594a3b1
LP
2543
2544done:
2545 if (!buf)
2546 return -ENOMEM;
2547
2548 *ret = TAKE_PTR(buf);
2549 return 0;
2550}
2551
2552static int context_dump_partition_bar(Context *context, const char *node) {
2553 _cleanup_free_ Partition **bar = NULL;
2554 _cleanup_free_ size_t *start_array = NULL;
03677889 2555 Partition *last = NULL;
e594a3b1
LP
2556 bool z = false;
2557 size_t c, j = 0;
2558
f391597c 2559 assert_se((c = columns()) >= 2);
e594a3b1
LP
2560 c -= 2; /* We do not use the leftmost and rightmost character cell */
2561
2562 bar = new0(Partition*, c);
2563 if (!bar)
2564 return log_oom();
2565
2566 start_array = new(size_t, context->n_partitions);
2567 if (!start_array)
2568 return log_oom();
2569
2570 LIST_FOREACH(partitions, p, context->partitions)
2571 context_bar_char_process_partition(context, bar, c, p, start_array + j++);
2572
2573 putc(' ', stdout);
2574
2575 for (size_t i = 0; i < c; i++) {
2576 if (bar[i]) {
2577 if (last != bar[i])
2578 z = !z;
2579
2580 fputs(z ? ansi_green() : ansi_yellow(), stdout);
2581 fputs(special_glyph(SPECIAL_GLYPH_DARK_SHADE), stdout);
2582 } else {
2583 fputs(ansi_normal(), stdout);
2584 fputs(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE), stdout);
2585 }
2586
2587 last = bar[i];
2588 }
2589
2590 fputs(ansi_normal(), stdout);
2591 putc('\n', stdout);
2592
2593 for (size_t i = 0; i < context->n_partitions; i++) {
2594 _cleanup_free_ char **line = NULL;
2595
2596 line = new0(char*, c);
2597 if (!line)
2598 return log_oom();
2599
2600 j = 0;
2601 LIST_FOREACH(partitions, p, context->partitions) {
2602 _cleanup_free_ char *d = NULL;
2603 j++;
2604
2605 if (i < context->n_partitions - j) {
2606
2607 if (line[start_array[j-1]]) {
2608 const char *e;
2609
2610 /* Upgrade final corner to the right with a branch to the right */
2611 e = startswith(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_RIGHT));
2612 if (e) {
2613 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), e);
2614 if (!d)
2615 return log_oom();
2616 }
2617 }
2618
2619 if (!d) {
2620 d = strdup(special_glyph(SPECIAL_GLYPH_TREE_VERTICAL));
2621 if (!d)
2622 return log_oom();
2623 }
2624
2625 } else if (i == context->n_partitions - j) {
2626 _cleanup_free_ char *hint = NULL;
2627
2628 (void) partition_hint(p, node, &hint);
2629
2630 if (streq_ptr(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_VERTICAL)))
2631 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), " ", strna(hint));
2632 else
2633 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_RIGHT), " ", strna(hint));
2634
2635 if (!d)
2636 return log_oom();
2637 }
2638
2639 if (d)
2640 free_and_replace(line[start_array[j-1]], d);
2641 }
2642
2643 putc(' ', stdout);
2644
2645 j = 0;
2646 while (j < c) {
2647 if (line[j]) {
2648 fputs(line[j], stdout);
2649 j += utf8_console_width(line[j]);
2650 } else {
2651 putc(' ', stdout);
2652 j++;
2653 }
2654 }
2655
2656 putc('\n', stdout);
2657
2658 for (j = 0; j < c; j++)
2659 free(line[j]);
2660 }
2661
2662 return 0;
2663}
2664
b5b7879a
DDM
2665static bool context_has_roothash(Context *context) {
2666 LIST_FOREACH(partitions, p, context->partitions)
2667 if (p->roothash)
2668 return true;
2669
2670 return false;
2671}
2672
2673static int context_dump(Context *context, const char *node, bool late) {
a26d463d
DDM
2674 int r;
2675
2676 assert(context);
2677 assert(node);
2678
2679 if (arg_pretty == 0 && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
2680 return 0;
2681
b5b7879a
DDM
2682 /* If we're outputting JSON, only dump after doing all operations so we can include the roothashes
2683 * in the output. */
2684 if (!late && !FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
2685 return 0;
2686
2687 /* If we're not outputting JSON, only dump again after doing all operations if there are any
2688 * roothashes that we need to communicate to the user. */
2689 if (late && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && !context_has_roothash(context))
2690 return 0;
2691
a26d463d
DDM
2692 r = context_dump_partitions(context, node);
2693 if (r < 0)
2694 return r;
2695
b5b7879a
DDM
2696 /* Make sure we only write the partition bar once, even if we're writing the partition table twice to
2697 * communicate roothashes. */
2698 if (FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF) && !late) {
a26d463d
DDM
2699 putc('\n', stdout);
2700
2701 r = context_dump_partition_bar(context, node);
2702 if (r < 0)
2703 return r;
2704
2705 putc('\n', stdout);
2706 }
2707
2708 fflush(stdout);
2709
2710 return 0;
2711}
2712
2713
e594a3b1 2714static bool context_changed(const Context *context) {
03677889 2715 assert(context);
e594a3b1
LP
2716
2717 LIST_FOREACH(partitions, p, context->partitions) {
2718 if (p->dropped)
2719 continue;
2720
2721 if (p->allocated_to_area)
2722 return true;
2723
2724 if (p->new_size != p->current_size)
2725 return true;
2726 }
2727
2728 return false;
2729}
2730
81873a6b 2731static int context_wipe_range(Context *context, uint64_t offset, uint64_t size) {
e594a3b1
LP
2732 _cleanup_(blkid_free_probep) blkid_probe probe = NULL;
2733 int r;
2734
2735 assert(context);
81873a6b
LP
2736 assert(offset != UINT64_MAX);
2737 assert(size != UINT64_MAX);
e594a3b1
LP
2738
2739 probe = blkid_new_probe();
2740 if (!probe)
2741 return log_oom();
2742
e594a3b1 2743 errno = 0;
81873a6b 2744 r = blkid_probe_set_device(probe, fdisk_get_devfd(context->fdisk_context), offset, size);
e594a3b1 2745 if (r < 0)
81873a6b 2746 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to allocate device probe for wiping.");
e594a3b1
LP
2747
2748 errno = 0;
2749 if (blkid_probe_enable_superblocks(probe, true) < 0 ||
2750 blkid_probe_set_superblocks_flags(probe, BLKID_SUBLKS_MAGIC|BLKID_SUBLKS_BADCSUM) < 0 ||
2751 blkid_probe_enable_partitions(probe, true) < 0 ||
2752 blkid_probe_set_partitions_flags(probe, BLKID_PARTS_MAGIC) < 0)
81873a6b 2753 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to enable superblock and partition probing.");
e594a3b1
LP
2754
2755 for (;;) {
2756 errno = 0;
2757 r = blkid_do_probe(probe);
2758 if (r < 0)
2759 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe for file systems.");
2760 if (r > 0)
2761 break;
2762
2763 errno = 0;
2764 if (blkid_do_wipe(probe, false) < 0)
2765 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to wipe file system signature.");
2766 }
2767
e594a3b1
LP
2768 return 0;
2769}
2770
81873a6b
LP
2771static int context_wipe_partition(Context *context, Partition *p) {
2772 int r;
2773
2774 assert(context);
2775 assert(p);
2776 assert(!PARTITION_EXISTS(p)); /* Safety check: never wipe existing partitions */
2777
2778 assert(p->offset != UINT64_MAX);
2779 assert(p->new_size != UINT64_MAX);
2780
2781 r = context_wipe_range(context, p->offset, p->new_size);
2782 if (r < 0)
2783 return r;
2784
2785 log_info("Successfully wiped file system signatures from future partition %" PRIu64 ".", p->partno);
2786 return 0;
2787}
2788
2789static int context_discard_range(
2790 Context *context,
2791 uint64_t offset,
2792 uint64_t size) {
2793
e594a3b1
LP
2794 struct stat st;
2795 int fd;
2796
2797 assert(context);
2798 assert(offset != UINT64_MAX);
2799 assert(size != UINT64_MAX);
2800
2801 if (size <= 0)
2802 return 0;
2803
a26f4a49 2804 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
e594a3b1
LP
2805
2806 if (fstat(fd, &st) < 0)
2807 return -errno;
2808
2809 if (S_ISREG(st.st_mode)) {
2810 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, offset, size) < 0) {
2811 if (ERRNO_IS_NOT_SUPPORTED(errno))
2812 return -EOPNOTSUPP;
2813
2814 return -errno;
2815 }
2816
2817 return 1;
2818 }
2819
2820 if (S_ISBLK(st.st_mode)) {
2821 uint64_t range[2], end;
2822
994b3031 2823 range[0] = round_up_size(offset, context->sector_size);
e594a3b1 2824
55d38014
LP
2825 if (offset > UINT64_MAX - size)
2826 return -ERANGE;
2827
e594a3b1
LP
2828 end = offset + size;
2829 if (end <= range[0])
2830 return 0;
2831
994b3031 2832 range[1] = round_down_size(end - range[0], context->sector_size);
e594a3b1
LP
2833 if (range[1] <= 0)
2834 return 0;
2835
2836 if (ioctl(fd, BLKDISCARD, range) < 0) {
2837 if (ERRNO_IS_NOT_SUPPORTED(errno))
2838 return -EOPNOTSUPP;
2839
2840 return -errno;
2841 }
2842
2843 return 1;
2844 }
2845
2846 return -EOPNOTSUPP;
2847}
2848
2849static int context_discard_partition(Context *context, Partition *p) {
2850 int r;
2851
2852 assert(context);
2853 assert(p);
2854
2855 assert(p->offset != UINT64_MAX);
2856 assert(p->new_size != UINT64_MAX);
2857 assert(!PARTITION_EXISTS(p)); /* Safety check: never discard existing partitions */
2858
2859 if (!arg_discard)
2860 return 0;
2861
2862 r = context_discard_range(context, p->offset, p->new_size);
2863 if (r == -EOPNOTSUPP) {
5b5109e2 2864 log_info("Storage does not support discard, not discarding data in future partition %" PRIu64 ".", p->partno);
e594a3b1
LP
2865 return 0;
2866 }
22163eb5
LP
2867 if (r == -EBUSY) {
2868 /* Let's handle this gracefully: https://bugzilla.kernel.org/show_bug.cgi?id=211167 */
2869 log_info("Block device is busy, not discarding partition %" PRIu64 " because it probably is mounted.", p->partno);
2870 return 0;
2871 }
e594a3b1
LP
2872 if (r == 0) {
2873 log_info("Partition %" PRIu64 " too short for discard, skipping.", p->partno);
2874 return 0;
2875 }
2876 if (r < 0)
5b5109e2 2877 return log_error_errno(r, "Failed to discard data for future partition %" PRIu64 ".", p->partno);
e594a3b1 2878
5b5109e2 2879 log_info("Successfully discarded data from future partition %" PRIu64 ".", p->partno);
e594a3b1
LP
2880 return 1;
2881}
2882
2883static int context_discard_gap_after(Context *context, Partition *p) {
2884 uint64_t gap, next = UINT64_MAX;
e594a3b1
LP
2885 int r;
2886
2887 assert(context);
2888 assert(!p || (p->offset != UINT64_MAX && p->new_size != UINT64_MAX));
2889
5113436b
AF
2890 if (!arg_discard)
2891 return 0;
2892
e594a3b1
LP
2893 if (p)
2894 gap = p->offset + p->new_size;
2895 else
2896 gap = context->start;
2897
2898 LIST_FOREACH(partitions, q, context->partitions) {
2899 if (q->dropped)
2900 continue;
2901
2902 assert(q->offset != UINT64_MAX);
2903 assert(q->new_size != UINT64_MAX);
2904
2905 if (q->offset < gap)
2906 continue;
2907
2908 if (next == UINT64_MAX || q->offset < next)
2909 next = q->offset;
2910 }
2911
2912 if (next == UINT64_MAX) {
2913 next = context->end;
2914 if (gap > next)
2915 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end.");
2916 }
2917
2918 assert(next >= gap);
2919 r = context_discard_range(context, gap, next - gap);
2920 if (r == -EOPNOTSUPP) {
2921 if (p)
5b5109e2 2922 log_info("Storage does not support discard, not discarding gap after partition %" PRIu64 ".", p->partno);
e594a3b1 2923 else
5b5109e2 2924 log_info("Storage does not support discard, not discarding gap at beginning of disk.");
e594a3b1
LP
2925 return 0;
2926 }
2927 if (r == 0) /* Too short */
2928 return 0;
2929 if (r < 0) {
2930 if (p)
2931 return log_error_errno(r, "Failed to discard gap after partition %" PRIu64 ".", p->partno);
2932 else
2933 return log_error_errno(r, "Failed to discard gap at beginning of disk.");
2934 }
2935
2936 if (p)
2937 log_info("Successfully discarded gap after partition %" PRIu64 ".", p->partno);
2938 else
2939 log_info("Successfully discarded gap at beginning of disk.");
2940
2941 return 0;
2942}
2943
2944static int context_wipe_and_discard(Context *context, bool from_scratch) {
e594a3b1
LP
2945 int r;
2946
2947 assert(context);
2948
2949 /* Wipe and discard the contents of all partitions we are about to create. We skip the discarding if
2950 * we were supposed to start from scratch anyway, as in that case we just discard the whole block
2951 * device in one go early on. */
2952
2953 LIST_FOREACH(partitions, p, context->partitions) {
2954
2955 if (!p->allocated_to_area)
2956 continue;
2957
81d1098b
DDM
2958 if (partition_skip(p))
2959 continue;
2960
e594a3b1
LP
2961 r = context_wipe_partition(context, p);
2962 if (r < 0)
2963 return r;
2964
2965 if (!from_scratch) {
f0cb1b95
LP
2966 r = context_discard_partition(context, p);
2967 if (r < 0)
2968 return r;
2969
e594a3b1
LP
2970 r = context_discard_gap_after(context, p);
2971 if (r < 0)
2972 return r;
2973 }
2974 }
2975
2976 if (!from_scratch) {
2977 r = context_discard_gap_after(context, NULL);
2978 if (r < 0)
2979 return r;
2980 }
2981
2982 return 0;
2983}
2984
b9df3536 2985static int partition_encrypt(
994b3031 2986 Context *context,
b9df3536
LP
2987 Partition *p,
2988 const char *node,
2989 struct crypt_device **ret_cd,
2990 char **ret_volume,
2991 int *ret_fd) {
3dd8ae5c 2992#if HAVE_LIBCRYPTSETUP
0d12936d 2993 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
b9df3536
LP
2994 _cleanup_(erase_and_freep) void *volume_key = NULL;
2995 _cleanup_free_ char *dm_name = NULL, *vol = NULL;
b9df3536
LP
2996 size_t volume_key_size = 256 / 8;
2997 sd_id128_t uuid;
2998 int r;
2999
994b3031 3000 assert(context);
b9df3536 3001 assert(p);
889914ef
LP
3002 assert(p->encrypt != ENCRYPT_OFF);
3003
3004 log_debug("Encryption mode for partition %" PRIu64 ": %s", p->partno, encrypt_mode_to_string(p->encrypt));
b9df3536 3005
0d12936d
LP
3006 r = dlopen_cryptsetup();
3007 if (r < 0)
3008 return log_error_errno(r, "libcryptsetup not found, cannot encrypt: %m");
3009
b9df3536
LP
3010 if (asprintf(&dm_name, "luks-repart-%08" PRIx64, random_u64()) < 0)
3011 return log_oom();
3012
3013 if (ret_volume) {
3014 vol = path_join("/dev/mapper/", dm_name);
3015 if (!vol)
3016 return log_oom();
3017 }
3018
3019 r = derive_uuid(p->new_uuid, "luks-uuid", &uuid);
3020 if (r < 0)
3021 return r;
3022
3023 log_info("Encrypting future partition %" PRIu64 "...", p->partno);
3024
3025 volume_key = malloc(volume_key_size);
3026 if (!volume_key)
3027 return log_oom();
3028
87cb1ab6 3029 r = crypto_random_bytes(volume_key, volume_key_size);
b9df3536
LP
3030 if (r < 0)
3031 return log_error_errno(r, "Failed to generate volume key: %m");
3032
0d12936d 3033 r = sym_crypt_init(&cd, node);
b9df3536
LP
3034 if (r < 0)
3035 return log_error_errno(r, "Failed to allocate libcryptsetup context: %m");
3036
3037 cryptsetup_enable_logging(cd);
3038
0d12936d 3039 r = sym_crypt_format(cd,
b9df3536
LP
3040 CRYPT_LUKS2,
3041 "aes",
3042 "xts-plain64",
b7416360 3043 SD_ID128_TO_UUID_STRING(uuid),
b9df3536
LP
3044 volume_key,
3045 volume_key_size,
3046 &(struct crypt_params_luks2) {
be9ce018 3047 .label = strempty(p->new_label),
994b3031 3048 .sector_size = context->sector_size,
b9df3536
LP
3049 });
3050 if (r < 0)
3051 return log_error_errno(r, "Failed to LUKS2 format future partition: %m");
3052
889914ef
LP
3053 if (IN_SET(p->encrypt, ENCRYPT_KEY_FILE, ENCRYPT_KEY_FILE_TPM2)) {
3054 r = sym_crypt_keyslot_add_by_volume_key(
3055 cd,
3056 CRYPT_ANY_SLOT,
3057 volume_key,
3058 volume_key_size,
3059 strempty(arg_key),
3060 arg_key_size);
3061 if (r < 0)
3062 return log_error_errno(r, "Failed to add LUKS2 key: %m");
3063 }
3064
3065 if (IN_SET(p->encrypt, ENCRYPT_TPM2, ENCRYPT_KEY_FILE_TPM2)) {
3066#if HAVE_TPM2
3067 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
3068 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
3069 _cleanup_(erase_and_freep) void *secret = NULL;
02ef97cd 3070 _cleanup_free_ void *pubkey = NULL;
889914ef 3071 _cleanup_free_ void *blob = NULL, *hash = NULL;
02ef97cd 3072 size_t secret_size, blob_size, hash_size, pubkey_size = 0;
2b92a672 3073 uint16_t pcr_bank, primary_alg;
889914ef
LP
3074 int keyslot;
3075
02ef97cd
LP
3076 if (arg_tpm2_public_key_pcr_mask != 0) {
3077 r = tpm2_load_pcr_public_key(arg_tpm2_public_key, &pubkey, &pubkey_size);
3078 if (r < 0) {
3079 if (arg_tpm2_public_key || r != -ENOENT)
3080 return log_error_errno(r, "Failed read TPM PCR public key: %m");
3081
3082 log_debug_errno(r, "Failed to read TPM2 PCR public key, proceeding without: %m");
3083 arg_tpm2_public_key_pcr_mask = 0;
3084 }
3085 }
3086
d9b5841d
LP
3087 r = tpm2_seal(arg_tpm2_device,
3088 arg_tpm2_pcr_mask,
02ef97cd
LP
3089 pubkey, pubkey_size,
3090 arg_tpm2_public_key_pcr_mask,
d9b5841d
LP
3091 /* pin= */ NULL,
3092 &secret, &secret_size,
3093 &blob, &blob_size,
3094 &hash, &hash_size,
3095 &pcr_bank,
3096 &primary_alg);
889914ef
LP
3097 if (r < 0)
3098 return log_error_errno(r, "Failed to seal to TPM2: %m");
3099
3100 r = base64mem(secret, secret_size, &base64_encoded);
3101 if (r < 0)
3102 return log_error_errno(r, "Failed to base64 encode secret key: %m");
3103
3104 r = cryptsetup_set_minimal_pbkdf(cd);
3105 if (r < 0)
3106 return log_error_errno(r, "Failed to set minimal PBKDF: %m");
3107
3108 keyslot = sym_crypt_keyslot_add_by_volume_key(
3109 cd,
3110 CRYPT_ANY_SLOT,
3111 volume_key,
3112 volume_key_size,
3113 base64_encoded,
3114 strlen(base64_encoded));
3115 if (keyslot < 0)
3116 return log_error_errno(keyslot, "Failed to add new TPM2 key to %s: %m", node);
3117
f0f4fcae
LP
3118 r = tpm2_make_luks2_json(
3119 keyslot,
3120 arg_tpm2_pcr_mask,
3121 pcr_bank,
02ef97cd
LP
3122 pubkey, pubkey_size,
3123 arg_tpm2_public_key_pcr_mask,
f0f4fcae
LP
3124 primary_alg,
3125 blob, blob_size,
3126 hash, hash_size,
3127 0,
3128 &v);
889914ef
LP
3129 if (r < 0)
3130 return log_error_errno(r, "Failed to prepare TPM2 JSON token object: %m");
3131
3132 r = cryptsetup_add_token_json(cd, v);
3133 if (r < 0)
3134 return log_error_errno(r, "Failed to add TPM2 JSON token to LUKS2 header: %m");
3135#else
3136 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
3137 "Support for TPM2 enrollment not enabled.");
3138#endif
3139 }
b9df3536 3140
0d12936d 3141 r = sym_crypt_activate_by_volume_key(
b9df3536
LP
3142 cd,
3143 dm_name,
3144 volume_key,
3145 volume_key_size,
3146 arg_discard ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0);
3147 if (r < 0)
3148 return log_error_errno(r, "Failed to activate LUKS superblock: %m");
3149
3150 log_info("Successfully encrypted future partition %" PRIu64 ".", p->partno);
3151
3152 if (ret_fd) {
3153 _cleanup_close_ int dev_fd = -1;
3154
3155 dev_fd = open(vol, O_RDWR|O_CLOEXEC|O_NOCTTY);
3156 if (dev_fd < 0)
3157 return log_error_errno(errno, "Failed to open LUKS volume '%s': %m", vol);
3158
3159 *ret_fd = TAKE_FD(dev_fd);
3160 }
3161
3162 if (ret_cd)
3163 *ret_cd = TAKE_PTR(cd);
3164 if (ret_volume)
3165 *ret_volume = TAKE_PTR(vol);
3166
3167 return 0;
3dd8ae5c 3168#else
3169 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libcryptsetup is not supported, cannot encrypt: %m");
3170#endif
b9df3536
LP
3171}
3172
3173static int deactivate_luks(struct crypt_device *cd, const char *node) {
3dd8ae5c 3174#if HAVE_LIBCRYPTSETUP
b9df3536
LP
3175 int r;
3176
3177 if (!cd)
3178 return 0;
3179
3180 assert(node);
3181
3182 /* udev or so might access out block device in the background while we are done. Let's hence force
3183 * detach the volume. We sync'ed before, hence this should be safe. */
3184
0d12936d 3185 r = sym_crypt_deactivate_by_name(cd, basename(node), CRYPT_DEACTIVATE_FORCE);
b9df3536
LP
3186 if (r < 0)
3187 return log_error_errno(r, "Failed to deactivate LUKS device: %m");
3188
3189 return 1;
3dd8ae5c 3190#else
3191 return 0;
3192#endif
b9df3536
LP
3193}
3194
757bc2e4 3195static int context_copy_blocks(Context *context) {
b9df3536 3196 int whole_fd = -1, r;
757bc2e4
LP
3197
3198 assert(context);
3199
3200 /* Copy in file systems on the block level */
3201
3202 LIST_FOREACH(partitions, p, context->partitions) {
0d12936d 3203 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
b9df3536
LP
3204 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
3205 _cleanup_free_ char *encrypted = NULL;
3206 _cleanup_close_ int encrypted_dev_fd = -1;
b9df3536 3207 int target_fd;
757bc2e4
LP
3208
3209 if (p->copy_blocks_fd < 0)
3210 continue;
3211
3212 if (p->dropped)
3213 continue;
3214
3215 if (PARTITION_EXISTS(p)) /* Never copy over existing partitions */
3216 continue;
3217
81d1098b
DDM
3218 if (partition_skip(p))
3219 continue;
3220
757bc2e4
LP
3221 assert(p->new_size != UINT64_MAX);
3222 assert(p->copy_blocks_size != UINT64_MAX);
3223 assert(p->new_size >= p->copy_blocks_size);
3224
b9df3536
LP
3225 if (whole_fd < 0)
3226 assert_se((whole_fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
3227
889914ef 3228 if (p->encrypt != ENCRYPT_OFF) {
fd83c98e 3229 r = loop_device_make(whole_fd, O_RDWR, p->offset, p->new_size, 0, 0, LOCK_EX, &d);
b9df3536
LP
3230 if (r < 0)
3231 return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno);
3232
994b3031 3233 r = partition_encrypt(context, p, d->node, &cd, &encrypted, &encrypted_dev_fd);
b9df3536
LP
3234 if (r < 0)
3235 return log_error_errno(r, "Failed to encrypt device: %m");
757bc2e4 3236
b9df3536
LP
3237 if (flock(encrypted_dev_fd, LOCK_EX) < 0)
3238 return log_error_errno(errno, "Failed to lock LUKS device: %m");
3239
3240 target_fd = encrypted_dev_fd;
695cfd53 3241 } else {
b9df3536
LP
3242 if (lseek(whole_fd, p->offset, SEEK_SET) == (off_t) -1)
3243 return log_error_errno(errno, "Failed to seek to partition offset: %m");
3244
3245 target_fd = whole_fd;
3246 }
757bc2e4 3247
2b59bf51
ZJS
3248 log_info("Copying in '%s' (%s) on block level into future partition %" PRIu64 ".",
3249 p->copy_blocks_path, FORMAT_BYTES(p->copy_blocks_size), p->partno);
757bc2e4 3250
8c467ee8 3251 r = copy_bytes(p->copy_blocks_fd, target_fd, p->copy_blocks_size, COPY_REFLINK);
757bc2e4
LP
3252 if (r < 0)
3253 return log_error_errno(r, "Failed to copy in data from '%s': %m", p->copy_blocks_path);
3254
b9df3536 3255 if (fsync(target_fd) < 0)
8ac04a65 3256 return log_error_errno(errno, "Failed to synchronize copied data blocks: %m");
b9df3536 3257
889914ef 3258 if (p->encrypt != ENCRYPT_OFF) {
b9df3536
LP
3259 encrypted_dev_fd = safe_close(encrypted_dev_fd);
3260
3261 r = deactivate_luks(cd, encrypted);
3262 if (r < 0)
3263 return r;
3264
0d12936d 3265 sym_crypt_free(cd);
b9df3536
LP
3266 cd = NULL;
3267
3268 r = loop_device_sync(d);
3269 if (r < 0)
3270 return log_error_errno(r, "Failed to sync loopback device: %m");
3271 }
3272
757bc2e4
LP
3273 log_info("Copying in of '%s' on block level completed.", p->copy_blocks_path);
3274 }
3275
3276 return 0;
3277}
3278
c0fad2d9 3279static int do_copy_files(Partition *p, const char *root, const Set *denylist) {
8a794850
LP
3280 int r;
3281
3282 assert(p);
92cd7e7c 3283 assert(root);
8a794850
LP
3284
3285 STRV_FOREACH_PAIR(source, target, p->copy_files) {
3286 _cleanup_close_ int sfd = -1, pfd = -1, tfd = -1;
8a794850 3287
fd1ca01a 3288 sfd = chase_symlinks_and_open(*source, arg_root, CHASE_PREFIX_ROOT, O_CLOEXEC|O_NOCTTY, NULL);
8a794850
LP
3289 if (sfd < 0)
3290 return log_error_errno(sfd, "Failed to open source file '%s%s': %m", strempty(arg_root), *source);
3291
3292 r = fd_verify_regular(sfd);
3293 if (r < 0) {
3294 if (r != -EISDIR)
3295 return log_error_errno(r, "Failed to check type of source file '%s': %m", *source);
3296
3297 /* We are looking at a directory */
fd1ca01a 3298 tfd = chase_symlinks_and_open(*target, root, CHASE_PREFIX_ROOT, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
8a794850 3299 if (tfd < 0) {
f21a3a82
LP
3300 _cleanup_free_ char *dn = NULL, *fn = NULL;
3301
8a794850
LP
3302 if (tfd != -ENOENT)
3303 return log_error_errno(tfd, "Failed to open target directory '%s': %m", *target);
3304
f21a3a82
LP
3305 r = path_extract_filename(*target, &fn);
3306 if (r < 0)
3307 return log_error_errno(r, "Failed to extract filename from '%s': %m", *target);
3308
3309 r = path_extract_directory(*target, &dn);
3310 if (r < 0)
3311 return log_error_errno(r, "Failed to extract directory from '%s': %m", *target);
3312
92cd7e7c 3313 r = mkdir_p_root(root, dn, UID_INVALID, GID_INVALID, 0755);
8a794850
LP
3314 if (r < 0)
3315 return log_error_errno(r, "Failed to create parent directory '%s': %m", dn);
3316
fd1ca01a 3317 pfd = chase_symlinks_and_open(dn, root, CHASE_PREFIX_ROOT, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
8a794850
LP
3318 if (pfd < 0)
3319 return log_error_errno(pfd, "Failed to open parent directory of target: %m");
3320
652d9040
LP
3321 r = copy_tree_at(
3322 sfd, ".",
6020d00d 3323 pfd, fn,
652d9040 3324 UID_INVALID, GID_INVALID,
81427d0f 3325 COPY_REFLINK|COPY_HOLES|COPY_MERGE|COPY_REPLACE|COPY_SIGINT|COPY_HARDLINKS|COPY_ALL_XATTRS,
c0fad2d9 3326 denylist);
8a794850 3327 } else
652d9040
LP
3328 r = copy_tree_at(
3329 sfd, ".",
3330 tfd, ".",
3331 UID_INVALID, GID_INVALID,
81427d0f 3332 COPY_REFLINK|COPY_HOLES|COPY_MERGE|COPY_REPLACE|COPY_SIGINT|COPY_HARDLINKS|COPY_ALL_XATTRS,
c0fad2d9 3333 denylist);
8a794850 3334 if (r < 0)
554a2b64 3335 return log_error_errno(r, "Failed to copy '%s' to '%s%s': %m", *source, strempty(arg_root), *target);
8a794850 3336 } else {
f21a3a82
LP
3337 _cleanup_free_ char *dn = NULL, *fn = NULL;
3338
8a794850
LP
3339 /* We are looking at a regular file */
3340
f21a3a82
LP
3341 r = path_extract_filename(*target, &fn);
3342 if (r == -EADDRNOTAVAIL || r == O_DIRECTORY)
3343 return log_error_errno(SYNTHETIC_ERRNO(EISDIR),
3344 "Target path '%s' refers to a directory, but source path '%s' refers to regular file, can't copy.", *target, *source);
3345 if (r < 0)
3346 return log_error_errno(r, "Failed to extract filename from '%s': %m", *target);
3347
3348 r = path_extract_directory(*target, &dn);
3349 if (r < 0)
3350 return log_error_errno(r, "Failed to extract directory from '%s': %m", *target);
3351
92cd7e7c 3352 r = mkdir_p_root(root, dn, UID_INVALID, GID_INVALID, 0755);
8a794850
LP
3353 if (r < 0)
3354 return log_error_errno(r, "Failed to create parent directory: %m");
3355
fd1ca01a 3356 pfd = chase_symlinks_and_open(dn, root, CHASE_PREFIX_ROOT, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
8a794850 3357 if (pfd < 0)
a0ff9971 3358 return log_error_errno(pfd, "Failed to open parent directory of target: %m");
8a794850 3359
e2819067 3360 tfd = openat(pfd, fn, O_CREAT|O_EXCL|O_WRONLY|O_CLOEXEC, 0700);
8a794850
LP
3361 if (tfd < 0)
3362 return log_error_errno(errno, "Failed to create target file '%s': %m", *target);
3363
81427d0f 3364 r = copy_bytes(sfd, tfd, UINT64_MAX, COPY_REFLINK|COPY_HOLES|COPY_SIGINT);
8a794850 3365 if (r < 0)
554a2b64 3366 return log_error_errno(r, "Failed to copy '%s' to '%s%s': %m", *source, strempty(arg_root), *target);
8a794850 3367
23e026de 3368 (void) copy_xattr(sfd, tfd, COPY_ALL_XATTRS);
8a794850
LP
3369 (void) copy_access(sfd, tfd);
3370 (void) copy_times(sfd, tfd, 0);
3371 }
3372 }
3373
3374 return 0;
3375}
3376
92cd7e7c 3377static int do_make_directories(Partition *p, const char *root) {
d83d8048
LP
3378 int r;
3379
3380 assert(p);
92cd7e7c 3381 assert(root);
d83d8048
LP
3382
3383 STRV_FOREACH(d, p->make_directories) {
3384
92cd7e7c 3385 r = mkdir_p_root(root, *d, UID_INVALID, GID_INVALID, 0755);
d83d8048
LP
3386 if (r < 0)
3387 return log_error_errno(r, "Failed to create directory '%s' in file system: %m", *d);
3388 }
3389
3390 return 0;
3391}
3392
c0fad2d9 3393static int partition_populate_directory(Partition *p, const Set *denylist, char **ret_root, char **ret_tmp_root) {
95bfd3cd
DDM
3394 _cleanup_(rm_rf_physical_and_freep) char *root = NULL;
3395 int r;
3396
3397 assert(ret_root);
3398 assert(ret_tmp_root);
3399
95bfd3cd
DDM
3400 /* If we only have a single directory that's meant to become the root directory of the filesystem,
3401 * we can shortcut this function and just use that directory as the root directory instead. If we
3402 * allocate a temporary directory, it's stored in "ret_tmp_root" to indicate it should be removed.
3403 * Otherwise, we return the directory to use in "root" to indicate it should not be removed. */
3404
c0fad2d9
DDM
3405 if (strv_length(p->copy_files) == 2 && strv_length(p->make_directories) == 0 &&
3406 streq(p->copy_files[1], "/") && set_isempty(denylist)) {
95bfd3cd
DDM
3407 _cleanup_free_ char *s = NULL;
3408
fd1ca01a 3409 r = chase_symlinks(p->copy_files[0], arg_root, CHASE_PREFIX_ROOT, &s, NULL);
898db9f3
DDM
3410 if (r < 0)
3411 return log_error_errno(r, "Failed to resolve source '%s%s': %m", strempty(arg_root), p->copy_files[0]);
95bfd3cd
DDM
3412
3413 *ret_root = TAKE_PTR(s);
3414 *ret_tmp_root = NULL;
3415 return 0;
3416 }
3417
3418 r = mkdtemp_malloc("/var/tmp/repart-XXXXXX", &root);
3419 if (r < 0)
3420 return log_error_errno(r, "Failed to create temporary directory: %m");
3421
c0fad2d9 3422 r = do_copy_files(p, root, denylist);
95bfd3cd
DDM
3423 if (r < 0)
3424 return r;
3425
3426 r = do_make_directories(p, root);
3427 if (r < 0)
3428 return r;
3429
3430 *ret_root = NULL;
3431 *ret_tmp_root = TAKE_PTR(root);
3432 return 0;
3433}
3434
c0fad2d9 3435static int partition_populate_filesystem(Partition *p, const char *node, const Set *denylist) {
7c175152
DDM
3436 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
3437 struct stat st;
8a794850
LP
3438 int r;
3439
3440 assert(p);
3441 assert(node);
3442
d83d8048 3443 if (strv_isempty(p->copy_files) && strv_isempty(p->make_directories))
8a794850
LP
3444 return 0;
3445
7c175152
DDM
3446 if (stat(node, &st) < 0)
3447 return log_error_errno(errno, "Failed to stat %s: %m", node);
3448
3449 if (!S_ISBLK(st.st_mode)) {
3450 r = loop_device_make_by_path(node, O_RDWR, 0, LOCK_EX, &d);
3451 if (r < 0)
3452 return log_error_errno(r, "Failed to make loopback device of %s: %m", node);
3453
3454 node = d->node;
3455 }
3456
a7f1f7d8 3457 log_info("Populating %s filesystem with files.", p->format);
8a794850
LP
3458
3459 /* We copy in a child process, since we have to mount the fs for that, and we don't want that fs to
3460 * appear in the host namespace. Hence we fork a child that has its own file system namespace and
3461 * detached mount propagation. */
3462
3463 r = safe_fork("(sd-copy)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, NULL);
3464 if (r < 0)
3465 return r;
3466 if (r == 0) {
3467 static const char fs[] = "/run/systemd/mount-root";
3468 /* This is a child process with its own mount namespace and propagation to host turned off */
3469
3470 r = mkdir_p(fs, 0700);
3471 if (r < 0) {
3472 log_error_errno(r, "Failed to create mount point: %m");
3473 _exit(EXIT_FAILURE);
3474 }
3475
511a8cfe 3476 if (mount_nofollow_verbose(LOG_ERR, node, fs, p->format, MS_NOATIME|MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL) < 0)
8a794850
LP
3477 _exit(EXIT_FAILURE);
3478
c0fad2d9 3479 if (do_copy_files(p, fs, denylist) < 0)
8a794850
LP
3480 _exit(EXIT_FAILURE);
3481
d83d8048
LP
3482 if (do_make_directories(p, fs) < 0)
3483 _exit(EXIT_FAILURE);
3484
8a794850
LP
3485 r = syncfs_path(AT_FDCWD, fs);
3486 if (r < 0) {
3487 log_error_errno(r, "Failed to synchronize written files: %m");
3488 _exit(EXIT_FAILURE);
3489 }
3490
3491 _exit(EXIT_SUCCESS);
3492 }
3493
a7f1f7d8 3494 log_info("Successfully populated %s filesystem with files.", p->format);
8a794850
LP
3495 return 0;
3496}
3497
c0fad2d9
DDM
3498static int make_copy_files_denylist(Context *context, Set **ret) {
3499 _cleanup_set_free_ Set *denylist = NULL;
3500 int r;
3501
3502 assert(context);
3503 assert(ret);
3504
3505 LIST_FOREACH(partitions, p, context->partitions) {
22e932f4 3506 const char *sources = gpt_partition_type_mountpoint_nulstr(p->type);
c0fad2d9
DDM
3507 if (!sources)
3508 continue;
3509
3510 NULSTR_FOREACH(s, sources) {
3511 _cleanup_free_ char *d = NULL;
3512 struct stat st;
3513
3514 r = chase_symlinks_and_stat(s, arg_root, CHASE_PREFIX_ROOT, NULL, &st, NULL);
3515 if (r == -ENOENT)
3516 continue;
3517 if (r < 0)
3518 return log_error_errno(r, "Failed to stat source file '%s%s': %m",
3519 strempty(arg_root), s);
3520
3521 if (set_contains(denylist, &st))
3522 continue;
3523
3524 d = memdup(&st, sizeof(st));
3525 if (!d)
3526 return log_oom();
3527 if (set_ensure_put(&denylist, &inode_hash_ops, d) < 0)
3528 return log_oom();
3529
3530 TAKE_PTR(d);
3531 }
3532 }
3533
3534 *ret = TAKE_PTR(denylist);
3535 return 0;
3536}
3537
53171c04 3538static int context_mkfs(Context *context) {
c0fad2d9 3539 _cleanup_set_free_ Set *denylist = NULL;
53171c04
LP
3540 int fd = -1, r;
3541
3542 assert(context);
3543
3544 /* Make a file system */
3545
c0fad2d9
DDM
3546 r = make_copy_files_denylist(context, &denylist);
3547 if (r < 0)
3548 return r;
3549
53171c04 3550 LIST_FOREACH(partitions, p, context->partitions) {
0d12936d 3551 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
53171c04 3552 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
95bfd3cd
DDM
3553 _cleanup_(rm_rf_physical_and_freep) char *tmp_root = NULL;
3554 _cleanup_free_ char *encrypted = NULL, *root = NULL;
b9df3536
LP
3555 _cleanup_close_ int encrypted_dev_fd = -1;
3556 const char *fsdev;
53171c04
LP
3557
3558 if (p->dropped)
3559 continue;
3560
3561 if (PARTITION_EXISTS(p)) /* Never format existing partitions */
3562 continue;
3563
3564 if (!p->format)
3565 continue;
3566
c4a87b76
DDM
3567 /* Minimized partitions will use the copy blocks logic so let's make sure to skip those here. */
3568 if (p->copy_blocks_fd >= 0)
3569 continue;
3570
81d1098b
DDM
3571 if (partition_skip(p))
3572 continue;
3573
53171c04
LP
3574 assert(p->offset != UINT64_MAX);
3575 assert(p->new_size != UINT64_MAX);
3576
3577 if (fd < 0)
3578 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
3579
3580 /* Loopback block devices are not only useful to turn regular files into block devices, but
3581 * also to cut out sections of block devices into new block devices. */
3582
fd83c98e 3583 r = loop_device_make(fd, O_RDWR, p->offset, p->new_size, 0, 0, LOCK_EX, &d);
53171c04 3584 if (r < 0)
5b5109e2 3585 return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno);
53171c04 3586
889914ef 3587 if (p->encrypt != ENCRYPT_OFF) {
994b3031 3588 r = partition_encrypt(context, p, d->node, &cd, &encrypted, &encrypted_dev_fd);
b9df3536
LP
3589 if (r < 0)
3590 return log_error_errno(r, "Failed to encrypt device: %m");
3591
3592 if (flock(encrypted_dev_fd, LOCK_EX) < 0)
3593 return log_error_errno(errno, "Failed to lock LUKS device: %m");
3594
3595 fsdev = encrypted;
3596 } else
3597 fsdev = d->node;
3598
53171c04
LP
3599 log_info("Formatting future partition %" PRIu64 ".", p->partno);
3600
95bfd3cd
DDM
3601 /* Ideally, we populate filesystems using our own code after creating the filesystem to
3602 * ensure consistent handling of chattrs, xattrs and other similar things. However, when
eb43379c
DDM
3603 * using read-only filesystems such as squashfs, we can't populate after creating the
3604 * filesystem because it's read-only, so instead we create a temporary root to use as the
3605 * source tree when generating the read-only filesystem. */
95bfd3cd 3606
59e2be46 3607 if (mkfs_supports_root_option(p->format)) {
143c3c08
DDM
3608 r = partition_populate_directory(p, denylist, &root, &tmp_root);
3609 if (r < 0)
3610 return r;
3611 }
95bfd3cd 3612
8bbbdfd7 3613 r = make_filesystem(fsdev, p->format, strempty(p->new_label), root ?: tmp_root, p->fs_uuid, arg_discard);
b9df3536
LP
3614 if (r < 0) {
3615 encrypted_dev_fd = safe_close(encrypted_dev_fd);
3616 (void) deactivate_luks(cd, encrypted);
53171c04 3617 return r;
b9df3536 3618 }
53171c04
LP
3619
3620 log_info("Successfully formatted future partition %" PRIu64 ".", p->partno);
3621
b9df3536 3622 /* The file system is now created, no need to delay udev further */
889914ef 3623 if (p->encrypt != ENCRYPT_OFF)
b9df3536
LP
3624 if (flock(encrypted_dev_fd, LOCK_UN) < 0)
3625 return log_error_errno(errno, "Failed to unlock LUKS device: %m");
3626
eb43379c 3627 /* Now, we can populate all the other filesystems that aren't read-only. */
59e2be46 3628 if (!mkfs_supports_root_option(p->format)) {
143c3c08
DDM
3629 r = partition_populate_filesystem(p, fsdev, denylist);
3630 if (r < 0) {
3631 encrypted_dev_fd = safe_close(encrypted_dev_fd);
3632 (void) deactivate_luks(cd, encrypted);
3633 return r;
3634 }
b9df3536
LP
3635 }
3636
3637 /* Note that we always sync explicitly here, since mkfs.fat doesn't do that on its own, and
3638 * if we don't sync before detaching a block device the in-flight sectors possibly won't hit
3639 * the disk. */
3640
889914ef 3641 if (p->encrypt != ENCRYPT_OFF) {
b9df3536 3642 if (fsync(encrypted_dev_fd) < 0)
8ac04a65 3643 return log_error_errno(errno, "Failed to synchronize LUKS volume: %m");
b9df3536
LP
3644 encrypted_dev_fd = safe_close(encrypted_dev_fd);
3645
3646 r = deactivate_luks(cd, encrypted);
3647 if (r < 0)
3648 return r;
3649
0d12936d 3650 sym_crypt_free(cd);
b9df3536
LP
3651 cd = NULL;
3652 }
8a794850 3653
53171c04
LP
3654 r = loop_device_sync(d);
3655 if (r < 0)
3656 return log_error_errno(r, "Failed to sync loopback device: %m");
3657 }
3658
3659 return 0;
3660}
3661
b5b7879a
DDM
3662static int do_verity_format(
3663 LoopDevice *data_device,
3664 LoopDevice *hash_device,
3665 uint64_t sector_size,
3666 uint8_t **ret_roothash,
3667 size_t *ret_roothash_size) {
3668
3669#if HAVE_LIBCRYPTSETUP
3670 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
3671 _cleanup_free_ uint8_t *rh = NULL;
3672 size_t rhs;
3673 int r;
3674
3675 assert(data_device);
3676 assert(hash_device);
3677 assert(sector_size > 0);
3678 assert(ret_roothash);
3679 assert(ret_roothash_size);
3680
3681 r = dlopen_cryptsetup();
3682 if (r < 0)
3683 return log_error_errno(r, "libcryptsetup not found, cannot setup verity: %m");
3684
3685 r = sym_crypt_init(&cd, hash_device->node);
3686 if (r < 0)
3687 return log_error_errno(r, "Failed to allocate libcryptsetup context: %m");
3688
3689 r = sym_crypt_format(
3690 cd, CRYPT_VERITY, NULL, NULL, NULL, NULL, 0,
3691 &(struct crypt_params_verity){
3692 .data_device = data_device->node,
3693 .flags = CRYPT_VERITY_CREATE_HASH,
3694 .hash_name = "sha256",
3695 .hash_type = 1,
3696 .data_block_size = sector_size,
3697 .hash_block_size = sector_size,
3698 .salt_size = 32,
3699 });
3700 if (r < 0)
3701 return log_error_errno(r, "Failed to setup verity hash data: %m");
3702
3703 r = sym_crypt_get_volume_key_size(cd);
3704 if (r < 0)
3705 return log_error_errno(r, "Failed to determine verity root hash size: %m");
3706 rhs = (size_t) r;
3707
3708 rh = malloc(rhs);
3709 if (!rh)
3710 return log_oom();
3711
3712 r = sym_crypt_volume_key_get(cd, CRYPT_ANY_SLOT, (char *) rh, &rhs, NULL, 0);
3713 if (r < 0)
3714 return log_error_errno(r, "Failed to get verity root hash: %m");
3715
3716 *ret_roothash = TAKE_PTR(rh);
3717 *ret_roothash_size = rhs;
3718
3719 return 0;
3720#else
9c98e277 3721 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libcryptsetup is not supported, cannot setup verity hashes: %m");
b5b7879a
DDM
3722#endif
3723}
3724
b6db96a2 3725static int context_verity_hash(Context *context) {
b5b7879a
DDM
3726 int fd = -1, r;
3727
3728 assert(context);
3729
3730 LIST_FOREACH(partitions, p, context->partitions) {
3731 Partition *dp;
3732 _cleanup_(loop_device_unrefp) LoopDevice *hash_device = NULL, *data_device = NULL;
3733 _cleanup_free_ uint8_t *rh = NULL;
3734 size_t rhs = 0; /* Initialize to work around for GCC false positive. */
3735
3736 if (p->dropped)
3737 continue;
3738
3739 if (PARTITION_EXISTS(p)) /* Never format existing partitions */
3740 continue;
3741
3742 if (p->verity != VERITY_HASH)
3743 continue;
3744
81d1098b
DDM
3745 if (partition_skip(p))
3746 continue;
3747
b5b7879a
DDM
3748 assert_se(dp = p->siblings[VERITY_DATA]);
3749 assert(!dp->dropped);
3750
3751 if (fd < 0)
3752 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
3753
fd83c98e 3754 r = loop_device_make(fd, O_RDONLY, dp->offset, dp->new_size, 0, 0, LOCK_EX, &data_device);
b5b7879a
DDM
3755 if (r < 0)
3756 return log_error_errno(r,
3757 "Failed to make loopback device of verity data partition %" PRIu64 ": %m",
3758 p->partno);
3759
fd83c98e 3760 r = loop_device_make(fd, O_RDWR, p->offset, p->new_size, 0, 0, LOCK_EX, &hash_device);
b5b7879a
DDM
3761 if (r < 0)
3762 return log_error_errno(r,
3763 "Failed to make loopback device of verity hash partition %" PRIu64 ": %m",
3764 p->partno);
3765
3766 r = do_verity_format(data_device, hash_device, context->sector_size, &rh, &rhs);
3767 if (r < 0)
3768 return r;
3769
3770 assert(rhs >= sizeof(sd_id128_t) * 2);
3771
3772 if (!dp->new_uuid_is_set) {
3773 memcpy_safe(dp->new_uuid.bytes, rh, sizeof(sd_id128_t));
3774 dp->new_uuid_is_set = true;
3775 }
3776
3777 if (!p->new_uuid_is_set) {
3778 memcpy_safe(p->new_uuid.bytes, rh + rhs - sizeof(sd_id128_t), sizeof(sd_id128_t));
3779 p->new_uuid_is_set = true;
3780 }
3781
3782 p->roothash = TAKE_PTR(rh);
3783 p->roothash_size = rhs;
3784 }
3785
3786 return 0;
3787}
3788
b456191d
DDM
3789static int parse_x509_certificate(const char *certificate, size_t certificate_size, X509 **ret) {
3790#if HAVE_OPENSSL
3791 _cleanup_(X509_freep) X509 *cert = NULL;
3792 _cleanup_(BIO_freep) BIO *cb = NULL;
3793
3794 assert(certificate);
3795 assert(certificate_size > 0);
3796 assert(ret);
3797
3798 cb = BIO_new_mem_buf(certificate, certificate_size);
3799 if (!cb)
3800 return log_oom();
3801
3802 cert = PEM_read_bio_X509(cb, NULL, NULL, NULL);
3803 if (!cert)
3804 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Failed to parse X.509 certificate: %s",
3805 ERR_error_string(ERR_get_error(), NULL));
3806
3807 if (ret)
3808 *ret = TAKE_PTR(cert);
3809
3810 return 0;
3811#else
3812 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "openssl is not supported, cannot parse X509 certificate.");
3813#endif
3814}
3815
3816static int parse_private_key(const char *key, size_t key_size, EVP_PKEY **ret) {
3817#if HAVE_OPENSSL
3818 _cleanup_(BIO_freep) BIO *kb = NULL;
3819 _cleanup_(EVP_PKEY_freep) EVP_PKEY *pk = NULL;
3820
3821 assert(key);
3822 assert(key_size > 0);
3823 assert(ret);
3824
3825 kb = BIO_new_mem_buf(key, key_size);
3826 if (!kb)
3827 return log_oom();
3828
3829 pk = PEM_read_bio_PrivateKey(kb, NULL, NULL, NULL);
3830 if (!pk)
3831 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to parse PEM private key: %s",
3832 ERR_error_string(ERR_get_error(), NULL));
3833
3834 if (ret)
3835 *ret = TAKE_PTR(pk);
3836
3837 return 0;
3838#else
3839 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "openssl is not supported, cannot parse private key.");
3840#endif
3841}
3842
3843static int sign_verity_roothash(
3844 const uint8_t *roothash,
3845 size_t roothash_size,
3846 uint8_t **ret_signature,
3847 size_t *ret_signature_size) {
3848
3849#if HAVE_OPENSSL
3850 _cleanup_(BIO_freep) BIO *rb = NULL;
3851 _cleanup_(PKCS7_freep) PKCS7 *p7 = NULL;
3852 _cleanup_free_ char *hex = NULL;
3853 _cleanup_free_ uint8_t *sig = NULL;
3854 int sigsz;
3855
3856 assert(roothash);
3857 assert(roothash_size > 0);
3858 assert(ret_signature);
3859 assert(ret_signature_size);
3860
3861 hex = hexmem(roothash, roothash_size);
3862 if (!hex)
3863 return log_oom();
3864
3865 rb = BIO_new_mem_buf(hex, -1);
3866 if (!rb)
3867 return log_oom();
3868
3869 p7 = PKCS7_sign(arg_certificate, arg_private_key, NULL, rb, PKCS7_DETACHED|PKCS7_NOATTR|PKCS7_BINARY);
3870 if (!p7)
3871 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to calculate PKCS7 signature: %s",
3872 ERR_error_string(ERR_get_error(), NULL));
3873
3874 sigsz = i2d_PKCS7(p7, &sig);
3875 if (sigsz < 0)
3876 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to convert PKCS7 signature to DER: %s",
3877 ERR_error_string(ERR_get_error(), NULL));
3878
3879 *ret_signature = TAKE_PTR(sig);
3880 *ret_signature_size = sigsz;
3881
3882 return 0;
3883#else
3884 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "openssl is not supported, cannot setup verity signature: %m");
3885#endif
3886}
3887
3888static int context_verity_sig(Context *context) {
3889 int fd = -1, r;
3890
3891 assert(context);
3892
3893 LIST_FOREACH(partitions, p, context->partitions) {
3894 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
3895 _cleanup_free_ uint8_t *sig = NULL;
3896 _cleanup_free_ char *text = NULL;
3897 Partition *hp;
3898 uint8_t fp[X509_FINGERPRINT_SIZE];
419f2742 3899 size_t sigsz = 0, padsz; /* avoid false maybe-uninitialized warning */
b456191d
DDM
3900
3901 if (p->dropped)
3902 continue;
3903
3904 if (PARTITION_EXISTS(p))
3905 continue;
3906
3907 if (p->verity != VERITY_SIG)
3908 continue;
3909
81d1098b
DDM
3910 if (partition_skip(p))
3911 continue;
3912
b456191d
DDM
3913 assert_se(hp = p->siblings[VERITY_HASH]);
3914 assert(!hp->dropped);
3915
3916 assert(arg_certificate);
3917
3918 if (fd < 0)
3919 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
3920
3921 r = sign_verity_roothash(hp->roothash, hp->roothash_size, &sig, &sigsz);
3922 if (r < 0)
3923 return r;
3924
3925 r = x509_fingerprint(arg_certificate, fp);
3926 if (r < 0)
3927 return log_error_errno(r, "Unable to calculate X509 certificate fingerprint: %m");
3928
3929 r = json_build(&v,
3930 JSON_BUILD_OBJECT(
3931 JSON_BUILD_PAIR("rootHash", JSON_BUILD_HEX(hp->roothash, hp->roothash_size)),
3932 JSON_BUILD_PAIR(
3933 "certificateFingerprint",
3934 JSON_BUILD_HEX(fp, sizeof(fp))
3935 ),
3936 JSON_BUILD_PAIR("signature", JSON_BUILD_BASE64(sig, sigsz))
3937 )
3938 );
3939 if (r < 0)
3940 return log_error_errno(r, "Failed to build JSON object: %m");
3941
3942 r = json_variant_format(v, 0, &text);
3943 if (r < 0)
3944 return log_error_errno(r, "Failed to format JSON object: %m");
3945
3946 padsz = round_up_size(strlen(text), 4096);
3947 assert_se(padsz <= p->new_size);
3948
3949 r = strgrowpad0(&text, padsz);
3950 if (r < 0)
3951 return log_error_errno(r, "Failed to pad string to %s", FORMAT_BYTES(padsz));
3952
3953 if (lseek(fd, p->offset, SEEK_SET) == (off_t) -1)
3954 return log_error_errno(errno, "Failed to seek to partition offset: %m");
3955
3956 r = loop_write(fd, text, padsz, /*do_poll=*/ false);
3957 if (r < 0)
3958 return log_error_errno(r, "Failed to write verity signature to partition: %m");
3959
3960 if (fsync(fd) < 0)
3961 return log_error_errno(errno, "Failed to synchronize verity signature JSON: %m");
3962 }
3963
3964 return 0;
3965}
3966
e594a3b1
LP
3967static int partition_acquire_uuid(Context *context, Partition *p, sd_id128_t *ret) {
3968 struct {
3969 sd_id128_t type_uuid;
3970 uint64_t counter;
695cfd53 3971 } _packed_ plaintext = {};
e594a3b1 3972 union {
ade99252 3973 uint8_t md[SHA256_DIGEST_SIZE];
e594a3b1
LP
3974 sd_id128_t id;
3975 } result;
3976
3977 uint64_t k = 0;
e594a3b1
LP
3978 int r;
3979
3980 assert(context);
3981 assert(p);
3982 assert(ret);
3983
3984 /* Calculate a good UUID for the indicated partition. We want a certain degree of reproducibility,
3985 * hence we won't generate the UUIDs randomly. Instead we use a cryptographic hash (precisely:
3986 * HMAC-SHA256) to derive them from a single seed. The seed is generally the machine ID of the
3987 * installation we are processing, but if random behaviour is desired can be random, too. We use the
3988 * seed value as key for the HMAC (since the machine ID is something we generally don't want to leak)
3989 * and the partition type as plaintext. The partition type is suffixed with a counter (only for the
3990 * second and later partition of the same type) if we have more than one partition of the same
3991 * time. Or in other words:
3992 *
3993 * With:
3994 * SEED := /etc/machine-id
3995 *
3996 * If first partition instance of type TYPE_UUID:
3997 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID)
3998 *
3999 * For all later partition instances of type TYPE_UUID with INSTANCE being the LE64 encoded instance number:
4000 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID || INSTANCE)
4001 */
4002
4003 LIST_FOREACH(partitions, q, context->partitions) {
4004 if (p == q)
4005 break;
4006
22e932f4 4007 if (!sd_id128_equal(p->type.uuid, q->type.uuid))
e594a3b1
LP
4008 continue;
4009
4010 k++;
4011 }
4012
22e932f4 4013 plaintext.type_uuid = p->type.uuid;
e594a3b1
LP
4014 plaintext.counter = htole64(k);
4015
ade99252
KK
4016 hmac_sha256(context->seed.bytes, sizeof(context->seed.bytes),
4017 &plaintext,
4018 k == 0 ? sizeof(sd_id128_t) : sizeof(plaintext),
4019 result.md);
e594a3b1
LP
4020
4021 /* Take the first half, mark it as v4 UUID */
4022 assert_cc(sizeof(result.md) == sizeof(result.id) * 2);
4023 result.id = id128_make_v4_uuid(result.id);
4024
4025 /* Ensure this partition UUID is actually unique, and there's no remaining partition from an earlier run? */
4026 LIST_FOREACH(partitions, q, context->partitions) {
4027 if (p == q)
4028 continue;
4029
580f48cc 4030 if (sd_id128_in_set(result.id, q->current_uuid, q->new_uuid)) {
da1af43d 4031 log_warning("Partition UUID calculated from seed for partition %" PRIu64 " already used, reverting to randomized UUID.", p->partno);
e594a3b1
LP
4032
4033 r = sd_id128_randomize(&result.id);
4034 if (r < 0)
4035 return log_error_errno(r, "Failed to generate randomized UUID: %m");
4036
4037 break;
4038 }
4039 }
4040
4041 *ret = result.id;
4042 return 0;
4043}
4044
4045static int partition_acquire_label(Context *context, Partition *p, char **ret) {
4046 _cleanup_free_ char *label = NULL;
4047 const char *prefix;
4048 unsigned k = 1;
4049
4050 assert(context);
4051 assert(p);
4052 assert(ret);
4053
22e932f4 4054 prefix = gpt_partition_type_uuid_to_string(p->type.uuid);
e594a3b1
LP
4055 if (!prefix)
4056 prefix = "linux";
4057
4058 for (;;) {
4059 const char *ll = label ?: prefix;
4060 bool retry = false;
e594a3b1
LP
4061
4062 LIST_FOREACH(partitions, q, context->partitions) {
4063 if (p == q)
4064 break;
4065
4066 if (streq_ptr(ll, q->current_label) ||
4067 streq_ptr(ll, q->new_label)) {
4068 retry = true;
4069 break;
4070 }
4071 }
4072
4073 if (!retry)
4074 break;
4075
4076 label = mfree(label);
e594a3b1
LP
4077 if (asprintf(&label, "%s-%u", prefix, ++k) < 0)
4078 return log_oom();
4079 }
4080
4081 if (!label) {
4082 label = strdup(prefix);
4083 if (!label)
4084 return log_oom();
4085 }
4086
4087 *ret = TAKE_PTR(label);
4088 return 0;
4089}
4090
4091static int context_acquire_partition_uuids_and_labels(Context *context) {
e594a3b1
LP
4092 int r;
4093
4094 assert(context);
4095
4096 LIST_FOREACH(partitions, p, context->partitions) {
e594a3b1
LP
4097 /* Never touch foreign partitions */
4098 if (PARTITION_IS_FOREIGN(p)) {
4099 p->new_uuid = p->current_uuid;
4100
4101 if (p->current_label) {
78eee6ce
LP
4102 r = free_and_strdup_warn(&p->new_label, strempty(p->current_label));
4103 if (r < 0)
4104 return r;
e594a3b1
LP
4105 }
4106
4107 continue;
4108 }
4109
4110 if (!sd_id128_is_null(p->current_uuid))
4111 p->new_uuid = p->current_uuid; /* Never change initialized UUIDs */
b456191d 4112 else if (!p->new_uuid_is_set && !IN_SET(p->verity, VERITY_DATA, VERITY_HASH)) {
12963533 4113 /* Not explicitly set by user! */
e594a3b1
LP
4114 r = partition_acquire_uuid(context, p, &p->new_uuid);
4115 if (r < 0)
4116 return r;
11749b61
DDM
4117
4118 p->new_uuid_is_set = true;
e594a3b1
LP
4119 }
4120
8bbbdfd7
DDM
4121 /* Calculate the UUID for the file system as HMAC-SHA256 of the string "file-system-uuid",
4122 * keyed off the partition UUID. */
4123 r = derive_uuid(p->new_uuid, "file-system-uuid", &p->fs_uuid);
4124 if (r < 0)
4125 return r;
4126
e594a3b1 4127 if (!isempty(p->current_label)) {
78eee6ce
LP
4128 /* never change initialized labels */
4129 r = free_and_strdup_warn(&p->new_label, p->current_label);
4130 if (r < 0)
4131 return r;
12963533
TH
4132 } else if (!p->new_label) {
4133 /* Not explicitly set by user! */
4134
e594a3b1
LP
4135 r = partition_acquire_label(context, p, &p->new_label);
4136 if (r < 0)
4137 return r;
4138 }
4139 }
4140
4141 return 0;
4142}
4143
e73309c5
LP
4144static int set_gpt_flags(struct fdisk_partition *q, uint64_t flags) {
4145 _cleanup_free_ char *a = NULL;
4146
4147 for (unsigned i = 0; i < sizeof(flags) * 8; i++) {
4148 uint64_t bit = UINT64_C(1) << i;
4149 char buf[DECIMAL_STR_MAX(unsigned)+1];
4150
4151 if (!FLAGS_SET(flags, bit))
4152 continue;
4153
4154 xsprintf(buf, "%u", i);
4155 if (!strextend_with_separator(&a, ",", buf))
4156 return -ENOMEM;
4157 }
4158
4159 return fdisk_partition_set_attrs(q, a);
4160}
4161
1c41c1dc
LP
4162static uint64_t partition_merge_flags(Partition *p) {
4163 uint64_t f;
4164
4165 assert(p);
4166
4167 f = p->gpt_flags;
4168
ff0771bf 4169 if (p->no_auto >= 0) {
22e932f4 4170 if (gpt_partition_type_knows_no_auto(p->type))
92e72028 4171 SET_FLAG(f, SD_GPT_FLAG_NO_AUTO, p->no_auto);
ff0771bf 4172 else {
b7416360 4173 char buffer[SD_ID128_UUID_STRING_MAX];
ff0771bf
LP
4174 log_warning("Configured NoAuto=%s for partition type '%s' that doesn't support it, ignoring.",
4175 yes_no(p->no_auto),
22e932f4 4176 gpt_partition_type_uuid_to_string_harder(p->type.uuid, buffer));
ff0771bf
LP
4177 }
4178 }
4179
1c41c1dc 4180 if (p->read_only >= 0) {
22e932f4 4181 if (gpt_partition_type_knows_read_only(p->type))
92e72028 4182 SET_FLAG(f, SD_GPT_FLAG_READ_ONLY, p->read_only);
1c41c1dc 4183 else {
b7416360 4184 char buffer[SD_ID128_UUID_STRING_MAX];
1c41c1dc
LP
4185 log_warning("Configured ReadOnly=%s for partition type '%s' that doesn't support it, ignoring.",
4186 yes_no(p->read_only),
22e932f4 4187 gpt_partition_type_uuid_to_string_harder(p->type.uuid, buffer));
1c41c1dc
LP
4188 }
4189 }
4190
4191 if (p->growfs >= 0) {
22e932f4 4192 if (gpt_partition_type_knows_growfs(p->type))
92e72028 4193 SET_FLAG(f, SD_GPT_FLAG_GROWFS, p->growfs);
1c41c1dc 4194 else {
b7416360 4195 char buffer[SD_ID128_UUID_STRING_MAX];
1c41c1dc
LP
4196 log_warning("Configured GrowFileSystem=%s for partition type '%s' that doesn't support it, ignoring.",
4197 yes_no(p->growfs),
22e932f4 4198 gpt_partition_type_uuid_to_string_harder(p->type.uuid, buffer));
1c41c1dc
LP
4199 }
4200 }
4201
4202 return f;
4203}
4204
f28d4f42 4205static int context_mangle_partitions(Context *context) {
f28d4f42 4206 int r;
e594a3b1
LP
4207
4208 assert(context);
4209
e594a3b1
LP
4210 LIST_FOREACH(partitions, p, context->partitions) {
4211 if (p->dropped)
4212 continue;
4213
81d1098b
DDM
4214 if (partition_skip(p))
4215 continue;
4216
e594a3b1
LP
4217 assert(p->new_size != UINT64_MAX);
4218 assert(p->offset != UINT64_MAX);
4219 assert(p->partno != UINT64_MAX);
4220
4221 if (PARTITION_EXISTS(p)) {
4222 bool changed = false;
4223
4224 assert(p->current_partition);
4225
4226 if (p->new_size != p->current_size) {
4227 assert(p->new_size >= p->current_size);
994b3031 4228 assert(p->new_size % context->sector_size == 0);
e594a3b1
LP
4229
4230 r = fdisk_partition_size_explicit(p->current_partition, true);
4231 if (r < 0)
4232 return log_error_errno(r, "Failed to enable explicit sizing: %m");
4233
994b3031 4234 r = fdisk_partition_set_size(p->current_partition, p->new_size / context->sector_size);
e594a3b1
LP
4235 if (r < 0)
4236 return log_error_errno(r, "Failed to grow partition: %m");
4237
4238 log_info("Growing existing partition %" PRIu64 ".", p->partno);
4239 changed = true;
4240 }
4241
4242 if (!sd_id128_equal(p->new_uuid, p->current_uuid)) {
b7416360 4243 r = fdisk_partition_set_uuid(p->current_partition, SD_ID128_TO_UUID_STRING(p->new_uuid));
e594a3b1
LP
4244 if (r < 0)
4245 return log_error_errno(r, "Failed to set partition UUID: %m");
4246
4247 log_info("Initializing UUID of existing partition %" PRIu64 ".", p->partno);
4248 changed = true;
4249 }
4250
4251 if (!streq_ptr(p->new_label, p->current_label)) {
be9ce018 4252 r = fdisk_partition_set_name(p->current_partition, strempty(p->new_label));
e594a3b1
LP
4253 if (r < 0)
4254 return log_error_errno(r, "Failed to set partition label: %m");
4255
4256 log_info("Setting partition label of existing partition %" PRIu64 ".", p->partno);
4257 changed = true;
4258 }
4259
4260 if (changed) {
4261 assert(!PARTITION_IS_FOREIGN(p)); /* never touch foreign partitions */
4262
4263 r = fdisk_set_partition(context->fdisk_context, p->partno, p->current_partition);
4264 if (r < 0)
4265 return log_error_errno(r, "Failed to update partition: %m");
4266 }
4267 } else {
4268 _cleanup_(fdisk_unref_partitionp) struct fdisk_partition *q = NULL;
4269 _cleanup_(fdisk_unref_parttypep) struct fdisk_parttype *t = NULL;
e594a3b1
LP
4270
4271 assert(!p->new_partition);
994b3031
LP
4272 assert(p->offset % context->sector_size == 0);
4273 assert(p->new_size % context->sector_size == 0);
be9ce018 4274 assert(p->new_label);
e594a3b1
LP
4275
4276 t = fdisk_new_parttype();
4277 if (!t)
4278 return log_oom();
4279
22e932f4 4280 r = fdisk_parttype_set_typestr(t, SD_ID128_TO_UUID_STRING(p->type.uuid));
e594a3b1
LP
4281 if (r < 0)
4282 return log_error_errno(r, "Failed to initialize partition type: %m");
4283
4284 q = fdisk_new_partition();
4285 if (!q)
4286 return log_oom();
4287
4288 r = fdisk_partition_set_type(q, t);
4289 if (r < 0)
4290 return log_error_errno(r, "Failed to set partition type: %m");
4291
4292 r = fdisk_partition_size_explicit(q, true);
4293 if (r < 0)
4294 return log_error_errno(r, "Failed to enable explicit sizing: %m");
4295
994b3031 4296 r = fdisk_partition_set_start(q, p->offset / context->sector_size);
e594a3b1
LP
4297 if (r < 0)
4298 return log_error_errno(r, "Failed to position partition: %m");
4299
994b3031 4300 r = fdisk_partition_set_size(q, p->new_size / context->sector_size);
e594a3b1
LP
4301 if (r < 0)
4302 return log_error_errno(r, "Failed to grow partition: %m");
4303
4304 r = fdisk_partition_set_partno(q, p->partno);
4305 if (r < 0)
4306 return log_error_errno(r, "Failed to set partition number: %m");
4307
b7416360 4308 r = fdisk_partition_set_uuid(q, SD_ID128_TO_UUID_STRING(p->new_uuid));
e594a3b1
LP
4309 if (r < 0)
4310 return log_error_errno(r, "Failed to set partition UUID: %m");
4311
be9ce018 4312 r = fdisk_partition_set_name(q, strempty(p->new_label));
e594a3b1
LP
4313 if (r < 0)
4314 return log_error_errno(r, "Failed to set partition label: %m");
4315
ff0771bf 4316 /* Merge the no auto + read only + growfs setting with the literal flags, and set them for the partition */
1c41c1dc 4317 r = set_gpt_flags(q, partition_merge_flags(p));
e73309c5
LP
4318 if (r < 0)
4319 return log_error_errno(r, "Failed to set GPT partition flags: %m");
4320
5b5109e2 4321 log_info("Adding new partition %" PRIu64 " to partition table.", p->partno);
e594a3b1
LP
4322
4323 r = fdisk_add_partition(context->fdisk_context, q, NULL);
4324 if (r < 0)
4325 return log_error_errno(r, "Failed to add partition: %m");
4326
4327 assert(!p->new_partition);
4328 p->new_partition = TAKE_PTR(q);
4329 }
4330 }
4331
f28d4f42
LP
4332 return 0;
4333}
4334
4cee8333
DDM
4335static int split_name_printf(Partition *p) {
4336 assert(p);
4337
4338 const Specifier table[] = {
22e932f4
DDM
4339 { 't', specifier_string, GPT_PARTITION_TYPE_UUID_TO_STRING_HARDER(p->type.uuid) },
4340 { 'T', specifier_id128, &p->type.uuid },
4cee8333
DDM
4341 { 'U', specifier_id128, &p->new_uuid },
4342 { 'n', specifier_uint64, &p->partno },
4343
4344 COMMON_SYSTEM_SPECIFIERS,
4345 {}
4346 };
4347
4348 return specifier_printf(p->split_name_format, NAME_MAX, table, arg_root, p, &p->split_name_resolved);
4349}
4350
4351static int split_name_resolve(Context *context) {
4352 int r;
4353
4354 LIST_FOREACH(partitions, p, context->partitions) {
4355 if (p->dropped)
4356 continue;
4357
4358 if (!p->split_name_format)
4359 continue;
4360
4361 r = split_name_printf(p);
4362 if (r < 0)
4363 return log_error_errno(r, "Failed to resolve specifiers in %s: %m", p->split_name_format);
4364 }
4365
4366 LIST_FOREACH(partitions, p, context->partitions) {
4367 if (!p->split_name_resolved)
4368 continue;
4369
4370 LIST_FOREACH(partitions, q, context->partitions) {
4371 if (p == q)
4372 continue;
4373
4374 if (!q->split_name_resolved)
4375 continue;
4376
4377 if (!streq(p->split_name_resolved, q->split_name_resolved))
4378 continue;
4379
4380 return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ),
4381 "%s and %s have the same resolved split name \"%s\", refusing",
4382 p->definition_path, q->definition_path, p->split_name_resolved);
4383 }
4384 }
4385
4386 return 0;
4387}
4388
4389static int split_node(const char *node, char **ret_base, char **ret_ext) {
4390 _cleanup_free_ char *base = NULL, *ext = NULL;
4391 char *e;
4392 int r;
4393
4394 assert(node);
4395 assert(ret_base);
4396 assert(ret_ext);
4397
4398 r = path_extract_filename(node, &base);
4399 if (r == O_DIRECTORY || r == -EADDRNOTAVAIL)
4400 return log_error_errno(r, "Device node %s cannot be a directory", arg_node);
4401 if (r < 0)
4402 return log_error_errno(r, "Failed to extract filename from %s: %m", arg_node);
4403
4404 e = endswith(base, ".raw");
4405 if (e) {
4406 ext = strdup(e);
4407 if (!ext)
4408 return log_oom();
4409
4410 *e = 0;
4411 }
4412
4413 *ret_base = TAKE_PTR(base);
4414 *ret_ext = TAKE_PTR(ext);
4415
4416 return 0;
4417}
4418
4419static int context_split(Context *context) {
4420 _cleanup_free_ char *base = NULL, *ext = NULL;
4421 _cleanup_close_ int dir_fd = -1;
4422 int fd = -1, r;
4423
4424 if (!arg_split)
4425 return 0;
4426
4427 assert(context);
4428 assert(arg_node);
4429
4430 /* We can't do resolution earlier because the partition UUIDs for verity partitions are only filled
4431 * in after they've been generated. */
4432
4433 r = split_name_resolve(context);
4434 if (r < 0)
4435 return r;
4436
4437 r = split_node(arg_node, &base, &ext);
4438 if (r < 0)
4439 return r;
4440
4441 dir_fd = r = open_parent(arg_node, O_PATH|O_CLOEXEC, 0);
4442 if (r == -EDESTADDRREQ)
4443 dir_fd = AT_FDCWD;
4444 else if (r < 0)
4445 return log_error_errno(r, "Failed to open parent directory of %s: %m", arg_node);
4446
4447 LIST_FOREACH(partitions, p, context->partitions) {
4448 _cleanup_free_ char *fname = NULL;
4449 _cleanup_close_ int fdt = -1;
4450
4451 if (p->dropped)
4452 continue;
4453
4454 if (!p->split_name_resolved)
4455 continue;
4456
81d1098b
DDM
4457 if (partition_skip(p))
4458 continue;
4459
4cee8333
DDM
4460 fname = strjoin(base, ".", p->split_name_resolved, ext);
4461 if (!fname)
4462 return log_oom();
4463
4464 fdt = openat(dir_fd, fname, O_WRONLY|O_NOCTTY|O_CLOEXEC|O_NOFOLLOW|O_CREAT|O_EXCL, 0666);
4465 if (fdt < 0)
4466 return log_error_errno(errno, "Failed to open %s: %m", fname);
4467
4468 if (fd < 0)
4469 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
4470
4471 if (lseek(fd, p->offset, SEEK_SET) < 0)
4472 return log_error_errno(errno, "Failed to seek to partition offset: %m");
4473
a673b056 4474 r = copy_bytes(fd, fdt, p->new_size, COPY_REFLINK|COPY_HOLES);
4cee8333
DDM
4475 if (r < 0)
4476 return log_error_errno(r, "Failed to copy to split partition %s: %m", fname);
4477 }
4478
4479 return 0;
4480}
4481
f28d4f42
LP
4482static int context_write_partition_table(
4483 Context *context,
4484 const char *node,
4485 bool from_scratch) {
4486
4487 _cleanup_(fdisk_unref_tablep) struct fdisk_table *original_table = NULL;
4488 int capable, r;
4489
4490 assert(context);
4491
f28d4f42
LP
4492 if (!from_scratch && !context_changed(context)) {
4493 log_info("No changes.");
4494 return 0;
4495 }
4496
4497 if (arg_dry_run) {
4498 log_notice("Refusing to repartition, please re-run with --dry-run=no.");
4499 return 0;
4500 }
4501
4502 log_info("Applying changes.");
4503
4504 if (from_scratch) {
81873a6b
LP
4505 r = context_wipe_range(context, 0, context->total);
4506 if (r < 0)
4507 return r;
4508
4509 log_info("Wiped block device.");
4510
f28d4f42
LP
4511 r = context_discard_range(context, 0, context->total);
4512 if (r == -EOPNOTSUPP)
5b5109e2 4513 log_info("Storage does not support discard, not discarding entire block device data.");
f28d4f42
LP
4514 else if (r < 0)
4515 return log_error_errno(r, "Failed to discard entire block device: %m");
4516 else if (r > 0)
4517 log_info("Discarded entire block device.");
4518 }
4519
4520 r = fdisk_get_partitions(context->fdisk_context, &original_table);
4521 if (r < 0)
4522 return log_error_errno(r, "Failed to acquire partition table: %m");
4523
4524 /* Wipe fs signatures and discard sectors where the new partitions are going to be placed and in the
4525 * gaps between partitions, just to be sure. */
4526 r = context_wipe_and_discard(context, from_scratch);
4527 if (r < 0)
4528 return r;
4529
4530 r = context_copy_blocks(context);
4531 if (r < 0)
4532 return r;
4533
4534 r = context_mkfs(context);
4535 if (r < 0)
4536 return r;
4537
b6db96a2 4538 r = context_verity_hash(context);
b5b7879a
DDM
4539 if (r < 0)
4540 return r;
4541
b456191d 4542 r = context_verity_sig(context);
b5b7879a
DDM
4543 if (r < 0)
4544 return r;
4545
f28d4f42
LP
4546 r = context_mangle_partitions(context);
4547 if (r < 0)
4548 return r;
4549
e594a3b1
LP
4550 log_info("Writing new partition table.");
4551
4552 r = fdisk_write_disklabel(context->fdisk_context);
4553 if (r < 0)
4554 return log_error_errno(r, "Failed to write partition table: %m");
4555
911ba624 4556 capable = blockdev_partscan_enabled(fdisk_get_devfd(context->fdisk_context));
9a1deb85
LP
4557 if (capable == -ENOTBLK)
4558 log_debug("Not telling kernel to reread partition table, since we are not operating on a block device.");
4559 else if (capable < 0)
911ba624 4560 return log_error_errno(capable, "Failed to check if block device supports partition scanning: %m");
9a1deb85 4561 else if (capable > 0) {
e594a3b1
LP
4562 log_info("Telling kernel to reread partition table.");
4563
4564 if (from_scratch)
4565 r = fdisk_reread_partition_table(context->fdisk_context);
4566 else
4567 r = fdisk_reread_changes(context->fdisk_context, original_table);
4568 if (r < 0)
4569 return log_error_errno(r, "Failed to reread partition table: %m");
4570 } else
4571 log_notice("Not telling kernel to reread partition table, because selected image does not support kernel partition block devices.");
4572
4573 log_info("All done.");
4574
4575 return 0;
4576}
4577
4578static int context_read_seed(Context *context, const char *root) {
4579 int r;
4580
4581 assert(context);
4582
4583 if (!sd_id128_is_null(context->seed))
4584 return 0;
4585
4586 if (!arg_randomize) {
4587 _cleanup_close_ int fd = -1;
4588
4589 fd = chase_symlinks_and_open("/etc/machine-id", root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC, NULL);
4590 if (fd == -ENOENT)
4591 log_info("No machine ID set, using randomized partition UUIDs.");
4592 else if (fd < 0)
4593 return log_error_errno(fd, "Failed to determine machine ID of image: %m");
4594 else {
448b782c 4595 r = id128_read_fd(fd, ID128_PLAIN_OR_UNINIT, &context->seed);
e594a3b1
LP
4596 if (r == -ENOMEDIUM)
4597 log_info("No machine ID set, using randomized partition UUIDs.");
4598 else if (r < 0)
4599 return log_error_errno(r, "Failed to parse machine ID of image: %m");
4600
4601 return 0;
4602 }
4603 }
4604
4605 r = sd_id128_randomize(&context->seed);
4606 if (r < 0)
4607 return log_error_errno(r, "Failed to generate randomized seed: %m");
4608
4609 return 0;
4610}
4611
4612static int context_factory_reset(Context *context, bool from_scratch) {
e594a3b1
LP
4613 size_t n = 0;
4614 int r;
4615
4616 assert(context);
4617
4618 if (arg_factory_reset <= 0)
4619 return 0;
4620
4621 if (from_scratch) /* Nothing to reset if we start from scratch */
4622 return 0;
4623
4624 if (arg_dry_run) {
4625 log_notice("Refusing to factory reset, please re-run with --dry-run=no.");
4626 return 0;
4627 }
4628
4629 log_info("Applying factory reset.");
4630
4631 LIST_FOREACH(partitions, p, context->partitions) {
4632
4633 if (!p->factory_reset || !PARTITION_EXISTS(p))
4634 continue;
4635
4636 assert(p->partno != UINT64_MAX);
4637
4638 log_info("Removing partition %" PRIu64 " for factory reset.", p->partno);
4639
4640 r = fdisk_delete_partition(context->fdisk_context, p->partno);
4641 if (r < 0)
4642 return log_error_errno(r, "Failed to remove partition %" PRIu64 ": %m", p->partno);
4643
4644 n++;
4645 }
4646
4647 if (n == 0) {
4648 log_info("Factory reset requested, but no partitions to delete found.");
4649 return 0;
4650 }
4651
4652 r = fdisk_write_disklabel(context->fdisk_context);
4653 if (r < 0)
4654 return log_error_errno(r, "Failed to write disk label: %m");
4655
4656 log_info("Successfully deleted %zu partitions.", n);
4657 return 1;
4658}
4659
4660static int context_can_factory_reset(Context *context) {
e594a3b1
LP
4661 assert(context);
4662
4663 LIST_FOREACH(partitions, p, context->partitions)
4664 if (p->factory_reset && PARTITION_EXISTS(p))
4665 return true;
4666
4667 return false;
4668}
4669
5c08da58
LP
4670static int resolve_copy_blocks_auto_candidate(
4671 dev_t partition_devno,
22e932f4 4672 GptPartitionType partition_type,
5c08da58
LP
4673 dev_t restrict_devno,
4674 sd_id128_t *ret_uuid) {
4675
4676 _cleanup_(blkid_free_probep) blkid_probe b = NULL;
5c08da58 4677 _cleanup_close_ int fd = -1;
ca822829
YW
4678 _cleanup_free_ char *p = NULL;
4679 const char *pttype, *t;
5c08da58
LP
4680 sd_id128_t pt_parsed, u;
4681 blkid_partition pp;
4682 dev_t whole_devno;
4683 blkid_partlist pl;
5c08da58
LP
4684 int r;
4685
4686 /* Checks if the specified partition has the specified GPT type UUID, and is located on the specified
4687 * 'restrict_devno' device. The type check is particularly relevant if we have Verity volume which is
4688 * backed by two separate partitions: the data and the hash partitions, and we need to find the right
4689 * one of the two. */
4690
4691 r = block_get_whole_disk(partition_devno, &whole_devno);
4692 if (r < 0)
4693 return log_error_errno(
4694 r,
4695 "Unable to determine containing block device of partition %u:%u: %m",
4696 major(partition_devno), minor(partition_devno));
4697
4698 if (restrict_devno != (dev_t) -1 &&
4699 restrict_devno != whole_devno)
4700 return log_error_errno(
4701 SYNTHETIC_ERRNO(EPERM),
4702 "Partition %u:%u is located outside of block device %u:%u, refusing.",
4703 major(partition_devno), minor(partition_devno),
4704 major(restrict_devno), minor(restrict_devno));
4705
ca822829 4706 fd = r = device_open_from_devnum(S_IFBLK, whole_devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &p);
5c08da58 4707 if (r < 0)
ca822829
YW
4708 return log_error_errno(r, "Failed to open block device " DEVNUM_FORMAT_STR ": %m",
4709 DEVNUM_FORMAT_VAL(whole_devno));
5c08da58
LP
4710
4711 b = blkid_new_probe();
4712 if (!b)
4713 return log_oom();
4714
4715 errno = 0;
4716 r = blkid_probe_set_device(b, fd, 0, 0);
4717 if (r != 0)
4718 return log_error_errno(errno_or_else(ENOMEM), "Failed to open block device '%s': %m", p);
4719
4720 (void) blkid_probe_enable_partitions(b, 1);
4721 (void) blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
4722
4723 errno = 0;
4724 r = blkid_do_safeprobe(b);
4725 if (IN_SET(r, -2, 1)) { /* nothing found or ambiguous result */
4726 log_debug("Didn't find partition table on block device '%s'.", p);
4727 return false;
4728 }
4729 if (r != 0)
4730 return log_error_errno(errno_or_else(EIO), "Unable to probe for partition table of '%s': %m", p);
4731
4732 (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL);
4733 if (!streq_ptr(pttype, "gpt")) {
4734 log_debug("Didn't find a GPT partition table on '%s'.", p);
4735 return false;
4736 }
4737
4738 errno = 0;
4739 pl = blkid_probe_get_partitions(b);
4740 if (!pl)
4741 return log_error_errno(errno_or_else(EIO), "Unable read partition table of '%s': %m", p);
4742 errno = 0;
4743
4744 pp = blkid_partlist_devno_to_partition(pl, partition_devno);
4745 if (!pp) {
4746 log_debug("Partition %u:%u has no matching partition table entry on '%s'.",
4747 major(partition_devno), minor(partition_devno), p);
4748 return false;
4749 }
4750
4751 t = blkid_partition_get_type_string(pp);
4752 if (isempty(t)) {
4753 log_debug("Partition %u:%u has no type on '%s'.",
4754 major(partition_devno), minor(partition_devno), p);
4755 return false;
4756 }
4757
4758 r = sd_id128_from_string(t, &pt_parsed);
4759 if (r < 0) {
4760 log_debug_errno(r, "Failed to parse partition type \"%s\": %m", t);
4761 return false;
4762 }
4763
22e932f4 4764 if (!sd_id128_equal(pt_parsed, partition_type.uuid)) {
5c08da58
LP
4765 log_debug("Partition %u:%u has non-matching partition type " SD_ID128_FORMAT_STR " (needed: " SD_ID128_FORMAT_STR "), ignoring.",
4766 major(partition_devno), minor(partition_devno),
22e932f4 4767 SD_ID128_FORMAT_VAL(pt_parsed), SD_ID128_FORMAT_VAL(partition_type.uuid));
5c08da58
LP
4768 return false;
4769 }
4770
4771 t = blkid_partition_get_uuid(pp);
4772 if (isempty(t)) {
4773 log_debug("Partition %u:%u has no UUID.",
4774 major(partition_devno), minor(partition_devno));
4775 return false;
4776 }
4777
4778 r = sd_id128_from_string(t, &u);
4779 if (r < 0) {
4780 log_debug_errno(r, "Failed to parse partition UUID \"%s\": %m", t);
4781 return false;
4782 }
4783
4784 log_debug("Automatically found partition %u:%u of right type " SD_ID128_FORMAT_STR ".",
4785 major(partition_devno), minor(partition_devno),
4786 SD_ID128_FORMAT_VAL(pt_parsed));
4787
4788 if (ret_uuid)
4789 *ret_uuid = u;
4790
4791 return true;
4792}
4793
4794static int find_backing_devno(
4795 const char *path,
4796 const char *root,
4797 dev_t *ret) {
4798
4799 _cleanup_free_ char *resolved = NULL;
4800 int r;
4801
4802 assert(path);
4803
4804 r = chase_symlinks(path, root, CHASE_PREFIX_ROOT, &resolved, NULL);
4805 if (r < 0)
4806 return r;
4807
4808 r = path_is_mount_point(resolved, NULL, 0);
4809 if (r < 0)
4810 return r;
4811 if (r == 0) /* Not a mount point, then it's not a partition of its own, let's not automatically use it. */
4812 return -ENOENT;
4813
4814 r = get_block_device(resolved, ret);
4815 if (r < 0)
4816 return r;
4817 if (r == 0) /* Not backed by physical file system, we can't use this */
4818 return -ENOENT;
4819
4820 return 0;
4821}
4822
4823static int resolve_copy_blocks_auto(
22e932f4 4824 GptPartitionType type,
5c08da58
LP
4825 const char *root,
4826 dev_t restrict_devno,
1a037ba2 4827 dev_t *ret_devno,
5c08da58
LP
4828 sd_id128_t *ret_uuid) {
4829
4830 const char *try1 = NULL, *try2 = NULL;
4831 char p[SYS_BLOCK_PATH_MAX("/slaves")];
4832 _cleanup_(closedirp) DIR *d = NULL;
4833 sd_id128_t found_uuid = SD_ID128_NULL;
4834 dev_t devno, found = 0;
4835 int r;
4836
5c08da58
LP
4837 /* Enforce some security restrictions: CopyBlocks=auto should not be an avenue to get outside of the
4838 * --root=/--image= confinement. Specifically, refuse CopyBlocks= in combination with --root= at all,
4839 * and restrict block device references in the --image= case to loopback block device we set up.
4840 *
4841 * restrict_devno contain the dev_t of the loop back device we operate on in case of --image=, and
4842 * thus declares which device (and its partition subdevices) we shall limit access to. If
4843 * restrict_devno is zero no device probing access shall be allowed at all (used for --root=) and if
4844 * it is (dev_t) -1 then free access shall be allowed (if neither switch is used). */
4845
4846 if (restrict_devno == 0)
4847 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
4848 "Automatic discovery of backing block devices not permitted in --root= mode, refusing.");
4849
4850 /* Handles CopyBlocks=auto, and finds the right source partition to copy from. We look for matching
4851 * partitions in the host, using the appropriate directory as key and ensuring that the partition
4852 * type matches. */
4853
22e932f4 4854 if (type.designator == PARTITION_ROOT)
5c08da58 4855 try1 = "/";
22e932f4 4856 else if (type.designator == PARTITION_USR)
5c08da58 4857 try1 = "/usr/";
22e932f4 4858 else if (type.designator == PARTITION_ROOT_VERITY)
5c08da58 4859 try1 = "/";
22e932f4 4860 else if (type.designator == PARTITION_USR_VERITY)
5c08da58 4861 try1 = "/usr/";
22e932f4 4862 else if (type.designator == PARTITION_ESP) {
5c08da58
LP
4863 try1 = "/efi/";
4864 try2 = "/boot/";
22e932f4 4865 } else if (type.designator == PARTITION_XBOOTLDR)
5c08da58
LP
4866 try1 = "/boot/";
4867 else
4868 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
4869 "Partition type " SD_ID128_FORMAT_STR " not supported from automatic source block device discovery.",
22e932f4 4870 SD_ID128_FORMAT_VAL(type.uuid));
5c08da58
LP
4871
4872 r = find_backing_devno(try1, root, &devno);
4873 if (r == -ENOENT && try2)
4874 r = find_backing_devno(try2, root, &devno);
4875 if (r < 0)
4876 return log_error_errno(r, "Failed to resolve automatic CopyBlocks= path for partition type " SD_ID128_FORMAT_STR ", sorry: %m",
22e932f4 4877 SD_ID128_FORMAT_VAL(type.uuid));
5c08da58
LP
4878
4879 xsprintf_sys_block_path(p, "/slaves", devno);
4880 d = opendir(p);
4881 if (d) {
4882 struct dirent *de;
4883
4884 for (;;) {
4885 _cleanup_free_ char *q = NULL, *t = NULL;
4886 sd_id128_t u;
4887 dev_t sl;
4888
4889 errno = 0;
4890 de = readdir_no_dot(d);
4891 if (!de) {
4892 if (errno != 0)
4893 return log_error_errno(errno, "Failed to read directory '%s': %m", p);
4894
4895 break;
4896 }
4897
4898 if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
4899 continue;
4900
4901 q = path_join(p, de->d_name, "/dev");
4902 if (!q)
4903 return log_oom();
4904
4905 r = read_one_line_file(q, &t);
4906 if (r < 0)
4907 return log_error_errno(r, "Failed to read %s: %m", q);
4908
7176f06c 4909 r = parse_devnum(t, &sl);
5c08da58
LP
4910 if (r < 0) {
4911 log_debug_errno(r, "Failed to parse %s, ignoring: %m", q);
4912 continue;
4913 }
4914 if (major(sl) == 0) {
4915 log_debug_errno(r, "Device backing %s is special, ignoring: %m", q);
4916 continue;
4917 }
4918
22e932f4 4919 r = resolve_copy_blocks_auto_candidate(sl, type, restrict_devno, &u);
5c08da58
LP
4920 if (r < 0)
4921 return r;
4922 if (r > 0) {
4923 /* We found a matching one! */
4924 if (found != 0)
4925 return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ),
4926 "Multiple matching partitions found, refusing.");
4927
4928 found = sl;
4929 found_uuid = u;
4930 }
4931 }
4932 } else if (errno != ENOENT)
4933 return log_error_errno(errno, "Failed open %s: %m", p);
4934 else {
22e932f4 4935 r = resolve_copy_blocks_auto_candidate(devno, type, restrict_devno, &found_uuid);
5c08da58
LP
4936 if (r < 0)
4937 return r;
4938 if (r > 0)
4939 found = devno;
4940 }
4941
4942 if (found == 0)
4943 return log_error_errno(SYNTHETIC_ERRNO(ENXIO),
4944 "Unable to automatically discover suitable partition to copy blocks from.");
4945
1a037ba2
YW
4946 if (ret_devno)
4947 *ret_devno = found;
5c08da58
LP
4948
4949 if (ret_uuid)
4950 *ret_uuid = found_uuid;
4951
4952 return 0;
4953}
4954
4955static int context_open_copy_block_paths(
4956 Context *context,
5c08da58
LP
4957 dev_t restrict_devno) {
4958
757bc2e4
LP
4959 int r;
4960
4961 assert(context);
4962
4963 LIST_FOREACH(partitions, p, context->partitions) {
4964 _cleanup_close_ int source_fd = -1;
5c08da58
LP
4965 _cleanup_free_ char *opened = NULL;
4966 sd_id128_t uuid = SD_ID128_NULL;
757bc2e4
LP
4967 uint64_t size;
4968 struct stat st;
4969
4970 assert(p->copy_blocks_fd < 0);
4971 assert(p->copy_blocks_size == UINT64_MAX);
4972
4973 if (PARTITION_EXISTS(p)) /* Never copy over partitions that already exist! */
4974 continue;
4975
5c08da58 4976 if (p->copy_blocks_path) {
757bc2e4 4977
585c5c75 4978 source_fd = chase_symlinks_and_open(p->copy_blocks_path, p->copy_blocks_root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened);
5c08da58
LP
4979 if (source_fd < 0)
4980 return log_error_errno(source_fd, "Failed to open '%s': %m", p->copy_blocks_path);
757bc2e4 4981
5c08da58
LP
4982 if (fstat(source_fd, &st) < 0)
4983 return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened);
4984
4985 if (!S_ISREG(st.st_mode) && restrict_devno != (dev_t) -1)
4986 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
4987 "Copying from block device node is not permitted in --image=/--root= mode, refusing.");
4988
4989 } else if (p->copy_blocks_auto) {
1a037ba2 4990 dev_t devno;
5c08da58 4991
22e932f4 4992 r = resolve_copy_blocks_auto(p->type, p->copy_blocks_root, restrict_devno, &devno, &uuid);
5c08da58
LP
4993 if (r < 0)
4994 return r;
4995
ca822829 4996 source_fd = r = device_open_from_devnum(S_IFBLK, devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened);
1a037ba2 4997 if (r < 0)
ca822829
YW
4998 return log_error_errno(r, "Failed to open automatically determined source block copy device " DEVNUM_FORMAT_STR ": %m",
4999 DEVNUM_FORMAT_VAL(devno));
5c08da58
LP
5000
5001 if (fstat(source_fd, &st) < 0)
5002 return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened);
1a037ba2 5003 } else
5c08da58 5004 continue;
757bc2e4
LP
5005
5006 if (S_ISDIR(st.st_mode)) {
ca822829
YW
5007 _cleanup_free_ char *bdev = NULL;
5008 dev_t devt;
757bc2e4
LP
5009
5010 /* If the file is a directory, automatically find the backing block device */
5011
5012 if (major(st.st_dev) != 0)
ca822829 5013 devt = st.st_dev;
757bc2e4 5014 else {
757bc2e4 5015 /* Special support for btrfs */
757bc2e4 5016 r = btrfs_get_block_device_fd(source_fd, &devt);
67f0ac8c 5017 if (r == -EUCLEAN)
5c08da58 5018 return btrfs_log_dev_root(LOG_ERR, r, opened);
757bc2e4 5019 if (r < 0)
5c08da58 5020 return log_error_errno(r, "Unable to determine backing block device of '%s': %m", opened);
757bc2e4 5021 }
757bc2e4
LP
5022
5023 safe_close(source_fd);
5024
ca822829
YW
5025 source_fd = r = device_open_from_devnum(S_IFBLK, devt, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &bdev);
5026 if (r < 0)
5027 return log_error_errno(r, "Failed to open block device backing '%s': %m", opened);
757bc2e4
LP
5028
5029 if (fstat(source_fd, &st) < 0)
5030 return log_error_errno(errno, "Failed to stat block device '%s': %m", bdev);
757bc2e4
LP
5031 }
5032
5033 if (S_ISREG(st.st_mode))
5034 size = st.st_size;
5035 else if (S_ISBLK(st.st_mode)) {
5036 if (ioctl(source_fd, BLKGETSIZE64, &size) != 0)
5037 return log_error_errno(errno, "Failed to determine size of block device to copy from: %m");
5038 } else
5c08da58 5039 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
5040
5041 if (size <= 0)
5c08da58 5042 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File to copy bytes from '%s' has zero size, refusing.", opened);
757bc2e4 5043 if (size % 512 != 0)
5c08da58 5044 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
5045
5046 p->copy_blocks_fd = TAKE_FD(source_fd);
5047 p->copy_blocks_size = size;
5c08da58
LP
5048
5049 free_and_replace(p->copy_blocks_path, opened);
5050
5051 /* When copying from an existing partition copy that partitions UUID if none is configured explicitly */
11749b61 5052 if (!p->new_uuid_is_set && !sd_id128_is_null(uuid)) {
5c08da58 5053 p->new_uuid = uuid;
11749b61
DDM
5054 p->new_uuid_is_set = true;
5055 }
757bc2e4
LP
5056 }
5057
5058 return 0;
5059}
5060
c4a87b76
DDM
5061static int fd_apparent_size(int fd, uint64_t *ret) {
5062 off_t initial = 0;
5063 uint64_t size = 0;
5064
5065 assert(fd >= 0);
5066 assert(ret);
5067
5068 initial = lseek(fd, 0, SEEK_CUR);
5069 if (initial < 0)
5070 return log_error_errno(errno, "Failed to get file offset: %m");
5071
5072 for (off_t off = 0;;) {
5073 off_t r;
5074
5075 r = lseek(fd, off, SEEK_DATA);
5076 if (r < 0 && errno == ENXIO)
5077 /* If errno == ENXIO, that means we've reached the final hole of the file and
5078 * that hole isn't followed by more data. */
5079 break;
5080 if (r < 0)
5081 return log_error_errno(errno, "Failed to seek data in file from offset %"PRIi64": %m", off);
5082
5083 off = r; /* Set the offset to the start of the data segment. */
5084
5085 /* After copying a potential hole, find the end of the data segment by looking for
5086 * the next hole. If we get ENXIO, we're at EOF. */
5087 r = lseek(fd, off, SEEK_HOLE);
5088 if (r < 0) {
5089 if (errno == ENXIO)
5090 break;
5091 return log_error_errno(errno, "Failed to seek hole in file from offset %"PRIi64": %m", off);
5092 }
5093
5094 size += r - off;
5095 off = r;
5096 }
5097
5098 if (lseek(fd, initial, SEEK_SET) < 0)
5099 return log_error_errno(errno, "Failed to reset file offset: %m");
5100
5101 *ret = size;
5102
5103 return 0;
5104}
5105
5106static int context_minimize(Context *context) {
5107 _cleanup_set_free_ Set *denylist = NULL;
5108 const char *vt;
5109 int r;
5110
5111 assert(context);
5112
5113 r = make_copy_files_denylist(context, &denylist);
5114 if (r < 0)
5115 return r;
5116
5117 r = var_tmp_dir(&vt);
5118 if (r < 0)
5119 return log_error_errno(r, "Could not determine temporary directory: %m");
5120
5121 LIST_FOREACH(partitions, p, context->partitions) {
5122 _cleanup_(rm_rf_physical_and_freep) char *tmp_root = NULL;
5123 _cleanup_(unlink_and_freep) char *temp = NULL;
5124 _cleanup_free_ char *root = NULL;
5125 _cleanup_close_ int fd = -1;
5126 sd_id128_t fs_uuid;
5127 uint64_t fsz;
5128
5129 if (p->dropped)
5130 continue;
5131
5132 if (PARTITION_EXISTS(p)) /* Never format existing partitions */
5133 continue;
5134
5135 if (!p->format)
5136 continue;
5137
5138 if (!p->minimize)
5139 continue;
5140
5141 assert(!p->copy_blocks_path);
5142
5143 r = tempfn_random_child(vt, "repart", &temp);
5144 if (r < 0)
5145 return log_error_errno(r, "Failed to generate temporary file path: %m");
5146
59e2be46
DDM
5147 if (fstype_is_ro(p->format))
5148 fs_uuid = p->fs_uuid;
5149 else {
c4a87b76
DDM
5150 fd = open(temp, O_CREAT|O_EXCL|O_CLOEXEC|O_RDWR|O_NOCTTY, 0600);
5151 if (fd < 0)
5152 return log_error_errno(errno, "Failed to open temporary file %s: %m", temp);
5153
5154 /* This may seem huge but it will be created sparse so it doesn't take up any space
5155 * on disk until written to. */
5156 if (ftruncate(fd, 1024ULL * 1024ULL * 1024ULL * 1024ULL) < 0)
5157 return log_error_errno(errno, "Failed to truncate temporary file to %s: %m",
5158 FORMAT_BYTES(1024ULL * 1024ULL * 1024ULL * 1024ULL));
5159
5160 /* We're going to populate this filesystem twice so use a random UUID the first time
5161 * to avoid UUID conflicts. */
5162 r = sd_id128_randomize(&fs_uuid);
5163 if (r < 0)
5164 return r;
59e2be46
DDM
5165 }
5166
5167 if (mkfs_supports_root_option(p->format)) {
c4a87b76
DDM
5168 r = partition_populate_directory(p, denylist, &root, &tmp_root);
5169 if (r < 0)
5170 return r;
c4a87b76
DDM
5171 }
5172
5173 r = make_filesystem(temp, p->format, strempty(p->new_label), root ?: tmp_root, fs_uuid,
5174 arg_discard);
5175 if (r < 0)
5176 return r;
5177
5178 /* Read-only filesystems are minimal from the first try because they create and size the
5179 * loopback file for us. */
5180 if (fstype_is_ro(p->format)) {
5181 p->copy_blocks_path = TAKE_PTR(temp);
5182 continue;
5183 }
5184
59e2be46
DDM
5185 if (!mkfs_supports_root_option(p->format)) {
5186 r = partition_populate_filesystem(p, temp, denylist);
5187 if (r < 0)
5188 return r;
5189 }
c4a87b76
DDM
5190
5191 /* Other filesystems need to be provided with a pre-sized loopback file and will adapt to
5192 * fully occupy it. Because we gave the filesystem a 1T sparse file, we need to shrink the
5193 * filesystem down to a reasonable size again to fit it in the disk image. While there are
5194 * some filesystems that support shrinking, it doesn't always work properly (e.g. shrinking
5195 * btrfs gives us a 2.0G filesystem regardless of what we put in it). Instead, let's populate
5196 * the filesystem again, but this time, instead of providing the filesystem with a 1T sparse
5197 * loopback file, let's size the loopback file based on the actual data used by the
5198 * filesystem in the sparse file after the first attempt. This should be a good guess of the
5199 * minimal amount of space needed in the filesystem to fit all the required data.
5200 */
5201 r = fd_apparent_size(fd, &fsz);
5202 if (r < 0)
5203 return r;
5204
5205 /* Massage the size a bit because just going by actual data used in the sparse file isn't
5206 * fool-proof. */
5207 fsz = round_up_size(fsz + (fsz / 2), context->grain_size);
5208 if (minimal_size_by_fs_name(p->format) != UINT64_MAX)
5209 fsz = MAX(minimal_size_by_fs_name(p->format), fsz);
5210
5211 /* Erase the previous filesystem first. */
5212 if (ftruncate(fd, 0))
5213 return log_error_errno(errno, "Failed to erase temporary file: %m");
5214
5215 if (ftruncate(fd, fsz))
5216 return log_error_errno(errno, "Failed to truncate temporary file to %s: %m", FORMAT_BYTES(fsz));
5217
5218 r = make_filesystem(temp, p->format, strempty(p->new_label), root ?: tmp_root, p->fs_uuid,
5219 arg_discard);
5220 if (r < 0)
5221 return r;
5222
59e2be46
DDM
5223 if (!mkfs_supports_root_option(p->format)) {
5224 r = partition_populate_filesystem(p, temp, denylist);
5225 if (r < 0)
5226 return r;
5227 }
c4a87b76
DDM
5228
5229 p->copy_blocks_path = TAKE_PTR(temp);
5230 }
5231
5232 return 0;
5233}
5234
81d1098b
DDM
5235static int parse_filter_partitions(const char *p) {
5236 int r;
5237
5238 for (;;) {
5239 _cleanup_free_ char *name = NULL;
5240 GptPartitionType type;
5241
5242 r = extract_first_word(&p, &name, ",", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS);
5243 if (r == 0)
5244 break;
5245 if (r < 0)
5246 return log_error_errno(r, "Failed to extract partition designator: %s", optarg);
5247
5248 r = gpt_partition_type_from_string(name, &type);
5249 if (r < 0)
5250 return log_error_errno(r, "'%s' is not a valid partition designator", name);
5251
5252 if (!GREEDY_REALLOC(arg_filter_partitions, arg_filter_partitions_size + 1))
5253 return log_oom();
5254
5255 arg_filter_partitions[arg_filter_partitions_size++] = type.uuid;
5256 }
5257
5258 return 0;
5259}
5260
e594a3b1
LP
5261static int help(void) {
5262 _cleanup_free_ char *link = NULL;
5263 int r;
5264
5265 r = terminal_urlify_man("systemd-repart", "1", &link);
5266 if (r < 0)
5267 return log_oom();
5268
5269 printf("%s [OPTIONS...] [DEVICE]\n"
5270 "\n%sGrow and add partitions to partition table.%s\n\n"
5271 " -h --help Show this help\n"
5272 " --version Show package version\n"
896e678b
LP
5273 " --no-pager Do not pipe output into a pager\n"
5274 " --no-legend Do not show the headers and footers\n"
e594a3b1 5275 " --dry-run=BOOL Whether to run dry-run operation\n"
a26f4a49
LP
5276 " --empty=MODE One of refuse, allow, require, force, create; controls\n"
5277 " how to handle empty disks lacking partition tables\n"
e594a3b1 5278 " --discard=BOOL Whether to discard backing blocks for new partitions\n"
2d2d0a57 5279 " --pretty=BOOL Whether to show pretty summary before doing changes\n"
e594a3b1
LP
5280 " --factory-reset=BOOL Whether to remove data partitions before recreating\n"
5281 " them\n"
5282 " --can-factory-reset Test whether factory reset is defined\n"
5283 " --root=PATH Operate relative to root path\n"
252d6267 5284 " --image=PATH Operate relative to image file\n"
9d252fbb 5285 " --definitions=DIR Find partition definitions in specified directory\n"
b9df3536 5286 " --key-file=PATH Key to use when encrypting partitions\n"
b456191d
DDM
5287 " --private-key=PATH Private key to use when generating verity roothash\n"
5288 " signatures\n"
5289 " --certificate=PATH PEM certificate to use when generating verity\n"
5290 " roothash signatures\n"
889914ef 5291 " --tpm2-device=PATH Path to TPM2 device node to use\n"
a1788a69 5292 " --tpm2-pcrs=PCR1+PCR2+PCR3+…\n"
889914ef 5293 " TPM2 PCR indexes to use for TPM2 enrollment\n"
02ef97cd
LP
5294 " --tpm2-public-key=PATH\n"
5295 " Enroll signed TPM2 PCR policy against PEM public key\n"
5296 " --tpm2-public-key-pcrs=PCR1+PCR2+PCR3+…\n"
5297 " Enroll signed TPM2 PCR policy for specified TPM2 PCRs\n"
e594a3b1 5298 " --seed=UUID 128bit seed UUID to derive all UUIDs from\n"
a26f4a49 5299 " --size=BYTES Grow loopback file to specified size\n"
2d2d0a57 5300 " --json=pretty|short|off\n"
de8231b0 5301 " Generate JSON output\n"
4cee8333 5302 " --split=BOOL Whether to generate split artifacts\n"
81d1098b
DDM
5303 " --include-partitions=PARTITION1,PARTITION2,PARTITION3,…\n"
5304 " Only operate on partitions of the specified types\n"
5305 " --exclude-partitions=PARTITION1,PARTITION2,PARTITION3,…\n"
5306 " Don't operate on partitions of the specified types\n"
bc556335
DDM
5307 "\nSee the %s for details.\n",
5308 program_invocation_short_name,
5309 ansi_highlight(),
5310 ansi_normal(),
5311 link);
e594a3b1
LP
5312
5313 return 0;
5314}
5315
5316static int parse_argv(int argc, char *argv[]) {
5317
5318 enum {
5319 ARG_VERSION = 0x100,
896e678b
LP
5320 ARG_NO_PAGER,
5321 ARG_NO_LEGEND,
e594a3b1
LP
5322 ARG_DRY_RUN,
5323 ARG_EMPTY,
5324 ARG_DISCARD,
5325 ARG_FACTORY_RESET,
5326 ARG_CAN_FACTORY_RESET,
5327 ARG_ROOT,
252d6267 5328 ARG_IMAGE,
e594a3b1
LP
5329 ARG_SEED,
5330 ARG_PRETTY,
5331 ARG_DEFINITIONS,
a26f4a49 5332 ARG_SIZE,
a015fbe7 5333 ARG_JSON,
b9df3536 5334 ARG_KEY_FILE,
b456191d
DDM
5335 ARG_PRIVATE_KEY,
5336 ARG_CERTIFICATE,
889914ef
LP
5337 ARG_TPM2_DEVICE,
5338 ARG_TPM2_PCRS,
02ef97cd
LP
5339 ARG_TPM2_PUBLIC_KEY,
5340 ARG_TPM2_PUBLIC_KEY_PCRS,
4cee8333 5341 ARG_SPLIT,
81d1098b
DDM
5342 ARG_INCLUDE_PARTITIONS,
5343 ARG_EXCLUDE_PARTITIONS,
e594a3b1
LP
5344 };
5345
5346 static const struct option options[] = {
02ef97cd
LP
5347 { "help", no_argument, NULL, 'h' },
5348 { "version", no_argument, NULL, ARG_VERSION },
5349 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
5350 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
5351 { "dry-run", required_argument, NULL, ARG_DRY_RUN },
5352 { "empty", required_argument, NULL, ARG_EMPTY },
5353 { "discard", required_argument, NULL, ARG_DISCARD },
5354 { "factory-reset", required_argument, NULL, ARG_FACTORY_RESET },
5355 { "can-factory-reset", no_argument, NULL, ARG_CAN_FACTORY_RESET },
5356 { "root", required_argument, NULL, ARG_ROOT },
5357 { "image", required_argument, NULL, ARG_IMAGE },
5358 { "seed", required_argument, NULL, ARG_SEED },
5359 { "pretty", required_argument, NULL, ARG_PRETTY },
5360 { "definitions", required_argument, NULL, ARG_DEFINITIONS },
5361 { "size", required_argument, NULL, ARG_SIZE },
5362 { "json", required_argument, NULL, ARG_JSON },
5363 { "key-file", required_argument, NULL, ARG_KEY_FILE },
b456191d
DDM
5364 { "private-key", required_argument, NULL, ARG_PRIVATE_KEY },
5365 { "certificate", required_argument, NULL, ARG_CERTIFICATE },
02ef97cd
LP
5366 { "tpm2-device", required_argument, NULL, ARG_TPM2_DEVICE },
5367 { "tpm2-pcrs", required_argument, NULL, ARG_TPM2_PCRS },
5368 { "tpm2-public-key", required_argument, NULL, ARG_TPM2_PUBLIC_KEY },
5369 { "tpm2-public-key-pcrs", required_argument, NULL, ARG_TPM2_PUBLIC_KEY_PCRS },
4cee8333 5370 { "split", required_argument, NULL, ARG_SPLIT },
81d1098b
DDM
5371 { "include-partitions", required_argument, NULL, ARG_INCLUDE_PARTITIONS },
5372 { "exclude-partitions", required_argument, NULL, ARG_EXCLUDE_PARTITIONS },
e594a3b1
LP
5373 {}
5374 };
5375
a26f4a49 5376 int c, r, dry_run = -1;
e594a3b1
LP
5377
5378 assert(argc >= 0);
5379 assert(argv);
5380
5381 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
5382
5383 switch (c) {
5384
5385 case 'h':
5386 return help();
5387
5388 case ARG_VERSION:
5389 return version();
5390
896e678b
LP
5391 case ARG_NO_PAGER:
5392 arg_pager_flags |= PAGER_DISABLE;
5393 break;
5394
5395 case ARG_NO_LEGEND:
5396 arg_legend = false;
5397 break;
5398
e594a3b1 5399 case ARG_DRY_RUN:
599c7c54 5400 r = parse_boolean_argument("--dry-run=", optarg, &arg_dry_run);
e594a3b1 5401 if (r < 0)
599c7c54 5402 return r;
e594a3b1
LP
5403 break;
5404
5405 case ARG_EMPTY:
5406 if (isempty(optarg) || streq(optarg, "refuse"))
5407 arg_empty = EMPTY_REFUSE;
5408 else if (streq(optarg, "allow"))
5409 arg_empty = EMPTY_ALLOW;
5410 else if (streq(optarg, "require"))
5411 arg_empty = EMPTY_REQUIRE;
5412 else if (streq(optarg, "force"))
5413 arg_empty = EMPTY_FORCE;
a26f4a49
LP
5414 else if (streq(optarg, "create")) {
5415 arg_empty = EMPTY_CREATE;
5416
5417 if (dry_run < 0)
5418 dry_run = false; /* Imply --dry-run=no if we create the loopback file
5419 * anew. After all we cannot really break anyone's
5420 * partition tables that way. */
5421 } else
e594a3b1
LP
5422 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
5423 "Failed to parse --empty= parameter: %s", optarg);
5424 break;
5425
5426 case ARG_DISCARD:
599c7c54 5427 r = parse_boolean_argument("--discard=", optarg, &arg_discard);
e594a3b1 5428 if (r < 0)
599c7c54 5429 return r;
e594a3b1
LP
5430 break;
5431
5432 case ARG_FACTORY_RESET:
c3470872 5433 r = parse_boolean_argument("--factory-reset=", optarg, NULL);
e594a3b1 5434 if (r < 0)
c3470872 5435 return r;
e594a3b1
LP
5436 arg_factory_reset = r;
5437 break;
5438
5439 case ARG_CAN_FACTORY_RESET:
5440 arg_can_factory_reset = true;
5441 break;
5442
5443 case ARG_ROOT:
252d6267
LP
5444 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root);
5445 if (r < 0)
5446 return r;
5447 break;
5448
5449 case ARG_IMAGE:
5450 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
e594a3b1
LP
5451 if (r < 0)
5452 return r;
5453 break;
5454
5455 case ARG_SEED:
5456 if (isempty(optarg)) {
5457 arg_seed = SD_ID128_NULL;
5458 arg_randomize = false;
5459 } else if (streq(optarg, "random"))
5460 arg_randomize = true;
5461 else {
5462 r = sd_id128_from_string(optarg, &arg_seed);
5463 if (r < 0)
5464 return log_error_errno(r, "Failed to parse seed: %s", optarg);
5465
5466 arg_randomize = false;
5467 }
5468
5469 break;
5470
5471 case ARG_PRETTY:
c3470872 5472 r = parse_boolean_argument("--pretty=", optarg, NULL);
e594a3b1 5473 if (r < 0)
c3470872 5474 return r;
e594a3b1
LP
5475 arg_pretty = r;
5476 break;
5477
224c853f
RP
5478 case ARG_DEFINITIONS: {
5479 _cleanup_free_ char *path = NULL;
5480 r = parse_path_argument(optarg, false, &path);
e594a3b1
LP
5481 if (r < 0)
5482 return r;
224c853f
RP
5483 if (strv_consume(&arg_definitions, TAKE_PTR(path)) < 0)
5484 return log_oom();
e594a3b1 5485 break;
224c853f 5486 }
e594a3b1 5487
a26f4a49
LP
5488 case ARG_SIZE: {
5489 uint64_t parsed, rounded;
5490
170c9823
LP
5491 if (streq(optarg, "auto")) {
5492 arg_size = UINT64_MAX;
5493 arg_size_auto = true;
5494 break;
5495 }
5496
a26f4a49
LP
5497 r = parse_size(optarg, 1024, &parsed);
5498 if (r < 0)
5499 return log_error_errno(r, "Failed to parse --size= parameter: %s", optarg);
5500
5501 rounded = round_up_size(parsed, 4096);
5502 if (rounded == 0)
5503 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too small, refusing.");
5504 if (rounded == UINT64_MAX)
5505 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too large, refusing.");
5506
5507 if (rounded != parsed)
e2341b6b
DT
5508 log_warning("Specified size is not a multiple of 4096, rounding up automatically. (%" PRIu64 " %s %" PRIu64 ")",
5509 parsed, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), rounded);
a26f4a49
LP
5510
5511 arg_size = rounded;
170c9823 5512 arg_size_auto = false;
a26f4a49
LP
5513 break;
5514 }
b9df3536 5515
a015fbe7 5516 case ARG_JSON:
b1e8f46c 5517 r = parse_json_argument(optarg, &arg_json_format_flags);
6a01ea4a
LP
5518 if (r <= 0)
5519 return r;
a015fbe7
TH
5520
5521 break;
5522
b9df3536
LP
5523 case ARG_KEY_FILE: {
5524 _cleanup_(erase_and_freep) char *k = NULL;
5525 size_t n = 0;
5526
8b3c3a49 5527 r = read_full_file_full(
986311c2 5528 AT_FDCWD, optarg, UINT64_MAX, SIZE_MAX,
8b3c3a49
LP
5529 READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
5530 NULL,
5531 &k, &n);
b9df3536
LP
5532 if (r < 0)
5533 return log_error_errno(r, "Failed to read key file '%s': %m", optarg);
5534
5535 erase_and_free(arg_key);
5536 arg_key = TAKE_PTR(k);
5537 arg_key_size = n;
5538 break;
5539 }
a26f4a49 5540
b456191d
DDM
5541 case ARG_PRIVATE_KEY: {
5542 _cleanup_(erase_and_freep) char *k = NULL;
5543 size_t n = 0;
5544
5545 r = read_full_file_full(
5546 AT_FDCWD, optarg, UINT64_MAX, SIZE_MAX,
5547 READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
5548 NULL,
5549 &k, &n);
5550 if (r < 0)
5551 return log_error_errno(r, "Failed to read key file '%s': %m", optarg);
5552
5553 EVP_PKEY_free(arg_private_key);
5554 arg_private_key = NULL;
5555 r = parse_private_key(k, n, &arg_private_key);
5556 if (r < 0)
5557 return r;
5558 break;
5559 }
5560
5561 case ARG_CERTIFICATE: {
5562 _cleanup_free_ char *cert = NULL;
5563 size_t n = 0;
5564
5565 r = read_full_file_full(
5566 AT_FDCWD, optarg, UINT64_MAX, SIZE_MAX,
5567 READ_FULL_FILE_CONNECT_SOCKET,
5568 NULL,
5569 &cert, &n);
5570 if (r < 0)
5571 return log_error_errno(r, "Failed to read certificate file '%s': %m", optarg);
5572
5573 X509_free(arg_certificate);
5574 arg_certificate = NULL;
5575 r = parse_x509_certificate(cert, n, &arg_certificate);
5576 if (r < 0)
5577 return r;
5578 break;
5579 }
5580
889914ef
LP
5581 case ARG_TPM2_DEVICE: {
5582 _cleanup_free_ char *device = NULL;
5583
5584 if (streq(optarg, "list"))
5585 return tpm2_list_devices();
5586
5587 if (!streq(optarg, "auto")) {
5588 device = strdup(optarg);
5589 if (!device)
5590 return log_oom();
5591 }
5592
5593 free(arg_tpm2_device);
5594 arg_tpm2_device = TAKE_PTR(device);
5595 break;
5596 }
5597
222a951f
LP
5598 case ARG_TPM2_PCRS:
5599 r = tpm2_parse_pcr_argument(optarg, &arg_tpm2_pcr_mask);
889914ef
LP
5600 if (r < 0)
5601 return r;
5602
889914ef 5603 break;
889914ef 5604
02ef97cd
LP
5605 case ARG_TPM2_PUBLIC_KEY:
5606 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_tpm2_public_key);
5607 if (r < 0)
5608 return r;
5609
5610 break;
5611
5612 case ARG_TPM2_PUBLIC_KEY_PCRS:
5613 r = tpm2_parse_pcr_argument(optarg, &arg_tpm2_public_key_pcr_mask);
5614 if (r < 0)
5615 return r;
5616
5617 break;
5618
4cee8333
DDM
5619 case ARG_SPLIT:
5620 r = parse_boolean_argument("--split=", optarg, NULL);
5621 if (r < 0)
5622 return r;
5623
5624 arg_split = r;
5625 break;
5626
81d1098b
DDM
5627 case ARG_INCLUDE_PARTITIONS:
5628 if (arg_filter_partitions_type == FILTER_PARTITIONS_EXCLUDE)
5629 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
5630 "Combination of --include-partitions= and --exclude-partitions= is invalid.");
5631
5632 r = parse_filter_partitions(optarg);
5633 if (r < 0)
5634 return r;
5635
5636 arg_filter_partitions_type = FILTER_PARTITIONS_INCLUDE;
5637
5638 break;
5639
5640 case ARG_EXCLUDE_PARTITIONS:
5641 if (arg_filter_partitions_type == FILTER_PARTITIONS_INCLUDE)
5642 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
5643 "Combination of --include-partitions= and --exclude-partitions= is invalid.");
5644
5645 r = parse_filter_partitions(optarg);
5646 if (r < 0)
5647 return r;
5648
5649 arg_filter_partitions_type = FILTER_PARTITIONS_EXCLUDE;
5650
5651 break;
5652
e594a3b1
LP
5653 case '?':
5654 return -EINVAL;
5655
5656 default:
04499a70 5657 assert_not_reached();
e594a3b1
LP
5658 }
5659
5660 if (argc - optind > 1)
5661 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
5662 "Expected at most one argument, the path to the block device.");
5663
a26f4a49 5664 if (arg_factory_reset > 0 && IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE))
e594a3b1 5665 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
a26f4a49 5666 "Combination of --factory-reset=yes and --empty=force/--empty=require/--empty=create is invalid.");
e594a3b1
LP
5667
5668 if (arg_can_factory_reset)
a26f4a49
LP
5669 arg_dry_run = true; /* When --can-factory-reset is specified we don't make changes, hence
5670 * non-dry-run mode makes no sense. Thus, imply dry run mode so that we
5671 * open things strictly read-only. */
5672 else if (dry_run >= 0)
5673 arg_dry_run = dry_run;
5674
170c9823 5675 if (arg_empty == EMPTY_CREATE && (arg_size == UINT64_MAX && !arg_size_auto))
a26f4a49
LP
5676 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
5677 "If --empty=create is specified, --size= must be specified, too.");
e594a3b1 5678
252d6267
LP
5679 if (arg_image && arg_root)
5680 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
5681 else if (!arg_image && !arg_root && in_initrd()) {
8f47e32a
LP
5682
5683 /* By default operate on /sysusr/ or /sysroot/ when invoked in the initrd. We prefer the
5684 * former, if it is mounted, so that we have deterministic behaviour on systems where /usr/
5685 * is vendor-supplied but the root fs formatted on first boot. */
5686 r = path_is_mount_point("/sysusr/usr", NULL, 0);
5687 if (r <= 0) {
5688 if (r < 0 && r != -ENOENT)
5689 log_debug_errno(r, "Unable to determine whether /sysusr/usr is a mount point, assuming it is not: %m");
5690
5691 arg_root = strdup("/sysroot");
5692 } else
5693 arg_root = strdup("/sysusr");
252d6267
LP
5694 if (!arg_root)
5695 return log_oom();
5696 }
5697
e594a3b1 5698 arg_node = argc > optind ? argv[optind] : NULL;
a26f4a49 5699
252d6267 5700 if (IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE) && !arg_node && !arg_image)
a26f4a49
LP
5701 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
5702 "A path to a device node or loopback file must be specified when --empty=force, --empty=require or --empty=create are used.");
5703
4cee8333
DDM
5704 if (arg_split && !arg_node)
5705 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
5706 "A path to a loopback file must be specified when --split is used.");
5707
889914ef
LP
5708 if (arg_tpm2_pcr_mask == UINT32_MAX)
5709 arg_tpm2_pcr_mask = TPM2_PCR_MASK_DEFAULT;
02ef97cd
LP
5710 if (arg_tpm2_public_key_pcr_mask == UINT32_MAX)
5711 arg_tpm2_public_key_pcr_mask = UINT32_C(1) << TPM_PCR_INDEX_KERNEL_IMAGE;
889914ef 5712
a26d463d
DDM
5713 if (arg_pretty < 0 && isatty(STDOUT_FILENO))
5714 arg_pretty = true;
5715
e594a3b1
LP
5716 return 1;
5717}
5718
5719static int parse_proc_cmdline_factory_reset(void) {
5720 bool b;
5721 int r;
5722
5723 if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */
5724 return 0;
5725
5726 if (!in_initrd()) /* Never honour kernel command line factory reset request outside of the initrd */
5727 return 0;
5728
5729 r = proc_cmdline_get_bool("systemd.factory_reset", &b);
5730 if (r < 0)
5731 return log_error_errno(r, "Failed to parse systemd.factory_reset kernel command line argument: %m");
5732 if (r > 0) {
5733 arg_factory_reset = b;
5734
5735 if (b)
5736 log_notice("Honouring factory reset requested via kernel command line.");
5737 }
5738
5739 return 0;
5740}
5741
5742static int parse_efi_variable_factory_reset(void) {
5743 _cleanup_free_ char *value = NULL;
5744 int r;
5745
5746 if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */
5747 return 0;
5748
5749 if (!in_initrd()) /* Never honour EFI variable factory reset request outside of the initrd */
5750 return 0;
5751
e6f055cb 5752 r = efi_get_variable_string(EFI_SYSTEMD_VARIABLE(FactoryReset), &value);
e594a3b1
LP
5753 if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
5754 return 0;
5755 if (r < 0)
5756 return log_error_errno(r, "Failed to read EFI variable FactoryReset: %m");
5757
5758 r = parse_boolean(value);
5759 if (r < 0)
5760 return log_error_errno(r, "Failed to parse EFI variable FactoryReset: %m");
5761
5762 arg_factory_reset = r;
5763 if (r)
111a3aae 5764 log_notice("Factory reset requested via EFI variable FactoryReset.");
e594a3b1
LP
5765
5766 return 0;
5767}
5768
5769static int remove_efi_variable_factory_reset(void) {
5770 int r;
5771
e6f055cb 5772 r = efi_set_variable(EFI_SYSTEMD_VARIABLE(FactoryReset), NULL, 0);
e594a3b1
LP
5773 if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
5774 return 0;
5775 if (r < 0)
5776 return log_error_errno(r, "Failed to remove EFI variable FactoryReset: %m");
5777
5778 log_info("Successfully unset EFI variable FactoryReset.");
5779 return 0;
5780}
5781
252d6267
LP
5782static int acquire_root_devno(
5783 const char *p,
5784 const char *root,
5785 int mode,
5786 char **ret,
5787 int *ret_fd) {
5788
5789 _cleanup_free_ char *found_path = NULL;
5790 dev_t devno, fd_devno = MODE_INVALID;
e594a3b1
LP
5791 _cleanup_close_ int fd = -1;
5792 struct stat st;
e594a3b1
LP
5793 int r;
5794
a26f4a49
LP
5795 assert(p);
5796 assert(ret);
5797 assert(ret_fd);
5798
252d6267 5799 fd = chase_symlinks_and_open(p, root, CHASE_PREFIX_ROOT, mode, &found_path);
e594a3b1 5800 if (fd < 0)
252d6267 5801 return fd;
e594a3b1
LP
5802
5803 if (fstat(fd, &st) < 0)
5804 return -errno;
5805
5806 if (S_ISREG(st.st_mode)) {
252d6267 5807 *ret = TAKE_PTR(found_path);
a26f4a49 5808 *ret_fd = TAKE_FD(fd);
e594a3b1
LP
5809 return 0;
5810 }
5811
252d6267
LP
5812 if (S_ISBLK(st.st_mode)) {
5813 /* Refuse referencing explicit block devices if a root dir is specified, after all we should
5c08da58 5814 * not be able to leave the image the root path constrains us to. */
252d6267
LP
5815 if (root)
5816 return -EPERM;
5817
a26f4a49 5818 fd_devno = devno = st.st_rdev;
252d6267 5819 } else if (S_ISDIR(st.st_mode)) {
e594a3b1
LP
5820
5821 devno = st.st_dev;
a26f4a49 5822 if (major(devno) == 0) {
e594a3b1
LP
5823 r = btrfs_get_block_device_fd(fd, &devno);
5824 if (r == -ENOTTY) /* not btrfs */
5825 return -ENODEV;
5826 if (r < 0)
5827 return r;
5828 }
e594a3b1
LP
5829 } else
5830 return -ENOTBLK;
5831
5832 /* From dm-crypt to backing partition */
5833 r = block_get_originating(devno, &devno);
8e5f3cec
LP
5834 if (r == -ENOENT)
5835 log_debug_errno(r, "Device '%s' has no dm-crypt/dm-verity device, no need to look for underlying block device.", p);
5836 else if (r < 0)
e594a3b1
LP
5837 log_debug_errno(r, "Failed to find underlying block device for '%s', ignoring: %m", p);
5838
5839 /* From partition to whole disk containing it */
5840 r = block_get_whole_disk(devno, &devno);
5841 if (r < 0)
162392b7 5842 log_debug_errno(r, "Failed to find whole disk block device for '%s', ignoring: %m", p);
e594a3b1 5843
4fe46c34 5844 r = devname_from_devnum(S_IFBLK, devno, ret);
a26f4a49
LP
5845 if (r < 0)
5846 return log_debug_errno(r, "Failed to determine canonical path for '%s': %m", p);
5847
6bbae9f8 5848 /* Only if we still look at the same block device we can reuse the fd. Otherwise return an
a26f4a49 5849 * invalidated fd. */
f5fbe71d 5850 *ret_fd = fd_devno != MODE_INVALID && fd_devno == devno ? TAKE_FD(fd) : -1;
a26f4a49 5851 return 0;
e594a3b1
LP
5852}
5853
a26f4a49 5854static int find_root(char **ret, int *ret_fd) {
54632d2e 5855 _cleanup_free_ char *device = NULL;
5980d463 5856 int r;
e594a3b1 5857
a26f4a49
LP
5858 assert(ret);
5859 assert(ret_fd);
5860
e594a3b1 5861 if (arg_node) {
a26f4a49
LP
5862 if (arg_empty == EMPTY_CREATE) {
5863 _cleanup_close_ int fd = -1;
5864 _cleanup_free_ char *s = NULL;
5865
5866 s = strdup(arg_node);
5867 if (!s)
5868 return log_oom();
5869
5332d7c6 5870 fd = open(arg_node, O_RDONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOFOLLOW, 0666);
a26f4a49
LP
5871 if (fd < 0)
5872 return log_error_errno(errno, "Failed to create '%s': %m", arg_node);
5873
5874 *ret = TAKE_PTR(s);
5875 *ret_fd = TAKE_FD(fd);
5876 return 0;
5877 }
5878
252d6267
LP
5879 /* Note that we don't specify a root argument here: if the user explicitly configured a node
5880 * we'll take it relative to the host, not the image */
5881 r = acquire_root_devno(arg_node, NULL, O_RDONLY|O_CLOEXEC, ret, ret_fd);
67f0ac8c
LP
5882 if (r == -EUCLEAN)
5883 return btrfs_log_dev_root(LOG_ERR, r, arg_node);
e594a3b1 5884 if (r < 0)
aa2a74ad 5885 return log_error_errno(r, "Failed to open file or determine backing device of %s: %m", arg_node);
e594a3b1
LP
5886
5887 return 0;
5888 }
5889
a26f4a49
LP
5890 assert(IN_SET(arg_empty, EMPTY_REFUSE, EMPTY_ALLOW));
5891
54632d2e
KK
5892 /* If the root mount has been replaced by some form of volatile file system (overlayfs), the
5893 * original root block device node is symlinked in /run/systemd/volatile-root. Let's read that
5894 * here. */
5895 r = readlink_malloc("/run/systemd/volatile-root", &device);
5896 if (r == -ENOENT) { /* volatile-root not found */
5897 /* Let's search for the root device. We look for two cases here: first in /, and then in /usr. The
5898 * latter we check for cases where / is a tmpfs and only /usr is an actual persistent block device
5899 * (think: volatile setups) */
e594a3b1 5900
54632d2e 5901 FOREACH_STRING(p, "/", "/usr") {
e594a3b1 5902
54632d2e
KK
5903 r = acquire_root_devno(p, arg_root, O_RDONLY|O_DIRECTORY|O_CLOEXEC, ret, ret_fd);
5904 if (r < 0) {
5905 if (r == -EUCLEAN)
5906 return btrfs_log_dev_root(LOG_ERR, r, p);
5907 if (r != -ENODEV)
5908 return log_error_errno(r, "Failed to determine backing device of %s: %m", p);
5909 } else
5910 return 0;
5911 }
5912 } else if (r < 0)
5913 return log_error_errno(r, "Failed to read symlink /run/systemd/volatile-root: %m");
5914 else {
5915 r = acquire_root_devno(device, NULL, O_RDONLY|O_CLOEXEC, ret, ret_fd);
5916 if (r == -EUCLEAN)
5917 return btrfs_log_dev_root(LOG_ERR, r, device);
5918 if (r < 0)
5919 return log_error_errno(r, "Failed to open file or determine backing device of %s: %m", device);
5920
5921 return 0;
e594a3b1
LP
5922 }
5923
5924 return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "Failed to discover root block device.");
5925}
5926
f9b3afae 5927static int resize_pt(int fd) {
f9b3afae
LP
5928 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
5929 int r;
5930
5931 /* After resizing the backing file we need to resize the partition table itself too, so that it takes
5932 * possession of the enlarged backing file. For this it suffices to open the device with libfdisk and
5933 * immediately write it again, with no changes. */
5934
5935 c = fdisk_new_context();
5936 if (!c)
5937 return log_oom();
5938
ddb6eeaf 5939 r = fdisk_assign_device(c, FORMAT_PROC_FD_PATH(fd), 0);
f9b3afae 5940 if (r < 0)
ddb6eeaf 5941 return log_error_errno(r, "Failed to open device '%s': %m", FORMAT_PROC_FD_PATH(fd));
f9b3afae
LP
5942
5943 r = fdisk_has_label(c);
5944 if (r < 0)
ddb6eeaf 5945 return log_error_errno(r, "Failed to determine whether disk '%s' has a disk label: %m", FORMAT_PROC_FD_PATH(fd));
f9b3afae
LP
5946 if (r == 0) {
5947 log_debug("Not resizing partition table, as there currently is none.");
5948 return 0;
5949 }
5950
5951 r = fdisk_write_disklabel(c);
5952 if (r < 0)
5953 return log_error_errno(r, "Failed to write resized partition table: %m");
5954
5955 log_info("Resized partition table.");
5956 return 1;
5957}
5958
252d6267
LP
5959static int resize_backing_fd(
5960 const char *node, /* The primary way we access the disk image to operate on */
5961 int *fd, /* An O_RDONLY fd referring to that inode */
5962 const char *backing_file, /* If the above refers to a loopback device, the backing regular file for that, which we can grow */
5963 LoopDevice *loop_device) {
5964
a26f4a49 5965 _cleanup_close_ int writable_fd = -1;
252d6267 5966 uint64_t current_size;
a26f4a49
LP
5967 struct stat st;
5968 int r;
5969
5970 assert(node);
5971 assert(fd);
5972
5973 if (arg_size == UINT64_MAX) /* Nothing to do */
5974 return 0;
5975
5976 if (*fd < 0) {
5977 /* Open the file if we haven't opened it yet. Note that we open it read-only here, just to
5978 * keep a reference to the file we can pass around. */
5979 *fd = open(node, O_RDONLY|O_CLOEXEC);
5980 if (*fd < 0)
5981 return log_error_errno(errno, "Failed to open '%s' in order to adjust size: %m", node);
5982 }
5983
5984 if (fstat(*fd, &st) < 0)
5985 return log_error_errno(errno, "Failed to stat '%s': %m", node);
5986
252d6267
LP
5987 if (S_ISBLK(st.st_mode)) {
5988 if (!backing_file)
5989 return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Cannot resize block device '%s'.", node);
5990
5991 assert(loop_device);
a26f4a49 5992
252d6267
LP
5993 if (ioctl(*fd, BLKGETSIZE64, &current_size) < 0)
5994 return log_error_errno(errno, "Failed to determine size of block device %s: %m", node);
5995 } else {
5996 r = stat_verify_regular(&st);
5997 if (r < 0)
5998 return log_error_errno(r, "Specified path '%s' is not a regular file or loopback block device, cannot resize: %m", node);
5999
6000 assert(!backing_file);
6001 assert(!loop_device);
6002 current_size = st.st_size;
6003 }
6004
252d6267 6005 if (current_size >= arg_size) {
2b59bf51
ZJS
6006 log_info("File '%s' already is of requested size or larger, not growing. (%s >= %s)",
6007 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49
LP
6008 return 0;
6009 }
6010
252d6267
LP
6011 if (S_ISBLK(st.st_mode)) {
6012 assert(backing_file);
6013
6014 /* This is a loopback device. We can't really grow those directly, but we can grow the
6015 * backing file, hence let's do that. */
6016
6017 writable_fd = open(backing_file, O_WRONLY|O_CLOEXEC|O_NONBLOCK);
6018 if (writable_fd < 0)
6019 return log_error_errno(errno, "Failed to open backing file '%s': %m", backing_file);
6020
6021 if (fstat(writable_fd, &st) < 0)
6022 return log_error_errno(errno, "Failed to stat() backing file '%s': %m", backing_file);
6023
6024 r = stat_verify_regular(&st);
6025 if (r < 0)
6026 return log_error_errno(r, "Backing file '%s' of block device is not a regular file: %m", backing_file);
6027
6028 if ((uint64_t) st.st_size != current_size)
6029 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2b59bf51
ZJS
6030 "Size of backing file '%s' of loopback block device '%s' don't match, refusing.",
6031 node, backing_file);
252d6267
LP
6032 } else {
6033 assert(S_ISREG(st.st_mode));
6034 assert(!backing_file);
a26f4a49 6035
252d6267
LP
6036 /* The file descriptor is read-only. In order to grow the file we need to have a writable fd. We
6037 * reopen the file for that temporarily. We keep the writable fd only open for this operation though,
6038 * as fdisk can't accept it anyway. */
6039
6040 writable_fd = fd_reopen(*fd, O_WRONLY|O_CLOEXEC);
6041 if (writable_fd < 0)
6042 return log_error_errno(writable_fd, "Failed to reopen backing file '%s' writable: %m", node);
6043 }
a26f4a49
LP
6044
6045 if (!arg_discard) {
6046 if (fallocate(writable_fd, 0, 0, arg_size) < 0) {
6047 if (!ERRNO_IS_NOT_SUPPORTED(errno))
6048 return log_error_errno(errno, "Failed to grow '%s' from %s to %s by allocation: %m",
2b59bf51 6049 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49
LP
6050
6051 /* Fallback to truncation, if fallocate() is not supported. */
6052 log_debug("Backing file system does not support fallocate(), falling back to ftruncate().");
6053 } else {
252d6267 6054 if (current_size == 0) /* Likely regular file just created by us */
2b59bf51 6055 log_info("Allocated %s for '%s'.", FORMAT_BYTES(arg_size), node);
a26f4a49 6056 else
2b59bf51
ZJS
6057 log_info("File '%s' grown from %s to %s by allocation.",
6058 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49 6059
252d6267 6060 goto done;
a26f4a49
LP
6061 }
6062 }
6063
6064 if (ftruncate(writable_fd, arg_size) < 0)
6065 return log_error_errno(errno, "Failed to grow '%s' from %s to %s by truncation: %m",
2b59bf51 6066 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49 6067
252d6267 6068 if (current_size == 0) /* Likely regular file just created by us */
2b59bf51 6069 log_info("Sized '%s' to %s.", node, FORMAT_BYTES(arg_size));
252d6267 6070 else
2b59bf51
ZJS
6071 log_info("File '%s' grown from %s to %s by truncation.",
6072 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
252d6267
LP
6073
6074done:
f9b3afae
LP
6075 r = resize_pt(writable_fd);
6076 if (r < 0)
6077 return r;
6078
252d6267
LP
6079 if (loop_device) {
6080 r = loop_device_refresh_size(loop_device, UINT64_MAX, arg_size);
6081 if (r < 0)
6082 return log_error_errno(r, "Failed to update loop device size: %m");
6083 }
a26f4a49
LP
6084
6085 return 1;
6086}
6087
170c9823 6088static int determine_auto_size(Context *c) {
994b3031 6089 uint64_t sum;
170c9823 6090
ac33e147 6091 assert(c);
170c9823 6092
994b3031
LP
6093 sum = round_up_size(GPT_METADATA_SIZE, 4096);
6094
170c9823
LP
6095 LIST_FOREACH(partitions, p, c->partitions) {
6096 uint64_t m;
6097
6098 if (p->dropped)
6099 continue;
6100
994b3031 6101 m = partition_min_size_with_padding(c, p);
170c9823
LP
6102 if (m > UINT64_MAX - sum)
6103 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Image would grow too large, refusing.");
6104
6105 sum += m;
6106 }
6107
2b59bf51
ZJS
6108 if (c->total != UINT64_MAX)
6109 /* Image already allocated? Then show its size. */
6110 log_info("Automatically determined minimal disk image size as %s, current image size is %s.",
6111 FORMAT_BYTES(sum), FORMAT_BYTES(c->total));
6112 else
6113 /* If the image is being created right now, then it has no previous size, suppress any comment about it hence. */
6114 log_info("Automatically determined minimal disk image size as %s.",
6115 FORMAT_BYTES(sum));
170c9823
LP
6116
6117 arg_size = sum;
6118 return 0;
6119}
6120
e594a3b1 6121static int run(int argc, char *argv[]) {
252d6267 6122 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
252d6267 6123 _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
e594a3b1
LP
6124 _cleanup_(context_freep) Context* context = NULL;
6125 _cleanup_free_ char *node = NULL;
a26f4a49 6126 _cleanup_close_ int backing_fd = -1;
252d6267 6127 bool from_scratch, node_is_our_loop = false;
e594a3b1
LP
6128 int r;
6129
6130 log_show_color(true);
6131 log_parse_environment();
6132 log_open();
6133
e594a3b1
LP
6134 r = parse_argv(argc, argv);
6135 if (r <= 0)
6136 return r;
6137
6138 r = parse_proc_cmdline_factory_reset();
6139 if (r < 0)
6140 return r;
6141
6142 r = parse_efi_variable_factory_reset();
6143 if (r < 0)
6144 return r;
6145
30f19400
LP
6146#if HAVE_LIBCRYPTSETUP
6147 cryptsetup_enable_logging(NULL);
6148#endif
6149
252d6267
LP
6150 if (arg_image) {
6151 assert(!arg_root);
6152
6153 /* Mount this strictly read-only: we shall modify the partition table, not the file
6154 * systems */
6155 r = mount_image_privately_interactively(
6156 arg_image,
6157 DISSECT_IMAGE_MOUNT_READ_ONLY |
6158 (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) */
6159 DISSECT_IMAGE_GPT_ONLY |
6160 DISSECT_IMAGE_RELAX_VAR_CHECK |
6161 DISSECT_IMAGE_USR_NO_ROOT |
6162 DISSECT_IMAGE_REQUIRE_ROOT,
6163 &mounted_dir,
e330f97a 6164 &loop_device);
252d6267
LP
6165 if (r < 0)
6166 return r;
6167
6168 arg_root = strdup(mounted_dir);
6169 if (!arg_root)
6170 return log_oom();
6171
6172 if (!arg_node) {
6173 arg_node = strdup(loop_device->node);
6174 if (!arg_node)
6175 return log_oom();
6176
3d62af7d 6177 /* Remember that the device we are about to manipulate is actually the one we
252d6267
LP
6178 * allocated here, and thus to increase its backing file we know what to do */
6179 node_is_our_loop = true;
6180 }
6181 }
6182
e594a3b1
LP
6183 context = context_new(arg_seed);
6184 if (!context)
6185 return log_oom();
6186
224c853f
RP
6187 strv_uniq(arg_definitions);
6188
e594a3b1
LP
6189 r = context_read_definitions(context, arg_definitions, arg_root);
6190 if (r < 0)
6191 return r;
6192
a26f4a49 6193 if (context->n_partitions <= 0 && arg_empty == EMPTY_REFUSE) {
e2d65cd2 6194 log_info("Didn't find any partition definition files, nothing to do.");
0ae5ffe0 6195 return 0;
e2d65cd2 6196 }
0ae5ffe0 6197
a26f4a49 6198 r = find_root(&node, &backing_fd);
0ae5ffe0
YW
6199 if (r < 0)
6200 return r;
6201
a26f4a49 6202 if (arg_size != UINT64_MAX) {
252d6267
LP
6203 r = resize_backing_fd(
6204 node,
6205 &backing_fd,
6206 node_is_our_loop ? arg_image : NULL,
6207 node_is_our_loop ? loop_device : NULL);
a26f4a49
LP
6208 if (r < 0)
6209 return r;
6210 }
6211
6212 r = context_load_partition_table(context, node, &backing_fd);
e594a3b1
LP
6213 if (r == -EHWPOISON)
6214 return 77; /* Special return value which means "Not GPT, so not doing anything". This isn't
6215 * really an error when called at boot. */
6216 if (r < 0)
6217 return r;
6218 from_scratch = r > 0; /* Starting from scratch */
6219
6220 if (arg_can_factory_reset) {
6221 r = context_can_factory_reset(context);
6222 if (r < 0)
6223 return r;
6224 if (r == 0)
6225 return EXIT_FAILURE;
6226
6227 return 0;
6228 }
6229
6230 r = context_factory_reset(context, from_scratch);
6231 if (r < 0)
6232 return r;
6233 if (r > 0) {
6234 /* We actually did a factory reset! */
6235 r = remove_efi_variable_factory_reset();
6236 if (r < 0)
6237 return r;
6238
6239 /* Reload the reduced partition table */
6240 context_unload_partition_table(context);
a26f4a49 6241 r = context_load_partition_table(context, node, &backing_fd);
e594a3b1
LP
6242 if (r < 0)
6243 return r;
6244 }
6245
6246#if 0
6247 (void) context_dump_partitions(context, node);
6248 putchar('\n');
6249#endif
6250
6251 r = context_read_seed(context, arg_root);
6252 if (r < 0)
6253 return r;
6254
8bbbdfd7
DDM
6255 /* Make sure each partition has a unique UUID and unique label */
6256 r = context_acquire_partition_uuids_and_labels(context);
6257 if (r < 0)
6258 return r;
6259
c4a87b76
DDM
6260 r = context_minimize(context);
6261 if (r < 0)
6262 return r;
6263
757bc2e4 6264 /* Open all files to copy blocks from now, since we want to take their size into consideration */
5c08da58
LP
6265 r = context_open_copy_block_paths(
6266 context,
7802194a 6267 loop_device ? loop_device->devno : /* if --image= is specified, only allow partitions on the loopback device */
5c08da58
LP
6268 arg_root && !arg_image ? 0 : /* if --root= is specified, don't accept any block device */
6269 (dev_t) -1); /* if neither is specified, make no restrictions */
757bc2e4
LP
6270 if (r < 0)
6271 return r;
6272
170c9823
LP
6273 if (arg_size_auto) {
6274 r = determine_auto_size(context);
6275 if (r < 0)
6276 return r;
6277
6278 /* Flush out everything again, and let's grow the file first, then start fresh */
6279 context_unload_partition_table(context);
6280
ac33e147 6281 assert(arg_size != UINT64_MAX);
252d6267
LP
6282 r = resize_backing_fd(
6283 node,
6284 &backing_fd,
6285 node_is_our_loop ? arg_image : NULL,
6286 node_is_our_loop ? loop_device : NULL);
170c9823
LP
6287 if (r < 0)
6288 return r;
6289
6290 r = context_load_partition_table(context, node, &backing_fd);
6291 if (r < 0)
6292 return r;
6293 }
6294
e594a3b1
LP
6295 /* First try to fit new partitions in, dropping by priority until it fits */
6296 for (;;) {
14a4c4ed
LP
6297 uint64_t largest_free_area;
6298
6299 if (context_allocate_partitions(context, &largest_free_area))
e594a3b1
LP
6300 break; /* Success! */
6301
9ccceb9d 6302 if (!context_drop_or_foreignize_one_priority(context)) {
d17db7b2 6303 r = log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
14a4c4ed 6304 "Can't fit requested partitions into available free space (%s), refusing.",
2b59bf51 6305 FORMAT_BYTES(largest_free_area));
d17db7b2
LP
6306 determine_auto_size(context);
6307 return r;
6308 }
e594a3b1
LP
6309 }
6310
6311 /* Now assign free space according to the weight logic */
6312 r = context_grow_partitions(context);
6313 if (r < 0)
6314 return r;
6315
0b7f574f 6316 /* Now calculate where each new partition gets placed */
e594a3b1
LP
6317 context_place_partitions(context);
6318
b5b7879a 6319 (void) context_dump(context, node, /*late=*/ false);
a26d463d 6320
e594a3b1
LP
6321 r = context_write_partition_table(context, node, from_scratch);
6322 if (r < 0)
6323 return r;
6324
4cee8333
DDM
6325 r = context_split(context);
6326 if (r < 0)
6327 return r;
6328
b5b7879a
DDM
6329 (void) context_dump(context, node, /*late=*/ true);
6330
e594a3b1
LP
6331 return 0;
6332}
6333
6334DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);