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