partplan.c (19274B)
1 /* see LICENSE file for copyright and license details */ 2 #include "libdisk/gpt.h" 3 #include "libdisk/plan.h" 4 #include "arg.h" 5 #include "util.h" 6 7 #include <limits.h> 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 #include <sys/wait.h> 12 #include <unistd.h> 13 14 #define SECTORS_ZEROED \ 15 16384 /* 8 MiB at 512 byte sectors: old signatures that would be seen again */ 16 17 static void __attribute__((noreturn)) 18 usage(void) 19 { 20 eprintf( 21 "usage: %s guided [-s swap_mib] [-f root_fs] disk | disks | layout | read disk | new [-m] " 22 "disk |\n" 23 " show plan | gaps plan | changes plan | check [-rb] plan | write plan | mkfs plan | " 24 "mounts plan |\n" 25 " fstab plan | get plan esp|root|swap|bios|rootfs|rootuuid |\n" 26 " add [-t type] [-l label] plan size fs [mount] | delete plan n | set [-k] [-l label] " 27 "plan n fs [mount]\n", 28 argv0 29 ); 30 } 31 32 static int 33 load(struct plan *p, const char *path) 34 { 35 char why[200]; 36 FILE *f = path && strcmp(path, "-") ? fopen(path, "r") : stdin; 37 38 if (!f) 39 eprintf("%s:", path); 40 if (plan_load(p, f, why, sizeof why) < 0) 41 eprintf("%s: %s\n", path, why); 42 if (f != stdin) 43 fclose(f); 44 return 0; 45 } 46 47 /* the disks a system can be put on, a line each: name, bytes, model, and 1 when a 48 * partition of it is mounted (the media the system came from). not the devices that are 49 * no disk: loop, ram, optical, floppy and the mappers */ 50 static int 51 do_disks(void) 52 { 53 static const char *const skip[] = {"loop", "ram", "zram", "sr", "fd", "dm-", "md", "nbd"}; 54 struct BlockDevInfo *list = blockdev_get_list(), *d; 55 56 for (d = list; d; d = d->next) { 57 int busy = d->mountpoint[0] != '\0', skipped = 0; 58 size_t i; 59 struct BlockDevInfo *part; 60 61 for (i = 0; i < sizeof skip / sizeof skip[0]; i++) 62 if (strncmp(d->name, skip[i], strlen(skip[i])) == 0) 63 skipped = 1; 64 if (skipped || !d->size) 65 continue; 66 for (part = d->parts; part; part = part->next) 67 if (part->mountpoint[0]) 68 busy = 1; 69 printf("%s\t%llu\t%s\t%d\n", d->name, (unsigned long long)d->size, d->model, busy); 70 } 71 blockdev_free_list(list); 72 return 0; 73 } 74 75 static int 76 do_guided(int argc, char *argv[]) 77 { 78 struct plan p; 79 uint64_t swap = 0; 80 const char *fs = "ext4"; 81 82 ARGBEGIN 83 { 84 case 's': 85 swap = (uint64_t)estrtol(EARGF(usage()), 10); 86 break; 87 case 'f': 88 fs = EARGF(usage()); 89 break; 90 default: 91 usage(); 92 } 93 ARGEND 94 if (argc != 1) 95 usage(); 96 if (plan_guided(&p, argv[0], swap, fs) < 0) 97 eprintf("%s cannot hold the guided layout\n", argv[0]); 98 return plan_save(&p, stdout); 99 } 100 101 static void 102 show_line(const char *line, void *arg) 103 { 104 (void)arg; 105 puts(line); 106 } 107 108 static int 109 do_show(struct plan *p) 110 { 111 int i; 112 113 printf( 114 "%s, %s\n", 115 p->disk, 116 p->label.kind == LABEL_GPT ? "GPT" 117 : p->label.kind == LABEL_MBR ? "MBR" 118 : "no table" 119 ); 120 for (i = 0; i < p->label.slots; i++) { 121 const struct label_part *l = &p->label.part[i]; 122 const struct plan_part *a = &p->attr[i]; 123 char dev[PLAN_PATH + 8], size[32]; 124 125 if (!l->used) 126 continue; 127 plan_part_path(p->disk, i, dev, sizeof dev); 128 plan_size((l->end - l->start + 1) * p->label.sec_size, size, sizeof size); 129 printf( 130 "%-14s %9s %-18s %-6s %-12s %s\n", 131 dev, 132 size, 133 label_type_name(l), 134 a->fs, 135 a->mount, 136 a->format ? "format" : "" 137 ); 138 } 139 (void)show_line; 140 return 0; 141 } 142 143 static int 144 do_check(int argc, char *argv[]) 145 { 146 struct plan p; 147 unsigned need = 0; 148 char why[200]; 149 150 ARGBEGIN 151 { 152 case 'r': 153 need |= PLAN_NEED_ROOT; 154 break; 155 case 'b': 156 need |= PLAN_NEED_BOOT; 157 break; 158 default: 159 usage(); 160 } 161 ARGEND 162 if (argc != 1) 163 usage(); 164 load(&p, argv[0]); 165 if (plan_check(&p, need, why, sizeof why) < 0) { 166 weprintf("%s\n", why); 167 return 1; 168 } 169 return 0; 170 } 171 172 /* the first sectors of the disk zeroed, the table written, and the devices of it waited for */ 173 static int 174 do_write(struct plan *p) 175 { 176 struct BlockDev dev; 177 char *zero = ecalloc(SECTORS_ZEROED, 512); 178 int rc; 179 180 if (blockdev_open(&dev, p->disk, 1) < 0) 181 eprintf("%s:", p->disk); 182 /* what was there is read again as a signature of something: a filesystem, an old table */ 183 rc = blockdev_write(&dev, 0, zero, SECTORS_ZEROED * 512 / dev.sec_size); 184 free(zero); 185 if (rc < 0) 186 weprintf("could not clear the start of %s\n", p->disk); 187 rc = label_write(&dev, &p->label); 188 blockdev_close(&dev); 189 if (rc < 0) 190 eprintf("could not write the partition table of %s\n", p->disk); 191 /* a disk that is an image has no devices to wait for */ 192 if (access(p->disk, F_OK) == 0 && plan_wait_nodes(p, 100) < 0) 193 eprintf("the partition devices of %s did not appear\n", p->disk); 194 return 0; 195 } 196 197 static int 198 do_mkfs(struct plan *p) 199 { 200 char *argv[16], store[512]; 201 int i, status = 0; 202 203 for (i = 0; i < p->label.slots; i++) { 204 char dev[PLAN_PATH + 8]; 205 pid_t pid; 206 int st; 207 208 if (!p->label.part[i].used || !p->attr[i].format || !p->attr[i].fs[0]) 209 continue; 210 plan_part_path(p->disk, i, dev, sizeof dev); 211 printf("formatting %s as %s...\n", dev, p->attr[i].fs); 212 fflush(stdout); 213 if (plan_mkfs_argv(p, i, argv, 16, store, sizeof store) < 0) 214 eprintf("no way to make a %s filesystem\n", p->attr[i].fs); 215 /* swap is a warning when its maker is missing: the system works without it */ 216 switch ((pid = fork())) { 217 case -1: 218 eprintf("fork:"); 219 break; 220 case 0: 221 freopen("/dev/null", "w", stdout); 222 execvp(argv[0], argv); 223 _exit(127); 224 default: 225 if (waitpid(pid, &st, 0) < 0 || !WIFEXITED(st) || WEXITSTATUS(st) != 0) { 226 if (strcmp(p->attr[i].fs, "swap") == 0) { 227 weprintf("%s did not make swap on %s, going on without it\n", argv[0], dev); 228 } else { 229 weprintf("%s failed on %s\n", argv[0], dev); 230 status = 1; 231 } 232 } 233 } 234 } 235 return status; 236 } 237 238 /* "device mountpoint type", in the order to mount them */ 239 static int 240 do_mounts(struct plan *p) 241 { 242 int slots[LABEL_SLOTS], n = plan_mount_order(p, slots, LABEL_SLOTS), i; 243 244 for (i = 0; i < n; i++) { 245 const struct plan_fs *f = plan_filesystem(p->attr[slots[i]].fs); 246 char dev[PLAN_PATH + 8]; 247 248 if (!f || !f->mount_as) 249 continue; 250 plan_part_path(p->disk, slots[i], dev, sizeof dev); 251 printf("%s %s %s\n", dev, p->attr[slots[i]].mount, f->mount_as); 252 } 253 return 0; 254 } 255 256 /* how fstab names a partition: by its filesystem's UUID, which stays when 257 * another disk is plugged in and the names move, or its device when the 258 * filesystem has none to read */ 259 static void 260 fstab_source(const char *dev, char *out, size_t n) 261 { 262 struct BlockDev bd; 263 char type[32] = "", label[128] = "", uuid[128] = ""; 264 265 snprintf(out, n, "%s", dev); 266 if (blockdev_open(&bd, dev, 0) < 0) 267 return; 268 if (blockdev_detect_fs(&bd, type, sizeof type, label, sizeof label, uuid, sizeof uuid) == 0 269 && uuid[0]) 270 snprintf(out, n, "UUID=%s", uuid); 271 blockdev_close(&bd); 272 } 273 274 /* the lines of the installed system's fstab: what is mounted, and swap */ 275 static int 276 do_fstab(struct plan *p) 277 { 278 int slots[LABEL_SLOTS], n = plan_mount_order(p, slots, LABEL_SLOTS), i; 279 280 for (i = 0; i < n; i++) { 281 const struct plan_fs *f = plan_filesystem(p->attr[slots[i]].fs); 282 const char *mp = p->attr[slots[i]].mount; 283 char path[PLAN_PATH + 8], dev[PLAN_PATH + 140]; 284 285 if (!f || !f->mount_as) 286 continue; 287 plan_part_path(p->disk, slots[i], path, sizeof path); 288 fstab_source(path, dev, sizeof dev); 289 if (strcmp(mp, "/") == 0) 290 printf("%s / %s defaults,noatime 0 1\n", dev, f->mount_as); 291 else 292 printf("%s %s %s defaults 0 2\n", dev, mp, f->mount_as); 293 } 294 for (i = 0; i < p->label.slots; i++) 295 if (p->label.part[i].used && strcmp(p->attr[i].fs, "swap") == 0) { 296 char dev[PLAN_PATH + 8]; 297 298 plan_part_path(p->disk, i, dev, sizeof dev); 299 printf("%s none swap sw 0 0\n", dev); 300 } 301 return 0; 302 } 303 304 /* the plan written back over its file, whole or not at all */ 305 static void 306 save(const struct plan *p, const char *path) 307 { 308 char tmp[PATH_MAX]; 309 FILE *f; 310 311 if (snprintf(tmp, sizeof tmp, "%s.new", path) >= (int)sizeof tmp) 312 eprintf("%s: name too long\n", path); 313 if (!(f = fopen(tmp, "w"))) 314 eprintf("%s:", tmp); 315 if (plan_save(p, f) < 0 || fclose(f) == EOF) 316 eprintf("%s: could not write\n", tmp); 317 if (rename(tmp, path) < 0) 318 eprintf("%s:", path); 319 } 320 321 /* a partition named as a person does, by the number its device ends in */ 322 static int 323 slot_of(const struct plan *p, const char *n) 324 { 325 long slot = estrtol(n, 10) - 1; 326 327 if (slot < 0 || slot >= p->label.slots || !p->label.part[slot].used) 328 eprintf("%s: no such partition\n", n); 329 return (int)slot; 330 } 331 332 /* "512M", "20G", "1T", plain MiB, or "rest": sectors, 0 for what is left */ 333 static uint64_t 334 sectors_of(const struct plan *p, const char *size) 335 { 336 char *end; 337 uint64_t n, mul = 1048576; 338 339 if (strcmp(size, "rest") == 0 || strcmp(size, "0") == 0) 340 return 0; 341 n = strtoull(size, &end, 10); 342 switch (*end) { 343 case 'k': 344 case 'K': 345 mul = 1024; 346 end++; 347 break; 348 case 'm': 349 case 'M': 350 end++; 351 break; 352 case 'g': 353 case 'G': 354 mul = 1048576ULL * 1024; 355 end++; 356 break; 357 case 't': 358 case 'T': 359 mul = 1048576ULL * 1024 * 1024; 360 end++; 361 break; 362 } 363 if (*end == 'i' || *end == 'I') 364 end++; 365 if (*end == 'b' || *end == 'B') 366 end++; 367 if (end == size || *end || !n) 368 eprintf("%s: not a size (512M, 20G, rest)\n", size); 369 return (n * mul + p->label.sec_size - 1) / p->label.sec_size; 370 } 371 372 /* the type a filesystem is given when none is said: what a firmware, limine and a reader of the 373 * table expect */ 374 static const char * 375 type_of(const char *fs, const char *mount) 376 { 377 if (strcmp(fs, "swap") == 0) 378 return "8200"; 379 if (strcmp(fs, "vfat") == 0 && mount && strcmp(mount, "/boot/efi") == 0) 380 return "ef00"; 381 if (strcmp(fs, "bios") == 0) 382 return "ef02"; 383 if (strcmp(fs, "vfat") == 0) 384 return "0700"; 385 return "8300"; 386 } 387 388 static int 389 do_read(int argc, char *argv[]) 390 { 391 struct plan p; 392 393 if (argc != 2) 394 usage(); 395 if (plan_read_disk(&p, argv[1]) < 0) 396 eprintf("%s: could not read its partitions\n", argv[1]); 397 return plan_save(&p, stdout); 398 } 399 400 static int 401 do_new(int argc, char *argv[]) 402 { 403 struct plan p; 404 struct BlockDev dev; 405 enum label_kind kind = LABEL_GPT; 406 407 ARGBEGIN 408 { 409 case 'm': 410 kind = LABEL_MBR; 411 break; 412 default: 413 usage(); 414 } 415 ARGEND 416 if (argc != 1) 417 usage(); 418 memset(&p, 0, sizeof p); 419 snprintf(p.disk, sizeof p.disk, "%s", argv[0]); 420 if (blockdev_open(&dev, argv[0], 0) < 0) 421 eprintf("%s:", argv[0]); 422 if (label_init(&p.label, &dev, kind) < 0) 423 eprintf("%s is too small for a partition table\n", argv[0]); 424 blockdev_close(&dev); 425 return plan_save(&p, stdout); 426 } 427 428 static int 429 do_gaps(struct plan *p) 430 { 431 struct label_gap gaps[LABEL_SLOTS + 1]; 432 int n = label_gaps(&p->label, gaps, LABEL_SLOTS + 1), i; 433 char size[32]; 434 435 for (i = 0; i < n; i++) { 436 plan_size((gaps[i].end - gaps[i].start + 1) * p->label.sec_size, size, sizeof size); 437 printf( 438 "free %9s sectors %llu-%llu\n", 439 size, 440 (unsigned long long)gaps[i].start, 441 (unsigned long long)gaps[i].end 442 ); 443 } 444 return 0; 445 } 446 447 static int 448 do_changes(struct plan *p) 449 { 450 struct plan before; 451 452 if (plan_read_disk(&before, p->disk) < 0) 453 memset(&before, 0, sizeof before); 454 plan_changes(&before, p, show_line, NULL); 455 return 0; 456 } 457 458 static int 459 do_add(int argc, char *argv[]) 460 { 461 struct plan *p; 462 const char *type = NULL, *label = "", *fs, *mount; 463 char name[16]; 464 int slot; 465 466 ARGBEGIN 467 { 468 case 't': 469 type = EARGF(usage()); 470 break; 471 case 'l': 472 label = EARGF(usage()); 473 break; 474 default: 475 usage(); 476 } 477 ARGEND 478 if (argc < 3 || argc > 4) 479 usage(); 480 p = ecalloc(1, sizeof *p); 481 load(p, argv[0]); 482 if (p->label.kind == LABEL_NONE) 483 eprintf("%s has no partition table: start from partplan new\n", p->disk); 484 fs = argv[2]; 485 mount = argc == 4 ? argv[3] : ""; 486 if (!type) 487 type = type_of(fs, mount); 488 /* the BIOS boot partition holds no filesystem, it is only its type */ 489 if (strcmp(fs, "bios") == 0 || strcmp(fs, "none") == 0) 490 fs = ""; 491 snprintf( 492 name, 493 sizeof name, 494 "%s", 495 strcmp(type, "ef00") == 0 ? "ESP" 496 : strcmp(type, "ef02") == 0 ? "BIOS" 497 : strcmp(type, "8200") == 0 ? "SWAP" 498 : strcmp(mount, "/") == 0 ? "ROOT" 499 : "DATA" 500 ); 501 slot = plan_add(p, 0, sectors_of(p, argv[1]), type, name, fs, mount, label); 502 if (slot < 0) 503 eprintf( 504 "no room for %s, or %s %s is not a partition (gaps shows the room)\n", 505 argv[1], 506 argv[2], 507 mount 508 ); 509 save(p, argv[0]); 510 printf("%d\n", slot + 1); 511 free(p); 512 return 0; 513 } 514 515 static int 516 do_delete(int argc, char *argv[]) 517 { 518 struct plan *p; 519 520 if (argc != 3) 521 usage(); 522 p = ecalloc(1, sizeof *p); 523 load(p, argv[1]); 524 if (plan_delete(p, slot_of(p, argv[2])) < 0) 525 eprintf("could not delete %s\n", argv[2]); 526 save(p, argv[1]); 527 free(p); 528 return 0; 529 } 530 531 /* a partition's filesystem and place: -k keeps what is on it instead of making it anew */ 532 static int 533 do_set(int argc, char *argv[]) 534 { 535 struct plan *p; 536 const char *label = NULL; 537 int keep = 0, slot; 538 539 ARGBEGIN 540 { 541 case 'k': 542 keep = 1; 543 break; 544 case 'l': 545 label = EARGF(usage()); 546 break; 547 default: 548 usage(); 549 } 550 ARGEND 551 if (argc < 3 || argc > 4) 552 usage(); 553 p = ecalloc(1, sizeof *p); 554 load(p, argv[0]); 555 slot = slot_of(p, argv[1]); 556 if (strcmp(argv[2], "none") == 0) 557 argv[2] = ""; 558 if (plan_set( 559 p, slot, argv[2], argc == 4 ? argv[3] : "", label ? label : p->attr[slot].label, !keep 560 ) 561 < 0) 562 eprintf("%s is not a filesystem partplan makes, or its label is too long\n", argv[2]); 563 save(p, argv[0]); 564 free(p); 565 return 0; 566 } 567 568 /* the root partition's PARTUUID, which the kernel finds it by whatever the disk 569 * is called this boot: a GPT partition's own GUID, or the MBR's disk signature 570 * and the partition's number. it is read from the disk, as the table written 571 * made it, not from the plan, which carries none */ 572 static int 573 do_rootuuid(struct plan *p) 574 { 575 struct plan disk; 576 char guid[37]; 577 int i; 578 579 if (plan_read_disk(&disk, p->disk) < 0) 580 eprintf("%s: could not read its partitions\n", p->disk); 581 for (i = 0; i < p->label.slots; i++) { 582 if (!p->label.part[i].used || strcmp(p->attr[i].mount, "/") != 0) 583 continue; 584 if (!disk.label.part[i].used || disk.label.part[i].start != p->label.part[i].start) 585 eprintf("%s: the root partition is not on the disk: write the plan first\n", p->disk); 586 if (disk.label.kind == LABEL_GPT) { 587 format_guid(disk.label.part[i].guid, guid); 588 for (char *c = guid; *c; c++) 589 if (*c >= 'A' && *c <= 'F') 590 *c += 'a' - 'A'; 591 puts(guid); 592 } else { 593 printf("%08x-%02x\n", (unsigned)disk.label.mbr_id, (unsigned)(i + 1)); 594 } 595 return 0; 596 } 597 return 1; 598 } 599 600 // ?man partplan: a disk's layout as a plan, made, checked and applied 601 // ?man arguments: guided [-s swap_mib] [-f root_fs] disk | disks | layout | read disk | new [-m] disk | show plan | gaps plan | changes plan | check [-rb] plan | write plan | mkfs plan | mounts plan | fstab plan | get plan role | add [-t type] [-l label] plan size fs [mount] | delete plan n | set [-k] [-l label] plan n fs [mount] 602 // ?man partplan works on a plan, the text a partitioner and an installer 603 // ?man share: the disk, its partition table, and for each partition the 604 // ?man filesystem it holds, where it is mounted and whether it is made 605 // ?man anew. disks lists the disks a system can go on, a line each: name, bytes, 606 // ?man model and 1 for one in use. guided prints the layout for a whole disk, layout 607 // ?man the partitions it puts ahead of the root, show reads one, 608 // ?man check says whether it can be installed to (-r a root, -b a way to start), 609 // ?man write clears the start of the disk and writes the table, mkfs makes 610 // ?man the filesystems, mounts and fstab say where each goes, and get tells 611 // ?man the device of the esp, root or swap, the number of the bios boot partition, 612 // ?man the root filesystem, or rootuuid, the root's PARTUUID read from the disk. 613 // ?man read makes a plan of a disk as it is, new one of an empty GPT table 614 // ?man (-m an MBR one), and add, delete and set edit a plan file in place: 615 // ?man add puts a partition of size (512M, 20G, rest) holding fs (ext4, f2fs, 616 // ?man vfat, swap, bios, none) in the first room that fits it, set 617 // ?man gives partition n its filesystem and mount point, made anew unless -k 618 // ?man keeps what is on it. gaps lists the free room, changes what writing the 619 // ?man plan would do to the disk. a plan is read from the file named, or from 620 // ?man the standard input when it is - 621 int 622 main(int argc, char *argv[]) 623 { 624 struct plan *p; 625 int rc; 626 627 argv0 = argv[0]; 628 if (argc < 2) 629 usage(); 630 if (strcmp(argv[1], "guided") == 0) 631 return do_guided(argc - 1, argv + 1); 632 if (strcmp(argv[1], "check") == 0) 633 return do_check(argc - 1, argv + 1); 634 if (strcmp(argv[1], "disks") == 0) 635 return do_disks(); 636 if (strcmp(argv[1], "read") == 0) 637 return do_read(argc - 1, argv + 1); 638 if (strcmp(argv[1], "new") == 0) 639 return do_new(argc - 1, argv + 1); 640 if (strcmp(argv[1], "add") == 0) 641 return do_add(argc - 1, argv + 1); 642 if (strcmp(argv[1], "delete") == 0) 643 return do_delete(argc - 1, argv + 1); 644 if (strcmp(argv[1], "set") == 0) 645 return do_set(argc - 1, argv + 1); 646 /* what the guided layout puts ahead of the root, for a question to show */ 647 if (strcmp(argv[1], "layout") == 0) { 648 printf("BIOS boot|%d MiB\nEFI system|%d MiB\n", PLAN_BIOS_MIB, PLAN_ESP_MIB); 649 return 0; 650 } 651 if (argc < 3) 652 usage(); 653 p = ecalloc(1, sizeof *p); 654 load(p, argv[2]); 655 if (!strcmp(argv[1], "write") || !strcmp(argv[1], "mkfs")) { 656 char why[200]; 657 if (plan_check(p, 0, why, sizeof why) < 0) 658 eprintf("%s\n", why); 659 } 660 if (strcmp(argv[1], "show") == 0) 661 rc = do_show(p); 662 else if (strcmp(argv[1], "gaps") == 0) 663 rc = do_gaps(p); 664 else if (strcmp(argv[1], "changes") == 0) 665 rc = do_changes(p); 666 else if (strcmp(argv[1], "write") == 0) 667 rc = do_write(p); 668 else if (strcmp(argv[1], "mkfs") == 0) 669 rc = do_mkfs(p); 670 else if (strcmp(argv[1], "mounts") == 0) 671 rc = do_mounts(p); 672 else if (strcmp(argv[1], "fstab") == 0) 673 rc = do_fstab(p); 674 else if (strcmp(argv[1], "get") == 0 && argc == 4 && strcmp(argv[3], "rootuuid") == 0) 675 rc = do_rootuuid(p); 676 else if (strcmp(argv[1], "get") == 0 && argc == 4) { 677 char value[PLAN_PATH + 8]; 678 679 rc = plan_role(p, argv[3], value, sizeof value) ? (puts(value), 0) : 1; 680 } else 681 usage(); 682 free(p); 683 return rc; 684 }