forked from systemd/systemd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepart.c
4234 lines (3286 loc) · 154 KB
/
repart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#if HAVE_VALGRIND_MEMCHECK_H
#include <valgrind/memcheck.h>
#endif
#include <fcntl.h>
#include <getopt.h>
#include <libfdisk.h>
#include <linux/fs.h>
#include <linux/loop.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <openssl/hmac.h>
#include <openssl/sha.h>
#include "sd-id128.h"
#include "alloc-util.h"
#include "blkid-util.h"
#include "blockdev-util.h"
#include "btrfs-util.h"
#include "conf-files.h"
#include "conf-parser.h"
#include "cryptsetup-util.h"
#include "def.h"
#include "efivars.h"
#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-table.h"
#include "format-util.h"
#include "fs-util.h"
#include "gpt.h"
#include "hexdecoct.h"
#include "id128-util.h"
#include "json.h"
#include "list.h"
#include "locale-util.h"
#include "loop-util.h"
#include "main-func.h"
#include "mkdir.h"
#include "mkfs-util.h"
#include "mount-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "pretty-print.h"
#include "proc-cmdline.h"
#include "process-util.h"
#include "random-util.h"
#include "resize-fs.h"
#include "sort-util.h"
#include "specifier.h"
#include "stat-util.h"
#include "stdio-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "tpm2-util.h"
#include "user-util.h"
#include "utf8.h"
/* If not configured otherwise use a minimal partition size of 10M */
#define DEFAULT_MIN_SIZE (10*1024*1024)
/* Hard lower limit for new partition sizes */
#define HARD_MIN_SIZE 4096
/* libfdisk takes off slightly more than 1M of the disk size when creating a GPT disk label */
#define GPT_METADATA_SIZE (1044*1024)
/* LUKS2 takes off 16M of the partition size with its metadata by default */
#define LUKS2_METADATA_SIZE (16*1024*1024)
#if !HAVE_LIBCRYPTSETUP
struct crypt_device;
static inline void sym_crypt_free(struct crypt_device* cd) {}
static inline void sym_crypt_freep(struct crypt_device** cd) {}
#endif
/* Note: When growing and placing new partitions we always align to 4K sector size. It's how newer hard disks
* are designed, and if everything is aligned to that performance is best. And for older hard disks with 512B
* sector size devices were generally assumed to have an even number of sectors, hence at the worst we'll
* waste 3K per partition, which is probably fine. */
static enum {
EMPTY_REFUSE, /* refuse empty disks, never create a partition table */
EMPTY_ALLOW, /* allow empty disks, create partition table if necessary */
EMPTY_REQUIRE, /* require an empty disk, create a partition table */
EMPTY_FORCE, /* make disk empty, erase everything, create a partition table always */
EMPTY_CREATE, /* create disk as loopback file, create a partition table always */
} arg_empty = EMPTY_REFUSE;
static bool arg_dry_run = true;
static const char *arg_node = NULL;
static char *arg_root = NULL;
static char *arg_definitions = NULL;
static bool arg_discard = true;
static bool arg_can_factory_reset = false;
static int arg_factory_reset = -1;
static sd_id128_t arg_seed = SD_ID128_NULL;
static bool arg_randomize = false;
static int arg_pretty = -1;
static uint64_t arg_size = UINT64_MAX;
static bool arg_size_auto = false;
static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
static PagerFlags arg_pager_flags = 0;
static bool arg_legend = true;
static void *arg_key = NULL;
static size_t arg_key_size = 0;
static char *arg_tpm2_device = NULL;
static uint32_t arg_tpm2_pcr_mask = UINT32_MAX;
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
STATIC_DESTRUCTOR_REGISTER(arg_definitions, freep);
STATIC_DESTRUCTOR_REGISTER(arg_key, erase_and_freep);
STATIC_DESTRUCTOR_REGISTER(arg_tpm2_device, freep);
typedef struct Partition Partition;
typedef struct FreeArea FreeArea;
typedef struct Context Context;
typedef enum EncryptMode {
ENCRYPT_OFF,
ENCRYPT_KEY_FILE,
ENCRYPT_TPM2,
ENCRYPT_KEY_FILE_TPM2,
_ENCRYPT_MODE_MAX,
_ENCRYPT_MODE_INVALID = -1,
} EncryptMode;
struct Partition {
char *definition_path;
sd_id128_t type_uuid;
sd_id128_t current_uuid, new_uuid;
char *current_label, *new_label;
bool dropped;
bool factory_reset;
int32_t priority;
uint32_t weight, padding_weight;
uint64_t current_size, new_size;
uint64_t size_min, size_max;
uint64_t current_padding, new_padding;
uint64_t padding_min, padding_max;
uint64_t partno;
uint64_t offset;
struct fdisk_partition *current_partition;
struct fdisk_partition *new_partition;
FreeArea *padding_area;
FreeArea *allocated_to_area;
char *copy_blocks_path;
int copy_blocks_fd;
uint64_t copy_blocks_size;
char *format;
char **copy_files;
EncryptMode encrypt;
LIST_FIELDS(Partition, partitions);
};
#define PARTITION_IS_FOREIGN(p) (!(p)->definition_path)
#define PARTITION_EXISTS(p) (!!(p)->current_partition)
struct FreeArea {
Partition *after;
uint64_t size;
uint64_t allocated;
};
struct Context {
LIST_HEAD(Partition, partitions);
size_t n_partitions;
FreeArea **free_areas;
size_t n_free_areas, n_allocated_free_areas;
uint64_t start, end, total;
struct fdisk_context *fdisk_context;
sd_id128_t seed;
};
static const char *encrypt_mode_table[_ENCRYPT_MODE_MAX] = {
[ENCRYPT_OFF] = "off",
[ENCRYPT_KEY_FILE] = "key-file",
[ENCRYPT_TPM2] = "tpm2",
[ENCRYPT_KEY_FILE_TPM2] = "key-file+tpm2",
};
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(encrypt_mode, EncryptMode, ENCRYPT_KEY_FILE);
static uint64_t round_down_size(uint64_t v, uint64_t p) {
return (v / p) * p;
}
static uint64_t round_up_size(uint64_t v, uint64_t p) {
v = DIV_ROUND_UP(v, p);
if (v > UINT64_MAX / p)
return UINT64_MAX; /* overflow */
return v * p;
}
static Partition *partition_new(void) {
Partition *p;
p = new(Partition, 1);
if (!p)
return NULL;
*p = (Partition) {
.weight = 1000,
.padding_weight = 0,
.current_size = UINT64_MAX,
.new_size = UINT64_MAX,
.size_min = UINT64_MAX,
.size_max = UINT64_MAX,
.current_padding = UINT64_MAX,
.new_padding = UINT64_MAX,
.padding_min = UINT64_MAX,
.padding_max = UINT64_MAX,
.partno = UINT64_MAX,
.offset = UINT64_MAX,
.copy_blocks_fd = -1,
.copy_blocks_size = UINT64_MAX,
};
return p;
}
static Partition* partition_free(Partition *p) {
if (!p)
return NULL;
free(p->current_label);
free(p->new_label);
free(p->definition_path);
if (p->current_partition)
fdisk_unref_partition(p->current_partition);
if (p->new_partition)
fdisk_unref_partition(p->new_partition);
free(p->copy_blocks_path);
safe_close(p->copy_blocks_fd);
free(p->format);
strv_free(p->copy_files);
return mfree(p);
}
static Partition* partition_unlink_and_free(Context *context, Partition *p) {
if (!p)
return NULL;
LIST_REMOVE(partitions, context->partitions, p);
assert(context->n_partitions > 0);
context->n_partitions--;
return partition_free(p);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(Partition*, partition_free);
static Context *context_new(sd_id128_t seed) {
Context *context;
context = new(Context, 1);
if (!context)
return NULL;
*context = (Context) {
.start = UINT64_MAX,
.end = UINT64_MAX,
.total = UINT64_MAX,
.seed = seed,
};
return context;
}
static void context_free_free_areas(Context *context) {
assert(context);
for (size_t i = 0; i < context->n_free_areas; i++)
free(context->free_areas[i]);
context->free_areas = mfree(context->free_areas);
context->n_free_areas = 0;
context->n_allocated_free_areas = 0;
}
static Context *context_free(Context *context) {
if (!context)
return NULL;
while (context->partitions)
partition_unlink_and_free(context, context->partitions);
assert(context->n_partitions == 0);
context_free_free_areas(context);
if (context->fdisk_context)
fdisk_unref_context(context->fdisk_context);
return mfree(context);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(Context*, context_free);
static int context_add_free_area(
Context *context,
uint64_t size,
Partition *after) {
FreeArea *a;
assert(context);
assert(!after || !after->padding_area);
if (!GREEDY_REALLOC(context->free_areas, context->n_allocated_free_areas, context->n_free_areas + 1))
return -ENOMEM;
a = new(FreeArea, 1);
if (!a)
return -ENOMEM;
*a = (FreeArea) {
.size = size,
.after = after,
};
context->free_areas[context->n_free_areas++] = a;
if (after)
after->padding_area = a;
return 0;
}
static bool context_drop_one_priority(Context *context) {
int32_t priority = 0;
Partition *p;
bool exists = false;
LIST_FOREACH(partitions, p, context->partitions) {
if (p->dropped)
continue;
if (p->priority < priority)
continue;
if (p->priority == priority) {
exists = exists || PARTITION_EXISTS(p);
continue;
}
priority = p->priority;
exists = PARTITION_EXISTS(p);
}
/* Refuse to drop partitions with 0 or negative priorities or partitions of priorities that have at
* least one existing priority */
if (priority <= 0 || exists)
return false;
LIST_FOREACH(partitions, p, context->partitions) {
if (p->priority < priority)
continue;
if (p->dropped)
continue;
p->dropped = true;
log_info("Can't fit partition %s of priority %" PRIi32 ", dropping.", p->definition_path, p->priority);
}
return true;
}
static uint64_t partition_min_size(const Partition *p) {
uint64_t sz;
/* Calculate the disk space we really need at minimum for this partition. If the partition already
* exists the current size is what we really need. If it doesn't exist yet refuse to allocate less
* than 4K.
*
* DEFAULT_MIN_SIZE is the default SizeMin= we configure if nothing else is specified. */
if (PARTITION_IS_FOREIGN(p)) {
/* Don't allow changing size of partitions not managed by us */
assert(p->current_size != UINT64_MAX);
return p->current_size;
}
sz = p->current_size != UINT64_MAX ? p->current_size : HARD_MIN_SIZE;
if (!PARTITION_EXISTS(p)) {
uint64_t d = 0;
if (p->encrypt != ENCRYPT_OFF)
d += round_up_size(LUKS2_METADATA_SIZE, 4096);
if (p->copy_blocks_size != UINT64_MAX)
d += round_up_size(p->copy_blocks_size, 4096);
else if (p->format || p->encrypt != ENCRYPT_OFF) {
uint64_t f;
/* If we shall synthesize a file system, take minimal fs size into account (assumed to be 4K if not known) */
f = p->format ? minimal_size_by_fs_name(p->format) : UINT64_MAX;
d += f == UINT64_MAX ? 4096 : f;
}
if (d > sz)
sz = d;
}
return MAX(p->size_min != UINT64_MAX ? p->size_min : DEFAULT_MIN_SIZE, sz);
}
static uint64_t partition_max_size(const Partition *p) {
/* Calculate how large the partition may become at max. This is generally the configured maximum
* size, except when it already exists and is larger than that. In that case it's the existing size,
* since we never want to shrink partitions. */
if (PARTITION_IS_FOREIGN(p)) {
/* Don't allow changing size of partitions not managed by us */
assert(p->current_size != UINT64_MAX);
return p->current_size;
}
if (p->current_size != UINT64_MAX)
return MAX(p->current_size, p->size_max);
return p->size_max;
}
static uint64_t partition_min_size_with_padding(const Partition *p) {
uint64_t sz;
/* Calculate the disk space we need for this partition plus any free space coming after it. This
* takes user configured padding into account as well as any additional whitespace needed to align
* the next partition to 4K again. */
sz = partition_min_size(p);
if (p->padding_min != UINT64_MAX)
sz += p->padding_min;
if (PARTITION_EXISTS(p)) {
/* If the partition wasn't aligned, add extra space so that any we might add will be aligned */
assert(p->offset != UINT64_MAX);
return round_up_size(p->offset + sz, 4096) - p->offset;
}
/* If this is a new partition we'll place it aligned, hence we just need to round up the required size here */
return round_up_size(sz, 4096);
}
static uint64_t free_area_available(const FreeArea *a) {
assert(a);
/* Determines how much of this free area is not allocated yet */
assert(a->size >= a->allocated);
return a->size - a->allocated;
}
static uint64_t free_area_available_for_new_partitions(const FreeArea *a) {
uint64_t avail;
/* Similar to free_area_available(), but takes into account that the required size and padding of the
* preceding partition is honoured. */
avail = free_area_available(a);
if (a->after) {
uint64_t need, space;
need = partition_min_size_with_padding(a->after);
assert(a->after->offset != UINT64_MAX);
assert(a->after->current_size != UINT64_MAX);
space = round_up_size(a->after->offset + a->after->current_size, 4096) - a->after->offset + avail;
if (need >= space)
return 0;
return space - need;
}
return avail;
}
static int free_area_compare(FreeArea *const *a, FreeArea *const*b) {
return CMP(free_area_available_for_new_partitions(*a),
free_area_available_for_new_partitions(*b));
}
static uint64_t charge_size(uint64_t total, uint64_t amount) {
uint64_t rounded;
assert(amount <= total);
/* Subtract the specified amount from total, rounding up to multiple of 4K if there's room */
rounded = round_up_size(amount, 4096);
if (rounded >= total)
return 0;
return total - rounded;
}
static uint64_t charge_weight(uint64_t total, uint64_t amount) {
assert(amount <= total);
return total - amount;
}
static bool context_allocate_partitions(Context *context) {
Partition *p;
assert(context);
/* A simple first-fit algorithm, assuming the array of free areas is sorted by size in decreasing
* order. */
LIST_FOREACH(partitions, p, context->partitions) {
bool fits = false;
uint64_t required;
FreeArea *a = NULL;
/* Skip partitions we already dropped or that already exist */
if (p->dropped || PARTITION_EXISTS(p))
continue;
/* Sort by size */
typesafe_qsort(context->free_areas, context->n_free_areas, free_area_compare);
/* How much do we need to fit? */
required = partition_min_size_with_padding(p);
assert(required % 4096 == 0);
for (size_t i = 0; i < context->n_free_areas; i++) {
a = context->free_areas[i];
if (free_area_available_for_new_partitions(a) >= required) {
fits = true;
break;
}
}
if (!fits)
return false; /* 😢 Oh no! We can't fit this partition into any free area! */
/* Assign the partition to this free area */
p->allocated_to_area = a;
/* Budget the minimal partition size */
a->allocated += required;
}
return true;
}
static int context_sum_weights(Context *context, FreeArea *a, uint64_t *ret) {
uint64_t weight_sum = 0;
Partition *p;
assert(context);
assert(a);
assert(ret);
/* Determine the sum of the weights of all partitions placed in or before the specified free area */
LIST_FOREACH(partitions, p, context->partitions) {
if (p->padding_area != a && p->allocated_to_area != a)
continue;
if (p->weight > UINT64_MAX - weight_sum)
goto overflow_sum;
weight_sum += p->weight;
if (p->padding_weight > UINT64_MAX - weight_sum)
goto overflow_sum;
weight_sum += p->padding_weight;
}
*ret = weight_sum;
return 0;
overflow_sum:
return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Combined weight of partition exceeds unsigned 64bit range, refusing.");
}
static int scale_by_weight(uint64_t value, uint64_t weight, uint64_t weight_sum, uint64_t *ret) {
assert(weight_sum >= weight);
assert(ret);
if (weight == 0) {
*ret = 0;
return 0;
}
if (value > UINT64_MAX / weight)
return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "Scaling by weight of partition exceeds unsigned 64bit range, refusing.");
*ret = value * weight / weight_sum;
return 0;
}
typedef enum GrowPartitionPhase {
/* The first phase: we charge partitions which need more (according to constraints) than their weight-based share. */
PHASE_OVERCHARGE,
/* The second phase: we charge partitions which need less (according to constraints) than their weight-based share. */
PHASE_UNDERCHARGE,
/* The third phase: we distribute what remains among the remaining partitions, according to the weights */
PHASE_DISTRIBUTE,
} GrowPartitionPhase;
static int context_grow_partitions_phase(
Context *context,
FreeArea *a,
GrowPartitionPhase phase,
uint64_t *span,
uint64_t *weight_sum) {
Partition *p;
int r;
assert(context);
assert(a);
/* Now let's look at the intended weights and adjust them taking the minimum space assignments into
* account. i.e. if a partition has a small weight but a high minimum space value set it should not
* get any additional room from the left-overs. Similar, if two partitions have the same weight they
* should get the same space if possible, even if one has a smaller minimum size than the other. */
LIST_FOREACH(partitions, p, context->partitions) {
/* Look only at partitions associated with this free area, i.e. immediately
* preceding it, or allocated into it */
if (p->allocated_to_area != a && p->padding_area != a)
continue;
if (p->new_size == UINT64_MAX) {
bool charge = false, try_again = false;
uint64_t share, rsz, xsz;
/* Calculate how much this space this partition needs if everyone would get
* the weight based share */
r = scale_by_weight(*span, p->weight, *weight_sum, &share);
if (r < 0)
return r;
rsz = partition_min_size(p);
xsz = partition_max_size(p);
if (phase == PHASE_OVERCHARGE && rsz > share) {
/* This partition needs more than its calculated share. Let's assign
* it that, and take this partition out of all calculations and start
* again. */
p->new_size = rsz;
charge = try_again = true;
} else if (phase == PHASE_UNDERCHARGE && xsz != UINT64_MAX && xsz < share) {
/* This partition accepts less than its calculated
* share. Let's assign it that, and take this partition out
* of all calculations and start again. */
p->new_size = xsz;
charge = try_again = true;
} else if (phase == PHASE_DISTRIBUTE) {
/* This partition can accept its calculated share. Let's
* assign it. There's no need to restart things here since
* assigning this shouldn't impact the shares of the other
* partitions. */
if (PARTITION_IS_FOREIGN(p))
/* Never change of foreign partitions (i.e. those we don't manage) */
p->new_size = p->current_size;
else
p->new_size = MAX(round_down_size(share, 4096), rsz);
charge = true;
}
if (charge) {
*span = charge_size(*span, p->new_size);
*weight_sum = charge_weight(*weight_sum, p->weight);
}
if (try_again)
return 0; /* try again */
}
if (p->new_padding == UINT64_MAX) {
bool charge = false, try_again = false;
uint64_t share;
r = scale_by_weight(*span, p->padding_weight, *weight_sum, &share);
if (r < 0)
return r;
if (phase == PHASE_OVERCHARGE && p->padding_min != UINT64_MAX && p->padding_min > share) {
p->new_padding = p->padding_min;
charge = try_again = true;
} else if (phase == PHASE_UNDERCHARGE && p->padding_max != UINT64_MAX && p->padding_max < share) {
p->new_padding = p->padding_max;
charge = try_again = true;
} else if (phase == PHASE_DISTRIBUTE) {
p->new_padding = round_down_size(share, 4096);
if (p->padding_min != UINT64_MAX && p->new_padding < p->padding_min)
p->new_padding = p->padding_min;
charge = true;
}
if (charge) {
*span = charge_size(*span, p->new_padding);
*weight_sum = charge_weight(*weight_sum, p->padding_weight);
}
if (try_again)
return 0; /* try again */
}
}
return 1; /* done */
}
static int context_grow_partitions_on_free_area(Context *context, FreeArea *a) {
uint64_t weight_sum = 0, span;
int r;
assert(context);
assert(a);
r = context_sum_weights(context, a, &weight_sum);
if (r < 0)
return r;
/* Let's calculate the total area covered by this free area and the partition before it */
span = a->size;
if (a->after) {
assert(a->after->offset != UINT64_MAX);
assert(a->after->current_size != UINT64_MAX);
span += round_up_size(a->after->offset + a->after->current_size, 4096) - a->after->offset;
}
GrowPartitionPhase phase = PHASE_OVERCHARGE;
for (;;) {
r = context_grow_partitions_phase(context, a, phase, &span, &weight_sum);
if (r < 0)
return r;
if (r == 0) /* not done yet, re-run this phase */
continue;
if (phase == PHASE_OVERCHARGE)
phase = PHASE_UNDERCHARGE;
else if (phase == PHASE_UNDERCHARGE)
phase = PHASE_DISTRIBUTE;
else if (phase == PHASE_DISTRIBUTE)
break;
}
/* We still have space left over? Donate to preceding partition if we have one */
if (span > 0 && a->after && !PARTITION_IS_FOREIGN(a->after)) {
uint64_t m, xsz;
assert(a->after->new_size != UINT64_MAX);
m = a->after->new_size + span;
xsz = partition_max_size(a->after);
if (xsz != UINT64_MAX && m > xsz)
m = xsz;
span = charge_size(span, m - a->after->new_size);
a->after->new_size = m;
}
/* What? Even still some space left (maybe because there was no preceding partition, or it had a
* size limit), then let's donate it to whoever wants it. */
if (span > 0) {
Partition *p;
LIST_FOREACH(partitions, p, context->partitions) {
uint64_t m, xsz;
if (p->allocated_to_area != a)
continue;
if (PARTITION_IS_FOREIGN(p))
continue;
assert(p->new_size != UINT64_MAX);
m = p->new_size + span;
xsz = partition_max_size(p);
if (xsz != UINT64_MAX && m > xsz)
m = xsz;
span = charge_size(span, m - p->new_size);
p->new_size = m;
if (span == 0)
break;
}
}
/* Yuck, still no one? Then make it padding */
if (span > 0 && a->after) {
assert(a->after->new_padding != UINT64_MAX);
a->after->new_padding += span;
}
return 0;
}
static int context_grow_partitions(Context *context) {
Partition *p;
int r;
assert(context);
for (size_t i = 0; i < context->n_free_areas; i++) {
r = context_grow_partitions_on_free_area(context, context->free_areas[i]);
if (r < 0)
return r;
}
/* All existing partitions that have no free space after them can't change size */
LIST_FOREACH(partitions, p, context->partitions) {
if (p->dropped)
continue;
if (!PARTITION_EXISTS(p) || p->padding_area) {
/* The algorithm above must have initialized this already */
assert(p->new_size != UINT64_MAX);
continue;
}
assert(p->new_size == UINT64_MAX);
p->new_size = p->current_size;
assert(p->new_padding == UINT64_MAX);
p->new_padding = p->current_padding;
}
return 0;
}
static void context_place_partitions(Context *context) {
uint64_t partno = 0;
Partition *p;
assert(context);
/* Determine next partition number to assign */
LIST_FOREACH(partitions, p, context->partitions) {
if (!PARTITION_EXISTS(p))
continue;
assert(p->partno != UINT64_MAX);
if (p->partno >= partno)
partno = p->partno + 1;
}
for (size_t i = 0; i < context->n_free_areas; i++) {
FreeArea *a = context->free_areas[i];
uint64_t start, left;
if (a->after) {
assert(a->after->offset != UINT64_MAX);
assert(a->after->new_size != UINT64_MAX);
assert(a->after->new_padding != UINT64_MAX);
start = a->after->offset + a->after->new_size + a->after->new_padding;
} else
start = context->start;
start = round_up_size(start, 4096);
left = a->size;
LIST_FOREACH(partitions, p, context->partitions) {
if (p->allocated_to_area != a)
continue;
p->offset = start;
p->partno = partno++;
assert(left >= p->new_size);
start += p->new_size;
left -= p->new_size;
assert(left >= p->new_padding);
start += p->new_padding;
left -= p->new_padding;
}
}
}
static int config_parse_type(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
sd_id128_t *type_uuid = data;
int r;
assert(rvalue);
assert(type_uuid);
r = gpt_partition_type_uuid_from_string(rvalue, type_uuid);
if (r < 0)
return log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse partition type: %s", rvalue);
return 0;
}
static const Specifier specifier_table[] = {
COMMON_SYSTEM_SPECIFIERS,
{}
};
static int config_parse_label(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
_cleanup_free_ char16_t *recoded = NULL;
_cleanup_free_ char *resolved = NULL;
char **label = data;
int r;
assert(rvalue);
assert(label);
r = specifier_printf(rvalue, specifier_table, NULL, &resolved);
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to expand specifiers in Label=, ignoring: %s", rvalue);
return 0;
}
if (!utf8_is_valid(resolved)) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Partition label not valid UTF-8, ignoring: %s", rvalue);
return 0;
}
recoded = utf8_to_utf16(resolved, strlen(resolved));
if (!recoded)
return log_oom();
if (char16_strlen(recoded) > 36) {
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Partition label too long for GPT table, ignoring: \"%s\" (from \"%s\")",
resolved, rvalue);
return 0;
}
free_and_replace(*label, resolved);
return 0;
}
static int config_parse_weight(
const char *unit,
const char *filename,