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