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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
<html>
<head>
<link href="../lg.css" rel="stylesheet" type="text/css" media="screen, projection" />
<link rel="shortcut icon" href="../favicon.ico" />
<title>Compiling the Linux Kernel LG #111</title>
<style type="text/css" media="screen, projection">
<!--
-->
</style>
<link rel="alternate" type="application/rss+xml" title="LG RSS" href="lg.rss" />
<link rel="alternate" type="application/rdf+xml" title="LG RDF" href="lg.rdf" />
<link rel="alternate" type="application/atom+xml" title="LG Atom" href="lg.atom.xml" />
</head>
<body>
<img src="../gx/2003/newlogo-blank-200-gold2.jpg" id="logo" alt="Linux Gazette"/>
<p id="fun">...making Linux just a little more fun!</p>
<div class="content articlecontent">
<div id="previousnexttop">
<A HREF="engel.html" ><-- prev</A> | <A HREF="okopnik.html" >next --></A>
</div>
<h1>Compiling the Linux Kernel</h1>
<p id="by"><b>By <A HREF="../authors/krishnakumar.html">R. Krishnakumar</A></b></p>
<p>
<hr>
<em>This article will serve as a primer to people who are new to the
world of Linux hacking, and are attempting to compile the Linux kernel
from source. The various steps from downloading the kernel source to
booting from the new kernel image are explained. Also given are tips
on cleaning up the source code, doing verbose compilation etc. </em>
<hr>
<h3>1. Downloading the kernel source code</h3>
<p>In order to compile a new kernel we have to download the source code of
the Linux kernel. We can download the source from <a
href="http://www.kernel.org">www.kernel.org</a>. Here we
can find all versions of the Linux kernel source code. Let's take an
example. Suppose we want to compile the 2.6.9 version of the linux kernel.
We have to download the 2.6.9 source code from:
<p>
<a href="http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.9.tar.bz2">http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.9.tar.bz2</a>
<p> It's better to download the bzipped version, as that will be more
compressed than its gzipped counterpart; hence will take less
time to download. A <tt>wget</tt> from the command line will look like:
<blockquote>
<pre>
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.9.tar.bz2
</pre>
</blockquote>
<p> Once we download the required kernel version source, we need to bunzip
and untar it. We can do the following:
<p>
<blockquote>
<pre>
tar xvjf linux-2.6.9.tar.bz2
</pre>
</blockquote>
<p> The 'x' option is to denote the untarring (e'x'traction), 'v' for
verbose, 'j' for specifying that we need to bunzip the file before
untarring and 'f' for stating the name of the input file.
<p> The file will untar into the directory linux-2.6.9. Once it's
untarred '<tt>cd</tt>' to <tt>linux-2.6.9</tt>.
<h3>2. Configuring the kernel</h3>
We have to configure the kernel before we start compiling it.
During the configuration phase, we will select the components which
we want to be part of the kernel. For example: suppose we are using
the ext3 filesystem. Then we need to select the ext3 filesystem support
while configuring the kernel. Typically we have to run a
<blockquote>
<pre>
make menuconfig
</pre>
</blockquote>
This will bring up the ncurses interface for configuring the kernel.
There are other options such as 'make xconfig' and 'make config'.
The former will bring up the configuration menu in graphical mode and
the latter in text mode.
<p> Once we select the different components we want for our kernel, we
can exit the configuration interface. We should select the option
to save the configuration from the configuration menu, before exiting.
<p> After we have configured the kernel as mentioned above, we can find
a file named '<tt>.config</tt>' in the top level directory of the source.
This file is the configuration file. It contains various options and
their states (whether they are selected or not). For example, if we choose
to have the PCI support in our kernel we can find an entry of the form:
<blockquote>
<pre>
CONFIG_PCI=y
</pre>
</blockquote>
in the <tt>.config</tt> file. Similarly, options which are selected as not
required will appear as not set. Suppose we have not selected
the XFS filesystem support in our kernel we will find the following
in the <tt>.config</tt>
<blockquote>
<pre>
# CONFIG_XFS_FS is not set
</pre>
</blockquote>
<p> A great feature of 2.6 kernels is that if we are running
<tt>make menuconfig</tt> (or
xconfig or config) for the first time, then the configuration menu we are
presented with is based on our current kernel configuration. In my case,
I have a Fedora Core 1 system. The kernel which I run is '2.4.22-1.2115.nptl'.
Hence when I run a 'make menuconfig' for the first time on the source then the
configuration menu presented will contain the options as given in
'/boot/config-2.4.22-1.2115.nptl'.
<h3>3. Building Dependencies</h3>
This step is required in kernels prior to 2.6 series (here I am only
referring to the stable series kernels). For example if we are using a
2.4 kernel then we have to build the dependencies explicitly. We have
to run the following:
<blockquote>
<pre>
make dep
</pre>
</blockquote>
This will build the dependencies. But for a 2.6 kernel we can skip
this step. The dependencies are automatically created when making the
final image with a 2.6 kernel.
<h3>4. Creating the final image</h3>
We can build various types of kernel binary images.
We can build a plain kernel image, a gzipped compressed image
or a bzipped compressed image. Usual choice is the 'bzImage',
which is the linux kernel image in the bzipped compressed form.
We can create the bzImage by running
<blockquote>
<pre>
make bzImage
</pre>
</blockquote>
In 2.6 kernels this step will also resolve the dependencies and proceed
to create a bzImage image.
<p> After the compilation is over we can find the kernel image at the path
<tt>arch/i386/boot/bzImage</tt> in case of an image for a 386 based processor
(Pentium, AMD etc.).
<h3>5. Compiling and Installing the modules</h3>
In the configuring section if we have selected some components to be
built as kernel modules then we need to compile those modules.
To compile the modules we should run the command:
<blockquote>
<pre>
make modules
</pre>
</blockquote>
This command will compile the components (which are selected
for module compilation) to modules. In a 2.4 kernel the result
will be .o files of the corresponding components. But in a
2.6 kernel the output file will be a .ko module. For example
if we have given the option for the Network driver of Realtek
cards to be built as modules then after giving a 'make modules'
we can find in '<tt>driver/net/</tt>' a file named <tt>8139too.o</tt>
in the case of a 2.4 kernel and <tt>8139too.ko</tt> in the case of a
2.6 kernel.
<p> After we have compiled the modules, it's time now to install the modules.
To install the modules run:
<blockquote>
<pre>
make modules_install
</pre>
</blockquote>
as root. This will install the modules and other necessary files into the
<tt>/lib/modules/2.6.9</tt> directory.
<h3>6. Booting from the new kernel</h3>
Once we are done with the installation of modules, we
can go for an automatic installation procedure for the
kernel binary. We just have to run
<blockquote>
<pre>
make install
</pre>
</blockquote>
This will update the kernel image on to the <tt>/boot</tt> area, update
the configuration file of the bootloader (<tt>lilo.conf</tt> or
<tt>grub.conf</tt>) and then do the necessary actions to make the new
kernel bootable.
<p> After this we need to reboot the machine. When the machine boots
next time the boot menu will present us with the option to boot
from the new kernel we built. We choose that option and voila!!
boot into a kernel we built all by ourselves!
<h3>7. Manual installation of the kernel </h3>
In case '<tt>make install</tt>' does not work, or if we cannot perform an
automatic installation due to some other reason, we can go for a manual
installation of the kernel. For example, if we are using the grub boot
loader then we have to copy the bzImage into the boot partition and then
change the '<tt>/etc/grub.conf</tt>' to reflect the presence of the new
image. If we are having lilo boot loader then we have to copy the bzImage
to the boot location and then modify the <tt>lilo.conf</tt> and then run
the '<tt>lilo</tt>' command to make sure that next time we boot we will
have our new image as a choice to boot from. The following are the steps we
should perform as root user if we are using lilo boot loader: <blockquote>
<pre> cp -a arch/i386/boot/bzImage /boot/bzImage-2.6.9 </pre> </blockquote>
After this we add the following entry to /etc/lilo.conf
<blockquote>
<pre>
image=/boot/bzImage-2.6.9
label=2.6.9-kernel
root=your_root_disk
</pre>
</blockquote>
We should run lilo after this
<blockquote>
<pre>
lilo -v
</pre>
</blockquote>
We will reboot the machine after this. When we are prompted at
the lilo prompt enter '2.6.9-kernel' as the boot option and we
will be booting to the new custom built kernel.
<h3>8. Verbose compilation</h3>
We find that the compilation of the kernel is very quiet. Much less
information on what is getting compiled is shown on the screen while the
compilation proceeds.
<pre class="code">
#make bzImage
CHK include/linux/version.h
UPD include/linux/version.h
SPLIT include/linux/autoconf.h -> include/config/*
CC scripts/mod/empty.o
HOSTCC scripts/mod/mk_elfconfig
MKELF scripts/mod/elfconfig.h
HOSTCC scripts/mod/file2alias.o
HOSTCC scripts/mod/modpost.o
HOSTCC scripts/mod/sumversion.o
....
....
</pre>
If we need to know what commands are used for compilation,
then we need to give the verbose compilation option while compiling.
That is:
<blockquote>
<pre>
make bzImage V=1
</pre>
</blockquote>
This will output the commands which are executed while compiling.
Here is a snippet from the compilation output:
<pre class="code">
<..snip..>
make -f scripts/Makefile.build obj=init
gcc -Wp,-MD,init/.main.o.d -nostdinc -iwithprefix include -D__KERNEL__ -Iinclude -Wall
-Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -pipe -msoft-float
-mpreferred-stack-boundary=2 -march=i686 -Iinclude/asm-i386/mach-default -O2
-fomit-frame-pointer -DKBUILD_BASENAME=main -DKBUILD_MODNAME=main -c -o init/main.o
init/main.c
CHK include/linux/compile.h
UPD include/linux/compile.h
gcc -Wp,-MD,init/.version.o.d -nostdinc -iwithprefix include -D__KERNEL__ -Iinclude -Wall
-Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -pipe -msoft-float
-mpreferred-stack-boundary=2 -march=i686 -Iinclude/asm-i386/mach-default -O2
-fomit-frame-pointer -DKBUILD_BASENAME=version -DKBUILD_MODNAME=version -c -o
init/version.o init/version.c
<..snip..>
</pre>
<h3>9. Cleaning the kernel source</h3>
After we have initiated compilation once on the source if we want
to clean the object files and other temporary files then
we have to run the following:
<blockquote>
<pre>
make clean
</pre>
</blockquote>
This will remove most generated files but will keep the configuration
file.
<p> If we need an absolute cleaning, i.e. if we want to return the source to
the state in which it was before we started the compilation, then do a
<p>
<blockquote>
<pre>
make mrproper
</pre>
</blockquote>
This command will delete all generated files, the configuration file as
well as various backup files. This will in effect unwind all the changes
we made to the source. The source after this step will be as good as it
was just after the download and untar.
<h3>10. Conclusion</h3>
We have seen how to obtain the linux kernel source, how to configure it,
how to build the kernel image and modules, how to boot from the newly compiled
kernel and how to do a verbose compilation. Also we have seen how to clean up
the temporary files and configuration files which were created during the
compilation. The next step for a budding kernel hacker would be to modify the
kernel source and try experimenting with it.
</p>
<!-- *** BEGIN author bio *** -->
<P>
<P>
<!-- *** BEGIN bio *** -->
<hr>
<p>
<img align="left" alt="[BIO]" src="../gx/authors/krishnakumar.jpg" class="bio">
<em>
Krishnakumar loves to hack the Linux kernel. He works
for Hewlett-Packard and is a BTech from Govt. Engg. College
Thrissur.
</em>
<br clear="all">
<!-- *** END bio *** -->
<!-- *** END author bio *** -->
<div id="articlefooter">
<p>
Copyright © 2005, R. Krishnakumar. Released under the
<a href="http://linuxgazette.net/copying.html">Open Publication
license</a>. Linux Gazette is not produced, sponsored, or endorsed by
its prior host, SSC, Inc.
</p>
<p>
Published in Issue 111 of Linux Gazette, February 2005
</p>
</div>
<div id="previousnextbottom">
<A HREF="engel.html" ><-- prev</A> | <A HREF="okopnik.html" >next --></A>
</div>
</div>
<div id="navigation">
<a href="../index.html">Home</a>
<a href="../faq/index.html">FAQ</a>
<a href="../lg_index.html">Site Map</a>
<a href="../mirrors.html">Mirrors</a>
<a href="../mirrors.html">Translations</a>
<a href="../search.html">Search</a>
<a href="../archives.html">Archives</a>
<a href="../authors/index.html">Authors</a>
<a href="../contact.html">Contact Us</a>
</div>
<div id="breadcrumbs">
<a href="../index.html">Home</a> >
<a href="index.html">February 2005 (#111)</a> >
Article
</div>
<img src="../gx/2003/sit3-shine.7-2.gif" id="tux" alt="Tux"/>
</body>
</html>
|