[RFC] Parse /proc/cmdline in modprobe

Ozan Çağlayan ozan at pardus.org.tr
Sat Jan 9 17:36:52 UTC 2010


Hi,

Current modprobe in modutils/ isn't aware of the parameters passed through /proc/cmdline. module-init-tools offers this possibility since 2-3 releases. I need that feature for busybox which I'm using in initramfs.

Note that this will only fork for calls like below:

$ modprobe <modulename>

I don't know if it should parse /proc/cmdline when:
  * using modalias instead of modulename (will need modalias->modulename resolving before parsing /proc/cmdline),
  * using -a and then giving more than one module to probe.

That's my first attempt to contribute to busybox, I tried my best to use libbb, I now need your ideas and code review :)

If it will be upstreamed, i'll send the reviewed patches with git-send-email,

Thanks
Ozan Caglayan



diff -Naur /var/pisi/mkinitramfs-1.15.3-61/work/busybox-1.15.3/modutils/modprobe.c /tmp/busybox-1.15.3/modutils/modprobe.c
--- busybox-1.15.3/modutils/modprobe.c	2009-12-12 23:13:28.000000000 +0200
+++ busybox-1.15.3/modutils/modprobe.c	2010-01-09 19:17:39.603083389 +0200
@@ -400,10 +400,16 @@
 			add_probe(*argv++);
 		} while (*argv);
 	} else {
+		char *cmdline_params, *kcmdline_params;
 		/* First argument is module name, rest are parameters */
 		DBG("probing just module %s", *argv);
 		add_probe(argv[0]);
-		G.cmdline_mopts = parse_cmdline_module_options(argv);
+		cmdline_params = parse_cmdline_module_options(argv);
+		kcmdline_params = parse_kcmdline_module_options(*argv);
+		G.cmdline_mopts = xzalloc(strlen(cmdline_params) + strlen(kcmdline_params) + 1);
+		/* Allow overriding /proc/cmdline parameters with the ones given in command line */
+		strcat(G.cmdline_mopts, kcmdline_params);
+		strcat(G.cmdline_mopts, cmdline_params);
 	}
 
 	/* Happens if all requested modules are already loaded */
diff -Naur /var/pisi/mkinitramfs-1.15.3-61/work/busybox-1.15.3/modutils/modutils.c /tmp/busybox-1.15.3/modutils/modutils.c
--- busybox-1.15.3/modutils/modutils.c	2009-12-12 23:13:28.000000000 +0200
+++ busybox-1.15.3/modutils/modutils.c	2010-01-09 18:00:23.353324254 +0200
@@ -77,6 +77,48 @@
 	return options;
 }
 
+char * FAST_FUNC parse_kcmdline_module_options(char *modulename)
+{
+	char *kcmdline_buf;
+	char *options;
+	char *token, *sep, *modname, *modoption;
+	int len;
+
+	/* defined in arch/<architecture>/include/asm/setup.h
+	 * maximum is 2048 for IA64 and SPARC */
+	kcmdline_buf = xmalloc(2048);
+
+	len = open_read_close("/tmp/cmdline", kcmdline_buf, 2047);
+	if (len < 0)
+		return NULL;
+
+	/* Read was succesful */
+	kcmdline_buf[len] = '\0';
+
+	/* Initial allocation */
+	options = xzalloc(1);
+	len = 0;
+
+	while ((token = strsep(&kcmdline_buf, "\n\t ")) != NULL) {
+		if ((sep = strchr(token, '.')) && strchr(sep, '=')) {
+			modname = token;
+			*sep = '\0';
+			modoption = ++sep;
+			if (!strcmp(modulename, modname)) {
+				/* Found parameters for the relevant module */
+				options = xrealloc(options, len + strlen(modoption) + 2);
+				len += sprintf(options + len, "%s ", modoption);
+			}
+		}
+	}
+	free(kcmdline_buf);
+
+	/* Strip last space */
+	if (len > 0)
+	    options[len-1] = '\0';
+	return options;
+}
+
 /* Return:
  * 0 on success,
  * -errno on open/read error,
diff -Naur /var/pisi/mkinitramfs-1.15.3-61/work/busybox-1.15.3/modutils/modutils.h /tmp/busybox-1.15.3/modutils/modutils.h
--- busybox-1.15.3/modutils/modutils.h	2009-12-12 23:13:28.000000000 +0200
+++ busybox-1.15.3/modutils/modutils.h	2010-01-09 18:00:23.353324254 +0200
@@ -22,6 +22,7 @@
 int string_to_llist(char *string, llist_t **llist, const char *delim) FAST_FUNC;
 char *filename2modname(const char *filename, char *modname) FAST_FUNC;
 char *parse_cmdline_module_options(char **argv) FAST_FUNC;
+char *parse_kcmdline_module_options(char *modulename) FAST_FUNC;
 
 #define INSMOD_OPTS \
 	"vq" \




More information about the busybox mailing list