Linux内核编译前需要了解的内容

 Linux内核版本5.x

  • < http://kernel.org/ >
    这些是Linux版本5的发行说明。请仔细阅读它们,因为它们告诉您所有内容,解释如何安装内核以及出现问题时应采取的措施。

什么是Linux?

Linux是Unix操作系统的克隆,由Linus Torvalds在网络松散的黑客团队的协助下从零开始编写。它旨在实现POSIX和Single UNIX规范的合规性。

它具有您在现代的成熟Unix中所期望的所有功能,包括真正的多任务,虚拟内存,共享库,需求加载,共享的写时复制可执行文件,适当的内存管理以及包括IPv4和IPv6在内的多堆栈网络。

它根据GNU通用公共许可证v2分发-有关更多详细信息,请参见随附的COPYING文件。

 它在什么硬件上运行?

尽管最初最初是为基于32位x86的PC(386或更高版本)开发的,但今天的Linux也可以(至少)在Compaq Alpha AXP,Sun SPARC和UltraSPARC,Motorola 68000,PowerPC,PowerPC64,ARM,Hitachi SuperH,Cell上运行,IBM S / 390,MIPS,HP PA-RISC,英特尔IA-64,DEC VAX,AMD x86-64 Xtensa和ARC体系结构。

只要具有分页内存管理单元(PMMU)和GNU C编译器(gcc)的端口(属于GNU编译器集合,GCC的一部分),Linux便可以轻松移植到大多数通用的32位或64位体系结构中。 。尽管功能显然受到限制,但Linux也已移植到许多没有PMMU的体系结构中。Linux也已移植到其自身。现在,您可以将内核作为用户空间应用程序运行-这称为UserMode Linux(UML)。

文献资料

互联网上和书籍中都有许多电子形式的文档,既特定于Linux,又涉及一般的UNIX问题。我建议您查看任何Linux FTP站点上的LDP(Linux文档项目)书籍的文档子目录。本README并不是系统上的文档:有更好的可用资源。
Documentation /子目录中有各种README文件:例如,这些文件通常包含某些驱动程序的特定于内核的安装说明。

安装内核源文件

如果安装完整源代码,请将内核tarball放在您具有权限的目录中(例如,主目录)并解压缩它:

xz -cd linux-5.x.tar.xz | tar xvf -

将“ X”替换为最新内核的版本号。

不要使用/usr/src/linux 路径!该区域具有(通常不完整)一组由库的头文件使用的内核头文件。它们应该与库匹配,并且不会因kernel-du-jour碰巧而陷入混乱。

您也可以通过补丁在5.x版本之间升级内核。补丁以xz格式分发。要通过补丁安装,请获取所有较新的补丁文件,输入内核源代码的顶层目录(linux-5.x)并执行:

xz -cd ../patch-5.x.xz | patch -p1

对于大于当前源代码树in_order版本“ x”的所有版本替换“ x” ,您应该可以。您可能要删除备份文件(some-file-name〜或some-file-name.orig),并确保没有失败的补丁程序(some-file-name#或some-file-name.rej)。 。如果有,则您或我都犯了一个错误。

与5.x内核的修补程序不同,5.xy内核(也称为-stable内核)的修补程序不是增量的,而是直接应用于基本5.x内核。例如,如果您的基本内核是5.0,并且您想应用5.0.3补丁程序,则一定不要首先应用5.0.1和5.0.2补丁程序。同样,如果您正在运行5.0.2内核版本并希望跳至5.0.3,则必须先反转5.0.2补丁(即补丁-R),然后再应用5.0.3补丁。

另外,脚本patch-kernel可用于自动执行此过程。它确定当前的内核版本并应用找到的所有修补程序:

linux/scripts/patch-kernel linux

上面命令中的第一个参数是内核源的位置。打补丁的程序是从当前目录的应用程序,但是可以将备用目录指定为第二个参数。

  • 确保您周围没有旧的.o文件和依赖项:
    cd linux
    make mrproper

    您现在应该正确安装了内核源码。

 软件需求

编译和运行5.x内核需要各种软件包的最新版本。请教 编译内核所需的最低版本号的最低要求以及如何获取这些软件包的更新。请注意,使用这些软件包的旧版本可能会导致间接错误,很难跟踪,因此请不要假定您仅在构建或操作过程中出现明显问题时才可以更新软件包。

 内核的构建目录

默认情况下,编译内核时,所有输出文件将与内核源代码一起存储。使用该选项,您可以为输出文件(包括.config)指定备用位置。例:

make O=output/dir
kernel source code: /usr/src/linux-5.x
build directory:    /home/name/build/kernel

要配置和构建内核,请使用:

cd /usr/src/linux-5.x
make O=/home/name/build/kernel menuconfig
make O=/home/name/build/kernel
sudo make O=/home/name/build/kernel modules_install install
  • 请注意:如果使用该O=output/dir选项,则必须将其用于所有的make调用。

配置内核

即使仅升级一个次要版本,也不要跳过此步骤。每个版本中都添加了新的配置选项,如果未按预期设置配置文件,则会出现奇怪的问题。如果要以最少的工作量将现有配置带到新版本,请使用,它只会询问您新问题的答案。

make oldconfig

备用配置命令是:

"make config"      Plain text interface.
"make menuconfig"  Text based color menus, radiolists & dialogs.
"make nconfig"     Enhanced text based color menus.
"make xconfig"     Qt based configuration tool.
"make gconfig"     GTK+ based configuration tool.
"make oldconfig"   Default all questions based on the contents of
                   your existing ./.config file and asking about
                   new config symbols.
"make olddefconfig"
                   Like above, but sets new symbols to their default
                   values without prompting.
"make defconfig"   Create a ./.config file by using the default
                   symbol values from either arch/$ARCH/defconfig
                   or arch/$ARCH/configs/${PLATFORM}_defconfig,
                   depending on the architecture.
"make ${PLATFORM}_defconfig"
                   Create a ./.config file by using the default
                   symbol values from
                   arch/$ARCH/configs/${PLATFORM}_defconfig.
                   Use "make help" to get a list of all available
                   platforms of your architecture.
"make allyesconfig"
                   Create a ./.config file by setting symbol
                   values to 'y' as much as possible.
"make allmodconfig"
                   Create a ./.config file by setting symbol
                   values to 'm' as much as possible.
"make allnoconfig" Create a ./.config file by setting symbol
                   values to 'n' as much as possible.
"make randconfig"  Create a ./.config file by setting symbol
                   values to random values.
"make localmodconfig" Create a config based on current config and
                      loaded modules (lsmod). Disables any module
                      option that is not needed for the loaded modules.

                      To create a localmodconfig for another machine,
                      store the lsmod of that machine into a file
                      and pass it in as a LSMOD parameter.

                      Also, you can preserve modules in certain folders
                      or kconfig files by specifying their paths in
                      parameter LMC_KEEP.

              target$ lsmod > /tmp/mylsmod
              target$ scp /tmp/mylsmod host:/tmp

              host$ make LSMOD=/tmp/mylsmod \
                      LMC_KEEP="drivers/usb:drivers/gpu:fs" \
                      localmodconfig

                      The above also works when cross compiling.

"make localyesconfig" Similar to localmodconfig, except it will convert
                      all module options to built in (=y) options. You can
                      also preserve modules by LMC_KEEP.
"make kvmconfig"   Enable additional options for kvm guest kernel support.
"make xenconfig"   Enable additional options for xen dom0 guest kernel
                   support.
"make tinyconfig"  Configure the tiniest possible kernel.

您可以在Kconfig make config中找到有关使用Linux内核配置工具的更多信息。

关于的注释:make config

拥有不必要的驱动程序会使内核变大,并且在某些情况下可能会导致问题:检测不存在的控制器可能会使您的其他控制器卡住。
如果存在带有编译的数学仿真的内核,则仍将使用协处理器:在这种情况下,永远不会使用数学仿真。内核会稍大一些,但是无论它们是否具有数学协处理器,它都可以在不同的机器上工作。
“内核hacking”配置细节通常会导致更大或更慢的内核(或同时出现这两者),并且甚至可以通过配置一些例程来主动尝试破坏坏代码以发现内核问题(kmalloc()),从而降低内核的稳定性。因此,您可能应该对“开发”,“实验”或“调试”功能的问题回答“ n”。

 编译内核

确保您至少有可用的gcc 4.9。有关更多信息,请参阅编译内核的最低要求。

请注意,您仍然可以使用此内核运行a.out用户程序。

执行make以创建压缩的内核映像。如果您已经安装了适合内核makefile的lilo,也可以这样做,但是您可能需要首先检查特定的lilo设置。

make install

要进行实际安装,您必须是root用户,但是任何正常的构建都不需要这样做。

如果将内核的任何部分都配置为modules,则还必须这样做。

make modules_install

详细的内核编译/生成输出:

通常,内核构建系统以相当安静的模式运行(但不是完全安静)。
但是,有时您或其他内核开发人员需要在执行时完全看到编译,链接或其他命令。
为此,请使用“详细”构建模式。这可以通过传递V=1给make命令来完成 ,例如:

make V=1 all

要使构建系统还告诉您重建每个目标的原因,请使用V=2。默认值为V=0

  • 万一出现问题,请随时准备备份内核。
    对于开发版本尤其如此,因为每个新版本都包含尚未调试的新代码。
    确保还保留了与该内核对应的模块的备份。
    如果要安装与工作内核具有相同版本号的新内核,请在执行之前对modules目录进行备份。

    make modules_install

    另外,在编译之前,请使用内核配置选项LOCALVERSION将唯一的后缀附加到常规内核版本中。可以在常规设置菜单中设置LOCALVERSION

为了引导您的新内核,您需要将内核映像(例如,编译后的…/linux/arch/x86/boot/bzImage)复制到找到常规可引导内核的位置。

** 不再支持在没有引导加载程序(例如LILO)帮助的情况下直接从软盘引导内核。

如果从硬盘驱动器引导Linux,则有可能使用LILO,LILO使用文件/etc/lilo.conf中指定的内核映像。内核映像文件通常是/vmlinuz,/boot/vmlinuz,/ bzImage或/boot bzImage。
要使用新内核,请保存旧内核的副本,然后将新内核镜像复制到旧内核镜像上。然后,您必须重新运行LILO才能更新加载映射!否则,您将无法引导新的内核镜像。

  • 重新安装LILO通常是运行/sbin/lilo的问题。您可能希望编辑/etc/lilo.conf为旧内核映像(例如/vmlinux.old)指定一个条目,以防新内核映像不起作用。有关更多信息,请参见LILO文档。

  • 重新安装LILO之后,您应该已经准备就绪。

  • 关闭系统,重新启动,然后享受!

如果您需要在内核映像中更改默认的根设备,视频模式等,请在适当的地方使用引导加载程序的引导选项。无需重新编译内核即可更改这些参数。

  • 使用新内核重新启动并开始浪吧!

Linux内核编译环境详解

Minimal requirements to compile the Kernel

Intro

This document is designed to provide a list of the minimum levels of software necessary to run the 4.x kernels.

This document is originally based on my “Changes” file for 2.0.x kernels and therefore owes credit to the same people as that file (Jared Mauch, Axel Boldt, Alessandro Sigala, and countless other users all over the ‘net).

Current Minimal Requirements

Upgrade to at least these software revisions before thinking you’ve encountered a bug! If you’re unsure what version you’re currently running, the suggested command should tell you.

Again, keep in mind that this list assumes you are already functionally running a Linux kernel. Also, not all tools are necessary on all systems; obviously, if you don’t have any PC Card hardware, for example, you probably needn’t concern yourself with pcmciautils.

Program Minimal version Command to check the version
GNU C 4.9 gcc –version
Clang/LLVM (optional) 10.0.1 clang –version
GNU make 3.81 make –version
binutils 2.23 ld -v
flex 2.5.35 flex –version
bison 2.0 bison –version
util-linux 2.10o fdformat –version
kmod 13 depmod -V
e2fsprogs 1.41.4 e2fsck -V
jfsutils 1.1.3 fsck.jfs -V
reiserfsprogs 3.6.3 reiserfsck -V
xfsprogs 2.6.0 xfs_db -V
squashfs-tools 4.0 mksquashfs -version
btrfs-progs 0.18 btrfsck
pcmciautils 004 pccardctl -V
quota-tools 3.09 quota -V
PPP 2.4.0 pppd –version
nfs-utils 1.0.5 showmount –version
procps 3.2.0 ps –version
oprofile 0.9 oprofiled –version
udev 081 udevd –version
grub 0.93 grub –version / grub-install –version
mcelog 0.6 mcelog –version
iptables 1.4.2 iptables -V
openssl & libcrypto 1.0.0 openssl version
bc 1.06.95 bc –version
Sphinx[1] 1.3 sphinx-build –version
  • [1] Sphinx is needed only to build the Kernel documentation

    Kernel compilation

  • GCC
    The gcc version requirements may vary depending on the type of CPU in your computer.

  • Clang/LLVM (optional)
    The latest formal release of clang and LLVM utils (according to releases.llvm.org) are supported for building kernels. Older releases aren’t guaranteed to work, and we may drop workarounds from the kernel that were used to support older versions. Please see additional docs on Building Linux with Clang/LLVM.

  • Make
    You will need GNU make 3.81 or later to build the kernel.

  • Binutils
    Binutils 2.23 or newer is needed to build the kernel.

  • pkg-config
    The build system, as of 4.18, requires pkg-config to check for installed kconfig tools and to determine flags settings for use in ‘make {g,x}config’. Previously pkg-config was being used but not verified or documented.

  • Flex
    Since Linux 4.16, the build system generates lexical analyzers during build. This requires flex 2.5.35 or later.

  • Bison
    Since Linux 4.16, the build system generates parsers during build. This requires bison 2.0 or later.

  • Perl
    You will need perl 5 and the following modules: Getopt::Long, Getopt::Std, File::Basename, and File::Find to build the kernel.

  • BC
    You will need bc to build kernels 3.10 and higher

  • OpenSSL
    Module signing and external certificate handling use the OpenSSL program and crypto library to do key creation and signature generation.

You will need openssl to build kernels 3.7 and higher if module signing is enabled. You will also need openssl development packages to build kernels 4.3 and higher.

System utilities

32-bit UID support is now in place. Have fun!

Linux documentation for functions is transitioning to inline documentation via specially-formatted comments near their definitions in the source. These comments can be combined with ReST files the Documentation/ directory to make enriched documentation, which can then be converted to PostScript, HTML, LaTex, ePUB and PDF files. In order to convert from ReST format to a format of your choice, you’ll need Sphinx.

  • Util-linux
    New versions of util-linux provide fdisk support for larger disks, support new options to mount, recognize more supported partition types, have a fdformat which works with 2.4 kernels, and similar goodies. You’ll probably want to upgrade.

  • Ksymoops
    If the unthinkable happens and your kernel oopses, you may need the ksymoops tool to decode it, but in most cases you don’t. It is generally preferred to build the kernel with CONFIG_KALLSYMS so that it produces readable dumps that can be used as-is (this also produces better output than ksymoops). If for some reason your kernel is not build with CONFIG_KALLSYMS and you have no way to rebuild and reproduce the Oops with that option, then you can still decode that Oops with ksymoops.

  • Mkinitrd
    These changes to the /lib/modules file tree layout also require that mkinitrd be upgraded.

  • E2fsprogs
    The latest version of e2fsprogs fixes several bugs in fsck and debugfs. Obviously, it’s a good idea to upgrade.

  • JFSutils
    The jfsutils package contains the utilities for the file system. The following utilities are available:

  • fsck.jfs - initiate replay of the transaction log, and check and repair a JFS formatted partition.

  • mkfs.jfs - create a JFS formatted partition.
    other file system utilities are also available in this package.

  • Reiserfsprogs
    The reiserfsprogs package should be used for reiserfs-3.6.x (Linux kernels 2.4.x). It is a combined package and contains working versions of mkreiserfs, resize_reiserfs, debugreiserfs and reiserfsck. These utils work on both i386 and alpha platforms.

  • Xfsprogs
    The latest version of xfsprogs contains mkfs.xfs, xfs_db, and the xfs_repair utilities, among others, for the XFS filesystem. It is architecture independent and any version from 2.0.0 onward should work correctly with this version of the XFS kernel code (2.6.0 or later is recommended, due to some significant improvements).

  • PCMCIAutils
    PCMCIAutils replaces pcmcia-cs. It properly sets up PCMCIA sockets at system startup and loads the appropriate modules for 16-bit PCMCIA devices if the kernel is modularized and the hotplug subsystem is used.

  • Quota-tools
    Support for 32 bit uid’s and gid’s is required if you want to use the newer version 2 quota format. Quota-tools version 3.07 and newer has this support. Use the recommended version or newer from the table above.

  • Intel IA32 microcode
    A driver has been added to allow updating of Intel IA32 microcode, accessible as a normal (misc) character device. If you are not using udev you may need to:

mkdir /dev/cpu
mknod /dev/cpu/microcode c 10 184
chmod 0644 /dev/cpu/microcode
as root before you can use this. You’ll probably also want to get the user-space microcode_ctl utility to use with this.

  • udev
    udev is a userspace application for populating /dev dynamically with only entries for devices actually present. udev replaces the basic functionality of devfs, while allowing persistent device naming for devices.

  • FUSE
    Needs libfuse 2.4.0 or later. Absolute minimum is 2.3.0 but mount options direct_io and kernel_cache won’t work.

Networking

  • General changes
    If you have advanced network configuration needs, you should probably consider using the network tools from ip-route2.

  • Packet Filter / NAT
    The packet filtering and NAT code uses the same tools like the previous 2.4.x kernel series (iptables). It still includes backwards-compatibility modules for 2.2.x-style ipchains and 2.0.x-style ipfwadm.

  • PPP
    The PPP driver has been restructured to support multilink and to enable it to operate over diverse media layers. If you use PPP, upgrade pppd to at least 2.4.0.

If you are not using udev, you must have the device file /dev/ppp which can be made by:

mknod /dev/ppp c 108 0
as root.

  • NFS-utils
    In ancient (2.4 and earlier) kernels, the nfs server needed to know about any client that expected to be able to access files via NFS. This information would be given to the kernel by mountd when the client mounted the filesystem, or by exportfs at system startup. exportfs would take information about active clients from /var/lib/nfs/rmtab.

This approach is quite fragile as it depends on rmtab being correct which is not always easy, particularly when trying to implement fail-over. Even when the system is working well, rmtab suffers from getting lots of old entries that never get removed.

With modern kernels we have the option of having the kernel tell mountd when it gets a request from an unknown host, and mountd can give appropriate export information to the kernel. This removes the dependency on rmtab and means that the kernel only needs to know about currently active clients.

To enable this new functionality, you need to:

mount -t nfsd nfsd /proc/fs/nfsd
before running exportfs or mountd. It is recommended that all NFS services be protected from the internet-at-large by a firewall where that is possible.

  • mcelog
    On x86 kernels the mcelog utility is needed to process and log machine check events when CONFIG_X86_MCE is enabled. Machine check events are errors reported by the CPU. Processing them is strongly encouraged.

Kernel documentation

  • Sphinx
    Please see Sphinx Install in Introduction for details about Sphinx requirements.

Getting updated software