kernel-install: make "inspect" work more like "add" regarding omission of parameters

This makes "kernel-install inspect" work more "kernel-install add": if
the version or kernel image is specified as "-" or omitted we'll make it
up.
This commit is contained in:
Lennart Poettering 2023-11-06 18:25:46 +01:00
parent ec1399f567
commit 758d14767d
2 changed files with 69 additions and 31 deletions

View file

@ -200,13 +200,15 @@
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>
<term> <term>
<command>inspect [[<replaceable>KERNEL-VERSION</replaceable>] <replaceable>KERNEL-IMAGE</replaceable>] [<replaceable>INITRD-FILE</replaceable> ...]</command> <command>inspect [[[<replaceable>KERNEL-VERSION</replaceable>] <replaceable>KERNEL-IMAGE</replaceable>] [<replaceable>INITRD-FILE</replaceable> ...]]</command>
</term> </term>
<listitem> <listitem>
<para>Takes the same parameters as <command>add</command>.</para>
<para>Shows the various paths and parameters configured or auto-detected. In particular shows the <para>Shows the various paths and parameters configured or auto-detected. In particular shows the
values of the various <varname>$KERNEL_INSTALL_*</varname> environment variables listed values of the various <varname>$KERNEL_INSTALL_*</varname> environment variables listed below, as
below. The <option>--json</option> option can be used to get the output of this verb as a JSON they would be passed to plugins. The <option>--json</option> option can be used to get the output
object.</para> of this verb as a JSON object.</para>
<xi:include href="version-info.xml" xpointer="v251"/> <xi:include href="version-info.xml" xpointer="v251"/>
</listitem> </listitem>

View file

@ -1126,6 +1126,28 @@ static int do_add(
return context_execute(c); return context_execute(c);
} }
static int kernel_from_version(const char *version, char **ret_kernel) {
_cleanup_free_ char *vmlinuz = NULL;
int r;
assert(version);
vmlinuz = path_join("/usr/lib/modules/", version, "/vmlinuz");
if (!vmlinuz)
return log_oom();
r = laccess(vmlinuz, F_OK);
if (r < 0) {
if (r == -ENOENT)
return log_error_errno(r, "Kernel image not installed to '%s', requiring manual kernel image path specification.", vmlinuz);
return log_error_errno(r, "Failed to determin if kernel image is installed to '%s': %m", vmlinuz);
}
*ret_kernel = TAKE_PTR(vmlinuz);
return 0;
}
static int verb_add(int argc, char *argv[], void *userdata) { static int verb_add(int argc, char *argv[], void *userdata) {
Context *c = ASSERT_PTR(userdata); Context *c = ASSERT_PTR(userdata);
_cleanup_free_ char *vmlinuz = NULL; _cleanup_free_ char *vmlinuz = NULL;
@ -1158,17 +1180,9 @@ static int verb_add(int argc, char *argv[], void *userdata) {
} }
if (!kernel) { if (!kernel) {
vmlinuz = path_join("/usr/lib/modules/", version, "/vmlinuz"); r = kernel_from_version(version, &vmlinuz);
if (!vmlinuz) if (r < 0)
return log_oom(); return r;
r = laccess(vmlinuz, F_OK);
if (r < 0) {
if (r == -ENOENT)
return log_error_errno(r, "Kernel image not installed to '%s', requiring manual kernel image path specification.", vmlinuz);
return log_error_errno(r, "Failed to determin if kernel image is installed to '%s': %m", vmlinuz);
}
kernel = vmlinuz; kernel = vmlinuz;
} }
@ -1301,28 +1315,49 @@ static int verb_remove(int argc, char *argv[], void *userdata) {
static int verb_inspect(int argc, char *argv[], void *userdata) { static int verb_inspect(int argc, char *argv[], void *userdata) {
Context *c = ASSERT_PTR(userdata); Context *c = ASSERT_PTR(userdata);
_cleanup_(table_unrefp) Table *t = NULL; _cleanup_(table_unrefp) Table *t = NULL;
_cleanup_free_ char *vmlinuz = NULL;
const char *version, *kernel;
char **initrds;
struct utsname un;
int r; int r;
c->action = ACTION_INSPECT; c->action = ACTION_INSPECT;
if (argc == 2) { /* When only a single parameter is specified 'inspect' it's the kernel image path, and not the kernel
r = context_set_kernel(c, argv[1]); * version. i.e. it's the first argument that is optional, not the 2nd. That's a bit unfortunate, but
if (r < 0) * we keep the behaviour for compatibility. If users want to specify only the version (and have the
return r; * kernel image path derived automatically), then they may specify an empty string or "dash" as
} else if (argc >= 3) { * kernel image path. */
r = context_set_version(c, argv[1]); version = argc > 2 ? empty_or_dash_to_null(argv[1]) : NULL;
if (r < 0) kernel = argc > 2 ? empty_or_dash_to_null(argv[2]) :
return r; (argc > 1 ? empty_or_dash_to_null(argv[1]) : NULL);
initrds = strv_skip(argv, 3);
r = context_set_kernel(c, argv[2]); if (!version) {
if (r < 0) assert_se(uname(&un) >= 0);
return r; version = un.release;
r = context_set_initrds(c, strv_skip(argv, 3));
if (r < 0)
return r;
} }
if (!kernel) {
r = kernel_from_version(version, &vmlinuz);
if (r < 0)
return r;
kernel = vmlinuz;
}
r = context_set_version(c, version);
if (r < 0)
return r;
r = context_set_kernel(c, kernel);
if (r < 0)
return r;
r = context_set_initrds(c, initrds);
if (r < 0)
return r;
r = context_prepare_execution(c); r = context_prepare_execution(c);
if (r < 0) if (r < 0)
return r; return r;
@ -1464,7 +1499,8 @@ static int help(void) {
" kernel-install [OPTIONS...] add [[[KERNEL-VERSION] KERNEL-IMAGE] [INITRD ...]]\n" " kernel-install [OPTIONS...] add [[[KERNEL-VERSION] KERNEL-IMAGE] [INITRD ...]]\n"
" kernel-install [OPTIONS...] add-all\n" " kernel-install [OPTIONS...] add-all\n"
" kernel-install [OPTIONS...] remove KERNEL-VERSION\n" " kernel-install [OPTIONS...] remove KERNEL-VERSION\n"
" kernel-install [OPTIONS...] inspect [KERNEL-VERSION] KERNEL-IMAGE [INITRD ...]\n" " kernel-install [OPTIONS...] inspect [[[KERNEL-VERSION] KERNEL-IMAGE]\n"
" [INITRD ...]]\n"
" kernel-install [OPTIONS...] list\n" " kernel-install [OPTIONS...] list\n"
"\n%3$sOptions:%4$s\n" "\n%3$sOptions:%4$s\n"
" -h --help Show this help\n" " -h --help Show this help\n"