streamline_config.pl 10 KB

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