Quality control (1)/Operating System

OS week1 install virtual box / start_kernel()

빈그레 2022. 10. 3. 21:17

 


 

Lecture 2: Installing/Reading Linux Kernel

1. What's in the linux kernel

init: start up code
   main.c : beginning of linux

kernel: cpu-indendent code for basic system management
    fork.c, exit.c, sched.c : process creation, exit, scheduling

arch : cpu-dependent code for basic system management
   arch/x86 : cpu-dependent code for intel x86 cpu
   arch/x86/boot : boot-related code
   arch/x86/kernel: low-level intel cpu code for initialization, interrupt handling, etc
        entry_32.S : code for interrupt entry point
        head_32.S : code for system initialization
        i8259_32.c : code for interrupt controller
        irq_32.c   : code for IRQ
        process_32.c : code for process control
        time_32.c : code for timer
        traps_32.c : code for exception handling
        signal_32.c : code for signal handling

fs: file system code
    open.c, read_write.c, file.c, ... : fs system call handling
    inode.c : inode handling
    fs/ext2  : ext2 file system code
    fs/ntfs   : windows NT file system code

mm : memory management code
net: network handling code
drivers: device handling code
include : include files
   inclde/asm-x86 : include files for intel cpu specific code
ipc: code for inter-process communication

2. Linux starting location: init/main.c: start_kernel()

 

1) Install Gentoo Linux on virtual machine.

1.0) Download Virtualbox from Internet and install.
1.1) Download Gentoo virtualbox files (gentoo.zip) from the I-class and un-compress it.
1.2) Run VirtualBox, and click File>Import and go to the gentoo directory. Select Gentoo2.ovf. Uncheck USB controller. Select Import. This will install Gentoo Linux on your virtual box. 
1.3) Run Gentoo. If you have an error, run VirutalBox as administrator and try again. For USB error, simply disable usb controller in "Setting" tab. For Hyper-V error (Raw-mode is unavailable), turn off Hyper-V feature in control panel>program and feature>window feature. Select My Linux. Login as root and hit Enter for the password prompt. If VirtualBox still cannot open the session, you need to look at the log file (right click on Gentoo VM and select “log file”) and see what is the error and fix it.
(In some cases, you may need to download and install virtualbox extension package.)

 

 

Gentoo on Virtual_box


단계에 따라 virtual box를 다운받고 gentoo 압축 해제 후 virtual_Box에서 열었다.
gentoo2에서 My linux로 들어갔다.

 

gentoo download error


virtual_box에서 gentoo를 불러오는 과정에서 error가 발생하였다.

gentoo 저장경로에 한글이 포함되는 경우나 저장용량 부족으로 error가 뜨는 경우가 있다하여 gentoo를 바탕화면이 아닌 inha one drive에 저장하였더니 error 없이 정상적으로 gentoo를 실행할 수 있었다.(용량부족 문제였다 뫄하하,,,)

 

 

My linux login
id : root , pw : “enter”클릭

 

 

1.4) Make a simple program, ex1.c, with vi which displays "hello world". Compile and run it.

Make simple program
vi ex1.c

command(명령어) mode에서 insert mode로 변경한 뒤에 ex1.c code file에 code 작성을 시작한다.

 

다음과 같이 "hello world"를 출력하는 코드를 작성한다.

 

esc + :wq

file을 저장하고 file에서 나온다.

 

Compile ex1.c

 

gcc -o ex1 ex1.c

ex1이라는 기계어 파일을 만들어서 ex1.c파일을 compile한다.

 

hello world가 정상적으로 출력되는 것을 확인할 수 있었다.

 


2) Go to linux-2.6.25.10 directory and find all the file referred in Section 1 such as main.c

(cd : change directory)
cd linux-2.6.25.10

ls 명령어를 통해 현재 위치에서의 하위 directory들을 확인하고 cd 명령어를 통해 하위 directory로 이동한다.

init directory 에서  main.c file 을 찾을 수 있었다.

 

상위 directory로 이동
cd ..

 

다른 파일을 찾기 위해서는 cd .. 명령어를 통해 상위 디렉토리로 이동한 뒤에 ls를 통해 해당 directory에 있는 directory 및 file들을 확인하고 찾고자하는 파일을 찾으면 된다

 

 

3) Find the location of start_kernel().

   To find a string "start_kernel", go to the linux top directory (linux-2.6.25.10) and do

    $ grep -nr "start_kernel" * | more  //띄어쓰기 주의


   Use "space" to move down the screen, "q" to exit

start_kernel () at init/main.c :395

grep을 통하여 가장 상위 directory(linux-2.6.25.10) 에서 start_kernel의 위치를 찾을 수 있었다. 

start_kernel은 init directory의 main.c file에 있음을 확인할 수 있었다.

 

"q"로 grep에서 나온다

   

Once you found the file that has "start_kernel", use "vi" to read the file.

In vi, type "/start_kernel" to search for the first instance of "start_kernel".

 For the next string, simply type "/". Repeat "/" until you find the start_kernel() function.

 

"/start_kernel"을 입력하여 파일 내에서 start_kernel을 확인한다.

"/"를 repeat하여 start_kernel을 포함한 코드를 모두 확인하며

start_kernel이 단순 호출된 부분이 아니라 구현되어 있는 부분을 찾는다.
 

 

start_kernel() function 정의

찾았다앙~!


   Use "j" to mode down the cursor, "k" to move up, "^f" to move one screen down, "^b" to move   
   up one screen up. 

 

j,k로 commnad mode를 해제하지 않고 코드 손상 없이 위 아래로 움직이며 start_kernel function 내에서 printk()함수를 통해 무엇이 출력되는지 확인하였다.

 

printk(KERN_NOTICE);
printk(linux_banner);

 

grep을 이용하여 printk의 인자로 들어있는 것들을 가장 상위 directory인 linux 2.6에서 찾았다.

 

[ KERN_NOTICE ]  

=> 정상 메세지를 의미한다.

 

 

[linux_banner]

=> init/version.c에 존재하는 char[]

 

 

4-3)Confirm your prediction with "dmesg > x" and "vi x". The kernel remembers the booting message in the system buffer and dmesg displays the content of this buffer to the screen. "dmesg > x" will send the booting message to file x. With "vi x" you can look at the file x.

dmesg를 통해 booting message를 x에 복사하였고

vi를 통해 x에 booting message가 정상적으로 복사되었음을 확인할 수 있었다.

 


5) Find the location of the following functions called in start_kernel() and briefly explain your guessing about what each function is doing (but do not copy the whole code).

 

//start_kernel함수 내부에서 호출되고 있는 함수들 분석

 trap_init(), init_IRQ(), sched_init(), time_init(), console_init(), mem_init(), rest_init()


6) Why can't we use "printf" instead of "printk" in Linux kernel?

 

Printk는 os동작중에 메시지를 출력하는 용도이며 커널모드에서만 호출이 가능하고 printf는 유저모드에서만호출이 가능합니다. prinrf함수의 경우 내부적으로 버퍼에 출력할 것을 모아두었다가 방출시키는 함수이고 printk는 버퍼없이 바로 출력하는 함수 입니다.