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