Add support for SystemdOptions EFI var to augment /proc/cmdline
In various circumstances, overriding the kernel commandline can be inconvenient. People have different bootloaders, and e.g. the grub config can be pretty scary. grubby helps, but it isn't always available. This option adds an alternative mechanism that can quite convenient on EFI systems. cmdline settings have higher priority, because they can be (usually) changed on the bootloader prompt. $SYSTEMD_EFI_OPTIONS can be used to override, same as $SYSTEMD_PROC_CMDLINE.
This commit is contained in:
+58
-24
@@ -5,6 +5,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "efivars.h"
|
||||
#include "extract-word.h"
|
||||
#include "fileio.h"
|
||||
#include "macro.h"
|
||||
@@ -117,6 +118,17 @@ int proc_cmdline_parse(proc_cmdline_parse_t parse_item, void *data, ProcCmdlineF
|
||||
|
||||
assert(parse_item);
|
||||
|
||||
/* We parse the EFI variable first, because later settings have higher priority. */
|
||||
|
||||
r = efi_systemd_options_variable(&line);
|
||||
if (r < 0 && r != -ENODATA)
|
||||
log_debug_errno(r, "Failed to get SystemdOptions EFI variable, ignoring: %m");
|
||||
|
||||
r = proc_cmdline_parse_given(line, parse_item, data, flags);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
line = mfree(line);
|
||||
r = proc_cmdline(&line);
|
||||
if (r < 0)
|
||||
return r;
|
||||
@@ -156,34 +168,14 @@ bool proc_cmdline_key_streq(const char *x, const char *y) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int proc_cmdline_get_key(const char *key, ProcCmdlineFlags flags, char **ret_value) {
|
||||
_cleanup_free_ char *line = NULL, *ret = NULL;
|
||||
static int cmdline_get_key(const char *line, const char *key, ProcCmdlineFlags flags, char **ret_value) {
|
||||
_cleanup_free_ char *ret = NULL;
|
||||
bool found = false;
|
||||
const char *p;
|
||||
int r;
|
||||
|
||||
/* Looks for a specific key on the kernel command line. Supports three modes:
|
||||
*
|
||||
* a) The "ret_value" parameter is used. In this case a parameter beginning with the "key" string followed by
|
||||
* "=" is searched for, and the value following it is returned in "ret_value".
|
||||
*
|
||||
* b) as above, but the PROC_CMDLINE_VALUE_OPTIONAL flag is set. In this case if the key is found as a separate
|
||||
* word (i.e. not followed by "=" but instead by whitespace or the end of the command line), then this is
|
||||
* also accepted, and "value" is returned as NULL.
|
||||
*
|
||||
* c) The "ret_value" parameter is NULL. In this case a search for the exact "key" parameter is performed.
|
||||
*
|
||||
* In all three cases, > 0 is returned if the key is found, 0 if not. */
|
||||
|
||||
if (isempty(key))
|
||||
return -EINVAL;
|
||||
|
||||
if (FLAGS_SET(flags, PROC_CMDLINE_VALUE_OPTIONAL) && !ret_value)
|
||||
return -EINVAL;
|
||||
|
||||
r = proc_cmdline(&line);
|
||||
if (r < 0)
|
||||
return r;
|
||||
assert(line);
|
||||
assert(key);
|
||||
|
||||
p = line;
|
||||
for (;;) {
|
||||
@@ -226,6 +218,48 @@ int proc_cmdline_get_key(const char *key, ProcCmdlineFlags flags, char **ret_val
|
||||
return found;
|
||||
}
|
||||
|
||||
int proc_cmdline_get_key(const char *key, ProcCmdlineFlags flags, char **ret_value) {
|
||||
_cleanup_free_ char *line = NULL;
|
||||
int r;
|
||||
|
||||
/* Looks for a specific key on the kernel command line and (with lower priority) the EFI variable.
|
||||
* Supports three modes:
|
||||
*
|
||||
* a) The "ret_value" parameter is used. In this case a parameter beginning with the "key" string followed by
|
||||
* "=" is searched for, and the value following it is returned in "ret_value".
|
||||
*
|
||||
* b) as above, but the PROC_CMDLINE_VALUE_OPTIONAL flag is set. In this case if the key is found as a separate
|
||||
* word (i.e. not followed by "=" but instead by whitespace or the end of the command line), then this is
|
||||
* also accepted, and "value" is returned as NULL.
|
||||
*
|
||||
* c) The "ret_value" parameter is NULL. In this case a search for the exact "key" parameter is performed.
|
||||
*
|
||||
* In all three cases, > 0 is returned if the key is found, 0 if not. */
|
||||
|
||||
if (isempty(key))
|
||||
return -EINVAL;
|
||||
|
||||
if (FLAGS_SET(flags, PROC_CMDLINE_VALUE_OPTIONAL) && !ret_value)
|
||||
return -EINVAL;
|
||||
|
||||
r = proc_cmdline(&line);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = cmdline_get_key(line, key, flags, ret_value);
|
||||
if (r != 0) /* Either error or true if found. */
|
||||
return r;
|
||||
|
||||
line = mfree(line);
|
||||
r = efi_systemd_options_variable(&line);
|
||||
if (r == -ENODATA)
|
||||
return false; /* Not found */
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return cmdline_get_key(line, key, flags, ret_value);
|
||||
}
|
||||
|
||||
int proc_cmdline_get_bool(const char *key, bool *ret) {
|
||||
_cleanup_free_ char *v = NULL;
|
||||
int r;
|
||||
|
||||
Reference in New Issue
Block a user