preinit: parse rootflags= in kernel command line

This commit is contained in:
Daniel Barlow 2024-01-07 23:52:01 +00:00
parent 6971d03520
commit 63f034e362
3 changed files with 77 additions and 51 deletions

7
pkgs/preinit/opts.h Normal file
View file

@ -0,0 +1,7 @@
struct root_opts {
char *device;
char *fstype;
char *mount_opts;
};
void parseopts(char * cmdline, struct root_opts *opts);

View file

@ -3,6 +3,9 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "opts.h"
static int begins_with(char * str, char * prefix) static int begins_with(char * str, char * prefix)
{ {
while(*prefix) { while(*prefix) {
@ -42,29 +45,31 @@ char * pr_u32(int32_t input) {
return NULL; return NULL;
} }
static char *eat_word(char *p)
{
while(*p && (*p != ' ')) p++;
void parseopts(char * cmdline, char **root, char **rootfstype) { if(*p) {
*root = 0; *p = '\0';
*rootfstype = 0; p++;
};
return p;
}
static char * eat_param(char *p, char *param_name, char **out)
{
if(begins_with(p, param_name)) {
*out = p + strlen(param_name);
p = eat_word(p);
};
return p;
}
void parseopts(char * cmdline, struct root_opts *opts) {
for(char *p = cmdline; *p; p++) { for(char *p = cmdline; *p; p++) {
if(begins_with(p, "root=")) { p = eat_param(p, "root=", &(opts->device));
*root = p + strlen("root="); p = eat_param(p, "rootfstype=", &(opts->fstype));
while(*p && (*p != ' ')) p++; p = eat_param(p, "rootflags=", &(opts->mount_opts));
if(*p) {
*p = '\0';
p++;
};
};
if(begins_with(p, "rootfstype=")) {
*rootfstype = p + strlen("rootfstype=");
while(*p && (*p != ' ')) p++;
if(*p) {
*p = '\0';
p++;
};
};
}; };
} }
@ -83,45 +88,52 @@ void parseopts(char * cmdline, char **root, char **rootfstype) {
int main() int main()
{ {
char * root = "/dev/hda1"; struct root_opts opts = {
char * rootfstype = "xiafs"; .device = "/dev/hda1",
.fstype = "xiafs",
.mount_opts = NULL
};
char *buf; char *buf;
// finds root= and rootfstype= options // finds root= rootfstype= rootopts= options
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 foo"); 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 foo");
parseopts(buf, &root, &rootfstype); memset(&opts, '\0', sizeof opts); parseopts(buf, &opts);
expect_equal(root, "/dev/mtdblock0"); expect_equal(opts.device, "/dev/mtdblock0");
expect_equal(rootfstype, "ubifs"); expect_equal(opts.fstype, "ubifs");
expect_equal(opts.mount_opts, "subvol=1");
// in case of duplicates, chooses the latter // in case of duplicates, chooses the latter
// also: works if the option is end of string // 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"); 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");
parseopts(buf, &root, &rootfstype); memset(&opts, '\0', sizeof opts); parseopts(buf, &opts);
expect_equal(root, "/dev/mtdblock0"); expect_equal(opts.device, "/dev/mtdblock0");
expect_equal(rootfstype, "ubifs"); expect_equal(opts.fstype, "ubifs");
// options may appear in either order // options may appear in either order
buf = strdup("liminix fw_devlink=off root=/dev/hda1 rootfstype=ubifs foo"); buf = strdup("liminix fw_devlink=off root=/dev/hda1 rootfstype=ubifs foo");
parseopts(buf, &root, &rootfstype); memset(&opts, '\0', sizeof opts); parseopts(buf, &opts);
expect_equal(root, "/dev/hda1"); expect_equal(opts.device, "/dev/hda1");
expect_equal(rootfstype, "ubifs"); expect_equal(opts.fstype, "ubifs");
buf = strdup("liminix rootfstype=ubifs fw_devlink=off root=/dev/hda1 foo"); buf = strdup("liminix rootfstype=ubifs fw_devlink=off root=/dev/hda1 foo");
parseopts(buf, &root, &rootfstype); memset(&opts, '\0', sizeof opts); parseopts(buf, &opts);
expect_equal(rootfstype, "ubifs"); expect_equal(opts.fstype, "ubifs");
expect_equal(root, "/dev/hda1"); expect_equal(opts.device, "/dev/hda1");
// provides NULL for missing options // provides NULL for missing options
buf = strdup("liminix rufustype=ubifs fw_devlink=off foot=/dev/hda1"); buf = strdup("liminix rufustype=ubifs fw_devlink=off foot=/dev/hda1");
parseopts(buf, &root, &rootfstype); memset(&opts, '\0', sizeof opts); parseopts(buf, &opts);
if(rootfstype) die("expected null rootfstype, got \"%s\"", rootfstype);
if(root) die("expected null root, got \"%s\"", root); 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);
// provides empty strings for empty options // provides empty strings for empty options
buf = strdup("liminix rootfstype= fw_devlink=off root= /dev/hda1"); buf = strdup("liminix rootfstype= fw_devlink=off root= /dev/hda1");
parseopts(buf, &root, &rootfstype); memset(&opts, '\0', sizeof opts); parseopts(buf, &opts);
if(strlen(rootfstype)) die("expected empty rootfstype, got \"%s\"", rootfstype);
if(strlen(root)) die("expected null root, got \"%s\"", root); if(strlen(opts.fstype)) die("expected empty rootfstype, got \"%s\"", opts.fstype);
if(strlen(opts.device)) die("expected null root, got \"%s\"", opts.device);
expect_equal("01", pr_u32(1)); expect_equal("01", pr_u32(1));
expect_equal("ab", pr_u32(0xab)); expect_equal("ab", pr_u32(0xab));

View file

@ -10,7 +10,7 @@
#include <asm/setup.h> /* for COMMAND_LINE_SIZE */ #include <asm/setup.h> /* for COMMAND_LINE_SIZE */
void parseopts(char * cmdline, char **root, char **rootfstype); #include "opts.h"
#define ERR(x) write(2, x, strlen(x)) #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("\n"); } } while(0)
@ -49,8 +49,11 @@ char buf[COMMAND_LINE_SIZE];
int main(int argc, char *argv[], char *envp[]) int main(int argc, char *argv[], char *envp[])
{ {
char *rootdevice = 0; struct root_opts opts = {
char *rootfstype = 0; .device = NULL,
.fstype = NULL,
.mount_opts = NULL
};
write(1, banner, strlen(banner)); write(1, banner, strlen(banner));
@ -73,16 +76,20 @@ int main(int argc, char *argv[], char *envp[])
ERR("failed: open(\"/proc/cmdline\")\n"); ERR("failed: open(\"/proc/cmdline\")\n");
die(); die();
} }
parseopts(buf, &rootdevice, &rootfstype); parseopts(buf, &opts);
if(rootdevice) { if(opts.device) {
if(!rootfstype) rootfstype = "jffs2"; /* backward compatibility */ if(!opts.fstype) opts.fstype = "jffs2"; /* backward compatibility */
write(1, "rootdevice ", 11); write(1, "rootdevice ", 11);
write(1, rootdevice, strlen(rootdevice)); write(1, opts.device, strlen(opts.device));
write(1, " (", 2); write(1, " (", 2);
write(1, rootfstype, strlen(rootfstype)); write(1, opts.fstype, strlen(opts.fstype));
if(opts.mount_opts) {
write(1, ", opts=", 6);
write(1, opts.mount_opts, strlen(opts.mount_opts));
}
write(1, ")\n", 2); write(1, ")\n", 2);
AVER(mount(rootdevice, "/target/persist", rootfstype, 0, NULL)); AVER(mount(opts.device, "/target/persist", opts.fstype, 0, opts.mount_opts));
AVER(mount("/target/persist/nix", "/target/nix", AVER(mount("/target/persist/nix", "/target/nix",
"bind", MS_BIND, NULL)); "bind", MS_BIND, NULL));