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