-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathch04.xml
615 lines (535 loc) · 20.3 KB
/
ch04.xml
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<chapter id="LKN-chapter-building">
<title>
Configuring and Building
</title>
<para>
Now that you have
downloaded the source for your selected kernel version and installed it into a local directory, it is time to build the
code. The first step is to configure the kernel with
the appropriate options; the kernel can then be compiled. Both tasks
are done through the standard <command>make</command> utility.
</para>
<sect1>
<title>Creating a configuration</title>
<para>
The kernel configuration is kept in a file called
<filename>.config</filename> in the top directory of the kernel source
tree. If you have just expanded the kernel source code, there will be
no <filename>.config</filename> file, so it needs
to be created. It can be created from scratch, created by basing it
on the "default configuration," taken from a running kernel version,
or taken
from a distribution kernel release. We will cover the first two methods
here, and the last two methods in <xref linkend="LKN-custom" />.
</para>
<sect2>
<title>Configuring from scratch</title>
<para>
<!--
AO: So unless you start with a default kernel built by somebody
else, you can't use gconfig or xconfig?
gkh: No, you need a configuration file from somewhere for xconfig and
gconfig to be useful, otherwise it's just a mess.
-->
The most basic method of configuring a kernel is to use the <command>make
config</command> method:
<screen>
$ <userinput>cd linux-2.6.17.10</userinput>
$ <userinput>make config</userinput>
make config
scripts/kconfig/conf arch/i386/Kconfig
*
* Linux Kernel Configuration
*
*
* Code maturity level options
*
Prompt for development and/or incomplete code/drivers (EXPERIMENTAL) [Y/n/?] <userinput>Y</userinput>
*
* General setup
*
Local version - append to kernel release (LOCALVERSION) [] <userinput> </userinput>
Automatically append version information to the version string (LOCALVERSION_AUTO) [Y/n/?] <userinput>Y</userinput>
...
</screen>
The kernel configuration program will step through every configuration
option and ask you if you wish to enable this option or
not. Typically, your choices for each option
are shown in the format <literal>[Y/m/n/?]</literal> The capitalized
letter is the default, and can be selected by just pressing the Enter
key. The four choices are:
</para>
<variablelist>
<varlistentry>
<term>
<literal>y</literal>
</term>
<listitem>
<para>
Build directly into the kernel.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>n</literal>
</term>
<listitem>
<para>
Leave entirely out of the kernel.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>m</literal>
</term>
<listitem>
<para>
Build as a module, to be loaded if needed.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>?</literal>
</term>
<listitem>
<para>
Print a brief descriptive message and repeat the prompt.
</para>
</listitem>
</varlistentry>
</variablelist>
<para>
The kernel contains almost two thousand different configuration options, so
being asked for every individual one will take a very long time.
Luckily, there is an easier way to configure a kernel: base the
configuration on a pre-built configuration.
</para>
</sect2>
<sect2>
<title>Default configuration options</title>
<para>
Every kernel version comes with a "default" kernel configuration. This
configuration is loosely based on the defaults that the kernel maintainer
of that architecture feels are the best options to be used. In some cases,
it is merely the configuration that is used by the kernel maintainer
himself for his personal machines. This is true for the i386
architecture, where the default kernel configuration matches closely what
Linus Torvalds uses for his main development machine.
</para>
<para>
To create this default configuration, do the following:
<screen>
$ <userinput>cd linux-2.6.17.10</userinput>
$ <userinput>make defconfig</userinput>
</screen>
A huge number of configuration options will scroll quickly by the screen,
and a <filename>.config</filename> file will be written out and placed in
the kernel directory. The kernel is now successfully configured, but it
should be customised to your machine in order to make sure it will operate
correctly.
</para>
</sect2>
</sect1>
<sect1>
<title>Modifying the configuration</title>
<para>
Now that we have a basic configuration file created, it should be modified
to support the hardware you have present in the system. For details on how
to find out which configuration options you need to select to achieve this,
please see <xref linkend="LKN-custom" />. Here we will show you how to
select the options you wish to change.
</para>
<para>
There are three different interactive kernel configuration tools: a
terminal-based one called <literal>menuconfig</literal>, a GTK+-based
graphical one called <literal>gconfig</literal>, and a QT-based graphical
one called <literal>xconfig</literal>.
</para>
<sect2>
<title>Console configuration method</title>
<para>
The <literal>menuconfig</literal> way of configuring a kernel is a console-based program that offers a way to move around the kernel configuration
using the arrow keys on the keyboard. To start up this configuration mode,
enter:
<screen>
$ <userinput>make menuconfig</userinput>
</screen>
You will be shown a screen much like <xref linkend="menuconfig_1" />.
</para>
<figure id="menuconfig_1">
<title>Initial menuconfig screen</title>
<graphic fileref="images/menuconfig_1.png" scalefit="1"/>
</figure>
<para>
The instructions for navigating through the program, and the
meanings of the different characters, are shown at the top of
the screen. The rest of the screen containing the different kernel
configuration options.
</para>
<para>
The kernel configuration is divided up into sections. Each section
contains options that correspond to a specific topic. Within those
sections can be sub-sections for various specialized topics. As an
example, all kernel device drivers can be found under the main menu option
<literal>Device Drivers</literal>. To enter that menu, move the arrow
key down nine times until the line
<literal>Device Drivers ---></literal> is highlighted, as shown in
<xref linkend="menuconfig_2" />.
<figure id="menuconfig_2">
<title>Device Drivers option selected</title>
<graphic fileref="images/menuconfig_2.png" scalefit="1"/>
</figure>
Then press the Enter key. It will move you into the
<literal>Device Drivers</literal> submenu and show it as illustrated in
<xref linkend="menuconfig_3" />.
<figure id="menuconfig_3">
<title>Device Drivers submenu</title>
<graphic fileref="images/menuconfig_3.png" scalefit="1"/>
</figure>
</para>
<para>
You can continue to move down through the menu hierarchy the same way.
To see the <literal>Generic Driver Options</literal> submenu, press
Enter again, and you will see the three
options shown in <xref linkend="menuconfig_4" />.
<figure id="menuconfig_4">
<title>Generic Driver Options submenu</title>
<graphic fileref="images/menuconfig_4.png" scalefit="1"/>
</figure>
The first two options have a <literal>[*]</literal> mark by them. That
means that this option is selected (by virtue of the <literal>*</literal>
being in the middle of the <literal>[ ]</literal> characters), and that this
option is a yes-or-no option. The third option has a
<literal>< ></literal> marking, showing that this option can be
built into the kernel (<literal>Y</literal>), built as a module
(<literal>M</literal>), or left out altogether (<literal>N</literal>).
</para>
<para>
If the option is selected with <literal>Y</literal>, the angle
brackets will contian a
<literal>*</literal> character. If it is selected as a
module with a <literal>M</literal>, they will contain an <literal>M</literal>
character. If it is disabled with <literal>N</literal>, they will
show only a blank space.
</para>
<para>
So, if we wish to change these three options to select only drivers that
do not need external firmware at compile time, disable the option to
prevent firmware from being built, and build the userspace firmware loader
as a module, we would press <literal>Y</literal> for the first
option, <literal>N</literal> for the second option, and
<literal>M</literal> for the third, making the screen look like <xref linkend="menuconfig_5" />.
<figure id="menuconfig_5">
<title>Generic Driver Options submenu changed</title>
<graphic fileref="images/menuconfig_5.png" scalefit="1"/>
</figure>
</para>
<para>
After you are done with your changes to this screen, press either the
Escape key or the right arrow followed by the
Enter key to leave this submenu. All of the different
kernel options can be explored in this manner.
</para>
<para>
When you are finished making all of the changes you wish to make to the
kernel configuration, exit the program by pressing the
Escape key on the main menu. You will be shown the screen in <xref linkend="menuconfig_6" />, asking whether you wish to save your changed kernel
configuration.
<figure id="menuconfig_6">
<title>Saving kernel options</title>
<graphic fileref="images/menuconfig_6.png" scalefit="1"/>
</figure>
Press Enter to save the configuration, or if you wish to
discard any changes made, press the right arrow to move to the
<literal><No></literal> selection and then press
Enter.
</para>
</sect2>
<sect2>
<title>Graphical configuration methods</title>
<para>
The <literal>gconfig</literal> and <literal>xconfig</literal> methods of
configuring a kernel use a graphical program to allow you to modify the
kernel configuration. The two methods are almost identical, the only
difference being the different graphical toolkit with which they are
written. <literal>gconfig</literal> is written using the GTK+ toolkit
and has a two-pane screen looking like <xref linkend="gconfig_1" />.
<figure id="gconfig_1">
<title>make gconfig screen</title>
<graphic fileref="images/gconfig_1.png" scalefit="1"/>
</figure>
The <literal>xconfig</literal> method is written using the QT toolkit
and has a three-pane screen looking like <xref linkend="xconfig_1" />.
<figure id="xconfig_1">
<title>make xconfig screen</title>
<graphic fileref="images/xconfig_1.png" scalefit="1"/>
</figure>
</para>
<para>
Use the mouse to navigate the submenus and select options. For
instance, you can use it in <xref linkend="xconfig_1" /> to select the <literal>Generic Driver Options</literal>
submenu of the <literal>Device Drivers</literal> menu. This will change
the <literal>xconfig</literal> screen to look like <xref linkend="xconfig_2" />.
<figure id="xconfig_2">
<title>make xconfig Generic Driver Options</title>
<graphic fileref="images/xconfig_2.png" scalefit="1"/>
</figure>
<!--
AO: I changed the order because only xconfig lets you directly
select the Generic Driver Options option.
gkh: fine.
-->
The corresponding <literal>gconfig</literal> screen is <xref linkend="gconfig_2" />.
<figure id="gconfig_2">
<title>make gconfig Generic Driver Options</title>
<graphic fileref="images/gconfig_2.png" scalefit="1"/>
</figure>
</para>
<para>
Changing this submenu to disable the second option and make the third
option be built as a module causes the screens to look like <xref
linkend="xconfig_3" /> and <xref linkend="gconfig_3" />.
</para>
<figure id="xconfig_3">
<title>make xconfig Generic Driver Options changed</title>
<graphic fileref="images/xconfig_3.png" scalefit="1"/>
</figure>
<figure id="gconfig_3">
<title>make gconfig Generic Driver Options changed</title>
<graphic fileref="images/gconfig_3.png" scalefit="1"/>
</figure>
<para>
Please note that in the <literal>gconfig</literal> method,
a checked box signifies that the option will be built
into the kernel, whereas a line though the box means the option will be built
as a module.
In the <literal>xconfig</literal> method, an option built as a module
will be shown with a dot in the box.
</para>
<para>
Both of these methods will prompt you to save your changed configuration
when exiting the program, and offer the option to write that
configuration out to a different file. In that way you can create
multiple, differing configurations.
</para>
</sect2>
</sect1>
<sect1>
<title>Building the kernel</title>
<para>
Now that you have created a kernel configuration that you wish to use,
you need to build the
kernel. This is as simple as entering a one-word command:
<screen>
$ <userinput>make</userinput>
CHK include/linux/version.h
UPD include/linux/version.h
SYMLINK include/asm -> include/asm-i386
SPLIT include/linux/autoconf.h -> include/config/*
CC arch/i386/kernel/asm-offsets.s
GEN include/asm-i386/asm-offsets.h
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
HOSTLD scripts/mod/modpost
HOSTCC scripts/kallsyms
HOSTCC scripts/conmakehash
HOSTCC scripts/bin2c
CC init/main.o
CHK include/linux/compile.h
UPD include/linux/compile.h
CC init/version.o
CC init/do_mounts.o
...
</screen>
Running <command>make</command> will cause the kernel build system to use
the configuration you have selected to build a kernel and all modules needed to support that configuration.
<footnote>
<para>
Older kernel versions prior to the 2.6 release required the additional step
of <command>make modules</command> to build all needed kernel modules.
That is no longer required.
</para>
</footnote>
While the kernel is building, <command>make</command> will display the individual file names of
what is currently happening, along with any build warnings or errors.
</para>
<para>
If the kernel build finished without any errors, you have successfully
created a kernel image. However, it needs to be installed properly
before you try to boot from it. See <xref linkend="LKN-installing" /> for how to do this.
</para>
<para>
It is very unusual to get any build errors when building a released kernel
version. If you do, please report them to the Linux kernel developers so
they can be fixed.
</para>
</sect1>
<sect1>
<title>Advanced building options</title>
<para>
The kernel build system allows you to do many more things than just build
the full kernel and modules. <xref linkend="LKN-build-command-line" />
includes the full list of options that the kernel build system provides. In
this section, we will discuss some of these advanced build options. To see
a full description of how to use other advanced build options, refer to the
in-kernel documentation on the build system, which can be found in the
<filename>Documentation/kbuild/</filename> directory of the sources.
</para>
<sect2>
<title>Building faster on multiprocessor machines</title>
<para>
The kernel build system works very well as a task that can be split up into
little pieces and given to different processors. By doing this, you can
use the full power of a multiprocessor machine and reduce the kernel build
time considerably.
</para>
<para>
To build the kernel in a multithreaded way, use the <option>-j</option>
option to the <command>make</command> program. It is best to give a number
to the <option>-j</option> option that corresponds to twice the number of
processors in the system. So, for a machine with 2 processors present,
use:
<screen>
$ <userinput>make -j4</userinput>
</screen>
and for a machine with four processors, use:
<screen>
$ <userinput>make -j8</userinput>
</screen>
If you do not pass a numerical value to the <option>-j</option> option
<screen>
$ <userinput>make -j</userinput>
</screen>
the build
system will create a new thread for every subdirectory in the kernel tree,
which can easily cause your machine to become unresponsive and take a much
longer time to complete the build. Because of this, it is
recommended that you always pass a number to the <option>-j</option> option.
</para>
</sect2>
<sect2>
<title>Building only a portion of the kernel</title>
<para>
When doing kernel development, sometimes you wish to build only a specific
subdirectory or a single file within the whole kernel tree. The kernel
build system allows you to easily do this. To selectively build a specific
directory, specify it on the build command line. For example, to
build the files in the <filename>drivers/usb/serial</filename> directory,
enter:
<screen>
$ <userinput>make drivers/usb/serial</userinput>
</screen>
Using this syntax, however, will not build the final module images in that
directory. To do that, you can use the <literal>M=</literal>
argument:
<screen>
$ <userinput>make M=drivers/usb/serial</userinput>
</screen>
which will build all the needed files in that directory and link the final
module images.
</para>
<para>
When you build a single directory in one of the ways shown, the final kernel image is not
relinked together. Therefore, any changes that were made to the subdirectories will
not affect the final kernel image, which is probably not what you desire.
Execute a final:
<screen>
$ <userinput>make</userinput>
</screen>
to have the build system check all changed object files and do the final
kernel image link properly.
</para>
<para>
<!--
AO: Does the following cause a final link if the file specified is
built-in, or only if the file is a module?
gkh: as you are asking for the final linked module, it will do that. If you
ask for the built-in linked file, it will create that too.
-->
To build only a specific file in the kernel tree, just pass it as the
argument to <command>make</command>. For example, if you wish to build only
the <filename>drivers/usb/serial/visor.ko</filename> kernel module, enter:
<screen>
$ <userinput>make drivers/usb/serial/visor.ko</userinput>
</screen>
The build system will build all needed files for the
<filename>visor.ko</filename> kernel module, and do the final link to
create the module.
</para>
</sect2>
<sect2>
<title>Source in one place, output in another</title>
<para>
Sometimes it is easier to have the source code for the kernel tree in a
read-only location (such as on a CD-ROM, or in a source code control system),
and place the output of the kernel build elsewhere, so that
you do not disturb the original source tree. The kernel build system
handles this easily, by requiring only the single argument
<literal>O=</literal> to tell it where to place the output of the build.
For example, if the kernel source is located on a CD-ROM mounted on
<filename>/mnt/cdrom/</filename> and you wish to place the built files in
your local directory, enter:
<screen>
$ <userinput>cd /mnt/cdrom/linux-2.6.17.11/</userinput>
$ <userinput>make O=~/linux/linux-2.6.17.11</userinput>
</screen>
All of the build files will be created in the
<filename>~/linux/linux-2.6.17.11/</filename> directory. Please note that
this <literal>O=</literal> option should also be passed to the
configuration options of the build so that the configuration is correctly
placed in the output directory and not in the directory containing the
source code.
</para>
</sect2>
<sect2>
<title>Different architectures</title>
<para>
It is very useful to build the kernel in a cross-compiled manner to allow a
more powerful machine to build a kernel for a smaller embedded system, or
just to check a build for a different architecture
to ensure that a change to the source code did not break something
unexpected. The kernel build system allows you to specify a different
architecture from the current system with the
<literal>ARCH=</literal> argument. The build system also allows you to
specify the specific compiler that you wish to use for the build by
using the <literal>CC=</literal> argument or a cross-compile toolchain with
the <literal>CROSS_COMPILE</literal> argument.
</para>
<para>
For example, to get the default kernel configuration of the x86_64
architecture, you would enter:
<screen>
$ <userinput>make ARCH=x86_64 defconfig</userinput>
</screen>
To build the whole kernel with an ARM toolchain located in
<filename>/usr/local/bin/</filename> you would enter:
<screen>
$ <userinput>make ARCH=arm CROSS_COMPILE=/usr/local/bin/arm-linux-</userinput>
</screen>
</para>
<para>
It is useful even for a non-cross-compiled kernel to change what the build
system uses for the compiler. Examples of this are using the
<command>distcc</command> or <command>ccache</command> programs, both of
which help greatly reduce the time it takes to build a kernel. To use the
<command>ccache</command> program as part of the build system, enter:
<screen>
$ <userinput>make CC="ccache gcc"</userinput>
</screen>
To use both <command>distcc</command> and <command>ccache</command>
together, enter:
<screen>
$ <userinput>make CC="ccache distcc"</userinput>
</screen>
</para>
</sect2>
</sect1>
</chapter>
<!-- vim: set ai tw=72 : -->