Linux内核升级之制作initrd.img及其new-kernel-pkg(.sh)使用

本文详细介绍了如何在Linux系统中升级内核,包括从源码复制bzImage和System.map文件、使用new-kernel-pkg脚本创建initrd-2.6.32.6.img,以及更新GRUB和LILO配置。同时,文章提供了new-kernel-pkg.sh脚本的源代码,帮助理解其工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、从编译好的源码中拷贝bzImage和System.map文件到/boot目录,并重命名。

# cp linux-2.6.32.6/arch/i386/boot/bzImage /boot/vmlinuz-2.6.32.6

# cp linux-2.6.32.6/System.map /boot/System.map-2.6.32.6

 

注意:

1)、vmlinuz和System.map的全名(包括版本号)一定要正确,要不new-kernel-pkg的脚本无法找到上述文件!

2)、编译过程中执行:make bzImage, make modules, make modules_install(拷贝*.ko文件到/lib/modules/2.6.32.6目录下)

 

2、执行命令制作initrd-2.6.32.6.img

# new-kernel-pkg --mkinitrd --depmod --install 2.6.32.6

 

3、/boot目录文件

 

4、new-kernel-pkg脚本

 

 

#!/bin/bash

#

# new-kernel-pkg

# Invoked upon installation or removal of a kernel package, the following

# tasks are/can be done here:

# creation/removal of initrd

# run of depmod/removal of depmod generated files

# addition/removal of kernel images from grub/lilo configuration (via grubby)

#

# Copyright 2002-2008 Red Hat, Inc.  All rights reserved.

#

# This program is free software; you can redistribute it and/or modify

# it under the terms of the GNU General Public License as published by

# the Free Software Foundation; either version 2 of the License, or

# (at your option) any later version.

#

# This program is distributed in the hope that it will be useful,

# but WITHOUT ANY WARRANTY; without even the implied warranty of

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

# GNU General Public License for more details.

#

# You should have received a copy of the GNU General Public License

# along with this program.  If not, see <http://www.gnu.org/licenses/>.

#

 

PATH=/sbin:/bin:$PATH

 

lilo=/sbin/lilo

 

# some defaults that are sane for most arches

kernelName=vmlinuz

 

if [ -x ./grubby ]; then

    grubby=./grubby

else

    grubby=/sbin/grubby

fi

 

[ -f /etc/sysconfig/kernel ] && . /etc/sysconfig/kernel

 

cfgGrub=""

cfgLilo=""

runLilo=""

grubConfig=""

 

ARCH=$(uname -m)

 

if [ $ARCH = 'ia64' ]; then

    liloConfig=/boot/efi/EFI/redhat/elilo.conf

    bootPrefix=/boot/efi/EFI/redhat

    liloFlag=elilo

    isx86=""

elif [ $ARCH = 'ppc64' -o $ARCH = 'ppc' ]; then

    liloConfig=/etc/yaboot.conf

    bootPrefix=/boot

    lilo=/sbin/ybin

    liloFlag=yaboot

    runLilo="yes"

    isx86=""

elif [ $ARCH = 'sparc' -o $ARCH = 'sparc64' ]; then

    liloConfig=/etc/silo.conf

    bootPrefix=/boot

    liloFlag=silo

    lilo=/sbin/silo

    isx86=""

elif [ $ARCH = 's390' -o $ARCH = 's390x' ]; then

    liloConfig=/etc/zipl.conf

    bootPrefix=/boot

    liloFlag=zipl

    lilo=/sbin/zipl

    runLilo="yes"

    isx86=""

else

    # this leaves i?86 and x86_64

    liloConfig=/etc/lilo.conf

    grubConfig=$(readlink -f /etc/grub.conf 2>/dev/null)

    bootPrefix=/boot

    liloFlag=lilo

    isx86="yes"

fi

 

mode=""

version=""

initrd=""

dracut=""

initrdfile=""

moddep=""

verbose=""

makedefault=""

package=""

mbkernel=""

mbargs=""

adddracutargs=""

addplymouthinitrd=""

 

usage() {

    echo "Usage: `basename $0` [

<think>好的,我现在需要解决用户提到的关于无法找到'/boot/initrd.img-5.4.0-100-generic'文件的问题。首先,我得弄清楚这个错误出现的背景。用户可能在尝试执行某个需要访问initrd.img文件的操作时遇到了问题,比如系统启动或者某个配置命令。initrd.img是初始化内存磁盘文件,用于在启动时加载必要的驱动和模块,缺少这个文件可能导致系统无法正常启动。 接下来,我需要回忆一下常见的解决方案。根据之前的知识库引用,用户提到了引用[2]中的initrd是initial ramdisk的缩写,用于临时引导硬件直到内核接管。所以,重建initrd文件可能是一个解决方法。通常,在Linux系统中,可以使用update-initramfs命令来重新生成initrd文件。此外,如果内核相关的包损坏,可能需要重新安装内核镜像包,比如linux-image-generic。 然后,我需要检查步骤的正确性。首先建议用户尝试重建initrd,命令是sudo update-initramfs -u -k 5.4.0-100-generic。如果这不起作用,可能需要重新安装内核包,比如使用sudo apt install --reinstall linux-image-5.4.0-100-generic。如果问题依旧存在,检查/boot分区空间是否足够,因为空间不足可能导致文件无法生成。此外,引导修复工具boot-repair也是一个可行的备选方案。 还要考虑用户可能遇到的其他问题,比如依赖包问题,这在引用[1]中提到的安装依赖包问题可能有关系。因此,在重新安装内核包之前,确保系统已经更新,运行sudo apt update和sudo apt upgrade可能会有帮助。 最后,生成相关问题时要涵盖可能导致此错误的其他原因,比如引导配置错误、内核升级失败等,以及如何预防这类问题,比如定期维护系统更新和备份重要文件。</think>### 解决方案:修复缺失的`initrd.img-5.4.0-100-generic`文件 #### 1. **重建`initrd.img`文件** - 使用`update-initramfs`工具重新生成初始化内存磁盘文件: ```bash sudo update-initramfs -u -k 5.4.0-100-generic ``` 参数说明: - `-u`:更新现有`initrd`文件。 - `-k`:指定内核版本(需与报错中的版本一致)[^2]。 - **验证**:检查`/boot`目录是否生成对应文件: ```bash ls /boot/initrd.img-5.4.0-100-generic ``` #### 2. **重新安装内核镜像包** - 若重建失败,可能是内核包损坏。重新安装对应版本的内核包: ```bash sudo apt install --reinstall linux-image-5.4.0-100-generic ``` - **依赖修复**:如果提示依赖问题,先执行: ```bash sudo apt update && sudo apt --fix-broken install ``` 引用[1]中提到的依赖包问题解决方法可辅助此步骤[^1]。 #### 3. **检查`/boot`分区空间** - 使用`df -h`命令查看`/boot`分区剩余空间: ```bash df -h /boot ``` - 若空间不足,删除旧内核文件或清理缓存: ```bash sudo apt autoremove && sudo apt clean ``` #### 4. **使用`boot-repair`工具修复引导** - 安装并运行引导修复工具: ```bash sudo add-apt-repository ppa:yannubuntu/boot-repair sudo apt update sudo apt install boot-repair boot-repair ``` - 根据工具提示选择“推荐修复”选项。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值