]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/partition/repart.c
repart: Extract context dump into a separate function
[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"
f4351959 22#include "chase-symlinks.h"
e594a3b1
LP
23#include "conf-files.h"
24#include "conf-parser.h"
1e2f3230 25#include "cryptsetup-util.h"
e594a3b1 26#include "def.h"
ca822829 27#include "device-util.h"
7176f06c 28#include "devnum-util.h"
5c08da58 29#include "dirent-util.h"
e594a3b1
LP
30#include "efivars.h"
31#include "errno-util.h"
32#include "fd-util.h"
4dc07c3a 33#include "fdisk-util.h"
b9df3536 34#include "fileio.h"
e594a3b1
LP
35#include "format-table.h"
36#include "format-util.h"
37#include "fs-util.h"
d8e32c47 38#include "glyph-util.h"
e594a3b1 39#include "gpt.h"
889914ef 40#include "hexdecoct.h"
ade99252 41#include "hmac.h"
e594a3b1 42#include "id128-util.h"
a015fbe7 43#include "json.h"
e594a3b1 44#include "list.h"
53171c04 45#include "loop-util.h"
e594a3b1 46#include "main-func.h"
8a794850 47#include "mkdir.h"
53171c04 48#include "mkfs-util.h"
8a794850 49#include "mount-util.h"
5c08da58 50#include "mountpoint-util.h"
614b022c 51#include "parse-argument.h"
c3eaba2d 52#include "parse-helpers.h"
e594a3b1
LP
53#include "pretty-print.h"
54#include "proc-cmdline.h"
8a794850 55#include "process-util.h"
b9df3536 56#include "random-util.h"
170c9823 57#include "resize-fs.h"
e594a3b1 58#include "sort-util.h"
e031166e 59#include "specifier.h"
e594a3b1 60#include "stdio-util.h"
889914ef 61#include "string-table.h"
e594a3b1
LP
62#include "string-util.h"
63#include "strv.h"
bf819d3a 64#include "sync-util.h"
e594a3b1 65#include "terminal-util.h"
889914ef 66#include "tpm2-util.h"
8a794850 67#include "user-util.h"
e594a3b1
LP
68#include "utf8.h"
69
fb08381c
LP
70/* If not configured otherwise use a minimal partition size of 10M */
71#define DEFAULT_MIN_SIZE (10*1024*1024)
72
73/* Hard lower limit for new partition sizes */
74#define HARD_MIN_SIZE 4096
75
69e3234d 76/* libfdisk takes off slightly more than 1M of the disk size when creating a GPT disk label */
170c9823
LP
77#define GPT_METADATA_SIZE (1044*1024)
78
79/* LUKS2 takes off 16M of the partition size with its metadata by default */
80#define LUKS2_METADATA_SIZE (16*1024*1024)
81
e594a3b1
LP
82/* Note: When growing and placing new partitions we always align to 4K sector size. It's how newer hard disks
83 * are designed, and if everything is aligned to that performance is best. And for older hard disks with 512B
84 * sector size devices were generally assumed to have an even number of sectors, hence at the worst we'll
85 * waste 3K per partition, which is probably fine. */
86
87static enum {
88 EMPTY_REFUSE, /* refuse empty disks, never create a partition table */
89 EMPTY_ALLOW, /* allow empty disks, create partition table if necessary */
90 EMPTY_REQUIRE, /* require an empty disk, create a partition table */
91 EMPTY_FORCE, /* make disk empty, erase everything, create a partition table always */
a26f4a49 92 EMPTY_CREATE, /* create disk as loopback file, create a partition table always */
e594a3b1
LP
93} arg_empty = EMPTY_REFUSE;
94
95static bool arg_dry_run = true;
96static const char *arg_node = NULL;
97static char *arg_root = NULL;
252d6267 98static char *arg_image = NULL;
224c853f 99static char **arg_definitions = NULL;
e594a3b1
LP
100static bool arg_discard = true;
101static bool arg_can_factory_reset = false;
102static int arg_factory_reset = -1;
103static sd_id128_t arg_seed = SD_ID128_NULL;
104static bool arg_randomize = false;
105static int arg_pretty = -1;
a26f4a49 106static uint64_t arg_size = UINT64_MAX;
170c9823 107static bool arg_size_auto = false;
6a01ea4a 108static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
896e678b
LP
109static PagerFlags arg_pager_flags = 0;
110static bool arg_legend = true;
b9df3536
LP
111static void *arg_key = NULL;
112static size_t arg_key_size = 0;
889914ef
LP
113static char *arg_tpm2_device = NULL;
114static uint32_t arg_tpm2_pcr_mask = UINT32_MAX;
e594a3b1
LP
115
116STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
252d6267 117STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
224c853f 118STATIC_DESTRUCTOR_REGISTER(arg_definitions, strv_freep);
b9df3536 119STATIC_DESTRUCTOR_REGISTER(arg_key, erase_and_freep);
889914ef 120STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep);
e594a3b1
LP
121
122typedef struct Partition Partition;
123typedef struct FreeArea FreeArea;
124typedef struct Context Context;
125
889914ef
LP
126typedef enum EncryptMode {
127 ENCRYPT_OFF,
128 ENCRYPT_KEY_FILE,
129 ENCRYPT_TPM2,
130 ENCRYPT_KEY_FILE_TPM2,
131 _ENCRYPT_MODE_MAX,
2d93c20e 132 _ENCRYPT_MODE_INVALID = -EINVAL,
889914ef
LP
133} EncryptMode;
134
e594a3b1
LP
135struct Partition {
136 char *definition_path;
39fc0174 137 char **drop_in_files;
e594a3b1
LP
138
139 sd_id128_t type_uuid;
140 sd_id128_t current_uuid, new_uuid;
11749b61 141 bool new_uuid_is_set;
e594a3b1
LP
142 char *current_label, *new_label;
143
144 bool dropped;
145 bool factory_reset;
146 int32_t priority;
147
148 uint32_t weight, padding_weight;
149
150 uint64_t current_size, new_size;
151 uint64_t size_min, size_max;
152
153 uint64_t current_padding, new_padding;
154 uint64_t padding_min, padding_max;
155
156 uint64_t partno;
157 uint64_t offset;
158
159 struct fdisk_partition *current_partition;
160 struct fdisk_partition *new_partition;
161 FreeArea *padding_area;
162 FreeArea *allocated_to_area;
163
757bc2e4 164 char *copy_blocks_path;
5c08da58 165 bool copy_blocks_auto;
757bc2e4
LP
166 int copy_blocks_fd;
167 uint64_t copy_blocks_size;
168
53171c04 169 char *format;
8a794850 170 char **copy_files;
d83d8048 171 char **make_directories;
889914ef 172 EncryptMode encrypt;
53171c04 173
e73309c5 174 uint64_t gpt_flags;
ff0771bf 175 int no_auto;
e73309c5 176 int read_only;
1c41c1dc 177 int growfs;
e73309c5 178
e594a3b1
LP
179 LIST_FIELDS(Partition, partitions);
180};
181
182#define PARTITION_IS_FOREIGN(p) (!(p)->definition_path)
183#define PARTITION_EXISTS(p) (!!(p)->current_partition)
184
185struct FreeArea {
186 Partition *after;
187 uint64_t size;
188 uint64_t allocated;
189};
190
191struct Context {
192 LIST_HEAD(Partition, partitions);
193 size_t n_partitions;
194
195 FreeArea **free_areas;
319a4f4b 196 size_t n_free_areas;
e594a3b1
LP
197
198 uint64_t start, end, total;
199
200 struct fdisk_context *fdisk_context;
994b3031
LP
201 uint64_t sector_size;
202 uint64_t grain_size;
e594a3b1
LP
203
204 sd_id128_t seed;
205};
206
889914ef
LP
207static const char *encrypt_mode_table[_ENCRYPT_MODE_MAX] = {
208 [ENCRYPT_OFF] = "off",
209 [ENCRYPT_KEY_FILE] = "key-file",
210 [ENCRYPT_TPM2] = "tpm2",
211 [ENCRYPT_KEY_FILE_TPM2] = "key-file+tpm2",
212};
213
2709d029 214#if HAVE_LIBCRYPTSETUP
889914ef 215DEFINE_PRIVATE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(encrypt_mode, EncryptMode, ENCRYPT_KEY_FILE);
2709d029
MH
216#else
217DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(encrypt_mode, EncryptMode, ENCRYPT_KEY_FILE);
218#endif
219
889914ef 220
e594a3b1
LP
221static uint64_t round_down_size(uint64_t v, uint64_t p) {
222 return (v / p) * p;
223}
224
225static uint64_t round_up_size(uint64_t v, uint64_t p) {
226
227 v = DIV_ROUND_UP(v, p);
228
229 if (v > UINT64_MAX / p)
230 return UINT64_MAX; /* overflow */
231
232 return v * p;
233}
234
235static Partition *partition_new(void) {
236 Partition *p;
237
238 p = new(Partition, 1);
239 if (!p)
240 return NULL;
241
242 *p = (Partition) {
243 .weight = 1000,
244 .padding_weight = 0,
245 .current_size = UINT64_MAX,
246 .new_size = UINT64_MAX,
247 .size_min = UINT64_MAX,
248 .size_max = UINT64_MAX,
249 .current_padding = UINT64_MAX,
250 .new_padding = UINT64_MAX,
251 .padding_min = UINT64_MAX,
252 .padding_max = UINT64_MAX,
253 .partno = UINT64_MAX,
254 .offset = UINT64_MAX,
757bc2e4
LP
255 .copy_blocks_fd = -1,
256 .copy_blocks_size = UINT64_MAX,
ff0771bf 257 .no_auto = -1,
e73309c5 258 .read_only = -1,
1c41c1dc 259 .growfs = -1,
e594a3b1
LP
260 };
261
262 return p;
263}
264
265static Partition* partition_free(Partition *p) {
266 if (!p)
267 return NULL;
268
269 free(p->current_label);
270 free(p->new_label);
271 free(p->definition_path);
39fc0174 272 strv_free(p->drop_in_files);
e594a3b1
LP
273
274 if (p->current_partition)
275 fdisk_unref_partition(p->current_partition);
276 if (p->new_partition)
277 fdisk_unref_partition(p->new_partition);
278
757bc2e4
LP
279 free(p->copy_blocks_path);
280 safe_close(p->copy_blocks_fd);
281
53171c04 282 free(p->format);
8a794850 283 strv_free(p->copy_files);
d83d8048 284 strv_free(p->make_directories);
53171c04 285
e594a3b1
LP
286 return mfree(p);
287}
288
289static Partition* partition_unlink_and_free(Context *context, Partition *p) {
290 if (!p)
291 return NULL;
292
293 LIST_REMOVE(partitions, context->partitions, p);
294
295 assert(context->n_partitions > 0);
296 context->n_partitions--;
297
298 return partition_free(p);
299}
300
301DEFINE_TRIVIAL_CLEANUP_FUNC(Partition*, partition_free);
302
303static Context *context_new(sd_id128_t seed) {
304 Context *context;
305
306 context = new(Context, 1);
307 if (!context)
308 return NULL;
309
310 *context = (Context) {
311 .start = UINT64_MAX,
312 .end = UINT64_MAX,
313 .total = UINT64_MAX,
314 .seed = seed,
315 };
316
317 return context;
318}
319
320static void context_free_free_areas(Context *context) {
321 assert(context);
322
323 for (size_t i = 0; i < context->n_free_areas; i++)
324 free(context->free_areas[i]);
325
326 context->free_areas = mfree(context->free_areas);
327 context->n_free_areas = 0;
e594a3b1
LP
328}
329
330static Context *context_free(Context *context) {
331 if (!context)
332 return NULL;
333
334 while (context->partitions)
335 partition_unlink_and_free(context, context->partitions);
336 assert(context->n_partitions == 0);
337
338 context_free_free_areas(context);
339
340 if (context->fdisk_context)
341 fdisk_unref_context(context->fdisk_context);
342
343 return mfree(context);
344}
345
346DEFINE_TRIVIAL_CLEANUP_FUNC(Context*, context_free);
347
348static int context_add_free_area(
349 Context *context,
350 uint64_t size,
351 Partition *after) {
352
353 FreeArea *a;
354
355 assert(context);
356 assert(!after || !after->padding_area);
357
319a4f4b 358 if (!GREEDY_REALLOC(context->free_areas, context->n_free_areas + 1))
e594a3b1
LP
359 return -ENOMEM;
360
361 a = new(FreeArea, 1);
362 if (!a)
363 return -ENOMEM;
364
365 *a = (FreeArea) {
366 .size = size,
367 .after = after,
368 };
369
370 context->free_areas[context->n_free_areas++] = a;
371
372 if (after)
373 after->padding_area = a;
374
375 return 0;
376}
377
378static bool context_drop_one_priority(Context *context) {
379 int32_t priority = 0;
e594a3b1
LP
380 bool exists = false;
381
382 LIST_FOREACH(partitions, p, context->partitions) {
383 if (p->dropped)
384 continue;
385 if (p->priority < priority)
386 continue;
387 if (p->priority == priority) {
388 exists = exists || PARTITION_EXISTS(p);
389 continue;
390 }
391
392 priority = p->priority;
393 exists = PARTITION_EXISTS(p);
394 }
395
396 /* Refuse to drop partitions with 0 or negative priorities or partitions of priorities that have at
397 * least one existing priority */
398 if (priority <= 0 || exists)
399 return false;
400
401 LIST_FOREACH(partitions, p, context->partitions) {
402 if (p->priority < priority)
403 continue;
404
405 if (p->dropped)
406 continue;
407
408 p->dropped = true;
409 log_info("Can't fit partition %s of priority %" PRIi32 ", dropping.", p->definition_path, p->priority);
410 }
411
412 return true;
413}
414
994b3031 415static uint64_t partition_min_size(Context *context, const Partition *p) {
e594a3b1
LP
416 uint64_t sz;
417
994b3031
LP
418 assert(context);
419 assert(p);
420
e594a3b1
LP
421 /* Calculate the disk space we really need at minimum for this partition. If the partition already
422 * exists the current size is what we really need. If it doesn't exist yet refuse to allocate less
fb08381c
LP
423 * than 4K.
424 *
425 * DEFAULT_MIN_SIZE is the default SizeMin= we configure if nothing else is specified. */
e594a3b1
LP
426
427 if (PARTITION_IS_FOREIGN(p)) {
428 /* Don't allow changing size of partitions not managed by us */
429 assert(p->current_size != UINT64_MAX);
430 return p->current_size;
431 }
432
fb08381c 433 sz = p->current_size != UINT64_MAX ? p->current_size : HARD_MIN_SIZE;
757bc2e4 434
170c9823
LP
435 if (!PARTITION_EXISTS(p)) {
436 uint64_t d = 0;
437
889914ef 438 if (p->encrypt != ENCRYPT_OFF)
994b3031 439 d += round_up_size(LUKS2_METADATA_SIZE, context->grain_size);
170c9823
LP
440
441 if (p->copy_blocks_size != UINT64_MAX)
994b3031 442 d += round_up_size(p->copy_blocks_size, context->grain_size);
889914ef 443 else if (p->format || p->encrypt != ENCRYPT_OFF) {
170c9823
LP
444 uint64_t f;
445
446 /* If we shall synthesize a file system, take minimal fs size into account (assumed to be 4K if not known) */
994b3031
LP
447 f = p->format ? round_up_size(minimal_size_by_fs_name(p->format), context->grain_size) : UINT64_MAX;
448 d += f == UINT64_MAX ? context->grain_size : f;
170c9823
LP
449 }
450
451 if (d > sz)
452 sz = d;
453 }
757bc2e4 454
994b3031 455 return MAX(round_up_size(p->size_min != UINT64_MAX ? p->size_min : DEFAULT_MIN_SIZE, context->grain_size), sz);
e594a3b1
LP
456}
457
994b3031
LP
458static uint64_t partition_max_size(const Context *context, const Partition *p) {
459 uint64_t sm;
460
e594a3b1
LP
461 /* Calculate how large the partition may become at max. This is generally the configured maximum
462 * size, except when it already exists and is larger than that. In that case it's the existing size,
463 * since we never want to shrink partitions. */
464
994b3031
LP
465 assert(context);
466 assert(p);
467
e594a3b1
LP
468 if (PARTITION_IS_FOREIGN(p)) {
469 /* Don't allow changing size of partitions not managed by us */
470 assert(p->current_size != UINT64_MAX);
471 return p->current_size;
472 }
473
994b3031
LP
474 sm = round_down_size(p->size_max, context->grain_size);
475
e594a3b1 476 if (p->current_size != UINT64_MAX)
994b3031 477 return MAX(p->current_size, sm);
e594a3b1 478
994b3031 479 return sm;
e594a3b1
LP
480}
481
994b3031 482static uint64_t partition_min_size_with_padding(Context *context, const Partition *p) {
e594a3b1
LP
483 uint64_t sz;
484
485 /* Calculate the disk space we need for this partition plus any free space coming after it. This
486 * takes user configured padding into account as well as any additional whitespace needed to align
487 * the next partition to 4K again. */
488
994b3031
LP
489 assert(context);
490 assert(p);
491
492 sz = partition_min_size(context, p);
e594a3b1
LP
493
494 if (p->padding_min != UINT64_MAX)
495 sz += p->padding_min;
496
497 if (PARTITION_EXISTS(p)) {
498 /* If the partition wasn't aligned, add extra space so that any we might add will be aligned */
499 assert(p->offset != UINT64_MAX);
994b3031 500 return round_up_size(p->offset + sz, context->grain_size) - p->offset;
e594a3b1
LP
501 }
502
503 /* If this is a new partition we'll place it aligned, hence we just need to round up the required size here */
994b3031 504 return round_up_size(sz, context->grain_size);
e594a3b1
LP
505}
506
507static uint64_t free_area_available(const FreeArea *a) {
508 assert(a);
509
510 /* Determines how much of this free area is not allocated yet */
511
512 assert(a->size >= a->allocated);
513 return a->size - a->allocated;
514}
515
994b3031 516static uint64_t free_area_available_for_new_partitions(Context *context, const FreeArea *a) {
e594a3b1
LP
517 uint64_t avail;
518
994b3031
LP
519 assert(context);
520 assert(a);
521
e594a3b1 522 /* Similar to free_area_available(), but takes into account that the required size and padding of the
162392b7 523 * preceding partition is honoured. */
e594a3b1
LP
524
525 avail = free_area_available(a);
526 if (a->after) {
1052a114 527 uint64_t need, space_end, new_end;
e594a3b1 528
994b3031 529 need = partition_min_size_with_padding(context, a->after);
e594a3b1
LP
530
531 assert(a->after->offset != UINT64_MAX);
532 assert(a->after->current_size != UINT64_MAX);
533
1052a114 534 /* Calculate where the free area ends, based on the offset of the partition preceding it */
994b3031 535 space_end = round_up_size(a->after->offset + a->after->current_size, context->grain_size) + avail;
e594a3b1 536
1052a114 537 /* Calculate where the partition would end when we give it as much as it needs */
994b3031 538 new_end = round_up_size(a->after->offset + need, context->grain_size);
1052a114
LP
539
540 /* Calculate saturated difference of the two: that's how much we have free for other partitions */
541 return LESS_BY(space_end, new_end);
e594a3b1
LP
542 }
543
544 return avail;
545}
546
994b3031
LP
547static int free_area_compare(FreeArea *const *a, FreeArea *const*b, Context *context) {
548 assert(context);
549
550 return CMP(free_area_available_for_new_partitions(context, *a),
551 free_area_available_for_new_partitions(context, *b));
e594a3b1
LP
552}
553
994b3031
LP
554static uint64_t charge_size(Context *context, uint64_t total, uint64_t amount) {
555 assert(context);
e594a3b1 556 /* Subtract the specified amount from total, rounding up to multiple of 4K if there's room */
184cf99a 557 assert(amount <= total);
994b3031 558 return LESS_BY(total, round_up_size(amount, context->grain_size));
e594a3b1
LP
559}
560
561static uint64_t charge_weight(uint64_t total, uint64_t amount) {
562 assert(amount <= total);
563 return total - amount;
564}
565
14a4c4ed 566static bool context_allocate_partitions(Context *context, uint64_t *ret_largest_free_area) {
e594a3b1
LP
567 assert(context);
568
14a4c4ed 569 /* Sort free areas by size, putting smallest first */
994b3031 570 typesafe_qsort_r(context->free_areas, context->n_free_areas, free_area_compare, context);
e594a3b1 571
14a4c4ed
LP
572 /* In any case return size of the largest free area (i.e. not the size of all free areas
573 * combined!) */
574 if (ret_largest_free_area)
575 *ret_largest_free_area =
576 context->n_free_areas == 0 ? 0 :
994b3031 577 free_area_available_for_new_partitions(context, context->free_areas[context->n_free_areas-1]);
14a4c4ed
LP
578
579 /* A simple first-fit algorithm. We return true if we can fit the partitions in, otherwise false. */
e594a3b1
LP
580 LIST_FOREACH(partitions, p, context->partitions) {
581 bool fits = false;
582 uint64_t required;
583 FreeArea *a = NULL;
584
585 /* Skip partitions we already dropped or that already exist */
586 if (p->dropped || PARTITION_EXISTS(p))
587 continue;
588
e594a3b1 589 /* How much do we need to fit? */
994b3031
LP
590 required = partition_min_size_with_padding(context, p);
591 assert(required % context->grain_size == 0);
e594a3b1
LP
592
593 for (size_t i = 0; i < context->n_free_areas; i++) {
594 a = context->free_areas[i];
595
994b3031 596 if (free_area_available_for_new_partitions(context, a) >= required) {
e594a3b1
LP
597 fits = true;
598 break;
599 }
600 }
601
602 if (!fits)
603 return false; /* 😢 Oh no! We can't fit this partition into any free area! */
604
605 /* Assign the partition to this free area */
606 p->allocated_to_area = a;
607
608 /* Budget the minimal partition size */
609 a->allocated += required;
610 }
611
612 return true;
613}
614
615static int context_sum_weights(Context *context, FreeArea *a, uint64_t *ret) {
616 uint64_t weight_sum = 0;
e594a3b1
LP
617
618 assert(context);
619 assert(a);
620 assert(ret);
621
622 /* Determine the sum of the weights of all partitions placed in or before the specified free area */
623
624 LIST_FOREACH(partitions, p, context->partitions) {
625 if (p->padding_area != a && p->allocated_to_area != a)
626 continue;
627
628 if (p->weight > UINT64_MAX - weight_sum)
629 goto overflow_sum;
630 weight_sum += p->weight;
631
632 if (p->padding_weight > UINT64_MAX - weight_sum)
633 goto overflow_sum;
634 weight_sum += p->padding_weight;
635 }
636
637 *ret = weight_sum;
638 return 0;
639
640overflow_sum:
641 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Combined weight of partition exceeds unsigned 64bit range, refusing.");
642}
643
644static int scale_by_weight(uint64_t value, uint64_t weight, uint64_t weight_sum, uint64_t *ret) {
645 assert(weight_sum >= weight);
646 assert(ret);
647
648 if (weight == 0) {
649 *ret = 0;
650 return 0;
651 }
652
653 if (value > UINT64_MAX / weight)
654 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Scaling by weight of partition exceeds unsigned 64bit range, refusing.");
655
656 *ret = value * weight / weight_sum;
657 return 0;
658}
659
660typedef enum GrowPartitionPhase {
661 /* The first phase: we charge partitions which need more (according to constraints) than their weight-based share. */
662 PHASE_OVERCHARGE,
663
664 /* The second phase: we charge partitions which need less (according to constraints) than their weight-based share. */
665 PHASE_UNDERCHARGE,
666
667 /* The third phase: we distribute what remains among the remaining partitions, according to the weights */
668 PHASE_DISTRIBUTE,
ae0613c6
LP
669
670 _GROW_PARTITION_PHASE_MAX,
e594a3b1
LP
671} GrowPartitionPhase;
672
673static int context_grow_partitions_phase(
674 Context *context,
675 FreeArea *a,
676 GrowPartitionPhase phase,
677 uint64_t *span,
678 uint64_t *weight_sum) {
679
e594a3b1
LP
680 int r;
681
682 assert(context);
683 assert(a);
684
685 /* Now let's look at the intended weights and adjust them taking the minimum space assignments into
686 * account. i.e. if a partition has a small weight but a high minimum space value set it should not
687 * get any additional room from the left-overs. Similar, if two partitions have the same weight they
688 * should get the same space if possible, even if one has a smaller minimum size than the other. */
689 LIST_FOREACH(partitions, p, context->partitions) {
690
691 /* Look only at partitions associated with this free area, i.e. immediately
162392b7 692 * preceding it, or allocated into it */
e594a3b1
LP
693 if (p->allocated_to_area != a && p->padding_area != a)
694 continue;
695
696 if (p->new_size == UINT64_MAX) {
697 bool charge = false, try_again = false;
698 uint64_t share, rsz, xsz;
699
700 /* Calculate how much this space this partition needs if everyone would get
701 * the weight based share */
702 r = scale_by_weight(*span, p->weight, *weight_sum, &share);
703 if (r < 0)
704 return r;
705
994b3031
LP
706 rsz = partition_min_size(context, p);
707 xsz = partition_max_size(context, p);
e594a3b1
LP
708
709 if (phase == PHASE_OVERCHARGE && rsz > share) {
710 /* This partition needs more than its calculated share. Let's assign
711 * it that, and take this partition out of all calculations and start
712 * again. */
713
714 p->new_size = rsz;
715 charge = try_again = true;
716
717 } else if (phase == PHASE_UNDERCHARGE && xsz != UINT64_MAX && xsz < share) {
718 /* This partition accepts less than its calculated
719 * share. Let's assign it that, and take this partition out
720 * of all calculations and start again. */
721
722 p->new_size = xsz;
723 charge = try_again = true;
724
725 } else if (phase == PHASE_DISTRIBUTE) {
726 /* This partition can accept its calculated share. Let's
727 * assign it. There's no need to restart things here since
728 * assigning this shouldn't impact the shares of the other
729 * partitions. */
730
731 if (PARTITION_IS_FOREIGN(p))
732 /* Never change of foreign partitions (i.e. those we don't manage) */
733 p->new_size = p->current_size;
734 else
994b3031 735 p->new_size = MAX(round_down_size(share, context->grain_size), rsz);
e594a3b1
LP
736
737 charge = true;
738 }
739
740 if (charge) {
994b3031 741 *span = charge_size(context, *span, p->new_size);
e594a3b1
LP
742 *weight_sum = charge_weight(*weight_sum, p->weight);
743 }
744
745 if (try_again)
746 return 0; /* try again */
747 }
748
749 if (p->new_padding == UINT64_MAX) {
750 bool charge = false, try_again = false;
751 uint64_t share;
752
753 r = scale_by_weight(*span, p->padding_weight, *weight_sum, &share);
754 if (r < 0)
755 return r;
756
757 if (phase == PHASE_OVERCHARGE && p->padding_min != UINT64_MAX && p->padding_min > share) {
758 p->new_padding = p->padding_min;
759 charge = try_again = true;
760 } else if (phase == PHASE_UNDERCHARGE && p->padding_max != UINT64_MAX && p->padding_max < share) {
761 p->new_padding = p->padding_max;
762 charge = try_again = true;
763 } else if (phase == PHASE_DISTRIBUTE) {
764
994b3031 765 p->new_padding = round_down_size(share, context->grain_size);
e594a3b1
LP
766 if (p->padding_min != UINT64_MAX && p->new_padding < p->padding_min)
767 p->new_padding = p->padding_min;
768
769 charge = true;
770 }
771
772 if (charge) {
994b3031 773 *span = charge_size(context, *span, p->new_padding);
e594a3b1
LP
774 *weight_sum = charge_weight(*weight_sum, p->padding_weight);
775 }
776
777 if (try_again)
778 return 0; /* try again */
779 }
780 }
781
782 return 1; /* done */
783}
784
785static int context_grow_partitions_on_free_area(Context *context, FreeArea *a) {
786 uint64_t weight_sum = 0, span;
787 int r;
788
789 assert(context);
790 assert(a);
791
792 r = context_sum_weights(context, a, &weight_sum);
793 if (r < 0)
794 return r;
795
796 /* Let's calculate the total area covered by this free area and the partition before it */
797 span = a->size;
798 if (a->after) {
799 assert(a->after->offset != UINT64_MAX);
800 assert(a->after->current_size != UINT64_MAX);
801
994b3031 802 span += round_up_size(a->after->offset + a->after->current_size, context->grain_size) - a->after->offset;
e594a3b1
LP
803 }
804
ae0613c6 805 for (GrowPartitionPhase phase = 0; phase < _GROW_PARTITION_PHASE_MAX;) {
e594a3b1
LP
806 r = context_grow_partitions_phase(context, a, phase, &span, &weight_sum);
807 if (r < 0)
808 return r;
809 if (r == 0) /* not done yet, re-run this phase */
810 continue;
811
ae0613c6 812 phase++; /* got to next phase */
e594a3b1
LP
813 }
814
162392b7 815 /* We still have space left over? Donate to preceding partition if we have one */
e594a3b1
LP
816 if (span > 0 && a->after && !PARTITION_IS_FOREIGN(a->after)) {
817 uint64_t m, xsz;
818
819 assert(a->after->new_size != UINT64_MAX);
0b7f574f
LP
820
821 /* Calculate new size and align (but ensure this doesn't shrink the size) */
994b3031 822 m = MAX(a->after->new_size, round_down_size(a->after->new_size + span, context->grain_size));
e594a3b1 823
994b3031 824 xsz = partition_max_size(context, a->after);
e594a3b1
LP
825 if (xsz != UINT64_MAX && m > xsz)
826 m = xsz;
827
994b3031 828 span = charge_size(context, span, m - a->after->new_size);
e594a3b1
LP
829 a->after->new_size = m;
830 }
831
162392b7 832 /* What? Even still some space left (maybe because there was no preceding partition, or it had a
e594a3b1 833 * size limit), then let's donate it to whoever wants it. */
03677889 834 if (span > 0)
e594a3b1
LP
835 LIST_FOREACH(partitions, p, context->partitions) {
836 uint64_t m, xsz;
837
838 if (p->allocated_to_area != a)
839 continue;
840
841 if (PARTITION_IS_FOREIGN(p))
842 continue;
843
844 assert(p->new_size != UINT64_MAX);
994b3031 845 m = MAX(p->new_size, round_down_size(p->new_size + span, context->grain_size));
e594a3b1 846
994b3031 847 xsz = partition_max_size(context, p);
e594a3b1
LP
848 if (xsz != UINT64_MAX && m > xsz)
849 m = xsz;
850
994b3031 851 span = charge_size(context, span, m - p->new_size);
e594a3b1
LP
852 p->new_size = m;
853
854 if (span == 0)
855 break;
856 }
e594a3b1 857
162392b7 858 /* Yuck, still no one? Then make it padding */
e594a3b1
LP
859 if (span > 0 && a->after) {
860 assert(a->after->new_padding != UINT64_MAX);
861 a->after->new_padding += span;
862 }
863
864 return 0;
865}
866
867static int context_grow_partitions(Context *context) {
e594a3b1
LP
868 int r;
869
870 assert(context);
871
872 for (size_t i = 0; i < context->n_free_areas; i++) {
873 r = context_grow_partitions_on_free_area(context, context->free_areas[i]);
874 if (r < 0)
875 return r;
876 }
877
878 /* All existing partitions that have no free space after them can't change size */
879 LIST_FOREACH(partitions, p, context->partitions) {
880 if (p->dropped)
881 continue;
882
883 if (!PARTITION_EXISTS(p) || p->padding_area) {
884 /* The algorithm above must have initialized this already */
885 assert(p->new_size != UINT64_MAX);
886 continue;
887 }
888
889 assert(p->new_size == UINT64_MAX);
890 p->new_size = p->current_size;
891
892 assert(p->new_padding == UINT64_MAX);
893 p->new_padding = p->current_padding;
894 }
895
896 return 0;
897}
898
899static void context_place_partitions(Context *context) {
900 uint64_t partno = 0;
e594a3b1
LP
901
902 assert(context);
903
904 /* Determine next partition number to assign */
905 LIST_FOREACH(partitions, p, context->partitions) {
906 if (!PARTITION_EXISTS(p))
907 continue;
908
909 assert(p->partno != UINT64_MAX);
910 if (p->partno >= partno)
911 partno = p->partno + 1;
912 }
913
914 for (size_t i = 0; i < context->n_free_areas; i++) {
915 FreeArea *a = context->free_areas[i];
2ea7eb00
FS
916 _unused_ uint64_t left;
917 uint64_t start;
e594a3b1
LP
918
919 if (a->after) {
920 assert(a->after->offset != UINT64_MAX);
921 assert(a->after->new_size != UINT64_MAX);
922 assert(a->after->new_padding != UINT64_MAX);
923
924 start = a->after->offset + a->after->new_size + a->after->new_padding;
925 } else
926 start = context->start;
927
994b3031 928 start = round_up_size(start, context->grain_size);
e594a3b1
LP
929 left = a->size;
930
931 LIST_FOREACH(partitions, p, context->partitions) {
932 if (p->allocated_to_area != a)
933 continue;
934
935 p->offset = start;
936 p->partno = partno++;
937
938 assert(left >= p->new_size);
939 start += p->new_size;
940 left -= p->new_size;
941
942 assert(left >= p->new_padding);
943 start += p->new_padding;
944 left -= p->new_padding;
945 }
946 }
947}
948
e594a3b1
LP
949static int config_parse_type(
950 const char *unit,
951 const char *filename,
952 unsigned line,
953 const char *section,
954 unsigned section_line,
955 const char *lvalue,
956 int ltype,
957 const char *rvalue,
958 void *data,
959 void *userdata) {
960
961 sd_id128_t *type_uuid = data;
962 int r;
963
964 assert(rvalue);
965 assert(type_uuid);
966
967 r = gpt_partition_type_uuid_from_string(rvalue, type_uuid);
968 if (r < 0)
969 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse partition type: %s", rvalue);
970
971 return 0;
972}
973
974static int config_parse_label(
975 const char *unit,
976 const char *filename,
977 unsigned line,
978 const char *section,
979 unsigned section_line,
980 const char *lvalue,
981 int ltype,
982 const char *rvalue,
983 void *data,
984 void *userdata) {
985
e031166e 986 _cleanup_free_ char *resolved = NULL;
e594a3b1
LP
987 char **label = data;
988 int r;
989
990 assert(rvalue);
991 assert(label);
992
be9ce018
LP
993 /* Nota bene: the empty label is a totally valid one. Let's hence not follow our usual rule of
994 * assigning the empty string to reset to default here, but really accept it as label to set. */
995
de61a04b 996 r = specifier_printf(rvalue, GPT_LABEL_MAX, system_and_tmp_specifier_table, arg_root, NULL, &resolved);
e031166e 997 if (r < 0) {
e459258f 998 log_syntax(unit, LOG_WARNING, filename, line, r,
e031166e
LP
999 "Failed to expand specifiers in Label=, ignoring: %s", rvalue);
1000 return 0;
1001 }
1002
1003 if (!utf8_is_valid(resolved)) {
e594a3b1
LP
1004 log_syntax(unit, LOG_WARNING, filename, line, 0,
1005 "Partition label not valid UTF-8, ignoring: %s", rvalue);
1006 return 0;
1007 }
1008
22a0a36e
LP
1009 r = gpt_partition_label_valid(resolved);
1010 if (r < 0) {
1011 log_syntax(unit, LOG_WARNING, filename, line, r,
1012 "Failed to check if string is valid as GPT partition label, ignoring: \"%s\" (from \"%s\")",
1013 resolved, rvalue);
1014 return 0;
1015 }
1016 if (!r) {
e594a3b1 1017 log_syntax(unit, LOG_WARNING, filename, line, 0,
46072ae3
ZJS
1018 "Partition label too long for GPT table, ignoring: \"%s\" (from \"%s\")",
1019 resolved, rvalue);
e594a3b1
LP
1020 return 0;
1021 }
1022
e031166e 1023 free_and_replace(*label, resolved);
e594a3b1
LP
1024 return 0;
1025}
1026
1027static int config_parse_weight(
1028 const char *unit,
1029 const char *filename,
1030 unsigned line,
1031 const char *section,
1032 unsigned section_line,
1033 const char *lvalue,
1034 int ltype,
1035 const char *rvalue,
1036 void *data,
1037 void *userdata) {
1038
1039 uint32_t *priority = data, v;
1040 int r;
1041
1042 assert(rvalue);
1043 assert(priority);
1044
1045 r = safe_atou32(rvalue, &v);
1046 if (r < 0) {
1047 log_syntax(unit, LOG_WARNING, filename, line, r,
1048 "Failed to parse weight value, ignoring: %s", rvalue);
1049 return 0;
1050 }
1051
1052 if (v > 1000U*1000U) {
c8f3d767 1053 log_syntax(unit, LOG_WARNING, filename, line, 0,
e594a3b1
LP
1054 "Weight needs to be in range 0…10000000, ignoring: %" PRIu32, v);
1055 return 0;
1056 }
1057
1058 *priority = v;
1059 return 0;
1060}
1061
1062static int config_parse_size4096(
1063 const char *unit,
1064 const char *filename,
1065 unsigned line,
1066 const char *section,
1067 unsigned section_line,
1068 const char *lvalue,
1069 int ltype,
1070 const char *rvalue,
1071 void *data,
1072 void *userdata) {
1073
1074 uint64_t *sz = data, parsed;
1075 int r;
1076
1077 assert(rvalue);
1078 assert(data);
1079
1080 r = parse_size(rvalue, 1024, &parsed);
1081 if (r < 0)
c8f3d767 1082 return log_syntax(unit, LOG_ERR, filename, line, r,
e594a3b1
LP
1083 "Failed to parse size value: %s", rvalue);
1084
1085 if (ltype > 0)
1086 *sz = round_up_size(parsed, 4096);
1087 else if (ltype < 0)
1088 *sz = round_down_size(parsed, 4096);
1089 else
1090 *sz = parsed;
1091
1092 if (*sz != parsed)
e2341b6b
DT
1093 log_syntax(unit, LOG_NOTICE, filename, line, r, "Rounded %s= size %" PRIu64 " %s %" PRIu64 ", a multiple of 4096.",
1094 lvalue, parsed, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), *sz);
e594a3b1
LP
1095
1096 return 0;
1097}
1098
53171c04
LP
1099static int config_parse_fstype(
1100 const char *unit,
1101 const char *filename,
1102 unsigned line,
1103 const char *section,
1104 unsigned section_line,
1105 const char *lvalue,
1106 int ltype,
1107 const char *rvalue,
1108 void *data,
1109 void *userdata) {
1110
1111 char **fstype = data;
1112
1113 assert(rvalue);
1114 assert(data);
1115
1116 if (!filename_is_valid(rvalue))
1117 return log_syntax(unit, LOG_ERR, filename, line, 0,
1118 "File system type is not valid, refusing: %s", rvalue);
1119
1120 return free_and_strdup_warn(fstype, rvalue);
1121}
1122
8a794850
LP
1123static int config_parse_copy_files(
1124 const char *unit,
1125 const char *filename,
1126 unsigned line,
1127 const char *section,
1128 unsigned section_line,
1129 const char *lvalue,
1130 int ltype,
1131 const char *rvalue,
1132 void *data,
1133 void *userdata) {
1134
1135 _cleanup_free_ char *source = NULL, *buffer = NULL, *resolved_source = NULL, *resolved_target = NULL;
1136 const char *p = rvalue, *target;
1137 Partition *partition = data;
1138 int r;
1139
1140 assert(rvalue);
1141 assert(partition);
1142
1143 r = extract_first_word(&p, &source, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS);
1144 if (r < 0)
1145 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract source path: %s", rvalue);
1146 if (r == 0) {
1147 log_syntax(unit, LOG_WARNING, filename, line, 0, "No argument specified: %s", rvalue);
1148 return 0;
1149 }
1150
1151 r = extract_first_word(&p, &buffer, ":", EXTRACT_CUNESCAPE|EXTRACT_DONT_COALESCE_SEPARATORS);
1152 if (r < 0)
1153 return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to extract target path: %s", rvalue);
1154 if (r == 0)
1155 target = source; /* No target, then it's the same as the source */
1156 else
1157 target = buffer;
1158
1159 if (!isempty(p))
1160 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL), "Too many arguments: %s", rvalue);
1161
de61a04b 1162 r = specifier_printf(source, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_source);
8a794850
LP
1163 if (r < 0) {
1164 log_syntax(unit, LOG_WARNING, filename, line, r,
1165 "Failed to expand specifiers in CopyFiles= source, ignoring: %s", rvalue);
1166 return 0;
1167 }
1168
0ade2213
LP
1169 r = path_simplify_and_warn(resolved_source, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1170 if (r < 0)
8a794850 1171 return 0;
8a794850 1172
de61a04b 1173 r = specifier_printf(target, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &resolved_target);
8a794850
LP
1174 if (r < 0) {
1175 log_syntax(unit, LOG_WARNING, filename, line, r,
1176 "Failed to expand specifiers in CopyFiles= target, ignoring: %s", resolved_target);
1177 return 0;
1178 }
1179
0ade2213
LP
1180 r = path_simplify_and_warn(resolved_target, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1181 if (r < 0)
8a794850 1182 return 0;
8a794850
LP
1183
1184 r = strv_consume_pair(&partition->copy_files, TAKE_PTR(resolved_source), TAKE_PTR(resolved_target));
1185 if (r < 0)
1186 return log_oom();
1187
1188 return 0;
1189}
1190
5c08da58
LP
1191static int config_parse_copy_blocks(
1192 const char *unit,
1193 const char *filename,
1194 unsigned line,
1195 const char *section,
1196 unsigned section_line,
1197 const char *lvalue,
1198 int ltype,
1199 const char *rvalue,
1200 void *data,
1201 void *userdata) {
1202
1203 _cleanup_free_ char *d = NULL;
1204 Partition *partition = data;
1205 int r;
1206
1207 assert(rvalue);
1208 assert(partition);
1209
1210 if (isempty(rvalue)) {
1211 partition->copy_blocks_path = mfree(partition->copy_blocks_path);
1212 partition->copy_blocks_auto = false;
1213 return 0;
1214 }
1215
1216 if (streq(rvalue, "auto")) {
1217 partition->copy_blocks_path = mfree(partition->copy_blocks_path);
1218 partition->copy_blocks_auto = true;
1219 return 0;
1220 }
1221
de61a04b 1222 r = specifier_printf(rvalue, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &d);
5c08da58
LP
1223 if (r < 0) {
1224 log_syntax(unit, LOG_WARNING, filename, line, r,
1225 "Failed to expand specifiers in CopyBlocks= source path, ignoring: %s", rvalue);
1226 return 0;
1227 }
1228
1229 r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1230 if (r < 0)
1231 return 0;
1232
1233 free_and_replace(partition->copy_blocks_path, d);
1234 partition->copy_blocks_auto = false;
1235 return 0;
1236}
1237
d83d8048
LP
1238static int config_parse_make_dirs(
1239 const char *unit,
1240 const char *filename,
1241 unsigned line,
1242 const char *section,
1243 unsigned section_line,
1244 const char *lvalue,
1245 int ltype,
1246 const char *rvalue,
1247 void *data,
1248 void *userdata) {
1249
1250 Partition *partition = data;
1251 const char *p = rvalue;
1252 int r;
1253
1254 assert(rvalue);
1255 assert(partition);
1256
1257 for (;;) {
1258 _cleanup_free_ char *word = NULL, *d = NULL;
1259
1260 r = extract_first_word(&p, &word, NULL, EXTRACT_UNQUOTE);
1261 if (r == -ENOMEM)
1262 return log_oom();
1263 if (r < 0) {
1264 log_syntax(unit, LOG_WARNING, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
1265 return 0;
1266 }
1267 if (r == 0)
1268 return 0;
1269
de61a04b 1270 r = specifier_printf(word, PATH_MAX-1, system_and_tmp_specifier_table, arg_root, NULL, &d);
d83d8048
LP
1271 if (r < 0) {
1272 log_syntax(unit, LOG_WARNING, filename, line, r,
1273 "Failed to expand specifiers in MakeDirectories= parameter, ignoring: %s", word);
1274 continue;
1275 }
1276
1277 r = path_simplify_and_warn(d, PATH_CHECK_ABSOLUTE, unit, filename, line, lvalue);
1278 if (r < 0)
1279 continue;
1280
1281 r = strv_consume(&partition->make_directories, TAKE_PTR(d));
1282 if (r < 0)
1283 return log_oom();
1284 }
1285}
1286
889914ef
LP
1287static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_encrypt, encrypt_mode, EncryptMode, ENCRYPT_OFF, "Invalid encryption mode");
1288
e73309c5
LP
1289static int config_parse_gpt_flags(
1290 const char *unit,
1291 const char *filename,
1292 unsigned line,
1293 const char *section,
1294 unsigned section_line,
1295 const char *lvalue,
1296 int ltype,
1297 const char *rvalue,
1298 void *data,
1299 void *userdata) {
1300
1301 uint64_t *gpt_flags = data;
1302 int r;
1303
1304 assert(rvalue);
1305 assert(gpt_flags);
1306
1307 r = safe_atou64(rvalue, gpt_flags);
1308 if (r < 0) {
1309 log_syntax(unit, LOG_WARNING, filename, line, r,
1310 "Failed to parse Flags= value, ignoring: %s", rvalue);
1311 return 0;
1312 }
1313
1314 return 0;
1315}
1316
11749b61
DDM
1317static int config_parse_uuid(
1318 const char *unit,
1319 const char *filename,
1320 unsigned line,
1321 const char *section,
1322 unsigned section_line,
1323 const char *lvalue,
1324 int ltype,
1325 const char *rvalue,
1326 void *data,
1327 void *userdata) {
1328
1329 Partition *partition = ASSERT_PTR(data);
1330 int r;
1331
1332 if (isempty(rvalue)) {
1333 partition->new_uuid = SD_ID128_NULL;
1334 partition->new_uuid_is_set = false;
1335 return 0;
1336 }
1337
1338 if (streq(rvalue, "null")) {
1339 partition->new_uuid = SD_ID128_NULL;
1340 partition->new_uuid_is_set = true;
1341 return 0;
1342 }
1343
1344 r = sd_id128_from_string(rvalue, &partition->new_uuid);
1345 if (r < 0) {
1346 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse 128bit ID/UUID, ignoring: %s", rvalue);
1347 return 0;
1348 }
1349
1350 partition->new_uuid_is_set = true;
1351
1352 return 0;
1353}
1354
39fc0174 1355static int partition_read_definition(Partition *p, const char *path, const char *const *conf_file_dirs) {
e594a3b1
LP
1356
1357 ConfigTableItem table[] = {
5c08da58
LP
1358 { "Partition", "Type", config_parse_type, 0, &p->type_uuid },
1359 { "Partition", "Label", config_parse_label, 0, &p->new_label },
11749b61 1360 { "Partition", "UUID", config_parse_uuid, 0, p },
5c08da58
LP
1361 { "Partition", "Priority", config_parse_int32, 0, &p->priority },
1362 { "Partition", "Weight", config_parse_weight, 0, &p->weight },
1363 { "Partition", "PaddingWeight", config_parse_weight, 0, &p->padding_weight },
1364 { "Partition", "SizeMinBytes", config_parse_size4096, 1, &p->size_min },
1365 { "Partition", "SizeMaxBytes", config_parse_size4096, -1, &p->size_max },
1366 { "Partition", "PaddingMinBytes", config_parse_size4096, 1, &p->padding_min },
1367 { "Partition", "PaddingMaxBytes", config_parse_size4096, -1, &p->padding_max },
1368 { "Partition", "FactoryReset", config_parse_bool, 0, &p->factory_reset },
1369 { "Partition", "CopyBlocks", config_parse_copy_blocks, 0, p },
1370 { "Partition", "Format", config_parse_fstype, 0, &p->format },
1371 { "Partition", "CopyFiles", config_parse_copy_files, 0, p },
1372 { "Partition", "MakeDirectories", config_parse_make_dirs, 0, p },
1373 { "Partition", "Encrypt", config_parse_encrypt, 0, &p->encrypt },
e73309c5
LP
1374 { "Partition", "Flags", config_parse_gpt_flags, 0, &p->gpt_flags },
1375 { "Partition", "ReadOnly", config_parse_tristate, 0, &p->read_only },
ff0771bf 1376 { "Partition", "NoAuto", config_parse_tristate, 0, &p->no_auto },
1c41c1dc 1377 { "Partition", "GrowFileSystem", config_parse_tristate, 0, &p->growfs },
e594a3b1
LP
1378 {}
1379 };
1380 int r;
39fc0174
RP
1381 _cleanup_free_ char *filename = NULL;
1382 const char* dropin_dirname;
e594a3b1 1383
39fc0174
RP
1384 r = path_extract_filename(path, &filename);
1385 if (r < 0)
1386 return log_error_errno(r, "Failed to extract filename from path '%s': %m", path);;
1387
1388 dropin_dirname = strjoina(filename, ".d");
1389
1390 r = config_parse_many(
1391 STRV_MAKE_CONST(path),
1392 conf_file_dirs,
1393 dropin_dirname,
1394 "Partition\0",
1395 config_item_table_lookup, table,
1396 CONFIG_PARSE_WARN,
1397 p,
1398 NULL,
1399 &p->drop_in_files);
e594a3b1
LP
1400 if (r < 0)
1401 return r;
1402
1403 if (p->size_min != UINT64_MAX && p->size_max != UINT64_MAX && p->size_min > p->size_max)
1404 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1405 "SizeMinBytes= larger than SizeMaxBytes=, refusing.");
1406
1407 if (p->padding_min != UINT64_MAX && p->padding_max != UINT64_MAX && p->padding_min > p->padding_max)
1408 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1409 "PaddingMinBytes= larger than PaddingMaxBytes=, refusing.");
1410
1411 if (sd_id128_is_null(p->type_uuid))
1412 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1413 "Type= not defined, refusing.");
1414
5c08da58
LP
1415 if ((p->copy_blocks_path || p->copy_blocks_auto) &&
1416 (p->format || !strv_isempty(p->copy_files) || !strv_isempty(p->make_directories)))
53171c04 1417 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
5c08da58 1418 "Format=/CopyFiles=/MakeDirectories= and CopyBlocks= cannot be combined, refusing.");
53171c04 1419
d83d8048 1420 if ((!strv_isempty(p->copy_files) || !strv_isempty(p->make_directories)) && streq_ptr(p->format, "swap"))
8a794850
LP
1421 return log_syntax(NULL, LOG_ERR, path, 1, SYNTHETIC_ERRNO(EINVAL),
1422 "Format=swap and CopyFiles= cannot be combined, refusing.");
1423
5c08da58 1424 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 1425 /* Pick "ext4" as file system if we are configured to copy files or encrypt the device */
8a794850
LP
1426 p->format = strdup("ext4");
1427 if (!p->format)
1428 return log_oom();
1429 }
1430
e73309c5
LP
1431 /* Verity partitions are read only, let's imply the RO flag hence, unless explicitly configured otherwise. */
1432 if ((gpt_partition_type_is_root_verity(p->type_uuid) ||
1433 gpt_partition_type_is_usr_verity(p->type_uuid)) &&
1434 p->read_only < 0)
1435 p->read_only = true;
1436
1c41c1dc
LP
1437 /* Default to "growfs" on, unless read-only */
1438 if (gpt_partition_type_knows_growfs(p->type_uuid) &&
1439 p->read_only <= 0)
1440 p->growfs = true;
1441
e594a3b1
LP
1442 return 0;
1443}
1444
1445static int context_read_definitions(
1446 Context *context,
224c853f 1447 char **directories,
e594a3b1
LP
1448 const char *root) {
1449
1450 _cleanup_strv_free_ char **files = NULL;
1451 Partition *last = NULL;
e594a3b1 1452 int r;
39fc0174 1453 const char *const *dirs;
e594a3b1
LP
1454
1455 assert(context);
1456
224c853f 1457 dirs = (const char* const*) (directories ?: CONF_PATHS_STRV("repart.d"));
39fc0174 1458
224c853f 1459 r = conf_files_list_strv(&files, ".conf", directories ? NULL : root, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, dirs);
e594a3b1
LP
1460 if (r < 0)
1461 return log_error_errno(r, "Failed to enumerate *.conf files: %m");
1462
1463 STRV_FOREACH(f, files) {
1464 _cleanup_(partition_freep) Partition *p = NULL;
1465
1466 p = partition_new();
1467 if (!p)
1468 return log_oom();
1469
1470 p->definition_path = strdup(*f);
1471 if (!p->definition_path)
1472 return log_oom();
1473
39fc0174 1474 r = partition_read_definition(p, *f, dirs);
e594a3b1
LP
1475 if (r < 0)
1476 return r;
1477
1478 LIST_INSERT_AFTER(partitions, context->partitions, last, p);
1479 last = TAKE_PTR(p);
1480 context->n_partitions++;
1481 }
1482
1483 return 0;
1484}
1485
e594a3b1
LP
1486static int determine_current_padding(
1487 struct fdisk_context *c,
1488 struct fdisk_table *t,
1489 struct fdisk_partition *p,
994b3031
LP
1490 uint64_t secsz,
1491 uint64_t grainsz,
e594a3b1
LP
1492 uint64_t *ret) {
1493
1494 size_t n_partitions;
1495 uint64_t offset, next = UINT64_MAX;
1496
1497 assert(c);
1498 assert(t);
1499 assert(p);
1500
1501 if (!fdisk_partition_has_end(p))
1502 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition has no end!");
1503
1504 offset = fdisk_partition_get_end(p);
994b3031
LP
1505 assert(offset < UINT64_MAX / secsz);
1506 offset *= secsz;
e594a3b1
LP
1507
1508 n_partitions = fdisk_table_get_nents(t);
695cfd53 1509 for (size_t i = 0; i < n_partitions; i++) {
e594a3b1
LP
1510 struct fdisk_partition *q;
1511 uint64_t start;
1512
1513 q = fdisk_table_get_partition(t, i);
1514 if (!q)
1515 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
1516
1517 if (fdisk_partition_is_used(q) <= 0)
1518 continue;
1519
1520 if (!fdisk_partition_has_start(q))
1521 continue;
1522
1523 start = fdisk_partition_get_start(q);
994b3031
LP
1524 assert(start < UINT64_MAX / secsz);
1525 start *= secsz;
e594a3b1
LP
1526
1527 if (start >= offset && (next == UINT64_MAX || next > start))
1528 next = start;
1529 }
1530
1531 if (next == UINT64_MAX) {
1532 /* No later partition? In that case check the end of the usable area */
1533 next = fdisk_get_last_lba(c);
1534 assert(next < UINT64_MAX);
1535 next++; /* The last LBA is one sector before the end */
1536
994b3031
LP
1537 assert(next < UINT64_MAX / secsz);
1538 next *= secsz;
e594a3b1
LP
1539
1540 if (offset > next)
1541 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end.");
1542 }
1543
1544 assert(next >= offset);
994b3031
LP
1545 offset = round_up_size(offset, grainsz);
1546 next = round_down_size(next, grainsz);
e594a3b1 1547
a6f44d61 1548 *ret = LESS_BY(next, offset); /* Saturated subtraction, rounding might have fucked things up */
e594a3b1
LP
1549 return 0;
1550}
1551
1552static int fdisk_ask_cb(struct fdisk_context *c, struct fdisk_ask *ask, void *data) {
1553 _cleanup_free_ char *ids = NULL;
1554 int r;
1555
1556 if (fdisk_ask_get_type(ask) != FDISK_ASKTYPE_STRING)
1557 return -EINVAL;
1558
b7416360 1559 ids = new(char, SD_ID128_UUID_STRING_MAX);
e594a3b1
LP
1560 if (!ids)
1561 return -ENOMEM;
1562
b7416360 1563 r = fdisk_ask_string_set_result(ask, sd_id128_to_uuid_string(*(sd_id128_t*) data, ids));
e594a3b1
LP
1564 if (r < 0)
1565 return r;
1566
1567 TAKE_PTR(ids);
1568 return 0;
1569}
1570
1571static int fdisk_set_disklabel_id_by_uuid(struct fdisk_context *c, sd_id128_t id) {
1572 int r;
1573
1574 r = fdisk_set_ask(c, fdisk_ask_cb, &id);
1575 if (r < 0)
1576 return r;
1577
1578 r = fdisk_set_disklabel_id(c);
1579 if (r < 0)
1580 return r;
1581
1582 return fdisk_set_ask(c, NULL, NULL);
1583}
1584
53171c04 1585static int derive_uuid(sd_id128_t base, const char *token, sd_id128_t *ret) {
e594a3b1 1586 union {
ade99252 1587 uint8_t md[SHA256_DIGEST_SIZE];
e594a3b1
LP
1588 sd_id128_t id;
1589 } result;
1590
53171c04 1591 assert(token);
e594a3b1
LP
1592 assert(ret);
1593
53171c04
LP
1594 /* Derive a new UUID from the specified UUID in a stable and reasonably safe way. Specifically, we
1595 * calculate the HMAC-SHA256 of the specified token string, keyed by the supplied base (typically the
1596 * machine ID). We use the machine ID as key (and not as cleartext!) of the HMAC operation since it's
1597 * the machine ID we don't want to leak. */
e594a3b1 1598
ade99252 1599 hmac_sha256(base.bytes, sizeof(base.bytes), token, strlen(token), result.md);
e594a3b1
LP
1600
1601 /* Take the first half, mark it as v4 UUID */
1602 assert_cc(sizeof(result.md) == sizeof(result.id) * 2);
1603 *ret = id128_make_v4_uuid(result.id);
1604 return 0;
1605}
1606
a26f4a49
LP
1607static int context_load_partition_table(
1608 Context *context,
1609 const char *node,
1610 int *backing_fd) {
1611
e594a3b1
LP
1612 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
1613 _cleanup_(fdisk_unref_tablep) struct fdisk_table *t = NULL;
1614 uint64_t left_boundary = UINT64_MAX, first_lba, last_lba, nsectors;
1615 _cleanup_free_ char *disk_uuid_string = NULL;
1616 bool from_scratch = false;
1617 sd_id128_t disk_uuid;
1618 size_t n_partitions;
994b3031
LP
1619 unsigned long secsz;
1620 uint64_t grainsz;
e594a3b1
LP
1621 int r;
1622
1623 assert(context);
1624 assert(node);
a26f4a49 1625 assert(backing_fd);
170c9823
LP
1626 assert(!context->fdisk_context);
1627 assert(!context->free_areas);
1628 assert(context->start == UINT64_MAX);
1629 assert(context->end == UINT64_MAX);
1630 assert(context->total == UINT64_MAX);
e594a3b1
LP
1631
1632 c = fdisk_new_context();
1633 if (!c)
1634 return log_oom();
1635
a26f4a49
LP
1636 /* libfdisk doesn't have an API to operate on arbitrary fds, hence reopen the fd going via the
1637 * /proc/self/fd/ magic path if we have an existing fd. Open the original file otherwise. */
1638 if (*backing_fd < 0)
1639 r = fdisk_assign_device(c, node, arg_dry_run);
ddb6eeaf
LP
1640 else
1641 r = fdisk_assign_device(c, FORMAT_PROC_FD_PATH(*backing_fd), arg_dry_run);
170c9823
LP
1642 if (r == -EINVAL && arg_size_auto) {
1643 struct stat st;
1644
1645 /* libfdisk returns EINVAL if opening a file of size zero. Let's check for that, and accept
1646 * it if automatic sizing is requested. */
1647
1648 if (*backing_fd < 0)
1649 r = stat(node, &st);
1650 else
1651 r = fstat(*backing_fd, &st);
1652 if (r < 0)
1653 return log_error_errno(errno, "Failed to stat block device '%s': %m", node);
1654
994b3031
LP
1655 if (S_ISREG(st.st_mode) && st.st_size == 0) {
1656 /* User the fallback values if we have no better idea */
1657 context->sector_size = 512;
1658 context->grain_size = 4096;
170c9823 1659 return /* from_scratch = */ true;
994b3031 1660 }
170c9823
LP
1661
1662 r = -EINVAL;
1663 }
e594a3b1 1664 if (r < 0)
a26f4a49
LP
1665 return log_error_errno(r, "Failed to open device '%s': %m", node);
1666
1667 if (*backing_fd < 0) {
1668 /* If we have no fd referencing the device yet, make a copy of the fd now, so that we have one */
38f81e93 1669 *backing_fd = fd_reopen(fdisk_get_devfd(c), O_RDONLY|O_CLOEXEC);
a26f4a49 1670 if (*backing_fd < 0)
38f81e93 1671 return log_error_errno(*backing_fd, "Failed to duplicate fdisk fd: %m");
e594a3b1 1672
25baae50
DDM
1673 /* Tell udev not to interfere while we are processing the device */
1674 if (flock(*backing_fd, arg_dry_run ? LOCK_SH : LOCK_EX) < 0)
1675 return log_error_errno(errno, "Failed to lock block device: %m");
1676 }
e594a3b1 1677
994b3031
LP
1678 /* The offsets/sizes libfdisk returns to us will be in multiple of the sector size of the
1679 * device. This is typically 512, and sometimes 4096. Let's query libfdisk once for it, and then use
1680 * it for all our needs. Note that the values we use ourselves always are in bytes though, thus mean
1681 * the same thing universally. Also note that regardless what kind of sector size is in use we'll
1682 * place partitions at multiples of 4K. */
1683 secsz = fdisk_get_sector_size(c);
1684
1685 /* Insist on a power of two, and that it's a multiple of 512, i.e. the traditional sector size. */
983ce0b5
LP
1686 if (secsz < 512 || !ISPOWEROF2(secsz))
1687 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Sector size %lu is not a power of two larger than 512? Refusing.", secsz);
994b3031
LP
1688
1689 /* Use at least 4K, and ensure it's a multiple of the sector size, regardless if that is smaller or
1690 * larger */
1691 grainsz = secsz < 4096 ? 4096 : secsz;
1692
1693 log_debug("Sector size of device is %lu bytes. Using grain size of %" PRIu64 ".", secsz, grainsz);
1694
e594a3b1
LP
1695 switch (arg_empty) {
1696
1697 case EMPTY_REFUSE:
1698 /* Refuse empty disks, insist on an existing GPT partition table */
1699 if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
1700 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has no GPT disk label, not repartitioning.", node);
1701
1702 break;
1703
1704 case EMPTY_REQUIRE:
1705 /* Require an empty disk, refuse any existing partition table */
1706 r = fdisk_has_label(c);
1707 if (r < 0)
1708 return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", node);
1709 if (r > 0)
1710 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s already has a disk label, refusing.", node);
1711
1712 from_scratch = true;
1713 break;
1714
1715 case EMPTY_ALLOW:
1716 /* Allow both an empty disk and an existing partition table, but only GPT */
1717 r = fdisk_has_label(c);
1718 if (r < 0)
1719 return log_error_errno(r, "Failed to determine whether disk %s has a disk label: %m", node);
1720 if (r > 0) {
1721 if (!fdisk_is_labeltype(c, FDISK_DISKLABEL_GPT))
1722 return log_notice_errno(SYNTHETIC_ERRNO(EHWPOISON), "Disk %s has non-GPT disk label, not repartitioning.", node);
1723 } else
1724 from_scratch = true;
1725
1726 break;
1727
1728 case EMPTY_FORCE:
a26f4a49 1729 case EMPTY_CREATE:
e594a3b1
LP
1730 /* Always reinitiaize the disk, don't consider what there was on the disk before */
1731 from_scratch = true;
1732 break;
1733 }
1734
1735 if (from_scratch) {
e594a3b1
LP
1736 r = fdisk_create_disklabel(c, "gpt");
1737 if (r < 0)
1738 return log_error_errno(r, "Failed to create GPT disk label: %m");
1739
53171c04 1740 r = derive_uuid(context->seed, "disk-uuid", &disk_uuid);
e594a3b1
LP
1741 if (r < 0)
1742 return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
1743
1744 r = fdisk_set_disklabel_id_by_uuid(c, disk_uuid);
1745 if (r < 0)
1746 return log_error_errno(r, "Failed to set GPT disk label: %m");
1747
1748 goto add_initial_free_area;
1749 }
1750
1751 r = fdisk_get_disklabel_id(c, &disk_uuid_string);
1752 if (r < 0)
1753 return log_error_errno(r, "Failed to get current GPT disk label UUID: %m");
1754
1755 r = sd_id128_from_string(disk_uuid_string, &disk_uuid);
1756 if (r < 0)
1757 return log_error_errno(r, "Failed to parse current GPT disk label UUID: %m");
1758
1759 if (sd_id128_is_null(disk_uuid)) {
53171c04 1760 r = derive_uuid(context->seed, "disk-uuid", &disk_uuid);
e594a3b1
LP
1761 if (r < 0)
1762 return log_error_errno(r, "Failed to acquire disk GPT uuid: %m");
1763
1764 r = fdisk_set_disklabel_id(c);
1765 if (r < 0)
1766 return log_error_errno(r, "Failed to set GPT disk label: %m");
1767 }
1768
1769 r = fdisk_get_partitions(c, &t);
1770 if (r < 0)
1771 return log_error_errno(r, "Failed to acquire partition table: %m");
1772
1773 n_partitions = fdisk_table_get_nents(t);
695cfd53 1774 for (size_t i = 0; i < n_partitions; i++) {
e594a3b1 1775 _cleanup_free_ char *label_copy = NULL;
03677889 1776 Partition *last = NULL;
e594a3b1
LP
1777 struct fdisk_partition *p;
1778 struct fdisk_parttype *pt;
1779 const char *pts, *ids, *label;
1780 uint64_t sz, start;
1781 bool found = false;
1782 sd_id128_t ptid, id;
1783 size_t partno;
1784
1785 p = fdisk_table_get_partition(t, i);
1786 if (!p)
1787 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to read partition metadata: %m");
1788
1789 if (fdisk_partition_is_used(p) <= 0)
1790 continue;
1791
1792 if (fdisk_partition_has_start(p) <= 0 ||
1793 fdisk_partition_has_size(p) <= 0 ||
1794 fdisk_partition_has_partno(p) <= 0)
1795 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a position, size or number.");
1796
1797 pt = fdisk_partition_get_type(p);
1798 if (!pt)
1799 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition: %m");
1800
1801 pts = fdisk_parttype_get_string(pt);
1802 if (!pts)
1803 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to acquire type of partition as string: %m");
1804
1805 r = sd_id128_from_string(pts, &ptid);
1806 if (r < 0)
1807 return log_error_errno(r, "Failed to parse partition type UUID %s: %m", pts);
1808
1809 ids = fdisk_partition_get_uuid(p);
1810 if (!ids)
1811 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Found a partition without a UUID.");
1812
1813 r = sd_id128_from_string(ids, &id);
1814 if (r < 0)
1815 return log_error_errno(r, "Failed to parse partition UUID %s: %m", ids);
1816
1817 label = fdisk_partition_get_name(p);
1818 if (!isempty(label)) {
1819 label_copy = strdup(label);
1820 if (!label_copy)
1821 return log_oom();
1822 }
1823
1824 sz = fdisk_partition_get_size(p);
ac33e147 1825 assert(sz <= UINT64_MAX/secsz);
994b3031 1826 sz *= secsz;
e594a3b1
LP
1827
1828 start = fdisk_partition_get_start(p);
ac33e147 1829 assert(start <= UINT64_MAX/secsz);
994b3031 1830 start *= secsz;
e594a3b1
LP
1831
1832 partno = fdisk_partition_get_partno(p);
1833
1834 if (left_boundary == UINT64_MAX || left_boundary > start)
1835 left_boundary = start;
1836
1837 /* Assign this existing partition to the first partition of the right type that doesn't have
1838 * an existing one assigned yet. */
1839 LIST_FOREACH(partitions, pp, context->partitions) {
1840 last = pp;
1841
1842 if (!sd_id128_equal(pp->type_uuid, ptid))
1843 continue;
1844
1845 if (!pp->current_partition) {
1846 pp->current_uuid = id;
1847 pp->current_size = sz;
1848 pp->offset = start;
1849 pp->partno = partno;
1850 pp->current_label = TAKE_PTR(label_copy);
1851
1852 pp->current_partition = p;
1853 fdisk_ref_partition(p);
1854
994b3031 1855 r = determine_current_padding(c, t, p, secsz, grainsz, &pp->current_padding);
e594a3b1
LP
1856 if (r < 0)
1857 return r;
1858
1859 if (pp->current_padding > 0) {
1860 r = context_add_free_area(context, pp->current_padding, pp);
1861 if (r < 0)
1862 return r;
1863 }
1864
1865 found = true;
1866 break;
1867 }
1868 }
1869
1870 /* If we have no matching definition, create a new one. */
1871 if (!found) {
1872 _cleanup_(partition_freep) Partition *np = NULL;
1873
1874 np = partition_new();
1875 if (!np)
1876 return log_oom();
1877
1878 np->current_uuid = id;
1879 np->type_uuid = ptid;
1880 np->current_size = sz;
1881 np->offset = start;
1882 np->partno = partno;
1883 np->current_label = TAKE_PTR(label_copy);
1884
1885 np->current_partition = p;
1886 fdisk_ref_partition(p);
1887
994b3031 1888 r = determine_current_padding(c, t, p, secsz, grainsz, &np->current_padding);
e594a3b1
LP
1889 if (r < 0)
1890 return r;
1891
1892 if (np->current_padding > 0) {
1893 r = context_add_free_area(context, np->current_padding, np);
1894 if (r < 0)
1895 return r;
1896 }
1897
1898 LIST_INSERT_AFTER(partitions, context->partitions, last, TAKE_PTR(np));
1899 context->n_partitions++;
1900 }
1901 }
1902
1903add_initial_free_area:
1904 nsectors = fdisk_get_nsectors(c);
994b3031
LP
1905 assert(nsectors <= UINT64_MAX/secsz);
1906 nsectors *= secsz;
e594a3b1
LP
1907
1908 first_lba = fdisk_get_first_lba(c);
994b3031
LP
1909 assert(first_lba <= UINT64_MAX/secsz);
1910 first_lba *= secsz;
e594a3b1
LP
1911
1912 last_lba = fdisk_get_last_lba(c);
1913 assert(last_lba < UINT64_MAX);
1914 last_lba++;
994b3031
LP
1915 assert(last_lba <= UINT64_MAX/secsz);
1916 last_lba *= secsz;
e594a3b1
LP
1917
1918 assert(last_lba >= first_lba);
1919
1920 if (left_boundary == UINT64_MAX) {
1921 /* No partitions at all? Then the whole disk is up for grabs. */
1922
994b3031
LP
1923 first_lba = round_up_size(first_lba, grainsz);
1924 last_lba = round_down_size(last_lba, grainsz);
e594a3b1
LP
1925
1926 if (last_lba > first_lba) {
1927 r = context_add_free_area(context, last_lba - first_lba, NULL);
1928 if (r < 0)
1929 return r;
1930 }
1931 } else {
1932 /* Add space left of first partition */
1933 assert(left_boundary >= first_lba);
1934
994b3031
LP
1935 first_lba = round_up_size(first_lba, grainsz);
1936 left_boundary = round_down_size(left_boundary, grainsz);
1937 last_lba = round_down_size(last_lba, grainsz);
e594a3b1
LP
1938
1939 if (left_boundary > first_lba) {
1940 r = context_add_free_area(context, left_boundary - first_lba, NULL);
1941 if (r < 0)
1942 return r;
1943 }
1944 }
1945
1946 context->start = first_lba;
1947 context->end = last_lba;
1948 context->total = nsectors;
994b3031
LP
1949 context->sector_size = secsz;
1950 context->grain_size = grainsz;
e594a3b1
LP
1951 context->fdisk_context = TAKE_PTR(c);
1952
1953 return from_scratch;
1954}
1955
1956static void context_unload_partition_table(Context *context) {
e594a3b1
LP
1957 assert(context);
1958
80a226b2 1959 LIST_FOREACH(partitions, p, context->partitions) {
e594a3b1
LP
1960
1961 /* Entirely remove partitions that have no configuration */
1962 if (PARTITION_IS_FOREIGN(p)) {
1963 partition_unlink_and_free(context, p);
1964 continue;
1965 }
1966
1967 /* Otherwise drop all data we read off the block device and everything we might have
1968 * calculated based on it */
1969
1970 p->dropped = false;
1971 p->current_size = UINT64_MAX;
1972 p->new_size = UINT64_MAX;
1973 p->current_padding = UINT64_MAX;
1974 p->new_padding = UINT64_MAX;
1975 p->partno = UINT64_MAX;
1976 p->offset = UINT64_MAX;
1977
1978 if (p->current_partition) {
1979 fdisk_unref_partition(p->current_partition);
1980 p->current_partition = NULL;
1981 }
1982
1983 if (p->new_partition) {
1984 fdisk_unref_partition(p->new_partition);
1985 p->new_partition = NULL;
1986 }
1987
1988 p->padding_area = NULL;
1989 p->allocated_to_area = NULL;
1990
15d43e30
LP
1991 p->current_uuid = SD_ID128_NULL;
1992 p->current_label = mfree(p->current_label);
e594a3b1
LP
1993 }
1994
1995 context->start = UINT64_MAX;
1996 context->end = UINT64_MAX;
1997 context->total = UINT64_MAX;
1998
1999 if (context->fdisk_context) {
2000 fdisk_unref_context(context->fdisk_context);
2001 context->fdisk_context = NULL;
2002 }
2003
2004 context_free_free_areas(context);
2005}
2006
2007static int format_size_change(uint64_t from, uint64_t to, char **ret) {
2b59bf51 2008 char *t;
e594a3b1
LP
2009
2010 if (from != UINT64_MAX) {
2011 if (from == to || to == UINT64_MAX)
2b59bf51 2012 t = strdup(FORMAT_BYTES(from));
e594a3b1 2013 else
fc03e80c 2014 t = strjoin(FORMAT_BYTES(from), " ", special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), " ", FORMAT_BYTES(to));
e594a3b1 2015 } else if (to != UINT64_MAX)
fc03e80c 2016 t = strjoin(special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), " ", FORMAT_BYTES(to));
e594a3b1
LP
2017 else {
2018 *ret = NULL;
2019 return 0;
2020 }
2021
2b59bf51 2022 if (!t)
e594a3b1
LP
2023 return log_oom();
2024
2b59bf51 2025 *ret = t;
e594a3b1
LP
2026 return 1;
2027}
2028
2029static const char *partition_label(const Partition *p) {
2030 assert(p);
2031
2032 if (p->new_label)
2033 return p->new_label;
2034
2035 if (p->current_label)
2036 return p->current_label;
2037
2038 return gpt_partition_type_uuid_to_string(p->type_uuid);
2039}
2040
2041static int context_dump_partitions(Context *context, const char *node) {
2042 _cleanup_(table_unrefp) Table *t = NULL;
2043 uint64_t sum_padding = 0, sum_size = 0;
e594a3b1 2044 int r;
39fc0174
RP
2045 const size_t dropin_files_col = 13;
2046 bool no_dropin_files = true;
e594a3b1 2047
6a01ea4a 2048 if ((arg_json_format_flags & JSON_FORMAT_OFF) && context->n_partitions == 0) {
a015fbe7
TH
2049 log_info("Empty partition table.");
2050 return 0;
2051 }
2052
39fc0174 2053 t = table_new("type", "label", "uuid", "file", "node", "offset", "old size", "raw size", "size", "old padding", "raw padding", "padding", "activity", "drop-in files");
e594a3b1
LP
2054 if (!t)
2055 return log_oom();
2056
a015fbe7 2057 if (!DEBUG_LOGGING) {
6a01ea4a 2058 if (arg_json_format_flags & JSON_FORMAT_OFF)
a015fbe7 2059 (void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
39fc0174 2060 (size_t) 8, (size_t) 11, dropin_files_col);
a015fbe7
TH
2061 else
2062 (void) table_set_display(t, (size_t) 0, (size_t) 1, (size_t) 2, (size_t) 3, (size_t) 4,
39fc0174
RP
2063 (size_t) 5, (size_t) 6, (size_t) 7, (size_t) 9, (size_t) 10, (size_t) 12,
2064 dropin_files_col);
a015fbe7 2065 }
e594a3b1 2066
e594a3b1 2067 (void) table_set_align_percent(t, table_get_cell(t, 0, 5), 100);
9c07c9ec
LP
2068 (void) table_set_align_percent(t, table_get_cell(t, 0, 6), 100);
2069 (void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
2070 (void) table_set_align_percent(t, table_get_cell(t, 0, 8), 100);
2071 (void) table_set_align_percent(t, table_get_cell(t, 0, 9), 100);
2072 (void) table_set_align_percent(t, table_get_cell(t, 0, 10), 100);
2073 (void) table_set_align_percent(t, table_get_cell(t, 0, 11), 100);
e594a3b1
LP
2074
2075 LIST_FOREACH(partitions, p, context->partitions) {
2076 _cleanup_free_ char *size_change = NULL, *padding_change = NULL, *partname = NULL;
b7416360 2077 char uuid_buffer[SD_ID128_UUID_STRING_MAX];
a015fbe7 2078 const char *label, *activity = NULL;
e594a3b1
LP
2079
2080 if (p->dropped)
2081 continue;
2082
a015fbe7
TH
2083 if (p->current_size == UINT64_MAX)
2084 activity = "create";
2085 else if (p->current_size != p->new_size)
2086 activity = "resize";
2087
e594a3b1
LP
2088 label = partition_label(p);
2089 partname = p->partno != UINT64_MAX ? fdisk_partname(node, p->partno+1) : NULL;
2090
2091 r = format_size_change(p->current_size, p->new_size, &size_change);
2092 if (r < 0)
2093 return r;
2094
2095 r = format_size_change(p->current_padding, p->new_padding, &padding_change);
2096 if (r < 0)
2097 return r;
2098
2099 if (p->new_size != UINT64_MAX)
2100 sum_size += p->new_size;
2101 if (p->new_padding != UINT64_MAX)
2102 sum_padding += p->new_padding;
2103
2104 r = table_add_many(
2105 t,
2106 TABLE_STRING, gpt_partition_type_uuid_to_string_harder(p->type_uuid, uuid_buffer),
be9ce018 2107 TABLE_STRING, empty_to_null(label) ?: "-", TABLE_SET_COLOR, empty_to_null(label) ? NULL : ansi_grey(),
11749b61 2108 TABLE_UUID, p->new_uuid_is_set ? p->new_uuid : p->current_uuid,
e594a3b1 2109 TABLE_STRING, p->definition_path ? basename(p->definition_path) : "-", TABLE_SET_COLOR, p->definition_path ? NULL : ansi_grey(),
a015fbe7 2110 TABLE_STRING, partname ?: "-", TABLE_SET_COLOR, partname ? NULL : ansi_highlight(),
e594a3b1 2111 TABLE_UINT64, p->offset,
a015fbe7 2112 TABLE_UINT64, p->current_size == UINT64_MAX ? 0 : p->current_size,
e594a3b1
LP
2113 TABLE_UINT64, p->new_size,
2114 TABLE_STRING, size_change, TABLE_SET_COLOR, !p->partitions_next && sum_size > 0 ? ansi_underline() : NULL,
a015fbe7 2115 TABLE_UINT64, p->current_padding == UINT64_MAX ? 0 : p->current_padding,
e594a3b1 2116 TABLE_UINT64, p->new_padding,
a015fbe7 2117 TABLE_STRING, padding_change, TABLE_SET_COLOR, !p->partitions_next && sum_padding > 0 ? ansi_underline() : NULL,
39fc0174
RP
2118 TABLE_STRING, activity ?: "unchanged",
2119 TABLE_STRV, p->drop_in_files);
e594a3b1 2120 if (r < 0)
f987a261 2121 return table_log_add_error(r);
39fc0174
RP
2122
2123 no_dropin_files = no_dropin_files && strv_isempty(p->drop_in_files);
e594a3b1
LP
2124 }
2125
6a01ea4a 2126 if ((arg_json_format_flags & JSON_FORMAT_OFF) && (sum_padding > 0 || sum_size > 0)) {
e594a3b1
LP
2127 const char *a, *b;
2128
2b59bf51
ZJS
2129 a = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_size));
2130 b = strjoina(special_glyph(SPECIAL_GLYPH_SIGMA), " = ", FORMAT_BYTES(sum_padding));
e594a3b1
LP
2131
2132 r = table_add_many(
2133 t,
2134 TABLE_EMPTY,
2135 TABLE_EMPTY,
2136 TABLE_EMPTY,
2137 TABLE_EMPTY,
2138 TABLE_EMPTY,
2139 TABLE_EMPTY,
2140 TABLE_EMPTY,
a015fbe7 2141 TABLE_EMPTY,
e594a3b1
LP
2142 TABLE_STRING, a,
2143 TABLE_EMPTY,
a015fbe7
TH
2144 TABLE_EMPTY,
2145 TABLE_STRING, b,
39fc0174 2146 TABLE_EMPTY,
a015fbe7 2147 TABLE_EMPTY);
e594a3b1 2148 if (r < 0)
f987a261 2149 return table_log_add_error(r);
e594a3b1
LP
2150 }
2151
39fc0174
RP
2152 if (no_dropin_files) {
2153 r = table_hide_column_from_display(t, dropin_files_col);
2154 if (r < 0)
2155 return log_error_errno(r, "Failed to set columns to display: %m");
2156 }
2157
896e678b 2158 return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
e594a3b1
LP
2159}
2160
2161static void context_bar_char_process_partition(
2162 Context *context,
2163 Partition *bar[],
2164 size_t n,
2165 Partition *p,
2166 size_t *ret_start) {
2167
2168 uint64_t from, to, total;
2169 size_t x, y;
2170
2171 assert(context);
2172 assert(bar);
2173 assert(n > 0);
2174 assert(p);
2175
2176 if (p->dropped)
2177 return;
2178
2179 assert(p->offset != UINT64_MAX);
2180 assert(p->new_size != UINT64_MAX);
2181
2182 from = p->offset;
2183 to = from + p->new_size;
2184
d8daed09
TY
2185 assert(context->total > 0);
2186 total = context->total;
e594a3b1 2187
d8daed09
TY
2188 assert(from <= total);
2189 x = from * n / total;
e594a3b1 2190
d8daed09
TY
2191 assert(to <= total);
2192 y = to * n / total;
e594a3b1
LP
2193
2194 assert(x <= y);
2195 assert(y <= n);
2196
2197 for (size_t i = x; i < y; i++)
2198 bar[i] = p;
2199
2200 *ret_start = x;
2201}
2202
2203static int partition_hint(const Partition *p, const char *node, char **ret) {
2204 _cleanup_free_ char *buf = NULL;
e594a3b1
LP
2205 const char *label;
2206 sd_id128_t id;
2207
2208 /* Tries really hard to find a suitable description for this partition */
2209
2210 if (p->definition_path) {
2211 buf = strdup(basename(p->definition_path));
2212 goto done;
2213 }
2214
2215 label = partition_label(p);
2216 if (!isempty(label)) {
2217 buf = strdup(label);
2218 goto done;
2219 }
2220
2221 if (p->partno != UINT64_MAX) {
2222 buf = fdisk_partname(node, p->partno+1);
2223 goto done;
2224 }
2225
11749b61 2226 if (p->new_uuid_is_set)
e594a3b1
LP
2227 id = p->new_uuid;
2228 else if (!sd_id128_is_null(p->current_uuid))
2229 id = p->current_uuid;
2230 else
2231 id = p->type_uuid;
2232
b7416360 2233 buf = strdup(SD_ID128_TO_UUID_STRING(id));
e594a3b1
LP
2234
2235done:
2236 if (!buf)
2237 return -ENOMEM;
2238
2239 *ret = TAKE_PTR(buf);
2240 return 0;
2241}
2242
2243static int context_dump_partition_bar(Context *context, const char *node) {
2244 _cleanup_free_ Partition **bar = NULL;
2245 _cleanup_free_ size_t *start_array = NULL;
03677889 2246 Partition *last = NULL;
e594a3b1
LP
2247 bool z = false;
2248 size_t c, j = 0;
2249
f391597c 2250 assert_se((c = columns()) >= 2);
e594a3b1
LP
2251 c -= 2; /* We do not use the leftmost and rightmost character cell */
2252
2253 bar = new0(Partition*, c);
2254 if (!bar)
2255 return log_oom();
2256
2257 start_array = new(size_t, context->n_partitions);
2258 if (!start_array)
2259 return log_oom();
2260
2261 LIST_FOREACH(partitions, p, context->partitions)
2262 context_bar_char_process_partition(context, bar, c, p, start_array + j++);
2263
2264 putc(' ', stdout);
2265
2266 for (size_t i = 0; i < c; i++) {
2267 if (bar[i]) {
2268 if (last != bar[i])
2269 z = !z;
2270
2271 fputs(z ? ansi_green() : ansi_yellow(), stdout);
2272 fputs(special_glyph(SPECIAL_GLYPH_DARK_SHADE), stdout);
2273 } else {
2274 fputs(ansi_normal(), stdout);
2275 fputs(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE), stdout);
2276 }
2277
2278 last = bar[i];
2279 }
2280
2281 fputs(ansi_normal(), stdout);
2282 putc('\n', stdout);
2283
2284 for (size_t i = 0; i < context->n_partitions; i++) {
2285 _cleanup_free_ char **line = NULL;
2286
2287 line = new0(char*, c);
2288 if (!line)
2289 return log_oom();
2290
2291 j = 0;
2292 LIST_FOREACH(partitions, p, context->partitions) {
2293 _cleanup_free_ char *d = NULL;
2294 j++;
2295
2296 if (i < context->n_partitions - j) {
2297
2298 if (line[start_array[j-1]]) {
2299 const char *e;
2300
2301 /* Upgrade final corner to the right with a branch to the right */
2302 e = startswith(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_RIGHT));
2303 if (e) {
2304 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), e);
2305 if (!d)
2306 return log_oom();
2307 }
2308 }
2309
2310 if (!d) {
2311 d = strdup(special_glyph(SPECIAL_GLYPH_TREE_VERTICAL));
2312 if (!d)
2313 return log_oom();
2314 }
2315
2316 } else if (i == context->n_partitions - j) {
2317 _cleanup_free_ char *hint = NULL;
2318
2319 (void) partition_hint(p, node, &hint);
2320
2321 if (streq_ptr(line[start_array[j-1]], special_glyph(SPECIAL_GLYPH_TREE_VERTICAL)))
2322 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_BRANCH), " ", strna(hint));
2323 else
2324 d = strjoin(special_glyph(SPECIAL_GLYPH_TREE_RIGHT), " ", strna(hint));
2325
2326 if (!d)
2327 return log_oom();
2328 }
2329
2330 if (d)
2331 free_and_replace(line[start_array[j-1]], d);
2332 }
2333
2334 putc(' ', stdout);
2335
2336 j = 0;
2337 while (j < c) {
2338 if (line[j]) {
2339 fputs(line[j], stdout);
2340 j += utf8_console_width(line[j]);
2341 } else {
2342 putc(' ', stdout);
2343 j++;
2344 }
2345 }
2346
2347 putc('\n', stdout);
2348
2349 for (j = 0; j < c; j++)
2350 free(line[j]);
2351 }
2352
2353 return 0;
2354}
2355
a26d463d
DDM
2356static int context_dump(Context *context, const char *node) {
2357 int r;
2358
2359 assert(context);
2360 assert(node);
2361
2362 if (arg_pretty == 0 && FLAGS_SET(arg_json_format_flags, JSON_FORMAT_OFF))
2363 return 0;
2364
2365 r = context_dump_partitions(context, node);
2366 if (r < 0)
2367 return r;
2368
2369 if (arg_json_format_flags & JSON_FORMAT_OFF) {
2370 putc('\n', stdout);
2371
2372 r = context_dump_partition_bar(context, node);
2373 if (r < 0)
2374 return r;
2375
2376 putc('\n', stdout);
2377 }
2378
2379 fflush(stdout);
2380
2381 return 0;
2382}
2383
2384
e594a3b1 2385static bool context_changed(const Context *context) {
03677889 2386 assert(context);
e594a3b1
LP
2387
2388 LIST_FOREACH(partitions, p, context->partitions) {
2389 if (p->dropped)
2390 continue;
2391
2392 if (p->allocated_to_area)
2393 return true;
2394
2395 if (p->new_size != p->current_size)
2396 return true;
2397 }
2398
2399 return false;
2400}
2401
81873a6b 2402static int context_wipe_range(Context *context, uint64_t offset, uint64_t size) {
e594a3b1
LP
2403 _cleanup_(blkid_free_probep) blkid_probe probe = NULL;
2404 int r;
2405
2406 assert(context);
81873a6b
LP
2407 assert(offset != UINT64_MAX);
2408 assert(size != UINT64_MAX);
e594a3b1
LP
2409
2410 probe = blkid_new_probe();
2411 if (!probe)
2412 return log_oom();
2413
e594a3b1 2414 errno = 0;
81873a6b 2415 r = blkid_probe_set_device(probe, fdisk_get_devfd(context->fdisk_context), offset, size);
e594a3b1 2416 if (r < 0)
81873a6b 2417 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to allocate device probe for wiping.");
e594a3b1
LP
2418
2419 errno = 0;
2420 if (blkid_probe_enable_superblocks(probe, true) < 0 ||
2421 blkid_probe_set_superblocks_flags(probe, BLKID_SUBLKS_MAGIC|BLKID_SUBLKS_BADCSUM) < 0 ||
2422 blkid_probe_enable_partitions(probe, true) < 0 ||
2423 blkid_probe_set_partitions_flags(probe, BLKID_PARTS_MAGIC) < 0)
81873a6b 2424 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to enable superblock and partition probing.");
e594a3b1
LP
2425
2426 for (;;) {
2427 errno = 0;
2428 r = blkid_do_probe(probe);
2429 if (r < 0)
2430 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to probe for file systems.");
2431 if (r > 0)
2432 break;
2433
2434 errno = 0;
2435 if (blkid_do_wipe(probe, false) < 0)
2436 return log_error_errno(errno ?: SYNTHETIC_ERRNO(EIO), "Failed to wipe file system signature.");
2437 }
2438
e594a3b1
LP
2439 return 0;
2440}
2441
81873a6b
LP
2442static int context_wipe_partition(Context *context, Partition *p) {
2443 int r;
2444
2445 assert(context);
2446 assert(p);
2447 assert(!PARTITION_EXISTS(p)); /* Safety check: never wipe existing partitions */
2448
2449 assert(p->offset != UINT64_MAX);
2450 assert(p->new_size != UINT64_MAX);
2451
2452 r = context_wipe_range(context, p->offset, p->new_size);
2453 if (r < 0)
2454 return r;
2455
2456 log_info("Successfully wiped file system signatures from future partition %" PRIu64 ".", p->partno);
2457 return 0;
2458}
2459
2460static int context_discard_range(
2461 Context *context,
2462 uint64_t offset,
2463 uint64_t size) {
2464
e594a3b1
LP
2465 struct stat st;
2466 int fd;
2467
2468 assert(context);
2469 assert(offset != UINT64_MAX);
2470 assert(size != UINT64_MAX);
2471
2472 if (size <= 0)
2473 return 0;
2474
a26f4a49 2475 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
e594a3b1
LP
2476
2477 if (fstat(fd, &st) < 0)
2478 return -errno;
2479
2480 if (S_ISREG(st.st_mode)) {
2481 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, offset, size) < 0) {
2482 if (ERRNO_IS_NOT_SUPPORTED(errno))
2483 return -EOPNOTSUPP;
2484
2485 return -errno;
2486 }
2487
2488 return 1;
2489 }
2490
2491 if (S_ISBLK(st.st_mode)) {
2492 uint64_t range[2], end;
2493
994b3031 2494 range[0] = round_up_size(offset, context->sector_size);
e594a3b1 2495
55d38014
LP
2496 if (offset > UINT64_MAX - size)
2497 return -ERANGE;
2498
e594a3b1
LP
2499 end = offset + size;
2500 if (end <= range[0])
2501 return 0;
2502
994b3031 2503 range[1] = round_down_size(end - range[0], context->sector_size);
e594a3b1
LP
2504 if (range[1] <= 0)
2505 return 0;
2506
2507 if (ioctl(fd, BLKDISCARD, range) < 0) {
2508 if (ERRNO_IS_NOT_SUPPORTED(errno))
2509 return -EOPNOTSUPP;
2510
2511 return -errno;
2512 }
2513
2514 return 1;
2515 }
2516
2517 return -EOPNOTSUPP;
2518}
2519
2520static int context_discard_partition(Context *context, Partition *p) {
2521 int r;
2522
2523 assert(context);
2524 assert(p);
2525
2526 assert(p->offset != UINT64_MAX);
2527 assert(p->new_size != UINT64_MAX);
2528 assert(!PARTITION_EXISTS(p)); /* Safety check: never discard existing partitions */
2529
2530 if (!arg_discard)
2531 return 0;
2532
2533 r = context_discard_range(context, p->offset, p->new_size);
2534 if (r == -EOPNOTSUPP) {
5b5109e2 2535 log_info("Storage does not support discard, not discarding data in future partition %" PRIu64 ".", p->partno);
e594a3b1
LP
2536 return 0;
2537 }
22163eb5
LP
2538 if (r == -EBUSY) {
2539 /* Let's handle this gracefully: https://bugzilla.kernel.org/show_bug.cgi?id=211167 */
2540 log_info("Block device is busy, not discarding partition %" PRIu64 " because it probably is mounted.", p->partno);
2541 return 0;
2542 }
e594a3b1
LP
2543 if (r == 0) {
2544 log_info("Partition %" PRIu64 " too short for discard, skipping.", p->partno);
2545 return 0;
2546 }
2547 if (r < 0)
5b5109e2 2548 return log_error_errno(r, "Failed to discard data for future partition %" PRIu64 ".", p->partno);
e594a3b1 2549
5b5109e2 2550 log_info("Successfully discarded data from future partition %" PRIu64 ".", p->partno);
e594a3b1
LP
2551 return 1;
2552}
2553
2554static int context_discard_gap_after(Context *context, Partition *p) {
2555 uint64_t gap, next = UINT64_MAX;
e594a3b1
LP
2556 int r;
2557
2558 assert(context);
2559 assert(!p || (p->offset != UINT64_MAX && p->new_size != UINT64_MAX));
2560
2561 if (p)
2562 gap = p->offset + p->new_size;
2563 else
2564 gap = context->start;
2565
2566 LIST_FOREACH(partitions, q, context->partitions) {
2567 if (q->dropped)
2568 continue;
2569
2570 assert(q->offset != UINT64_MAX);
2571 assert(q->new_size != UINT64_MAX);
2572
2573 if (q->offset < gap)
2574 continue;
2575
2576 if (next == UINT64_MAX || q->offset < next)
2577 next = q->offset;
2578 }
2579
2580 if (next == UINT64_MAX) {
2581 next = context->end;
2582 if (gap > next)
2583 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Partition end beyond disk end.");
2584 }
2585
2586 assert(next >= gap);
2587 r = context_discard_range(context, gap, next - gap);
2588 if (r == -EOPNOTSUPP) {
2589 if (p)
5b5109e2 2590 log_info("Storage does not support discard, not discarding gap after partition %" PRIu64 ".", p->partno);
e594a3b1 2591 else
5b5109e2 2592 log_info("Storage does not support discard, not discarding gap at beginning of disk.");
e594a3b1
LP
2593 return 0;
2594 }
2595 if (r == 0) /* Too short */
2596 return 0;
2597 if (r < 0) {
2598 if (p)
2599 return log_error_errno(r, "Failed to discard gap after partition %" PRIu64 ".", p->partno);
2600 else
2601 return log_error_errno(r, "Failed to discard gap at beginning of disk.");
2602 }
2603
2604 if (p)
2605 log_info("Successfully discarded gap after partition %" PRIu64 ".", p->partno);
2606 else
2607 log_info("Successfully discarded gap at beginning of disk.");
2608
2609 return 0;
2610}
2611
2612static int context_wipe_and_discard(Context *context, bool from_scratch) {
e594a3b1
LP
2613 int r;
2614
2615 assert(context);
2616
2617 /* Wipe and discard the contents of all partitions we are about to create. We skip the discarding if
2618 * we were supposed to start from scratch anyway, as in that case we just discard the whole block
2619 * device in one go early on. */
2620
2621 LIST_FOREACH(partitions, p, context->partitions) {
2622
2623 if (!p->allocated_to_area)
2624 continue;
2625
e594a3b1
LP
2626 r = context_wipe_partition(context, p);
2627 if (r < 0)
2628 return r;
2629
2630 if (!from_scratch) {
f0cb1b95
LP
2631 r = context_discard_partition(context, p);
2632 if (r < 0)
2633 return r;
2634
e594a3b1
LP
2635 r = context_discard_gap_after(context, p);
2636 if (r < 0)
2637 return r;
2638 }
2639 }
2640
2641 if (!from_scratch) {
2642 r = context_discard_gap_after(context, NULL);
2643 if (r < 0)
2644 return r;
2645 }
2646
2647 return 0;
2648}
2649
b9df3536 2650static int partition_encrypt(
994b3031 2651 Context *context,
b9df3536
LP
2652 Partition *p,
2653 const char *node,
2654 struct crypt_device **ret_cd,
2655 char **ret_volume,
2656 int *ret_fd) {
3dd8ae5c 2657#if HAVE_LIBCRYPTSETUP
0d12936d 2658 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
b9df3536
LP
2659 _cleanup_(erase_and_freep) void *volume_key = NULL;
2660 _cleanup_free_ char *dm_name = NULL, *vol = NULL;
b9df3536
LP
2661 size_t volume_key_size = 256 / 8;
2662 sd_id128_t uuid;
2663 int r;
2664
994b3031 2665 assert(context);
b9df3536 2666 assert(p);
889914ef
LP
2667 assert(p->encrypt != ENCRYPT_OFF);
2668
2669 log_debug("Encryption mode for partition %" PRIu64 ": %s", p->partno, encrypt_mode_to_string(p->encrypt));
b9df3536 2670
0d12936d
LP
2671 r = dlopen_cryptsetup();
2672 if (r < 0)
2673 return log_error_errno(r, "libcryptsetup not found, cannot encrypt: %m");
2674
b9df3536
LP
2675 if (asprintf(&dm_name, "luks-repart-%08" PRIx64, random_u64()) < 0)
2676 return log_oom();
2677
2678 if (ret_volume) {
2679 vol = path_join("/dev/mapper/", dm_name);
2680 if (!vol)
2681 return log_oom();
2682 }
2683
2684 r = derive_uuid(p->new_uuid, "luks-uuid", &uuid);
2685 if (r < 0)
2686 return r;
2687
2688 log_info("Encrypting future partition %" PRIu64 "...", p->partno);
2689
2690 volume_key = malloc(volume_key_size);
2691 if (!volume_key)
2692 return log_oom();
2693
87cb1ab6 2694 r = crypto_random_bytes(volume_key, volume_key_size);
b9df3536
LP
2695 if (r < 0)
2696 return log_error_errno(r, "Failed to generate volume key: %m");
2697
0d12936d 2698 r = sym_crypt_init(&cd, node);
b9df3536
LP
2699 if (r < 0)
2700 return log_error_errno(r, "Failed to allocate libcryptsetup context: %m");
2701
2702 cryptsetup_enable_logging(cd);
2703
0d12936d 2704 r = sym_crypt_format(cd,
b9df3536
LP
2705 CRYPT_LUKS2,
2706 "aes",
2707 "xts-plain64",
b7416360 2708 SD_ID128_TO_UUID_STRING(uuid),
b9df3536
LP
2709 volume_key,
2710 volume_key_size,
2711 &(struct crypt_params_luks2) {
be9ce018 2712 .label = strempty(p->new_label),
994b3031 2713 .sector_size = context->sector_size,
b9df3536
LP
2714 });
2715 if (r < 0)
2716 return log_error_errno(r, "Failed to LUKS2 format future partition: %m");
2717
889914ef
LP
2718 if (IN_SET(p->encrypt, ENCRYPT_KEY_FILE, ENCRYPT_KEY_FILE_TPM2)) {
2719 r = sym_crypt_keyslot_add_by_volume_key(
2720 cd,
2721 CRYPT_ANY_SLOT,
2722 volume_key,
2723 volume_key_size,
2724 strempty(arg_key),
2725 arg_key_size);
2726 if (r < 0)
2727 return log_error_errno(r, "Failed to add LUKS2 key: %m");
2728 }
2729
2730 if (IN_SET(p->encrypt, ENCRYPT_TPM2, ENCRYPT_KEY_FILE_TPM2)) {
2731#if HAVE_TPM2
2732 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
2733 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
2734 _cleanup_(erase_and_freep) void *secret = NULL;
2735 _cleanup_free_ void *blob = NULL, *hash = NULL;
2736 size_t secret_size, blob_size, hash_size;
2b92a672 2737 uint16_t pcr_bank, primary_alg;
889914ef
LP
2738 int keyslot;
2739
2f5a892a 2740 r = tpm2_seal(arg_tpm2_device, arg_tpm2_pcr_mask, NULL, &secret, &secret_size, &blob, &blob_size, &hash, &hash_size, &pcr_bank, &primary_alg);
889914ef
LP
2741 if (r < 0)
2742 return log_error_errno(r, "Failed to seal to TPM2: %m");
2743
2744 r = base64mem(secret, secret_size, &base64_encoded);
2745 if (r < 0)
2746 return log_error_errno(r, "Failed to base64 encode secret key: %m");
2747
2748 r = cryptsetup_set_minimal_pbkdf(cd);
2749 if (r < 0)
2750 return log_error_errno(r, "Failed to set minimal PBKDF: %m");
2751
2752 keyslot = sym_crypt_keyslot_add_by_volume_key(
2753 cd,
2754 CRYPT_ANY_SLOT,
2755 volume_key,
2756 volume_key_size,
2757 base64_encoded,
2758 strlen(base64_encoded));
2759 if (keyslot < 0)
2760 return log_error_errno(keyslot, "Failed to add new TPM2 key to %s: %m", node);
2761
6c7a1681 2762 r = tpm2_make_luks2_json(keyslot, arg_tpm2_pcr_mask, pcr_bank, primary_alg, blob, blob_size, hash, hash_size, 0, &v);
889914ef
LP
2763 if (r < 0)
2764 return log_error_errno(r, "Failed to prepare TPM2 JSON token object: %m");
2765
2766 r = cryptsetup_add_token_json(cd, v);
2767 if (r < 0)
2768 return log_error_errno(r, "Failed to add TPM2 JSON token to LUKS2 header: %m");
2769#else
2770 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
2771 "Support for TPM2 enrollment not enabled.");
2772#endif
2773 }
b9df3536 2774
0d12936d 2775 r = sym_crypt_activate_by_volume_key(
b9df3536
LP
2776 cd,
2777 dm_name,
2778 volume_key,
2779 volume_key_size,
2780 arg_discard ? CRYPT_ACTIVATE_ALLOW_DISCARDS : 0);
2781 if (r < 0)
2782 return log_error_errno(r, "Failed to activate LUKS superblock: %m");
2783
2784 log_info("Successfully encrypted future partition %" PRIu64 ".", p->partno);
2785
2786 if (ret_fd) {
2787 _cleanup_close_ int dev_fd = -1;
2788
2789 dev_fd = open(vol, O_RDWR|O_CLOEXEC|O_NOCTTY);
2790 if (dev_fd < 0)
2791 return log_error_errno(errno, "Failed to open LUKS volume '%s': %m", vol);
2792
2793 *ret_fd = TAKE_FD(dev_fd);
2794 }
2795
2796 if (ret_cd)
2797 *ret_cd = TAKE_PTR(cd);
2798 if (ret_volume)
2799 *ret_volume = TAKE_PTR(vol);
2800
2801 return 0;
3dd8ae5c 2802#else
2803 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libcryptsetup is not supported, cannot encrypt: %m");
2804#endif
b9df3536
LP
2805}
2806
2807static int deactivate_luks(struct crypt_device *cd, const char *node) {
3dd8ae5c 2808#if HAVE_LIBCRYPTSETUP
b9df3536
LP
2809 int r;
2810
2811 if (!cd)
2812 return 0;
2813
2814 assert(node);
2815
2816 /* udev or so might access out block device in the background while we are done. Let's hence force
2817 * detach the volume. We sync'ed before, hence this should be safe. */
2818
0d12936d 2819 r = sym_crypt_deactivate_by_name(cd, basename(node), CRYPT_DEACTIVATE_FORCE);
b9df3536
LP
2820 if (r < 0)
2821 return log_error_errno(r, "Failed to deactivate LUKS device: %m");
2822
2823 return 1;
3dd8ae5c 2824#else
2825 return 0;
2826#endif
b9df3536
LP
2827}
2828
757bc2e4 2829static int context_copy_blocks(Context *context) {
b9df3536 2830 int whole_fd = -1, r;
757bc2e4
LP
2831
2832 assert(context);
2833
2834 /* Copy in file systems on the block level */
2835
2836 LIST_FOREACH(partitions, p, context->partitions) {
0d12936d 2837 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
b9df3536
LP
2838 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
2839 _cleanup_free_ char *encrypted = NULL;
2840 _cleanup_close_ int encrypted_dev_fd = -1;
b9df3536 2841 int target_fd;
757bc2e4
LP
2842
2843 if (p->copy_blocks_fd < 0)
2844 continue;
2845
2846 if (p->dropped)
2847 continue;
2848
2849 if (PARTITION_EXISTS(p)) /* Never copy over existing partitions */
2850 continue;
2851
2852 assert(p->new_size != UINT64_MAX);
2853 assert(p->copy_blocks_size != UINT64_MAX);
2854 assert(p->new_size >= p->copy_blocks_size);
2855
b9df3536
LP
2856 if (whole_fd < 0)
2857 assert_se((whole_fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
2858
889914ef 2859 if (p->encrypt != ENCRYPT_OFF) {
7f52206a 2860 r = loop_device_make(whole_fd, O_RDWR, p->offset, p->new_size, 0, LOCK_EX, &d);
b9df3536
LP
2861 if (r < 0)
2862 return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno);
2863
994b3031 2864 r = partition_encrypt(context, p, d->node, &cd, &encrypted, &encrypted_dev_fd);
b9df3536
LP
2865 if (r < 0)
2866 return log_error_errno(r, "Failed to encrypt device: %m");
757bc2e4 2867
b9df3536
LP
2868 if (flock(encrypted_dev_fd, LOCK_EX) < 0)
2869 return log_error_errno(errno, "Failed to lock LUKS device: %m");
2870
2871 target_fd = encrypted_dev_fd;
695cfd53 2872 } else {
b9df3536
LP
2873 if (lseek(whole_fd, p->offset, SEEK_SET) == (off_t) -1)
2874 return log_error_errno(errno, "Failed to seek to partition offset: %m");
2875
2876 target_fd = whole_fd;
2877 }
757bc2e4 2878
2b59bf51
ZJS
2879 log_info("Copying in '%s' (%s) on block level into future partition %" PRIu64 ".",
2880 p->copy_blocks_path, FORMAT_BYTES(p->copy_blocks_size), p->partno);
757bc2e4 2881
b9df3536 2882 r = copy_bytes_full(p->copy_blocks_fd, target_fd, p->copy_blocks_size, 0, NULL, NULL, NULL, NULL);
757bc2e4
LP
2883 if (r < 0)
2884 return log_error_errno(r, "Failed to copy in data from '%s': %m", p->copy_blocks_path);
2885
b9df3536 2886 if (fsync(target_fd) < 0)
8ac04a65 2887 return log_error_errno(errno, "Failed to synchronize copied data blocks: %m");
b9df3536 2888
889914ef 2889 if (p->encrypt != ENCRYPT_OFF) {
b9df3536
LP
2890 encrypted_dev_fd = safe_close(encrypted_dev_fd);
2891
2892 r = deactivate_luks(cd, encrypted);
2893 if (r < 0)
2894 return r;
2895
0d12936d 2896 sym_crypt_free(cd);
b9df3536
LP
2897 cd = NULL;
2898
2899 r = loop_device_sync(d);
2900 if (r < 0)
2901 return log_error_errno(r, "Failed to sync loopback device: %m");
2902 }
2903
757bc2e4
LP
2904 log_info("Copying in of '%s' on block level completed.", p->copy_blocks_path);
2905 }
2906
2907 return 0;
2908}
2909
8a794850 2910static int do_copy_files(Partition *p, const char *fs) {
8a794850
LP
2911 int r;
2912
2913 assert(p);
2914 assert(fs);
2915
2916 STRV_FOREACH_PAIR(source, target, p->copy_files) {
2917 _cleanup_close_ int sfd = -1, pfd = -1, tfd = -1;
8a794850
LP
2918
2919 sfd = chase_symlinks_and_open(*source, arg_root, CHASE_PREFIX_ROOT|CHASE_WARN, O_CLOEXEC|O_NOCTTY, NULL);
2920 if (sfd < 0)
2921 return log_error_errno(sfd, "Failed to open source file '%s%s': %m", strempty(arg_root), *source);
2922
2923 r = fd_verify_regular(sfd);
2924 if (r < 0) {
2925 if (r != -EISDIR)
2926 return log_error_errno(r, "Failed to check type of source file '%s': %m", *source);
2927
2928 /* We are looking at a directory */
2929 tfd = chase_symlinks_and_open(*target, fs, CHASE_PREFIX_ROOT|CHASE_WARN, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
2930 if (tfd < 0) {
f21a3a82
LP
2931 _cleanup_free_ char *dn = NULL, *fn = NULL;
2932
8a794850
LP
2933 if (tfd != -ENOENT)
2934 return log_error_errno(tfd, "Failed to open target directory '%s': %m", *target);
2935
f21a3a82
LP
2936 r = path_extract_filename(*target, &fn);
2937 if (r < 0)
2938 return log_error_errno(r, "Failed to extract filename from '%s': %m", *target);
2939
2940 r = path_extract_directory(*target, &dn);
2941 if (r < 0)
2942 return log_error_errno(r, "Failed to extract directory from '%s': %m", *target);
2943
8a794850
LP
2944 r = mkdir_p_root(fs, dn, UID_INVALID, GID_INVALID, 0755);
2945 if (r < 0)
2946 return log_error_errno(r, "Failed to create parent directory '%s': %m", dn);
2947
2948 pfd = chase_symlinks_and_open(dn, fs, CHASE_PREFIX_ROOT|CHASE_WARN, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
2949 if (pfd < 0)
2950 return log_error_errno(pfd, "Failed to open parent directory of target: %m");
2951
652d9040
LP
2952 r = copy_tree_at(
2953 sfd, ".",
6020d00d 2954 pfd, fn,
652d9040 2955 UID_INVALID, GID_INVALID,
23e026de 2956 COPY_REFLINK|COPY_MERGE|COPY_REPLACE|COPY_SIGINT|COPY_HARDLINKS|COPY_ALL_XATTRS);
8a794850 2957 } else
652d9040
LP
2958 r = copy_tree_at(
2959 sfd, ".",
2960 tfd, ".",
2961 UID_INVALID, GID_INVALID,
23e026de 2962 COPY_REFLINK|COPY_MERGE|COPY_REPLACE|COPY_SIGINT|COPY_HARDLINKS|COPY_ALL_XATTRS);
8a794850 2963 if (r < 0)
554a2b64 2964 return log_error_errno(r, "Failed to copy '%s' to '%s%s': %m", *source, strempty(arg_root), *target);
8a794850 2965 } else {
f21a3a82
LP
2966 _cleanup_free_ char *dn = NULL, *fn = NULL;
2967
8a794850
LP
2968 /* We are looking at a regular file */
2969
f21a3a82
LP
2970 r = path_extract_filename(*target, &fn);
2971 if (r == -EADDRNOTAVAIL || r == O_DIRECTORY)
2972 return log_error_errno(SYNTHETIC_ERRNO(EISDIR),
2973 "Target path '%s' refers to a directory, but source path '%s' refers to regular file, can't copy.", *target, *source);
2974 if (r < 0)
2975 return log_error_errno(r, "Failed to extract filename from '%s': %m", *target);
2976
2977 r = path_extract_directory(*target, &dn);
2978 if (r < 0)
2979 return log_error_errno(r, "Failed to extract directory from '%s': %m", *target);
2980
8a794850
LP
2981 r = mkdir_p_root(fs, dn, UID_INVALID, GID_INVALID, 0755);
2982 if (r < 0)
2983 return log_error_errno(r, "Failed to create parent directory: %m");
2984
2985 pfd = chase_symlinks_and_open(dn, fs, CHASE_PREFIX_ROOT|CHASE_WARN, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
2986 if (pfd < 0)
a0ff9971 2987 return log_error_errno(pfd, "Failed to open parent directory of target: %m");
8a794850 2988
e2819067 2989 tfd = openat(pfd, fn, O_CREAT|O_EXCL|O_WRONLY|O_CLOEXEC, 0700);
8a794850
LP
2990 if (tfd < 0)
2991 return log_error_errno(errno, "Failed to create target file '%s': %m", *target);
2992
2993 r = copy_bytes(sfd, tfd, UINT64_MAX, COPY_REFLINK|COPY_SIGINT);
2994 if (r < 0)
554a2b64 2995 return log_error_errno(r, "Failed to copy '%s' to '%s%s': %m", *source, strempty(arg_root), *target);
8a794850 2996
23e026de 2997 (void) copy_xattr(sfd, tfd, COPY_ALL_XATTRS);
8a794850
LP
2998 (void) copy_access(sfd, tfd);
2999 (void) copy_times(sfd, tfd, 0);
3000 }
3001 }
3002
3003 return 0;
3004}
3005
d83d8048 3006static int do_make_directories(Partition *p, const char *fs) {
d83d8048
LP
3007 int r;
3008
3009 assert(p);
3010 assert(fs);
3011
3012 STRV_FOREACH(d, p->make_directories) {
3013
3014 r = mkdir_p_root(fs, *d, UID_INVALID, GID_INVALID, 0755);
3015 if (r < 0)
3016 return log_error_errno(r, "Failed to create directory '%s' in file system: %m", *d);
3017 }
3018
3019 return 0;
3020}
3021
3022static int partition_populate(Partition *p, const char *node) {
8a794850
LP
3023 int r;
3024
3025 assert(p);
3026 assert(node);
3027
d83d8048 3028 if (strv_isempty(p->copy_files) && strv_isempty(p->make_directories))
8a794850
LP
3029 return 0;
3030
3031 log_info("Populating partition %" PRIu64 " with files.", p->partno);
3032
3033 /* We copy in a child process, since we have to mount the fs for that, and we don't want that fs to
3034 * appear in the host namespace. Hence we fork a child that has its own file system namespace and
3035 * detached mount propagation. */
3036
3037 r = safe_fork("(sd-copy)", FORK_DEATHSIG|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, NULL);
3038 if (r < 0)
3039 return r;
3040 if (r == 0) {
3041 static const char fs[] = "/run/systemd/mount-root";
3042 /* This is a child process with its own mount namespace and propagation to host turned off */
3043
3044 r = mkdir_p(fs, 0700);
3045 if (r < 0) {
3046 log_error_errno(r, "Failed to create mount point: %m");
3047 _exit(EXIT_FAILURE);
3048 }
3049
511a8cfe 3050 if (mount_nofollow_verbose(LOG_ERR, node, fs, p->format, MS_NOATIME|MS_NODEV|MS_NOEXEC|MS_NOSUID, NULL) < 0)
8a794850
LP
3051 _exit(EXIT_FAILURE);
3052
3053 if (do_copy_files(p, fs) < 0)
3054 _exit(EXIT_FAILURE);
3055
d83d8048
LP
3056 if (do_make_directories(p, fs) < 0)
3057 _exit(EXIT_FAILURE);
3058
8a794850
LP
3059 r = syncfs_path(AT_FDCWD, fs);
3060 if (r < 0) {
3061 log_error_errno(r, "Failed to synchronize written files: %m");
3062 _exit(EXIT_FAILURE);
3063 }
3064
3065 _exit(EXIT_SUCCESS);
3066 }
3067
3068 log_info("Successfully populated partition %" PRIu64 " with files.", p->partno);
3069 return 0;
3070}
3071
53171c04 3072static int context_mkfs(Context *context) {
53171c04
LP
3073 int fd = -1, r;
3074
3075 assert(context);
3076
3077 /* Make a file system */
3078
3079 LIST_FOREACH(partitions, p, context->partitions) {
0d12936d 3080 _cleanup_(sym_crypt_freep) struct crypt_device *cd = NULL;
53171c04 3081 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
b9df3536
LP
3082 _cleanup_free_ char *encrypted = NULL;
3083 _cleanup_close_ int encrypted_dev_fd = -1;
3084 const char *fsdev;
53171c04
LP
3085 sd_id128_t fs_uuid;
3086
3087 if (p->dropped)
3088 continue;
3089
3090 if (PARTITION_EXISTS(p)) /* Never format existing partitions */
3091 continue;
3092
3093 if (!p->format)
3094 continue;
3095
3096 assert(p->offset != UINT64_MAX);
3097 assert(p->new_size != UINT64_MAX);
3098
3099 if (fd < 0)
3100 assert_se((fd = fdisk_get_devfd(context->fdisk_context)) >= 0);
3101
3102 /* Loopback block devices are not only useful to turn regular files into block devices, but
3103 * also to cut out sections of block devices into new block devices. */
3104
7f52206a 3105 r = loop_device_make(fd, O_RDWR, p->offset, p->new_size, 0, LOCK_EX, &d);
53171c04 3106 if (r < 0)
5b5109e2 3107 return log_error_errno(r, "Failed to make loopback device of future partition %" PRIu64 ": %m", p->partno);
53171c04 3108
889914ef 3109 if (p->encrypt != ENCRYPT_OFF) {
994b3031 3110 r = partition_encrypt(context, p, d->node, &cd, &encrypted, &encrypted_dev_fd);
b9df3536
LP
3111 if (r < 0)
3112 return log_error_errno(r, "Failed to encrypt device: %m");
3113
3114 if (flock(encrypted_dev_fd, LOCK_EX) < 0)
3115 return log_error_errno(errno, "Failed to lock LUKS device: %m");
3116
3117 fsdev = encrypted;
3118 } else
3119 fsdev = d->node;
3120
53171c04
LP
3121 log_info("Formatting future partition %" PRIu64 ".", p->partno);
3122
3123 /* Calculate the UUID for the file system as HMAC-SHA256 of the string "file-system-uuid",
3124 * keyed off the partition UUID. */
3125 r = derive_uuid(p->new_uuid, "file-system-uuid", &fs_uuid);
3126 if (r < 0)
3127 return r;
3128
be9ce018 3129 r = make_filesystem(fsdev, p->format, strempty(p->new_label), fs_uuid, arg_discard);
b9df3536
LP
3130 if (r < 0) {
3131 encrypted_dev_fd = safe_close(encrypted_dev_fd);
3132 (void) deactivate_luks(cd, encrypted);
53171c04 3133 return r;
b9df3536 3134 }
53171c04
LP
3135
3136 log_info("Successfully formatted future partition %" PRIu64 ".", p->partno);
3137
b9df3536 3138 /* The file system is now created, no need to delay udev further */
889914ef 3139 if (p->encrypt != ENCRYPT_OFF)
b9df3536
LP
3140 if (flock(encrypted_dev_fd, LOCK_UN) < 0)
3141 return log_error_errno(errno, "Failed to unlock LUKS device: %m");
3142
d83d8048 3143 r = partition_populate(p, fsdev);
b9df3536
LP
3144 if (r < 0) {
3145 encrypted_dev_fd = safe_close(encrypted_dev_fd);
3146 (void) deactivate_luks(cd, encrypted);
8a794850 3147 return r;
b9df3536
LP
3148 }
3149
3150 /* Note that we always sync explicitly here, since mkfs.fat doesn't do that on its own, and
3151 * if we don't sync before detaching a block device the in-flight sectors possibly won't hit
3152 * the disk. */
3153
889914ef 3154 if (p->encrypt != ENCRYPT_OFF) {
b9df3536 3155 if (fsync(encrypted_dev_fd) < 0)
8ac04a65 3156 return log_error_errno(errno, "Failed to synchronize LUKS volume: %m");
b9df3536
LP
3157 encrypted_dev_fd = safe_close(encrypted_dev_fd);
3158
3159 r = deactivate_luks(cd, encrypted);
3160 if (r < 0)
3161 return r;
3162
0d12936d 3163 sym_crypt_free(cd);
b9df3536
LP
3164 cd = NULL;
3165 }
8a794850 3166
53171c04
LP
3167 r = loop_device_sync(d);
3168 if (r < 0)
3169 return log_error_errno(r, "Failed to sync loopback device: %m");
3170 }
3171
3172 return 0;
3173}
3174
e594a3b1
LP
3175static int partition_acquire_uuid(Context *context, Partition *p, sd_id128_t *ret) {
3176 struct {
3177 sd_id128_t type_uuid;
3178 uint64_t counter;
695cfd53 3179 } _packed_ plaintext = {};
e594a3b1 3180 union {
ade99252 3181 uint8_t md[SHA256_DIGEST_SIZE];
e594a3b1
LP
3182 sd_id128_t id;
3183 } result;
3184
3185 uint64_t k = 0;
e594a3b1
LP
3186 int r;
3187
3188 assert(context);
3189 assert(p);
3190 assert(ret);
3191
3192 /* Calculate a good UUID for the indicated partition. We want a certain degree of reproducibility,
3193 * hence we won't generate the UUIDs randomly. Instead we use a cryptographic hash (precisely:
3194 * HMAC-SHA256) to derive them from a single seed. The seed is generally the machine ID of the
3195 * installation we are processing, but if random behaviour is desired can be random, too. We use the
3196 * seed value as key for the HMAC (since the machine ID is something we generally don't want to leak)
3197 * and the partition type as plaintext. The partition type is suffixed with a counter (only for the
3198 * second and later partition of the same type) if we have more than one partition of the same
3199 * time. Or in other words:
3200 *
3201 * With:
3202 * SEED := /etc/machine-id
3203 *
3204 * If first partition instance of type TYPE_UUID:
3205 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID)
3206 *
3207 * For all later partition instances of type TYPE_UUID with INSTANCE being the LE64 encoded instance number:
3208 * PARTITION_UUID := HMAC-SHA256(SEED, TYPE_UUID || INSTANCE)
3209 */
3210
3211 LIST_FOREACH(partitions, q, context->partitions) {
3212 if (p == q)
3213 break;
3214
3215 if (!sd_id128_equal(p->type_uuid, q->type_uuid))
3216 continue;
3217
3218 k++;
3219 }
3220
3221 plaintext.type_uuid = p->type_uuid;
3222 plaintext.counter = htole64(k);
3223
ade99252
KK
3224 hmac_sha256(context->seed.bytes, sizeof(context->seed.bytes),
3225 &plaintext,
3226 k == 0 ? sizeof(sd_id128_t) : sizeof(plaintext),
3227 result.md);
e594a3b1
LP
3228
3229 /* Take the first half, mark it as v4 UUID */
3230 assert_cc(sizeof(result.md) == sizeof(result.id) * 2);
3231 result.id = id128_make_v4_uuid(result.id);
3232
3233 /* Ensure this partition UUID is actually unique, and there's no remaining partition from an earlier run? */
3234 LIST_FOREACH(partitions, q, context->partitions) {
3235 if (p == q)
3236 continue;
3237
580f48cc 3238 if (sd_id128_in_set(result.id, q->current_uuid, q->new_uuid)) {
da1af43d 3239 log_warning("Partition UUID calculated from seed for partition %" PRIu64 " already used, reverting to randomized UUID.", p->partno);
e594a3b1
LP
3240
3241 r = sd_id128_randomize(&result.id);
3242 if (r < 0)
3243 return log_error_errno(r, "Failed to generate randomized UUID: %m");
3244
3245 break;
3246 }
3247 }
3248
3249 *ret = result.id;
3250 return 0;
3251}
3252
3253static int partition_acquire_label(Context *context, Partition *p, char **ret) {
3254 _cleanup_free_ char *label = NULL;
3255 const char *prefix;
3256 unsigned k = 1;
3257
3258 assert(context);
3259 assert(p);
3260 assert(ret);
3261
3262 prefix = gpt_partition_type_uuid_to_string(p->type_uuid);
3263 if (!prefix)
3264 prefix = "linux";
3265
3266 for (;;) {
3267 const char *ll = label ?: prefix;
3268 bool retry = false;
e594a3b1
LP
3269
3270 LIST_FOREACH(partitions, q, context->partitions) {
3271 if (p == q)
3272 break;
3273
3274 if (streq_ptr(ll, q->current_label) ||
3275 streq_ptr(ll, q->new_label)) {
3276 retry = true;
3277 break;
3278 }
3279 }
3280
3281 if (!retry)
3282 break;
3283
3284 label = mfree(label);
e594a3b1
LP
3285 if (asprintf(&label, "%s-%u", prefix, ++k) < 0)
3286 return log_oom();
3287 }
3288
3289 if (!label) {
3290 label = strdup(prefix);
3291 if (!label)
3292 return log_oom();
3293 }
3294
3295 *ret = TAKE_PTR(label);
3296 return 0;
3297}
3298
3299static int context_acquire_partition_uuids_and_labels(Context *context) {
e594a3b1
LP
3300 int r;
3301
3302 assert(context);
3303
3304 LIST_FOREACH(partitions, p, context->partitions) {
e594a3b1
LP
3305 /* Never touch foreign partitions */
3306 if (PARTITION_IS_FOREIGN(p)) {
3307 p->new_uuid = p->current_uuid;
3308
3309 if (p->current_label) {
78eee6ce
LP
3310 r = free_and_strdup_warn(&p->new_label, strempty(p->current_label));
3311 if (r < 0)
3312 return r;
e594a3b1
LP
3313 }
3314
3315 continue;
3316 }
3317
3318 if (!sd_id128_is_null(p->current_uuid))
3319 p->new_uuid = p->current_uuid; /* Never change initialized UUIDs */
11749b61 3320 else if (!p->new_uuid_is_set) {
12963533 3321 /* Not explicitly set by user! */
e594a3b1
LP
3322 r = partition_acquire_uuid(context, p, &p->new_uuid);
3323 if (r < 0)
3324 return r;
11749b61
DDM
3325
3326 p->new_uuid_is_set = true;
e594a3b1
LP
3327 }
3328
3329 if (!isempty(p->current_label)) {
78eee6ce
LP
3330 /* never change initialized labels */
3331 r = free_and_strdup_warn(&p->new_label, p->current_label);
3332 if (r < 0)
3333 return r;
12963533
TH
3334 } else if (!p->new_label) {
3335 /* Not explicitly set by user! */
3336
e594a3b1
LP
3337 r = partition_acquire_label(context, p, &p->new_label);
3338 if (r < 0)
3339 return r;
3340 }
3341 }
3342
3343 return 0;
3344}
3345
e73309c5
LP
3346static int set_gpt_flags(struct fdisk_partition *q, uint64_t flags) {
3347 _cleanup_free_ char *a = NULL;
3348
3349 for (unsigned i = 0; i < sizeof(flags) * 8; i++) {
3350 uint64_t bit = UINT64_C(1) << i;
3351 char buf[DECIMAL_STR_MAX(unsigned)+1];
3352
3353 if (!FLAGS_SET(flags, bit))
3354 continue;
3355
3356 xsprintf(buf, "%u", i);
3357 if (!strextend_with_separator(&a, ",", buf))
3358 return -ENOMEM;
3359 }
3360
3361 return fdisk_partition_set_attrs(q, a);
3362}
3363
1c41c1dc
LP
3364static uint64_t partition_merge_flags(Partition *p) {
3365 uint64_t f;
3366
3367 assert(p);
3368
3369 f = p->gpt_flags;
3370
ff0771bf
LP
3371 if (p->no_auto >= 0) {
3372 if (gpt_partition_type_knows_no_auto(p->type_uuid))
3373 SET_FLAG(f, GPT_FLAG_NO_AUTO, p->no_auto);
3374 else {
b7416360 3375 char buffer[SD_ID128_UUID_STRING_MAX];
ff0771bf
LP
3376 log_warning("Configured NoAuto=%s for partition type '%s' that doesn't support it, ignoring.",
3377 yes_no(p->no_auto),
3378 gpt_partition_type_uuid_to_string_harder(p->type_uuid, buffer));
3379 }
3380 }
3381
1c41c1dc
LP
3382 if (p->read_only >= 0) {
3383 if (gpt_partition_type_knows_read_only(p->type_uuid))
3384 SET_FLAG(f, GPT_FLAG_READ_ONLY, p->read_only);
3385 else {
b7416360 3386 char buffer[SD_ID128_UUID_STRING_MAX];
1c41c1dc
LP
3387 log_warning("Configured ReadOnly=%s for partition type '%s' that doesn't support it, ignoring.",
3388 yes_no(p->read_only),
3389 gpt_partition_type_uuid_to_string_harder(p->type_uuid, buffer));
3390 }
3391 }
3392
3393 if (p->growfs >= 0) {
3394 if (gpt_partition_type_knows_growfs(p->type_uuid))
3395 SET_FLAG(f, GPT_FLAG_GROWFS, p->growfs);
3396 else {
b7416360 3397 char buffer[SD_ID128_UUID_STRING_MAX];
1c41c1dc
LP
3398 log_warning("Configured GrowFileSystem=%s for partition type '%s' that doesn't support it, ignoring.",
3399 yes_no(p->growfs),
3400 gpt_partition_type_uuid_to_string_harder(p->type_uuid, buffer));
3401 }
3402 }
3403
3404 return f;
3405}
3406
f28d4f42 3407static int context_mangle_partitions(Context *context) {
f28d4f42 3408 int r;
e594a3b1
LP
3409
3410 assert(context);
3411
e594a3b1
LP
3412 LIST_FOREACH(partitions, p, context->partitions) {
3413 if (p->dropped)
3414 continue;
3415
3416 assert(p->new_size != UINT64_MAX);
3417 assert(p->offset != UINT64_MAX);
3418 assert(p->partno != UINT64_MAX);
3419
3420 if (PARTITION_EXISTS(p)) {
3421 bool changed = false;
3422
3423 assert(p->current_partition);
3424
3425 if (p->new_size != p->current_size) {
3426 assert(p->new_size >= p->current_size);
994b3031 3427 assert(p->new_size % context->sector_size == 0);
e594a3b1
LP
3428
3429 r = fdisk_partition_size_explicit(p->current_partition, true);
3430 if (r < 0)
3431 return log_error_errno(r, "Failed to enable explicit sizing: %m");
3432
994b3031 3433 r = fdisk_partition_set_size(p->current_partition, p->new_size / context->sector_size);
e594a3b1
LP
3434 if (r < 0)
3435 return log_error_errno(r, "Failed to grow partition: %m");
3436
3437 log_info("Growing existing partition %" PRIu64 ".", p->partno);
3438 changed = true;
3439 }
3440
3441 if (!sd_id128_equal(p->new_uuid, p->current_uuid)) {
b7416360 3442 r = fdisk_partition_set_uuid(p->current_partition, SD_ID128_TO_UUID_STRING(p->new_uuid));
e594a3b1
LP
3443 if (r < 0)
3444 return log_error_errno(r, "Failed to set partition UUID: %m");
3445
3446 log_info("Initializing UUID of existing partition %" PRIu64 ".", p->partno);
3447 changed = true;
3448 }
3449
3450 if (!streq_ptr(p->new_label, p->current_label)) {
be9ce018 3451 r = fdisk_partition_set_name(p->current_partition, strempty(p->new_label));
e594a3b1
LP
3452 if (r < 0)
3453 return log_error_errno(r, "Failed to set partition label: %m");
3454
3455 log_info("Setting partition label of existing partition %" PRIu64 ".", p->partno);
3456 changed = true;
3457 }
3458
3459 if (changed) {
3460 assert(!PARTITION_IS_FOREIGN(p)); /* never touch foreign partitions */
3461
3462 r = fdisk_set_partition(context->fdisk_context, p->partno, p->current_partition);
3463 if (r < 0)
3464 return log_error_errno(r, "Failed to update partition: %m");
3465 }
3466 } else {
3467 _cleanup_(fdisk_unref_partitionp) struct fdisk_partition *q = NULL;
3468 _cleanup_(fdisk_unref_parttypep) struct fdisk_parttype *t = NULL;
e594a3b1
LP
3469
3470 assert(!p->new_partition);
994b3031
LP
3471 assert(p->offset % context->sector_size == 0);
3472 assert(p->new_size % context->sector_size == 0);
be9ce018 3473 assert(p->new_label);
e594a3b1
LP
3474
3475 t = fdisk_new_parttype();
3476 if (!t)
3477 return log_oom();
3478
b7416360 3479 r = fdisk_parttype_set_typestr(t, SD_ID128_TO_UUID_STRING(p->type_uuid));
e594a3b1
LP
3480 if (r < 0)
3481 return log_error_errno(r, "Failed to initialize partition type: %m");
3482
3483 q = fdisk_new_partition();
3484 if (!q)
3485 return log_oom();
3486
3487 r = fdisk_partition_set_type(q, t);
3488 if (r < 0)
3489 return log_error_errno(r, "Failed to set partition type: %m");
3490
3491 r = fdisk_partition_size_explicit(q, true);
3492 if (r < 0)
3493 return log_error_errno(r, "Failed to enable explicit sizing: %m");
3494
994b3031 3495 r = fdisk_partition_set_start(q, p->offset / context->sector_size);
e594a3b1
LP
3496 if (r < 0)
3497 return log_error_errno(r, "Failed to position partition: %m");
3498
994b3031 3499 r = fdisk_partition_set_size(q, p->new_size / context->sector_size);
e594a3b1
LP
3500 if (r < 0)
3501 return log_error_errno(r, "Failed to grow partition: %m");
3502
3503 r = fdisk_partition_set_partno(q, p->partno);
3504 if (r < 0)
3505 return log_error_errno(r, "Failed to set partition number: %m");
3506
b7416360 3507 r = fdisk_partition_set_uuid(q, SD_ID128_TO_UUID_STRING(p->new_uuid));
e594a3b1
LP
3508 if (r < 0)
3509 return log_error_errno(r, "Failed to set partition UUID: %m");
3510
be9ce018 3511 r = fdisk_partition_set_name(q, strempty(p->new_label));
e594a3b1
LP
3512 if (r < 0)
3513 return log_error_errno(r, "Failed to set partition label: %m");
3514
ff0771bf 3515 /* Merge the no auto + read only + growfs setting with the literal flags, and set them for the partition */
1c41c1dc 3516 r = set_gpt_flags(q, partition_merge_flags(p));
e73309c5
LP
3517 if (r < 0)
3518 return log_error_errno(r, "Failed to set GPT partition flags: %m");
3519
5b5109e2 3520 log_info("Adding new partition %" PRIu64 " to partition table.", p->partno);
e594a3b1
LP
3521
3522 r = fdisk_add_partition(context->fdisk_context, q, NULL);
3523 if (r < 0)
3524 return log_error_errno(r, "Failed to add partition: %m");
3525
3526 assert(!p->new_partition);
3527 p->new_partition = TAKE_PTR(q);
3528 }
3529 }
3530
f28d4f42
LP
3531 return 0;
3532}
3533
3534static int context_write_partition_table(
3535 Context *context,
3536 const char *node,
3537 bool from_scratch) {
3538
3539 _cleanup_(fdisk_unref_tablep) struct fdisk_table *original_table = NULL;
3540 int capable, r;
3541
3542 assert(context);
3543
f28d4f42
LP
3544 if (!from_scratch && !context_changed(context)) {
3545 log_info("No changes.");
3546 return 0;
3547 }
3548
3549 if (arg_dry_run) {
3550 log_notice("Refusing to repartition, please re-run with --dry-run=no.");
3551 return 0;
3552 }
3553
3554 log_info("Applying changes.");
3555
3556 if (from_scratch) {
81873a6b
LP
3557 r = context_wipe_range(context, 0, context->total);
3558 if (r < 0)
3559 return r;
3560
3561 log_info("Wiped block device.");
3562
f28d4f42
LP
3563 r = context_discard_range(context, 0, context->total);
3564 if (r == -EOPNOTSUPP)
5b5109e2 3565 log_info("Storage does not support discard, not discarding entire block device data.");
f28d4f42
LP
3566 else if (r < 0)
3567 return log_error_errno(r, "Failed to discard entire block device: %m");
3568 else if (r > 0)
3569 log_info("Discarded entire block device.");
3570 }
3571
3572 r = fdisk_get_partitions(context->fdisk_context, &original_table);
3573 if (r < 0)
3574 return log_error_errno(r, "Failed to acquire partition table: %m");
3575
3576 /* Wipe fs signatures and discard sectors where the new partitions are going to be placed and in the
3577 * gaps between partitions, just to be sure. */
3578 r = context_wipe_and_discard(context, from_scratch);
3579 if (r < 0)
3580 return r;
3581
3582 r = context_copy_blocks(context);
3583 if (r < 0)
3584 return r;
3585
3586 r = context_mkfs(context);
3587 if (r < 0)
3588 return r;
3589
3590 r = context_mangle_partitions(context);
3591 if (r < 0)
3592 return r;
3593
e594a3b1
LP
3594 log_info("Writing new partition table.");
3595
3596 r = fdisk_write_disklabel(context->fdisk_context);
3597 if (r < 0)
3598 return log_error_errno(r, "Failed to write partition table: %m");
3599
911ba624 3600 capable = blockdev_partscan_enabled(fdisk_get_devfd(context->fdisk_context));
9a1deb85
LP
3601 if (capable == -ENOTBLK)
3602 log_debug("Not telling kernel to reread partition table, since we are not operating on a block device.");
3603 else if (capable < 0)
911ba624 3604 return log_error_errno(capable, "Failed to check if block device supports partition scanning: %m");
9a1deb85 3605 else if (capable > 0) {
e594a3b1
LP
3606 log_info("Telling kernel to reread partition table.");
3607
3608 if (from_scratch)
3609 r = fdisk_reread_partition_table(context->fdisk_context);
3610 else
3611 r = fdisk_reread_changes(context->fdisk_context, original_table);
3612 if (r < 0)
3613 return log_error_errno(r, "Failed to reread partition table: %m");
3614 } else
3615 log_notice("Not telling kernel to reread partition table, because selected image does not support kernel partition block devices.");
3616
3617 log_info("All done.");
3618
3619 return 0;
3620}
3621
3622static int context_read_seed(Context *context, const char *root) {
3623 int r;
3624
3625 assert(context);
3626
3627 if (!sd_id128_is_null(context->seed))
3628 return 0;
3629
3630 if (!arg_randomize) {
3631 _cleanup_close_ int fd = -1;
3632
3633 fd = chase_symlinks_and_open("/etc/machine-id", root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC, NULL);
3634 if (fd == -ENOENT)
3635 log_info("No machine ID set, using randomized partition UUIDs.");
3636 else if (fd < 0)
3637 return log_error_errno(fd, "Failed to determine machine ID of image: %m");
3638 else {
448b782c 3639 r = id128_read_fd(fd, ID128_PLAIN_OR_UNINIT, &context->seed);
e594a3b1
LP
3640 if (r == -ENOMEDIUM)
3641 log_info("No machine ID set, using randomized partition UUIDs.");
3642 else if (r < 0)
3643 return log_error_errno(r, "Failed to parse machine ID of image: %m");
3644
3645 return 0;
3646 }
3647 }
3648
3649 r = sd_id128_randomize(&context->seed);
3650 if (r < 0)
3651 return log_error_errno(r, "Failed to generate randomized seed: %m");
3652
3653 return 0;
3654}
3655
3656static int context_factory_reset(Context *context, bool from_scratch) {
e594a3b1
LP
3657 size_t n = 0;
3658 int r;
3659
3660 assert(context);
3661
3662 if (arg_factory_reset <= 0)
3663 return 0;
3664
3665 if (from_scratch) /* Nothing to reset if we start from scratch */
3666 return 0;
3667
3668 if (arg_dry_run) {
3669 log_notice("Refusing to factory reset, please re-run with --dry-run=no.");
3670 return 0;
3671 }
3672
3673 log_info("Applying factory reset.");
3674
3675 LIST_FOREACH(partitions, p, context->partitions) {
3676
3677 if (!p->factory_reset || !PARTITION_EXISTS(p))
3678 continue;
3679
3680 assert(p->partno != UINT64_MAX);
3681
3682 log_info("Removing partition %" PRIu64 " for factory reset.", p->partno);
3683
3684 r = fdisk_delete_partition(context->fdisk_context, p->partno);
3685 if (r < 0)
3686 return log_error_errno(r, "Failed to remove partition %" PRIu64 ": %m", p->partno);
3687
3688 n++;
3689 }
3690
3691 if (n == 0) {
3692 log_info("Factory reset requested, but no partitions to delete found.");
3693 return 0;
3694 }
3695
3696 r = fdisk_write_disklabel(context->fdisk_context);
3697 if (r < 0)
3698 return log_error_errno(r, "Failed to write disk label: %m");
3699
3700 log_info("Successfully deleted %zu partitions.", n);
3701 return 1;
3702}
3703
3704static int context_can_factory_reset(Context *context) {
e594a3b1
LP
3705 assert(context);
3706
3707 LIST_FOREACH(partitions, p, context->partitions)
3708 if (p->factory_reset && PARTITION_EXISTS(p))
3709 return true;
3710
3711 return false;
3712}
3713
5c08da58
LP
3714static int resolve_copy_blocks_auto_candidate(
3715 dev_t partition_devno,
3716 sd_id128_t partition_type_uuid,
3717 dev_t restrict_devno,
3718 sd_id128_t *ret_uuid) {
3719
3720 _cleanup_(blkid_free_probep) blkid_probe b = NULL;
5c08da58 3721 _cleanup_close_ int fd = -1;
ca822829
YW
3722 _cleanup_free_ char *p = NULL;
3723 const char *pttype, *t;
5c08da58
LP
3724 sd_id128_t pt_parsed, u;
3725 blkid_partition pp;
3726 dev_t whole_devno;
3727 blkid_partlist pl;
5c08da58
LP
3728 int r;
3729
3730 /* Checks if the specified partition has the specified GPT type UUID, and is located on the specified
3731 * 'restrict_devno' device. The type check is particularly relevant if we have Verity volume which is
3732 * backed by two separate partitions: the data and the hash partitions, and we need to find the right
3733 * one of the two. */
3734
3735 r = block_get_whole_disk(partition_devno, &whole_devno);
3736 if (r < 0)
3737 return log_error_errno(
3738 r,
3739 "Unable to determine containing block device of partition %u:%u: %m",
3740 major(partition_devno), minor(partition_devno));
3741
3742 if (restrict_devno != (dev_t) -1 &&
3743 restrict_devno != whole_devno)
3744 return log_error_errno(
3745 SYNTHETIC_ERRNO(EPERM),
3746 "Partition %u:%u is located outside of block device %u:%u, refusing.",
3747 major(partition_devno), minor(partition_devno),
3748 major(restrict_devno), minor(restrict_devno));
3749
ca822829 3750 fd = r = device_open_from_devnum(S_IFBLK, whole_devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &p);
5c08da58 3751 if (r < 0)
ca822829
YW
3752 return log_error_errno(r, "Failed to open block device " DEVNUM_FORMAT_STR ": %m",
3753 DEVNUM_FORMAT_VAL(whole_devno));
5c08da58
LP
3754
3755 b = blkid_new_probe();
3756 if (!b)
3757 return log_oom();
3758
3759 errno = 0;
3760 r = blkid_probe_set_device(b, fd, 0, 0);
3761 if (r != 0)
3762 return log_error_errno(errno_or_else(ENOMEM), "Failed to open block device '%s': %m", p);
3763
3764 (void) blkid_probe_enable_partitions(b, 1);
3765 (void) blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
3766
3767 errno = 0;
3768 r = blkid_do_safeprobe(b);
3769 if (IN_SET(r, -2, 1)) { /* nothing found or ambiguous result */
3770 log_debug("Didn't find partition table on block device '%s'.", p);
3771 return false;
3772 }
3773 if (r != 0)
3774 return log_error_errno(errno_or_else(EIO), "Unable to probe for partition table of '%s': %m", p);
3775
3776 (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL);
3777 if (!streq_ptr(pttype, "gpt")) {
3778 log_debug("Didn't find a GPT partition table on '%s'.", p);
3779 return false;
3780 }
3781
3782 errno = 0;
3783 pl = blkid_probe_get_partitions(b);
3784 if (!pl)
3785 return log_error_errno(errno_or_else(EIO), "Unable read partition table of '%s': %m", p);
3786 errno = 0;
3787
3788 pp = blkid_partlist_devno_to_partition(pl, partition_devno);
3789 if (!pp) {
3790 log_debug("Partition %u:%u has no matching partition table entry on '%s'.",
3791 major(partition_devno), minor(partition_devno), p);
3792 return false;
3793 }
3794
3795 t = blkid_partition_get_type_string(pp);
3796 if (isempty(t)) {
3797 log_debug("Partition %u:%u has no type on '%s'.",
3798 major(partition_devno), minor(partition_devno), p);
3799 return false;
3800 }
3801
3802 r = sd_id128_from_string(t, &pt_parsed);
3803 if (r < 0) {
3804 log_debug_errno(r, "Failed to parse partition type \"%s\": %m", t);
3805 return false;
3806 }
3807
3808 if (!sd_id128_equal(pt_parsed, partition_type_uuid)) {
3809 log_debug("Partition %u:%u has non-matching partition type " SD_ID128_FORMAT_STR " (needed: " SD_ID128_FORMAT_STR "), ignoring.",
3810 major(partition_devno), minor(partition_devno),
3811 SD_ID128_FORMAT_VAL(pt_parsed), SD_ID128_FORMAT_VAL(partition_type_uuid));
3812 return false;
3813 }
3814
3815 t = blkid_partition_get_uuid(pp);
3816 if (isempty(t)) {
3817 log_debug("Partition %u:%u has no UUID.",
3818 major(partition_devno), minor(partition_devno));
3819 return false;
3820 }
3821
3822 r = sd_id128_from_string(t, &u);
3823 if (r < 0) {
3824 log_debug_errno(r, "Failed to parse partition UUID \"%s\": %m", t);
3825 return false;
3826 }
3827
3828 log_debug("Automatically found partition %u:%u of right type " SD_ID128_FORMAT_STR ".",
3829 major(partition_devno), minor(partition_devno),
3830 SD_ID128_FORMAT_VAL(pt_parsed));
3831
3832 if (ret_uuid)
3833 *ret_uuid = u;
3834
3835 return true;
3836}
3837
3838static int find_backing_devno(
3839 const char *path,
3840 const char *root,
3841 dev_t *ret) {
3842
3843 _cleanup_free_ char *resolved = NULL;
3844 int r;
3845
3846 assert(path);
3847
3848 r = chase_symlinks(path, root, CHASE_PREFIX_ROOT, &resolved, NULL);
3849 if (r < 0)
3850 return r;
3851
3852 r = path_is_mount_point(resolved, NULL, 0);
3853 if (r < 0)
3854 return r;
3855 if (r == 0) /* Not a mount point, then it's not a partition of its own, let's not automatically use it. */
3856 return -ENOENT;
3857
3858 r = get_block_device(resolved, ret);
3859 if (r < 0)
3860 return r;
3861 if (r == 0) /* Not backed by physical file system, we can't use this */
3862 return -ENOENT;
3863
3864 return 0;
3865}
3866
3867static int resolve_copy_blocks_auto(
3868 sd_id128_t type_uuid,
3869 const char *root,
3870 dev_t restrict_devno,
1a037ba2 3871 dev_t *ret_devno,
5c08da58
LP
3872 sd_id128_t *ret_uuid) {
3873
3874 const char *try1 = NULL, *try2 = NULL;
3875 char p[SYS_BLOCK_PATH_MAX("/slaves")];
3876 _cleanup_(closedirp) DIR *d = NULL;
3877 sd_id128_t found_uuid = SD_ID128_NULL;
3878 dev_t devno, found = 0;
3879 int r;
3880
5c08da58
LP
3881 /* Enforce some security restrictions: CopyBlocks=auto should not be an avenue to get outside of the
3882 * --root=/--image= confinement. Specifically, refuse CopyBlocks= in combination with --root= at all,
3883 * and restrict block device references in the --image= case to loopback block device we set up.
3884 *
3885 * restrict_devno contain the dev_t of the loop back device we operate on in case of --image=, and
3886 * thus declares which device (and its partition subdevices) we shall limit access to. If
3887 * restrict_devno is zero no device probing access shall be allowed at all (used for --root=) and if
3888 * it is (dev_t) -1 then free access shall be allowed (if neither switch is used). */
3889
3890 if (restrict_devno == 0)
3891 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
3892 "Automatic discovery of backing block devices not permitted in --root= mode, refusing.");
3893
3894 /* Handles CopyBlocks=auto, and finds the right source partition to copy from. We look for matching
3895 * partitions in the host, using the appropriate directory as key and ensuring that the partition
3896 * type matches. */
3897
3898 if (gpt_partition_type_is_root(type_uuid))
3899 try1 = "/";
3900 else if (gpt_partition_type_is_usr(type_uuid))
3901 try1 = "/usr/";
3902 else if (gpt_partition_type_is_root_verity(type_uuid))
3903 try1 = "/";
3904 else if (gpt_partition_type_is_usr_verity(type_uuid))
3905 try1 = "/usr/";
3906 else if (sd_id128_equal(type_uuid, GPT_ESP)) {
3907 try1 = "/efi/";
3908 try2 = "/boot/";
3909 } else if (sd_id128_equal(type_uuid, GPT_XBOOTLDR))
3910 try1 = "/boot/";
3911 else
3912 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
3913 "Partition type " SD_ID128_FORMAT_STR " not supported from automatic source block device discovery.",
3914 SD_ID128_FORMAT_VAL(type_uuid));
3915
3916 r = find_backing_devno(try1, root, &devno);
3917 if (r == -ENOENT && try2)
3918 r = find_backing_devno(try2, root, &devno);
3919 if (r < 0)
3920 return log_error_errno(r, "Failed to resolve automatic CopyBlocks= path for partition type " SD_ID128_FORMAT_STR ", sorry: %m",
3921 SD_ID128_FORMAT_VAL(type_uuid));
3922
3923 xsprintf_sys_block_path(p, "/slaves", devno);
3924 d = opendir(p);
3925 if (d) {
3926 struct dirent *de;
3927
3928 for (;;) {
3929 _cleanup_free_ char *q = NULL, *t = NULL;
3930 sd_id128_t u;
3931 dev_t sl;
3932
3933 errno = 0;
3934 de = readdir_no_dot(d);
3935 if (!de) {
3936 if (errno != 0)
3937 return log_error_errno(errno, "Failed to read directory '%s': %m", p);
3938
3939 break;
3940 }
3941
3942 if (!IN_SET(de->d_type, DT_LNK, DT_UNKNOWN))
3943 continue;
3944
3945 q = path_join(p, de->d_name, "/dev");
3946 if (!q)
3947 return log_oom();
3948
3949 r = read_one_line_file(q, &t);
3950 if (r < 0)
3951 return log_error_errno(r, "Failed to read %s: %m", q);
3952
7176f06c 3953 r = parse_devnum(t, &sl);
5c08da58
LP
3954 if (r < 0) {
3955 log_debug_errno(r, "Failed to parse %s, ignoring: %m", q);
3956 continue;
3957 }
3958 if (major(sl) == 0) {
3959 log_debug_errno(r, "Device backing %s is special, ignoring: %m", q);
3960 continue;
3961 }
3962
3963 r = resolve_copy_blocks_auto_candidate(sl, type_uuid, restrict_devno, &u);
3964 if (r < 0)
3965 return r;
3966 if (r > 0) {
3967 /* We found a matching one! */
3968 if (found != 0)
3969 return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ),
3970 "Multiple matching partitions found, refusing.");
3971
3972 found = sl;
3973 found_uuid = u;
3974 }
3975 }
3976 } else if (errno != ENOENT)
3977 return log_error_errno(errno, "Failed open %s: %m", p);
3978 else {
3979 r = resolve_copy_blocks_auto_candidate(devno, type_uuid, restrict_devno, &found_uuid);
3980 if (r < 0)
3981 return r;
3982 if (r > 0)
3983 found = devno;
3984 }
3985
3986 if (found == 0)
3987 return log_error_errno(SYNTHETIC_ERRNO(ENXIO),
3988 "Unable to automatically discover suitable partition to copy blocks from.");
3989
1a037ba2
YW
3990 if (ret_devno)
3991 *ret_devno = found;
5c08da58
LP
3992
3993 if (ret_uuid)
3994 *ret_uuid = found_uuid;
3995
3996 return 0;
3997}
3998
3999static int context_open_copy_block_paths(
4000 Context *context,
4001 const char *root,
4002 dev_t restrict_devno) {
4003
757bc2e4
LP
4004 int r;
4005
4006 assert(context);
4007
4008 LIST_FOREACH(partitions, p, context->partitions) {
4009 _cleanup_close_ int source_fd = -1;
5c08da58
LP
4010 _cleanup_free_ char *opened = NULL;
4011 sd_id128_t uuid = SD_ID128_NULL;
757bc2e4
LP
4012 uint64_t size;
4013 struct stat st;
4014
4015 assert(p->copy_blocks_fd < 0);
4016 assert(p->copy_blocks_size == UINT64_MAX);
4017
4018 if (PARTITION_EXISTS(p)) /* Never copy over partitions that already exist! */
4019 continue;
4020
5c08da58 4021 if (p->copy_blocks_path) {
757bc2e4 4022
5c08da58
LP
4023 source_fd = chase_symlinks_and_open(p->copy_blocks_path, root, CHASE_PREFIX_ROOT, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened);
4024 if (source_fd < 0)
4025 return log_error_errno(source_fd, "Failed to open '%s': %m", p->copy_blocks_path);
757bc2e4 4026
5c08da58
LP
4027 if (fstat(source_fd, &st) < 0)
4028 return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened);
4029
4030 if (!S_ISREG(st.st_mode) && restrict_devno != (dev_t) -1)
4031 return log_error_errno(SYNTHETIC_ERRNO(EPERM),
4032 "Copying from block device node is not permitted in --image=/--root= mode, refusing.");
4033
4034 } else if (p->copy_blocks_auto) {
1a037ba2 4035 dev_t devno;
5c08da58 4036
1a037ba2 4037 r = resolve_copy_blocks_auto(p->type_uuid, root, restrict_devno, &devno, &uuid);
5c08da58
LP
4038 if (r < 0)
4039 return r;
4040
ca822829 4041 source_fd = r = device_open_from_devnum(S_IFBLK, devno, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &opened);
1a037ba2 4042 if (r < 0)
ca822829
YW
4043 return log_error_errno(r, "Failed to open automatically determined source block copy device " DEVNUM_FORMAT_STR ": %m",
4044 DEVNUM_FORMAT_VAL(devno));
5c08da58
LP
4045
4046 if (fstat(source_fd, &st) < 0)
4047 return log_error_errno(errno, "Failed to stat block copy file '%s': %m", opened);
1a037ba2 4048 } else
5c08da58 4049 continue;
757bc2e4
LP
4050
4051 if (S_ISDIR(st.st_mode)) {
ca822829
YW
4052 _cleanup_free_ char *bdev = NULL;
4053 dev_t devt;
757bc2e4
LP
4054
4055 /* If the file is a directory, automatically find the backing block device */
4056
4057 if (major(st.st_dev) != 0)
ca822829 4058 devt = st.st_dev;
757bc2e4 4059 else {
757bc2e4 4060 /* Special support for btrfs */
757bc2e4 4061 r = btrfs_get_block_device_fd(source_fd, &devt);
67f0ac8c 4062 if (r == -EUCLEAN)
5c08da58 4063 return btrfs_log_dev_root(LOG_ERR, r, opened);
757bc2e4 4064 if (r < 0)
5c08da58 4065 return log_error_errno(r, "Unable to determine backing block device of '%s': %m", opened);
757bc2e4 4066 }
757bc2e4
LP
4067
4068 safe_close(source_fd);
4069
ca822829
YW
4070 source_fd = r = device_open_from_devnum(S_IFBLK, devt, O_RDONLY|O_CLOEXEC|O_NONBLOCK, &bdev);
4071 if (r < 0)
4072 return log_error_errno(r, "Failed to open block device backing '%s': %m", opened);
757bc2e4
LP
4073
4074 if (fstat(source_fd, &st) < 0)
4075 return log_error_errno(errno, "Failed to stat block device '%s': %m", bdev);
757bc2e4
LP
4076 }
4077
4078 if (S_ISREG(st.st_mode))
4079 size = st.st_size;
4080 else if (S_ISBLK(st.st_mode)) {
4081 if (ioctl(source_fd, BLKGETSIZE64, &size) != 0)
4082 return log_error_errno(errno, "Failed to determine size of block device to copy from: %m");
4083 } else
5c08da58 4084 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
4085
4086 if (size <= 0)
5c08da58 4087 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File to copy bytes from '%s' has zero size, refusing.", opened);
757bc2e4 4088 if (size % 512 != 0)
5c08da58 4089 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
4090
4091 p->copy_blocks_fd = TAKE_FD(source_fd);
4092 p->copy_blocks_size = size;
5c08da58
LP
4093
4094 free_and_replace(p->copy_blocks_path, opened);
4095
4096 /* When copying from an existing partition copy that partitions UUID if none is configured explicitly */
11749b61 4097 if (!p->new_uuid_is_set && !sd_id128_is_null(uuid)) {
5c08da58 4098 p->new_uuid = uuid;
11749b61
DDM
4099 p->new_uuid_is_set = true;
4100 }
757bc2e4
LP
4101 }
4102
4103 return 0;
4104}
4105
e594a3b1
LP
4106static int help(void) {
4107 _cleanup_free_ char *link = NULL;
4108 int r;
4109
4110 r = terminal_urlify_man("systemd-repart", "1", &link);
4111 if (r < 0)
4112 return log_oom();
4113
4114 printf("%s [OPTIONS...] [DEVICE]\n"
4115 "\n%sGrow and add partitions to partition table.%s\n\n"
4116 " -h --help Show this help\n"
4117 " --version Show package version\n"
896e678b
LP
4118 " --no-pager Do not pipe output into a pager\n"
4119 " --no-legend Do not show the headers and footers\n"
e594a3b1 4120 " --dry-run=BOOL Whether to run dry-run operation\n"
a26f4a49
LP
4121 " --empty=MODE One of refuse, allow, require, force, create; controls\n"
4122 " how to handle empty disks lacking partition tables\n"
e594a3b1 4123 " --discard=BOOL Whether to discard backing blocks for new partitions\n"
2d2d0a57 4124 " --pretty=BOOL Whether to show pretty summary before doing changes\n"
e594a3b1
LP
4125 " --factory-reset=BOOL Whether to remove data partitions before recreating\n"
4126 " them\n"
4127 " --can-factory-reset Test whether factory reset is defined\n"
4128 " --root=PATH Operate relative to root path\n"
252d6267 4129 " --image=PATH Operate relative to image file\n"
9d252fbb 4130 " --definitions=DIR Find partition definitions in specified directory\n"
b9df3536 4131 " --key-file=PATH Key to use when encrypting partitions\n"
889914ef 4132 " --tpm2-device=PATH Path to TPM2 device node to use\n"
a1788a69 4133 " --tpm2-pcrs=PCR1+PCR2+PCR3+…\n"
889914ef 4134 " TPM2 PCR indexes to use for TPM2 enrollment\n"
e594a3b1 4135 " --seed=UUID 128bit seed UUID to derive all UUIDs from\n"
a26f4a49 4136 " --size=BYTES Grow loopback file to specified size\n"
2d2d0a57 4137 " --json=pretty|short|off\n"
de8231b0 4138 " Generate JSON output\n"
bc556335
DDM
4139 "\nSee the %s for details.\n",
4140 program_invocation_short_name,
4141 ansi_highlight(),
4142 ansi_normal(),
4143 link);
e594a3b1
LP
4144
4145 return 0;
4146}
4147
4148static int parse_argv(int argc, char *argv[]) {
4149
4150 enum {
4151 ARG_VERSION = 0x100,
896e678b
LP
4152 ARG_NO_PAGER,
4153 ARG_NO_LEGEND,
e594a3b1
LP
4154 ARG_DRY_RUN,
4155 ARG_EMPTY,
4156 ARG_DISCARD,
4157 ARG_FACTORY_RESET,
4158 ARG_CAN_FACTORY_RESET,
4159 ARG_ROOT,
252d6267 4160 ARG_IMAGE,
e594a3b1
LP
4161 ARG_SEED,
4162 ARG_PRETTY,
4163 ARG_DEFINITIONS,
a26f4a49 4164 ARG_SIZE,
a015fbe7 4165 ARG_JSON,
b9df3536 4166 ARG_KEY_FILE,
889914ef
LP
4167 ARG_TPM2_DEVICE,
4168 ARG_TPM2_PCRS,
e594a3b1
LP
4169 };
4170
4171 static const struct option options[] = {
4172 { "help", no_argument, NULL, 'h' },
4173 { "version", no_argument, NULL, ARG_VERSION },
896e678b
LP
4174 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
4175 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
e594a3b1
LP
4176 { "dry-run", required_argument, NULL, ARG_DRY_RUN },
4177 { "empty", required_argument, NULL, ARG_EMPTY },
4178 { "discard", required_argument, NULL, ARG_DISCARD },
4179 { "factory-reset", required_argument, NULL, ARG_FACTORY_RESET },
4180 { "can-factory-reset", no_argument, NULL, ARG_CAN_FACTORY_RESET },
4181 { "root", required_argument, NULL, ARG_ROOT },
252d6267 4182 { "image", required_argument, NULL, ARG_IMAGE },
e594a3b1
LP
4183 { "seed", required_argument, NULL, ARG_SEED },
4184 { "pretty", required_argument, NULL, ARG_PRETTY },
4185 { "definitions", required_argument, NULL, ARG_DEFINITIONS },
a26f4a49 4186 { "size", required_argument, NULL, ARG_SIZE },
a015fbe7 4187 { "json", required_argument, NULL, ARG_JSON },
b9df3536 4188 { "key-file", required_argument, NULL, ARG_KEY_FILE },
889914ef
LP
4189 { "tpm2-device", required_argument, NULL, ARG_TPM2_DEVICE },
4190 { "tpm2-pcrs", required_argument, NULL, ARG_TPM2_PCRS },
e594a3b1
LP
4191 {}
4192 };
4193
a26f4a49 4194 int c, r, dry_run = -1;
e594a3b1
LP
4195
4196 assert(argc >= 0);
4197 assert(argv);
4198
4199 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
4200
4201 switch (c) {
4202
4203 case 'h':
4204 return help();
4205
4206 case ARG_VERSION:
4207 return version();
4208
896e678b
LP
4209 case ARG_NO_PAGER:
4210 arg_pager_flags |= PAGER_DISABLE;
4211 break;
4212
4213 case ARG_NO_LEGEND:
4214 arg_legend = false;
4215 break;
4216
e594a3b1 4217 case ARG_DRY_RUN:
599c7c54 4218 r = parse_boolean_argument("--dry-run=", optarg, &arg_dry_run);
e594a3b1 4219 if (r < 0)
599c7c54 4220 return r;
e594a3b1
LP
4221 break;
4222
4223 case ARG_EMPTY:
4224 if (isempty(optarg) || streq(optarg, "refuse"))
4225 arg_empty = EMPTY_REFUSE;
4226 else if (streq(optarg, "allow"))
4227 arg_empty = EMPTY_ALLOW;
4228 else if (streq(optarg, "require"))
4229 arg_empty = EMPTY_REQUIRE;
4230 else if (streq(optarg, "force"))
4231 arg_empty = EMPTY_FORCE;
a26f4a49
LP
4232 else if (streq(optarg, "create")) {
4233 arg_empty = EMPTY_CREATE;
4234
4235 if (dry_run < 0)
4236 dry_run = false; /* Imply --dry-run=no if we create the loopback file
4237 * anew. After all we cannot really break anyone's
4238 * partition tables that way. */
4239 } else
e594a3b1
LP
4240 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
4241 "Failed to parse --empty= parameter: %s", optarg);
4242 break;
4243
4244 case ARG_DISCARD:
599c7c54 4245 r = parse_boolean_argument("--discard=", optarg, &arg_discard);
e594a3b1 4246 if (r < 0)
599c7c54 4247 return r;
e594a3b1
LP
4248 break;
4249
4250 case ARG_FACTORY_RESET:
c3470872 4251 r = parse_boolean_argument("--factory-reset=", optarg, NULL);
e594a3b1 4252 if (r < 0)
c3470872 4253 return r;
e594a3b1
LP
4254 arg_factory_reset = r;
4255 break;
4256
4257 case ARG_CAN_FACTORY_RESET:
4258 arg_can_factory_reset = true;
4259 break;
4260
4261 case ARG_ROOT:
252d6267
LP
4262 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root);
4263 if (r < 0)
4264 return r;
4265 break;
4266
4267 case ARG_IMAGE:
4268 r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
e594a3b1
LP
4269 if (r < 0)
4270 return r;
4271 break;
4272
4273 case ARG_SEED:
4274 if (isempty(optarg)) {
4275 arg_seed = SD_ID128_NULL;
4276 arg_randomize = false;
4277 } else if (streq(optarg, "random"))
4278 arg_randomize = true;
4279 else {
4280 r = sd_id128_from_string(optarg, &arg_seed);
4281 if (r < 0)
4282 return log_error_errno(r, "Failed to parse seed: %s", optarg);
4283
4284 arg_randomize = false;
4285 }
4286
4287 break;
4288
4289 case ARG_PRETTY:
c3470872 4290 r = parse_boolean_argument("--pretty=", optarg, NULL);
e594a3b1 4291 if (r < 0)
c3470872 4292 return r;
e594a3b1
LP
4293 arg_pretty = r;
4294 break;
4295
224c853f
RP
4296 case ARG_DEFINITIONS: {
4297 _cleanup_free_ char *path = NULL;
4298 r = parse_path_argument(optarg, false, &path);
e594a3b1
LP
4299 if (r < 0)
4300 return r;
224c853f
RP
4301 if (strv_consume(&arg_definitions, TAKE_PTR(path)) < 0)
4302 return log_oom();
e594a3b1 4303 break;
224c853f 4304 }
e594a3b1 4305
a26f4a49
LP
4306 case ARG_SIZE: {
4307 uint64_t parsed, rounded;
4308
170c9823
LP
4309 if (streq(optarg, "auto")) {
4310 arg_size = UINT64_MAX;
4311 arg_size_auto = true;
4312 break;
4313 }
4314
a26f4a49
LP
4315 r = parse_size(optarg, 1024, &parsed);
4316 if (r < 0)
4317 return log_error_errno(r, "Failed to parse --size= parameter: %s", optarg);
4318
4319 rounded = round_up_size(parsed, 4096);
4320 if (rounded == 0)
4321 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too small, refusing.");
4322 if (rounded == UINT64_MAX)
4323 return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Specified image size too large, refusing.");
4324
4325 if (rounded != parsed)
e2341b6b
DT
4326 log_warning("Specified size is not a multiple of 4096, rounding up automatically. (%" PRIu64 " %s %" PRIu64 ")",
4327 parsed, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), rounded);
a26f4a49
LP
4328
4329 arg_size = rounded;
170c9823 4330 arg_size_auto = false;
a26f4a49
LP
4331 break;
4332 }
b9df3536 4333
a015fbe7 4334 case ARG_JSON:
b1e8f46c 4335 r = parse_json_argument(optarg, &arg_json_format_flags);
6a01ea4a
LP
4336 if (r <= 0)
4337 return r;
a015fbe7
TH
4338
4339 break;
4340
b9df3536
LP
4341 case ARG_KEY_FILE: {
4342 _cleanup_(erase_and_freep) char *k = NULL;
4343 size_t n = 0;
4344
8b3c3a49 4345 r = read_full_file_full(
986311c2 4346 AT_FDCWD, optarg, UINT64_MAX, SIZE_MAX,
8b3c3a49
LP
4347 READ_FULL_FILE_SECURE|READ_FULL_FILE_WARN_WORLD_READABLE|READ_FULL_FILE_CONNECT_SOCKET,
4348 NULL,
4349 &k, &n);
b9df3536
LP
4350 if (r < 0)
4351 return log_error_errno(r, "Failed to read key file '%s': %m", optarg);
4352
4353 erase_and_free(arg_key);
4354 arg_key = TAKE_PTR(k);
4355 arg_key_size = n;
4356 break;
4357 }
a26f4a49 4358
889914ef
LP
4359 case ARG_TPM2_DEVICE: {
4360 _cleanup_free_ char *device = NULL;
4361
4362 if (streq(optarg, "list"))
4363 return tpm2_list_devices();
4364
4365 if (!streq(optarg, "auto")) {
4366 device = strdup(optarg);
4367 if (!device)
4368 return log_oom();
4369 }
4370
4371 free(arg_tpm2_device);
4372 arg_tpm2_device = TAKE_PTR(device);
4373 break;
4374 }
4375
222a951f
LP
4376 case ARG_TPM2_PCRS:
4377 r = tpm2_parse_pcr_argument(optarg, &arg_tpm2_pcr_mask);
889914ef
LP
4378 if (r < 0)
4379 return r;
4380
889914ef 4381 break;
889914ef 4382
e594a3b1
LP
4383 case '?':
4384 return -EINVAL;
4385
4386 default:
04499a70 4387 assert_not_reached();
e594a3b1
LP
4388 }
4389
4390 if (argc - optind > 1)
4391 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
4392 "Expected at most one argument, the path to the block device.");
4393
a26f4a49 4394 if (arg_factory_reset > 0 && IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE))
e594a3b1 4395 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
a26f4a49 4396 "Combination of --factory-reset=yes and --empty=force/--empty=require/--empty=create is invalid.");
e594a3b1
LP
4397
4398 if (arg_can_factory_reset)
a26f4a49
LP
4399 arg_dry_run = true; /* When --can-factory-reset is specified we don't make changes, hence
4400 * non-dry-run mode makes no sense. Thus, imply dry run mode so that we
4401 * open things strictly read-only. */
4402 else if (dry_run >= 0)
4403 arg_dry_run = dry_run;
4404
170c9823 4405 if (arg_empty == EMPTY_CREATE && (arg_size == UINT64_MAX && !arg_size_auto))
a26f4a49
LP
4406 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
4407 "If --empty=create is specified, --size= must be specified, too.");
e594a3b1 4408
252d6267
LP
4409 if (arg_image && arg_root)
4410 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
4411 else if (!arg_image && !arg_root && in_initrd()) {
8f47e32a
LP
4412
4413 /* By default operate on /sysusr/ or /sysroot/ when invoked in the initrd. We prefer the
4414 * former, if it is mounted, so that we have deterministic behaviour on systems where /usr/
4415 * is vendor-supplied but the root fs formatted on first boot. */
4416 r = path_is_mount_point("/sysusr/usr", NULL, 0);
4417 if (r <= 0) {
4418 if (r < 0 && r != -ENOENT)
4419 log_debug_errno(r, "Unable to determine whether /sysusr/usr is a mount point, assuming it is not: %m");
4420
4421 arg_root = strdup("/sysroot");
4422 } else
4423 arg_root = strdup("/sysusr");
252d6267
LP
4424 if (!arg_root)
4425 return log_oom();
4426 }
4427
e594a3b1 4428 arg_node = argc > optind ? argv[optind] : NULL;
a26f4a49 4429
252d6267 4430 if (IN_SET(arg_empty, EMPTY_FORCE, EMPTY_REQUIRE, EMPTY_CREATE) && !arg_node && !arg_image)
a26f4a49
LP
4431 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
4432 "A path to a device node or loopback file must be specified when --empty=force, --empty=require or --empty=create are used.");
4433
889914ef
LP
4434 if (arg_tpm2_pcr_mask == UINT32_MAX)
4435 arg_tpm2_pcr_mask = TPM2_PCR_MASK_DEFAULT;
4436
a26d463d
DDM
4437 if (arg_pretty < 0 && isatty(STDOUT_FILENO))
4438 arg_pretty = true;
4439
e594a3b1
LP
4440 return 1;
4441}
4442
4443static int parse_proc_cmdline_factory_reset(void) {
4444 bool b;
4445 int r;
4446
4447 if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */
4448 return 0;
4449
4450 if (!in_initrd()) /* Never honour kernel command line factory reset request outside of the initrd */
4451 return 0;
4452
4453 r = proc_cmdline_get_bool("systemd.factory_reset", &b);
4454 if (r < 0)
4455 return log_error_errno(r, "Failed to parse systemd.factory_reset kernel command line argument: %m");
4456 if (r > 0) {
4457 arg_factory_reset = b;
4458
4459 if (b)
4460 log_notice("Honouring factory reset requested via kernel command line.");
4461 }
4462
4463 return 0;
4464}
4465
4466static int parse_efi_variable_factory_reset(void) {
4467 _cleanup_free_ char *value = NULL;
4468 int r;
4469
4470 if (arg_factory_reset >= 0) /* Never override what is specified on the process command line */
4471 return 0;
4472
4473 if (!in_initrd()) /* Never honour EFI variable factory reset request outside of the initrd */
4474 return 0;
4475
e6f055cb 4476 r = efi_get_variable_string(EFI_SYSTEMD_VARIABLE(FactoryReset), &value);
e594a3b1
LP
4477 if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
4478 return 0;
4479 if (r < 0)
4480 return log_error_errno(r, "Failed to read EFI variable FactoryReset: %m");
4481
4482 r = parse_boolean(value);
4483 if (r < 0)
4484 return log_error_errno(r, "Failed to parse EFI variable FactoryReset: %m");
4485
4486 arg_factory_reset = r;
4487 if (r)
111a3aae 4488 log_notice("Factory reset requested via EFI variable FactoryReset.");
e594a3b1
LP
4489
4490 return 0;
4491}
4492
4493static int remove_efi_variable_factory_reset(void) {
4494 int r;
4495
e6f055cb 4496 r = efi_set_variable(EFI_SYSTEMD_VARIABLE(FactoryReset), NULL, 0);
e594a3b1
LP
4497 if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
4498 return 0;
4499 if (r < 0)
4500 return log_error_errno(r, "Failed to remove EFI variable FactoryReset: %m");
4501
4502 log_info("Successfully unset EFI variable FactoryReset.");
4503 return 0;
4504}
4505
252d6267
LP
4506static int acquire_root_devno(
4507 const char *p,
4508 const char *root,
4509 int mode,
4510 char **ret,
4511 int *ret_fd) {
4512
4513 _cleanup_free_ char *found_path = NULL;
4514 dev_t devno, fd_devno = MODE_INVALID;
e594a3b1
LP
4515 _cleanup_close_ int fd = -1;
4516 struct stat st;
e594a3b1
LP
4517 int r;
4518
a26f4a49
LP
4519 assert(p);
4520 assert(ret);
4521 assert(ret_fd);
4522
252d6267 4523 fd = chase_symlinks_and_open(p, root, CHASE_PREFIX_ROOT, mode, &found_path);
e594a3b1 4524 if (fd < 0)
252d6267 4525 return fd;
e594a3b1
LP
4526
4527 if (fstat(fd, &st) < 0)
4528 return -errno;
4529
4530 if (S_ISREG(st.st_mode)) {
252d6267 4531 *ret = TAKE_PTR(found_path);
a26f4a49 4532 *ret_fd = TAKE_FD(fd);
e594a3b1
LP
4533 return 0;
4534 }
4535
252d6267
LP
4536 if (S_ISBLK(st.st_mode)) {
4537 /* Refuse referencing explicit block devices if a root dir is specified, after all we should
5c08da58 4538 * not be able to leave the image the root path constrains us to. */
252d6267
LP
4539 if (root)
4540 return -EPERM;
4541
a26f4a49 4542 fd_devno = devno = st.st_rdev;
252d6267 4543 } else if (S_ISDIR(st.st_mode)) {
e594a3b1
LP
4544
4545 devno = st.st_dev;
a26f4a49 4546 if (major(devno) == 0) {
e594a3b1
LP
4547 r = btrfs_get_block_device_fd(fd, &devno);
4548 if (r == -ENOTTY) /* not btrfs */
4549 return -ENODEV;
4550 if (r < 0)
4551 return r;
4552 }
e594a3b1
LP
4553 } else
4554 return -ENOTBLK;
4555
4556 /* From dm-crypt to backing partition */
4557 r = block_get_originating(devno, &devno);
8e5f3cec
LP
4558 if (r == -ENOENT)
4559 log_debug_errno(r, "Device '%s' has no dm-crypt/dm-verity device, no need to look for underlying block device.", p);
4560 else if (r < 0)
e594a3b1
LP
4561 log_debug_errno(r, "Failed to find underlying block device for '%s', ignoring: %m", p);
4562
4563 /* From partition to whole disk containing it */
4564 r = block_get_whole_disk(devno, &devno);
4565 if (r < 0)
162392b7 4566 log_debug_errno(r, "Failed to find whole disk block device for '%s', ignoring: %m", p);
e594a3b1 4567
4fe46c34 4568 r = devname_from_devnum(S_IFBLK, devno, ret);
a26f4a49
LP
4569 if (r < 0)
4570 return log_debug_errno(r, "Failed to determine canonical path for '%s': %m", p);
4571
6bbae9f8 4572 /* Only if we still look at the same block device we can reuse the fd. Otherwise return an
a26f4a49 4573 * invalidated fd. */
f5fbe71d 4574 *ret_fd = fd_devno != MODE_INVALID && fd_devno == devno ? TAKE_FD(fd) : -1;
a26f4a49 4575 return 0;
e594a3b1
LP
4576}
4577
a26f4a49 4578static int find_root(char **ret, int *ret_fd) {
54632d2e 4579 _cleanup_free_ char *device = NULL;
5980d463 4580 int r;
e594a3b1 4581
a26f4a49
LP
4582 assert(ret);
4583 assert(ret_fd);
4584
e594a3b1 4585 if (arg_node) {
a26f4a49
LP
4586 if (arg_empty == EMPTY_CREATE) {
4587 _cleanup_close_ int fd = -1;
4588 _cleanup_free_ char *s = NULL;
4589
4590 s = strdup(arg_node);
4591 if (!s)
4592 return log_oom();
4593
5332d7c6 4594 fd = open(arg_node, O_RDONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOFOLLOW, 0666);
a26f4a49
LP
4595 if (fd < 0)
4596 return log_error_errno(errno, "Failed to create '%s': %m", arg_node);
4597
4598 *ret = TAKE_PTR(s);
4599 *ret_fd = TAKE_FD(fd);
4600 return 0;
4601 }
4602
252d6267
LP
4603 /* Note that we don't specify a root argument here: if the user explicitly configured a node
4604 * we'll take it relative to the host, not the image */
4605 r = acquire_root_devno(arg_node, NULL, O_RDONLY|O_CLOEXEC, ret, ret_fd);
67f0ac8c
LP
4606 if (r == -EUCLEAN)
4607 return btrfs_log_dev_root(LOG_ERR, r, arg_node);
e594a3b1 4608 if (r < 0)
aa2a74ad 4609 return log_error_errno(r, "Failed to open file or determine backing device of %s: %m", arg_node);
e594a3b1
LP
4610
4611 return 0;
4612 }
4613
a26f4a49
LP
4614 assert(IN_SET(arg_empty, EMPTY_REFUSE, EMPTY_ALLOW));
4615
54632d2e
KK
4616 /* If the root mount has been replaced by some form of volatile file system (overlayfs), the
4617 * original root block device node is symlinked in /run/systemd/volatile-root. Let's read that
4618 * here. */
4619 r = readlink_malloc("/run/systemd/volatile-root", &device);
4620 if (r == -ENOENT) { /* volatile-root not found */
4621 /* Let's search for the root device. We look for two cases here: first in /, and then in /usr. The
4622 * latter we check for cases where / is a tmpfs and only /usr is an actual persistent block device
4623 * (think: volatile setups) */
e594a3b1 4624
54632d2e 4625 FOREACH_STRING(p, "/", "/usr") {
e594a3b1 4626
54632d2e
KK
4627 r = acquire_root_devno(p, arg_root, O_RDONLY|O_DIRECTORY|O_CLOEXEC, ret, ret_fd);
4628 if (r < 0) {
4629 if (r == -EUCLEAN)
4630 return btrfs_log_dev_root(LOG_ERR, r, p);
4631 if (r != -ENODEV)
4632 return log_error_errno(r, "Failed to determine backing device of %s: %m", p);
4633 } else
4634 return 0;
4635 }
4636 } else if (r < 0)
4637 return log_error_errno(r, "Failed to read symlink /run/systemd/volatile-root: %m");
4638 else {
4639 r = acquire_root_devno(device, NULL, O_RDONLY|O_CLOEXEC, ret, ret_fd);
4640 if (r == -EUCLEAN)
4641 return btrfs_log_dev_root(LOG_ERR, r, device);
4642 if (r < 0)
4643 return log_error_errno(r, "Failed to open file or determine backing device of %s: %m", device);
4644
4645 return 0;
e594a3b1
LP
4646 }
4647
4648 return log_error_errno(SYNTHETIC_ERRNO(ENODEV), "Failed to discover root block device.");
4649}
4650
f9b3afae 4651static int resize_pt(int fd) {
f9b3afae
LP
4652 _cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
4653 int r;
4654
4655 /* After resizing the backing file we need to resize the partition table itself too, so that it takes
4656 * possession of the enlarged backing file. For this it suffices to open the device with libfdisk and
4657 * immediately write it again, with no changes. */
4658
4659 c = fdisk_new_context();
4660 if (!c)
4661 return log_oom();
4662
ddb6eeaf 4663 r = fdisk_assign_device(c, FORMAT_PROC_FD_PATH(fd), 0);
f9b3afae 4664 if (r < 0)
ddb6eeaf 4665 return log_error_errno(r, "Failed to open device '%s': %m", FORMAT_PROC_FD_PATH(fd));
f9b3afae
LP
4666
4667 r = fdisk_has_label(c);
4668 if (r < 0)
ddb6eeaf 4669 return log_error_errno(r, "Failed to determine whether disk '%s' has a disk label: %m", FORMAT_PROC_FD_PATH(fd));
f9b3afae
LP
4670 if (r == 0) {
4671 log_debug("Not resizing partition table, as there currently is none.");
4672 return 0;
4673 }
4674
4675 r = fdisk_write_disklabel(c);
4676 if (r < 0)
4677 return log_error_errno(r, "Failed to write resized partition table: %m");
4678
4679 log_info("Resized partition table.");
4680 return 1;
4681}
4682
252d6267
LP
4683static int resize_backing_fd(
4684 const char *node, /* The primary way we access the disk image to operate on */
4685 int *fd, /* An O_RDONLY fd referring to that inode */
4686 const char *backing_file, /* If the above refers to a loopback device, the backing regular file for that, which we can grow */
4687 LoopDevice *loop_device) {
4688
a26f4a49 4689 _cleanup_close_ int writable_fd = -1;
252d6267 4690 uint64_t current_size;
a26f4a49
LP
4691 struct stat st;
4692 int r;
4693
4694 assert(node);
4695 assert(fd);
4696
4697 if (arg_size == UINT64_MAX) /* Nothing to do */
4698 return 0;
4699
4700 if (*fd < 0) {
4701 /* Open the file if we haven't opened it yet. Note that we open it read-only here, just to
4702 * keep a reference to the file we can pass around. */
4703 *fd = open(node, O_RDONLY|O_CLOEXEC);
4704 if (*fd < 0)
4705 return log_error_errno(errno, "Failed to open '%s' in order to adjust size: %m", node);
4706 }
4707
4708 if (fstat(*fd, &st) < 0)
4709 return log_error_errno(errno, "Failed to stat '%s': %m", node);
4710
252d6267
LP
4711 if (S_ISBLK(st.st_mode)) {
4712 if (!backing_file)
4713 return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Cannot resize block device '%s'.", node);
4714
4715 assert(loop_device);
a26f4a49 4716
252d6267
LP
4717 if (ioctl(*fd, BLKGETSIZE64, &current_size) < 0)
4718 return log_error_errno(errno, "Failed to determine size of block device %s: %m", node);
4719 } else {
4720 r = stat_verify_regular(&st);
4721 if (r < 0)
4722 return log_error_errno(r, "Specified path '%s' is not a regular file or loopback block device, cannot resize: %m", node);
4723
4724 assert(!backing_file);
4725 assert(!loop_device);
4726 current_size = st.st_size;
4727 }
4728
252d6267 4729 if (current_size >= arg_size) {
2b59bf51
ZJS
4730 log_info("File '%s' already is of requested size or larger, not growing. (%s >= %s)",
4731 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49
LP
4732 return 0;
4733 }
4734
252d6267
LP
4735 if (S_ISBLK(st.st_mode)) {
4736 assert(backing_file);
4737
4738 /* This is a loopback device. We can't really grow those directly, but we can grow the
4739 * backing file, hence let's do that. */
4740
4741 writable_fd = open(backing_file, O_WRONLY|O_CLOEXEC|O_NONBLOCK);
4742 if (writable_fd < 0)
4743 return log_error_errno(errno, "Failed to open backing file '%s': %m", backing_file);
4744
4745 if (fstat(writable_fd, &st) < 0)
4746 return log_error_errno(errno, "Failed to stat() backing file '%s': %m", backing_file);
4747
4748 r = stat_verify_regular(&st);
4749 if (r < 0)
4750 return log_error_errno(r, "Backing file '%s' of block device is not a regular file: %m", backing_file);
4751
4752 if ((uint64_t) st.st_size != current_size)
4753 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2b59bf51
ZJS
4754 "Size of backing file '%s' of loopback block device '%s' don't match, refusing.",
4755 node, backing_file);
252d6267
LP
4756 } else {
4757 assert(S_ISREG(st.st_mode));
4758 assert(!backing_file);
a26f4a49 4759
252d6267
LP
4760 /* The file descriptor is read-only. In order to grow the file we need to have a writable fd. We
4761 * reopen the file for that temporarily. We keep the writable fd only open for this operation though,
4762 * as fdisk can't accept it anyway. */
4763
4764 writable_fd = fd_reopen(*fd, O_WRONLY|O_CLOEXEC);
4765 if (writable_fd < 0)
4766 return log_error_errno(writable_fd, "Failed to reopen backing file '%s' writable: %m", node);
4767 }
a26f4a49
LP
4768
4769 if (!arg_discard) {
4770 if (fallocate(writable_fd, 0, 0, arg_size) < 0) {
4771 if (!ERRNO_IS_NOT_SUPPORTED(errno))
4772 return log_error_errno(errno, "Failed to grow '%s' from %s to %s by allocation: %m",
2b59bf51 4773 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49
LP
4774
4775 /* Fallback to truncation, if fallocate() is not supported. */
4776 log_debug("Backing file system does not support fallocate(), falling back to ftruncate().");
4777 } else {
252d6267 4778 if (current_size == 0) /* Likely regular file just created by us */
2b59bf51 4779 log_info("Allocated %s for '%s'.", FORMAT_BYTES(arg_size), node);
a26f4a49 4780 else
2b59bf51
ZJS
4781 log_info("File '%s' grown from %s to %s by allocation.",
4782 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49 4783
252d6267 4784 goto done;
a26f4a49
LP
4785 }
4786 }
4787
4788 if (ftruncate(writable_fd, arg_size) < 0)
4789 return log_error_errno(errno, "Failed to grow '%s' from %s to %s by truncation: %m",
2b59bf51 4790 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
a26f4a49 4791
252d6267 4792 if (current_size == 0) /* Likely regular file just created by us */
2b59bf51 4793 log_info("Sized '%s' to %s.", node, FORMAT_BYTES(arg_size));
252d6267 4794 else
2b59bf51
ZJS
4795 log_info("File '%s' grown from %s to %s by truncation.",
4796 node, FORMAT_BYTES(current_size), FORMAT_BYTES(arg_size));
252d6267
LP
4797
4798done:
f9b3afae
LP
4799 r = resize_pt(writable_fd);
4800 if (r < 0)
4801 return r;
4802
252d6267
LP
4803 if (loop_device) {
4804 r = loop_device_refresh_size(loop_device, UINT64_MAX, arg_size);
4805 if (r < 0)
4806 return log_error_errno(r, "Failed to update loop device size: %m");
4807 }
a26f4a49
LP
4808
4809 return 1;
4810}
4811
170c9823 4812static int determine_auto_size(Context *c) {
994b3031 4813 uint64_t sum;
170c9823 4814
ac33e147 4815 assert(c);
170c9823 4816
994b3031
LP
4817 sum = round_up_size(GPT_METADATA_SIZE, 4096);
4818
170c9823
LP
4819 LIST_FOREACH(partitions, p, c->partitions) {
4820 uint64_t m;
4821
4822 if (p->dropped)
4823 continue;
4824
994b3031 4825 m = partition_min_size_with_padding(c, p);
170c9823
LP
4826 if (m > UINT64_MAX - sum)
4827 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Image would grow too large, refusing.");
4828
4829 sum += m;
4830 }
4831
2b59bf51
ZJS
4832 if (c->total != UINT64_MAX)
4833 /* Image already allocated? Then show its size. */
4834 log_info("Automatically determined minimal disk image size as %s, current image size is %s.",
4835 FORMAT_BYTES(sum), FORMAT_BYTES(c->total));
4836 else
4837 /* If the image is being created right now, then it has no previous size, suppress any comment about it hence. */
4838 log_info("Automatically determined minimal disk image size as %s.",
4839 FORMAT_BYTES(sum));
170c9823
LP
4840
4841 arg_size = sum;
4842 return 0;
4843}
4844
e594a3b1 4845static int run(int argc, char *argv[]) {
252d6267
LP
4846 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
4847 _cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL;
4848 _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
e594a3b1
LP
4849 _cleanup_(context_freep) Context* context = NULL;
4850 _cleanup_free_ char *node = NULL;
a26f4a49 4851 _cleanup_close_ int backing_fd = -1;
252d6267 4852 bool from_scratch, node_is_our_loop = false;
e594a3b1
LP
4853 int r;
4854
4855 log_show_color(true);
4856 log_parse_environment();
4857 log_open();
4858
e594a3b1
LP
4859 r = parse_argv(argc, argv);
4860 if (r <= 0)
4861 return r;
4862
4863 r = parse_proc_cmdline_factory_reset();
4864 if (r < 0)
4865 return r;
4866
4867 r = parse_efi_variable_factory_reset();
4868 if (r < 0)
4869 return r;
4870
30f19400
LP
4871#if HAVE_LIBCRYPTSETUP
4872 cryptsetup_enable_logging(NULL);
4873#endif
4874
252d6267
LP
4875 if (arg_image) {
4876 assert(!arg_root);
4877
4878 /* Mount this strictly read-only: we shall modify the partition table, not the file
4879 * systems */
4880 r = mount_image_privately_interactively(
4881 arg_image,
4882 DISSECT_IMAGE_MOUNT_READ_ONLY |
4883 (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) */
4884 DISSECT_IMAGE_GPT_ONLY |
4885 DISSECT_IMAGE_RELAX_VAR_CHECK |
4886 DISSECT_IMAGE_USR_NO_ROOT |
4887 DISSECT_IMAGE_REQUIRE_ROOT,
4888 &mounted_dir,
4889 &loop_device,
4890 &decrypted_image);
4891 if (r < 0)
4892 return r;
4893
4894 arg_root = strdup(mounted_dir);
4895 if (!arg_root)
4896 return log_oom();
4897
4898 if (!arg_node) {
4899 arg_node = strdup(loop_device->node);
4900 if (!arg_node)
4901 return log_oom();
4902
3d62af7d 4903 /* Remember that the device we are about to manipulate is actually the one we
252d6267
LP
4904 * allocated here, and thus to increase its backing file we know what to do */
4905 node_is_our_loop = true;
4906 }
4907 }
4908
e594a3b1
LP
4909 context = context_new(arg_seed);
4910 if (!context)
4911 return log_oom();
4912
224c853f
RP
4913 strv_uniq(arg_definitions);
4914
e594a3b1
LP
4915 r = context_read_definitions(context, arg_definitions, arg_root);
4916 if (r < 0)
4917 return r;
4918
a26f4a49 4919 if (context->n_partitions <= 0 && arg_empty == EMPTY_REFUSE) {
e2d65cd2 4920 log_info("Didn't find any partition definition files, nothing to do.");
0ae5ffe0 4921 return 0;
e2d65cd2 4922 }
0ae5ffe0 4923
a26f4a49 4924 r = find_root(&node, &backing_fd);
0ae5ffe0
YW
4925 if (r < 0)
4926 return r;
4927
a26f4a49 4928 if (arg_size != UINT64_MAX) {
252d6267
LP
4929 r = resize_backing_fd(
4930 node,
4931 &backing_fd,
4932 node_is_our_loop ? arg_image : NULL,
4933 node_is_our_loop ? loop_device : NULL);
a26f4a49
LP
4934 if (r < 0)
4935 return r;
4936 }
4937
4938 r = context_load_partition_table(context, node, &backing_fd);
e594a3b1
LP
4939 if (r == -EHWPOISON)
4940 return 77; /* Special return value which means "Not GPT, so not doing anything". This isn't
4941 * really an error when called at boot. */
4942 if (r < 0)
4943 return r;
4944 from_scratch = r > 0; /* Starting from scratch */
4945
4946 if (arg_can_factory_reset) {
4947 r = context_can_factory_reset(context);
4948 if (r < 0)
4949 return r;
4950 if (r == 0)
4951 return EXIT_FAILURE;
4952
4953 return 0;
4954 }
4955
4956 r = context_factory_reset(context, from_scratch);
4957 if (r < 0)
4958 return r;
4959 if (r > 0) {
4960 /* We actually did a factory reset! */
4961 r = remove_efi_variable_factory_reset();
4962 if (r < 0)
4963 return r;
4964
4965 /* Reload the reduced partition table */
4966 context_unload_partition_table(context);
a26f4a49 4967 r = context_load_partition_table(context, node, &backing_fd);
e594a3b1
LP
4968 if (r < 0)
4969 return r;
4970 }
4971
4972#if 0
4973 (void) context_dump_partitions(context, node);
4974 putchar('\n');
4975#endif
4976
4977 r = context_read_seed(context, arg_root);
4978 if (r < 0)
4979 return r;
4980
757bc2e4 4981 /* Open all files to copy blocks from now, since we want to take their size into consideration */
5c08da58
LP
4982 r = context_open_copy_block_paths(
4983 context,
4984 arg_root,
7802194a 4985 loop_device ? loop_device->devno : /* if --image= is specified, only allow partitions on the loopback device */
5c08da58
LP
4986 arg_root && !arg_image ? 0 : /* if --root= is specified, don't accept any block device */
4987 (dev_t) -1); /* if neither is specified, make no restrictions */
757bc2e4
LP
4988 if (r < 0)
4989 return r;
4990
170c9823
LP
4991 if (arg_size_auto) {
4992 r = determine_auto_size(context);
4993 if (r < 0)
4994 return r;
4995
4996 /* Flush out everything again, and let's grow the file first, then start fresh */
4997 context_unload_partition_table(context);
4998
ac33e147 4999 assert(arg_size != UINT64_MAX);
252d6267
LP
5000 r = resize_backing_fd(
5001 node,
5002 &backing_fd,
5003 node_is_our_loop ? arg_image : NULL,
5004 node_is_our_loop ? loop_device : NULL);
170c9823
LP
5005 if (r < 0)
5006 return r;
5007
5008 r = context_load_partition_table(context, node, &backing_fd);
5009 if (r < 0)
5010 return r;
5011 }
5012
e594a3b1
LP
5013 /* First try to fit new partitions in, dropping by priority until it fits */
5014 for (;;) {
14a4c4ed
LP
5015 uint64_t largest_free_area;
5016
5017 if (context_allocate_partitions(context, &largest_free_area))
e594a3b1
LP
5018 break; /* Success! */
5019
d17db7b2
LP
5020 if (!context_drop_one_priority(context)) {
5021 r = log_error_errno(SYNTHETIC_ERRNO(ENOSPC),
14a4c4ed 5022 "Can't fit requested partitions into available free space (%s), refusing.",
2b59bf51 5023 FORMAT_BYTES(largest_free_area));
d17db7b2
LP
5024 determine_auto_size(context);
5025 return r;
5026 }
e594a3b1
LP
5027 }
5028
5029 /* Now assign free space according to the weight logic */
5030 r = context_grow_partitions(context);
5031 if (r < 0)
5032 return r;
5033
0b7f574f 5034 /* Now calculate where each new partition gets placed */
e594a3b1
LP
5035 context_place_partitions(context);
5036
5037 /* Make sure each partition has a unique UUID and unique label */
5038 r = context_acquire_partition_uuids_and_labels(context);
5039 if (r < 0)
5040 return r;
5041
a26d463d
DDM
5042 context_dump(context, node);
5043
e594a3b1
LP
5044 r = context_write_partition_table(context, node, from_scratch);
5045 if (r < 0)
5046 return r;
5047
5048 return 0;
5049}
5050
5051DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);