plan.c (18272B)
1 /* see LICENSE file for copyright and license details */ 2 #include "plan.h" 3 #include "../util.h" 4 5 #include <ctype.h> 6 #include <errno.h> 7 #include <stdlib.h> 8 #include <time.h> 9 #include <unistd.h> 10 #include <string.h> 11 #include <strings.h> 12 13 static const struct plan_fs filesystems[] = { 14 {"ext4", "ext4", "ext4", 1, 16}, 15 {"f2fs", "f2fs", "f2fs", 1, 512}, 16 {"vfat", "FAT32 (vfat)", "vfat", 0, 11}, 17 {"swap", "swap", NULL, 0, 15}, 18 }; 19 #define NFS ((int)(sizeof filesystems / sizeof filesystems[0])) 20 21 const struct plan_fs * 22 plan_filesystems(int *n) 23 { 24 *n = NFS; 25 return filesystems; 26 } 27 28 const struct plan_fs * 29 plan_filesystem(const char *name) 30 { 31 int i; 32 33 for (i = 0; i < NFS; i++) 34 if (name && strcmp(name, filesystems[i].name) == 0) 35 return &filesystems[i]; 36 return NULL; 37 } 38 39 void 40 plan_part_path(const char *disk, int slot, char *buf, size_t n) 41 { 42 size_t len = strlen(disk); 43 44 /* a name that ends in a digit takes a p before the number: nvme0n1p1, mmcblk0p1 */ 45 snprintf( 46 buf, n, "%s%s%d", disk, len && isdigit((unsigned char)disk[len - 1]) ? "p" : "", slot + 1 47 ); 48 } 49 50 void 51 plan_size(uint64_t bytes, char *buf, size_t n) 52 { 53 static const char *units[] = {"B", "KiB", "MiB", "GiB", "TiB"}; 54 double v = (double)bytes; 55 int u = 0; 56 57 while (v >= 1024 && u < 4) { 58 v /= 1024; 59 u++; 60 } 61 if (u == 0 || v >= 100) 62 snprintf(buf, n, "%.0f %s", v, units[u]); 63 else 64 snprintf(buf, n, "%.1f %s", v, units[u]); 65 } 66 67 int 68 plan_read_disk(struct plan *p, const char *disk) 69 { 70 struct BlockDev dev; 71 int i; 72 73 memset(p, 0, sizeof *p); 74 snprintf(p->disk, sizeof p->disk, "%s", disk); 75 if (blockdev_open(&dev, disk, 0) < 0) 76 return -1; 77 if (label_read(&dev, &p->label) < 0) { 78 blockdev_close(&dev); 79 return -1; 80 } 81 if (p->label.kind == LABEL_NONE) 82 label_init(&p->label, &dev, LABEL_NONE); 83 blockdev_close(&dev); 84 for (i = 0; i < p->label.slots; i++) { 85 struct BlockDev part; 86 char path[PLAN_PATH + 8], type[16] = "", label[64] = "", uuid[64] = ""; 87 88 if (!p->label.part[i].used) 89 continue; 90 if (strcmp(p->label.part[i].type, "8200") == 0) 91 snprintf(p->attr[i].found, PLAN_FS, "swap"); 92 plan_part_path(disk, i, path, sizeof path); 93 if (blockdev_open(&part, path, 0) < 0) 94 continue; 95 if (blockdev_detect_fs(&part, type, sizeof type, label, sizeof label, uuid, sizeof uuid) == 0) { 96 /* the detector calls ext2, 3 and 4 alike */ 97 snprintf(p->attr[i].found, PLAN_FS, "%s", strcmp(type, "ext") == 0 ? "ext4" : type); 98 snprintf(p->attr[i].label, PLAN_LABEL, "%.*s", PLAN_LABEL - 1, label); 99 } 100 blockdev_close(&part); 101 } 102 return 0; 103 } 104 105 int 106 plan_set(struct plan *p, int slot, const char *fs, const char *mount, const char *label, int format) 107 { 108 struct plan_part *a; 109 const struct plan_fs *f; 110 111 if (slot < 0 || slot >= p->label.slots || !p->label.part[slot].used) 112 return -EINVAL; 113 f = fs && fs[0] ? plan_filesystem(fs) : NULL; 114 if (fs && fs[0] && !f) 115 return -EINVAL; 116 if (label && f && strlen(label) > (size_t)f->max_label) 117 return -EINVAL; 118 a = &p->attr[slot]; 119 snprintf(a->fs, sizeof a->fs, "%s", fs ? fs : ""); 120 snprintf(a->mount, sizeof a->mount, "%s", mount ? mount : ""); 121 snprintf(a->label, sizeof a->label, "%s", label ? label : ""); 122 a->format = f ? format : 0; 123 return 0; 124 } 125 126 int 127 plan_add( 128 struct plan *p, 129 uint64_t start, 130 uint64_t sectors, 131 const char *type, 132 const char *name, 133 const char *fs, 134 const char *mount, 135 const char *label 136 ) 137 { 138 int slot = label_add(&p->label, start, sectors, type, name); 139 140 if (slot < 0) 141 return slot; 142 memset(&p->attr[slot], 0, sizeof p->attr[slot]); 143 /* a new partition's filesystem is made: nothing is on it */ 144 if (plan_set(p, slot, fs, mount, label, 1) < 0) { 145 label_delete(&p->label, slot); 146 return -EINVAL; 147 } 148 return slot; 149 } 150 151 int 152 plan_delete(struct plan *p, int slot) 153 { 154 int rc = label_delete(&p->label, slot); 155 156 if (rc == 0) 157 memset(&p->attr[slot], 0, sizeof p->attr[slot]); 158 return rc; 159 } 160 161 static int 162 fail(char *why, size_t n, const char *fmt, const char *a) 163 { 164 snprintf(why, n, fmt, a ? a : ""); 165 return -1; 166 } 167 168 int 169 plan_check(const struct plan *p, unsigned need, char *why, size_t n) 170 { 171 int i, j, root = 0, esp = 0, bios = 0; 172 173 for (i = 0; i < p->label.slots; i++) { 174 const struct plan_part *a = &p->attr[i]; 175 const struct label_part *l = &p->label.part[i]; 176 const struct plan_fs *f = plan_filesystem(a->fs); 177 178 if (!l->used) 179 continue; 180 if (a->format && strcmp(a->fs, "ext4") == 0) { 181 uint64_t sectors = l->end - l->start + 1; 182 if (!p->label.sec_size || MKFS_EXT4_BLOCK_SIZE % p->label.sec_size 183 || sectors > UINT64_MAX / p->label.sec_size 184 || !mkfs_ext4_size_ok(sectors * p->label.sec_size)) 185 return fail(why, n, "%s", "ext4 needs 8 MiB to below 16 TiB and sectors dividing 4 KiB"); 186 } 187 if (a->mount[0] && a->mount[0] != '/') 188 return fail(why, n, "%s is not a place to mount: it starts with a /", a->mount); 189 if (a->mount[0] && !a->fs[0]) 190 return fail(why, n, "%s needs a filesystem", a->mount); 191 if (a->mount[0] && f && !f->mount_as) 192 return fail(why, n, "%s cannot be mounted: it is swap", a->mount); 193 for (j = i + 1; j < p->label.slots; j++) 194 if (p->label.part[j].used && a->mount[0] && strcmp(a->mount, p->attr[j].mount) == 0) 195 return fail(why, n, "two partitions are mounted at %s", a->mount); 196 if (strcmp(a->mount, "/") == 0) { 197 if (!f || !f->bootable) 198 return fail(why, n, "%s", "the system cannot start from the filesystem at /"); 199 root = 1; 200 } 201 if (strcmp(a->mount, "/boot/efi") == 0 && strcmp(l->type, "ef00") == 0 202 && strcmp(a->fs, "vfat") == 0) 203 esp = 1; 204 if (strcmp(l->type, "ef02") == 0) 205 bios = 1; 206 } 207 if ((need & PLAN_NEED_ROOT) && !root) 208 return fail(why, n, "%s", "no partition is mounted at /"); 209 if ((need & PLAN_NEED_BOOT) && !esp && !bios) 210 return fail( 211 why, 212 n, 213 "%s", 214 "the machine needs a way to start: an EFI system partition mounted at /boot/efi, or a BIOS " 215 "boot partition" 216 ); 217 return 0; 218 } 219 220 int 221 plan_changes( 222 const struct plan *before, 223 const struct plan *now, 224 void (*fn)(const char *line, void *arg), 225 void *arg 226 ) 227 { 228 char line[256], dev[PLAN_PATH + 8], size[32]; 229 int i, n = 0; 230 231 struct plan *none = NULL; 232 233 /* a table of another kind replaces the one there: all it held is gone, all the new one holds is 234 * made */ 235 if (before->label.kind != now->label.kind) { 236 if (before->label.kind != LABEL_NONE) { 237 snprintf( 238 line, 239 sizeof line, 240 "write a new %s partition table on %s, every partition on it is lost", 241 now->label.kind == LABEL_GPT ? "GPT" : "MBR", 242 now->disk 243 ); 244 if (fn) 245 fn(line, arg); 246 n++; 247 } 248 if (!(none = calloc(1, sizeof *none))) 249 return n; 250 none->label.slots = now->label.slots; 251 before = none; 252 } 253 for (i = 0; i < now->label.slots; i++) { 254 const struct label_part *a = &before->label.part[i], *b = &now->label.part[i]; 255 const struct plan_part *x = &before->attr[i], *y = &now->attr[i]; 256 int same = a->used && b->used && a->start == b->start && a->end == b->end 257 && strcmp(a->type, b->type) == 0; 258 259 plan_part_path(now->disk, i, dev, sizeof dev); 260 if (a->used && !same) { 261 snprintf(line, sizeof line, "delete %s (%s)", dev, label_type_name(a)); 262 if (fn) 263 fn(line, arg); 264 n++; 265 } 266 if (b->used && !same) { 267 plan_size((b->end - b->start + 1) * now->label.sec_size, size, sizeof size); 268 snprintf(line, sizeof line, "create %s, %s", dev, size); 269 if (fn) 270 fn(line, arg); 271 n++; 272 } 273 if (b->used && y->format && y->fs[0]) { 274 snprintf( 275 line, 276 sizeof line, 277 "format %s as %s%s%s", 278 dev, 279 y->fs, 280 y->label[0] ? " labelled " : "", 281 y->label 282 ); 283 if (fn) 284 fn(line, arg); 285 n++; 286 } else if (b->used && same && strcmp(x->mount, y->mount) != 0 && y->mount[0]) { 287 snprintf(line, sizeof line, "use %s at %s", dev, y->mount); 288 if (fn) 289 fn(line, arg); 290 n++; 291 } 292 } 293 free(none); 294 return n; 295 } 296 297 int 298 plan_write_table(const struct plan *p) 299 { 300 struct BlockDev dev; 301 int rc; 302 303 if (blockdev_open(&dev, p->disk, 1) < 0) 304 return -1; 305 rc = label_write(&dev, &p->label); 306 blockdev_close(&dev); 307 return rc; 308 } 309 310 /* the words of the command that makes a filesystem, each with its place in store */ 311 static int 312 put(char **argv, int *n, int max, char *store, size_t store_n, size_t *used, const char *word) 313 { 314 size_t len = strlen(word) + 1; 315 316 if (*n + 1 >= max || *used + len > store_n) 317 return -1; 318 memcpy(store + *used, word, len); 319 argv[(*n)++] = store + *used; 320 *used += len; 321 argv[*n] = NULL; 322 return 0; 323 } 324 325 int 326 plan_mkfs_argv(const struct plan *p, int slot, char **argv, int max, char *store, size_t store_n) 327 { 328 const struct plan_part *a = &p->attr[slot]; 329 char dev[PLAN_PATH + 8]; 330 size_t used = 0; 331 int n = 0, rc = 0; 332 333 if (slot < 0 || slot >= p->label.slots || !p->label.part[slot].used || !a->fs[0]) 334 return -1; 335 plan_part_path(p->disk, slot, dev, sizeof dev); 336 if (max < 1) 337 return -1; 338 argv[0] = NULL; 339 if (strcmp(a->fs, "ext4") == 0) { 340 /* mkfs.ext4 generates a fresh UUID, even for filesystems with the same label. */ 341 rc |= put(argv, &n, max, store, store_n, &used, "mkfs.ext4"); 342 if (a->label[0]) { 343 rc |= put(argv, &n, max, store, store_n, &used, "-L"); 344 rc |= put(argv, &n, max, store, store_n, &used, a->label); 345 } 346 } else if (strcmp(a->fs, "f2fs") == 0) { 347 rc |= put(argv, &n, max, store, store_n, &used, "mkfs.f2fs"); 348 rc |= put(argv, &n, max, store, store_n, &used, "-f"); 349 if (a->label[0]) { 350 rc |= put(argv, &n, max, store, store_n, &used, "-l"); 351 rc |= put(argv, &n, max, store, store_n, &used, a->label); 352 } 353 } else if (strcmp(a->fs, "vfat") == 0) { 354 rc |= put(argv, &n, max, store, store_n, &used, "mkfs.fat"); 355 rc |= put(argv, &n, max, store, store_n, &used, "-F"); 356 rc |= put(argv, &n, max, store, store_n, &used, "32"); 357 if (a->label[0]) { 358 rc |= put(argv, &n, max, store, store_n, &used, "-n"); 359 rc |= put(argv, &n, max, store, store_n, &used, a->label); 360 } 361 } else if (strcmp(a->fs, "swap") == 0) { 362 rc |= put(argv, &n, max, store, store_n, &used, "mkswap"); 363 if (a->label[0]) { 364 rc |= put(argv, &n, max, store, store_n, &used, "-L"); 365 rc |= put(argv, &n, max, store, store_n, &used, a->label); 366 } 367 } else 368 return -1; 369 rc |= put(argv, &n, max, store, store_n, &used, dev); 370 return rc ? -1 : 0; 371 } 372 373 int 374 plan_save(const struct plan *p, FILE *f) 375 { 376 int i; 377 378 fprintf( 379 f, 380 "disk %s\nlabel %s\n", 381 p->disk, 382 p->label.kind == LABEL_GPT ? "gpt" 383 : p->label.kind == LABEL_MBR ? "mbr" 384 : "none" 385 ); 386 for (i = 0; i < p->label.slots; i++) { 387 const struct label_part *l = &p->label.part[i]; 388 const struct plan_part *a = &p->attr[i]; 389 390 if (!l->used) 391 continue; 392 fprintf( 393 f, 394 "part %d start=%llu sectors=%llu type=%s", 395 i + 1, 396 (unsigned long long)l->start, 397 (unsigned long long)(l->end - l->start + 1), 398 l->type[0] ? l->type : "8300" 399 ); 400 if (l->name[0]) 401 fprintf(f, " name=%s", l->name); 402 if (a->fs[0]) 403 fprintf(f, " fs=%s", a->fs); 404 if (a->mount[0]) 405 fprintf(f, " mount=%s", a->mount); 406 if (a->label[0]) 407 fprintf(f, " label=%s", a->label); 408 if (a->format) 409 fprintf(f, " format=yes"); 410 fputc('\n', f); 411 } 412 return ferror(f) ? -1 : 0; 413 } 414 415 static int 416 bad(char *why, size_t n, const char *line) 417 { 418 snprintf(why, n, "not a line of a plan: %.60s", line); 419 return -1; 420 } 421 422 int 423 plan_load(struct plan *p, FILE *f, char *why, size_t n) 424 { 425 char line[512]; 426 struct BlockDev dev; 427 428 memset(p, 0, sizeof *p); 429 p->label.slots = 0; 430 while (fgets(line, sizeof line, f)) { 431 char *nl = strchr(line, '\n'), *word, *save = NULL; 432 433 if (nl) 434 *nl = '\0'; 435 if (!line[0] || line[0] == '#') 436 continue; 437 if (strncmp(line, "disk ", 5) == 0) { 438 snprintf(p->disk, sizeof p->disk, "%s", line + 5); 439 } else if (strncmp(line, "label ", 6) == 0) { 440 enum label_kind kind = strcmp(line + 6, "gpt") == 0 ? LABEL_GPT 441 : strcmp(line + 6, "mbr") == 0 ? LABEL_MBR 442 : LABEL_NONE; 443 444 /* the table's geometry comes from the disk it is for */ 445 if (kind == LABEL_NONE || !p->disk[0] || blockdev_open(&dev, p->disk, 0) < 0 446 || label_init(&p->label, &dev, kind) < 0) 447 return bad(why, n, line); 448 blockdev_close(&dev); 449 } else if (strncmp(line, "part ", 5) == 0) { 450 uint64_t start = 0, sectors = 0; 451 char type[8] = "8300", name[40] = "", fs[PLAN_FS] = "", mount[PLAN_MOUNT] = "", 452 label[PLAN_LABEL] = ""; 453 int format = 0, index = (int)strtol(line + 5, &word, 10); 454 455 if (p->label.kind == LABEL_NONE || index < 1 || index > p->label.slots) 456 return bad(why, n, line); 457 for (word = strtok_r(word, " ", &save); word; word = strtok_r(NULL, " ", &save)) { 458 if (strncmp(word, "start=", 6) == 0) 459 start = strtoull(word + 6, NULL, 10); 460 else if (strncmp(word, "sectors=", 8) == 0) 461 sectors = strtoull(word + 8, NULL, 10); 462 else if (strncmp(word, "type=", 5) == 0) 463 snprintf(type, sizeof type, "%s", word + 5); 464 else if (strncmp(word, "name=", 5) == 0) 465 snprintf(name, sizeof name, "%s", word + 5); 466 else if (strncmp(word, "fs=", 3) == 0) 467 snprintf(fs, sizeof fs, "%s", word + 3); 468 else if (strncmp(word, "mount=", 6) == 0) 469 snprintf(mount, sizeof mount, "%s", word + 6); 470 else if (strncmp(word, "label=", 6) == 0) 471 snprintf(label, sizeof label, "%s", word + 6); 472 else if (strcmp(word, "format=yes") == 0) 473 format = 1; 474 else 475 return bad(why, n, line); 476 } 477 /* a plan names the slots, so a partition goes where it says */ 478 if (start == 0 || p->label.part[index - 1].used) 479 return bad(why, n, line); 480 { 481 int slot = label_add(&p->label, start, sectors, type, name); 482 483 if (slot < 0) 484 return bad(why, n, line); 485 if (slot != index - 1) { 486 p->label.part[index - 1] = p->label.part[slot]; 487 memset(&p->label.part[slot], 0, sizeof p->label.part[slot]); 488 slot = index - 1; 489 } 490 if (plan_set(p, slot, fs, mount, label, format) < 0) 491 return bad(why, n, line); 492 } 493 } else 494 return bad(why, n, line); 495 } 496 return p->disk[0] ? 0 : (snprintf(why, n, "a plan names a disk"), -1); 497 } 498 499 int 500 plan_guided(struct plan *p, const char *disk, uint64_t swap_mib, const char *root_fs) 501 { 502 struct BlockDev dev; 503 uint64_t mib; 504 char why[160]; 505 int rc = 0; 506 507 memset(p, 0, sizeof *p); 508 snprintf(p->disk, sizeof p->disk, "%s", disk); 509 if (!plan_filesystem(root_fs) || !plan_filesystem(root_fs)->bootable 510 || blockdev_open(&dev, disk, 0) < 0) 511 return -1; 512 if (label_init(&p->label, &dev, LABEL_GPT) < 0) { 513 blockdev_close(&dev); 514 return -1; 515 } 516 mib = 1048576 / dev.sec_size; 517 blockdev_close(&dev); 518 /* the BIOS boot partition first, then the system partition, which limine and the firmware read */ 519 rc |= plan_add(p, 0, PLAN_BIOS_MIB * mib, "ef02", "BIOS", "", "", "") < 0; 520 rc |= plan_add(p, 0, PLAN_ESP_MIB * mib, "ef00", "ESP", "vfat", "/boot/efi", "ESP") < 0; 521 if (swap_mib) 522 rc |= plan_add(p, 0, swap_mib * mib, "8200", "SWAP", "swap", "", "") < 0; 523 rc |= plan_add(p, 0, 0, "8300", "ROOT", root_fs, "/", "root") < 0; 524 if (rc) 525 return -1; 526 return plan_check(p, 0, why, sizeof why); 527 } 528 529 int 530 plan_role(const struct plan *p, const char *role, char *buf, size_t n) 531 { 532 int i; 533 534 for (i = 0; i < p->label.slots; i++) { 535 const struct label_part *l = &p->label.part[i]; 536 const struct plan_part *a = &p->attr[i]; 537 538 if (!l->used) 539 continue; 540 if (strcmp(role, "esp") == 0 && strcmp(a->mount, "/boot/efi") == 0) { 541 plan_part_path(p->disk, i, buf, n); 542 return 1; 543 } 544 if (strcmp(role, "root") == 0 && strcmp(a->mount, "/") == 0) { 545 plan_part_path(p->disk, i, buf, n); 546 return 1; 547 } 548 if (strcmp(role, "swap") == 0 && strcmp(a->fs, "swap") == 0) { 549 plan_part_path(p->disk, i, buf, n); 550 return 1; 551 } 552 if (strcmp(role, "bios") == 0 && strcmp(l->type, "ef02") == 0) { 553 snprintf(buf, n, "%d", i + 1); 554 return 1; 555 } 556 if (strcmp(role, "rootfs") == 0 && strcmp(a->mount, "/") == 0) { 557 snprintf(buf, n, "%s", a->fs); 558 return 1; 559 } 560 } 561 return 0; 562 } 563 564 int 565 plan_mount_order(const struct plan *p, int *slots, int max) 566 { 567 int i, j, n = 0; 568 569 for (i = 0; i < p->label.slots && n < max; i++) 570 if (p->label.part[i].used && p->attr[i].mount[0]) 571 slots[n++] = i; 572 /* fewest path parts first: a mount point is made inside the ones above it */ 573 for (i = 1; i < n; i++) { 574 int s = slots[i], k; 575 576 for (j = i - 1; j >= 0; j--) { 577 int a = 0, b = 0; 578 579 for (k = 0; p->attr[slots[j]].mount[k]; k++) 580 a += p->attr[slots[j]].mount[k] == '/' && p->attr[slots[j]].mount[k + 1]; 581 for (k = 0; p->attr[s].mount[k]; k++) 582 b += p->attr[s].mount[k] == '/' && p->attr[s].mount[k + 1]; 583 if (a <= b) 584 break; 585 slots[j + 1] = slots[j]; 586 } 587 slots[j + 1] = s; 588 } 589 return n; 590 } 591 592 int 593 plan_wait_nodes(const struct plan *p, int tenths) 594 { 595 int t, i; 596 597 for (t = 0; t <= tenths; t++) { 598 int all = 1; 599 600 for (i = 0; i < p->label.slots; i++) { 601 char path[PLAN_PATH + 8]; 602 603 if (!p->label.part[i].used) 604 continue; 605 plan_part_path(p->disk, i, path, sizeof path); 606 if (access(path, F_OK) != 0) 607 all = 0; 608 } 609 if (all) 610 return 0; 611 nanosleep(&(struct timespec){0, 100000000L}, NULL); 612 } 613 return -1; 614 }