commit ae36e329c10f9d2d3b448c22f78800bb99348527
parent 033eb22f5ecf639833ab644bf0ab6e57d1666481
Author: xplshn <anto@xplshn.com.ar>
Date: Tue, 24 Nov 2026 02:39:06 -0300
partplan: guided layouts, checks, writing and formatting of a plan from the shell
Diffstat:
4 files changed, 429 insertions(+), 5 deletions(-)
diff --git a/cmd/extra/partplan.c b/cmd/extra/partplan.c
@@ -0,0 +1,280 @@
+/* see LICENSE file for copyright and license details */
+#include "libdisk/plan.h"
+#include "arg.h"
+#include "util.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#define SECTORS_ZEROED 16384 /* 8 MiB at 512 byte sectors: old signatures that would be seen again */
+
+static void
+usage(void)
+{
+ eprintf("usage: %s guided [-s swap_mib] [-f root_fs] disk | show plan | check [-rb] plan | write plan |\n"
+ " mkfs plan | mounts plan | fstab plan | get plan esp|root|swap|bios|rootfs\n",
+ argv0);
+}
+
+static int
+load(struct plan *p, const char *path)
+{
+ char why[200];
+ FILE *f = path && strcmp(path, "-") ? fopen(path, "r") : stdin;
+
+ if (!f)
+ eprintf("%s: %s:", argv0, path);
+ if (plan_load(p, f, why, sizeof why) < 0)
+ eprintf("%s: %s: %s\n", argv0, path, why);
+ if (f != stdin)
+ fclose(f);
+ return 0;
+}
+
+static int
+do_guided(int argc, char *argv[])
+{
+ struct plan p;
+ uint64_t swap = 0;
+ const char *fs = "ext4";
+
+ ARGBEGIN
+ {
+ case 's':
+ swap = (uint64_t)estrtol(EARGF(usage()), 10);
+ break;
+ case 'f':
+ fs = EARGF(usage());
+ break;
+ default:
+ usage();
+ }
+ ARGEND
+ if (argc != 1)
+ usage();
+ if (plan_guided(&p, argv[0], swap, fs) < 0)
+ eprintf("%s: %s cannot hold the guided layout\n", argv0, argv[0]);
+ return plan_save(&p, stdout);
+}
+
+static void
+show_line(const char *line, void *arg)
+{
+ (void)arg;
+ puts(line);
+}
+
+static int
+do_show(struct plan *p)
+{
+ int i;
+
+ printf("%s, %s\n", p->disk, p->label.kind == LABEL_GPT ? "GPT" : p->label.kind == LABEL_MBR ? "MBR" : "no table");
+ for (i = 0; i < p->label.slots; i++) {
+ const struct label_part *l = &p->label.part[i];
+ const struct plan_part *a = &p->attr[i];
+ char dev[PLAN_PATH + 8], size[32];
+
+ if (!l->used)
+ continue;
+ plan_part_path(p->disk, i, dev, sizeof dev);
+ plan_size((l->end - l->start + 1) * p->label.sec_size, size, sizeof size);
+ printf("%-14s %9s %-18s %-6s %-12s %s\n", dev, size, label_type_name(l), a->fs, a->mount, a->format ? "format" : "");
+ }
+ (void)show_line;
+ return 0;
+}
+
+static int
+do_check(int argc, char *argv[])
+{
+ struct plan p;
+ unsigned need = 0;
+ char why[200];
+
+ ARGBEGIN
+ {
+ case 'r':
+ need |= PLAN_NEED_ROOT;
+ break;
+ case 'b':
+ need |= PLAN_NEED_BOOT;
+ break;
+ default:
+ usage();
+ }
+ ARGEND
+ if (argc != 1)
+ usage();
+ load(&p, argv[0]);
+ if (plan_check(&p, need, why, sizeof why) < 0) {
+ weprintf("%s\n", why);
+ return 1;
+ }
+ return 0;
+}
+
+/* the first sectors of the disk zeroed, the table written, and the devices of it waited for */
+static int
+do_write(struct plan *p)
+{
+ struct BlockDev dev;
+ char *zero = ecalloc(SECTORS_ZEROED, 512);
+ int rc;
+
+ if (blockdev_open(&dev, p->disk, 1) < 0)
+ eprintf("%s: %s:", argv0, p->disk);
+ /* what was there is read again as a signature of something: a filesystem, an old table */
+ rc = blockdev_write(&dev, 0, zero, SECTORS_ZEROED * 512 / dev.sec_size);
+ free(zero);
+ if (rc < 0)
+ weprintf("%s: could not clear the start of %s\n", argv0, p->disk);
+ rc = label_write(&dev, &p->label);
+ blockdev_close(&dev);
+ if (rc < 0)
+ eprintf("%s: could not write the partition table of %s\n", argv0, p->disk);
+ /* a disk that is an image has no devices to wait for */
+ if (access(p->disk, F_OK) == 0 && plan_wait_nodes(p, 100) < 0)
+ eprintf("%s: the partition devices of %s did not appear\n", argv0, p->disk);
+ return 0;
+}
+
+static int
+do_mkfs(struct plan *p)
+{
+ char *argv[16], store[512];
+ int i, status = 0;
+
+ for (i = 0; i < p->label.slots; i++) {
+ char dev[PLAN_PATH + 8];
+ pid_t pid;
+ int st;
+
+ if (!p->label.part[i].used || !p->attr[i].format || !p->attr[i].fs[0])
+ continue;
+ plan_part_path(p->disk, i, dev, sizeof dev);
+ printf("formatting %s as %s...\n", dev, p->attr[i].fs);
+ fflush(stdout);
+ if (plan_mkfs_argv(p, i, argv, 16, store, sizeof store) < 0)
+ eprintf("%s: no way to make a %s filesystem\n", argv0, p->attr[i].fs);
+ /* swap is a warning when its maker is missing: the system works without it */
+ switch ((pid = fork())) {
+ case -1:
+ eprintf("%s: fork:", argv0);
+ break;
+ case 0:
+ freopen("/dev/null", "w", stdout);
+ execvp(argv[0], argv);
+ _exit(127);
+ default:
+ if (waitpid(pid, &st, 0) < 0 || !WIFEXITED(st) || WEXITSTATUS(st) != 0) {
+ if (strcmp(p->attr[i].fs, "swap") == 0) {
+ weprintf("%s: %s did not make swap on %s, going on without it\n", argv0, argv[0], dev);
+ } else {
+ weprintf("%s: %s failed on %s\n", argv0, argv[0], dev);
+ status = 1;
+ }
+ }
+ }
+ }
+ return status;
+}
+
+/* "device mountpoint type", in the order to mount them */
+static int
+do_mounts(struct plan *p)
+{
+ int slots[LABEL_SLOTS], n = plan_mount_order(p, slots, LABEL_SLOTS), i;
+
+ for (i = 0; i < n; i++) {
+ const struct plan_fs *f = plan_filesystem(p->attr[slots[i]].fs);
+ char dev[PLAN_PATH + 8];
+
+ if (!f || !f->mount_as)
+ continue;
+ plan_part_path(p->disk, slots[i], dev, sizeof dev);
+ printf("%s %s %s\n", dev, p->attr[slots[i]].mount, f->mount_as);
+ }
+ return 0;
+}
+
+/* the lines of the installed system's fstab: what is mounted, and swap */
+static int
+do_fstab(struct plan *p)
+{
+ int slots[LABEL_SLOTS], n = plan_mount_order(p, slots, LABEL_SLOTS), i;
+
+ for (i = 0; i < n; i++) {
+ const struct plan_fs *f = plan_filesystem(p->attr[slots[i]].fs);
+ const char *mp = p->attr[slots[i]].mount;
+ char dev[PLAN_PATH + 8];
+
+ if (!f || !f->mount_as)
+ continue;
+ plan_part_path(p->disk, slots[i], dev, sizeof dev);
+ if (strcmp(mp, "/") == 0)
+ printf("%s / %s defaults,noatime 0 1\n", dev, f->mount_as);
+ else
+ printf("%s %s %s defaults 0 2\n", dev, mp, f->mount_as);
+ }
+ for (i = 0; i < p->label.slots; i++)
+ if (p->label.part[i].used && strcmp(p->attr[i].fs, "swap") == 0) {
+ char dev[PLAN_PATH + 8];
+
+ plan_part_path(p->disk, i, dev, sizeof dev);
+ printf("%s none swap sw 0 0\n", dev);
+ }
+ return 0;
+}
+
+// ?man partplan: a disk's layout as a plan, made, checked and applied
+// ?man arguments: guided [-s swap_mib] [-f root_fs] disk | show plan | check [-rb] plan | write plan | mkfs plan | mounts plan | fstab plan | get plan role
+// ?man partplan works on a plan, the text a partitioner and an installer
+// ?man share: the disk, its partition table, and for each partition the
+// ?man filesystem it holds, where it is mounted and whether it is made
+// ?man anew. guided prints the layout for a whole disk, show reads one,
+// ?man check says whether it can be installed to (-r a root, -b a way to start),
+// ?man write clears the start of the disk and writes the table, mkfs makes
+// ?man the filesystems, mounts and fstab say where each goes, and get tells
+// ?man the device of the esp, root or swap, the number of the bios boot partition,
+// ?man or the root filesystem. a plan is read from the file named, or from the
+// ?man standard input when it is -
+int
+main(int argc, char *argv[])
+{
+ struct plan *p;
+ int rc;
+
+ argv0 = argv[0];
+ if (argc < 2)
+ usage();
+ if (strcmp(argv[1], "guided") == 0)
+ return do_guided(argc - 1, argv + 1);
+ if (strcmp(argv[1], "check") == 0)
+ return do_check(argc - 1, argv + 1);
+ if (argc < 3)
+ usage();
+ p = ecalloc(1, sizeof *p);
+ load(p, argv[2]);
+ if (strcmp(argv[1], "show") == 0)
+ rc = do_show(p);
+ else if (strcmp(argv[1], "write") == 0)
+ rc = do_write(p);
+ else if (strcmp(argv[1], "mkfs") == 0)
+ rc = do_mkfs(p);
+ else if (strcmp(argv[1], "mounts") == 0)
+ rc = do_mounts(p);
+ else if (strcmp(argv[1], "fstab") == 0)
+ rc = do_fstab(p);
+ else if (strcmp(argv[1], "get") == 0 && argc == 4) {
+ char value[PLAN_PATH + 8];
+
+ rc = plan_role(p, argv[3], value, sizeof value) ? (puts(value), 0) : 1;
+ } else
+ usage();
+ free(p);
+ return rc;
+}
diff --git a/shared/libdisk/plan.c b/shared/libdisk/plan.c
@@ -5,6 +5,8 @@
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
#include <string.h>
#include <strings.h>
@@ -292,8 +294,14 @@ plan_mkfs_argv(const struct plan *p, int slot, char **argv, int max, char *store
return -1;
argv[0] = NULL;
if (strcmp(a->fs, "ext4") == 0) {
- rc |= put(argv, &n, max, store, store_n, &used, "mkfs.ext4");
- rc |= put(argv, &n, max, store, store_n, &used, "-F");
+ char size[32];
+
+ /* MICE's ext4 maker, which is told the size of what it makes */
+ snprintf(size, sizeof size, "%llu",
+ (unsigned long long)((p->label.part[slot].end - p->label.part[slot].start + 1) * p->label.sec_size));
+ rc |= put(argv, &n, max, store, store_n, &used, "make_ext4fs");
+ rc |= put(argv, &n, max, store, store_n, &used, "-l");
+ rc |= put(argv, &n, max, store, store_n, &used, size);
if (a->label[0]) {
rc |= put(argv, &n, max, store, store_n, &used, "-L");
rc |= put(argv, &n, max, store, store_n, &used, a->label);
@@ -433,3 +441,116 @@ plan_load(struct plan *p, FILE *f, char *why, size_t n)
}
return p->disk[0] ? 0 : (snprintf(why, n, "a plan names a disk"), -1);
}
+
+int
+plan_guided(struct plan *p, const char *disk, uint64_t swap_mib, const char *root_fs)
+{
+ struct BlockDev dev;
+ uint64_t mib;
+ int rc = 0;
+
+ memset(p, 0, sizeof *p);
+ snprintf(p->disk, sizeof p->disk, "%s", disk);
+ if (!plan_filesystem(root_fs) || !plan_filesystem(root_fs)->bootable || blockdev_open(&dev, disk, 0) < 0)
+ return -1;
+ if (label_init(&p->label, &dev, LABEL_GPT) < 0) {
+ blockdev_close(&dev);
+ return -1;
+ }
+ mib = 1048576 / dev.sec_size;
+ blockdev_close(&dev);
+ /* the BIOS boot partition first, then the system partition, which limine and the firmware read */
+ rc |= plan_add(p, 0, PLAN_BIOS_MIB * mib, "ef02", "BIOS", "", "", "") < 0;
+ rc |= plan_add(p, 0, PLAN_ESP_MIB * mib, "ef00", "ESP", "vfat", "/boot/efi", "ESP") < 0;
+ if (swap_mib)
+ rc |= plan_add(p, 0, swap_mib * mib, "8200", "SWAP", "swap", "", "") < 0;
+ rc |= plan_add(p, 0, 0, "8300", "ROOT", root_fs, "/", "root") < 0;
+ return rc ? -1 : 0;
+}
+
+int
+plan_role(const struct plan *p, const char *role, char *buf, size_t n)
+{
+ int i;
+
+ for (i = 0; i < p->label.slots; i++) {
+ const struct label_part *l = &p->label.part[i];
+ const struct plan_part *a = &p->attr[i];
+
+ if (!l->used)
+ continue;
+ if (strcmp(role, "esp") == 0 && strcmp(a->mount, "/boot/efi") == 0) {
+ plan_part_path(p->disk, i, buf, n);
+ return 1;
+ }
+ if (strcmp(role, "root") == 0 && strcmp(a->mount, "/") == 0) {
+ plan_part_path(p->disk, i, buf, n);
+ return 1;
+ }
+ if (strcmp(role, "swap") == 0 && strcmp(a->fs, "swap") == 0) {
+ plan_part_path(p->disk, i, buf, n);
+ return 1;
+ }
+ if (strcmp(role, "bios") == 0 && strcmp(l->type, "ef02") == 0) {
+ snprintf(buf, n, "%d", i + 1);
+ return 1;
+ }
+ if (strcmp(role, "rootfs") == 0 && strcmp(a->mount, "/") == 0) {
+ snprintf(buf, n, "%s", a->fs);
+ return 1;
+ }
+ }
+ return 0;
+}
+
+int
+plan_mount_order(const struct plan *p, int *slots, int max)
+{
+ int i, j, n = 0;
+
+ for (i = 0; i < p->label.slots && n < max; i++)
+ if (p->label.part[i].used && p->attr[i].mount[0])
+ slots[n++] = i;
+ /* fewest path parts first: a mount point is made inside the ones above it */
+ for (i = 1; i < n; i++) {
+ int s = slots[i], k;
+
+ for (j = i - 1; j >= 0; j--) {
+ int a = 0, b = 0;
+
+ for (k = 0; p->attr[slots[j]].mount[k]; k++)
+ a += p->attr[slots[j]].mount[k] == '/' && p->attr[slots[j]].mount[k + 1];
+ for (k = 0; p->attr[s].mount[k]; k++)
+ b += p->attr[s].mount[k] == '/' && p->attr[s].mount[k + 1];
+ if (a <= b)
+ break;
+ slots[j + 1] = slots[j];
+ }
+ slots[j + 1] = s;
+ }
+ return n;
+}
+
+int
+plan_wait_nodes(const struct plan *p, int tenths)
+{
+ int t, i;
+
+ for (t = 0; t <= tenths; t++) {
+ int all = 1;
+
+ for (i = 0; i < p->label.slots; i++) {
+ char path[PLAN_PATH + 8];
+
+ if (!p->label.part[i].used)
+ continue;
+ plan_part_path(p->disk, i, path, sizeof path);
+ if (access(path, F_OK) != 0)
+ all = 0;
+ }
+ if (all)
+ return 0;
+ nanosleep(&(struct timespec){0, 100000000L}, NULL);
+ }
+ return -1;
+}
diff --git a/shared/libdisk/plan.h b/shared/libdisk/plan.h
@@ -78,6 +78,22 @@ int plan_check(const struct plan *p, unsigned need, char *why, size_t n);
* ("delete /dev/vda2", "create 512 MiB EFI system", "format /dev/vda3 as ext4"). how many */
int plan_changes(const struct plan *before, const struct plan *now, void (*fn)(const char *line, void *arg), void *arg);
+/* the layout an installer makes of a whole disk when it is not asked for another: a 1 MiB BIOS boot
+ * partition, a 512 MiB EFI system one at /boot/efi, swap when swap_mib is not 0, and the rest the
+ * root filesystem root_fs. 0, or -1 for a disk that cannot hold it */
+#define PLAN_BIOS_MIB 1
+#define PLAN_ESP_MIB 512
+int plan_guided(struct plan *p, const char *disk, uint64_t swap_mib, const char *root_fs);
+
+/* a part of the plan by its role: "esp", "root" and "swap" the device (/dev/vda2), "bios" the number
+ * of the BIOS boot partition, "rootfs" the filesystem at /. false when the plan has none */
+int plan_role(const struct plan *p, const char *role, char *buf, size_t n);
+/* the slots that have a place to be mounted, in the order to mount them: / before /boot before
+ * /boot/efi. how many, at most max */
+int plan_mount_order(const struct plan *p, int *slots, int max);
+/* waits for the devices of the partitions to be there after the table is written, up to tenths of a second */
+int plan_wait_nodes(const struct plan *p, int tenths);
+
/* the table written to the disk, the filesystems left for the caller */
int plan_write_table(const struct plan *p);
/* the command that makes the filesystem of slot: its argv into argv (at most max words,
diff --git a/shared/libdisk/plan_test.c b/shared/libdisk/plan_test.c
@@ -12,7 +12,7 @@
#include <unistd.h>
static int nlines; static void line(const char *l, void *a){ (void)a; nlines++; puts(l); }
int main(void){
- char path[]="/tmp/plan_test.XXXXXX"; int fd=mkstemp(path); assert(fd>=0 && ftruncate(fd,(off_t)256<<20)==0); close(fd);
+ char path[]="/tmp/plan_test.XXXXXX"; int fd=mkstemp(path); assert(fd>=0 && ftruncate(fd,(off_t)1024<<20)==0); close(fd);
struct plan before, p; char why[200]; char *argv[16]; char store[256];
assert(plan_read_disk(&before,path)==0 && before.label.kind==LABEL_NONE);
p=before;
@@ -23,8 +23,8 @@ int main(void){
assert(plan_add(&p,0,0,"8300","ROOT","ext4","/","")==2);
assert(plan_check(&p,PLAN_NEED_ROOT|PLAN_NEED_BOOT,why,sizeof why)==0);
assert(plan_changes(&before,&p,line,NULL)>=3);
- assert(plan_mkfs_argv(&p,2,argv,16,store,sizeof store)==0 && !strcmp(argv[0],"mkfs.ext4") && !strcmp(argv[1],"-F"));
- assert(argv[2] && argv[3] == NULL);
+ assert(plan_mkfs_argv(&p,2,argv,16,store,sizeof store)==0 && !strcmp(argv[0],"make_ext4fs") && !strcmp(argv[1],"-l"));
+ assert(argv[2] && argv[3] && argv[4] == NULL);
assert(plan_write_table(&p)==0);
FILE *f=tmpfile(); assert(plan_save(&p,f)==0); rewind(f);
struct plan q; assert(plan_load(&q,f,why,sizeof why)==0);
@@ -32,4 +32,11 @@ int main(void){
struct plan r; assert(plan_read_disk(&r,path)==0 && r.label.kind==LABEL_GPT && r.label.part[2].used);
p.attr[1].mount[0]='x'; assert(plan_check(&p,0,why,sizeof why)<0); puts(why);
plan_set(&p,1,"vfat","/",NULL,1); assert(plan_check(&p,0,why,sizeof why)<0); puts(why);
+ { struct plan g; char dev[64]; int order[8];
+ assert(plan_guided(&g,path,128,"ext4")==0);
+ assert(plan_role(&g,"esp",dev,sizeof dev)==1 && g.label.part[1].used);
+ assert(plan_role(&g,"bios",dev,sizeof dev)==1 && !strcmp(dev,"1"));
+ assert(plan_role(&g,"rootfs",dev,sizeof dev)==1 && !strcmp(dev,"ext4"));
+ assert(plan_mount_order(&g,order,8)==2 && !strcmp(g.attr[order[0]].mount,"/") && !strcmp(g.attr[order[1]].mount,"/boot/efi"));
+ assert(plan_check(&g,PLAN_NEED_ROOT|PLAN_NEED_BOOT,why,sizeof why)==0); }
unlink(path); puts("plan_test: ok"); return 0; }