streamline_config.pl 11 KB

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