blob: 9fa3f81b1ed9267714929f94b5fb8135f7552897 [file] [log] [blame]
Steven Rostedtdcc60242009-04-29 22:52:21 -04001#!/usr/bin/perl -w
2#
3# Copywrite 2005-2009 - Steven Rostedt
4# Licensed under the terms of the GNU GPL License version 2
5#
6# It's simple enough to figure out how this works.
7# If not, then you can ask me at stripconfig@goodmis.org
8#
9# What it does?
10#
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
14# is perfect for you.
15#
16# It gives you the ability to turn off all the modules that are
17# not loaded on your system.
18#
19# Howto:
20#
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
28# like config_strip.
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"
32#
33# Now your kernel is ready to be built with only the modules that
34# are loaded.
35#
36# Here's what I did with my Debian distribution.
37#
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
43# make oldconfig
44#
45my $config = ".config";
46my $linuxpath = ".";
47
Steven Rostedtcdfc4792009-04-30 14:39:48 -040048my $uname = `uname -r`;
49chomp $uname;
50
51my @searchconfigs = (
52 {
53 "file" => "/proc/config.gz",
54 "exec" => "zcat",
55 },
56 {
57 "file" => "/boot/vmlinuz-$uname",
58 "exec" => "scripts/extract-ikconfig",
59 "test" => "scripts/extract-ikconfig",
60 },
61 {
62 "file" => "vmlinux",
63 "exec" => "scripts/extract-ikconfig",
64 "test" => "scripts/extract-ikconfig",
65 },
66 {
67 "file" => "/lib/modules/$uname/kernel/kernel/configs.ko",
68 "exec" => "scripts/extract-ikconfig",
69 "test" => "scripts/extract-ikconfig",
70 },
71 {
72 "file" => "kernel/configs.ko",
73 "exec" => "scripts/extract-ikconfig",
74 "test" => "scripts/extract-ikconfig",
75 },
76 {
77 "file" => "kernel/configs.o",
78 "exec" => "scripts/extract-ikconfig",
79 "test" => "scripts/extract-ikconfig",
80 },
81 {
82 "file" => ".config",
83 "exec" => "cat",
84 },
85);
86
87sub find_config {
88 foreach my $conf (@searchconfigs) {
89 my $file = $conf->{"file"};
90
91 next if ( ! -f "$file");
92
93 if (defined($conf->{"test"})) {
94 `$conf->{"test"} $conf->{"file"} 2>/dev/null`;
95 next if ($?);
96 }
97
98 my $exec = $conf->{"exec"};
99
100 print STDERR "using config: '$file'\n";
101
102 open(CIN, "$exec $file |") || die "Failed to run $exec $file";
103 return;
104 }
105 die "No config file found";
106}
107
108find_config;
109
Steven Rostedtdcc60242009-04-29 22:52:21 -0400110my @makefiles = `find $linuxpath -name Makefile`;
111my %depends;
112my %selects;
113my %prompts;
114my %objects;
115my $var;
116my $cont = 0;
117
118# Get the top level Kconfig file (passed in)
119my $kconfig = $ARGV[0];
120
121# prevent recursion
122my %read_kconfigs;
123
124sub read_kconfig {
125 my ($kconfig) = @_;
126
127 my $state = "NONE";
128 my $config;
129 my @kconfigs;
130
131 open(KIN, $kconfig) || die "Can't open $kconfig";
132 while (<KIN>) {
133 chomp;
134
135 # collect any Kconfig sources
136 if (/^source\s*"(.*)"/) {
137 $kconfigs[$#kconfigs+1] = $1;
138 }
139
140 # configs found
141 if (/^\s*config\s+(\S+)\s*$/) {
142 $state = "NEW";
143 $config = $1;
144
145 # collect the depends for the config
146 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
147 $state = "DEP";
148 $depends{$config} = $1;
149 } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
150 $depends{$config} .= " " . $1;
151
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;
156 } else {
157 $selects{$1} = $config;
158 }
159
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;
164
165 # stop on "help"
166 } elsif (/^\s*help\s*$/) {
167 $state = "NONE";
168 }
169 }
170 close(KIN);
171
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);
177 }
178 }
179}
180
181if ($kconfig) {
182 read_kconfig($kconfig);
183}
184
185# Read all Makefiles to map the configs to the objects
186foreach my $makefile (@makefiles) {
187 chomp $makefile;
188
189 open(MIN,$makefile) || die "Can't open $makefile";
190 while (<MIN>) {
191 my $objs;
192
193 # is this a line after a line with a backslash?
194 if ($cont && /(\S.*)$/) {
195 $objs = $1;
196 }
197 $cont = 0;
198
199 # collect objects after obj-$(CONFIG_FOO_BAR)
200 if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
201 $var = $1;
202 $objs = $2;
203 }
204 if (defined($objs)) {
205 # test if the line ends with a backslash
206 if ($objs =~ m,(.*)\\$,) {
207 $objs = $1;
208 $cont = 1;
209 }
210
211 foreach my $obj (split /\s+/,$objs) {
212 $obj =~ s/-/_/g;
213 if ($obj =~ /(.*)\.o$/) {
214 # Objects may bes enabled by more than one config.
215 # Store configs in an array.
216 my @arr;
217
218 if (defined($objects{$1})) {
219 @arr = @{$objects{$1}};
220 }
221
222 $arr[$#arr+1] = $var;
223
224 # The objects have a hash mapping to a reference
225 # of an array of configs.
226 $objects{$1} = \@arr;
227 }
228 }
229 }
230 }
231 close(MIN);
232}
233
234my %modules;
235
236# see what modules are loaded on this system
237open(LIN,"/sbin/lsmod|") || die "Cant lsmod";
238while (<LIN>) {
239 next if (/^Module/); # Skip the first line.
240 if (/^(\S+)/) {
241 $modules{$1} = 1;
242 }
243}
244close (LIN);
245
246# add to the configs hash all configs that are needed to enable
247# a loaded module.
248my %configs;
249foreach my $module (keys(%modules)) {
250 if (defined($objects{$module})) {
251 @arr = @{$objects{$module}};
252 foreach my $conf (@arr) {
253 $configs{$conf} = $module;
254 }
255 } else {
256 # Most likely, someone has a custom (binary?) module loaded.
257 print STDERR "$module config not found!!\n";
258 }
259}
260
261my $valid = "A-Za-z_0-9";
262my $repeat = 1;
263
264#
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.
271#
272sub parse_config_dep_select
273{
274 my ($p) = @_;
275
276 while ($p =~ /[$valid]/) {
277
278 if ($p =~ /^[^$valid]*([$valid]+)/) {
279 my $conf = "CONFIG_" . $1;
280
281 $p =~ s/^[^$valid]*[$valid]+//;
282
283 if (!defined($configs{$conf})) {
284 # We must make sure that this config has its
285 # dependencies met.
286 $repeat = 1; # do again
287 $configs{$conf} = 1;
288 }
289 } else {
290 die "this should never happen";
291 }
292 }
293}
294
295while ($repeat) {
296 $repeat = 0;
297
298 foreach my $config (keys %configs) {
299 $config =~ s/^CONFIG_//;
300
Steven Rostedt74398d32009-04-30 10:17:51 -0400301 if (defined($depends{$config})) {
302 # This config has dependencies. Make sure they are also included
303 parse_config_dep_select $depends{$config};
Steven Rostedtdcc60242009-04-29 22:52:21 -0400304 }
305
Steven Rostedtdcc60242009-04-29 22:52:21 -0400306 if (defined($prompt{$config}) || !defined($selects{$config})) {
307 next;
308 }
309
310 # config has no prompt and must be selected.
311 parse_config_dep_select $selects{$config};
312 }
313}
314
315my %setconfigs;
316
317# Finally, read the .config file and turn off any module enabled that
318# we could not find a reason to keep enabled.
319while(<CIN>) {
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400320
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 is not set\n";
Steven Rostedtdcc60242009-04-29 22:52:21 -0400327 } else {
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400328 print;
Steven Rostedtdcc60242009-04-29 22:52:21 -0400329 }
Steven Rostedt744ffcb2009-04-30 12:15:10 -0400330 next;
331 }
332
333 if (/^(CONFIG.*)=(m|y)/) {
334 if (defined($configs{$1})) {
335 $setconfigs{$1} = $2;
336 print;
337 } elsif ($2 eq "m") {
338 print "# $1 is not set\n";
339 } else {
340 print;
341 }
342 } else {
343 print;
344 }
Steven Rostedtdcc60242009-04-29 22:52:21 -0400345}
346close(CIN);
347
348# Integrity check, make sure all modules that we want enabled do
349# indeed have their configs set.
350loop:
351foreach my $module (keys(%modules)) {
352 if (defined($objects{$module})) {
353 my @arr = @{$objects{$module}};
354 foreach my $conf (@arr) {
355 if (defined($setconfigs{$conf})) {
356 next loop;
357 }
358 }
359 print STDERR "module $module did not have configs";
360 foreach my $conf (@arr) {
361 print STDERR " " , $conf;
362 }
363 print STDERR "\n";
364 }
365}