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"; 46 47my $uname = `uname -r`; 48chomp $uname; 49 50my @searchconfigs = ( 51 { 52 "file" => ".config", 53 "exec" => "cat", 54 }, 55 { 56 "file" => "/proc/config.gz", 57 "exec" => "zcat", 58 }, 59 { 60 "file" => "/boot/config-$uname", 61 "exec" => "cat", 62 }, 63 { 64 "file" => "/boot/vmlinuz-$uname", 65 "exec" => "scripts/extract-ikconfig", 66 "test" => "scripts/extract-ikconfig", 67 }, 68 { 69 "file" => "vmlinux", 70 "exec" => "scripts/extract-ikconfig", 71 "test" => "scripts/extract-ikconfig", 72 }, 73 { 74 "file" => "/lib/modules/$uname/kernel/kernel/configs.ko", 75 "exec" => "scripts/extract-ikconfig", 76 "test" => "scripts/extract-ikconfig", 77 }, 78 { 79 "file" => "kernel/configs.ko", 80 "exec" => "scripts/extract-ikconfig", 81 "test" => "scripts/extract-ikconfig", 82 }, 83 { 84 "file" => "kernel/configs.o", 85 "exec" => "scripts/extract-ikconfig", 86 "test" => "scripts/extract-ikconfig", 87 }, 88); 89 90sub find_config { 91 foreach my $conf (@searchconfigs) { 92 my $file = $conf->{"file"}; 93 94 next if ( ! -f "$file"); 95 96 if (defined($conf->{"test"})) { 97 `$conf->{"test"} $conf->{"file"} 2>/dev/null`; 98 next if ($?); 99 } 100 101 my $exec = $conf->{"exec"}; 102 103 print STDERR "using config: '$file'\n"; 104 105 open(CIN, "$exec $file |") || die "Failed to run $exec $file"; 106 return; 107 } 108 die "No config file found"; 109} 110 111find_config; 112 113# Get the build source and top level Kconfig file (passed in) 114my $ksource = $ARGV[0]; 115my $kconfig = $ARGV[1]; 116my $lsmod_file = $ARGV[2]; 117 118my @makefiles = `find $ksource -name Makefile`; 119my %depends; 120my %selects; 121my %prompts; 122my %objects; 123my $var; 124my $cont = 0; 125my $iflevel = 0; 126my @ifdeps; 127 128# prevent recursion 129my %read_kconfigs; 130 131sub read_kconfig { 132 my ($kconfig) = @_; 133 134 my $state = "NONE"; 135 my $config; 136 my @kconfigs; 137 138 open(KIN, "$ksource/$kconfig") || die "Can't open $kconfig"; 139 while (<KIN>) { 140 chomp; 141 142 # collect any Kconfig sources 143 if (/^source\s*"(.*)"/) { 144 $kconfigs[$#kconfigs+1] = $1; 145 } 146 147 # configs found 148 if (/^\s*config\s+(\S+)\s*$/) { 149 $state = "NEW"; 150 $config = $1; 151 152 for (my $i = 0; $i < $iflevel; $i++) { 153 if ($i) { 154 $depends{$config} .= " " . $ifdeps[$i]; 155 } else { 156 $depends{$config} = $ifdeps[$i]; 157 } 158 $state = "DEP"; 159 } 160 161 # collect the depends for the config 162 } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) { 163 $state = "DEP"; 164 $depends{$config} = $1; 165 } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) { 166 $depends{$config} .= " " . $1; 167 168 # Get the configs that select this config 169 } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) { 170 if (defined($selects{$1})) { 171 $selects{$1} .= " " . $config; 172 } else { 173 $selects{$1} = $config; 174 } 175 176 # configs without prompts must be selected 177 } elsif ($state ne "NONE" && /^\s*tristate\s\S/) { 178 # note if the config has a prompt 179 $prompt{$config} = 1; 180 181 # Check for if statements 182 } elsif (/^if\s+(.*\S)\s*$/) { 183 my $deps = $1; 184 # remove beginning and ending non text 185 $deps =~ s/^[^a-zA-Z0-9_]*//; 186 $deps =~ s/[^a-zA-Z0-9_]*$//; 187 188 my @deps = split /[^a-zA-Z0-9_]+/, $deps; 189 190 $ifdeps[$iflevel++] = join ':', @deps; 191 192 } elsif (/^endif/) { 193 194 $iflevel-- if ($iflevel); 195 196 # stop on "help" 197 } elsif (/^\s*help\s*$/) { 198 $state = "NONE"; 199 } 200 } 201 close(KIN); 202 203 # read in any configs that were found. 204 foreach $kconfig (@kconfigs) { 205 if (!defined($read_kconfigs{$kconfig})) { 206 $read_kconfigs{$kconfig} = 1; 207 read_kconfig($kconfig); 208 } 209 } 210} 211 212if ($kconfig) { 213 read_kconfig($kconfig); 214} 215 216# Read all Makefiles to map the configs to the objects 217foreach my $makefile (@makefiles) { 218 chomp $makefile; 219 220 open(MIN,$makefile) || die "Can't open $makefile"; 221 while (<MIN>) { 222 my $objs; 223 224 # is this a line after a line with a backslash? 225 if ($cont && /(\S.*)$/) { 226 $objs = $1; 227 } 228 $cont = 0; 229 230 # collect objects after obj-$(CONFIG_FOO_BAR) 231 if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) { 232 $var = $1; 233 $objs = $2; 234 } 235 if (defined($objs)) { 236 # test if the line ends with a backslash 237 if ($objs =~ m,(.*)\\$,) { 238 $objs = $1; 239 $cont = 1; 240 } 241 242 foreach my $obj (split /\s+/,$objs) { 243 $obj =~ s/-/_/g; 244 if ($obj =~ /(.*)\.o$/) { 245 # Objects may bes enabled by more than one config. 246 # Store configs in an array. 247 my @arr; 248 249 if (defined($objects{$1})) { 250 @arr = @{$objects{$1}}; 251 } 252 253 $arr[$#arr+1] = $var; 254 255 # The objects have a hash mapping to a reference 256 # of an array of configs. 257 $objects{$1} = \@arr; 258 } 259 } 260 } 261 } 262 close(MIN); 263} 264 265my %modules; 266 267if (defined($lsmod_file)) { 268 if ( ! -f $lsmod_file) { 269 die "$lsmod_file not found"; 270 } 271 if ( -x $lsmod_file) { 272 # the file is executable, run it 273 open(LIN, "$lsmod_file|"); 274 } else { 275 # Just read the contents 276 open(LIN, "$lsmod_file"); 277 } 278} else { 279 280 # see what modules are loaded on this system 281 my $lsmod; 282 283 foreach $dir ( ("/sbin", "/bin", "/usr/sbin", "/usr/bin") ) { 284 if ( -x "$dir/lsmod" ) { 285 $lsmod = "$dir/lsmod"; 286 last; 287 } 288} 289 if (!defined($lsmod)) { 290 # try just the path 291 $lsmod = "lsmod"; 292 } 293 294 open(LIN,"$lsmod|") || die "Can not call lsmod with $lsmod"; 295} 296 297while (<LIN>) { 298 next if (/^Module/); # Skip the first line. 299 if (/^(\S+)/) { 300 $modules{$1} = 1; 301 } 302} 303close (LIN); 304 305# add to the configs hash all configs that are needed to enable 306# a loaded module. 307my %configs; 308foreach my $module (keys(%modules)) { 309 if (defined($objects{$module})) { 310 @arr = @{$objects{$module}}; 311 foreach my $conf (@arr) { 312 $configs{$conf} = $module; 313 } 314 } else { 315 # Most likely, someone has a custom (binary?) module loaded. 316 print STDERR "$module config not found!!\n"; 317 } 318} 319 320my $valid = "A-Za-z_0-9"; 321my $repeat = 1; 322 323# 324# Note, we do not care about operands (like: &&, ||, !) we want to add any 325# config that is in the depend list of another config. This script does 326# not enable configs that are not already enabled. If we come across a 327# config A that depends on !B, we can still add B to the list of depends 328# to keep on. If A was on in the original config, B would not have been 329# and B would not be turned on by this script. 330# 331sub parse_config_dep_select 332{ 333 my ($p) = @_; 334 335 while ($p =~ /[$valid]/) { 336 337 if ($p =~ /^[^$valid]*([$valid]+)/) { 338 my $conf = "CONFIG_" . $1; 339 340 $p =~ s/^[^$valid]*[$valid]+//; 341 342 if (!defined($configs{$conf})) { 343 # We must make sure that this config has its 344 # dependencies met. 345 $repeat = 1; # do again 346 $configs{$conf} = 1; 347 } 348 } else { 349 die "this should never happen"; 350 } 351 } 352} 353 354while ($repeat) { 355 $repeat = 0; 356 357 foreach my $config (keys %configs) { 358 $config =~ s/^CONFIG_//; 359 360 if (defined($depends{$config})) { 361 # This config has dependencies. Make sure they are also included 362 parse_config_dep_select $depends{$config}; 363 } 364 365 if (defined($prompt{$config}) || !defined($selects{$config})) { 366 next; 367 } 368 369 # config has no prompt and must be selected. 370 parse_config_dep_select $selects{$config}; 371 } 372} 373 374my %setconfigs; 375 376# Finally, read the .config file and turn off any module enabled that 377# we could not find a reason to keep enabled. 378while(<CIN>) { 379 380 if (/CONFIG_IKCONFIG/) { 381 if (/# CONFIG_IKCONFIG is not set/) { 382 # enable IKCONFIG at least as a module 383 print "CONFIG_IKCONFIG=m\n"; 384 # don't ask about PROC 385 print "# CONFIG_IKCONFIG_PROC is not set\n"; 386 } else { 387 print; 388 } 389 next; 390 } 391 392 if (/^(CONFIG.*)=(m|y)/) { 393 if (defined($configs{$1})) { 394 $setconfigs{$1} = $2; 395 } elsif ($2 eq "m") { 396 print "# $1 is not set\n"; 397 next; 398 } 399 } 400 print; 401} 402close(CIN); 403 404# Integrity check, make sure all modules that we want enabled do 405# indeed have their configs set. 406loop: 407foreach my $module (keys(%modules)) { 408 if (defined($objects{$module})) { 409 my @arr = @{$objects{$module}}; 410 foreach my $conf (@arr) { 411 if (defined($setconfigs{$conf})) { 412 next loop; 413 } 414 } 415 print STDERR "module $module did not have configs"; 416 foreach my $conf (@arr) { 417 print STDERR " " , $conf; 418 } 419 print STDERR "\n"; 420 } 421} 422