streamline_config.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright 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. use strict;
  46. use Getopt::Long;
  47. # set the environment variable LOCALMODCONFIG_DEBUG to get
  48. # debug output.
  49. my $debugprint = 0;
  50. $debugprint = 1 if (defined($ENV{LOCALMODCONFIG_DEBUG}));
  51. sub dprint {
  52. return if (!$debugprint);
  53. print STDERR @_;
  54. }
  55. my $config = ".config";
  56. my $uname = `uname -r`;
  57. chomp $uname;
  58. my @searchconfigs = (
  59. {
  60. "file" => ".config",
  61. "exec" => "cat",
  62. },
  63. {
  64. "file" => "/proc/config.gz",
  65. "exec" => "zcat",
  66. },
  67. {
  68. "file" => "/boot/config-$uname",
  69. "exec" => "cat",
  70. },
  71. {
  72. "file" => "/boot/vmlinuz-$uname",
  73. "exec" => "scripts/extract-ikconfig",
  74. "test" => "scripts/extract-ikconfig",
  75. },
  76. {
  77. "file" => "vmlinux",
  78. "exec" => "scripts/extract-ikconfig",
  79. "test" => "scripts/extract-ikconfig",
  80. },
  81. {
  82. "file" => "/lib/modules/$uname/kernel/kernel/configs.ko",
  83. "exec" => "scripts/extract-ikconfig",
  84. "test" => "scripts/extract-ikconfig",
  85. },
  86. {
  87. "file" => "kernel/configs.ko",
  88. "exec" => "scripts/extract-ikconfig",
  89. "test" => "scripts/extract-ikconfig",
  90. },
  91. {
  92. "file" => "kernel/configs.o",
  93. "exec" => "scripts/extract-ikconfig",
  94. "test" => "scripts/extract-ikconfig",
  95. },
  96. );
  97. sub find_config {
  98. foreach my $conf (@searchconfigs) {
  99. my $file = $conf->{"file"};
  100. next if ( ! -f "$file");
  101. if (defined($conf->{"test"})) {
  102. `$conf->{"test"} $conf->{"file"} 2>/dev/null`;
  103. next if ($?);
  104. }
  105. my $exec = $conf->{"exec"};
  106. print STDERR "using config: '$file'\n";
  107. open(CIN, "$exec $file |") || die "Failed to run $exec $file";
  108. return;
  109. }
  110. die "No config file found";
  111. }
  112. find_config;
  113. # Read in the entire config file into config_file
  114. my @config_file = <CIN>;
  115. close CIN;
  116. # Parse options
  117. my $localmodconfig = 0;
  118. my $localyesconfig = 0;
  119. GetOptions("localmodconfig" => \$localmodconfig,
  120. "localyesconfig" => \$localyesconfig);
  121. # Get the build source and top level Kconfig file (passed in)
  122. my $ksource = $ARGV[0];
  123. my $kconfig = $ARGV[1];
  124. my $lsmod_file = $ENV{'LSMOD'};
  125. my @makefiles = `find $ksource -name Makefile 2>/dev/null`;
  126. chomp @makefiles;
  127. my %depends;
  128. my %selects;
  129. my %prompts;
  130. my %objects;
  131. my $var;
  132. my $iflevel = 0;
  133. my @ifdeps;
  134. # prevent recursion
  135. my %read_kconfigs;
  136. sub read_kconfig {
  137. my ($kconfig) = @_;
  138. my $state = "NONE";
  139. my $config;
  140. my @kconfigs;
  141. my $cont = 0;
  142. my $line;
  143. my $source = "$ksource/$kconfig";
  144. my $last_source = "";
  145. # Check for any environment variables used
  146. while ($source =~ /\$(\w+)/ && $last_source ne $source) {
  147. my $env = $1;
  148. $last_source = $source;
  149. $source =~ s/\$$env/$ENV{$env}/;
  150. }
  151. open(KIN, "$source") || die "Can't open $kconfig";
  152. while (<KIN>) {
  153. chomp;
  154. # Make sure that lines ending with \ continue
  155. if ($cont) {
  156. $_ = $line . " " . $_;
  157. }
  158. if (s/\\$//) {
  159. $cont = 1;
  160. $line = $_;
  161. next;
  162. }
  163. $cont = 0;
  164. # collect any Kconfig sources
  165. if (/^source\s*"(.*)"/) {
  166. $kconfigs[$#kconfigs+1] = $1;
  167. }
  168. # configs found
  169. if (/^\s*(menu)?config\s+(\S+)\s*$/) {
  170. $state = "NEW";
  171. $config = $2;
  172. # Add depends for 'if' nesting
  173. for (my $i = 0; $i < $iflevel; $i++) {
  174. if ($i) {
  175. $depends{$config} .= " " . $ifdeps[$i];
  176. } else {
  177. $depends{$config} = $ifdeps[$i];
  178. }
  179. $state = "DEP";
  180. }
  181. # collect the depends for the config
  182. } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
  183. $state = "DEP";
  184. $depends{$config} = $1;
  185. } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
  186. $depends{$config} .= " " . $1;
  187. # Get the configs that select this config
  188. } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
  189. my $conf = $1;
  190. if (defined($selects{$conf})) {
  191. $selects{$conf} .= " " . $config;
  192. } else {
  193. $selects{$conf} = $config;
  194. }
  195. # configs without prompts must be selected
  196. } elsif ($state ne "NONE" && /^\s*tristate\s\S/) {
  197. # note if the config has a prompt
  198. $prompts{$config} = 1;
  199. # Check for if statements
  200. } elsif (/^if\s+(.*\S)\s*$/) {
  201. my $deps = $1;
  202. # remove beginning and ending non text
  203. $deps =~ s/^[^a-zA-Z0-9_]*//;
  204. $deps =~ s/[^a-zA-Z0-9_]*$//;
  205. my @deps = split /[^a-zA-Z0-9_]+/, $deps;
  206. $ifdeps[$iflevel++] = join ':', @deps;
  207. } elsif (/^endif/) {
  208. $iflevel-- if ($iflevel);
  209. # stop on "help"
  210. } elsif (/^\s*help\s*$/) {
  211. $state = "NONE";
  212. }
  213. }
  214. close(KIN);
  215. # read in any configs that were found.
  216. foreach $kconfig (@kconfigs) {
  217. if (!defined($read_kconfigs{$kconfig})) {
  218. $read_kconfigs{$kconfig} = 1;
  219. read_kconfig($kconfig);
  220. }
  221. }
  222. }
  223. if ($kconfig) {
  224. read_kconfig($kconfig);
  225. }
  226. # Makefiles can use variables to define their dependencies
  227. sub convert_vars {
  228. my ($line, %vars) = @_;
  229. my $process = "";
  230. while ($line =~ s/^(.*?)(\$\((.*?)\))//) {
  231. my $start = $1;
  232. my $variable = $2;
  233. my $var = $3;
  234. if (defined($vars{$var})) {
  235. $process .= $start . $vars{$var};
  236. } else {
  237. $process .= $start . $variable;
  238. }
  239. }
  240. $process .= $line;
  241. return $process;
  242. }
  243. # Read all Makefiles to map the configs to the objects
  244. foreach my $makefile (@makefiles) {
  245. my $line = "";
  246. my %make_vars;
  247. open(MIN,$makefile) || die "Can't open $makefile";
  248. while (<MIN>) {
  249. # if this line ends with a backslash, continue
  250. chomp;
  251. if (/^(.*)\\$/) {
  252. $line .= $1;
  253. next;
  254. }
  255. $line .= $_;
  256. $_ = $line;
  257. $line = "";
  258. my $objs;
  259. # Convert variables in a line (could define configs)
  260. $_ = convert_vars($_, %make_vars);
  261. # collect objects after obj-$(CONFIG_FOO_BAR)
  262. if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
  263. $var = $1;
  264. $objs = $2;
  265. # check if variables are set
  266. } elsif (/^\s*(\S+)\s*[:]?=\s*(.*\S)/) {
  267. $make_vars{$1} = $2;
  268. }
  269. if (defined($objs)) {
  270. foreach my $obj (split /\s+/,$objs) {
  271. $obj =~ s/-/_/g;
  272. if ($obj =~ /(.*)\.o$/) {
  273. # Objects may be enabled by more than one config.
  274. # Store configs in an array.
  275. my @arr;
  276. if (defined($objects{$1})) {
  277. @arr = @{$objects{$1}};
  278. }
  279. $arr[$#arr+1] = $var;
  280. # The objects have a hash mapping to a reference
  281. # of an array of configs.
  282. $objects{$1} = \@arr;
  283. }
  284. }
  285. }
  286. }
  287. close(MIN);
  288. }
  289. my %modules;
  290. if (defined($lsmod_file)) {
  291. if ( ! -f $lsmod_file) {
  292. if ( -f $ENV{'objtree'}."/".$lsmod_file) {
  293. $lsmod_file = $ENV{'objtree'}."/".$lsmod_file;
  294. } else {
  295. die "$lsmod_file not found";
  296. }
  297. }
  298. if ( -x $lsmod_file) {
  299. # the file is executable, run it
  300. open(LIN, "$lsmod_file|");
  301. } else {
  302. # Just read the contents
  303. open(LIN, "$lsmod_file");
  304. }
  305. } else {
  306. # see what modules are loaded on this system
  307. my $lsmod;
  308. foreach my $dir ( ("/sbin", "/bin", "/usr/sbin", "/usr/bin") ) {
  309. if ( -x "$dir/lsmod" ) {
  310. $lsmod = "$dir/lsmod";
  311. last;
  312. }
  313. }
  314. if (!defined($lsmod)) {
  315. # try just the path
  316. $lsmod = "lsmod";
  317. }
  318. open(LIN,"$lsmod|") || die "Can not call lsmod with $lsmod";
  319. }
  320. while (<LIN>) {
  321. next if (/^Module/); # Skip the first line.
  322. if (/^(\S+)/) {
  323. $modules{$1} = 1;
  324. }
  325. }
  326. close (LIN);
  327. # add to the configs hash all configs that are needed to enable
  328. # a loaded module. This is a direct obj-${CONFIG_FOO} += bar.o
  329. # where we know we need bar.o so we add FOO to the list.
  330. my %configs;
  331. foreach my $module (keys(%modules)) {
  332. if (defined($objects{$module})) {
  333. my @arr = @{$objects{$module}};
  334. foreach my $conf (@arr) {
  335. $configs{$conf} = $module;
  336. dprint "$conf added by direct ($module)\n";
  337. }
  338. } else {
  339. # Most likely, someone has a custom (binary?) module loaded.
  340. print STDERR "$module config not found!!\n";
  341. }
  342. }
  343. # Read the current config, and see what is enabled. We want to
  344. # ignore configs that we would not enable anyway.
  345. my %orig_configs;
  346. my $valid = "A-Za-z_0-9";
  347. foreach my $line (@config_file) {
  348. $_ = $line;
  349. if (/(CONFIG_[$valid]*)=(m|y)/) {
  350. $orig_configs{$1} = $2;
  351. }
  352. }
  353. my $repeat = 1;
  354. my $depconfig;
  355. #
  356. # Note, we do not care about operands (like: &&, ||, !) we want to add any
  357. # config that is in the depend list of another config. This script does
  358. # not enable configs that are not already enabled. If we come across a
  359. # config A that depends on !B, we can still add B to the list of depends
  360. # to keep on. If A was on in the original config, B would not have been
  361. # and B would not be turned on by this script.
  362. #
  363. sub parse_config_depends
  364. {
  365. my ($p) = @_;
  366. while ($p =~ /[$valid]/) {
  367. if ($p =~ /^[^$valid]*([$valid]+)/) {
  368. my $conf = "CONFIG_" . $1;
  369. $p =~ s/^[^$valid]*[$valid]+//;
  370. # We only need to process if the depend config is a module
  371. if (!defined($orig_configs{$conf}) || !$orig_configs{conf} eq "m") {
  372. next;
  373. }
  374. if (!defined($configs{$conf})) {
  375. # We must make sure that this config has its
  376. # dependencies met.
  377. $repeat = 1; # do again
  378. dprint "$conf selected by depend $depconfig\n";
  379. $configs{$conf} = 1;
  380. }
  381. } else {
  382. die "this should never happen";
  383. }
  384. }
  385. }
  386. # Select is treated a bit differently than depends. We call this
  387. # when a config has no prompt and requires another config to be
  388. # selected. We use to just select all configs that selected this
  389. # config, but found that that can balloon into enabling hundreds
  390. # of configs that we do not care about.
  391. #
  392. # The idea is we look at all the configs that select it. If one
  393. # is already in our list of configs to enable, then there's nothing
  394. # else to do. If there isn't, we pick the first config that was
  395. # enabled in the orignal config and use that.
  396. sub parse_config_selects
  397. {
  398. my ($config, $p) = @_;
  399. my $next_config;
  400. while ($p =~ /[$valid]/) {
  401. if ($p =~ /^[^$valid]*([$valid]+)/) {
  402. my $conf = "CONFIG_" . $1;
  403. $p =~ s/^[^$valid]*[$valid]+//;
  404. # Make sure that this config exists in the current .config file
  405. if (!defined($orig_configs{$conf})) {
  406. dprint "$conf not set for $config select\n";
  407. next;
  408. }
  409. # Check if something other than a module selects this config
  410. if (defined($orig_configs{$conf}) && $orig_configs{$conf} ne "m") {
  411. dprint "$conf (non module) selects config, we are good\n";
  412. # we are good with this
  413. return;
  414. }
  415. if (defined($configs{$conf})) {
  416. dprint "$conf selects $config so we are good\n";
  417. # A set config selects this config, we are good
  418. return;
  419. }
  420. # Set this config to be selected
  421. if (!defined($next_config)) {
  422. $next_config = $conf;
  423. }
  424. } else {
  425. die "this should never happen";
  426. }
  427. }
  428. # If no possible config selected this, then something happened.
  429. if (!defined($next_config)) {
  430. print STDERR "WARNING: $config is required, but nothing in the\n";
  431. print STDERR " current config selects it.\n";
  432. return;
  433. }
  434. # If we are here, then we found no config that is set and
  435. # selects this config. Repeat.
  436. $repeat = 1;
  437. # Make this config need to be selected
  438. $configs{$next_config} = 1;
  439. dprint "$next_config selected by select $config\n";
  440. }
  441. my %process_selects;
  442. # loop through all configs, select their dependencies.
  443. sub loop_depend {
  444. $repeat = 1;
  445. while ($repeat) {
  446. $repeat = 0;
  447. forloop:
  448. foreach my $config (keys %configs) {
  449. # If this config is not a module, we do not need to process it
  450. if (defined($orig_configs{$config}) && $orig_configs{$config} ne "m") {
  451. next forloop;
  452. }
  453. $config =~ s/^CONFIG_//;
  454. $depconfig = $config;
  455. if (defined($depends{$config})) {
  456. # This config has dependencies. Make sure they are also included
  457. parse_config_depends $depends{$config};
  458. }
  459. # If the config has no prompt, then we need to check if a config
  460. # that is enabled selected it. Or if we need to enable one.
  461. if (!defined($prompts{$config}) && defined($selects{$config})) {
  462. $process_selects{$config} = 1;
  463. }
  464. }
  465. }
  466. }
  467. sub loop_select {
  468. foreach my $config (keys %process_selects) {
  469. $config =~ s/^CONFIG_//;
  470. dprint "Process select $config\n";
  471. # config has no prompt and must be selected.
  472. parse_config_selects $config, $selects{$config};
  473. }
  474. }
  475. while ($repeat) {
  476. # Get the first set of configs and their dependencies.
  477. loop_depend;
  478. $repeat = 0;
  479. # Now we need to see if we have to check selects;
  480. loop_select;
  481. }
  482. my %setconfigs;
  483. # Finally, read the .config file and turn off any module enabled that
  484. # we could not find a reason to keep enabled.
  485. foreach my $line (@config_file) {
  486. $_ = $line;
  487. if (/CONFIG_IKCONFIG/) {
  488. if (/# CONFIG_IKCONFIG is not set/) {
  489. # enable IKCONFIG at least as a module
  490. print "CONFIG_IKCONFIG=m\n";
  491. # don't ask about PROC
  492. print "# CONFIG_IKCONFIG_PROC is not set\n";
  493. } else {
  494. print;
  495. }
  496. next;
  497. }
  498. if (/^(CONFIG.*)=(m|y)/) {
  499. if (defined($configs{$1})) {
  500. if ($localyesconfig) {
  501. $setconfigs{$1} = 'y';
  502. } else {
  503. $setconfigs{$1} = $2;
  504. }
  505. } elsif ($2 eq "m") {
  506. print "# $1 is not set\n";
  507. next;
  508. }
  509. }
  510. print;
  511. }
  512. # Integrity check, make sure all modules that we want enabled do
  513. # indeed have their configs set.
  514. loop:
  515. foreach my $module (keys(%modules)) {
  516. if (defined($objects{$module})) {
  517. my @arr = @{$objects{$module}};
  518. foreach my $conf (@arr) {
  519. if (defined($setconfigs{$conf})) {
  520. next loop;
  521. }
  522. }
  523. print STDERR "module $module did not have configs";
  524. foreach my $conf (@arr) {
  525. print STDERR " " , $conf;
  526. }
  527. print STDERR "\n";
  528. }
  529. }