内核如何启动init进程
init/main.c
static int __ref kernel_init(void *unused)
{int ret;kernel_init_freeable();/* need to finish all async __init code before freeing the memory */async_synchronize_full();ftrace_free_init_mem();jump_label_invalidate_initmem();free_initmem();mark_readonly();/** Kernel mappings are now finalized - update the userspace page-table* to finalize PTI.*/pti_finalize();system_state = SYSTEM_RUNNING;numa_default_policy();rcu_end_inkernel_boot();if (ramdisk_execute_command) {ret = run_init_process(ramdisk_execute_command);if (!ret)return 0;pr_err("Failed to execute %s (error %d)\n",ramdisk_execute_command, ret);}/** 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) {ret = run_init_process(execute_command);if (!ret)return 0;panic("Requested init %s failed (error %d).",execute_command, ret);}if (!try_to_run_init_process("/sbin/init") ||!try_to_run_init_process("/etc/init") ||!try_to_run_init_process("/bin/init") ||!try_to_run_init_process("/bin/sh"))return 0;panic("No working init found. Try passing init= option to kernel. ""See Linux Documentation/admin-guide/init.rst for guidance.");
}
static const char *argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
const char *envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };static int run_init_process(const char *init_filename)
{argv_init[0] = init_filename;pr_info("Run %s as init process\n", init_filename);return do_execve(getname_kernel(init_filename),(const char __user *const __user *)argv_init,(const char __user *const __user *)envp_init);
}
依次尝试执行/sbin/init /etc/init /bin/init /bin/sh
ls -l /sbin/init
lrwxrwxrwx 1 test test 14 Aug 3 2022 /sbin/init -> ../bin/busybox
简介: linux内核启动的第一个init进程,并且集成了大量的linux命令和工具的软件和系统初始化运行的配置,比如ls,mv,ifconfig等命令,是一个大的工具箱,
使用就是配置相关的选项,选择自己想要的功能,然后编译即可.
(1)官网和源码包下载https://busybox.net/
(2)解压mkdir rootfstar -vxjf busybox-1.29.0.tar.bz2
(3)配置busybox,裁剪和选择需要的功能cp ../config_xxx ./.configmake ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig
(4)编译busybox make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j 20
(5)install 生成所需要的busyboxmake install make CONFIG_PREFIX=../busybox_output/ install cp -af ./_install/* ../busybox_output/
(6)查看成品
ls -lh
total 12K
drwxr-xr-x 2 root root 4.0K Jun 21 14:50 bin
lrwxrwxrwx 1 root root 11 Jun 21 14:50 linuxrc -> bin/busybox
drwxr-xr-x 2 root root 4.0K Jun 21 14:50 sbin
drwxr-xr-x 4 root root 4.0K Jun 21 14:50 usr