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