系统启动及内核管理

一、知识整理

1、nohup 命令:不挂断地运行命令,远程连接当断网或断开shell时还可以继续运行。无论是否将nohup命令的输出重定向到终端,输出都将附加到当前目录下的nohup.out文件中,如果当前不可写,则重定向到家目录nohup.out文件,除非定义了输出文件。

nohup COMMAND 输出重定向至文件

nohup COMMAND & 使用jobs查看任务,按后台任务关闭方式关闭。

2、设置和调节nice值:

[root@localhost ~]# nice -n -5 ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.039 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.058 ms
 PID USER     PR  NI    VIRT    RES    SHR S  %CPU %MEM  TIME+ COMMAND                            
  6533 root    15  -5  114380    692    560 S   0.0  0.1   0:00.01 ping    
[root@localhost ~]# nice -5 ping  127.0.0.1

设置nice5.

renice调整nice值,普通用户只能调大自己进程的nice值,即降低程序优先级

[root@localhost ~]# renice +5 -p 6578 
6578 (process ID) old priority 5, new priority 5
[root@localhost ~]# renice 7 -p 6578 
6578 (process ID) old priority 5, new priority 7
[root@localhost ~]# renice -2 -p 6578 
6578 (process ID) old priority 7, new priority -2

3rc#.d登录等级:KS开头的服务,数字越小,越先运行。

ramdisk虚拟内存盘技术:使用缓冲和缓存来加速对磁盘上的文件访问:centos6中为initramfs功能,文件为/boot/initramfs,若损坏可使用mkinitrd可以生成,命令如下:

[root@centos68 boot]# mkinitrd initramfs-`uname -r`.img `uname -r`

4、查看运行级别:

[root@centos68 boot]# who -r
         运行级别 3 2016-09-08 04:51
[root@centos68 boot]# runlevel 
N 3

runlevel显示上次启动级别和本次启动级别

5、查看/boot/initramfs.***.img文件,

[root@centos68 ~]# file initramfs-2.6.32-642.el6.x86_64.img 
initramfs-2.6.32-642.el6.x86_64.img: gzip compressed data, from Unix, last modified: Sat Sep 10 01:02:33 
2016, max compression
[root@centos68 ~]# mv initramfs-2.6.32-642.el6.x86_64.img initramfs-2.6.32-642.el6.x86_64.img.gz
[root@centos68 ~]# gunzip initramfs-2.6.32-642.el6.x86_64.img.gz
[root@centos68 ~]# file initramfs-2.6.32-642.el6.x86_64.img 
initramfs-2.6.32-642.el6.x86_64.img: ASCII cpio archive (SVR4 with no CRC)

[root@centos68 ~]# cpio -vt <  initramfs-2.6.32-642.el6.x86_64.img 
-rwxr-xr-x   1 root     root       587041 Sep 10 01:02 lib64/libm-2.12.so
-rwxr-xr-x   1 root     root      1491968 Sep 10 01:02 lib64/liblvm2cmd.so.2.02
-rwxr-xr-x   1 root     root        12688 Dec  8  2011 lib64/libfipscheck.so.1.1.0
lrwxrwxrwx   1 root     root           15 Sep 10 01:02 lib64/libacl.so.1 -> libacl.so.1.1.0
lrwxrwxrwx   1 root     root           17 Sep 10 01:02 lib64/libaudit.so.1 -> libaudit.so.1.0.0
-rwxr-xr-x   1 root     root       272008 Sep 10 01:02 lib64/libreadline.so.6.0
140255 块

可以查看文件中的内容,此文件给内核驱动磁盘提供了必要文件

6、centos5在文件/etc/inittab中定义了ctrl alt delete重启,在虚拟机中用insert代替delete。在centos6中是/etc/init/control-alt-delete.conf文件。在centos5inittab定义的信息在centos6中分散放置,详情可以由inittab查看。终端mingetty定义respawn,不能被杀死,centos6中定义在/etc/init/serial.conf中,可以更改F10为第六个终端等,按照格式写即可。

二、命令详解和事例

1、系统启动流程:

第一步:加电自检,加载BIOS

加电自检POSTpower-on-self-test),是BIOSbasic input and output system)功能的一个部分,负责完成对CPU、主板、内存、硬盘等硬件情况监测。BIOS是集成在主板上的一个ROM芯片中,其中保存着计算机最重要的基本输入/输出程序、系统信息设置和自检程序等;而用户设定保存的各种参数信息是保存在RAM芯片中的,由UPS持续供电,这个设置叫CMOS

第二步:读取MBR

磁盘第0磁道第一个扇区被称为MBRmaster boot record),即主要引导记录,大小512字节,存放了预启动信息、分区表信息。而前446个字节就是boot loader,系统找到MBR后,就会将其复制到物理内存中。

第三步:Boot Loader

boot loader就是在操作系统内核运行前运行的一段小程序,通过将这段小程序放入物理内存,我们可以初始化硬件设备、建立内存空间的映射图,从而将系统的软硬件环境带到一个合适的状态,以便最终调用操作系统内核,而boot loader驱动的便是boot目录,内有内核和假文件系统initramfs的映像。

关于legacy grub典型grubstage1stage1.5stage2stage1就是MBR,有第一个扇区512字节的内容,作用就是bootloader的作用;stage1.5是后面27个扇区的内容,用于识别boot分区,以进行stage2stage2用于打印选择内核界面、加密、运行grub.conf文件等,选择界面出现说明stage2已经完成。

第四步:加载内核

根据grub设定的内核映像所在路径,读取内核映像并解压缩,以加载内核。此时的问题就是,真正的根分区并不在boot目录下,系统启动需要的一些lib文件也并不在boot目录下,内核要启动根分区就得加载根分区的驱动,由于内核大小和开机速度限制,以及不同的主机不同的根文件系统的限制,boot并没有那么多驱动。此时就要借助一个假文件系统initramfs作为中介,此文件是安装系统的时候就生成的,按照主机的硬件情况存入必要的驱动,给内核驱动真正的根系统提供/proc /sys /dev等文件,因此内核便可以启动真正的rootfs来替换掉假文件系统,也就可以进行后续步骤。

步骤五:系统的第一个进程init

内核和分区被加载完成后,第一个运行的进程是/sbin/init该文件会读取/etc/inittab文件,并依据此文件来进行初始化工作。inittab文件的作用是设定运行等级。init程序的类型:UpstartinitcentOS6;配置文件:/etc/fstab/etc/init/*.conf

步骤六:系统初始化

init执行/etc/rc.d/rc.sysinit系统初始化脚本:设置主机名,设置欢迎界面,激活udevselinux,挂载fstab文件中定义的文件系统,检测根文件系统并以读写方式重新挂载根文件系统,设置系统时钟,激活swap,根据/etc/sysctl.conf文件设置内核参数,激活lvmraid设备,加载额外设备的驱动程序等。

步骤七:启动内核模块

依据/etc/modules.conf文件或/etc/modules.d目录下的文件来装载内核模块。

步骤八:执行不同级别的脚本程序:

根据运行等级不同,运行rc0.drc6.d中的相应脚本程序,来完成相应的初始化工作和启动相应的服务。

步骤九:执行rc.local

rc.local就是在一切初始化工作后,Linux留给用户进行个性化的地方,是开机启动的最后一个文件可以把想设置和启动的东西放在这里正常级别下,最后启动的服务S99local没有链接至/etc/rc.d/init.d一个服务脚本,而是指向了/etc/rc.d/rc.local脚本

步骤十:登录

mingetty tty-6-login。执行/bin/login,启动登录程序,输入账号密码登录系统。

2、chkconfig命令,图形化命令:ntsysv

查看服务在所有级别的启动或关闭设定情形:chkconfig [–list] [name]

添加服务:chkconfig –add name

删除服务:chkconfig –del name

修改指定的连接类型 chkconfig [–level levels] name <on|off|reset>级别省略表示2345

3、grub命令行接口:在grub.conf文件被破坏或有错误时,开机会进入grub命令行,也可以在选择内核时按c进入,也可以在命令行输入grub命令进入;grub命令行用于更改内核特性,开机参数,也可以指定内核和虚根文件等,输入help查看可以使用的命令。如果.conf文件损坏,可以添加如下两行,支持tab键补全:

kernel (hd0,0)/vmlinuz.2.6.13….  root=/dev/sda2  max_loop=100 selinux=0 init=/bin/bash rhgb quiet

initrd (hd0,0)/initramfs…..img

或者将(hd0,0)取出,单独成行

root (hd0,0)

kernel /vmlinuz.2.6.13….  root=/dev/sda2  max_loop=100 selinux=0 init=/bin/bash rhgb quiet

initrd /initramfs…..img

最后boot重新开机。参数中rhgb不显示最后的加载图像,只显示启动情况;quiet隐藏内核加载信息,及初始化信息,只显示之后的开启服务信息。kernelinitrd前直接是根目录的原因:(hd0,0)指第一块磁盘的第一个分区,当刚开机的时候,只有boot,因此默认把boot当做根,当通过虚根识别了真正的rootfs后,才挂载为boot

4、grub.conf配置文件:

default=#设定默认启动的菜单项;从0开始;

timeout=#指定菜单项等待选项选择的时长

splashimage=(hd#,#)/PATH/TO/XPM_FILE:菜单背景图文件路径

hiddenmenu隐藏选择菜单,不点击按键5秒后便跳过

password [–md5] STRING:启动菜单编辑认证,p键输入密码才可以进入编辑内核,即ace三个模式,不影响开机

title TITLE定义菜单标题

root (hd#,#)  给定stage2和内核的位置

kernel 启动的内核

initrd 虚根文件

password [–md5] STRING 选择内核后,需要输入密码。

5、安装或者修复grub:第一种方法:在命令行或修复模式grub-install –root-directory=/PATH /dev/sd?;第二种方法:使用grub命令,依次输入roothd##)和setuphd#)。

[root@centos68 ~]# grub-install --root-directory=/ /dev/sda
Installation finished. No error reported.
This is the contents of the device map //boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.
(fd0) /dev/fd0
(hd0) /dev/sda
[root@centos68 ~]# grub
Probing devices to guess BIOS drives. This may take a long time.
    GNU GRUB  version 0.97  (640K lower / 3072K upper memory)
 [ Minimal BASH-like line editing is supported.  For the first word, TAB
   lists possible command completions.  Anywhere else TAB lists the possible
   completions of a device/filename.]
grub> root (hd0,0)     
root (hd0,0)
 Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)
setup (hd0)
 Checking if "/boot/grub/stage1" exists... no
 Checking if "/grub/stage1" exists... yes
 Checking if "/grub/stage2" exists... yes
 Checking if "/grub/e2fs_stage1_5" exists... yes
 Running "embed /grub/e2fs_stage1_5 (hd0)"...  27 sectors are embedded.
succeeded
 Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.

三、课后练习

1、添加一个系统服务,设置启动级别和开机启动:

写一个脚本,添加如下行:

#! /bin/bash

# chkconfig: 2345 10 90

# description: A

其中chkconfig一行定义了启动级别和启动顺序和关闭顺序,此优先级是按照字符顺序,跟数组大小无关;

放入/etc/init.d/目录下,使用chkconfig –add *.sh添加服务

使用chkconfig –list 查看服务启动级别

使用chkconfig –level # *.sh更改启动级别

ntsysv图形化界面,更改当前级别是否启动,–level=#更改指定级别;

使用service *.sh restart|stop|status|restart管理服务。

2、自制Linux系统:

步骤一:添加一块单独的硬盘,此硬盘是单个文件,以便于单独使用运行:

blob.png

步骤二:分区,挂载;

将此磁盘为两分区,一个作为boot使用,一个作为根目录使用;然后挂载:

blob.png 

注意:boot的挂载点名称一定要使用boot,因为grub生成时,已经决定名称不能变化了。

步骤三:恢复内核,生成grub,注意文件参数的顺序:

[root@centos68 mnt]# cp /boot/vmlinuz-2.6.32-642.el6.x86_64  ./boot/
[root@centos68 mnt]# cp /boot/initramfs-2.6.32-642.el6.x86_64.img  ./boot/
[root@centos68 mnt]# grub-install --root-directory=/mnt/ /dev/sdb
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /mnt//boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.
(fd0) /dev/fd0
(hd0) /dev/sda
(hd1) /dev/sdb
[root@centos68 mnt]# vim boot/grub/grub.conf
[root@centos68 mnt]# cat boot/grub/grub.conf
default=0
timeout=5
title zizhicentos
kernel (hd0,0)/vmlinuz-2.6.32-642.el6.x86_64 root=/dev/sda2  selinux=0  init=/bin/bash  
initrd (hd0,0)/initramfs-2.6.32-642.el6.x86_64.img

步骤四:创建必要配置文件和目录:

[root@centos68 sysroot]# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  proc  root  sbin  sys  usr  var
[root@centos68 mnt]# vim sysroot/etc/fstab
[root@centos68 mnt]# cat sysroot/etc/fstab
/dev/sda2  /  ext4   defaults 1 1
/dev/sda1  /boot  ext4  defaults  1 2

步骤五:使用脚本复制命令及其相关的库文件并检测:

脚本内容如下:

[root@centos68 mnt]# cat ~/copycmd0.sh 
#!/bin/bash
ch_root="/mnt/sysroot"
[ ! -d $ch_root ] && mkdir $ch_root
bincopy() {
    if which $1 &>/dev/null; then
        local cmd_path=`which --skip-alias $1`
        local bin_dir=`dirname $cmd_path`
        [ -d ${ch_root}${bin_dir} ] || mkdir -p ${ch_root}${bin_dir}
        [ -f ${ch_root}${cmd_path} ] || cp -f $cmd_path ${ch_root}${bin_dir}
        return 0
    else
        echo "Command not found."
        return 1
    fi
}
libcopy() {
    local lib_list=$(ldd `which --skip-alias $1` | grep -Eo '/[^[:space:]]+')
    for loop in $lib_list;do
        local lib_dir=`dirname $loop`
        [ -d ${ch_root}${lib_dir} ] || mkdir -p  ${ch_root}${lib_dir}
        [ -f ${ch_root}${loop} ] || cp -f $loop ${ch_root}${lib_dir}
    done
}
read -p "Please input a command or quit: " command
while [ "$command" != "quit" ];do
    if bincopy $command ;then
        libcopy $command
    fi
    read -p "Please input a command or quit: " command
done
[root@centos68 ~]# ./copycmd0.sh 
Please input a command or quit: /bin/bash
Please input a command or quit: /bin/ls
Please input a command or quit: /bin/hostname
Please input a command or quit: /bin/mount
Please input a command or quit: quit
[root@centos68 sysroot]# cd lib64/
[root@centos68 lib64]# ls
ld-linux-x86-64.so.2  libblkid.so.1  libdl.so.2       libselinux.so.1  libuuid.so.1
libacl.so.1           libcap.so.2    libpthread.so.0  libsepol.so.1
libattr.so.1          libc.so.6      librt.so.1       libtinfo.so.5
[root@centos68 mnt]# chroot sysroot/
bash-4.1# ls
bin  boot  dev etc  home  lib lib64  media  mnt  proc  root  sbin  sys  usr  var

步骤六:将做好的磁盘单独作为一个系统运行并调试:

[root@centos68 mnt]# sync
[root@centos68 mnt]# sync

blob.png

blob.png

原创文章,作者:SilencePavilion,如若转载,请注明出处:http://www.178linux.com/47615

(0)
上一篇 2016-09-19 13:47
下一篇 2016-09-19 13:47

相关推荐

  • Linux 下的帮助及文件管理

    1、whatis     whatis command 或 man -f command     仅能提供命令基本的介绍。不提具体用法。     基于数据库的查找,优点:检索速度快;缺点:缺乏实时性    &n…

    2017-07-15
  • 学习宣言

    我是喻成,我参加了马帮第22期的linux运维网络班,于2016年8月开始跟随马哥学习linux, 我的学习宣言是:要享受学习的每一刻,我思故我在。

    Linux干货 2016-08-15
  • linux下解压命令大全

    .tar  解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压1:gunzip FileName.gz 解压2:gzip -d FileName.gz 压缩:gzip FileName .tar.gz 和 .t…

    Linux干货 2017-07-11
  • 使用cp命令定时复制数据

    cp命令 cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或者目录。 cp [选项] [参数] 常用选项: -r -R 递归拷贝目录及目录里所有的内容 -i 交互式,默认支持 -d 复制的时候,如果目录有链接文件,则只复制链接文件 -p –preserv,复制的时候将文件的属性一起复制 -a = -dpR -v 复制的时候显示详细过程 如何定期执…

    Linux干货 2017-04-03
  • 正则表达式

         正则表达式是由一类特殊字符所编写的模式,通过这些字符的使用,我们能检索 查找文本中符合某些规则的内容。 正则表达式元字符的分类有:字符匹配,匹配次数,位置锚定,分组等。      字符匹配:      . : 匹配任意单个字符     &n…

    Linux干货 2017-04-06
  • 用户相关属性权限、正则表达式的应用(网络班21期第四周博客)

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。          mkdir /home/tuser1        &n…

    Linux干货 2016-08-02

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-20 14:26

    文章整体思路比较清晰,理论介绍的很全面,同时实验部分也很完整,赞一个。