diff mbox series

[v2] depmod: Introduce outdir option

Message ID 20230206131834.559229-1-emil.l.velikov@gmail.com (mailing list archive)
State New, archived
Headers show
Series [v2] depmod: Introduce outdir option | expand

Commit Message

Emil Velikov Feb. 6, 2023, 1:18 p.m. UTC
From: Emil Velikov <emil.velikov@collabora.com>

This option is equivalent to basedir, with the small difference being
that's where the meta-data files are generated. In other words, this
allows us to have read-only input modules and modules.dep, while still
being able to generate the meta-data files.

Cc: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
---
Here's a handy feature behind the request at
https://github.com/kmod-project/kmod/issues/13

v2:
 - alternative wording for manpage and help screen (thanks Lucas)
 - add test case

NOTE: skipping the test (as well as some surrounding ones) seems
dubious, but it's added for consistency. Will send another series with
papercuts shortly, to drop that... if applicable that is :-)
---
 man/depmod.xml                                | 20 ++++++++++
 testsuite/populate-modules.sh                 |  3 ++
 .../lib/modules/4.4.4/modules.alias           | 37 +++++++++++++++++++
 .../lib/modules/4.4.4/modules.builtin         |  0
 .../lib/modules/4.4.4/modules.order           |  7 ++++
 testsuite/test-depmod.c                       | 34 +++++++++++++++++
 tools/depmod.c                                | 25 +++++++++++--
 7 files changed, 123 insertions(+), 3 deletions(-)
 create mode 100644 testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.alias
 create mode 100644 testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.builtin
 create mode 100644 testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.order

Comments

Lucas De Marchi Feb. 8, 2023, 6:18 p.m. UTC | #1
On Mon, Feb 06, 2023 at 01:18:34PM +0000, Emil Velikov wrote:
>From: Emil Velikov <emil.velikov@collabora.com>
>
>This option is equivalent to basedir, with the small difference being
>that's where the meta-data files are generated. In other words, this
>allows us to have read-only input modules and modules.dep, while still
>being able to generate the meta-data files.
>
>Cc: Lucas De Marchi <lucas.demarchi@intel.com>
>Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
>---
>Here's a handy feature behind the request at
>https://github.com/kmod-project/kmod/issues/13
>
>v2:
> - alternative wording for manpage and help screen (thanks Lucas)
> - add test case
>
>NOTE: skipping the test (as well as some surrounding ones) seems
>dubious, but it's added for consistency. Will send another series with
>papercuts shortly, to drop that... if applicable that is :-)
>---
> man/depmod.xml                                | 20 ++++++++++
> testsuite/populate-modules.sh                 |  3 ++
> .../lib/modules/4.4.4/modules.alias           | 37 +++++++++++++++++++
> .../lib/modules/4.4.4/modules.builtin         |  0
> .../lib/modules/4.4.4/modules.order           |  7 ++++
> testsuite/test-depmod.c                       | 34 +++++++++++++++++
> tools/depmod.c                                | 25 +++++++++++--
> 7 files changed, 123 insertions(+), 3 deletions(-)
> create mode 100644 testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.alias
> create mode 100644 testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.builtin
> create mode 100644 testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.order
>
>diff --git a/man/depmod.xml b/man/depmod.xml
>index ea0be27..3b00971 100644
>--- a/man/depmod.xml
>+++ b/man/depmod.xml
>@@ -45,6 +45,7 @@
>     <cmdsynopsis>
>       <command>depmod</command>
>       <arg><option>-b <replaceable>basedir</replaceable></option></arg>
>+      <arg><option>-o <replaceable>outdir</replaceable></option></arg>
>       <arg><option>-e</option></arg>
>       <arg><option>-E <replaceable>Module.symvers</replaceable></option></arg>
>       <arg><option>-F <replaceable>System.map</replaceable></option></arg>
>@@ -151,6 +152,25 @@
>           </para>
>         </listitem>
>       </varlistentry>
>+      <varlistentry>
>+        <term>
>+          <option>-o <replaceable>outdir</replaceable></option>
>+        </term>
>+        <term>
>+          <option>--outdir <replaceable>outdir</replaceable></option>
>+        </term>
>+        <listitem>
>+          <para>
>+            Set the output directory where depmod will store any generated file.
>+            <replaceable>outdir</replaceable> serves as a root to that location,
>+            similar to how <replaceable>basedir</replaceable> is used. Also this
>+            setting takes precedence and if used together with
>+            <replaceable>basedir</replaceable> it will result in the input being
>+            that directory, but the output being the one set by
>+            <replaceable>outdir</replaceable>.
>+          </para>
>+        </listitem>
>+      </varlistentry>
>       <varlistentry>
>         <term>
>           <option>-C</option>
>diff --git a/testsuite/populate-modules.sh b/testsuite/populate-modules.sh
>index aa6d5c2..5009cac 100755
>--- a/testsuite/populate-modules.sh
>+++ b/testsuite/populate-modules.sh
>@@ -61,6 +61,9 @@ map=(
>     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
>     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
>     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
>+    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
>+    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
>+    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
>     ["test-modinfo/mod-simple-i386.ko"]="mod-simple-i386.ko"
>     ["test-modinfo/mod-simple-x86_64.ko"]="mod-simple-x86_64.ko"
>     ["test-modinfo/mod-simple-sparc64.ko"]="mod-simple-sparc64.ko"
>diff --git a/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.alias b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.alias
>new file mode 100644
>index 0000000..5675329
>--- /dev/null
>+++ b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.alias
>@@ -0,0 +1,37 @@
>+# Aliases extracted from modules themselves.
>+alias pci:v0000103Cd00003230sv0000103Csd0000323Dbc*sc*i* cciss
>+alias pci:v0000103Cd00003230sv0000103Csd00003237bc*sc*i* cciss
>+alias pci:v0000103Cd00003238sv0000103Csd00003215bc*sc*i* cciss
>+alias pci:v0000103Cd00003238sv0000103Csd00003214bc*sc*i* cciss
>+alias pci:v0000103Cd00003238sv0000103Csd00003213bc*sc*i* cciss
>+alias pci:v0000103Cd00003238sv0000103Csd00003212bc*sc*i* cciss
>+alias pci:v0000103Cd00003238sv0000103Csd00003211bc*sc*i* cciss
>+alias pci:v0000103Cd00003230sv0000103Csd00003235bc*sc*i* cciss
>+alias pci:v0000103Cd00003230sv0000103Csd00003234bc*sc*i* cciss
>+alias pci:v0000103Cd00003230sv0000103Csd00003223bc*sc*i* cciss
>+alias pci:v0000103Cd00003220sv0000103Csd00003225bc*sc*i* cciss
>+alias pci:v00000E11d00000046sv00000E11sd0000409Dbc*sc*i* cciss
>+alias pci:v00000E11d00000046sv00000E11sd0000409Cbc*sc*i* cciss
>+alias pci:v00000E11d00000046sv00000E11sd0000409Bbc*sc*i* cciss
>+alias pci:v00000E11d00000046sv00000E11sd0000409Abc*sc*i* cciss
>+alias pci:v00000E11d00000046sv00000E11sd00004091bc*sc*i* cciss
>+alias pci:v00000E11d0000B178sv00000E11sd00004083bc*sc*i* cciss
>+alias pci:v00000E11d0000B178sv00000E11sd00004082bc*sc*i* cciss
>+alias pci:v00000E11d0000B178sv00000E11sd00004080bc*sc*i* cciss
>+alias pci:v00000E11d0000B060sv00000E11sd00004070bc*sc*i* cciss
>+alias pci:v0000103Cd*sv*sd*bc01sc04i* hpsa
>+alias pci:v0000103Cd0000323Bsv0000103Csd00003356bc*sc*i* hpsa
>+alias pci:v0000103Cd0000323Bsv0000103Csd00003355bc*sc*i* hpsa
>+alias pci:v0000103Cd0000323Bsv0000103Csd00003354bc*sc*i* hpsa
>+alias pci:v0000103Cd0000323Bsv0000103Csd00003353bc*sc*i* hpsa
>+alias pci:v0000103Cd0000323Bsv0000103Csd00003352bc*sc*i* hpsa
>+alias pci:v0000103Cd0000323Bsv0000103Csd00003351bc*sc*i* hpsa
>+alias pci:v0000103Cd0000323Bsv0000103Csd00003350bc*sc*i* hpsa
>+alias pci:v0000103Cd0000323Asv0000103Csd00003233bc*sc*i* hpsa
>+alias pci:v0000103Cd0000323Asv0000103Csd0000324Bbc*sc*i* hpsa
>+alias pci:v0000103Cd0000323Asv0000103Csd0000324Abc*sc*i* hpsa
>+alias pci:v0000103Cd0000323Asv0000103Csd00003249bc*sc*i* hpsa
>+alias pci:v0000103Cd0000323Asv0000103Csd00003247bc*sc*i* hpsa
>+alias pci:v0000103Cd0000323Asv0000103Csd00003245bc*sc*i* hpsa
>+alias pci:v0000103Cd0000323Asv0000103Csd00003243bc*sc*i* hpsa
>+alias pci:v0000103Cd0000323Asv0000103Csd00003241bc*sc*i* hpsa
>diff --git a/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.builtin b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.builtin
>new file mode 100644
>index 0000000..e69de29
>diff --git a/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.order b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.order
>new file mode 100644
>index 0000000..4b64309
>--- /dev/null
>+++ b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.order
>@@ -0,0 +1,7 @@
>+#336
>+kernel/drivers/block/cciss.ko
>+#2094
>+kernel/drivers/scsi/scsi_mod.ko
>+#2137
>+kernel/drivers/scsi/hpsa.ko
>+
>diff --git a/testsuite/test-depmod.c b/testsuite/test-depmod.c
>index d7802d7..6465230 100644
>--- a/testsuite/test-depmod.c
>+++ b/testsuite/test-depmod.c
>@@ -57,6 +57,40 @@ DEFINE_TEST(depmod_modules_order_for_compressed,
> 		},
> 	});
>
>+#define MODULES_OUTDIR_UNAME "4.4.4"
>+#define MODULES_OUTDIR_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-outdir"
>+#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir/lib/modules/" MODULES_OUTDIR_UNAME
>+#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS "/lib/modules/" MODULES_OUTDIR_UNAME
>+static noreturn int depmod_modules_outdir(const struct test *t)
>+{
>+	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
>+	const char *const args[] = {
>+		progname,
>+		"--outdir", MODULES_OUTDIR_ROOTFS "/outdir/",
>+		NULL,
>+	};
>+
>+	test_spawn_prog(progname, args);
>+	exit(EXIT_FAILURE);
>+}
>+
>+DEFINE_TEST(depmod_modules_outdir,
>+#if defined(KMOD_SYSCONFDIR_NOT_ETC)
>+        .skip = true,
>+#endif
>+	.description = "check if depmod honours the outdir option",
>+	.config = {
>+		[TC_UNAME_R] = MODULES_OUTDIR_UNAME,
>+		[TC_ROOTFS] = MODULES_OUTDIR_ROOTFS,
>+	},
>+	.output = {
>+		.files = (const struct keyval[]) {
>+			{ MODULES_OUTDIR_LIB_MODULES_OUTPUT "/modules.alias",
>+			  MODULES_OUTDIR_LIB_MODULES_INPUT "/modules.alias" },

I think this is a little bit weird because modules.alias is not really
used as input. It's always produced by depmod, i.e. an output I propose we do 2 things:

1) move the correct files we are using to compare to MODULES_OUTDIR_ROOTFS
2) also compare modules.dep, because we then ensure the outdirname is
being correctly stripped off the output.

I just gave this a try with the additional diff on top. If you agree, I
can squash in your commit before pushing.

	$ ./testsuite/test-depmod depmod_modules_outdir
	TESTSUITE: running depmod_modules_outdir, in forked context
	depmod: WARNING: could not open modules.builtin.modinfo at /lib/modules/4.4.4: No such file or directory
	TESTSUITE: 'depmod_modules_outdir' [56832] exited with return code 0
	TESTSUITE: PASSED: depmod_modules_outdir
	TESTSUITE: ------

--------8<-------
diff --git a/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.alias b/testsuite/rootfs-pristine/test-depmod/modules-outdir/correct-modules.alias
similarity index 100%
rename from testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.alias
rename to testsuite/rootfs-pristine/test-depmod/modules-outdir/correct-modules.alias
diff --git a/testsuite/rootfs-pristine/test-depmod/modules-outdir/correct-modules.dep b/testsuite/rootfs-pristine/test-depmod/modules-outdir/correct-modules.dep
new file mode 100644
index 0000000..ec50ac3
--- /dev/null
+++ b/testsuite/rootfs-pristine/test-depmod/modules-outdir/correct-modules.dep
@@ -0,0 +1,3 @@
+kernel/drivers/block/cciss.ko:
+kernel/drivers/scsi/scsi_mod.ko:
+kernel/drivers/scsi/hpsa.ko: kernel/drivers/scsi/scsi_mod.ko
diff --git a/testsuite/test-depmod.c b/testsuite/test-depmod.c
index 6465230..6e3ae56 100644
--- a/testsuite/test-depmod.c
+++ b/testsuite/test-depmod.c
@@ -85,8 +85,10 @@ DEFINE_TEST(depmod_modules_outdir,
  	},
  	.output = {
  		.files = (const struct keyval[]) {
+			{ MODULES_OUTDIR_LIB_MODULES_OUTPUT "/modules.dep",
+			  MODULES_OUTDIR_ROOTFS "/correct-modules.dep" },
  			{ MODULES_OUTDIR_LIB_MODULES_OUTPUT "/modules.alias",
-			  MODULES_OUTDIR_LIB_MODULES_INPUT "/modules.alias" },
+			  MODULES_OUTDIR_ROOTFS "/correct-modules.alias" },
  			{ }
  		},
  	});
--------8<-------


thanks
Lucas De Marchi
Emil Velikov Feb. 9, 2023, 2:15 p.m. UTC | #2
On Wed, 8 Feb 2023 at 18:19, Lucas De Marchi <lucas.demarchi@intel.com> wrote:
>
> On Mon, Feb 06, 2023 at 01:18:34PM +0000, Emil Velikov wrote:
> >From: Emil Velikov <emil.velikov@collabora.com>
> >
> >This option is equivalent to basedir, with the small difference being
> >that's where the meta-data files are generated. In other words, this
> >allows us to have read-only input modules and modules.dep, while still
> >being able to generate the meta-data files.
> >
> >Cc: Lucas De Marchi <lucas.demarchi@intel.com>
> >Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
> >---
> >Here's a handy feature behind the request at
> >https://github.com/kmod-project/kmod/issues/13
> >
> >v2:
> > - alternative wording for manpage and help screen (thanks Lucas)
> > - add test case
> >
> >NOTE: skipping the test (as well as some surrounding ones) seems
> >dubious, but it's added for consistency. Will send another series with
> >papercuts shortly, to drop that... if applicable that is :-)
> >---
> > man/depmod.xml                                | 20 ++++++++++
> > testsuite/populate-modules.sh                 |  3 ++
> > .../lib/modules/4.4.4/modules.alias           | 37 +++++++++++++++++++
> > .../lib/modules/4.4.4/modules.builtin         |  0
> > .../lib/modules/4.4.4/modules.order           |  7 ++++
> > testsuite/test-depmod.c                       | 34 +++++++++++++++++
> > tools/depmod.c                                | 25 +++++++++++--
> > 7 files changed, 123 insertions(+), 3 deletions(-)
> > create mode 100644 testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.alias
> > create mode 100644 testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.builtin
> > create mode 100644 testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.order
> >
> >diff --git a/man/depmod.xml b/man/depmod.xml
> >index ea0be27..3b00971 100644
> >--- a/man/depmod.xml
> >+++ b/man/depmod.xml
> >@@ -45,6 +45,7 @@
> >     <cmdsynopsis>
> >       <command>depmod</command>
> >       <arg><option>-b <replaceable>basedir</replaceable></option></arg>
> >+      <arg><option>-o <replaceable>outdir</replaceable></option></arg>
> >       <arg><option>-e</option></arg>
> >       <arg><option>-E <replaceable>Module.symvers</replaceable></option></arg>
> >       <arg><option>-F <replaceable>System.map</replaceable></option></arg>
> >@@ -151,6 +152,25 @@
> >           </para>
> >         </listitem>
> >       </varlistentry>
> >+      <varlistentry>
> >+        <term>
> >+          <option>-o <replaceable>outdir</replaceable></option>
> >+        </term>
> >+        <term>
> >+          <option>--outdir <replaceable>outdir</replaceable></option>
> >+        </term>
> >+        <listitem>
> >+          <para>
> >+            Set the output directory where depmod will store any generated file.
> >+            <replaceable>outdir</replaceable> serves as a root to that location,
> >+            similar to how <replaceable>basedir</replaceable> is used. Also this
> >+            setting takes precedence and if used together with
> >+            <replaceable>basedir</replaceable> it will result in the input being
> >+            that directory, but the output being the one set by
> >+            <replaceable>outdir</replaceable>.
> >+          </para>
> >+        </listitem>
> >+      </varlistentry>
> >       <varlistentry>
> >         <term>
> >           <option>-C</option>
> >diff --git a/testsuite/populate-modules.sh b/testsuite/populate-modules.sh
> >index aa6d5c2..5009cac 100755
> >--- a/testsuite/populate-modules.sh
> >+++ b/testsuite/populate-modules.sh
> >@@ -61,6 +61,9 @@ map=(
> >     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
> >     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
> >     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
> >+    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
> >+    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
> >+    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
> >     ["test-modinfo/mod-simple-i386.ko"]="mod-simple-i386.ko"
> >     ["test-modinfo/mod-simple-x86_64.ko"]="mod-simple-x86_64.ko"
> >     ["test-modinfo/mod-simple-sparc64.ko"]="mod-simple-sparc64.ko"
> >diff --git a/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.alias b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.alias
> >new file mode 100644
> >index 0000000..5675329
> >--- /dev/null
> >+++ b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.alias
> >@@ -0,0 +1,37 @@
> >+# Aliases extracted from modules themselves.
> >+alias pci:v0000103Cd00003230sv0000103Csd0000323Dbc*sc*i* cciss
> >+alias pci:v0000103Cd00003230sv0000103Csd00003237bc*sc*i* cciss
> >+alias pci:v0000103Cd00003238sv0000103Csd00003215bc*sc*i* cciss
> >+alias pci:v0000103Cd00003238sv0000103Csd00003214bc*sc*i* cciss
> >+alias pci:v0000103Cd00003238sv0000103Csd00003213bc*sc*i* cciss
> >+alias pci:v0000103Cd00003238sv0000103Csd00003212bc*sc*i* cciss
> >+alias pci:v0000103Cd00003238sv0000103Csd00003211bc*sc*i* cciss
> >+alias pci:v0000103Cd00003230sv0000103Csd00003235bc*sc*i* cciss
> >+alias pci:v0000103Cd00003230sv0000103Csd00003234bc*sc*i* cciss
> >+alias pci:v0000103Cd00003230sv0000103Csd00003223bc*sc*i* cciss
> >+alias pci:v0000103Cd00003220sv0000103Csd00003225bc*sc*i* cciss
> >+alias pci:v00000E11d00000046sv00000E11sd0000409Dbc*sc*i* cciss
> >+alias pci:v00000E11d00000046sv00000E11sd0000409Cbc*sc*i* cciss
> >+alias pci:v00000E11d00000046sv00000E11sd0000409Bbc*sc*i* cciss
> >+alias pci:v00000E11d00000046sv00000E11sd0000409Abc*sc*i* cciss
> >+alias pci:v00000E11d00000046sv00000E11sd00004091bc*sc*i* cciss
> >+alias pci:v00000E11d0000B178sv00000E11sd00004083bc*sc*i* cciss
> >+alias pci:v00000E11d0000B178sv00000E11sd00004082bc*sc*i* cciss
> >+alias pci:v00000E11d0000B178sv00000E11sd00004080bc*sc*i* cciss
> >+alias pci:v00000E11d0000B060sv00000E11sd00004070bc*sc*i* cciss
> >+alias pci:v0000103Cd*sv*sd*bc01sc04i* hpsa
> >+alias pci:v0000103Cd0000323Bsv0000103Csd00003356bc*sc*i* hpsa
> >+alias pci:v0000103Cd0000323Bsv0000103Csd00003355bc*sc*i* hpsa
> >+alias pci:v0000103Cd0000323Bsv0000103Csd00003354bc*sc*i* hpsa
> >+alias pci:v0000103Cd0000323Bsv0000103Csd00003353bc*sc*i* hpsa
> >+alias pci:v0000103Cd0000323Bsv0000103Csd00003352bc*sc*i* hpsa
> >+alias pci:v0000103Cd0000323Bsv0000103Csd00003351bc*sc*i* hpsa
> >+alias pci:v0000103Cd0000323Bsv0000103Csd00003350bc*sc*i* hpsa
> >+alias pci:v0000103Cd0000323Asv0000103Csd00003233bc*sc*i* hpsa
> >+alias pci:v0000103Cd0000323Asv0000103Csd0000324Bbc*sc*i* hpsa
> >+alias pci:v0000103Cd0000323Asv0000103Csd0000324Abc*sc*i* hpsa
> >+alias pci:v0000103Cd0000323Asv0000103Csd00003249bc*sc*i* hpsa
> >+alias pci:v0000103Cd0000323Asv0000103Csd00003247bc*sc*i* hpsa
> >+alias pci:v0000103Cd0000323Asv0000103Csd00003245bc*sc*i* hpsa
> >+alias pci:v0000103Cd0000323Asv0000103Csd00003243bc*sc*i* hpsa
> >+alias pci:v0000103Cd0000323Asv0000103Csd00003241bc*sc*i* hpsa
> >diff --git a/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.builtin b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.builtin
> >new file mode 100644
> >index 0000000..e69de29
> >diff --git a/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.order b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.order
> >new file mode 100644
> >index 0000000..4b64309
> >--- /dev/null
> >+++ b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.order
> >@@ -0,0 +1,7 @@
> >+#336
> >+kernel/drivers/block/cciss.ko
> >+#2094
> >+kernel/drivers/scsi/scsi_mod.ko
> >+#2137
> >+kernel/drivers/scsi/hpsa.ko
> >+
> >diff --git a/testsuite/test-depmod.c b/testsuite/test-depmod.c
> >index d7802d7..6465230 100644
> >--- a/testsuite/test-depmod.c
> >+++ b/testsuite/test-depmod.c
> >@@ -57,6 +57,40 @@ DEFINE_TEST(depmod_modules_order_for_compressed,
> >               },
> >       });
> >
> >+#define MODULES_OUTDIR_UNAME "4.4.4"
> >+#define MODULES_OUTDIR_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-outdir"
> >+#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir/lib/modules/" MODULES_OUTDIR_UNAME
> >+#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS "/lib/modules/" MODULES_OUTDIR_UNAME
> >+static noreturn int depmod_modules_outdir(const struct test *t)
> >+{
> >+      const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
> >+      const char *const args[] = {
> >+              progname,
> >+              "--outdir", MODULES_OUTDIR_ROOTFS "/outdir/",
> >+              NULL,
> >+      };
> >+
> >+      test_spawn_prog(progname, args);
> >+      exit(EXIT_FAILURE);
> >+}
> >+
> >+DEFINE_TEST(depmod_modules_outdir,
> >+#if defined(KMOD_SYSCONFDIR_NOT_ETC)
> >+        .skip = true,
> >+#endif
> >+      .description = "check if depmod honours the outdir option",
> >+      .config = {
> >+              [TC_UNAME_R] = MODULES_OUTDIR_UNAME,
> >+              [TC_ROOTFS] = MODULES_OUTDIR_ROOTFS,
> >+      },
> >+      .output = {
> >+              .files = (const struct keyval[]) {
> >+                      { MODULES_OUTDIR_LIB_MODULES_OUTPUT "/modules.alias",
> >+                        MODULES_OUTDIR_LIB_MODULES_INPUT "/modules.alias" },
>
> I think this is a little bit weird because modules.alias is not really
> used as input. It's always produced by depmod, i.e. an output I propose we do 2 things:
>
> 1) move the correct files we are using to compare to MODULES_OUTDIR_ROOTFS
> 2) also compare modules.dep, because we then ensure the outdirname is
> being correctly stripped off the output.
>
> I just gave this a try with the additional diff on top. If you agree, I
> can squash in your commit before pushing.
>

Of course, the extra chunk looks reasonable, Thank you o/

Emil
Lucas De Marchi Feb. 9, 2023, 3:13 p.m. UTC | #3
On Mon, 06 Feb 2023 13:18:34 +0000, Emil Velikov wrote:
> This option is equivalent to basedir, with the small difference being
> that's where the meta-data files are generated. In other words, this
> allows us to have read-only input modules and modules.dep, while still
> being able to generate the meta-data files.
> 
> 

I amended the commit with the additional diff as I shared and applied. Thanks!

[1/1] depmod: Introduce outdir option
      commit: 1712a1548eeaad61143f303b09afcb4215943203

Best regards,
diff mbox series

Patch

diff --git a/man/depmod.xml b/man/depmod.xml
index ea0be27..3b00971 100644
--- a/man/depmod.xml
+++ b/man/depmod.xml
@@ -45,6 +45,7 @@ 
     <cmdsynopsis>
       <command>depmod</command>
       <arg><option>-b <replaceable>basedir</replaceable></option></arg>
+      <arg><option>-o <replaceable>outdir</replaceable></option></arg>
       <arg><option>-e</option></arg>
       <arg><option>-E <replaceable>Module.symvers</replaceable></option></arg>
       <arg><option>-F <replaceable>System.map</replaceable></option></arg>
@@ -151,6 +152,25 @@ 
           </para>
         </listitem>
       </varlistentry>
+      <varlistentry>
+        <term>
+          <option>-o <replaceable>outdir</replaceable></option>
+        </term>
+        <term>
+          <option>--outdir <replaceable>outdir</replaceable></option>
+        </term>
+        <listitem>
+          <para>
+            Set the output directory where depmod will store any generated file.
+            <replaceable>outdir</replaceable> serves as a root to that location,
+            similar to how <replaceable>basedir</replaceable> is used. Also this
+            setting takes precedence and if used together with
+            <replaceable>basedir</replaceable> it will result in the input being
+            that directory, but the output being the one set by
+            <replaceable>outdir</replaceable>.
+          </para>
+        </listitem>
+      </varlistentry>
       <varlistentry>
         <term>
           <option>-C</option>
diff --git a/testsuite/populate-modules.sh b/testsuite/populate-modules.sh
index aa6d5c2..5009cac 100755
--- a/testsuite/populate-modules.sh
+++ b/testsuite/populate-modules.sh
@@ -61,6 +61,9 @@  map=(
     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
     ["test-depmod/modules-order-compressed/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
+    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/block/cciss.ko"]="mod-fake-cciss.ko"
+    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/hpsa.ko"]="mod-fake-hpsa.ko"
+    ["test-depmod/modules-outdir/lib/modules/4.4.4/kernel/drivers/scsi/scsi_mod.ko"]="mod-fake-scsi-mod.ko"
     ["test-modinfo/mod-simple-i386.ko"]="mod-simple-i386.ko"
     ["test-modinfo/mod-simple-x86_64.ko"]="mod-simple-x86_64.ko"
     ["test-modinfo/mod-simple-sparc64.ko"]="mod-simple-sparc64.ko"
diff --git a/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.alias b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.alias
new file mode 100644
index 0000000..5675329
--- /dev/null
+++ b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.alias
@@ -0,0 +1,37 @@ 
+# Aliases extracted from modules themselves.
+alias pci:v0000103Cd00003230sv0000103Csd0000323Dbc*sc*i* cciss
+alias pci:v0000103Cd00003230sv0000103Csd00003237bc*sc*i* cciss
+alias pci:v0000103Cd00003238sv0000103Csd00003215bc*sc*i* cciss
+alias pci:v0000103Cd00003238sv0000103Csd00003214bc*sc*i* cciss
+alias pci:v0000103Cd00003238sv0000103Csd00003213bc*sc*i* cciss
+alias pci:v0000103Cd00003238sv0000103Csd00003212bc*sc*i* cciss
+alias pci:v0000103Cd00003238sv0000103Csd00003211bc*sc*i* cciss
+alias pci:v0000103Cd00003230sv0000103Csd00003235bc*sc*i* cciss
+alias pci:v0000103Cd00003230sv0000103Csd00003234bc*sc*i* cciss
+alias pci:v0000103Cd00003230sv0000103Csd00003223bc*sc*i* cciss
+alias pci:v0000103Cd00003220sv0000103Csd00003225bc*sc*i* cciss
+alias pci:v00000E11d00000046sv00000E11sd0000409Dbc*sc*i* cciss
+alias pci:v00000E11d00000046sv00000E11sd0000409Cbc*sc*i* cciss
+alias pci:v00000E11d00000046sv00000E11sd0000409Bbc*sc*i* cciss
+alias pci:v00000E11d00000046sv00000E11sd0000409Abc*sc*i* cciss
+alias pci:v00000E11d00000046sv00000E11sd00004091bc*sc*i* cciss
+alias pci:v00000E11d0000B178sv00000E11sd00004083bc*sc*i* cciss
+alias pci:v00000E11d0000B178sv00000E11sd00004082bc*sc*i* cciss
+alias pci:v00000E11d0000B178sv00000E11sd00004080bc*sc*i* cciss
+alias pci:v00000E11d0000B060sv00000E11sd00004070bc*sc*i* cciss
+alias pci:v0000103Cd*sv*sd*bc01sc04i* hpsa
+alias pci:v0000103Cd0000323Bsv0000103Csd00003356bc*sc*i* hpsa
+alias pci:v0000103Cd0000323Bsv0000103Csd00003355bc*sc*i* hpsa
+alias pci:v0000103Cd0000323Bsv0000103Csd00003354bc*sc*i* hpsa
+alias pci:v0000103Cd0000323Bsv0000103Csd00003353bc*sc*i* hpsa
+alias pci:v0000103Cd0000323Bsv0000103Csd00003352bc*sc*i* hpsa
+alias pci:v0000103Cd0000323Bsv0000103Csd00003351bc*sc*i* hpsa
+alias pci:v0000103Cd0000323Bsv0000103Csd00003350bc*sc*i* hpsa
+alias pci:v0000103Cd0000323Asv0000103Csd00003233bc*sc*i* hpsa
+alias pci:v0000103Cd0000323Asv0000103Csd0000324Bbc*sc*i* hpsa
+alias pci:v0000103Cd0000323Asv0000103Csd0000324Abc*sc*i* hpsa
+alias pci:v0000103Cd0000323Asv0000103Csd00003249bc*sc*i* hpsa
+alias pci:v0000103Cd0000323Asv0000103Csd00003247bc*sc*i* hpsa
+alias pci:v0000103Cd0000323Asv0000103Csd00003245bc*sc*i* hpsa
+alias pci:v0000103Cd0000323Asv0000103Csd00003243bc*sc*i* hpsa
+alias pci:v0000103Cd0000323Asv0000103Csd00003241bc*sc*i* hpsa
diff --git a/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.builtin b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.builtin
new file mode 100644
index 0000000..e69de29
diff --git a/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.order b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.order
new file mode 100644
index 0000000..4b64309
--- /dev/null
+++ b/testsuite/rootfs-pristine/test-depmod/modules-outdir/lib/modules/4.4.4/modules.order
@@ -0,0 +1,7 @@ 
+#336
+kernel/drivers/block/cciss.ko
+#2094
+kernel/drivers/scsi/scsi_mod.ko
+#2137
+kernel/drivers/scsi/hpsa.ko
+
diff --git a/testsuite/test-depmod.c b/testsuite/test-depmod.c
index d7802d7..6465230 100644
--- a/testsuite/test-depmod.c
+++ b/testsuite/test-depmod.c
@@ -57,6 +57,40 @@  DEFINE_TEST(depmod_modules_order_for_compressed,
 		},
 	});
 
+#define MODULES_OUTDIR_UNAME "4.4.4"
+#define MODULES_OUTDIR_ROOTFS TESTSUITE_ROOTFS "test-depmod/modules-outdir"
+#define MODULES_OUTDIR_LIB_MODULES_OUTPUT MODULES_OUTDIR_ROOTFS "/outdir/lib/modules/" MODULES_OUTDIR_UNAME
+#define MODULES_OUTDIR_LIB_MODULES_INPUT MODULES_OUTDIR_ROOTFS "/lib/modules/" MODULES_OUTDIR_UNAME
+static noreturn int depmod_modules_outdir(const struct test *t)
+{
+	const char *progname = ABS_TOP_BUILDDIR "/tools/depmod";
+	const char *const args[] = {
+		progname,
+		"--outdir", MODULES_OUTDIR_ROOTFS "/outdir/",
+		NULL,
+	};
+
+	test_spawn_prog(progname, args);
+	exit(EXIT_FAILURE);
+}
+
+DEFINE_TEST(depmod_modules_outdir,
+#if defined(KMOD_SYSCONFDIR_NOT_ETC)
+        .skip = true,
+#endif
+	.description = "check if depmod honours the outdir option",
+	.config = {
+		[TC_UNAME_R] = MODULES_OUTDIR_UNAME,
+		[TC_ROOTFS] = MODULES_OUTDIR_ROOTFS,
+	},
+	.output = {
+		.files = (const struct keyval[]) {
+			{ MODULES_OUTDIR_LIB_MODULES_OUTPUT "/modules.alias",
+			  MODULES_OUTDIR_LIB_MODULES_INPUT "/modules.alias" },
+			{ }
+		},
+	});
+
 #define SEARCH_ORDER_SIMPLE_ROOTFS TESTSUITE_ROOTFS "test-depmod/search-order-simple"
 static noreturn int depmod_search_order_simple(const struct test *t)
 {
diff --git a/tools/depmod.c b/tools/depmod.c
index 364b7d4..5536597 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -58,11 +58,12 @@  static const char *default_cfg_paths[] = {
 	NULL
 };
 
-static const char cmdopts_s[] = "aAb:C:E:F:euqrvnP:wmVh";
+static const char cmdopts_s[] = "aAb:o:C:E:F:euqrvnP:wmVh";
 static const struct option cmdopts[] = {
 	{ "all", no_argument, 0, 'a' },
 	{ "quick", no_argument, 0, 'A' },
 	{ "basedir", required_argument, 0, 'b' },
+	{ "outdir", required_argument, 0, 'o' },
 	{ "config", required_argument, 0, 'C' },
 	{ "symvers", required_argument, 0, 'E' },
 	{ "filesyms", required_argument, 0, 'F' },
@@ -104,6 +105,7 @@  static void help(void)
 		"\n"
 		"The following options are useful for people managing distributions:\n"
 		"\t-b, --basedir=DIR    Use an image of a module tree.\n"
+		"\t-o, --outdir=DIR     Output directory for generated files.\n"
 		"\t-F, --filesyms=FILE  Use the file instead of the\n"
 		"\t                     current kernel symbols.\n"
 		"\t-E, --symvers=FILE   Use Module.symvers file to check\n"
@@ -467,6 +469,8 @@  struct cfg {
 	const char *kversion;
 	char dirname[PATH_MAX];
 	size_t dirnamelen;
+	char outdirname[PATH_MAX];
+	size_t outdirnamelen;
 	char sym_prefix;
 	uint8_t check_symvers;
 	uint8_t print_unknown;
@@ -2576,7 +2580,7 @@  static int depmod_output(struct depmod *depmod, FILE *out)
 		{ "modules.devname", output_devname },
 		{ }
 	};
-	const char *dname = depmod->cfg->dirname;
+	const char *dname = depmod->cfg->outdirname;
 	int dfd, err = 0;
 	struct timeval tv;
 
@@ -2585,6 +2589,11 @@  static int depmod_output(struct depmod *depmod, FILE *out)
 	if (out != NULL)
 		dfd = -1;
 	else {
+		err = mkdir_p(dname, strlen(dname), 0755);
+		if (err < 0) {
+			CRIT("could not create directory %s: %m\n", dname);
+			return err;
+		}
 		dfd = open(dname, O_RDONLY);
 		if (dfd < 0) {
 			err = -errno;
@@ -2898,6 +2907,7 @@  static int do_depmod(int argc, char *argv[])
 	FILE *out = NULL;
 	int err = 0, all = 0, maybe_all = 0, n_config_paths = 0;
 	_cleanup_free_ char *root = NULL;
+	_cleanup_free_ char *out_root = NULL;
 	_cleanup_free_ const char **config_paths = NULL;
 	const char *system_map = NULL;
 	const char *module_symvers = NULL;
@@ -2927,6 +2937,11 @@  static int do_depmod(int argc, char *argv[])
 				free(root);
 			root = path_make_absolute_cwd(optarg);
 			break;
+		case 'o':
+			if (out_root)
+				free(out_root);
+			out_root = path_make_absolute_cwd(optarg);
+			break;
 		case 'C': {
 			size_t bytes = sizeof(char *) * (n_config_paths + 2);
 			void *tmp = realloc(config_paths, bytes);
@@ -3009,7 +3024,11 @@  static int do_depmod(int argc, char *argv[])
 
 	cfg.dirnamelen = snprintf(cfg.dirname, PATH_MAX,
 				  "%s/lib/modules/%s",
-				  root == NULL ? "" : root, cfg.kversion);
+				  root ?: "", cfg.kversion);
+
+	cfg.outdirnamelen = snprintf(cfg.outdirname, PATH_MAX,
+				     "%s/lib/modules/%s",
+				     out_root ?: (root ?: ""), cfg.kversion);
 
 	if (optind == argc)
 		all = 1;