全部博文(51)
分类: LINUX
2016-08-18 11:35:23
boot启动完成后,进入内核:
start_kernel-->rest_init-->kernel_init在此函数中会调用preinit脚本:
if (!try_to_run_init_process("/etc/preinit") || !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; |
root@OpenWrt:/etc/rc.d# cat /etc/preinit [ -z "$PREINIT" ] && exec /sbin/init
export PATH=/usr/sbin:/usr/bin:/sbin:/bin
pi_ifname= pi_ip=192.168.1.1 pi_broadcast=192.168.1.255 pi_netmask=255.255.255.0
fs_failsafe_ifname= fs_failsafe_ip=192.168.1.1 fs_failsafe_broadcast=192.168.1.255 fs_failsafe_netmask=255.255.255.0
fs_failsafe_wait_timeout=2
pi_suppress_stderr="y" pi_init_suppress_stderr="y" pi_init_path="/usr/sbin:/usr/bin:/sbin:/bin" pi_init_cmd="/sbin/init"
. /lib/functions.sh . /lib/functions/preinit.sh . /lib/functions/system.sh
boot_hook_init preinit_essential boot_hook_init preinit_main boot_hook_init failsafe boot_hook_init initramfs boot_hook_init preinit_mount_root
for pi_source_file in /lib/preinit/*; do . $pi_source_file done
boot_run_hook preinit_essential
pi_mount_skip_next=false pi_jffs2_mount_success=false pi_failsafe_net_message=false
boot_run_hook preinit_main root@OpenWrt:/etc/rc.d# |
首次调用该脚本时,$PREINIT未定义,所以执行/sbin/init。
-->ulog_open
设置日志打印级别及输出日志方式;
-->设置信号处理函数,对于SIGTERM,SIGUSR1,SIGUSR2信号的处理是重启系统
-->early()
-->early_mounts();
挂载文件系统,在tmp目录下建立文件夹;
-->early_env();
设置PATH环境变量为"/usr/sbin:/usr/bin:/sbin:/bin"
-->cmdline(void)
设置日志级别
-->watchdog_init
watchdog初始化
-->创建一进程运行/sbin/kmodloader,加载一些ko模块
root@OpenWrt:/# ls -l /etc/modules-boot.d/ lrwxrwxrwx 1 root root 27 Aug 18 01:57 02-crypto-hash -> ../modules.d/02-crypto-hash lrwxrwxrwx 1 root root 29 Aug 18 01:57 04-crypto-crc32c -> ../modules.d/04-crypto-crc32c lrwxrwxrwx 1 root root 27 Aug 18 01:57 09-crypto-aead -> ../modules.d/09-crypto-aead lrwxrwxrwx 1 root root 25 Aug 18 01:57 20-lib-crc16 -> ../modules.d/20-lib-crc16 lrwxrwxrwx 1 root root 24 Aug 18 01:57 20-usb-core -> ../modules.d/20-usb-core lrwxrwxrwx 1 root root 30 Aug 18 01:57 30-button-hotplug -> ../modules.d/30-button-hotplug lrwxrwxrwx 1 root root 35 Aug 18 01:57 30-gpio-button-hotplug -> ../modules.d/30-gpio-button-hotplug lrwxrwxrwx 1 root root 33 Aug 18 01:57 40-ata-ahci-platform -> ../modules.d/40-ata-ahci-platform lrwxrwxrwx 1 root root 25 Aug 18 01:57 40-scsi-core -> ../modules.d/40-scsi-core lrwxrwxrwx 1 root root 20 Aug 18 01:57 40-usb2 -> ../modules.d/40-usb2 lrwxrwxrwx 1 root root 24 Aug 18 01:57 41-ata-ahci -> ../modules.d/41-ata-ahci lrwxrwxrwx 1 root root 36 Aug 18 01:57 45-usb-phy-dwc3-ipq40xx -> ../modules.d/45-usb-phy-dwc3-ipq40xx lrwxrwxrwx 1 root root 33 Aug 18 01:57 45-usb-phy-dwc3-qcom -> ../modules.d/45-usb-phy-dwc3-qcom lrwxrwxrwx 1 root root 32 Aug 18 01:57 53-usb-dwc3-ipq40xx -> ../modules.d/53-usb-dwc3-ipq40xx lrwxrwxrwx 1 root root 29 Aug 18 01:57 53-usb-dwc3-qcom -> ../modules.d/53-usb-dwc3-qcom lrwxrwxrwx 1 root root 24 Aug 18 01:57 54-usb-dwc3 -> ../modules.d/54-usb-dwc3 lrwxrwxrwx 1 root root 20 Aug 18 01:57 54-usb3 -> ../modules.d/54-usb3 lrwxrwxrwx 1 root root 25 Aug 18 01:57 60-leds-gpio -> ../modules.d/60-leds-gpio lrwxrwxrwx 1 root root 24 Aug 18 01:57 usb-storage -> ../modules.d/usb-storage root@OpenWrt:/# root@OpenWrt:/# cat /etc/modules-boot.d/30-button-hotplug button-hotplug root@OpenWrt:/# find / -name button-hotplug.ko /lib/modules/3.14.43/button-hotplug.ko /rom/lib/modules/3.14.43/button-hotplug.ko root@OpenWrt:/# |
该进程首先在/etc/modules-boot.d/遍历所有的文件内容,根据文件名+后缀(.ko),在系统寻找ko模块并加载;
-->uloop_init()
-->preinit(void)
这里执行两部分内容:
1、{ "/sbin/procd", "-h", "/etc/hotplug-preinit.json", NULL }
注册热插拔事件处理函数;
/etc/hotplug-preinit.json文件为接收到热插拔时间后所需要调用的脚本;
2、{ "/bin/sh", "/etc/preinit", NULL }
à设置PREINIT变量为1;setenv("PREINIT", "1", 1);
à执行/etc/preinit脚本,此时PREINIT已经设置为1,可以执行脚本后半部分;
à在这里注册了一个回调,preinit_proc.cb = spawn_procd;在init执行完毕后执行该回调,就由procd来继续执行初始化动作;
-->uloop_run(void)
后介绍uloop及/etc/preinit脚本内容。