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