📄 kernelconfig-building.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta name="generator" content="HTML Tidy, see www.w3.org" /><title>Building and Installing a Custom Kernel</title><meta name="GENERATOR" content="Modular DocBook HTML Stylesheet Version 1.7" /><link rel="HOME" title="FreeBSD Handbook" href="index.html" /><link rel="UP" title="Configuring the FreeBSD Kernel" href="kernelconfig.html" /><link rel="PREVIOUS" title="Why Build a Custom Kernel?"href="kernelconfig-custom-kernel.html" /><link rel="NEXT" title="The Configuration File" href="kernelconfig-config.html" /><link rel="STYLESHEET" type="text/css" href="docbook.css" /></head><body class="SECT1" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#840084"alink="#0000FF"><div class="NAVHEADER"><table summary="Header navigation table" width="100%" border="0" cellpadding="0"cellspacing="0"><tr><th colspan="3" align="center">FreeBSD Handbook</th></tr><tr><td width="10%" align="left" valign="bottom"><a href="kernelconfig-custom-kernel.html"accesskey="P">Prev</a></td><td width="80%" align="center" valign="bottom">Chapter 8 Configuring the FreeBSDKernel</td><td width="10%" align="right" valign="bottom"><a href="kernelconfig-config.html"accesskey="N">Next</a></td></tr></table><hr align="LEFT" width="100%" /></div><div class="SECT1"><h1 class="SECT1"><a id="KERNELCONFIG-BUILDING" name="KERNELCONFIG-BUILDING">8.3 Buildingand Installing a Custom Kernel</a></h1><p>First, let us take a quick tour of the kernel build directory. All directoriesmentioned will be relative to the main <tt class="FILENAME">/usr/src/sys</tt> directory,which is also accessible through the path name <tt class="FILENAME">/sys</tt>. There area number of subdirectories here representing different parts of the kernel, but the mostimportant for our purposes are <tt class="FILENAME"><varclass="REPLACEABLE">arch</var>/conf</tt>, where you will edit your custom kernelconfiguration, and <tt class="FILENAME">compile</tt>, which is the staging area whereyour kernel will be built. <var class="REPLACEABLE">arch</var> represents one of <ttclass="FILENAME">i386</tt>, <tt class="FILENAME">alpha</tt>, <ttclass="FILENAME">amd64</tt>, <tt class="FILENAME">ia64</tt>, <ttclass="FILENAME">powerpc</tt>, <tt class="FILENAME">sparc64</tt>, or <ttclass="FILENAME">pc98</tt> (an alternative development branch of PC hardware, popular inJapan). Everything inside a particular architecture's directory deals with thatarchitecture only; the rest of the code is machine independent code common to allplatforms to which FreeBSD could potentially be ported. Notice the logical organizationof the directory structure, with each supported device, file system, and option in itsown subdirectory. Versions of FreeBSD prior to 5.X support only the <ttclass="FILENAME">i386</tt>, <tt class="FILENAME">alpha</tt> and <ttclass="FILENAME">pc98</tt> architectures.</p><p>This chapter assumes that you are using the i386 architecture in the examples. If thisis not the case for your situation, make appropriate adjustments to the path names foryour system's architecture.</p><div class="NOTE"><blockquote class="NOTE"><p><b>Note:</b> If there is <span class="emphasis"><i class="EMPHASIS">not</i></span> a<tt class="FILENAME">/usr/src/sys</tt> directory on your system, then the kernel sourcehas not been installed. The easiest way to do this is by running <ttclass="COMMAND">/stand/sysinstall</tt> as <tt class="USERNAME">root</tt>, choosing <spanclass="GUIMENUITEM">Configure</span>, then <spanclass="GUIMENUITEM">Distributions</span>, then <span class="GUIMENUITEM">src</span>, then<span class="GUIMENUITEM">sys</span>. If you have an aversion to <bclass="APPLICATION">sysinstall</b> and you have access to an ``official'' FreeBSD CDROM,then you can also install the source from the command line:</p><pre class="SCREEN"><samp class="PROMPT">#</samp> <kbd class="USERINPUT">mount /cdrom</kbd><samp class="PROMPT">#</samp> <kbd class="USERINPUT">mkdir -p /usr/src/sys</kbd><samp class="PROMPT">#</samp> <kbd class="USERINPUT">ln -s /usr/src/sys /sys</kbd><samp class="PROMPT">#</samp> <kbdclass="USERINPUT">cat /cdrom/src/ssys.[a-d]* | tar -xzvf -</kbd></pre></blockquote></div><p>Next, move to the <tt class="FILENAME"><var class="REPLACEABLE">arch</var>/conf</tt>directory and copy the <tt class="FILENAME">GENERIC</tt> configuration file to the nameyou want to give your kernel. For example:</p><pre class="SCREEN"><samp class="PROMPT">#</samp> <kbd class="USERINPUT">cd /usr/src/sys/<varclass="REPLACEABLE">i386</var>/conf</kbd><samp class="PROMPT">#</samp> <kbd class="USERINPUT">cp GENERIC MYKERNEL</kbd></pre><p>Traditionally, this name is in all capital letters and, if you are maintainingmultiple FreeBSD machines with different hardware, it is a good idea to name it afteryour machine's hostname. We will call it <tt class="FILENAME">MYKERNEL</tt> for thepurpose of this example.</p><div class="TIP"><blockquote class="TIP"><p><b>Tip:</b> Storing your kernel config file directly under <ttclass="FILENAME">/usr/src</tt> can be a bad idea. If you are experiencing problems it canbe tempting to just delete <tt class="FILENAME">/usr/src</tt> and start again. Afterdoing this, it usually only takes a few seconds for you to realize that you have deletedyour custom kernel config file. Also, do not edit <tt class="FILENAME">GENERIC</tt>directly, as it may get overwritten the next time you <a href="cutting-edge.html">updateyour source tree</a>, and your kernel modifications will be lost.</p><p>You might want to keep your kernel config file elsewhere, and then create a symboliclink to the file in the <tt class="FILENAME"><var class="REPLACEABLE">i386</var></tt>directory.</p><p>For example:</p><pre class="SCREEN"><samp class="PROMPT">#</samp> <kbd class="USERINPUT">cd /usr/src/sys/<varclass="REPLACEABLE">i386</var>/conf</kbd><samp class="PROMPT">#</samp> <kbd class="USERINPUT">mkdir /root/kernels</kbd><samp class="PROMPT">#</samp> <kbd class="USERINPUT">cp GENERIC /root/kernels/<varclass="REPLACEABLE">MYKERNEL</var></kbd> <samp class="PROMPT">#</samp> <kbd class="USERINPUT">ln -s /root/kernels/<varclass="REPLACEABLE">MYKERNEL</var></kbd></pre></blockquote></div><p>Now, edit <tt class="FILENAME">MYKERNEL</tt> with your favorite text editor. If youare just starting out, the only editor available will probably be <bclass="APPLICATION">vi</b>, which is too complex to explain here, but is covered well inmany books in the <a href="bibliography.html">bibliography</a>. However, FreeBSD doesoffer an easier editor called <b class="APPLICATION">ee</b> which, if you are a beginner,should be your editor of choice. Feel free to change the comment lines at the top toreflect your configuration or the changes you have made to differentiate it from <ttclass="FILENAME">GENERIC</tt>.</p><p>If you have built a kernel under <span class="TRADEMARK">SunOS</span>™ or someother BSD operating system, much of this file will be very familiar to you. If you arecoming from some other operating system such as DOS, on the other hand, the <ttclass="FILENAME">GENERIC</tt> configuration file might seem overwhelming to you, sofollow the descriptions in the <a href="kernelconfig-config.html">Configuration File</a>section slowly and carefully.</p><div class="NOTE"><blockquote class="NOTE"><p><b>Note:</b> If you <a href="cutting-edge.html">sync your source tree</a> with thelatest sources of the FreeBSD project, be sure to always check the file <ttclass="FILENAME">/usr/src/UPDATING</tt> before you perform any update steps. This filedescribes any important issues or areas requiring special attention within the updatedsource code. <tt class="FILENAME">/usr/src/UPDATING</tt> always matches your version ofthe FreeBSD source, and is therefore more up to date with new information than thishandbook.</p></blockquote></div><p>You must now compile the source code for the kernel. There are two procedures you canuse to do this, and the one you will use depends on why you are rebuilding the kernel andthe version of FreeBSD that you are running.</p><ul><li><p>If you have installed <span class="emphasis"><i class="EMPHASIS">only</i></span> thekernel source code, use procedure 1.</p></li><li><p>If you are running a FreeBSD version prior to 4.0, and you are <spanclass="emphasis"><i class="EMPHASIS">not</i></span> upgrading to FreeBSD 4.0 orhigher using the <tt class="MAKETARGET">make world</tt> procedure, use procedure 1.</p></li><li><p>If you are building a new kernel without updating the source code (perhaps just to adda new option, such as <var class="LITERAL">IPFIREWALL</var>) you can use eitherprocedure.</p></li><li><p>If you are rebuilding the kernel as part of a <tt class="MAKETARGET">make world</tt>process, use procedure 2.</p></li></ul><p>If you have <span class="emphasis"><i class="EMPHASIS">not</i></span> upgraded yoursource tree in any way since the last time you successfully completed a <ttclass="MAKETARGET">buildworld</tt>-<tt class="MAKETARGET">installworld</tt> cycle (youhave not run <b class="APPLICATION">CVSup</b>, <b class="APPLICATION">CTM</b>, or used <bclass="APPLICATION">anoncvs</b>), then it is safe to use the <ttclass="COMMAND">config</tt>, <tt class="MAKETARGET">make depend</tt>, <ttclass="COMMAND">make</tt>, <tt class="MAKETARGET">make install</tt> sequence.</p><div class="PROCEDURE"><p><b>Procedure 1. Building a Kernel the ``Traditional'' Way</b></p><ol type="1"><li><p>Run <a href="http://www.FreeBSD.org/cgi/man.cgi?query=config&sektion=8"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">config</span>(8)</span></a> to generatethe kernel source code.</p><pre class="SCREEN"><samp class="PROMPT">#</samp> <kbd class="USERINPUT">/usr/sbin/config <varclass="REPLACEABLE">MYKERNEL</var></kbd></pre></li><li><p>Change into the build directory. <ahref="http://www.FreeBSD.org/cgi/man.cgi?query=config&sektion=8"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">config</span>(8)</span></a> will printthe name of this directory after being run as above.</p><pre class="SCREEN"><samp class="PROMPT">#</samp> <kbd class="USERINPUT">cd ../compile/<varclass="REPLACEABLE">MYKERNEL</var></kbd></pre><p>For FreeBSD versions prior to 5.0, use the following form instead:</p><pre class="SCREEN"><samp class="PROMPT">#</samp> <kbd class="USERINPUT">cd ../../compile/<varclass="REPLACEABLE">MYKERNEL</var></kbd></pre></li><li><p>Compile the kernel.</p><pre class="SCREEN"><samp class="PROMPT">#</samp> <kbd class="USERINPUT">make depend</kbd><samp class="PROMPT">#</samp> <kbd class="USERINPUT">make</kbd></pre></li><li><p>Install the new kernel.</p><pre class="SCREEN"><samp class="PROMPT">#</samp> <kbd class="USERINPUT">make install</kbd></pre></li></ol></div><div class="PROCEDURE"><p><b>Procedure 2. Building a Kernel the ``New'' Way</b></p><ol type="1"><li><p>Change to the <tt class="FILENAME">/usr/src</tt> directory.</p><pre class="SCREEN"><samp class="PROMPT">#</samp> <kbd class="USERINPUT">cd /usr/src</kbd></pre></li><li><p>Compile the kernel.</p><pre class="SCREEN"><samp class="PROMPT">#</samp> <kbd class="USERINPUT">make buildkernel KERNCONF=<varclass="REPLACEABLE">MYKERNEL</var></kbd></pre></li><li><p>Install the new kernel.</p><pre class="SCREEN"><samp class="PROMPT">#</samp> <kbd class="USERINPUT">make installkernel KERNCONF=<varclass="REPLACEABLE">MYKERNEL</var></kbd></pre></li></ol></div><div class="NOTE"><blockquote class="NOTE"><p><b>Note:</b> In FreeBSD 4.2 and older you must replace <varclass="LITERAL">KERNCONF=</var> with <var class="LITERAL">KERNEL=</var>. 4.2-STABLE thatwas fetched before Feb 2nd, 2001 does not recognize <varclass="LITERAL">KERNCONF=</var>.</p></blockquote></div><p>The new kernel will be copied to the <tt class="FILENAME">/boot/kernel</tt> directoryas <tt class="FILENAME">/boot/kernel/kernel</tt> and the old kernel will be moved to <ttclass="FILENAME">/boot/kernel.old/kernel</tt>. Now, shutdown the system and reboot to useyour new kernel. If something goes wrong, there are some <ahref="kernelconfig-trouble.html">troubleshooting</a> instructions at the end of thischapter that you may find useful. Be sure to read the section which explains how torecover in case your new kernel <ahref="kernelconfig-trouble.html#KERNELCONFIG-NOBOOT">does not boot</a>.</p><div class="NOTE"><blockquote class="NOTE"><p><b>Note:</b> In FreeBSD 4.X and earlier, kernels are installed in <ttclass="FILENAME">/kernel</tt>, modules in <tt class="FILENAME">/modules</tt>, and oldkernels are backed up in <tt class="FILENAME">/kernel.old</tt>. Other files relating tothe boot process, such as the boot <ahref="http://www.FreeBSD.org/cgi/man.cgi?query=loader&sektion=8"><spanclass="CITEREFENTRY"><span class="REFENTRYTITLE">loader</span>(8)</span></a> andconfiguration are stored in <tt class="FILENAME">/boot</tt>. Third party or custommodules can be placed in <tt class="FILENAME">/modules</tt>, although users should beaware that keeping modules in sync with the compiled kernel is very important. Modulesnot intended to run with the compiled kernel may result in instability orincorrectness.</p></blockquote></div><div class="NOTE"><blockquote class="NOTE"><p><b>Note:</b> If you have added any new devices (such as sound cards) and you arerunning FreeBSD 4.X or previous versions, you may have to add some device nodes toyour <tt class="FILENAME">/dev</tt> directory before you can use them. For moreinformation, take a look at <a href="kernelconfig-nodes.html">Making Device Nodes</a>section later on in this chapter.</p></blockquote></div></div><div class="NAVFOOTER"><hr align="LEFT" width="100%" /><table summary="Footer navigation table" width="100%" border="0" cellpadding="0"cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="kernelconfig-custom-kernel.html"accesskey="P">Prev</a></td><td width="34%" align="center" valign="top"><a href="index.html"accesskey="H">Home</a></td><td width="33%" align="right" valign="top"><a href="kernelconfig-config.html"accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Why Build a Custom Kernel?</td><td width="34%" align="center" valign="top"><a href="kernelconfig.html"accesskey="U">Up</a></td><td width="33%" align="right" valign="top">The Configuration File</td></tr></table></div><p align="center"><small>This, and other documents, can be downloaded from <ahref="ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/">ftp://ftp.FreeBSD.org/pub/FreeBSD/doc/</a>.</small></p><p align="center"><small>For questions about FreeBSD, read the <ahref="http://www.FreeBSD.org/docs.html">documentation</a> before contacting <<ahref="mailto:questions@FreeBSD.org">questions@FreeBSD.org</a>>.<br />For questions about this documentation, e-mail <<ahref="mailto:doc@FreeBSD.org">doc@FreeBSD.org</a>>.</small></p></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -