【OS】实验一:openEuler安装与内核编译

6,804 阅读1分钟

实验一:openEuler安装与内核编译

使用华为云进行实验

image-20211224192753237

一、系统备份

 cd ~
 yum install lrzsz      # rz和sz可以在终端下很方便的传输文件
 tar czvf boot_origin.tgz /boot/
 sz boot_origin.tgz    # 将备份文件发送到本地

二、内核源码下载

wget https://gitee.com/openeuler/kernel/repository/archive/5.10.0-13.0.0.zip

image-20211223221121613

解压:

unzip 5.10.0-13.0.0.zip

进入源码根目录:

image-20211223222112959

三、编译内核

  1. 进入解压好的源码文件夹执行命令,清理过去内核编译产生的文件,第一次编译时可不执行此命令
cd kernel-5.10.0-8.0.0
make mrproper
  1. 生成内核配置文件.config
cp -v /boot/config-$(uname -r) .config

执行依赖安装

yum install ncurses-devel

然后使用make menuconfig 对配置进行需要的更改,决定将内核的各个功能系统编译进内核还是编译为模块还是不编译

make menuconfig

首先载入原始数据:

image-20211223224340584

之后save保存配置文件:

image-20211223224221954

  1. make 开始安装

    产生错误:

可以在makefile中将date-time的error提示去除即可:

image-20211223231911536

image-20211223231942633

make modules_install #编译完成后安装模块
make install #安装内核

img

  1. 重启系统

    reboot # 查看内核版本信息
    uname -a #查看内核版本信息可分析出内核是否更新成功
    

    img

四、内核模块编程

1 创建文件夹task1

mkdir task1

2 创建文件helloworld.c

#include<linux/module.h>
MODULE_LICENSE("GPL");
int __init hello_init(void)
{
        printk("hello init\n");
        printk("hello,world!\n");
        return 0;
}
void __exit hello_exit(void)
{
        printk("hello exit\n");
}
module_init(hello_init);
module_exit(hello_exit);

image-20211223233109094

3 编写makefile

ifneq ($(KERNELRELEASE),)
        obj-m := helloword.o
else
        KERNELDIR ?=/root/kernel-5.10.0-13.0.0
        PWD := $(shell pwd)
default:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
.PHONY:clean
clean:
        -rm *.mod.c *.o *.order *.symvers *.ko

image-20211223233240333

4 编译make指令

make

img

img

5 加载模块

insmod helloword.ko

image-20211224215713573

查看加载的内容

dmesg | tail -n 2 #可以看到打印的信息:

image-20211224215744626

 lsmod | grep main  #可以查看所有名字中包含main的内核模块:

image-20211224215847874

6 模块的卸载及查看

rmmod helloword #卸载helloworld模块
dmesg | tail -n 1 #查看卸载内容

image-20211224215934501

遇到问题

dnf 指令找不到

image-20211223214633253

使用 yum 代替:

image-20211223221259437