2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "alloc-util.h"
30 #include "capability-util.h"
32 #include "kmod-setup.h"
34 #include "string-util.h"
37 static void systemd_kmod_log(
40 const char *file
, int line
,
45 /* library logging is enabled at debug only */
46 DISABLE_WARNING_FORMAT_NONLITERAL
;
47 log_internalv(LOG_DEBUG
, 0, file
, line
, fn
, format
, args
);
51 static int has_virtio_rng_nftw_cb(
53 const struct stat
*sb
,
57 _cleanup_free_
char *alias
= NULL
;
60 if ((FTW_D
== tflag
) && (ftwbuf
->level
> 2))
61 return FTW_SKIP_SUBTREE
;
66 if (!endswith(fpath
, "/modalias"))
69 r
= read_one_line_file(fpath
, &alias
);
71 return FTW_SKIP_SIBLINGS
;
73 if (startswith(alias
, "pci:v00001AF4d00001005"))
76 if (startswith(alias
, "pci:v00001AF4d00001044"))
79 return FTW_SKIP_SIBLINGS
;
82 static bool has_virtio_rng(void) {
83 return (nftw("/sys/devices/pci0000:00", has_virtio_rng_nftw_cb
, 64, FTW_MOUNT
|FTW_PHYS
|FTW_ACTIONRETVAL
) == FTW_STOP
);
87 int kmod_setup(void) {
93 bool warn_if_unavailable
:1;
94 bool warn_if_module
:1;
95 bool (*condition_fn
)(void);
97 /* auto-loading on use doesn't work before udev is up */
98 { "autofs4", "/sys/class/misc/autofs", true, false, NULL
},
100 /* early configure of ::1 on the loopback device */
101 { "ipv6", "/sys/module/ipv6", false, true, NULL
},
103 /* this should never be a module */
104 { "unix", "/proc/net/unix", true, true, NULL
},
107 /* netfilter is needed by networkd, nspawn among others, and cannot be autoloaded */
108 { "ip_tables", "/proc/net/ip_tables_names", false, false, NULL
},
110 /* virtio_rng would be loaded by udev later, but real entropy might be needed very early */
111 { "virtio_rng", NULL
, false, false, has_virtio_rng
},
113 struct kmod_ctx
*ctx
= NULL
;
117 if (have_effective_cap(CAP_SYS_MODULE
) == 0)
120 for (i
= 0; i
< ELEMENTSOF(kmod_table
); i
++) {
121 struct kmod_module
*mod
;
123 if (kmod_table
[i
].path
&& access(kmod_table
[i
].path
, F_OK
) >= 0)
126 if (kmod_table
[i
].condition_fn
&& !kmod_table
[i
].condition_fn())
129 if (kmod_table
[i
].warn_if_module
)
130 log_debug("Your kernel apparently lacks built-in %s support. Might be "
131 "a good idea to compile it in. We'll now try to work around "
132 "this by loading the module...", kmod_table
[i
].module
);
135 ctx
= kmod_new(NULL
, NULL
);
139 kmod_set_log_fn(ctx
, systemd_kmod_log
, NULL
);
140 kmod_load_resources(ctx
);
143 r
= kmod_module_new_from_name(ctx
, kmod_table
[i
].module
, &mod
);
145 log_error("Failed to lookup module '%s'", kmod_table
[i
].module
);
149 r
= kmod_module_probe_insert_module(mod
, KMOD_PROBE_APPLY_BLACKLIST
, NULL
, NULL
, NULL
, NULL
);
151 log_debug("Inserted module '%s'", kmod_module_get_name(mod
));
152 else if (r
== KMOD_PROBE_APPLY_BLACKLIST
)
153 log_info("Module '%s' is blacklisted", kmod_module_get_name(mod
));
155 bool print_warning
= kmod_table
[i
].warn_if_unavailable
|| (r
< 0 && r
!= -ENOENT
);
157 log_full_errno(print_warning
? LOG_WARNING
: LOG_DEBUG
, r
,
158 "Failed to insert module '%s': %m", kmod_module_get_name(mod
));
161 kmod_module_unref(mod
);