2019-2020-1 20199310 "Linux kernel principle and Analysis" in the fourth week of work

1. Problem Description

In the previous article, has contact with some knowledge of the Linux kernel, this will further start from the directory structure of the Linux kernel source code, to construct a simple operating system MenuOS in Oracle VM VirtualBox Linux environment, the kernel code version 3.18. 6, to solve the problem appeared different from the laboratory building, while tracking startup of the operating system through gdb.

2. settlement process

2.1 Linux kernel source directory

arch: the relevant code for storing CPU architecture.
block: Linux storage system storing blocks of code on the device management.
crypto: C language code stored common encryption algorithm.
Documentation: store some documents.
drivers: drive directory, sub-category store the driver source code for all hardware devices supported by the Linux kernel.
firmware: the firmware.
fs: file system, the implementation of various storage file system supported by Linux.
include: header file directories stored public header files.
init: storing initialization code when the Linux kernel.
lib: Putting Linux shared library files.
mm: storage memory management.
net: store the relevant code Linux network.
README: kernel file reading documents.

2.2 System configuration MenuOS

2.2.1 source download

cd 20199310            #进入20199310目录
mkdir LinuxKernel            #创建LinuxKernel目录            
cd LinuxKernel            #进入Linux目录
wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.6.tar.xz            #在官网下载内核源码,版本未3.18.6
xz -d linux-3.18.6.tar.xz            #解压压缩包得到linux-3.18.6.tar
tar -xvf linux-3.18.6.tar            #解压压缩包得到linux-3.18.6
cd linux-3.18.6            #进入linux-3.18.6目录
make i386_defconfig            #开始编译源码
make

The following error may occur during compilation:

fatal error: linux/compiler-gcc6.h:没有那个文件或目录

The reason is that the system gcc compiler version is too high, is not compatible with the kernel source code, you need to download compiler-gcc6.h placed under the include / linux directory from the Internet, because the compiler-gcc6.h and compiler-gcc5.h generally similar, so You can also copy compiler-gcc5.h rename compiler-gcc6.h, may also encounter similar error messages subsequent compilation process can be solved the same way.
FIG modifications are as follows:

2.2.2 root file system production

Back LinuxKernel directory and enter the following command:

mkdir rootfs           #创建rootfs目录            
git clone https://github.com/mengning/menu.git            #从GitHub上克隆menu
cd menu            #进入menu目录
gcc -pthread -o init linktable.c menu.c test.c -m32 -static           #编译
cd ../rootfs           #进入rootfs目录
cp ../menu/init ./            #把init复制到rootfs下
find . | cpio -o -Hnewc |grip -9 > ../rootfs.img            #把当前rootfs下的所用文件打包成一个镜像文件

When executing the gcc command, there are mistakes:

online searching, to know the installation environment is not installed gcc perfect, try sudo apt-get install gcc-multilib gcc installed to improve environmental
image file Packaging Success:

2.2.3 kernel debugging to track

Reconfigure compile the Linux kernel to make it carry debugging information, as follows:

sudo apt-get install libncurses5-dev            #安装libncurses5-dev
make menuconfig           #配置Linux内核,依次选择kernel hacking -> Conpile-time checks and compiler options -> [*]compile the kernel with debug info         
make            #开始编译

After performing the first step appears as an error

because the make menuconfig performed in the absence of full-screen, showing a window of the terminal is too small, it is necessary to enlarge the operating window or full screen.
By querying, learned can be enhanced by installing the virtual machine function, automatically adjusting resolution of a page, but also has the following six functions:
(1) realization of the mouse between the host and the smooth movement of the virtual machine
(2) to realize the virtual machine host sharing files
(3) supports seamless pattern
(4) and the virtual machine host to exchange data, monitoring client can start a program in the client
(5) for time synchronization with the host
(6) virtual machine and the host share clipboard contents, that can be copied directly between the host, the client, paste
the virtual machine running window bar click on the device - after> installation enhancements, you can restart automatically adapt to the resolution by changing the zoom window.
After adjusting the size of the window into the Kernel Configuration, in accordance with the following steps:




Finally a make compiling a long time, after completion of the translation start the debugging process can track the Linux kernel.

2.3 trace debugging Linux kernel boot process

qemu -kernel linux-3.18.6/arch/x86/boot/bzImage -initrd rootfs.img -S -s            #开启内核

At this time, the sudden appearance of an error message qemu command can not be found, using sudo apt-get install qemu after you install the update found that the problem is still not resolved, found by querying data, qemu command set installation instructions sudo apt-get install qemu-system , which is related to further comprising command qemu-system-arm start emulation chip arm, qemu-system-arm -M viewing platform which supports the arm (boards), qemu-system-arm -cpu View support arm which cpu, qemu-img format of virtual client mapping image, additional storage devices in a storage network and the like, only one of which is QEMU .
Into the core, since it is frozen -S parameters, shown below:

Another parameter -s means creating a gdb-server on port.
With ctrl + shift + t open another terminal window and at LinuxKernel gdb, enter the following code:

gdb            #进入gdb
file linux-3.18.6/vmlinux            #选择调试文件
target remote:1234            #用1234这个端口进行链接
break start_kernel            #在start_kernel()设置断点
c            #运行至第一个断点
break rest_init           #在rest_init ()设置断点
c            #运行至第二个断点
c            #运行剩余启动代码

Results are as follows:



the main code is run by the kernel arch / main.c, wherein start_init () corresponding to the general main.c main function is started running kernel, the hardware for performing system initialization process init_task 0 to task_struct type, a process descriptor, rest_init () and used New kernel_init kthreadd kernel threads.

3. Summary

This paper studied the directory structure of the Linux kernel source code, and construct a simple operating system Oracle VM VirtualBox MenuOS in the Linux environment, to solve many problems in the process. When you configure and compile the Linux kernel, due to the different experimental environment is often a lack of command files or not installed; problems encountered in the make menuconfig find for a long time, starting with a shortcut key to toggle full-screen mode, mode and seamless scale mode , found that the actual resolution is not changed, and then modify the configuration file by way of command, after the restart has not been successful, and finally found the installation enhancements such a powerful way to find a resolution to modify. In short, this experiment still makes me a lot.

Guess you like

Origin www.cnblogs.com/louhao-20199310/p/11620795.html