streamline_config.pl 9.4 KB

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