streamline_config.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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 read_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(my $infile, '-|', "$exec $file") || die "Failed to run $exec $file";
  108. my @x = <$infile>;
  109. close $infile;
  110. return @x;
  111. }
  112. die "No config file found";
  113. }
  114. my @config_file = read_config;
  115. # Parse options
  116. my $localmodconfig = 0;
  117. my $localyesconfig = 0;
  118. GetOptions("localmodconfig" => \$localmodconfig,
  119. "localyesconfig" => \$localyesconfig);
  120. # Get the build source and top level Kconfig file (passed in)
  121. my $ksource = ($ARGV[0] ? $ARGV[0] : '.');
  122. my $kconfig = $ARGV[1];
  123. my $lsmod_file = $ENV{'LSMOD'};
  124. my @makefiles = `find $ksource -name Makefile -or -name Kbuild 2>/dev/null`;
  125. chomp @makefiles;
  126. my %depends;
  127. my %selects;
  128. my %prompts;
  129. my %objects;
  130. my $var;
  131. my $iflevel = 0;
  132. my @ifdeps;
  133. # prevent recursion
  134. my %read_kconfigs;
  135. sub read_kconfig {
  136. my ($kconfig) = @_;
  137. my $state = "NONE";
  138. my $config;
  139. my $cont = 0;
  140. my $line;
  141. my $source = "$ksource/$kconfig";
  142. my $last_source = "";
  143. # Check for any environment variables used
  144. while ($source =~ /\$(\w+)/ && $last_source ne $source) {
  145. my $env = $1;
  146. $last_source = $source;
  147. $source =~ s/\$$env/$ENV{$env}/;
  148. }
  149. open(my $kinfile, '<', $source) || die "Can't open $kconfig";
  150. while (<$kinfile>) {
  151. chomp;
  152. # Make sure that lines ending with \ continue
  153. if ($cont) {
  154. $_ = $line . " " . $_;
  155. }
  156. if (s/\\$//) {
  157. $cont = 1;
  158. $line = $_;
  159. next;
  160. }
  161. $cont = 0;
  162. # collect any Kconfig sources
  163. if (/^source\s+"?([^"]+)/) {
  164. my $kconfig = $1;
  165. # prevent reading twice.
  166. if (!defined($read_kconfigs{$kconfig})) {
  167. $read_kconfigs{$kconfig} = 1;
  168. read_kconfig($kconfig);
  169. }
  170. next;
  171. }
  172. # configs found
  173. if (/^\s*(menu)?config\s+(\S+)\s*$/) {
  174. $state = "NEW";
  175. $config = $2;
  176. # Add depends for 'if' nesting
  177. for (my $i = 0; $i < $iflevel; $i++) {
  178. if ($i) {
  179. $depends{$config} .= " " . $ifdeps[$i];
  180. } else {
  181. $depends{$config} = $ifdeps[$i];
  182. }
  183. $state = "DEP";
  184. }
  185. # collect the depends for the config
  186. } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
  187. $state = "DEP";
  188. $depends{$config} = $1;
  189. } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
  190. $depends{$config} .= " " . $1;
  191. } elsif ($state eq "DEP" && /^\s*def(_(bool|tristate)|ault)\s+(\S.*)$/) {
  192. my $dep = $3;
  193. if ($dep !~ /^\s*(y|m|n)\s*$/) {
  194. $dep =~ s/.*\sif\s+//;
  195. $depends{$config} .= " " . $dep;
  196. dprint "Added default depends $dep to $config\n";
  197. }
  198. # Get the configs that select this config
  199. } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
  200. my $conf = $1;
  201. if (defined($selects{$conf})) {
  202. $selects{$conf} .= " " . $config;
  203. } else {
  204. $selects{$conf} = $config;
  205. }
  206. # configs without prompts must be selected
  207. } elsif ($state ne "NONE" && /^\s*(tristate\s+\S|prompt\b)/) {
  208. # note if the config has a prompt
  209. $prompts{$config} = 1;
  210. # Check for if statements
  211. } elsif (/^if\s+(.*\S)\s*$/) {
  212. my $deps = $1;
  213. # remove beginning and ending non text
  214. $deps =~ s/^[^a-zA-Z0-9_]*//;
  215. $deps =~ s/[^a-zA-Z0-9_]*$//;
  216. my @deps = split /[^a-zA-Z0-9_]+/, $deps;
  217. $ifdeps[$iflevel++] = join ':', @deps;
  218. } elsif (/^endif/) {
  219. $iflevel-- if ($iflevel);
  220. # stop on "help" and keywords that end a menu entry
  221. } elsif (/^\s*(---)?help(---)?\s*$/ || /^(comment|choice|menu)\b/) {
  222. $state = "NONE";
  223. }
  224. }
  225. close($kinfile);
  226. }
  227. if ($kconfig) {
  228. read_kconfig($kconfig);
  229. }
  230. # Makefiles can use variables to define their dependencies
  231. sub convert_vars {
  232. my ($line, %vars) = @_;
  233. my $process = "";
  234. while ($line =~ s/^(.*?)(\$\((.*?)\))//) {
  235. my $start = $1;
  236. my $variable = $2;
  237. my $var = $3;
  238. if (defined($vars{$var})) {
  239. $process .= $start . $vars{$var};
  240. } else {
  241. $process .= $start . $variable;
  242. }
  243. }
  244. $process .= $line;
  245. return $process;
  246. }
  247. # Read all Makefiles to map the configs to the objects
  248. foreach my $makefile (@makefiles) {
  249. my $line = "";
  250. my %make_vars;
  251. open(my $infile, '<', $makefile) || die "Can't open $makefile";
  252. while (<$infile>) {
  253. # if this line ends with a backslash, continue
  254. chomp;
  255. if (/^(.*)\\$/) {
  256. $line .= $1;
  257. next;
  258. }
  259. $line .= $_;
  260. $_ = $line;
  261. $line = "";
  262. my $objs;
  263. # Convert variables in a line (could define configs)
  264. $_ = convert_vars($_, %make_vars);
  265. # collect objects after obj-$(CONFIG_FOO_BAR)
  266. if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
  267. $var = $1;
  268. $objs = $2;
  269. # check if variables are set
  270. } elsif (/^\s*(\S+)\s*[:]?=\s*(.*\S)/) {
  271. $make_vars{$1} = $2;
  272. }
  273. if (defined($objs)) {
  274. foreach my $obj (split /\s+/,$objs) {
  275. $obj =~ s/-/_/g;
  276. if ($obj =~ /(.*)\.o$/) {
  277. # Objects may be enabled by more than one config.
  278. # Store configs in an array.
  279. my @arr;
  280. if (defined($objects{$1})) {
  281. @arr = @{$objects{$1}};
  282. }
  283. $arr[$#arr+1] = $var;
  284. # The objects have a hash mapping to a reference
  285. # of an array of configs.
  286. $objects{$1} = \@arr;
  287. }
  288. }
  289. }
  290. }
  291. close($infile);
  292. }
  293. my %modules;
  294. my $linfile;
  295. if (defined($lsmod_file)) {
  296. if ( ! -f $lsmod_file) {
  297. if ( -f $ENV{'objtree'}."/".$lsmod_file) {
  298. $lsmod_file = $ENV{'objtree'}."/".$lsmod_file;
  299. } else {
  300. die "$lsmod_file not found";
  301. }
  302. }
  303. my $otype = ( -x $lsmod_file) ? '-|' : '<';
  304. open($linfile, $otype, $lsmod_file);
  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($linfile, '-|', $lsmod) || die "Can not call lsmod with $lsmod";
  319. }
  320. while (<$linfile>) {
  321. next if (/^Module/); # Skip the first line.
  322. if (/^(\S+)/) {
  323. $modules{$1} = 1;
  324. }
  325. }
  326. close ($linfile);
  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. if ($debugprint) {
  338. my $c=$conf;
  339. $c =~ s/^CONFIG_//;
  340. if (defined($depends{$c})) {
  341. dprint " deps = $depends{$c}\n";
  342. } else {
  343. dprint " no deps\n";
  344. }
  345. }
  346. }
  347. } else {
  348. # Most likely, someone has a custom (binary?) module loaded.
  349. print STDERR "$module config not found!!\n";
  350. }
  351. }
  352. # Read the current config, and see what is enabled. We want to
  353. # ignore configs that we would not enable anyway.
  354. my %orig_configs;
  355. my $valid = "A-Za-z_0-9";
  356. foreach my $line (@config_file) {
  357. $_ = $line;
  358. if (/(CONFIG_[$valid]*)=(m|y)/) {
  359. $orig_configs{$1} = $2;
  360. }
  361. }
  362. my $repeat = 1;
  363. my $depconfig;
  364. #
  365. # Note, we do not care about operands (like: &&, ||, !) we want to add any
  366. # config that is in the depend list of another config. This script does
  367. # not enable configs that are not already enabled. If we come across a
  368. # config A that depends on !B, we can still add B to the list of depends
  369. # to keep on. If A was on in the original config, B would not have been
  370. # and B would not be turned on by this script.
  371. #
  372. sub parse_config_depends
  373. {
  374. my ($p) = @_;
  375. while ($p =~ /[$valid]/) {
  376. if ($p =~ /^[^$valid]*([$valid]+)/) {
  377. my $conf = "CONFIG_" . $1;
  378. $p =~ s/^[^$valid]*[$valid]+//;
  379. # We only need to process if the depend config is a module
  380. if (!defined($orig_configs{$conf}) || $orig_configs{$conf} eq "y") {
  381. next;
  382. }
  383. if (!defined($configs{$conf})) {
  384. # We must make sure that this config has its
  385. # dependencies met.
  386. $repeat = 1; # do again
  387. dprint "$conf selected by depend $depconfig\n";
  388. $configs{$conf} = 1;
  389. }
  390. } else {
  391. die "this should never happen";
  392. }
  393. }
  394. }
  395. # Select is treated a bit differently than depends. We call this
  396. # when a config has no prompt and requires another config to be
  397. # selected. We use to just select all configs that selected this
  398. # config, but found that that can balloon into enabling hundreds
  399. # of configs that we do not care about.
  400. #
  401. # The idea is we look at all the configs that select it. If one
  402. # is already in our list of configs to enable, then there's nothing
  403. # else to do. If there isn't, we pick the first config that was
  404. # enabled in the orignal config and use that.
  405. sub parse_config_selects
  406. {
  407. my ($config, $p) = @_;
  408. my $next_config;
  409. while ($p =~ /[$valid]/) {
  410. if ($p =~ /^[^$valid]*([$valid]+)/) {
  411. my $conf = "CONFIG_" . $1;
  412. $p =~ s/^[^$valid]*[$valid]+//;
  413. # Make sure that this config exists in the current .config file
  414. if (!defined($orig_configs{$conf})) {
  415. dprint "$conf not set for $config select\n";
  416. next;
  417. }
  418. # Check if something other than a module selects this config
  419. if (defined($orig_configs{$conf}) && $orig_configs{$conf} ne "m") {
  420. dprint "$conf (non module) selects config, we are good\n";
  421. # we are good with this
  422. return;
  423. }
  424. if (defined($configs{$conf})) {
  425. dprint "$conf selects $config so we are good\n";
  426. # A set config selects this config, we are good
  427. return;
  428. }
  429. # Set this config to be selected
  430. if (!defined($next_config)) {
  431. $next_config = $conf;
  432. }
  433. } else {
  434. die "this should never happen";
  435. }
  436. }
  437. # If no possible config selected this, then something happened.
  438. if (!defined($next_config)) {
  439. print STDERR "WARNING: $config is required, but nothing in the\n";
  440. print STDERR " current config selects it.\n";
  441. return;
  442. }
  443. # If we are here, then we found no config that is set and
  444. # selects this config. Repeat.
  445. $repeat = 1;
  446. # Make this config need to be selected
  447. $configs{$next_config} = 1;
  448. dprint "$next_config selected by select $config\n";
  449. }
  450. my %process_selects;
  451. # loop through all configs, select their dependencies.
  452. sub loop_depend {
  453. $repeat = 1;
  454. while ($repeat) {
  455. $repeat = 0;
  456. forloop:
  457. foreach my $config (keys %configs) {
  458. # If this config is not a module, we do not need to process it
  459. if (defined($orig_configs{$config}) && $orig_configs{$config} ne "m") {
  460. next forloop;
  461. }
  462. $config =~ s/^CONFIG_//;
  463. $depconfig = $config;
  464. if (defined($depends{$config})) {
  465. # This config has dependencies. Make sure they are also included
  466. parse_config_depends $depends{$config};
  467. }
  468. # If the config has no prompt, then we need to check if a config
  469. # that is enabled selected it. Or if we need to enable one.
  470. if (!defined($prompts{$config}) && defined($selects{$config})) {
  471. $process_selects{$config} = 1;
  472. }
  473. }
  474. }
  475. }
  476. sub loop_select {
  477. foreach my $config (keys %process_selects) {
  478. $config =~ s/^CONFIG_//;
  479. dprint "Process select $config\n";
  480. # config has no prompt and must be selected.
  481. parse_config_selects $config, $selects{$config};
  482. }
  483. }
  484. while ($repeat) {
  485. # Get the first set of configs and their dependencies.
  486. loop_depend;
  487. $repeat = 0;
  488. # Now we need to see if we have to check selects;
  489. loop_select;
  490. }
  491. my %setconfigs;
  492. # Finally, read the .config file and turn off any module enabled that
  493. # we could not find a reason to keep enabled.
  494. foreach my $line (@config_file) {
  495. $_ = $line;
  496. if (/CONFIG_IKCONFIG/) {
  497. if (/# CONFIG_IKCONFIG is not set/) {
  498. # enable IKCONFIG at least as a module
  499. print "CONFIG_IKCONFIG=m\n";
  500. # don't ask about PROC
  501. print "# CONFIG_IKCONFIG_PROC is not set\n";
  502. } else {
  503. print;
  504. }
  505. next;
  506. }
  507. if (/CONFIG_MODULE_SIG_KEY="(.+)"/) {
  508. my $orig_cert = $1;
  509. my $default_cert = "certs/signing_key.pem";
  510. # Check that the logic in this script still matches the one in Kconfig
  511. if (!defined($depends{"MODULE_SIG_KEY"}) ||
  512. $depends{"MODULE_SIG_KEY"} !~ /"\Q$default_cert\E"/) {
  513. print STDERR "WARNING: MODULE_SIG_KEY assertion failure, ",
  514. "update needed to ", __FILE__, " line ", __LINE__, "\n";
  515. print;
  516. } elsif ($orig_cert ne $default_cert && ! -f $orig_cert) {
  517. print STDERR "Module signature verification enabled but ",
  518. "module signing key \"$orig_cert\" not found. Resetting ",
  519. "signing key to default value.\n";
  520. print "CONFIG_MODULE_SIG_KEY=\"$default_cert\"\n";
  521. } else {
  522. print;
  523. }
  524. next;
  525. }
  526. if (/CONFIG_SYSTEM_TRUSTED_KEYS="(.+)"/) {
  527. my $orig_keys = $1;
  528. if (! -f $orig_keys) {
  529. print STDERR "System keyring enabled but keys \"$orig_keys\" ",
  530. "not found. Resetting keys to default value.\n";
  531. print "CONFIG_SYSTEM_TRUSTED_KEYS=\"\"\n";
  532. } else {
  533. print;
  534. }
  535. next;
  536. }
  537. if (/^(CONFIG.*)=(m|y)/) {
  538. if (defined($configs{$1})) {
  539. if ($localyesconfig) {
  540. $setconfigs{$1} = 'y';
  541. print "$1=y\n";
  542. next;
  543. } else {
  544. $setconfigs{$1} = $2;
  545. }
  546. } elsif ($2 eq "m") {
  547. print "# $1 is not set\n";
  548. next;
  549. }
  550. }
  551. print;
  552. }
  553. # Integrity check, make sure all modules that we want enabled do
  554. # indeed have their configs set.
  555. loop:
  556. foreach my $module (keys(%modules)) {
  557. if (defined($objects{$module})) {
  558. my @arr = @{$objects{$module}};
  559. foreach my $conf (@arr) {
  560. if (defined($setconfigs{$conf})) {
  561. next loop;
  562. }
  563. }
  564. print STDERR "module $module did not have configs";
  565. foreach my $conf (@arr) {
  566. print STDERR " " , $conf;
  567. }
  568. print STDERR "\n";
  569. }
  570. }