内核/升级

From Gentoo Wiki
< Kernel
Jump to:navigation Jump to:search
This page is a translated version of the page Kernel/Upgrade and the translation is 59% complete.
Outdated translations are marked like this.
Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français • ‎italiano • ‎português do Brasil • ‎русский • ‎中文(中国大陆)‎ • ‎日本語 • ‎한국어

这篇文章描述了升级到新版本内核的步骤。

从新的源代码制作一个新的内核基本上和安装系统时的制作内核是一样的。不同之处在于可以基于旧内核的配置文件来为新内核制作一个配置文件。使用旧的配置文件可以让使用者不从头到尾再做一次内核选项(像 make menuconfig 一样)。

内核的配置文件 .config 保存在内核源代码的目录下面。新内核也许会添加或移除一些特性和选项。内核配置文件指定了是否开启内核的功能和选项,要么嵌入内核,要么以模块形式构建从而允许在内核运行时被按需加载。因此,新内核的配置文件可能有新的但是旧内核的配置文件没有的选项,同时新内核的配置文件可能没有旧内核的配置文件现有的选项。

要处理配置文件的更改,旧内核的配置文件需要转换为可用于新内核的配置。本文展示了如何通过转换旧内核的配置文件来制作一个新内核。

Kernel upgrade in Gentoo involves these steps:

  • Step 1: Emerging the new kernel sources.
  • Step 2: Setting the right symlink to the new kernel.
  • Step 3: Moving to the new kernel's folder.
  • Step 4: Adjusting the .config file for the options introduced to, or removed from the new kernel's configuration.
  • Step 5: Building the kernel and the initramfs.
  • Step 6: Updating the bootloader.
  • Step 7: Removing or keeping the old kernel.


Important
It is wise to make a backup of the kernel configuration so that the previous configurations are not lost. Many users devote considerable time to figure out the best configuration for the system, and losing that information is definitely not wanted. One of the ways introduced in Copy the previous kernel configuration of this article can be used for making a backup of the configuration file.

Emrege 新的内核源码

当安装新的内核源代码后,升级您的内核或许是一个很棒的主意。当您运行以下命令时,新的内核源代码有时会随着系统的更新而安装。

root #emerge --ask --update --deep --with-bdeps=y --newuse @world

当然,也可以使用下面的命令直接安装内核(用任何正在使用的内核版本替换“gentoo-sources”):

root #emerge --ask --update --deep --with-bdeps=y --newuse sys-kernel/gentoo-sources

安装新的内核源代码并不会给使用者提供一个新的内核。从新内核源代码构建和安装一个新的内核,然后重启系统从而真正的运行新的内核是有必要的。

设置一个符号链接到新的内核源代码

The kernel configuration is saved in a file named .config in the directory that holds the kernel sources, a symlink is used to point to that directory.

符号链接 /usr/src/linux 应该始终指向正在运行的内核的源代码的目录。下面三种方法中任意一个可以实现这一点:

  1. 安装内核源代码时使用 USE="symlink"
  2. 用 eselect 设置链接
  3. 手动更新符号链接

默认:使用 eselect 设置链接

eselect 设置链接:

user $eselect kernel list
Available kernel symlink targets:
 [1] linux-3.14.14-gentoo *
 [2] linux-3.16.3-gentoo

这将输出可用的内核源代码。星号表示已经选择的来源。

要更改内核源,例如选第二个选项:

root #eselect kernel set 2

备选方案 1:手动更新符号链接

手动设置符号链接:

root #ln -sf /usr/src/linux-3.16.3-gentoo /usr/src/linux
user $ls -l /usr/src/linux
lrwxrwxrwx 1 root root 19 Oct  4 10:21 /usr/src/linux -> linux-3.16.3-gentoo

备选方案 2:安装使用 USE Flag 的内核源代码

这将会让 /usr/src/linux 指向新安装的内核源代码。

如果有必要的话,也可以选择下面两种方法之一来进行稍后修改。

移动到新的内核文件夹

现在已修改符号链接,将工作目录更改为新的内核文件夹。

user $cd /usr/src/linux
附注
即使在修改符号链接时工作目录已经是/usr/src/linux,此命令仍然是必需的。在实际遵循新的符号链接之前,控制台仍将位于内核的目录中。

对新内核调整 .config 文件

复制以前的内核配置文件

旧内核的配置需要被复制到新的内核中。还可以在多个位置找到旧配置:

  • 如果在当前内核中选项Enable access to .config through /proc/config.gz(CONFIG_IKCONFIG_PROC)是激活的,配置文件在procfs 文件系统:
root #zcat /proc/config.gz > /usr/src/linux/.config
  • 从老内核中提取。仅当老内核使用CONFIG_IKCONFIG被编译时可用:
root #/usr/src/linux/scripts/extract-ikconfig /path/to/old/kernel >/usr/src/linux/.config
  • /boot 目录里获取,如果你把配置文件安装到了这里:
root #cp /boot/config-3.14.14-gentoo /usr/src/linux/.config
  • 当前运行的内核的源代码目录:
root #cp /usr/src/linux-3.14.14-gentoo/.config /usr/src/linux/
  • In the /etc/kernels/ directory, if SAVE_CONFIG="yes" is set in /etc/genkernel.conf and genkernel was previously used:
root #cp /etc/kernels/kernel-config-x86_64-3.14.14-gentoo /usr/src/linux/.config

更新 .config 文件

Note
Invoking make oldconfig and make menuconfig can be done automatically via genkernel in the build process by enabling the OLDCONFIG and MENUCONFIG parameters in /etc/genkernel.conf. If OLDCONFIG is enabled in genkernel's configuration or if it's going to be enabled by passing --oldconfig option to genkernel command, jump to the build section in this article.

要使用适用于新内核的旧内核的配置,需要转换旧内核的配置。转换可以通过运行 make silentoldconfig 或者 make olddefconfig。只用其中一个,不要都使用。

make oldconfig

重要
make silentoldconfig从linux 4.19版本开始被删除,它将被make syncconfig取代。

下面的配置是基于类似文本的配置 make config 。 对于新的配置,它提供选择。例如:

user $cd /usr/src/linux
root #make oldconfig
Anticipatory I/O scheduler (IOSCHED_AS) [Y/n/m/?] (NEW)

字符串“(NEW)”在该行结束时标记该选项是新的。左边的方括号中的字符串是可能的答案:“Y”是,“N”不是,“m”模块,或者“?”显示帮助。推荐(或者说是默认)答案是大写(“Y”)。帮助说明解释了选项或驱动程序。

可惜 make silentoldconfig 不会在每一个选项显示太多的信息,例如上下文,因此有时候做出正确的选择比较困难。这种情况下最好的办法就是记住选项的名字,之后通过 配置工具 重新修正。

制作旧的配置 (make olddefconfig)

如果所有的新配置选项都应该被设置成推荐 (也就是default)值就用这条命令 make olddefconfig:

user $cd /usr/src/linux
root #make olddefconfig

make help

使用 make help 查看其他可用的转换方法:

user $make help

Observing the difference

A diff tool can be used to compare the old and new .config files to see what options have been added:

user $diff <(sort .config) <(sort .config.old) | awk '/^<.*(=|Linux)/ { $1=""; print }'
CONFIG_64BIT_TIME=y
CONFIG_AMD_NB=y
CONFIG_ARCH_CLOCKSOURCE_INIT=y
CONFIG_ARCH_HAS_GIGANTIC_PAGE=y
CONFIG_ARCH_HAS_PTE_DEVMAP=y
CONFIG_ARCH_HAS_SET_DIRECT_MAP=y
CONFIG_ARCH_STACKWALK=y
CONFIG_ARCH_USE_MEMREMAP_PROT=y
CONFIG_BLK_PM=y
CONFIG_CC_CAN_LINK=y
CONFIG_CC_DISABLE_WARN_MAYBE_UNINITIALIZED=y
CONFIG_CC_HAS_ASM_INLINE=y
CONFIG_CC_HAS_KASAN_GENERIC=y
CONFIG_CC_HAS_WARN_MAYBE_UNINITIALIZED=y
CONFIG_CPU_SUP_AMD=y
CONFIG_CPU_SUP_HYGON=y
CONFIG_CPU_SUP_ZHAOXIN=y
CONFIG_CRYPTO_LIB_AES=y
CONFIG_CRYPTO_LIB_ARC4=y
CONFIG_CRYPTO_LIB_DES=y
CONFIG_CRYPTO_LIB_SHA256=y
CONFIG_DEBUG_MISC=y
CONFIG_DRM_I915_FORCE_PROBE=""
CONFIG_DRM_I915_SPIN_REQUEST=5
CONFIG_DRM_I915_USERFAULT_AUTOSUSPEND=250
CONFIG_DYNAMIC_EVENTS=y
CONFIG_EFI_EARLYCON=y
CONFIG_GCC_PLUGINS=y
CONFIG_GENERIC_GETTIMEOFDAY=y
CONFIG_HAVE_ARCH_JUMP_LABEL_RELATIVE=y
CONFIG_HAVE_ARCH_STACKLEAK=y
CONFIG_HAVE_ASM_MODVERSIONS=y
CONFIG_HAVE_EISA=y
CONFIG_HAVE_FAST_GUP=y
CONFIG_HAVE_FUNCTION_ARG_ACCESS_API=y
CONFIG_HAVE_GENERIC_VDSO=y
CONFIG_HAVE_MOVE_PMD=y
CONFIG_HAVE_PCI=y
CONFIG_INIT_STACK_NONE=y
CONFIG_IO_URING=y
CONFIG_KASAN_STACK=1
CONFIG_LEDS_TRIGGER_AUDIO=y
CONFIG_LSM="lockdown,yama,loadpin,safesetid,integrity"
CONFIG_MMC_SDHCI_IO_ACCESSORS=y
CONFIG_NETFILTER_XT_TARGET_MASQUERADE=m
CONFIG_NET_VENDOR_GOOGLE=y
CONFIG_NET_VENDOR_MICROCHIP=y
CONFIG_NET_VENDOR_PENSANDO=y
CONFIG_NET_VENDOR_XILINX=y
CONFIG_NF_NAT=m
CONFIG_NF_NAT_MASQUERADE=y
CONFIG_NVMEM_SYSFS=y
CONFIG_OPTIMIZE_INLINING=y
CONFIG_POWER_SUPPLY_HWMON=y
CONFIG_PROC_PID_ARCH_STATUS=y
CONFIG_SKB_EXTENSIONS=y
CONFIG_SND_INTEL_NHLT=y
CONFIG_UBSAN_ALIGNMENT=y
CONFIG_UNIX_SCM=y
CONFIG_USB_AUTOSUSPEND_DELAY=2
CONFIG_X86_ACPI_CPUFREQ_CPB=y
CONFIG_X86_MCE_AMD=y
# end of Gentoo Linux
# Linux/x86 5.4.0-gentoo Kernel Configuration

And which have been removed:

user $diff <(sort .config) <(sort .config.old) | awk '/^>.*(=|Linux)/ { $1=""; print }'
CONFIG_ANON_INODES=y
CONFIG_ARCH_DISCARD_MEMBLOCK=y
CONFIG_ARCH_HAS_SG_CHAIN=y
CONFIG_ARCH_HAS_ZONE_DEVICE=y
CONFIG_ARCH_SUPPORTS_OPTIMIZED_INLINING=y
CONFIG_ARM_GIC_MAX_NR=1
CONFIG_AUDIT_TREE=y
CONFIG_AUDIT_WATCH=y
CONFIG_BACKLIGHT_LCD_SUPPORT=y
CONFIG_CRYPTO_WORKQUEUE=y
CONFIG_DEFAULT_CFQ=y
CONFIG_DEFAULT_IO_DELAY_TYPE=0
CONFIG_DEFAULT_IOSCHED="cfq"
CONFIG_DEFAULT_SECURITY=""
CONFIG_DMA_DIRECT_OPS=y
CONFIG_FB_BACKLIGHT=y
CONFIG_GENERIC_HWEIGHT=y
CONFIG_HAVE_DEBUG_STACKOVERFLOW=y
CONFIG_HAVE_GENERIC_GUP=y
CONFIG_HAVE_MEMBLOCK=y
CONFIG_IA32_AOUT=y
CONFIG_INET6_XFRM_MODE_BEET=y
CONFIG_INET6_XFRM_MODE_TRANSPORT=y
CONFIG_INET6_XFRM_MODE_TUNNEL=y
CONFIG_INET_XFRM_MODE_TRANSPORT=y
CONFIG_INET_XFRM_MODE_TUNNEL=y
CONFIG_IO_DELAY_TYPE_0X80=0
CONFIG_IO_DELAY_TYPE_0XED=1
CONFIG_IO_DELAY_TYPE_NONE=3
CONFIG_IO_DELAY_TYPE_UDELAY=2
CONFIG_IOSCHED_CFQ=y
CONFIG_IOSCHED_NOOP=y
CONFIG_MAY_USE_DEVLINK=y
CONFIG_NO_BOOTMEM=y
CONFIG_RWSEM_XCHGADD_ALGORITHM=y
CONFIG_X86_DEV_DMA_OPS=y
# Linux/x86 4.19.86-gentoo Kernel Configuration

Alternatively, the kernel provides a script to cleanly compare two config files even if the options have moved in the file itself:

user $/usr/src/linux/scripts/diffconfig .config.old .config

make menuconfig

The options can then be researched and changed if necessary by running:

root #make menuconfig

The menuconfig target is helpful because it safely handles kernel symbol dependency resolution.

编译

重要
当外部内核模块安装 (如 NVIDIA 或 ZFS),在制作内核前可能需要运行 make modules_prepare这里 有它的描述。一些模块在内核构建之前不能被安装或准备好。
重要
别忘了重新配置 bootloader 去登记新内核的名称。如果使用了initramfs,请重新制作initramfs

这一步请参考 手动配置

自动构建和安装

可以使用Portage钩子自动构建和安装新出现的内核。虽然其他方法也是可行的,但以下内容基于genkernel和gentoo-sources包。它需要以下先决条件:

  1. genkernel all能够构建和安装内核到/usr/src/linux符号链接指向的$BOOTDIR和引导程序。
  2. 为内核ebuild设置了symlink use标志。

如果满足这些要求,只需安装post_pkg_postinst Portage钩子,如下所示。

FILE /etc/portage/env/sys-kernel/gentoo-sourcesAutomated kernel build and installation portage hook
post_pkg_postinst() {
	CURRENT_KV=$(uname -r)
	# Check to see if genkernel has been run previously for the running kernel and use that config
	if [[ -f "${EROOT}/etc/kernels/kernel-config-${CURRENT_KV}" ]] ; then
		genkernel --kernel-config="${EROOT}/etc/kernels/kernel-config-${CURRENT_KV}" all
	elif [[ -f "${EROOT}/usr/src/linux-${CURRENT_KV}/.config" ]] ; then # Use latest kernel config from current kernel
		genkernel --kernel-config="${EROOT}/usr/src/linux-${CURRENT_KV}/.config" all
	elif [[ -f /proc/config.gz ]] ; then # Use known running good kernel
		zcat /proc/config.gz >> "${EROOT}/tmp/genkernel.config"
		genkernel --kernel-config="${EROOT}/tmp/genkernel.config" all
		rm "${EROOT}/tmp/genkernel.config"
	else # No valid configs known
		genkernel all
	fi
}

重新安装外部内核模块

附注
如果构建整个内核,则不需要modules_prepare步骤,因为此功能是作为标准过程的一部分完成的。

任何外部的内核模块,例如二进制内核模块, 需要为新的内核重新编译。如果新内核还没有编译,需要首先为编译外部内核模块做准备:

root #make modules_prepare

包含内核模块的包可以使用@module-rebuild命令重新编译:

root #emerge --ask @module-rebuild

解决编译的问题

当重新编译当前内核的时候遇到问题,这也许有助于净化内核源代码。确保首先备份了.config文件,因为操作过程会删除它。确保备份文件不要以 .bak 或者 ~ 为后缀,因为 make distclean 同样会删除这些文件。

root #cp .config /usr/src/kernel_config_bk
root #make distclean
root #mv /usr/src/kernel_config_bk .config

Update the bootloader

The upgraded and built kernel needs to be set up and eventually a bootloader or boot item updated, see Kernel/Configuration#Setup. Users of Grub can use the method below, users of other bootloaders must consult the handbook.

After making sure /boot partition is mounted,

Using grub-mkconfig

The following command can be executed for updating grub's configuration file:

root #grub-mkconfig -o /boot/grub/grub.cfg

Using systemd-boot-gen to update systemd-boot UEFI configuration

Warning
Installing from the cargo, the Rust package manager, should be done at one's own risk. The Gentoo project cannot what is built from it and could contain security risks particularly at the bootloader level. This could mean installing a rootkit if compromised. Proceed with the following section with caution.

Manual installation of systemd-boot-gen:

user $cargo install systemd-boot-gen

Copy the kernel parameters to /etc/default/cmdline, the file should contain:

FILE /etc/default/cmdline
CMDLINE="<kernel command line parameters>"

And run it using root privileges:

user $sudo ~/.cargo/bin/systemd-boot-gen

This will generate the boot configuration in /boot/loader/entries/ for all the available /boot/vmlinuz-* that have a matching /boot/initramfs-*.

Keeping or removing the old kernel

Keeping the old kernel sources

Kernel sources will eventually become unsupported. Some packages require the current sources to build. To protect the new sources from being removed by a depclean, it can be added to the world file (/var/lib/portage/world) by:

root #emerge --noreplace sys-kernel/gentoo-sources:newsource.version.here

Old sources that are no longer supported can be removed from the world file by:

root #emerge --deselect sys-kernel/gentoo-sources:unwanted.version.here

移除旧的内核

参考 移除旧的内核

另请参阅

外部资源