从0编译内核

mac2025-04-09  11

编译器永久生效的方法

如需永久修改,请修改用户配置文件, Ubuntu系统下,修改如下:

vim ~/.bashrc

在行尾添加或修改:

export ARCH=arm export CROSS_COMPILE=arm-openstlinux_weston-linux-gnueabi- export PATH=$PATH:/home/my-linux/STM32MPU_workspace/STM32MP15-Ecosystem-v1.1.0/Developer-Package/SDK/sysroots/x86_64-openstlinux_weston_sdk-linux/usr/bin/arm-openstlinux_weston-linux-gnueabi/

设置完成后执行 source ~/.bashrc 才能使其生效

一键配置开发环境脚本,在Ubuntu终端下直接执行,即可配置好Linux开发环境依赖(包含tftpp nfs samba/不包含交叉编译工具链 ARCH CROSS_COMPILE设置)。 wget --no-check-certificate -O Configuring_ubuntu.sh https://raw.githubusercontent.com/100askTeam/DevelopmentEnvConf/master/Configuring_ubuntu.sh && sudo chmod +x Configuring_ubuntu.sh && sudo ./Configuring_ubuntu.sh

编译预览:

1.配置

 make ARCH=arm O="$PWD/../build" multi_v7_defconfig fragment*.config for f in `ls -1 ../fragment*.config`; do scripts/kconfig/merge_config.sh -m -r -O $PWD/../build $PWD/../build/.config $f; done yes ' '   | make ARCH=arm oldconfig O="$PWD/../build"

2.编译 make ARCH=arm uImage vmlinux dtbs LOADADDR=0xC2000040 O="$PWD/../build" -j8 

3.拷贝到目录 cp  ../build/arch/arm/boot/uImage  ~/tftpboot  cp  ../build/arch/arm/boot/dts/*.dtb  ~/tftpboot 

If you have never configured your git configuration, run the following commands:     $> git config --global user.name "your_name"     $> git config --global user.email "your_email@example.com"

一.详细步骤

 

1.安装OpenSTLinux SDK .

编译内核需要安装: a. libncurses 和 libncursesw 设备包     Ubuntu: sudo apt-get install libncurses5-dev libncursesw5-dev  b.mkimage      Ubuntu:sudo apt-get install u-boot-tools  

创建标准内核源码文件夹或补丁文件

1)从官网下载内核源码

2)mkdir direct建立一个源码目录

3)解压内核源码

 $> tar xfz <kernel source>.tar.gz     or     $> tar xfj <kernel source>.tar.bz2     or     $> tar xfJ <kernel source>.tar.xz

4) 进入标准源码文件夹

cd <directory to kernel source code> 5)如果不用git管理源码b并且有补丁文件,用下面命令打补丁

for p in `ls -1 <path to patch>/*.patch`; do patch -p1 < $p; done

首先列出补丁目录的全部补丁文件,然后开始打补丁

如果用git管理源码,用下面命令

git clone https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git     $> cd <kernel source>     $> git checkout -b WORKING INVALID     $> for p in `ls -1 <path to patch>/*.patch`; do git am $p; done   NB: this way is slightly slower than the tarball extraction but you get       advantage of all git history.  

二.配置内核源码(推荐在源码树外面目录编译)

2.1  有两种方式配置和编译内核源码

1)在源码树目录外的目录编译:优点

a.避免通过编译生成和一些源码目录文件混淆的文件

b.如果要移除编译生成的所有文件,   非常方便去移除这些目录

c.在不同的目录下编译不同的配置文件

2.2 配置步骤

1)进入源码目录

> cd  <directory to kernel source code>

2)创建编译输出的目录

> mkdir  -p  ../build

3)在源码目录进行编译

a.输出在build目录

> make   ARCH=arm O="$PWD/../build"  multi_v7_defconfig fragment*.config

O="$PWD/../build":表示把输出文件放在当前路径的上一级目录build

ARCH=arm:指定架构是arm

如果还有一些模块碎片需要配置用下面命令循环配置

>for f in `ls -1 ../fragment*.config`; do scripts/kconfig/merge_config.sh -m -r -O $PWD/../build $PWD/../build/.config $f; done  > yes '' | make ARCH=arm oldconfig O="$PWD/../build"

b.输出在当前源码树目录编译配置

> cd   <directory to kernel source code>

> make   ARCH=arm  multi_v7_defconfig   fragment*.config

> for f in `ls -1 ../fragment*.config`; do scripts/kconfig/merge_config.sh -m -r .config $f; done > yes '' | make ARCH=arm oldconfig

注意:提供两种不同形式的模块碎片     *官方碎片:(fragment-xxx.config)     * 作为例子可以供选择的模块碎片 (optional-fragment-xxx.config) ,想去加入一个特定功能而不是默认的功能     模块碎片申请的顺序有模块碎片文件名字决定比如(fragment-001, fragment-002, e.g.).     请一定要注意你选择的模块的名字,确保你选择一个正确的功能

 

三.编译内核源码

必须从已经有配置好文件的目录进行编译(例如 这个文件夹包含 '.config' 文件).

用指定目录编译的方式去更好管理源代码的的改变  (同样所有编译的文件将会在指定的文件目录中).

3.1在build指定目录编译(指定)

1)进入编译目录build

 $> cd <build directory>

2)编译镜像设备树    * Build kernel images (uImage and vmlinux) and device tree (dtbs)     $> make ARCH=arm uImage vmlinux dtbs LOADADDR=0xC2000040 O="$PWD/../build"

3)编译内核模块     $> make ARCH=arm modules O="$PWD/../build"  

4)生成输出文件     $> make ARCH=arm INSTALL_MOD_PATH="$PWD/../build/install_artifact" modules_install O="$PWD/../build"     $> mkdir -p $PWD/../build/install_artifact/boot/     $> cp $PWD/../build/arch/arm/boot/uImage $PWD/../build/install_artifact/boot/     $> cp $PWD/../build/arch/arm/boot/dts/st*.dtb $PWD/../build/install_artifact/boot/

3.2.在build目录编译

    $> cd <build directory>     * 编译内核镜像(uImage and vmlinux) 和设备树(dtbs)     $> make ARCH=arm uImage vmlinux dtbs LOADADDR=0xC2000040     * 编译内核模块     $> make ARCH=arm modules     * 生成输出文件     $> make ARCH=arm INSTALL_MOD_PATH="$PWD/../build/install_artifact" modules_install     $> mkdir -p $PWD/../build/install_artifact/boot/     $> cp $PWD/../build/arch/arm/boot/uImage $PWD/../build/install_artifact/boot/     $> cp $PWD/../build/arch/arm/boot/dts/st*.dtb $PWD/../build/install_artifact/boot/

 

3.3直接在源码目录编译

 

1)进入当前源码树目录     $> cd <directory to kernel source code>  2)编译内核镜像(uImage and vmlinux) 和设备树(dtbs)

$> make ARCH=arm uImage vmlinux dtbs LOADADDR=0xC2000040

3)编译模块驱动     $> make ARCH=arm modules     * Generate output build artifacts

4)创建输出文件     $> make ARCH=arm INSTALL_MOD_PATH="$PWD/install_artifact" modules_install     $> mkdir -p $PWD/install_artifact/boot/     $> cp $PWD/arch/arm/boot/uImage $PWD/install_artifact/boot/     $> cp $PWD/arch/arm/boot/dts/st*.dtb $PWD/install_artifact/boot/  

四.在Linux板子上更新软件

通过网络进行更新,也就是ssh远程挂接

1.内核+设备树更新

1)进入安装目录路径

$> cd   <path to install_artifact dir>/install_artifact

2)挂接远程主机到本机目标目录

$> ssh   root@<ip of board> mount <device corresponding to bootfs>   /boot

把设备相应目录通过板子ip把板子系统安装到本地系统/boot目录

3)拷贝当前目录,就是把本机boot目录的所有东西拷贝到<板子ip>这个服务器的/boot目录下

$> scp -r boot/*   root@<ip of board>:/boot/   

4)卸载挂载的目录

//exit退出当前登录

$> ssh root@<ip of board> umount   /boot

2.内核模块kernel modules

1)进入安装路径下面的install_artifact

$> cd  <path to install_artifact dir>/install_artifact 

2)移除install_artifact/lib/modules/<kernel version>/目录下的链接

>rm lib/modules/<kernel version>/source lib/modules/<kernel version>/build

3)有选择的卸载内核驱动模块

find . -name "*.ko" | xargs $STRIP --strip-debug --remove-section=.comment --remove-section=.note --preserve-dates

4)复制内核模块

$> scp -r lib/modules/* root@<ip of board>:/lib/modules/ 5)生成模块依赖列表(modules.dep)和符号列表,模块提供列表符号(modules.symbols):

$> ssh root@<ip of board> /sbin/depmod -a

  将磁盘上的数据与内存同步

$> ssh root@<ip of board> sync 重启板子以便去更新t $> ssh root@<ip of board> reboot

 

 

 

最新回复(0)