How Is the Init Process Started in the Linux Kernel

What is the first process a typical Linux kernel starts?

Typically it is the init process, the path of which is hard coded into the kernel itself. init performs very low level functions like starting upstart in the case of Ubuntu (prior to 15.40) or systemd in the case of Ubuntu 15.04 and later, Arch, Fedora, and others, which load the remaining processes and setup. Note that the system is not done booting when init runs - that is a common misconception. In fact, init sets up your login screen and other related tasks. Here's a WikiPedia page on init: https://en.wikipedia.org/wiki/Linux_startup_process#SysV_init

Init is the father of all processes. Its primary role is to create processes from a script stored in the file /etc/inittab. This file usually has entries which cause init to spawn gettys on each line that users can log in. It also controls autonomous processes required by any particular system. A run level is a software configuration of the system which allows only a selected group of processes to exist. The processes spawned by init for each of these run levels are defined in the /etc/inittab file.

However, the Linux kernel does start the scheduler but it is not in userspace, which is what most people associate as the home for a process. Also, the Bourne Shell (/bin/sh) can be substituted if the init is missing or cannot be called. You can also in theory substitute it for any executable by using the init=*some path here* Linux kernel boot option.

Where does task_struct get initialized in the Linux kernel?

Since the only way to create a new process in Linux is through the clone() syscall (or other variants like fork()), there is no real function to "create a new task" from scratch, but there sure is a function to duplicate an existing task, applying the needed modifications. The function used for this is copy_process(), which uses dup_task_struct() to duplicate the associated struct task_struct.

There is however one special exception to this rule, the init process (the first process created after booting) is created by the kernel itself (every other process is then created by init or by some child of init through clone() + execve()). The task_struct for the init task is statically defined at compile time (see here). You can look at this other answer if you want to know more.

C files for init ELF executable?

Where are the C files available in linux(I use Ubuntu) that generate init executable?

It depends which package provides the init executable. In a default Ubuntu Utopic install, /sbin/init is a symlink to /sbin/upstart, so you would want to find the source to upstart. A simple search with your favorite search engine will probably point you to the Upstart page, which includes links to source downloads and the VCS repository.

You can also use the apt-get source command to download the sources for a particular package. You also need the dpkg-dev package installed (apt-get install dpkg-dev), after which you can run:

apt-get source upstart

After which you will have:

root@ubuntu:~# ls upstart-1.13.2/
ABOUT-NLS config.guess contrib extra ltmain.sh po TODO
aclocal.m4 config.h.in COPYING HACKING m4 README util
AUTHORS config.rpath dbus init Makefile.am README.tests
ChangeLog config.sub debian INSTALL Makefile.in scripts
compile configure depcomp install-sh missing test
conf configure.ac doc lib NEWS test-driver

On Ubuntu Vivid, /sbin/init is a symlink to /lib/systemd/systemd, so you will want to get the systemd sources.

Note that both upstart and systemd operate very different from the legacy /sbin/init system. The documentation at the above links describes how each system operates.

How is the init called from the kernel module; how does the bootloader call init module after reaching out to the kernel /boot/vmlinuz file?

The bootloader doesn't call init. The bootloader loads and executes the kernel.

Is there a way to trace which function calls init function? What I tried so far: Tried to go through readelf and nm but couldn't trace back to the callee using them.

I'm not clear what you're trying to do here. The kernel calls /sbin/init. Maybe this question is relevant.

Boot procedure after systemd replacing init. I came to an understanding that the listening sockets are invoked first related to udev and d-bus; and then every process kick starts and get connections to these sockets. But I needed clarity in understanding how the system works.

As I said earlier, systemd didn't replace init, it replaced upstart, and upstart replaced init. Hopefully the documentation available at the systemd web site is able to help you understand how things work. If not, you will probably get better answers if you ask questions about specific things that are unclear or not behaving as you think they should.

why init process can't create dynamically?

Actually, the init_task with type struct task_struct * is defined in arch/*/kernel/init_task.c files. This struct is used early in start_kernel: http://lxr.free-electrons.com/source/init/main.c?v=3.10#L471

471 asmlinkage void __init start_kernel(void)
472 {

481 smp_setup_processor_id();

489 cgroup_init_early();

498 boot_cpu_init();
499 page_address_init();
500 pr_notice("%s", linux_banner);
501 setup_arch(&command_line);
502 mm_init_owner(&init_mm, &init_task);

So, slab allocator may be available at moment of init starting, but the task_struct of the init is used earlier, both in start_kernel and in other places (http://lxr.free-electrons.com/ident?v=3.10&i=init_task) e.g. to statically init the current_task pointer on other CPUs.

http://lxr.free-electrons.com/source/arch/x86/kernel/cpu/common.c?v=3.10#L1080

1084 DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned =
1085 &init_task;


Related Topics



Leave a reply



Submit