+static int parse_new_root_from_proc_cmdline(void) {
+ _cleanup_free_ char *what = NULL, *type = NULL, *opts = NULL, *line = NULL;
+ char *w, *state;
+ int r;
+ size_t l;
+ bool noauto, nofail;
+
+ r = read_one_line_file("/proc/cmdline", &line);
+ if (r < 0) {
+ log_error("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
+ return 0;
+ }
+
+ opts = strdup("ro");
+ type = strdup("auto");
+ if (!opts || !type)
+ return log_oom();
+
+ /* root= and roofstype= may occur more than once, the last instance should take precedence.
+ * In the case of multiple rootflags= the arguments should be concatenated */
+ FOREACH_WORD_QUOTED(w, l, line, state) {
+ _cleanup_free_ char *word;
+
+ word = strndup(w, l);
+ if (!word)
+ return log_oom();
+
+ else if (startswith(word, "root=")) {
+ free(what);
+ what = fstab_node_to_udev_node(word+5);
+ if (!what)
+ return log_oom();
+
+ } else if (startswith(word, "rootfstype=")) {
+ free(type);
+ type = strdup(word + 11);
+ if (!type)
+ return log_oom();
+
+ } else if (startswith(word, "rootflags=")) {
+ char *o;
+
+ o = strjoin(opts, ",", word + 10, NULL);
+ if (!o)
+ return log_oom();
+
+ free(opts);
+ opts = o;
+
+ } else if (streq(word, "ro") || streq(word, "rw")) {
+ char *o;
+
+ o = strjoin(opts, ",", word, NULL);
+ if (!o)
+ return log_oom();
+
+ free(opts);
+ opts = o;
+ }
+ }
+
+ noauto = !!strstr(opts, "noauto");
+ nofail = !!strstr(opts, "nofail");
+
+ if (!what) {
+ log_debug("Could not find a root= entry on the kernel commandline.");
+ return 0;
+ }
+
+ if (what[0] != '/') {
+ log_debug("Skipping entry what=%s where=/sysroot type=%s", what, type);
+ return 0;
+ }
+
+ log_debug("Found entry what=%s where=/sysroot type=%s", what, type);
+ r = add_mount(what, "/sysroot", type, opts, 0, noauto, nofail, false,
+ false, NULL, NULL, NULL, SPECIAL_INITRD_ROOT_FS_TARGET, "/proc/cmdline");
+
+ return (r < 0) ? r : 0;
+}
+