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