linux: the kernel system startup and management

Linux components:

  • kernel (kernel): process management, memory management, network management, drivers, file system, security features
  • rootfs: program and glibc
  • Library: a collection of functions
  • Program: binaries

Kernel:

  • linux kernel features:

    模块化:.ko文件(内核对象)。如文件系统,硬件驱动,网络协议等。
    支持内核模块动态的加载和卸载
    
  • Core components:

    /boot/vmlinuz #压缩内核,二进制文件
    

CentOS start the process:

  1. POST: Power On Self Test BIOS main function, responsible for hardware detection

    ROM:BIOS
    RAM:保存BIOS设置的各种参数
    
  2. MBR boot:

    MBR:磁盘第一个扇区
    前445字节 bootloader
    中间64字节分区表
    最后2字节 55AA 结束标记位
    
  3. bootloader: operating system boot loader

    windows:ntloader,仅支持windows os,无法启用linux
    Linux:GRUB,支持多系统,功能强大
    /boot/grub	记录了内核位置
    
  4. Load the kernel: / boot / vmlinuz (kernel file)

    自身初始化:
    检测硬件设备
    加载驱动程序(借助ramdisk加载驱动)-->boot/initramfs这个文件
    只读挂载根文件系统
    运行初始化/sbin/init(systemd)——>再从磁盘读取驱动,重新挂载根(可读写)
    

init type:

  • Sysv: / sbin / init, before Centos5

    Configuration file: / etc / inittab

    id:5:initialization	#设置系统开机运行级别
    si::sysinit:/etc/rc.d/rc.sysinit	#加载系统开机脚本
    l3:3:wait:/etc/rc.d/rc 5  #加载/etc/rc.d/rc脚本,用于启动或关闭服务 ,5表示运行runlevel 5
    
  • Upstart:/sbin/init,Centos6

    Configuration file: / etc / inittab, / etc / init / * conf.

  • Systemd: systemd, Centos7

    Profile: / usr / lib / systemd / system, / etc / systemd / system

/etc/rc/d/rc.sysinit: system initialization script

#此脚本只在系统开机时运行一次。
1、设置主机名
2、设置欢迎信息
3、激活udev(设备名)和selinux
4、挂载/etc/fstab文件中定义的文件系统
5、检测根文件系统,以读写方式重新挂载跟文件系统
6、设置系统时钟
7、激活swap设备
8、根据/etc/sysctl.conf文件设置内核参数
9、激活lvm及software raid设备
10、加载额外设备的驱动程序
11、清理操作

/etc/rc.d/rc: enable or disable system services

[11:21:21 root@centos6 ~]#cd /etc/rc
rc0.d/ rc1.d/ rc2.d/ rc3.d/ rc4.d/ rc5.d/ rc6.d/ rc.d/   #不同运行等级文件夹
[11:22:22 root@centos6 rc5.d]#ll	#文件夹内容为指向不通服务的软连接
lrwxrwxrwx. 1 root root 16 Dec 11 22:31 K01smartd -> ../init.d/smartd   #
lrwxrwxrwx. 1 root root 14 Dec 11 22:31 S05rdma -> ../init.d/rdma
脚本将运行以S开头的服务

Adjust the boot service:

  • ntsysv: You can only adjust a mode

    ntsysv --level=3	#调整level 3的启动服务
    跳出界面,*表示自启动,按空格取消
    
  • chkconfig: can be adjusted at the same time more difficult to use level centos7

    #显示服务
    chkconfig --list	#显示多个level 服务自启动状态 list后面可指定服务名
    NetworkManager 	0:off	1:off	2:on	3:on	4:on	5:on	6:off
    abrt-ccpp      	0:off	1:off	2:off	3:on	4:off	5:on	6:off
    abrtd          	0:off	1:off	2:off	3:on	4:off	5:on	6:off
    #设置自启动服务 
    chkconfig --level 35 atd on	#这是3,5运行级  atd服务自启动
    chkconfig --level atd on # 不加运行级,默认设置2345级
    # 添加自定义服务
    chkconfig --add testsry	#自定义服务名。 必须放在/etc/init.d/下
    

Service script: Location /etc/init.d/

以at服务为例格式要求
#!/bin/sh
# atd Starts/stop the "at" daemon
# chkconfig:   345 95 5    #这行必须有,235 表示level 345模式 自启动,95表示自启动S编号 5表示程序关闭K编号
# description: Runs commands scheduled by the "at" command at the time \	#这个描述也必须有
#    specified when "at" was run, and runs batch commands when the load \
#    average is low enough.

Custom Service Script Example: vim /etc/init.d/testsry.sh (centos6 environment)

#!/bin/bash
#
# chkconfig: - 98 3
#description: test service
. /etc/init.d/functions	#调用系统函数库 ,主要用action函数,显示服务信息
# 定义函数
start(){
	touch /var/lock/subsys/`basename $0`	#测试用
	action "starting `basename $0`"
}
stop(){
	rm -f /var/lock/subsys/`basename $0`
	action "stoping `basename $0`"
}
status(){
	[ -f /var/lock/subsys/`basename $0` ] && echo `basename $0` is running || echo `basename $0 is stoped`
}

case $1 in
start)
	start
	;;
stop)
	stop
	;;
restart)
	stop
	start
	;;
status)
	status
	;;
*)
	echo "Usage: /etc/init.d/`basename $0` {start|stop|status|restart}"
esac 
#脚本结束

#设置自启动
chkconfig --add testsrv	#加入服务列表
chkconfig --level testsry on #开启服务

# 后续可以使用 service testsry [start | stop] 控制服务

Start Troubleshooting:

1.error 15: File not found # kernel error, not found

修复 /boot/vmlinuz内核
#进入救援模式,救援开始
查看原系统挂载目录,默认/mnt/sysimage
mkdir /mnt/cdrom;mount /dev/sr0 /mnt/cdrom	#挂载修复光盘
cp /mnt/cdrom/isolinux/vmlinuz /mnt/sysimage/boot/vmlinuz-`uname -r`	#复制内核文件
保存重启

2. has been flashing cursor: Could not load file system driver, the kernel can not mount

修复/boot/initramfs 驱动文件
#进入救援模式,救援开始
chroot /mnt/sysimage/	   #切换/到,真正磁盘上的/
mkinitrd /boot/initramfs-`uname -r`.img `uanme -r`	#利用修复工具重新生成驱动文件
保存重启

3 has been automatically shut down after boot or reboot: runlevel system error may be set to 0 or 6 (centos6)

3.1 When the program displays the following screen, quickly press any key, the boot menu

3.2 the boot menu, according to a modified kernel parameters

3.3 into the kernel parameter modification interface, the input stage 3 enters runlevel3 (command line)

3.4 After successfully entering the system, modify the / etc / inittab configuration file

vim /etc/inittab
id:3:initdefault:				#找到对应的行,修改

4. The system boot stuck in a service does not move:

按住 ctrl+alt+del 重启电脑
重启后进入单用户模式
chkconfig 服务名 off #临时关闭当前无法启动的服务 

4.1 If a single user can not enter: the kernel modification mode

#进入内核模式修改模式
init=/bin/bash
bash-4.1#	#此事进入bash,根为只读模式,需要重新挂载
mount -o remount,rw /
chkconfig --level 123456 服务名 off #关闭服务
exit	#重新启动

The reproduction root password:

#centos6
进入单用户模式
passwd	#直接修改密码   超级简单。

#centos7

tool:

lsmod: View loaded drive

rmmod: unload modules

rmmod ext3  #卸载ext3驱动模块

Guess you like

Origin www.cnblogs.com/franc/p/12587462.html