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