feat(preinit): support alternative roots

In A/B schemas, it is possible to have multiple rootfs.

Thus, it is necessary to let the B kernel preinitialize either the A
rootfs or the B rootfs failing to mount the A rootfs.

Ideally, we should also try to switch to the B rootfs if we cannot start
up the init on the A rootfs, but that's a FUTUREWORK.

Signed-off-by: Raito Bezarius <masterancpp@gmail.com>
This commit is contained in:
Raito Bezarius 2024-04-21 15:38:44 +02:00
parent 771585546d
commit afb14d844b
3 changed files with 30 additions and 4 deletions

View file

@ -1,5 +1,6 @@
struct root_opts {
char *device;
char *altdevice; /* For A/B schemas */
char *fstype;
char *mount_opts;
};

View file

@ -70,6 +70,7 @@ void parseopts(char * cmdline, struct root_opts *opts) {
p = eat_param(p, "root=", &(opts->device));
p = eat_param(p, "rootfstype=", &(opts->fstype));
p = eat_param(p, "rootflags=", &(opts->mount_opts));
p = eat_param(p, "altroot=", &(opts->altdevice));
};
}
@ -102,6 +103,14 @@ int main()
expect_equal(opts.fstype, "ubifs");
expect_equal(opts.mount_opts, "subvol=1");
// finds altroot= options
buf = strdup("liminix console=ttyS0,115200 panic=10 oops=panic init=/bin/init loglevel=8 root=/dev/ubi0_4 rootfstype=ubifs rootflags=subvol=1 fw_devlink=off mtdparts=phram0:18M(rootfs) phram.phram=phram0,0x40400000,18874368,65536 root=/dev/mtdblock0 altroot=/dev/mtdblock6 foo");
memset(&opts, '\0', sizeof opts); parseopts(buf, &opts);
expect_equal(opts.device, "/dev/mtdblock0");
expect_equal(opts.altdevice, "/dev/mtdblock6");
expect_equal(opts.fstype, "ubifs");
expect_equal(opts.mount_opts, "subvol=1");
// in case of duplicates, chooses the latter
// also: works if the option is end of string
buf = strdup("liminix console=ttyS0,115200 panic=10 oops=panic init=/bin/init loglevel=8 root=/dev/ubi0_4 rootfstype=ubifs fw_devlink=off mtdparts=phram0:18M(rootfs) phram.phram=phram0,0x40400000,18874368,65536 root=/dev/mtdblock0");
@ -134,13 +143,15 @@ int main()
if(opts.fstype) die("expected null rootfstype, got \"%s\"", opts.fstype);
if(opts.device) die("expected null root, got \"%s\"", opts.device);
if(opts.mount_opts) die("expected null mount_opts, got \"%s\"", opts.mount_opts);
if(opts.altdevice) die("expected null altdevice, got \"%s\"", opts.altdevice);
// provides empty strings for empty options
buf = strdup("liminix rootfstype= fw_devlink=off root= /dev/hda1");
buf = strdup("liminix rootfstype= fw_devlink=off root= altroot= /dev/hda1");
memset(&opts, '\0', sizeof opts); parseopts(buf, &opts);
if(strlen(opts.fstype)) die("expected empty rootfstype, got \"%s\"", opts.fstype);
if(strlen(opts.device)) die("expected null root, got \"%s\"", opts.device);
if(strlen(opts.device)) die("expected empty root, got \"%s\"", opts.device);
if(strlen(opts.altdevice)) die("expected empty altroot, got \"%s\"", opts.altdevice);
expect_equal("01", pr_u32(1));
expect_equal("ab", pr_u32(0xab));

View file

@ -13,7 +13,7 @@
#include "opts.h"
#define ERR(x) write(2, x, strlen(x))
#define AVER(c) do { if(c < 0) { ERR("failed: " #c ": error=0x" ); pr_u32(errno); ERR("\n"); } } while(0)
#define AVER(c) do { if(c < 0) { ERR("failed: " #c ": error=0x" ); pr_u32(errno); ERR ( " - "); ERR(strerror(errno)); ERR("\n"); } } while(0)
char * pr_u32(int32_t input);
@ -88,8 +88,21 @@ int main(int argc, char *argv[], char *envp[])
write(1, ", opts=", 7);
write(1, opts.mount_opts, strlen(opts.mount_opts));
}
if(opts.altdevice) {
write(1, ", altdevice=", 12);
write(1, opts.altdevice, strlen(opts.altdevice));
}
write(1, ")\n", 2);
if(!opts.altdevice) {
AVER(mount(opts.device, "/target/persist", opts.fstype, 0, opts.mount_opts));
} else {
if(mount(opts.device, "/target/persist", opts.fstype, 0, opts.mount_opts) < 0) {
AVER(mount(opts.altdevice, "/target/persist", opts.fstype, 0, opts.mount_opts));
}
}
// FUTUREWORK: any failure using `opts.device` should force us to consider rerunning this with the alternative rootfs.
AVER(mount("/target/persist/nix", "/target/nix",
"bind", MS_BIND, NULL));
@ -102,6 +115,7 @@ int main(int argc, char *argv[], char *envp[])
argv[0] = "init";
argv[1] = NULL;
AVER(execve("/persist/init", argv, envp));
}
die();