<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>K&#039;s Lair &#187; kernel</title>
	<atom:link href="http://andreas.goelzer.de/tag/kernel/feed" rel="self" type="application/rss+xml" />
	<link>http://andreas.goelzer.de</link>
	<description>Electronics and small programs and other stuff</description>
	<lastBuildDate>Thu, 02 Sep 2010 12:30:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>kernel config based on lsmod output</title>
		<link>http://andreas.goelzer.de/kernel-config-based-on-lsmod-output</link>
		<comments>http://andreas.goelzer.de/kernel-config-based-on-lsmod-output#comments</comments>
		<pubDate>Mon, 06 Oct 2008 11:35:20 +0000</pubDate>
		<dc:creator>Andreas Goelzer</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://andreas.goelzer.de/?p=23</guid>
		<description><![CDATA[After reading about the amazing 5-seconds bootup I decided to once again compile a kernel myself. Compiling a kernel is almost trivial these days, but customizing the configuration can still be quite confusing. For example, the names of the modules &#8230; <a href="http://andreas.goelzer.de/kernel-config-based-on-lsmod-output">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After reading about the amazing <a href="http://lwn.net/Articles/299483/">5-seconds bootup</a> I decided to once again compile a kernel myself. Compiling a kernel is almost trivial these days, but customizing the configuration can still be quite confusing. For example, the names of the modules in lsmod aren&#8217;t the ones you select as config options. To map them, I found some scripts in the <a href="http://wiki.linuxquestions.org/wiki/Configuring_linux_kernel">LQWiki</a>, but they weren&#8217;t that easily to use, and also programming in bash is just painful.</p>
<p>So i wrote a python variant, that takes an input config(for example your distributions config) and changes the reply to &#8220;y&#8221; for all config options if the respective module is loaded(ie. if ext3 is loaded, CONFIG_EXT3_FS=y will be set).</p>
<p>In almost all cases you still want to tweak the resulting config file with make menuconfig. Also keep in mind that some things don&#8217;t work that easily if compiled in, for example if firmware has to be loaded from a file, the disk should be accessible.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
<span style="color: #483d8b;">&quot;&quot;&quot;
Script to modify kernel config based on lsmod output
Copyright (C) 2008  Andreas Goelzer
&nbsp;
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
&nbsp;
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
&nbsp;
You should have received a copy of the GNU General Public License
along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
&nbsp;
&nbsp;
Based on a previous config and the output of lsmod, this script determines
which modules could be compiled in and generates a new config.
See http://andreas.goelzer.de/kernel-config-based-on-lsmod-output for updates
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">optparse</span> <span style="color: #ff7700;font-weight:bold;">import</span> OptionParser<span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">os</span> <span style="color: #ff7700;font-weight:bold;">import</span> popen<span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">sys</span> <span style="color: #ff7700;font-weight:bold;">import</span> stderr, stdout, stdin<span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #dc143c;">parser</span> = OptionParser<span style="color: black;">&#40;</span>version=<span style="color: #483d8b;">&quot;%prog 0.31&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-i&quot;</span>, <span style="color: #483d8b;">&quot;--infile&quot;</span>, dest=<span style="color: #483d8b;">&quot;cfgfile&quot;</span>,
                  <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;input config file&quot;</span>, default=<span style="color: #483d8b;">&quot;.config&quot;</span>, metavar=<span style="color: #483d8b;">&quot;FILE&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-o&quot;</span>, <span style="color: #483d8b;">&quot;--outfile&quot;</span>, dest=<span style="color: #483d8b;">&quot;outfile&quot;</span>,
                  <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;output config file&quot;</span>, default=<span style="color: #483d8b;">&quot;-&quot;</span>, metavar=<span style="color: #483d8b;">&quot;FILE&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-l&quot;</span>, <span style="color: #483d8b;">&quot;--logfile&quot;</span>, dest=<span style="color: #483d8b;">&quot;logfile&quot;</span>,
                  <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;file to log errors to&quot;</span>, default=<span style="color: #483d8b;">&quot;-&quot;</span>, metavar=<span style="color: #483d8b;">&quot;FILE&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-s&quot;</span>, <span style="color: #483d8b;">&quot;--sourcedir&quot;</span>, dest=<span style="color: #483d8b;">&quot;sourcedir&quot;</span>,
                  <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;kernel source tree&quot;</span>, default=<span style="color: #483d8b;">&quot;.&quot;</span>, metavar=<span style="color: #483d8b;">&quot;DIR&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #808080; font-style: italic;">#parser.add_option(&quot;-v&quot;, &quot;--verbose&quot;,</span>
                  <span style="color: #808080; font-style: italic;">#action=&quot;store_true&quot;, dest=&quot;verbose&quot;, default=False,</span>
                  <span style="color: #808080; font-style: italic;">#help=&quot;print debug messages&quot;);</span>
&nbsp;
<span style="color: black;">&#40;</span>options, args<span style="color: black;">&#41;</span> = <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>options.<span style="color: black;">outfile</span> == <span style="color: #483d8b;">'-'</span><span style="color: black;">&#41;</span>: of = stdout<span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>: of = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>options.<span style="color: black;">outfile</span>, <span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>options.<span style="color: black;">logfile</span> <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">'-'</span><span style="color: black;">&#41;</span>: stderr = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>options.<span style="color: black;">logfile</span>, <span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
&nbsp;
loadedmods=popen<span style="color: black;">&#40;</span><span style="color: #483d8b;">'lsmod | tail -n+2'</span><span style="color: black;">&#41;</span>.<span style="color: black;">readlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
getmodname=<span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;^(?P&lt;modname&gt;<span style="color: #000099; font-weight: bold;">\w</span>*)&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#prob. need to replace kernel with sth. like (kernel|ubuntu) for an ubuntu kernel source</span>
parsepath=<span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;/kernel(?P&lt;path&gt;/.*/)(?P&lt;file&gt;.*).ko&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
wantin=<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> module <span style="color: #ff7700;font-weight:bold;">in</span> loadedmods:
	modname = <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span>getmodname,module<span style="color: black;">&#41;</span>.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'modname'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	moduleprops=<span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span>parsepath,popen<span style="color: black;">&#40;</span><span style="color: #483d8b;">'modinfo -n '</span> + modname<span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>moduleprops<span style="color: black;">&#41;</span>:
		<span style="color: #808080; font-style: italic;">#search the makefile for the module name</span>
		<span style="color: #ff7700;font-weight:bold;">try</span>:
			f=<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>options.<span style="color: black;">sourcedir</span> + moduleprops.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'path'</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">'Makefile'</span> , <span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IOError</span>:
			stderr.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Could not find Makefile for '</span> + modname + <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
			<span style="color: #ff7700;font-weight:bold;">continue</span>
		cont=f.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
		f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
		m=<span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;obj-<span style="color: #000099; font-weight: bold;">\$</span><span style="color: #000099; font-weight: bold;">\(</span>(?P&lt;cfgname&gt;[A-Z0-9_]*)<span style="color: #000099; font-weight: bold;">\)</span><span style="color: #000099; font-weight: bold;">\W</span>*<span style="color: #000099; font-weight: bold;">\+</span>=<span style="color: #000099; font-weight: bold;">\W</span>*&quot;</span>+moduleprops.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'file'</span><span style="color: black;">&#41;</span>+r<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\.</span>o&quot;</span>,cont<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>m<span style="color: black;">&#41;</span>:wantin.<span style="color: black;">append</span><span style="color: black;">&#40;</span>m.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'cfgname'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:stderr.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Could not determine config name for '</span> + modname + <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">else</span>:
		stderr.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Could not parse modinfo for '</span> + modname + <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>options.<span style="color: black;">cfgfile</span> <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">'-'</span><span style="color: black;">&#41;</span>: 
	f=<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>options.<span style="color: black;">cfgfile</span>, <span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	lines=f.<span style="color: black;">readlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
	lines=stdin.<span style="color: black;">readlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
confparse = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\W</span>*(?P&lt;iscomment&gt;#?)<span style="color: #000099; font-weight: bold;">\W</span>*(?P&lt;cfgname&gt;CONFIG_[A-Z0-9_]*)<span style="color: #000099; font-weight: bold;">\W</span>*=?<span style="color: #000099; font-weight: bold;">\W</span>*(?P&lt;answer&gt;[nmy]?)&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> lines:
	matches = <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span>confparse,line<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>matches <span style="color: #ff7700;font-weight:bold;">and</span> matches.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'cfgname'</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> wantin<span style="color: black;">&#41;</span>: 
		of.<span style="color: black;">write</span><span style="color: black;">&#40;</span>matches.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'cfgname'</span><span style="color: black;">&#41;</span>+<span style="color: #483d8b;">'=y<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">else</span>: 
		<span style="color: #808080; font-style: italic;">#if(matches and matches.group('answer') == 'm'):of.write(matches.group('cfgname')+'=n\n');</span>
		of.<span style="color: black;">write</span><span style="color: black;">&#40;</span>line<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span></pre></td></tr></table></div>

<hr /><a href='http://andreas.goelzer.de/download/customconfig.py'><img src="http://andreas.goelzer.de/wp-content/plugins/kfile/fileicons/file-source-alt.png" width="16" height="16" alt="filetype" class="icon16" /> customconfig.py</a> (3.41 kiB, 2009-05-19)<br/><br />
<hr />To use the script, call it for example like <code>./customconfig.py -i /boot/config-2.6.24-21-generic -o .config</code> if it is located in a linux kernel directory. Then modify the resulting config to suit your needs with <code>make menuconfig</code>, and then you can proceed to compile your kernel.</p>
]]></content:encoded>
			<wfw:commentRss>http://andreas.goelzer.de/kernel-config-based-on-lsmod-output/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)

Served from: andreas.goelzer.de @ 2010-09-09 08:08:22 -->