aboutsummaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorSteven Rostedt <srostedt@redhat.com>2009-04-29 22:52:21 -0400
committerSteven Rostedt <rostedt@goodmis.org>2009-08-18 21:49:43 -0400
commitdcc60243e726978576cb02b74c84440629c69c87 (patch)
treef1a4cd4a41c942118a21c3b1cb0b4857fe2c63e1 /scripts
parentdc8ed71eeb8adce08d3070f4130e12ee540baa59 (diff)
kconfig: add streamline_config.pl to scripts
streamline_config.pl is a very powerful tool. For those that install a kernel to a new box using the config file from the distribution know that it can take forever to compile the kernel. Making a custom config file that will still boot your box, but bring down the compile time of the kernel can be quit painful, and to ask someone that reported a bug to do this can be a large burdon since that person may not even know how to build a kernel. This script will perform "lsmod" to find all the modules loaded on the current running system. It will read all the Makefiles to map which CONFIG enables a module. It will read the Kconfig files to find the dependencies and selects that may be needed to support a CONFIG. Finally, it reads the .config file and removes any module "=m" that is not needed to enable the currently loaded modules. The output goes to standard out. Here's a way to run the script. From the Linux directory that holds a distribution .config. $ scripts/kconfig/streamline_config.pl arch/x86/Kconfig > config-sl $ mv .config config-save $ mv config-sl .config $ make oldconfig Now you have a .config that will still build all your modules, but also take much less time to build the kernel. Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
Diffstat (limited to 'scripts')
-rw-r--r--scripts/kconfig/streamline_config.pl291
1 files changed, 291 insertions, 0 deletions
diff --git a/scripts/kconfig/streamline_config.pl b/scripts/kconfig/streamline_config.pl
new file mode 100644
index 000000000000..79d85573ee0f
--- /dev/null
+++ b/scripts/kconfig/streamline_config.pl
@@ -0,0 +1,291 @@
1#!/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
48open(CIN,$config) || die "Can't open current config file: $config";
49my @makefiles = `find $linuxpath -name Makefile`;
50my %depends;
51my %selects;
52my %prompts;
53my %objects;
54my $var;
55my $cont = 0;
56
57# Get the top level Kconfig file (passed in)
58my $kconfig = $ARGV[0];
59
60# prevent recursion
61my %read_kconfigs;
62
63sub read_kconfig {
64 my ($kconfig) = @_;
65
66 my $state = "NONE";
67 my $config;
68 my @kconfigs;
69
70 open(KIN, $kconfig) || die "Can't open $kconfig";
71 while (<KIN>) {
72 chomp;
73
74 # collect any Kconfig sources
75 if (/^source\s*"(.*)"/) {
76 $kconfigs[$#kconfigs+1] = $1;
77 }
78
79 # configs found
80 if (/^\s*config\s+(\S+)\s*$/) {
81 $state = "NEW";
82 $config = $1;
83
84 # collect the depends for the config
85 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
86 $state = "DEP";
87 $depends{$config} = $1;
88 } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
89 $depends{$config} .= " " . $1;
90
91 # Get the configs that select this config
92 } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
93 if (defined($selects{$1})) {
94 $selects{$1} .= " " . $config;
95 } else {
96 $selects{$1} = $config;
97 }
98
99 # configs without prompts must be selected
100 } elsif ($state ne "NONE" && /^\s*tristate\s\S/) {
101 # note if the config has a prompt
102 $prompt{$config} = 1;
103
104 # stop on "help"
105 } elsif (/^\s*help\s*$/) {
106 $state = "NONE";
107 }
108 }
109 close(KIN);
110
111 # read in any configs that were found.
112 foreach $kconfig (@kconfigs) {
113 if (!defined($read_kconfigs{$kconfig})) {
114 $read_kconfigs{$kconfig} = 1;
115 read_kconfig($kconfig);
116 }
117 }
118}
119
120if ($kconfig) {
121 read_kconfig($kconfig);
122}
123
124# Read all Makefiles to map the configs to the objects
125foreach my $makefile (@makefiles) {
126 chomp $makefile;
127
128 open(MIN,$makefile) || die "Can't open $makefile";
129 while (<MIN>) {
130 my $objs;
131
132 # is this a line after a line with a backslash?
133 if ($cont && /(\S.*)$/) {
134 $objs = $1;
135 }
136 $cont = 0;
137
138 # collect objects after obj-$(CONFIG_FOO_BAR)
139 if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
140 $var = $1;
141 $objs = $2;
142 }
143 if (defined($objs)) {
144 # test if the line ends with a backslash
145 if ($objs =~ m,(.*)\\$,) {
146 $objs = $1;
147 $cont = 1;
148 }
149
150 foreach my $obj (split /\s+/,$objs) {
151 $obj =~ s/-/_/g;
152 if ($obj =~ /(.*)\.o$/) {
153 # Objects may bes enabled by more than one config.
154 # Store configs in an array.
155 my @arr;
156
157 if (defined($objects{$1})) {
158 @arr = @{$objects{$1}};
159 }
160
161 $arr[$#arr+1] = $var;
162
163 # The objects have a hash mapping to a reference
164 # of an array of configs.
165 $objects{$1} = \@arr;
166 }
167 }
168 }
169 }
170 close(MIN);
171}
172
173my %modules;
174
175# see what modules are loaded on this system
176open(LIN,"/sbin/lsmod|") || die "Cant lsmod";
177while (<LIN>) {
178 next if (/^Module/); # Skip the first line.
179 if (/^(\S+)/) {
180 $modules{$1} = 1;
181 }
182}
183close (LIN);
184
185# add to the configs hash all configs that are needed to enable
186# a loaded module.
187my %configs;
188foreach my $module (keys(%modules)) {
189 if (defined($objects{$module})) {
190 @arr = @{$objects{$module}};
191 foreach my $conf (@arr) {
192 $configs{$conf} = $module;
193 }
194 } else {
195 # Most likely, someone has a custom (binary?) module loaded.
196 print STDERR "$module config not found!!\n";
197 }
198}
199
200my $valid = "A-Za-z_0-9";
201my $repeat = 1;
202
203#
204# Note, we do not care about operands (like: &&, ||, !) we want to add any
205# config that is in the depend list of another config. This script does
206# not enable configs that are not already enabled. If we come across a
207# config A that depends on !B, we can still add B to the list of depends
208# to keep on. If A was on in the original config, B would not have been
209# and B would not be turned on by this script.
210#
211sub parse_config_dep_select
212{
213 my ($p) = @_;
214
215 while ($p =~ /[$valid]/) {
216
217 if ($p =~ /^[^$valid]*([$valid]+)/) {
218 my $conf = "CONFIG_" . $1;
219
220 $p =~ s/^[^$valid]*[$valid]+//;
221
222 if (!defined($configs{$conf})) {
223 # We must make sure that this config has its
224 # dependencies met.
225 $repeat = 1; # do again
226 $configs{$conf} = 1;
227 }
228 } else {
229 die "this should never happen";
230 }
231 }
232}
233
234while ($repeat) {
235 $repeat = 0;
236
237 foreach my $config (keys %configs) {
238 $config =~ s/^CONFIG_//;
239
240 if (!defined($depends{$config})) {
241 next;
242 }
243
244 # This config has dependencies. Make sure they are also included
245 parse_config_dep_select $depends{$config};
246
247 if (defined($prompt{$config}) || !defined($selects{$config})) {
248 next;
249 }
250
251 # config has no prompt and must be selected.
252 parse_config_dep_select $selects{$config};
253 }
254}
255
256my %setconfigs;
257
258# Finally, read the .config file and turn off any module enabled that
259# we could not find a reason to keep enabled.
260while(<CIN>) {
261 if (/^(CONFIG.*)=m/) {
262 if (defined($configs{$1})) {
263 $setconfigs{$1} = 1;
264 print;
265 } else {
266 print "# $1 is not set\n";
267 }
268 } else {
269 print;
270 }
271}
272close(CIN);
273
274# Integrity check, make sure all modules that we want enabled do
275# indeed have their configs set.
276loop:
277foreach my $module (keys(%modules)) {
278 if (defined($objects{$module})) {
279 my @arr = @{$objects{$module}};
280 foreach my $conf (@arr) {
281 if (defined($setconfigs{$conf})) {
282 next loop;
283 }
284 }
285 print STDERR "module $module did not have configs";
286 foreach my $conf (@arr) {
287 print STDERR " " , $conf;
288 }
289 print STDERR "\n";
290 }
291}