<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
  <!ENTITY % general-entities SYSTEM "../general.ent">
  %general-entities;
]>

<sect1 id="ch-bootable-kernel" role="wrap">
  <?dbhtml filename="kernel.html"?>

  <sect1info condition="script">
    <productname>linux</productname>
    <productnumber>&linux-version;</productnumber>
    <address>&linux-url;</address>
  </sect1info>

  <title>Linux-&linux-version;</title>

  <indexterm zone="ch-bootable-kernel">
    <primary sortas="a-Linux">Linux</primary>
  </indexterm>

  <sect2 role="package">
    <title/>

    <para>Linux 软件包包含 Linux 内核。</para>

    <segmentedlist>
      <segtitle>&buildtime;</segtitle>
      <segtitle>&diskspace;</segtitle>

      <seglistitem>
        <seg>&linux-ch8-sbu;</seg>
        <seg>&linux-ch8-du;</seg>
      </seglistitem>
    </segmentedlist>

  </sect2>

  <sect2 role="installation">
    <title>安装内核</title>

    <!--para>Building the kernel involves a few steps&mdash;configuration,
    compilation, and installation. Read the <filename>README</filename> file
    in the kernel source tree for alternative methods to the way this book
    configures the kernel.</para-->
    <para>构建内核需要三步 —— 配置、编译、安装。
		阅读内核源代码树中的 <filename>README</filename> 文件，
		了解不同于本手册的内核配置方法。</para>

    <para>运行以下命令，准备编译内核：</para>

<screen><userinput remap="pre">make mrproper</userinput></screen>

    <!--para>This ensures that the kernel tree is absolutely clean. The
    kernel team recommends that this command be issued prior to each
    kernel compilation. Do not rely on the source tree being clean after
    un-tarring.</para-->
    <para>该命令确保内核源代码树绝对干净，
		内核开发组建议在每次编译内核前运行该命令。
		尽管内核源代码树在解压后应该是干净的，但这并不完全可靠。</para>

    <!-- Support for compiling a keymap into the kernel is deliberately removed -->

    <!--para>Configure the kernel via a menu-driven interface. For general
    information on kernel configuration see <ulink
    url="&hints-root;kernel-configuration.txt"/>.  BLFS has some information
    regarding particular kernel configuration requirements of packages outside
    of LFS at <ulink
    url="&blfs-book;longindex.html#kernel-config-index"/>.  Additional
    information about configuring and building the kernel can be found at
    <ulink url="http://www.kroah.com/lkn/"/> </para-->
    <para>下面通过菜单界面配置内核，阅读
		<ulink url="&hints-root;kernel-configuration.txt"/>
		了解关于内核配置的一般信息。
		BLFS 的某些软件包需要特定内核配置，阅读
		<ulink url="&blfs-book;longindex.html#kernel-config-index"/>
		了解它们。另外在 <ulink url="http://www.kroah.com/lkn"/>
		也有一些关于配置和构建内核的信息。</para>

    <note>
    
      <!--para>A good starting place for setting up the kernel configuration is to
      run <command>make defconfig</command>. This will set the base
      configuration to a good state that takes your current system architecture
      into account.</para-->
      <para>一个较好的初始内核配置可以通过运行
		  <command>make defconfig</command> 获得。
		  它会考虑您的当前系统体系结构，将基本内核配置设定到较好的状态。
	  </para>

      <!--para>Be sure to enable/disable/set the following features or the system might
      not work correctly or boot at all:</para-->
      <para>一定要按照以下列表启用/禁用/设定其中列出的内核特性，
		  否则系统可能不能正常工作，甚至根本无法引导：</para>

      <screen role="nodump" revision="sysv">
Device Drivers  ---&gt;
  Generic Driver Options  ---&gt;
   [ ] Support for uevent helper [CONFIG_UEVENT_HELPER]
   [*] Maintain a devtmpfs filesystem to mount at /dev [CONFIG_DEVTMPFS]

Kernel hacking  ---&gt;
       Choose kernel unwinder (Frame pointer unwinder)  ---&gt; [CONFIG_UNWINDER_FRAME_POINTER]</screen>

      <screen role="nodump" revision="systemd">
General setup -->
   [ ] Enable deprecated sysfs features to support old userspace tools [CONFIG_SYSFS_DEPRECATED]
   [ ] Enable deprecated sysfs features by default [CONFIG_SYSFS_DEPRECATED_V2]
   [*] open by fhandle syscalls [CONFIG_FHANDLE]
   [ ] Auditing support [CONFIG_AUDIT]
   [*] Control Group support [CONFIG_CGROUPS]
Processor type and features  ---&gt;
   [*] Enable seccomp to safely compute untrusted bytecode [CONFIG_SECCOMP]
Networking support  ---&gt;
  Networking options  ---&gt;
   &lt;*&gt; The IPv6 protocol [CONFIG_IPV6]
Device Drivers  ---&gt;
  Generic Driver Options  ---&gt;
   [ ] Support for uevent helper [CONFIG_UEVENT_HELPER]
   [*] Maintain a devtmpfs filesystem to mount at /dev [CONFIG_DEVTMPFS]
   [ ] Fallback user-helper invocation for firmware loading [CONFIG_FW_LOADER_USER_HELPER]
Firmware Drivers  ---&gt;
   [*] Export DMI identification via sysfs to userspace [CONFIG_DMIID]
File systems  ---&gt;
   [*] Inotify support for userspace [CONFIG_INOTIFY_USER]
   &lt;*&gt; Kernel automounter version 4 support (also supports v3) [CONFIG_AUTOFS4_FS]
  Pseudo filesystems  ---&gt;
   [*] Tmpfs POSIX Access Control Lists [CONFIG_TMPFS_POSIX_ACL]
   [*] Tmpfs extended attributes [CONFIG_TMPFS_XATTR]
Kernel hacking  ---&gt;
       Choose kernel unwinder (Frame pointer unwinder)  ---&gt; [CONFIG_UNWINDER_FRAME_POINTER]</screen>
    </note>

    <note revision="systemd">
      <!--para>While "The IPv6 Protocol" is not strictly
      required, it is highly recommended by the systemd developers.</para-->
      <para>尽管 <quote>The IPv6 Protocol</quote> （IPv6 协议支持）
		  并不是严格要求的，但是 systemd 开发者强烈推荐启用它。</para>
    </note>

    <para revision="sysv">There are several other options that may be desired
    depending on the requirements for the system. For a list of options needed
    for BLFS packages, see the <ulink
    url="&lfs-root;blfs/view/&short-version;/longindex.html#kernel-config-index">BLFS
    Index of Kernel Settings</ulink>
    (&lfs-root;blfs/view/&short-version;/longindex.html#kernel-config-index).</para>

    <note>
      <!--para>If your host hardware is using UEFI, then the 'make defconfig'
      above should automatically add in some EFI-related kernel options.</para-->
      <para>如果您的硬件平台使用 UEFI，则 <command>make defconfig</command>
		  命令也会自动加入一些 EFI 相关的内核选项。</para>

      <!--para>In order to allow your LFS kernel to be booted from within your
      host's UEFI boot environment, your kernel must have this option
      selected:</para-->
      <para>为了允许从宿主系统的 UEFI 引导环境引导 LFS 内核，
		  您必须选择一个内核选项：</para>

<screen role="nodump">Processor type and features  ---&gt;
   [*]   EFI stub support  [CONFIG_EFI_STUB]</screen>

      <!--para>A fuller description of managing UEFI environments from within LFS
      is covered by the lfs-uefi.txt hint at
      <ulink 
        url="http://www.linuxfromscratch.org/hints/downloads/files/lfs-uefi.txt"/>.
      </para-->
	  <para>在 LFS 中管理 UEFI 环境的较完整说明包含在 lfs-uefi.txt 中，
		  它位于 <ulink url="&hints-root;lfs-uefi.txt"/>。
	  </para>
    </note>
	<note><title>译注</title>
		<para>lfs-uefi.txt 的内容较老，译者维护了它的一份更新版本，
			见
<ulink url="http://svn.linuxfromscratch.org/hints/trunk/lfs-uefi-20180409.txt"/>
			。</para>
	</note>

    <variablelist>
      <title>上述配置选项的含义：</title>

      <varlistentry>
        <term><parameter>Support for uevent helper</parameter></term>
        <listitem>
          <para>如果启用了该选项，它可能干扰 Udev/Eudev 的设备管理。</para>
        </listitem>
      </varlistentry>

      <varlistentry>
        <term><parameter>Maintain a devtmpfs</parameter></term>
        <listitem>
          <!--para>This will create automated device nodes which are populated by the
          kernel, even without Udev running.  Udev then runs on top of this,
          managing permissions and adding symlinks.  This configuration
          item is required for all users of Udev/Eudev.</para-->
          <para>该选项会使内核自动创建设备节点，即使 Udev 没有运行。
			  Udev 之后才在这些设备节点的基础上运行，
			  管理它们的访问权限并为它们建立符号链接。
			  所有 Udev/Eudev 用户都需要启用该选项。</para>
        </listitem>
      </varlistentry>

    </variablelist>

<screen role="nodump"><userinput>make menuconfig</userinput></screen>

    <variablelist>
      <title>以上命令中可选的 make 环境变量及含义：</title>

      <varlistentry>
        <term><parameter>LANG=&lt;host_LANG_value&gt; LC_ALL=</parameter></term>
        <listitem>
          <!--para>This establishes the locale setting to the one used on the
          host.  This may be needed for a proper menuconfig ncurses interface
          line drawing on a UTF-8 linux text console.</para>

          <para>If used, be sure to replace
          <replaceable>&lt;host_LANG_value&gt;</replaceable> by the value of
          the <envar>$LANG</envar> variable from your host.  You can
          alternatively use instead the host's value of <envar>$LC_ALL</envar>
          or <envar>$LC_CTYPE</envar>.</para-->
          <para>它们根据宿主使用的 locale 建立 locale 设定。
			  在 UTF-8 Linux 文本终端下，有时必须这样做才能正确绘制
			  基于 ncurses 的配置菜单接口。</para>
		  <para>在这种情况下，一定要将 <replaceable>&lt;host_LANG_value&gt;
			  </replaceable> 替换成宿主环境中的 <envar>$LANG</envar>
			  变量值。您也可以使用宿主环境中 <envar>$LC_ALL</envar>
			  或 <envar>$LC_CTYPE</envar> 的值代替。</para>
        </listitem>
      </varlistentry>

    </variablelist>

    <!--para>Alternatively, <command>make oldconfig</command> may be more
    appropriate in some situations. See the <filename>README</filename>
    file for more information.</para-->
    <para>某些情况下，<command>make oldconfig</command> 更为合适。
		阅读 <filename>README</filename> 文件了解更多信息。</para>

    <!--para>If desired, skip kernel configuration by copying the kernel
    config file, <filename>.config</filename>, from the host system
    (assuming it is available) to the unpacked <filename
    class="directory">linux-&linux-version;</filename> directory. However,
    we do not recommend this option. It is often better to explore all the
    configuration menus and create the kernel configuration from
    scratch.</para-->
    <para>如果希望的话，也可以将宿主系统的内核配置文件 <filename>.config
		</filename> 拷贝到解压出的
		<filename class="directory">linux-&linux-version;</filename>
		目录（前提是可以找到该文件）。然而我们不推荐这样做，
		一般来说，浏览整个配置目录，并从头创建内核配置是更好的选择。</para>

    <para>编译内核映像和模块：</para>

<screen><userinput remap="make">make</userinput></screen>

    <!--para>If using kernel modules, module configuration in <filename
    class="directory">/etc/modprobe.d</filename> may be required.
    Information pertaining to modules and kernel configuration is
    located in <xref linkend="ch-scripts-udev"/> and in the kernel
    documentation in the <filename
    class="directory">linux-&linux-version;/Documentation</filename> directory.
    Also, <filename>modprobe.d(5)</filename> may be of interest.</para-->
    <para>如果要使用内核模块，可能需要在
		<filename class="directory">/etc/modprobe.d</filename>
		中写入模块配置。讨论模块和内核配置的信息位于
		<xref linkend="ch-scripts-udev"/> 和
		<filename class="directory">linux-&linux-version;/Documentation
		</filename> 目录下的内核文档中。另外
		<filename>modprobe.d(5)</filename> 也可以作为参考。</para>

    <para>如果内核配置使用了模块，安装它们：</para>

<screen><userinput remap="install">make modules_install</userinput></screen>

    <!--para>After kernel compilation is complete, additional steps are
    required to complete the installation. Some files need to be copied to
    the <filename class="directory">/boot</filename> directory.</para-->
    <para>在内核编译完成后，需要进行额外步骤完成安装，
		一些文件需要拷贝到 <filename class="directory">/boot</filename>
		目录中。</para>

    <caution>
      <!--para>If the host system has a separate /boot partition, the files copied
      below should go there. The easiest way to do that is to bind /boot on the
      host (outside chroot) to /mnt/lfs/boot before proceeding.  As the root
      user in the <emphasis>host system</emphasis>:</para-->
      <para>如果宿主系统有单独的 /boot 分区，需要将这些文件拷贝到该分区中。
		  最简单的方法是将宿主系统的 /boot （在 chroot 之外）绑定到
		  /mnt/lfs/boot 再拷贝文件，在<emphasis>宿主系统</emphasis>中，
		  以 root 身份执行：</para>

<screen role="nodump"><userinput>mount --bind /boot /mnt/lfs/boot</userinput></screen>
    </caution>

    <!--para>The path to the kernel image may vary depending on the platform being
    used. The filename below can be changed to suit your taste, but the stem of
    the filename should be <emphasis>vmlinuz</emphasis> to be compatible with
    the automatic setup of the boot process described in the next section.  The
    following command assumes an x86 architecture:</para-->
    <para>指向内核映像的路径可能随机器平台的不同而变化。
		下面使用的文件名可以依照您的需要改变，
		但文件名的开头应该保持为 <emphasis>vmlinuz</emphasis>，
		以保证和下一节描述的引导过程自动设定相兼容。
		下面的命令假定是机器是 x86 体系结构：</para>

<screen revision="sysv"><userinput remap="install">cp -iv arch/x86/boot/bzImage /boot/vmlinuz-&linux-version;-lfs-&version;</userinput></screen>

<screen revision="systemd"><userinput remap="install">cp -iv arch/x86/boot/bzImage /boot/vmlinuz-&linux-version;-lfs-&versiond;</userinput></screen>

    <!--para><filename>System.map</filename> is a symbol file for the kernel.
    It maps the function entry points of every function in the kernel API,
    as well as the addresses of the kernel data structures for the running
    kernel.  It is used as a resource when investigating kernel problems.
    Issue the following command to install the map file:</para-->
    <para><filename>System.map</filename> 是内核符号文件，
		它将内核 API 的每个函数入口点和运行时数据结构映射到它们的地址。
		它在调查分析内核出现的问题时被使用。执行以下命令安装该文件：</para>

<screen><userinput remap="install">cp -iv System.map /boot/System.map-&linux-version;</userinput></screen>

    <!--para>The kernel configuration file <filename>.config</filename>
    produced by the <command>make menuconfig</command> step
    above contains all the configuration selections for the kernel
    that was just compiled. It is a good idea to keep this file for future
    reference:</para-->
    <para><command>make menuconfig</command> 生成的内核配置文件
		<filename>.config</filename> 包含编译好的内核的所有配置选项。
		最好能将它保留下来以供日后参考：</para>

<screen><userinput remap="install">cp -iv .config /boot/config-&linux-version;</userinput></screen>

    <para>安装 Linux 内核文档：</para>

<screen><userinput remap="install">install -d /usr/share/doc/linux-&linux-version;
cp -r Documentation/* /usr/share/doc/linux-&linux-version;</userinput></screen>

    <!--para>It is important to note that the files in the kernel source
    directory are not owned by <emphasis>root</emphasis>. Whenever a
    package is unpacked as user <emphasis>root</emphasis> (like we did
    inside chroot), the files have the user and group IDs of whatever
    they were on the packager's computer. This is usually not a problem
    for any other package to be installed because the source tree is
    removed after the installation. However, the Linux source tree is
    often retained for a long time.  Because of this, there is a chance
    that whatever user ID the packager used will be assigned to somebody
    on the machine. That person would then have write access to the kernel
    source.</para-->
    <para>需要注意的是，在内核源代码目录中可能有不属于 <emphasis>root
		</emphasis> 的文件。在以 <emphasis>root</emphasis>
		身份解压源代码包时（就像我们在 chroot 环境中所做的那样），
		这些文件会获得它们之前在软件包创建者的计算机上的用户和组 ID。
		这一般不会造成问题，因为在安装后通常会删除源代码目录树。
		然而，Linux 源代码目录树一般会被保留较长时间，
		这样创建者当时使用的用户 ID 就可能被分配给本机的某个用户，
		导致该用户拥有内核源代码的写权限。</para>

    <note>
      <!---para>In many cases, the configuration of the kernel will need to be 
      updated for packages that will be installed later in BLFS.  Unlike
      other packages, it is not necessary to remove the kernel source tree
      after the newly built kernel is installed.</para-->  
      <para>之后在 BLFS 中安装软件包时往往需要修改内核配置。
		  因此，和其他软件包不同，我们在安装好内核后可以不移除源代码树。
	  </para>
    
      <!--para>If the kernel source tree is going to be retained, run
      <command>chown -R 0:0</command> on the <filename
      class="directory">linux-&linux-version;</filename> directory to ensure
      all files are owned by user <emphasis>root</emphasis>.</para-->
      <para>如果要保留内核源代码树，对目录 <filename class="directory">
			  linux-&linux-version;</filename> 执行
		  <command>chown -R 0:0</command>，以保证其中所有文件都属于
		  <emphasis>root</emphasis>。</para>
    </note>

    <warning>
      <!--para>Some kernel documentation recommends creating a symlink from
      <filename class="symlink">/usr/src/linux</filename> pointing to the kernel
      source directory.  This is specific to kernels prior to the 2.6 series and
      <emphasis>must not</emphasis> be created on an LFS system as it can cause
      problems for packages you may wish to build once your base LFS system is
      complete.</para-->
      <para>有的内核文档建议创建符号链接
		  <filename class="symlink">/usr/src/linux</filename>
		  指向内核源代码目录，这仅仅适用于 2.6 系列之前的内核。
		  在 LFS 系统上<emphasis>绝对不要</emphasis>创建它，
		  因为在构建完基本 LFS 系统后，它可能在您构建其他软件包时引起问题。
	  </para>
    </warning>

    <warning>
      <!--para>The headers in the system's <filename
      class="directory">include</filename> directory (<filename
      class="directory">/usr/include</filename>) should
      <emphasis>always</emphasis> be the ones against which Glibc was compiled,
      that is, the sanitised headers installed in <xref
      linkend="ch-system-linux-headers"/>.  Therefore, they should
      <emphasis>never</emphasis> be replaced by either the raw kernel headers
      or any other kernel sanitized headers.</para-->
      <para>在系统 <filename class="directory">include</filename>
		  目录（即 <filename class="directory">/usr/include</filename>
		  中的内核头文件应该<emphasis>总是</emphasis>与构建 Glibc
		  时使用的内核头文件一致，即保持为
		  <xref linkend="ch-system-linux-headers"/> 中安装的净化头文件。
		  换句话说，<emphasis>永远不要</emphasis>用原始内核头文件，
		  或其他版本内核的净化头文件替换它们。</para>
    </warning>

  </sect2>

  <sect2 id="conf-modprobe" role="configuration">
    <title>配置 Linux 内核模块加载顺序</title>

    <indexterm zone="conf-modprobe">
      <primary sortas="e-/etc/modprobe.d/usb.conf">/etc/modprobe.d/usb.conf</primary>
    </indexterm>

    <!--para>Most of the time Linux modules are loaded automatically, but
    sometimes it needs some specific direction.  The program that loads
    modules, <command>modprobe</command> or <command>insmod</command>, uses
    <filename>/etc/modprobe.d/usb.conf</filename> for this purpose.  This file
    needs to be created so that if the USB drivers (ehci_hcd, ohci_hcd and
    uhci_hcd) have been built as modules, they will be loaded in the correct
    order; ehci_hcd needs to be loaded prior to ohci_hcd and uhci_hcd in order
    to avoid a warning being output at boot time.</para-->
    <para>多数情况下 Linux 内核模块可以自动加载，但有时需要指定加载顺序。
		负责加载内核模块的程序 <command>modprobe</command> 和
		<command>insmod</command> 从 <filename>/etc/modprobe.d</filename>
		下的配置文件中读取加载顺序，例如，如果 USB 驱动程序
		（ehci_hcd、ohci_hcd 和 uhci_hcd）被构建为模块，
		则必须按照先加载 echi_hcd ，再加载 ohci_hcd 和 uhci_hcd 的正确顺序，
		才能避免引导时出现警告信息。</para>

	<para>为此，执行以下命令创建文件
		<filename>/etc/modprobe.d/usb.conf</filename>：</para>

<screen><userinput>install -v -m755 -d /etc/modprobe.d
cat &gt; /etc/modprobe.d/usb.conf &lt;&lt; "EOF"
<literal># Begin /etc/modprobe.d/usb.conf

install ohci_hcd /sbin/modprobe ehci_hcd ; /sbin/modprobe -i ohci_hcd ; true
install uhci_hcd /sbin/modprobe ehci_hcd ; /sbin/modprobe -i uhci_hcd ; true

# End /etc/modprobe.d/usb.conf</literal>
EOF</userinput></screen>

  </sect2>

  <sect2 id="contents-kernel" role="content">
    <title>Linux 的内容</title>

    <segmentedlist>
      <segtitle>安装的文件</segtitle>
      <segtitle>安装的目录</segtitle>

      <seglistitem>
        <seg>config-&linux-version;,
        <phrase revision="sysv">vmlinuz-&linux-version;-lfs-&version;,</phrase>
        <phrase revision="systemd">vmlinuz-&linux-version;-lfs-&versiond;,</phrase>
        以及 System.map-&linux-version;</seg>
        <seg>/lib/modules 和 /usr/share/doc/linux-&linux-version;</seg>
      </seglistitem>
    </segmentedlist>

    <variablelist>
      <bridgehead renderas="sect3">简要描述</bridgehead>
      <?dbfo list-presentation="list"?>
      <?dbhtml list-presentation="table"?>

      <varlistentry id="config">
        <term><filename>config-&linux-version;</filename></term>
        <listitem>
          <para>包含所有内核配置选项的值</para>
          <indexterm zone="ch-bootable-kernel config">
            <primary sortas="e-/boot/config">/boot/config-&linux-version;</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="lfskernel">
        <term revision="sysv"><filename>vmlinuz-&linux-version;-lfs-&version;</filename></term>
        <term revision="systemd"><filename>vmlinuz-&linux-version;-lfs-&versiond;</filename></term>
        <listitem>
          <!--para>The engine of the Linux system. When turning on the computer,
          the kernel is the first part of the operating system that gets loaded.
          It detects and initializes all components of the computer's hardware,
          then makes these components available as a tree of files to the
          software and turns a single CPU into a multitasking machine capable
          of running scores of programs seemingly at the same time</para-->
          <para>Linux 系统的引擎，在启动计算机时，
			  它是操作系统中被最早加载的部分。它检测并初始化计算机硬件，
			  将它们以目录树的形式提供给软件，
			  并将单个 CPU 封装成多任务系统，
			  使多个用户程序看上去在同时执行。
		  </para>
          <indexterm zone="ch-bootable-kernel lfskernel">
            <primary sortas="b-lfskernel">lfskernel-&linux-version;</primary>
          </indexterm>
        </listitem>
      </varlistentry>

      <varlistentry id="System.map">
        <term><filename>System.map-&linux-version;</filename></term>
        <listitem>
          <!--para>A list of addresses and symbols; it maps the entry points and
          addresses of all the functions and data structures in the
          kernel</para-->
          <para>地址和符号列表，将函数和数据结构映射为入口点和地址</para>
          <indexterm zone="ch-bootable-kernel System.map">
            <primary sortas="e-/boot/System.map">/boot/System.map-&linux-version;</primary>
          </indexterm>
        </listitem>
      </varlistentry>

    </variablelist>

  </sect2>

</sect1>
