streamline_config.pl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 $linuxpath = ".";
  47. open(CIN,$config) || die "Can't open current config file: $config";
  48. my @makefiles = `find $linuxpath -name Makefile`;
  49. my %depends;
  50. my %selects;
  51. my %prompts;
  52. my %objects;
  53. my $var;
  54. my $cont = 0;
  55. # Get the top level Kconfig file (passed in)
  56. my $kconfig = $ARGV[0];
  57. # prevent recursion
  58. my %read_kconfigs;
  59. sub read_kconfig {
  60. my ($kconfig) = @_;
  61. my $state = "NONE";
  62. my $config;
  63. my @kconfigs;
  64. open(KIN, $kconfig) || die "Can't open $kconfig";
  65. while (<KIN>) {
  66. chomp;
  67. # collect any Kconfig sources
  68. if (/^source\s*"(.*)"/) {
  69. $kconfigs[$#kconfigs+1] = $1;
  70. }
  71. # configs found
  72. if (/^\s*config\s+(\S+)\s*$/) {
  73. $state = "NEW";
  74. $config = $1;
  75. # collect the depends for the config
  76. } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
  77. $state = "DEP";
  78. $depends{$config} = $1;
  79. } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
  80. $depends{$config} .= " " . $1;
  81. # Get the configs that select this config
  82. } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
  83. if (defined($selects{$1})) {
  84. $selects{$1} .= " " . $config;
  85. } else {
  86. $selects{$1} = $config;
  87. }
  88. # configs without prompts must be selected
  89. } elsif ($state ne "NONE" && /^\s*tristate\s\S/) {
  90. # note if the config has a prompt
  91. $prompt{$config} = 1;
  92. # stop on "help"
  93. } elsif (/^\s*help\s*$/) {
  94. $state = "NONE";
  95. }
  96. }
  97. close(KIN);
  98. # read in any configs that were found.
  99. foreach $kconfig (@kconfigs) {
  100. if (!defined($read_kconfigs{$kconfig})) {
  101. $read_kconfigs{$kconfig} = 1;
  102. read_kconfig($kconfig);
  103. }
  104. }
  105. }
  106. if ($kconfig) {
  107. read_kconfig($kconfig);
  108. }
  109. # Read all Makefiles to map the configs to the objects
  110. foreach my $makefile (@makefiles) {
  111. chomp $makefile;
  112. open(MIN,$makefile) || die "Can't open $makefile";
  113. while (<MIN>) {
  114. my $objs;
  115. # is this a line after a line with a backslash?
  116. if ($cont && /(\S.*)$/) {
  117. $objs = $1;
  118. }
  119. $cont = 0;
  120. # collect objects after obj-$(CONFIG_FOO_BAR)
  121. if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
  122. $var = $1;
  123. $objs = $2;
  124. }
  125. if (defined($objs)) {
  126. # test if the line ends with a backslash
  127. if ($objs =~ m,(.*)\\$,) {
  128. $objs = $1;
  129. $cont = 1;
  130. }
  131. foreach my $obj (split /\s+/,$objs) {
  132. $obj =~ s/-/_/g;
  133. if ($obj =~ /(.*)\.o$/) {
  134. # Objects may bes enabled by more than one config.
  135. # Store configs in an array.
  136. my @arr;
  137. if (defined($objects{$1})) {
  138. @arr = @{$objects{$1}};
  139. }
  140. $arr[$#arr+1] = $var;
  141. # The objects have a hash mapping to a reference
  142. # of an array of configs.
  143. $objects{$1} = \@arr;
  144. }
  145. }
  146. }
  147. }
  148. close(MIN);
  149. }
  150. my %modules;
  151. # see what modules are loaded on this system
  152. open(LIN,"/sbin/lsmod|") || die "Cant lsmod";
  153. while (<LIN>) {
  154. next if (/^Module/); # Skip the first line.
  155. if (/^(\S+)/) {
  156. $modules{$1} = 1;
  157. }
  158. }
  159. close (LIN);
  160. # add to the configs hash all configs that are needed to enable
  161. # a loaded module.
  162. my %configs;
  163. foreach my $module (keys(%modules)) {
  164. if (defined($objects{$module})) {
  165. @arr = @{$objects{$module}};
  166. foreach my $conf (@arr) {
  167. $configs{$conf} = $module;
  168. }
  169. } else {
  170. # Most likely, someone has a custom (binary?) module loaded.
  171. print STDERR "$module config not found!!\n";
  172. }
  173. }
  174. my $valid = "A-Za-z_0-9";
  175. my $repeat = 1;
  176. #
  177. # Note, we do not care about operands (like: &&, ||, !) we want to add any
  178. # config that is in the depend list of another config. This script does
  179. # not enable configs that are not already enabled. If we come across a
  180. # config A that depends on !B, we can still add B to the list of depends
  181. # to keep on. If A was on in the original config, B would not have been
  182. # and B would not be turned on by this script.
  183. #
  184. sub parse_config_dep_select
  185. {
  186. my ($p) = @_;
  187. while ($p =~ /[$valid]/) {
  188. if ($p =~ /^[^$valid]*([$valid]+)/) {
  189. my $conf = "CONFIG_" . $1;
  190. $p =~ s/^[^$valid]*[$valid]+//;
  191. if (!defined($configs{$conf})) {
  192. # We must make sure that this config has its
  193. # dependencies met.
  194. $repeat = 1; # do again
  195. $configs{$conf} = 1;
  196. }
  197. } else {
  198. die "this should never happen";
  199. }
  200. }
  201. }
  202. while ($repeat) {
  203. $repeat = 0;
  204. foreach my $config (keys %configs) {
  205. $config =~ s/^CONFIG_//;
  206. if (defined($depends{$config})) {
  207. # This config has dependencies. Make sure they are also included
  208. parse_config_dep_select $depends{$config};
  209. }
  210. if (defined($prompt{$config}) || !defined($selects{$config})) {
  211. next;
  212. }
  213. # config has no prompt and must be selected.
  214. parse_config_dep_select $selects{$config};
  215. }
  216. }
  217. my %setconfigs;
  218. # Finally, read the .config file and turn off any module enabled that
  219. # we could not find a reason to keep enabled.
  220. while(<CIN>) {
  221. if (/CONFIG_IKCONFIG/) {
  222. if (/# CONFIG_IKCONFIG is not set/) {
  223. # enable IKCONFIG at least as a module
  224. print "CONFIG_IKCONFIG=m\n";
  225. # don't ask about PROC
  226. print "# CONFIG_IKCONFIG is not set\n";
  227. } else {
  228. print;
  229. }
  230. next;
  231. }
  232. if (/^(CONFIG.*)=(m|y)/) {
  233. if (defined($configs{$1})) {
  234. $setconfigs{$1} = $2;
  235. print;
  236. } elsif ($2 eq "m") {
  237. print "# $1 is not set\n";
  238. } else {
  239. print;
  240. }
  241. } else {
  242. print;
  243. }
  244. }
  245. close(CIN);
  246. # Integrity check, make sure all modules that we want enabled do
  247. # indeed have their configs set.
  248. loop:
  249. foreach my $module (keys(%modules)) {
  250. if (defined($objects{$module})) {
  251. my @arr = @{$objects{$module}};
  252. foreach my $conf (@arr) {
  253. if (defined($setconfigs{$conf})) {
  254. next loop;
  255. }
  256. }
  257. print STDERR "module $module did not have configs";
  258. foreach my $conf (@arr) {
  259. print STDERR " " , $conf;
  260. }
  261. print STDERR "\n";
  262. }
  263. }