1diff --git a/Makefile.am b/Makefile.am
2index d4eeb7e..5c9f603 100644
3--- a/Makefile.am
4+++ b/Makefile.am
5@@ -19,6 +19,7 @@ AM_CPPFLAGS = \
6 -include $(top_builddir)/config.h \
7 -I$(top_srcdir) \
8 -DSYSCONFDIR=\""$(sysconfdir)"\" \
9+ -DMODULESDIRS=\""$(shell echo $(modulesdirs) | $(SED) 's|:|\\",\\"|g')"\" \
10 ${zlib_CFLAGS}
11
12 AM_CFLAGS = $(OUR_CFLAGS)
13diff --git a/configure.ac b/configure.ac
14index 23510c8..66490cf 100644
15--- a/configure.ac
16+++ b/configure.ac
17@@ -202,6 +202,12 @@ GTK_DOC_CHECK([1.14],[--flavour no-tmpl-flat])
18 ], [
19 AM_CONDITIONAL([ENABLE_GTK_DOC], false)])
20
21+AC_ARG_WITH([modulesdirs],
22+ AS_HELP_STRING([--with-modulesdirs=DIRS], [Kernel modules directories, separated by :]),
23+ [],
24+ [with_modulesdirs=/lib/modules])
25+AC_SUBST([modulesdirs], [$with_modulesdirs])
26+
27
28 #####################################################################
29 # Default CFLAGS and LDFLAGS
30diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c
31index 69fe431..d37da32 100644
32--- a/libkmod/libkmod.c
33+++ b/libkmod/libkmod.c
34@@ -206,12 +206,15 @@ static int log_priority(const char *priority)
35 return 0;
36 }
37
38-static const char *dirname_default_prefix = "/lib/modules";
39+static const char *dirname_default_prefixes[] = {
40+ MODULESDIRS,
41+ NULL
42+};
43
44 static char *get_kernel_release(const char *dirname)
45 {
46 struct utsname u;
47- char *p;
48+ char *p, *dirname_prefix;
49
50 if (dirname != NULL)
51 return path_make_absolute_cwd(dirname);
52@@ -219,8 +222,42 @@ static char *get_kernel_release(const char *dirname)
53 if (uname(&u) < 0)
54 return NULL;
55
56- if (asprintf(&p, "%s/%s", dirname_default_prefix, u.release) < 0)
57- return NULL;
58+ if ((dirname_prefix = getenv("MODULE_DIR")) != NULL) {
59+ if(asprintf(&p, "%s/%s", dirname_prefix, u.release) < 0)
60+ return NULL;
61+ } else {
62+ size_t i;
63+ char buf[PATH_MAX];
64+
65+ for (i = 0; dirname_default_prefixes[i] != NULL; i++) {
66+ int plen;
67+ struct stat dirstat;
68+
69+ plen = snprintf(buf, sizeof(buf), "%s/%s", dirname_default_prefixes[i], u.release);
70+ if (plen < 0)
71+ return NULL;
72+ else if (plen >= PATH_MAX)
73+ continue;
74+
75+ if (dirname_default_prefixes[i + 1] != NULL) {
76+ if (stat(buf, &dirstat) < 0) {
77+ if (errno == ENOENT)
78+ continue;
79+ else
80+ return NULL;
81+ }
82+
83+ if (!S_ISDIR(dirstat.st_mode))
84+ continue;
85+ }
86+
87+ p = malloc(plen + 1);
88+ if (p == NULL)
89+ return NULL;
90+ memcpy(p, buf, plen + 1);
91+ break;
92+ }
93+ }
94
95 return p;
96 }
97diff --git a/tools/static-nodes.c b/tools/static-nodes.c
98index 8d2356d..2ed306d 100644
99--- a/tools/static-nodes.c
100+++ b/tools/static-nodes.c
101@@ -29,10 +29,11 @@
102 #include <unistd.h>
103 #include <sys/stat.h>
104 #include <sys/types.h>
105-#include <sys/utsname.h>
106
107 #include <shared/util.h>
108
109+#include <libkmod/libkmod.h>
110+
111 #include "kmod.h"
112
113 struct static_nodes_format {
114@@ -154,8 +155,8 @@ static void help(void)
115
116 static int do_static_nodes(int argc, char *argv[])
117 {
118- struct utsname kernel;
119 char modules[PATH_MAX], buf[4096];
120+ struct kmod_ctx *ctx;
121 const char *output = "/dev/stdout";
122 FILE *in = NULL, *out = NULL;
123 const struct static_nodes_format *format = &static_nodes_format_human;
124@@ -206,22 +207,25 @@ static int do_static_nodes(int argc, char *argv[])
125 }
126 }
127
128- if (uname(&kernel) < 0) {
129- fputs("Error: uname failed!\n", stderr);
130+ ctx = kmod_new(NULL, NULL);
131+ if (ctx == NULL) {
132+ fprintf(stderr, "Error: failed to create kmod context\n");
133 ret = EXIT_FAILURE;
134 goto finish;
135 }
136-
137- snprintf(modules, sizeof(modules), "/lib/modules/%s/modules.devname", kernel.release);
138+ if (snprintf(modules, sizeof(modules), "%s/modules.devname", kmod_get_dirname(ctx)) < 0) {
139+ fprintf(stderr, "Error: path to modules.devname is too long\n");
140+ ret = EXIT_FAILURE;
141+ goto finish;
142+ }
143+ kmod_unref(ctx);
144 in = fopen(modules, "re");
145 if (in == NULL) {
146 if (errno == ENOENT) {
147- fprintf(stderr, "Warning: /lib/modules/%s/modules.devname not found - ignoring\n",
148- kernel.release);
149+ fprintf(stderr, "Warning: %s not found - ignoring\n", modules);
150 ret = EXIT_SUCCESS;
151 } else {
152- fprintf(stderr, "Error: could not open /lib/modules/%s/modules.devname - %m\n",
153- kernel.release);
154+ fprintf(stderr, "Error: could not open %s - %m\n", modules);
155 ret = EXIT_FAILURE;
156 }
157 goto finish;