1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-09 01:18:19 +03:00

Merge pull request #32994 from keszybz/kernel-install-parsing

Improve parsing of kernel/install.conf
This commit is contained in:
Luca Boccassi 2024-05-24 11:25:26 +02:00 committed by GitHub
commit e67129e5e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 110 additions and 77 deletions

View File

@ -14,6 +14,7 @@
#include "fs-util.h"
#include "glyph-util.h"
#include "id128-util.h"
#include "kernel-config.h"
#include "os-util.h"
#include "path-util.h"
#include "rm-rf.h"
@ -81,46 +82,19 @@ static int load_etc_machine_info(void) {
return 0;
}
static int load_kernel_install_conf(void) {
static int load_kernel_install_layout(void) {
_cleanup_free_ char *layout = NULL;
const ConfigTableItem items[] = {
{ NULL, "layout", config_parse_string, 0, &layout },
{}
};
int r;
const char *conf_root = getenv("KERNEL_INSTALL_CONF_ROOT");
if (conf_root) {
_cleanup_free_ char *conf = NULL;
conf = path_join(conf_root, "install.conf");
if (!conf)
return log_oom();
r = config_parse_many(
STRV_MAKE_CONST(conf),
STRV_MAKE_CONST(conf_root),
"install.conf.d",
/* root= */ NULL, /* $KERNEL_INSTALL_CONF_ROOT and --root are independent */
/* sections= */ NULL,
config_item_table_lookup, items,
CONFIG_PARSE_WARN,
/* userdata = */ NULL,
/* ret_stats_by_path= */ NULL,
/* ret_dropin_files= */ NULL);
} else
r = config_parse_standard_file_with_dropins_full(
arg_root,
"kernel/install.conf",
/* sections= */ NULL,
config_item_table_lookup, items,
CONFIG_PARSE_WARN,
/* userdata = */ NULL,
/* ret_stats_by_path= */ NULL,
/* ret_dropin_files= */ NULL);
if (r < 0)
return r == -ENOENT ? 0 : r;
r = load_kernel_install_conf(arg_root,
getenv("KERNEL_INSTALL_CONF_ROOT"),
/* ret_machine_id= */ NULL,
/* ret_boot_root= */ NULL,
&layout,
/* ret_initrd_generator= */ NULL,
/* ret_uki_generator= */ NULL);
if (r <= 0)
return r;
if (!isempty(layout)) {
log_debug("layout=%s is specified in config.", layout);
@ -147,7 +121,7 @@ static int settle_make_entry_directory(void) {
if (r < 0)
return r;
r = load_kernel_install_conf();
r = load_kernel_install_layout();
if (r < 0)
return r;

View File

@ -19,6 +19,7 @@
#include "fs-util.h"
#include "id128-util.h"
#include "image-policy.h"
#include "kernel-config.h"
#include "kernel-image.h"
#include "main-func.h"
#include "mkdir.h"
@ -434,48 +435,19 @@ static int context_load_environment(Context *c) {
static int context_load_install_conf(Context *c) {
_cleanup_free_ char *machine_id = NULL, *boot_root = NULL, *layout = NULL,
*initrd_generator = NULL, *uki_generator = NULL;
const ConfigTableItem items[] = {
{ NULL, "MACHINE_ID", config_parse_string, 0, &machine_id },
{ NULL, "BOOT_ROOT", config_parse_string, 0, &boot_root },
{ NULL, "layout", config_parse_string, 0, &layout },
{ NULL, "initrd_generator", config_parse_string, 0, &initrd_generator },
{ NULL, "uki_generator", config_parse_string, 0, &uki_generator },
{}
};
int r;
assert(c);
if (c->conf_root) {
_cleanup_free_ char *conf = NULL;
conf = path_join(c->conf_root, "install.conf");
if (!conf)
return log_oom();
r = config_parse_many(
STRV_MAKE_CONST(conf),
STRV_MAKE_CONST(c->conf_root),
"install.conf.d",
/* root= */ NULL, /* $KERNEL_INSTALL_CONF_ROOT and --root are independent */
/* sections= */ NULL,
config_item_table_lookup, items,
CONFIG_PARSE_WARN,
/* userdata = */ NULL,
/* ret_stats_by_path= */ NULL,
/* ret_dropin_files= */ NULL);
} else
r = config_parse_standard_file_with_dropins_full(
arg_root,
"kernel/install.conf",
/* sections= */ NULL,
config_item_table_lookup, items,
CONFIG_PARSE_WARN,
/* userdata = */ NULL,
/* ret_stats_by_path= */ NULL,
/* ret_dropin_files= */ NULL);
if (r < 0)
return r == -ENOENT ? 0 : r;
r = load_kernel_install_conf(arg_root,
c->conf_root,
&machine_id,
&boot_root,
&layout,
&initrd_generator,
&uki_generator);
if (r <= 0)
return r;
(void) context_set_machine_id(c, machine_id, "config");
(void) context_set_boot_root(c, boot_root, "config");

View File

@ -160,7 +160,11 @@ static int next_assignment(
/* Warn about unknown non-extension fields. */
if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-"))
log_syntax(unit, LOG_WARNING, filename, line, 0,
"Unknown key name '%s' in section '%s', ignoring.", lvalue, section);
"Unknown key '%s'%s%s%s, ignoring.",
lvalue,
section ? " in section [" : "",
strempty(section),
section ? "]" : "");
return 0;
}

View File

@ -0,0 +1,72 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <errno.h>
#include "conf-parser.h"
#include "kernel-config.h"
#include "macro.h"
#include "path-util.h"
#include "strv.h"
int load_kernel_install_conf(
const char *root,
const char *conf_root,
char **ret_machine_id,
char **ret_boot_root,
char **ret_layout,
char **ret_initrd_generator,
char **ret_uki_generator) {
_cleanup_free_ char *machine_id = NULL, *boot_root = NULL, *layout = NULL,
*initrd_generator = NULL, *uki_generator = NULL;
const ConfigTableItem items[] = {
{ NULL, "MACHINE_ID", config_parse_string, 0, &machine_id },
{ NULL, "BOOT_ROOT", config_parse_string, 0, &boot_root },
{ NULL, "layout", config_parse_string, 0, &layout },
{ NULL, "initrd_generator", config_parse_string, 0, &initrd_generator },
{ NULL, "uki_generator", config_parse_string, 0, &uki_generator },
{}
};
int r;
if (conf_root) {
_cleanup_free_ char *conf = path_join(conf_root, "install.conf");
if (!conf)
return log_oom();
r = config_parse_many(
STRV_MAKE_CONST(conf),
STRV_MAKE_CONST(conf_root),
"install.conf.d",
/* root= */ NULL, /* $KERNEL_INSTALL_CONF_ROOT and --root are independent */
/* sections= */ NULL,
config_item_table_lookup, items,
CONFIG_PARSE_WARN,
/* userdata= */ NULL,
/* ret_stats_by_path= */ NULL,
/* ret_dropin_files= */ NULL);
} else
r = config_parse_standard_file_with_dropins_full(
root,
"kernel/install.conf",
/* sections= */ NULL,
config_item_table_lookup, items,
CONFIG_PARSE_WARN,
/* userdata= */ NULL,
/* ret_stats_by_path= */ NULL,
/* ret_dropin_files= */ NULL);
if (r < 0 && r != -ENOENT)
return r;
if (ret_machine_id)
*ret_machine_id = TAKE_PTR(machine_id);
if (ret_boot_root)
*ret_boot_root = TAKE_PTR(boot_root);
if (ret_layout)
*ret_layout = TAKE_PTR(layout);
if (ret_initrd_generator)
*ret_initrd_generator = TAKE_PTR(initrd_generator);
if (ret_uki_generator)
*ret_uki_generator = TAKE_PTR(uki_generator);
return r >= 0; /* Return 0 if we got -ENOENT above, 1 otherwise. */
}

View File

@ -0,0 +1,10 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
int load_kernel_install_conf(
const char *root,
const char *conf_root,
char **ret_machine_id,
char **ret_boot_root,
char **ret_layout,
char **ret_initrd_generator,
char **ret_uki_generator);

View File

@ -98,6 +98,7 @@ shared_sources = files(
'journal-util.c',
'json.c',
'kbd-util.c',
'kernel-config.c',
'kernel-image.c',
'keyring-util.c',
'killall.c',