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