streamline_config.pl 11 KB

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