streamline_config.pl 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. 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. my @makefiles = `find $linuxpath -name Makefile`;
  105. my %depends;
  106. my %selects;
  107. my %prompts;
  108. my %objects;
  109. my $var;
  110. my $cont = 0;
  111. # Get the top level Kconfig file (passed in)
  112. my $kconfig = $ARGV[0];
  113. # prevent recursion
  114. my %read_kconfigs;
  115. sub read_kconfig {
  116. my ($kconfig) = @_;
  117. my $state = "NONE";
  118. my $config;
  119. my @kconfigs;
  120. open(KIN, $kconfig) || die "Can't open $kconfig";
  121. while (<KIN>) {
  122. chomp;
  123. # collect any Kconfig sources
  124. if (/^source\s*"(.*)"/) {
  125. $kconfigs[$#kconfigs+1] = $1;
  126. }
  127. # configs found
  128. if (/^\s*config\s+(\S+)\s*$/) {
  129. $state = "NEW";
  130. $config = $1;
  131. # collect the depends for the config
  132. } elsif ($state eq "NEW" && /^\s*depends\s+on\s+(.*)$/) {
  133. $state = "DEP";
  134. $depends{$config} = $1;
  135. } elsif ($state eq "DEP" && /^\s*depends\s+on\s+(.*)$/) {
  136. $depends{$config} .= " " . $1;
  137. # Get the configs that select this config
  138. } elsif ($state ne "NONE" && /^\s*select\s+(\S+)/) {
  139. if (defined($selects{$1})) {
  140. $selects{$1} .= " " . $config;
  141. } else {
  142. $selects{$1} = $config;
  143. }
  144. # configs without prompts must be selected
  145. } elsif ($state ne "NONE" && /^\s*tristate\s\S/) {
  146. # note if the config has a prompt
  147. $prompt{$config} = 1;
  148. # stop on "help"
  149. } elsif (/^\s*help\s*$/) {
  150. $state = "NONE";
  151. }
  152. }
  153. close(KIN);
  154. # read in any configs that were found.
  155. foreach $kconfig (@kconfigs) {
  156. if (!defined($read_kconfigs{$kconfig})) {
  157. $read_kconfigs{$kconfig} = 1;
  158. read_kconfig($kconfig);
  159. }
  160. }
  161. }
  162. if ($kconfig) {
  163. read_kconfig($kconfig);
  164. }
  165. # Read all Makefiles to map the configs to the objects
  166. foreach my $makefile (@makefiles) {
  167. chomp $makefile;
  168. open(MIN,$makefile) || die "Can't open $makefile";
  169. while (<MIN>) {
  170. my $objs;
  171. # is this a line after a line with a backslash?
  172. if ($cont && /(\S.*)$/) {
  173. $objs = $1;
  174. }
  175. $cont = 0;
  176. # collect objects after obj-$(CONFIG_FOO_BAR)
  177. if (/obj-\$\((CONFIG_[^\)]*)\)\s*[+:]?=\s*(.*)/) {
  178. $var = $1;
  179. $objs = $2;
  180. }
  181. if (defined($objs)) {
  182. # test if the line ends with a backslash
  183. if ($objs =~ m,(.*)\\$,) {
  184. $objs = $1;
  185. $cont = 1;
  186. }
  187. foreach my $obj (split /\s+/,$objs) {
  188. $obj =~ s/-/_/g;
  189. if ($obj =~ /(.*)\.o$/) {
  190. # Objects may bes enabled by more than one config.
  191. # Store configs in an array.
  192. my @arr;
  193. if (defined($objects{$1})) {
  194. @arr = @{$objects{$1}};
  195. }
  196. $arr[$#arr+1] = $var;
  197. # The objects have a hash mapping to a reference
  198. # of an array of configs.
  199. $objects{$1} = \@arr;
  200. }
  201. }
  202. }
  203. }
  204. close(MIN);
  205. }
  206. my %modules;
  207. # see what modules are loaded on this system
  208. open(LIN,"/sbin/lsmod|") || die "Cant lsmod";
  209. while (<LIN>) {
  210. next if (/^Module/); # Skip the first line.
  211. if (/^(\S+)/) {
  212. $modules{$1} = 1;
  213. }
  214. }
  215. close (LIN);
  216. # add to the configs hash all configs that are needed to enable
  217. # a loaded module.
  218. my %configs;
  219. foreach my $module (keys(%modules)) {
  220. if (defined($objects{$module})) {
  221. @arr = @{$objects{$module}};
  222. foreach my $conf (@arr) {
  223. $configs{$conf} = $module;
  224. }
  225. } else {
  226. # Most likely, someone has a custom (binary?) module loaded.
  227. print STDERR "$module config not found!!\n";
  228. }
  229. }
  230. my $valid = "A-Za-z_0-9";
  231. my $repeat = 1;
  232. #
  233. # Note, we do not care about operands (like: &&, ||, !) we want to add any
  234. # config that is in the depend list of another config. This script does
  235. # not enable configs that are not already enabled. If we come across a
  236. # config A that depends on !B, we can still add B to the list of depends
  237. # to keep on. If A was on in the original config, B would not have been
  238. # and B would not be turned on by this script.
  239. #
  240. sub parse_config_dep_select
  241. {
  242. my ($p) = @_;
  243. while ($p =~ /[$valid]/) {
  244. if ($p =~ /^[^$valid]*([$valid]+)/) {
  245. my $conf = "CONFIG_" . $1;
  246. $p =~ s/^[^$valid]*[$valid]+//;
  247. if (!defined($configs{$conf})) {
  248. # We must make sure that this config has its
  249. # dependencies met.
  250. $repeat = 1; # do again
  251. $configs{$conf} = 1;
  252. }
  253. } else {
  254. die "this should never happen";
  255. }
  256. }
  257. }
  258. while ($repeat) {
  259. $repeat = 0;
  260. foreach my $config (keys %configs) {
  261. $config =~ s/^CONFIG_//;
  262. if (defined($depends{$config})) {
  263. # This config has dependencies. Make sure they are also included
  264. parse_config_dep_select $depends{$config};
  265. }
  266. if (defined($prompt{$config}) || !defined($selects{$config})) {
  267. next;
  268. }
  269. # config has no prompt and must be selected.
  270. parse_config_dep_select $selects{$config};
  271. }
  272. }
  273. my %setconfigs;
  274. # Finally, read the .config file and turn off any module enabled that
  275. # we could not find a reason to keep enabled.
  276. while(<CIN>) {
  277. if (/CONFIG_IKCONFIG/) {
  278. if (/# CONFIG_IKCONFIG is not set/) {
  279. # enable IKCONFIG at least as a module
  280. print "CONFIG_IKCONFIG=m\n";
  281. # don't ask about PROC
  282. print "# CONFIG_IKCONFIG_PROC is not set\n";
  283. } else {
  284. print;
  285. }
  286. next;
  287. }
  288. if (/^(CONFIG.*)=(m|y)/) {
  289. if (defined($configs{$1})) {
  290. $setconfigs{$1} = $2;
  291. } elsif ($2 eq "m") {
  292. print "# $1 is not set\n";
  293. next;
  294. }
  295. }
  296. print;
  297. }
  298. close(CIN);
  299. # Integrity check, make sure all modules that we want enabled do
  300. # indeed have their configs set.
  301. loop:
  302. foreach my $module (keys(%modules)) {
  303. if (defined($objects{$module})) {
  304. my @arr = @{$objects{$module}};
  305. foreach my $conf (@arr) {
  306. if (defined($setconfigs{$conf})) {
  307. next loop;
  308. }
  309. }
  310. print STDERR "module $module did not have configs";
  311. foreach my $conf (@arr) {
  312. print STDERR " " , $conf;
  313. }
  314. print STDERR "\n";
  315. }
  316. }