3 # Copywrite 2005-2009 - Steven Rostedt
4 # Licensed under the terms of the GNU GPL License version 2
6 # It's simple enough to figure out how this works.
7 # If not, then you can ask me at stripconfig@goodmis.org
11 # If you have installed a Linux kernel from a distribution
12 # that turns on way too many modules than you need, and
13 # you only want the modules you use, then this program
16 # It gives you the ability to turn off all the modules that are
17 # not loaded on your system.
21 # 1. Boot up the kernel that you want to stream line the config on.
22 # 2. Change directory to the directory holding the source of the
23 # kernel that you just booted.
24 # 3. Copy the configuraton file to this directory as .config
25 # 4. Have all your devices that you need modules for connected and
26 # operational (make sure that their corresponding modules are loaded)
27 # 5. Run this script redirecting the output to some other file
29 # 6. Back up your old config (if you want too).
30 # 7. copy the config_strip file to .config
31 # 8. Run "make oldconfig"
33 # Now your kernel is ready to be built with only the modules that
36 # Here's what I did with my Debian distribution.
38 # cd /usr/src/linux-2.6.10
39 # cp /boot/config-2.6.10-1-686-smp .config
40 # ~/bin/streamline_config > config_strip
41 # mv .config config_sav
42 # mv config_strip .config
45 my $config = ".config";
48 my $uname = `uname -r`;
53 "file" => "/proc/config.gz",
57 "file" => "/boot/vmlinuz-$uname",
58 "exec" => "scripts/extract-ikconfig",
59 "test" => "scripts/extract-ikconfig",
63 "exec" => "scripts/extract-ikconfig",
64 "test" => "scripts/extract-ikconfig",
67 "file" => "/lib/modules/$uname/kernel/kernel/configs.ko",
68 "exec" => "scripts/extract-ikconfig",
69 "test" => "scripts/extract-ikconfig",
72 "file" => "kernel/configs.ko",
73 "exec" => "scripts/extract-ikconfig",
74 "test" => "scripts/extract-ikconfig",
77 "file" => "kernel/configs.o",
78 "exec" => "scripts/extract-ikconfig",
79 "test" => "scripts/extract-ikconfig",
88 foreach my $conf (@searchconfigs) {
89 my $file = $conf->{"file"};
91 next if ( ! -f "$file");
93 if (defined($conf->{"test"})) {
94 `$conf->{"test"} $conf->{"file"} 2>/dev/null`;
98 my $exec = $conf->{"exec"};
100 print STDERR "using config: '$file'\n";
102 open(CIN, "$exec $file |") || die "Failed to run $exec $file";
105 die "No config file found";
110 my @makefiles = `find $linuxpath -name Makefile`;
118 # Get the top level Kconfig file (passed in)
119 my $kconfig = $ARGV[0];
131 open(KIN, $kconfig) || die "Can't open $kconfig";
135 # collect any Kconfig sources
136 if (/^source\s*"(.*)"/) {
137 $kconfigs[$#kconfigs+1] = $1;
141 if (/^\s*config\s+(\S+)\s*$/) {
145 # collect the depends for the config
146 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
148 $depends{$config} = $1;
149 } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
150 $depends{$config} .= " " . $1;
152 # Get the configs that select this config
153 } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
154 if (defined($selects{$1})) {
155 $selects{$1} .= " " . $config;
157 $selects{$1} = $config;
160 # configs without prompts must be selected
161 } elsif ($state ne "NONE" && /^\s*tristate\s\S/) {
162 # note if the config has a prompt
163 $prompt{$config} = 1;
166 } elsif (/^\s*help\s*$/) {
172 # read in any configs that were found.
173 foreach $kconfig (@kconfigs) {
174 if (!defined($read_kconfigs{$kconfig})) {
175 $read_kconfigs{$kconfig} = 1;
176 read_kconfig($kconfig);
182 read_kconfig($kconfig);
185 # Read all Makefiles to map the configs to the objects
186 foreach my $makefile (@makefiles) {
189 open(MIN,$makefile) || die "Can't open $makefile";
193 # is this a line after a line with a backslash?
194 if ($cont && /(\S.*)$/) {
199 # collect objects after obj-$(CONFIG_FOO_BAR)
200 if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
204 if (defined($objs)) {
205 # test if the line ends with a backslash
206 if ($objs =~ m,(.*)\\$,) {
211 foreach my $obj (split /\s+/,$objs) {
213 if ($obj =~ /(.*)\.o$/) {
214 # Objects may bes enabled by more than one config.
215 # Store configs in an array.
218 if (defined($objects{$1})) {
219 @arr = @{$objects{$1}};
222 $arr[$#arr+1] = $var;
224 # The objects have a hash mapping to a reference
225 # of an array of configs.
226 $objects{$1} = \@arr;
236 # see what modules are loaded on this system
237 open(LIN,"/sbin/lsmod|") || die "Cant lsmod";
239 next if (/^Module/); # Skip the first line.
246 # add to the configs hash all configs that are needed to enable
249 foreach my $module (keys(%modules)) {
250 if (defined($objects{$module})) {
251 @arr = @{$objects{$module}};
252 foreach my $conf (@arr) {
253 $configs{$conf} = $module;
256 # Most likely, someone has a custom (binary?) module loaded.
257 print STDERR "$module config not found!!\n";
261 my $valid = "A-Za-z_0-9";
265 # Note, we do not care about operands (like: &&, ||, !) we want to add any
266 # config that is in the depend list of another config. This script does
267 # not enable configs that are not already enabled. If we come across a
268 # config A that depends on !B, we can still add B to the list of depends
269 # to keep on. If A was on in the original config, B would not have been
270 # and B would not be turned on by this script.
272 sub parse_config_dep_select
276 while ($p =~ /[$valid]/) {
278 if ($p =~ /^[^$valid]*([$valid]+)/) {
279 my $conf = "CONFIG_" . $1;
281 $p =~ s/^[^$valid]*[$valid]+//;
283 if (!defined($configs{$conf})) {
284 # We must make sure that this config has its
286 $repeat = 1; # do again
290 die "this should never happen";
298 foreach my $config (keys %configs) {
299 $config =~ s/^CONFIG_//;
301 if (defined($depends{$config})) {
302 # This config has dependencies. Make sure they are also included
303 parse_config_dep_select $depends{$config};
306 if (defined($prompt{$config}) || !defined($selects{$config})) {
310 # config has no prompt and must be selected.
311 parse_config_dep_select $selects{$config};
317 # Finally, read the .config file and turn off any module enabled that
318 # we could not find a reason to keep enabled.
321 if (/CONFIG_IKCONFIG/) {
322 if (/# CONFIG_IKCONFIG is not set/) {
323 # enable IKCONFIG at least as a module
324 print "CONFIG_IKCONFIG=m\n";
325 # don't ask about PROC
326 print "# CONFIG_IKCONFIG_PROC is not set\n";
333 if (/^(CONFIG.*)=(m|y)/) {
334 if (defined($configs{$1})) {
335 $setconfigs{$1} = $2;
336 } elsif ($2 eq "m") {
337 print "# $1 is not set\n";
345 # Integrity check, make sure all modules that we want enabled do
346 # indeed have their configs set.
348 foreach my $module (keys(%modules)) {
349 if (defined($objects{$module})) {
350 my @arr = @{$objects{$module}};
351 foreach my $conf (@arr) {
352 if (defined($setconfigs{$conf})) {
356 print STDERR "module $module did not have configs";
357 foreach my $conf (@arr) {
358 print STDERR " " , $conf;