Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>
init()

5.7. init()

커널의 부팅에 필요한 기본 초기화(CPU, 메모리 등)가 끝나면 init 프로세스가 만들어지고 시스템에 존재하는 다른 하드웨어 등을 초기화 한다음 루트 디바이스를 찾아 나머지 부팅을 시작한다. init 프로세스는 1번 프로세스 번호를 갖는다.

static int init(void * unused)
{
	lock_kernel();
(1)
	do_basic_setup();

(2)
	prepare_namespace();

	/*
	 * Ok, we have completed the initial bootup, and
	 * we're essentially up and running. Get rid of the
	 * initmem segments and start the user-mode stuff..
	 */
(3)
	free_initmem();
	unlock_kernel();

(4)
	if (open("/dev/console", O_RDWR, 0) < 0)
		printk("Warning: unable to open an initial console.\n");

	(void) dup(0);
	(void) dup(0);
	
	/*
	 * We try each of these until one succeeds.
	 *
	 * The Bourne shell can be used instead of init if we are 
	 * trying to recover a really broken machine.
	 */

	if (execute_command)
		execve(execute_command,argv_init,envp_init);
(5)
	execve("/sbin/init",argv_init,envp_init);
	execve("/etc/init",argv_init,envp_init);
	execve("/bin/init",argv_init,envp_init);
	execve("/bin/sh",argv_init,envp_init);
(6)
	execve("/sbin/init",argv_init,envp_init);
	panic("No init found.  Try passing init= option to kernel.");
}

(1)
여기 까지의 상태는 시스템이 이제 사용 가능한 상태까지는 왔지만 붙어 있는 다른 모든 디바이스에 대해선 초기화가 되지 않은 상태다. CPU 하부 시스템, 메모리 그리고 프로세스 관리는 동작하는 상태다.

이제 할 것은 나머지 디바이스 들을 모두 초기화 하는 일을 하는 것이다. 초기화 하는 목록은 다음과 같다. 이외에 더 있으나 중요한 것만 조금 간추렸다.

mtrr

현재는 i386에서만 존재 하는 기능으로 MTRR(Memory Type Range Register)를 말한다. PCI나 AGP 비디오 카드를 좀더 빨리 쓸 수 있도록 해준다.

sysctrl

proc file system을 사용하도록 설정 되어 있으면 이를 초기화 해준다.

pci

PCI 시스템을 초기화 한다. PCI의 루트 디바이스를 초기화 하고 이어 PCI 버스에 연결된 모든 다른 하드웨어를 찾아 리스트에 등록한다.

isapnp

ISA 버스에 물려 있는 PnP 디바이스를 초기화한다.

socket

사용되는 프로토콜을 초기화 한다. 소켓 용 슬랩도 초기화 하고 netlink, netfileter 등도 초기화 한다.

context thread

keventd를 kernel thread로 실행한다.

pcmcia

PCMCIA 디바이스 초기화 한다.

(2)
무엇을 어디서 마운트할 지 결정한다. 루트 디바이스를 마운트하고 램디스크를 읽어 들이는 일도 한다.
(3)
바로 전까지 실행되면 이제 커널이 완전히 부팅한 것으로 봐도 된다. 커널 부팅에 사용된 메모리 중 필요 없는 것을 반환한다.
(4)
초기 콘솔을 열고 stdin, stdout, stderr을 open 한다.
(5)
이제 마운트된 루트 파일 시스템에서 init를 찾아 실행해 준다.
(6)
init를 찾지 못하면 여기와서 커널의 부팅이 멈춘다. 여기 까지 온다는 것은 아마도 루트 파일 시스템을 마운트하지 못했거나 루트 파일 시스템에 init가 없기 때문일 것이다.