[kmod,v2,3/4] kmod: Add config command to show compile time configuration as JSON

Message ID 20230712140103.5468-3-msuchanek@suse.de
State New
Headers
Series None |

Commit Message

Michal Suchánek July 12, 2023, 2 p.m. UTC
  Show prefix (where configuration files are searched/to be installed),
module compressions, and module signatures supported.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
v2: mention module signature in commit message
---
 man/kmod.xml |  6 ++++++
 tools/kmod.c | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)
  

Comments

Jan Engelhardt July 14, 2023, 1:52 p.m. UTC | #1
On Wednesday 2023-07-12 16:00, Michal Suchanek wrote:

>Show prefix (where configuration files are searched/to be installed),
>module compressions, and module signatures supported.

What about doing it like systemd and generate a .pc file instead 
that can then be queried like so, e.g.:

$ pkg-config kmod --variable=modulesdir
/usr/lib/modules
  
Michal Suchánek July 14, 2023, 2:02 p.m. UTC | #2
Hello,

On Fri, Jul 14, 2023 at 03:52:05PM +0200, Jan Engelhardt wrote:
> 
> On Wednesday 2023-07-12 16:00, Michal Suchanek wrote:
> 
> >Show prefix (where configuration files are searched/to be installed),
> >module compressions, and module signatures supported.
> 
> What about doing it like systemd and generate a .pc file instead 
> that can then be queried like so, e.g.:
> 
> $ pkg-config kmod --variable=modulesdir
> /usr/lib/modules

 - AFAICS tools packed with kernel generate but do not consume .pc files
   while JSON and jq are commonly used througout the kernel ecosystem
 - .pc files would be shipped with libkmod development package, not the
   kmod tool in a binary distribution

Other than that JSON and .pc files are roughly quivalent in usability.

Thanks

Michal
  
Jan Engelhardt July 14, 2023, 2:12 p.m. UTC | #3
On Friday 2023-07-14 16:02, Michal Suchánek wrote:
>> 
>> What about doing it like systemd and generate a .pc file instead 
>> that can then be queried like so, e.g.:
>> 
>> $ pkg-config kmod --variable=modulesdir
>
> - .pc files would be shipped with libkmod development package, not the
>   kmod tool in a binary distribution

On that point: No, they would not.
Again, confer with systemd: systemd.rpm(SUSE) provides systemd.pc,
systemd-devel.rpm provides libsystemd.pc. kmod.rpm would provide
kmod.pc, kmod-devel.rpm would continue to provide libkmod.pc as
it does today.
  
Nicolas Schier July 14, 2023, 3:26 p.m. UTC | #4
On Wed, Jul 12, 2023 at 04:00:47PM +0200 Michal Suchanek wrote:
> Show prefix (where configuration files are searched/to be installed),
> module compressions, and module signatures supported.
> 
> Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> ---
> v2: mention module signature in commit message
> ---
>  man/kmod.xml |  6 ++++++
>  tools/kmod.c | 39 +++++++++++++++++++++++++++++++++++++++
>  2 files changed, 45 insertions(+)
> 
> diff --git a/man/kmod.xml b/man/kmod.xml
> index 0706ad58c2cc..f992a500f836 100644
> --- a/man/kmod.xml
> +++ b/man/kmod.xml
> @@ -71,6 +71,12 @@
>            <para>Show the help message.</para>
>          </listitem>
>        </varlistentry>
> +      <varlistentry>
> +        <term><command>config</command></term>
> +        <listitem>
> +          <para>Show compile time options in JSON.</para>
> +        </listitem>
> +      </varlistentry>
>        <varlistentry>
>          <term><command>list</command></term>
>          <listitem>
> diff --git a/tools/kmod.c b/tools/kmod.c
> index 55689c075ab1..5a13716955c1 100644
> --- a/tools/kmod.c
> +++ b/tools/kmod.c
> @@ -37,9 +37,11 @@ static const struct option options[] = {
>  };
>  
>  static const struct kmod_cmd kmod_cmd_help;
> +static const struct kmod_cmd kmod_cmd_config;
>  
>  static const struct kmod_cmd *kmod_cmds[] = {
>  	&kmod_cmd_help,
> +	&kmod_cmd_config,
>  	&kmod_cmd_list,
>  	&kmod_cmd_static_nodes,
>  
> @@ -95,6 +97,43 @@ static const struct kmod_cmd kmod_cmd_help = {
>  	.help = "Show help message",
>  };
>  
> +static const char *compressions[] = {
> +#ifdef ENABLE_ZSTD
> +			"zstd",
> +#endif
> +#ifdef ENABLE_XZ
> +			"xz",
> +#endif
> +#ifdef ENABLE_ZLIB
> +			"gz",
> +#endif
> +			NULL
> +};
> +
> +static int kmod_config(int argc, char *argv[])
> +{
> +	unsigned i;
> +	printf("{\"prefix\":\"" PREFIX "\""
> +			",\"module_signature\":["
> +#ifdef ENABLE_OPENSSL
> +			"\"PKCS#7\","
> +#endif
> +			"\"legacy\"]"
> +			",\"module_compression\":[");
> +	for(i = 0; compressions[i]; i++) {
> +		printf("%s\"%s\"", i ? "," : "", compressions[i]);
> +	}
> +	printf("]}\n");
> +
> +	return EXIT_SUCCESS;
> +}
> +
> +static const struct kmod_cmd kmod_cmd_config = {
> +	.name = "config",
> +	.cmd = kmod_config,
> +	.help = "Show compile time options in JSON",
> +};
> +
>  static int handle_kmod_commands(int argc, char *argv[])
>  {
>  	const char *cmd;
> -- 
> 2.41.0

If kmod could show selected configs without some (JSON) syntax
around, it could simplify its proposed use in kbuild. E.g.:

    kmod config prefix 2>/dev/null

instead of

    kmod config &>/dev/null && kmod config | jq -r .prefix
.
  
Michal Suchánek July 14, 2023, 4:58 p.m. UTC | #5
On Fri, Jul 14, 2023 at 05:26:43PM +0200, Nicolas Schier wrote:
> On Wed, Jul 12, 2023 at 04:00:47PM +0200 Michal Suchanek wrote:
> > Show prefix (where configuration files are searched/to be installed),
> > module compressions, and module signatures supported.
> > 
> > Signed-off-by: Michal Suchanek <msuchanek@suse.de>
> > ---
> > v2: mention module signature in commit message
> > ---
> >  man/kmod.xml |  6 ++++++
> >  tools/kmod.c | 39 +++++++++++++++++++++++++++++++++++++++
> >  2 files changed, 45 insertions(+)
> > 
> > diff --git a/man/kmod.xml b/man/kmod.xml
> > index 0706ad58c2cc..f992a500f836 100644
> > --- a/man/kmod.xml
> > +++ b/man/kmod.xml
> > @@ -71,6 +71,12 @@
> >            <para>Show the help message.</para>
> >          </listitem>
> >        </varlistentry>
> > +      <varlistentry>
> > +        <term><command>config</command></term>
> > +        <listitem>
> > +          <para>Show compile time options in JSON.</para>
> > +        </listitem>
> > +      </varlistentry>
> >        <varlistentry>
> >          <term><command>list</command></term>
> >          <listitem>
> > diff --git a/tools/kmod.c b/tools/kmod.c
> > index 55689c075ab1..5a13716955c1 100644
> > --- a/tools/kmod.c
> > +++ b/tools/kmod.c
> > @@ -37,9 +37,11 @@ static const struct option options[] = {
> >  };
> >  
> >  static const struct kmod_cmd kmod_cmd_help;
> > +static const struct kmod_cmd kmod_cmd_config;
> >  
> >  static const struct kmod_cmd *kmod_cmds[] = {
> >  	&kmod_cmd_help,
> > +	&kmod_cmd_config,
> >  	&kmod_cmd_list,
> >  	&kmod_cmd_static_nodes,
> >  
> > @@ -95,6 +97,43 @@ static const struct kmod_cmd kmod_cmd_help = {
> >  	.help = "Show help message",
> >  };
> >  
> > +static const char *compressions[] = {
> > +#ifdef ENABLE_ZSTD
> > +			"zstd",
> > +#endif
> > +#ifdef ENABLE_XZ
> > +			"xz",
> > +#endif
> > +#ifdef ENABLE_ZLIB
> > +			"gz",
> > +#endif
> > +			NULL
> > +};
> > +
> > +static int kmod_config(int argc, char *argv[])
> > +{
> > +	unsigned i;
> > +	printf("{\"prefix\":\"" PREFIX "\""
> > +			",\"module_signature\":["
> > +#ifdef ENABLE_OPENSSL
> > +			"\"PKCS#7\","
> > +#endif
> > +			"\"legacy\"]"
> > +			",\"module_compression\":[");
> > +	for(i = 0; compressions[i]; i++) {
> > +		printf("%s\"%s\"", i ? "," : "", compressions[i]);
> > +	}
> > +	printf("]}\n");
> > +
> > +	return EXIT_SUCCESS;
> > +}
> > +
> > +static const struct kmod_cmd kmod_cmd_config = {
> > +	.name = "config",
> > +	.cmd = kmod_config,
> > +	.help = "Show compile time options in JSON",
> > +};
> > +
> >  static int handle_kmod_commands(int argc, char *argv[])
> >  {
> >  	const char *cmd;
> > -- 
> > 2.41.0
> 
> If kmod could show selected configs without some (JSON) syntax
> around, it could simplify its proposed use in kbuild. E.g.:
> 
>     kmod config prefix 2>/dev/null
> 
> instead of
> 
>     kmod config &>/dev/null && kmod config | jq -r .prefix

Which would no longer hold for whole module directory:

      kmod config &>/dev/null && kmod config | jq -r .module_directory || echo /lib/modules

      vs

      kmod config module_directory &>/dev/null && kmod config module_directory || echo /lib/modules

Also JSON has standardized syntax for lists and users that can parse
JSON directly can load the whole configuration at once without several
calls to kmod config or pkg-config.

Thanks

Michal
  

Patch

diff --git a/man/kmod.xml b/man/kmod.xml
index 0706ad58c2cc..f992a500f836 100644
--- a/man/kmod.xml
+++ b/man/kmod.xml
@@ -71,6 +71,12 @@ 
           <para>Show the help message.</para>
         </listitem>
       </varlistentry>
+      <varlistentry>
+        <term><command>config</command></term>
+        <listitem>
+          <para>Show compile time options in JSON.</para>
+        </listitem>
+      </varlistentry>
       <varlistentry>
         <term><command>list</command></term>
         <listitem>
diff --git a/tools/kmod.c b/tools/kmod.c
index 55689c075ab1..5a13716955c1 100644
--- a/tools/kmod.c
+++ b/tools/kmod.c
@@ -37,9 +37,11 @@  static const struct option options[] = {
 };
 
 static const struct kmod_cmd kmod_cmd_help;
+static const struct kmod_cmd kmod_cmd_config;
 
 static const struct kmod_cmd *kmod_cmds[] = {
 	&kmod_cmd_help,
+	&kmod_cmd_config,
 	&kmod_cmd_list,
 	&kmod_cmd_static_nodes,
 
@@ -95,6 +97,43 @@  static const struct kmod_cmd kmod_cmd_help = {
 	.help = "Show help message",
 };
 
+static const char *compressions[] = {
+#ifdef ENABLE_ZSTD
+			"zstd",
+#endif
+#ifdef ENABLE_XZ
+			"xz",
+#endif
+#ifdef ENABLE_ZLIB
+			"gz",
+#endif
+			NULL
+};
+
+static int kmod_config(int argc, char *argv[])
+{
+	unsigned i;
+	printf("{\"prefix\":\"" PREFIX "\""
+			",\"module_signature\":["
+#ifdef ENABLE_OPENSSL
+			"\"PKCS#7\","
+#endif
+			"\"legacy\"]"
+			",\"module_compression\":[");
+	for(i = 0; compressions[i]; i++) {
+		printf("%s\"%s\"", i ? "," : "", compressions[i]);
+	}
+	printf("]}\n");
+
+	return EXIT_SUCCESS;
+}
+
+static const struct kmod_cmd kmod_cmd_config = {
+	.name = "config",
+	.cmd = kmod_config,
+	.help = "Show compile time options in JSON",
+};
+
 static int handle_kmod_commands(int argc, char *argv[])
 {
 	const char *cmd;