streamline_config.pl 16 KB

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