Home > System Tutorial > LINUX > body text

Huawei Cloud completes the compilation and installation of the Linux kernel (optional topic)

WBOY
Release: 2024-02-15 12:12:21
forward
598 people have browsed it

The experiment requires mastering the compilation and installation of the Linux kernel, mastering the basic concept design of Linux system calls and adding Linux system calls

(1) Change or return the priority (nice value and prio value) of the specified process (see textbook P328 for details) Tip: Possible reference kernel function: set_user_nice().

(2) Change the host name to a custom string (optional question)

1. Compilation and installation of the Linux kernel (use Huawei Cloud to complete the compilation and installation of the openEuler kernel)

(1) Log in to the system and check the current kernel version

[root@openEuler~]#uname-r

(2) Install tools and establish a development environment

[root@openEuler~]#yumgroupinstall-y"DevelopmentTools"

[root@openEuler~]#yuminstall-ybc

[root@openEuler~]#yuminstall-yopenssl-devel

(3) Back up the boot directory in case subsequent steps to update the kernel fail

[root@openEuler~]#tarczvfboot.origin.tgz/boot/

Save current kernel version information

[root@openEuler~]#uname–r>uname_r.log

(4) Obtain the kernel source code and decompress it

[root@openEuler~]#wget

[root@openEuler~]#unzipkernel-4.19.zip

(5)Compile kernel

[root@openEuler~]#cdkernel-kernel-4.19

[root@openEulerkernel]#makeopeneuler_defconfig

[root@openEulerkernel]#make-j4Imagemodulesdtbs

This step is to compile the image, modules and dtbs of the kernel. make-j4 means 4 thread compilation (can be adjusted according to the number of CPU cores)

(6)Install the kernel

[root@openEulerkernel]#makemodules_install

[root@openEulerkernel]#makeinstall

Note: The errors that occur during the last step "makeinstall" can be ignored here.

(7) Log in to ECS using VNC

(8) Restart the system

[root@openEulerkernel]#reboot

(9) Log in and verify

Select to boot the system with the newly compiled kernel in the VNC window

After compiling here, there is already a new kernel of version 4.19.208, select this kernel to log in

2. Master the basic concepts of Linux system calls

调用linux内核函数_linux内核调试方法总结_内核系统调用

The process of the Linux system processing system calls and the way to reduce system calls. The Linux system provides hundreds of system calls. In order to uniquely identify each system call, Linux sets a unique number for each system call, called a system call number. At the same time, each system call requires a service The interpreter completes its specific functions.

I won’t go into too much description here.

(The focus is how to add system calls!!!)

#define __NR_hello_euler 294
__SYSCALL(__NR_hello_euler, sys_hello_euler)
#undef __NR_syscalls
#define __NR_syscalls 295
Copy after login

asmlinkage long sys_hello_euler(void);
Copy after login

SYSCALL_DEFINE0(hello_euler)
{
	printk(KERN_INFO "xuehao:20273108");
	return 0;
}
Copy after login

After restart

#include 
#include 
#include 
int main()
{
ret = syscall(294);
return 0;
}
Copy after login

3. Design and add linux system calls

(1) Change or return the priority (nice value and prio value) of the specified process (see textbook P328 for details) Tip: Possible reference kernel function: set_user_nice().

#define _GNU_SOURCE
#include
#include
#include
#include
int main()
{
	pid_t pid;
	int nicevalue;
	int flag;
	int n=0;
	int p=0;
	int *prio;
	int *nice;
	prio = &p;
	nice = &n;
	printf("请输入pid: n");
	scanf("%d",&pid);
	printf("pid输入成功n请输入nice值:n");
	scanf("%d",&nicevalue);
	printf("nice输入成功n请输入flag(flag为1时修改,为0时查看):n");
	scanf("%d",&flag);
	syscall(295,pid,flag,nicevalue,prio,nice);
	printf("现在的nice为%d,prio为%dn",n,p);
	return 0;
}
Copy after login

(2) Change the host name to a custom string (optional question)

#define __NR_mysethostname 296
__SYSCALL(__NR_mysethostname,sys_mysethostname)
Copy after login

Similarly, #define__NR_syscalls296 below should be changed to #define__NR_syscalls297

4. Experiment summary

(1) Before you do the experiment, you must clearly identify whether it is x86 or arm architecture after reading the tutorial! ! ! This is where I stumbled and rebuilt Huawei Cloud three or four times

(2) If the following error occurs when logging in with VNC, congratulations, it is probably because your kernel has crashed~ My teacher said that as long as you are fast enough, you can use VNC to restart the virtual machine immediately. It is possible to log in, and I did not succeed. I could only rebuild the cloud host n times.

(3) Although the experiment report given by the teacher requires VNC login to call the Linux kernel function , I personally recommend using cloudshell to execute the command.

(4) During the initial test, the teacher suddenly asked me about the meaning and usage of the functions in the system call, and I was immediately stumbled. I will post the meaning and usage of some internal functions below.

1.find_get_pid(pid)

find_get_pid has different namespaces in the kernel. The pid value of the same process in their respective namespaces may be different. Find_get_pid is to find the real pid of the process in the kernel mode

2.set_user_nice(task,nicevalue)

Used to set the nice value of the process

3.copy_to_user()

Complete the copy from kernel space to user space, To target address linux operating system version linux system, this address is the address of user space; From source address, this address is the address of kernel space; N bytes of the data to be copied number.

If the data copy is successful, return zero; otherwise, return the number of data bytes that were not copied successfully.

4.copy_from_user()

copy_from_user copies the string pointed to by name from user space to kernel space. If it fails, it returns the number of bytes that have not been copied. If it succeeds, it returns 0.

5.down_write()

The function down_write() is called when the writer uses it to get the read semaphore sem. If the semaphore is held by the reader or writer Call the linux kernel function , then the function's The call will cause the caller to sleep and can only be used in the process context to obtain the write lock in the Linux kernel read semaphore.

6.memcpy(str1,str2,n)

Copy n bytes from storage area str2 to storage area str1.

The above is the detailed content of Huawei Cloud completes the compilation and installation of the Linux kernel (optional topic). For more information, please follow other related articles on the PHP Chinese website!

source:itcool.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!