八、Linux精简系统和内核管理裁剪(一)

简介:

一、内核

1、什么是内核

内核其实就是操作系统,是驱动计算机硬件,实现人机操作,并提供其他服务器功能的底层系统。linux操作系统也称为“内核”,指的是同一件事情。

linux的内核是以模块化的方式工作的,主要工作任务包括存储管理、cpu管理、进程管理、文件系统管理、设备驱动管理、网络管理、系统调用、安全管理等。

2、内核的组成部分包括vmlinuz、initrd(linux5)或者initramfs(linux6)、/lib/modules(模块,子核心)

二、内核设计

微内核

就是主核心加上子核心的方式。

例如:windows,salors

单内核

所有功能整合在一起使用的方式。

例如:linux(支持模块方式,*.ko,“kernel object”)


三、系统调用

linux内核实现了将硬件功能抽象化的过程,就是可以实现程序的管控,最终体现在程序上就是系统调用,系统调用就是对各个硬件功能的一种体现,实际上系统调用的功能有很多,所有就产生了很多种系统调用(函数)。开发者也可以开发出更多的标准库(glibc),就是用于和系统调用通讯的程序。这个标准库可以共享给更多的程序使用,简化了应用程序开发的复制程度。


实现系统调用的进程就是init进程。

四、init进程


init是系统启动的第一个进程,也是其他所有进程的父进程,其他进程的维护依赖于它,redhat5使用的是init这个名词,而redhat6设计了一个全新的init系统称为“upstart”,而最新的redhat7采用的是systemd的init系统。

windows操作系统使用的初始化引导文件是ntloader

运行级别:不同的运行级别系统加载的内核内容是不同的,类似windows的安全模式相关列表。

1、进程的种类:

1.1守护进程(daemon,服务):运行在后台的特殊进程,是一个长期存在的进程,独立于控制终端,并且周期性的执行某种任务或等待处理某些事情的发送。这里所说的独立于控制终端,就是说不受终端tty、pts之类的进程影响。通常进程名以“d”结尾。

[root@37-test ~]# ps axj

 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND

    0     1     1     1 ?           -1 Ss       0   0:01 /sbin/init

    0     2     0     0 ?           -1 S        0   0:00 [kthreadd]

    2     3     0     0 ?           -1 S        0   0:00 [migration/0]

    2     4     0     0 ?           -1 S        0   0:01 [ksoftirqd/0]

   2     5     0     0 ?           -1 S        0   0:00 [stopper/0]

    2     6     0     0 ?           -1 S        0   0:03 [watchdog/0]

    2     7     0     0 ?           -1 S        0   0:00 [migration/1]

    2     8     0     0 ?           -1 S        0   0:00 [stopper/1]

    2     9     0     0 ?           -1 S        0   0:00 [ksoftirqd/1]

    2    10     0     0 ?           -1 S        0   0:03 [watchdog/1]

    2    11     0     0 ?           -1 S        0   0:00 [migration/2]

TPGID标注-1的进程就是没有控制终端的进程即守护进程

command中带有[ ]表示内核进程

1.2交互进程

1.3批处理进程

2、who命令

-a等于-b -d --login -p -r -t -T -u 选项的组合

-b显示上次系统开启时间

-d显示死进程

—login显示系统登录进程

-p显示由init衍生的进程

-r显示运行级别

-T显示消息状态(mesg)

-u列出已经登录的用户

[root@37-test ~]# who -a

           系统引导 2016-12-09 10:35

           运行级别 3 2016-12-09 10:35

root     + pts/0        2017-01-10 11:09   .         21368 (10.40.0.57)

           pts/1        2016-12-23 11:50             23119 id=ts/1  终端=0 退出=0

查看系统版本:

[root@37-test ~]# cat /etc/issue

****************************************************************

             Wellcome to PEP e-Ecampus Live System!             

****************************************************************

  active status: actived

  serivce status: Error

  service address: eth0(10.40.0.37)

  setup address: eth1()

****************************************************************

[root@37-test ~]# uname -a

Linux 37-test 2.6.32-642.11.1.el6.x86_64 #1 SMP Fri Nov 18 19:25:05 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

运行级别操作

1、显示运行级别

who -r

[root@37-test ~]# who -r

         运行级别 3 2016-12-09 10:35

2、切换运行级别

[root@37-test ~]# init 3

3、chkconfig

手动设置运行级别程序

管理员可以手动添加、删除已知的程序,达到在系统启动时关闭或开启某一个程序的运行。

参数:

—add添加启动程序

—del删除启动程序

—level

—list查看当前启动的程序

[root@37-test ~]# chkconfig --level 3 httpd on

[root@37-test ~]# chkconfig --list httpd

httpd           0:关闭 1:关闭 2:关闭 3:启用 4:关闭 5:关闭 6:关闭

4、系统启动流程:

LILO:早期linux boot loader(引导程序)

GRUB:突破了446字节限制(mbr)。

grub如何实现突破限制的功能,其实很简单,就是讲应该写引导内容的位置改为存放一个连接地址,这个地址在硬盘上存放了引导文件,而非0扇区0磁道的512字节上。这样就突破了磁盘的限制。

进程类型:

守护进程:系统启动后启动服务时所运行的进程。

守护进程由init启动,由于有相互依赖关系,早期的启动是需要按依赖顺序启动的。

rhel5:init、按顺序启动

rhel6:upstart 、可以不按照顺序启动

fedora:upstart

fedora:systemd,基于d-bus通讯,兼容init,借鉴macios系统。

4.1、加电自检,post,加电后cpu加载rom里面的内容到cpu中,主要是检测硬件状况,cmos设置。查看启动设备(从什么设备上启动,是光驱、u盘、硬盘、网卡),如果是硬盘,读取mbr信息(LILO/NTLOADER/GRUB),包括:启动程序bootloader、分区表。通过mbr加载内核,了解文件系统及驱动。

4.2、加载linux的内核kernel,完成基本的硬件驱动初始化,装载/sbin/init根文件系统。linux内核是模块化的,这些模块存储在[root@37-test ~]# ll /lib/modules/2.6.32-220.el6.x86_64/ ,而这个目录又在后面需要加载的根目录下,会产生冲突,所以需要先将必要的模块直接整合到内核中,也就是initrd,initramfs。


RAM DISK(内存中临时根文件系统) ——initrd

RAM FS---------initramfs

内核模块位置:

[root@37-test ~]# ll /lib/modules/2.6.32-220.el6.x86_64/

总用量 3216

lrwxrwxrwx.  1 root root     46 8月  26 2012 build -> ../../../usr/src/kernels/2.6.32-220.el6.x86_64

drwxr-xr-x.  2 root root   4096 12月  7 2011 extra

drwxr-xr-x. 11 root root   4096 8月  26 2012 kernel

-rw-r--r--.  1 root root 529526 8月  26 2012 modules.alias

-rw-r--r--.  1 root root 509216 8月  26 2012 modules.alias.bin

-rw-r--r--.  1 root root   1310 12月  7 2011 modules.block

-rw-r--r--.  1 root root     69 8月  26 2012 modules.ccwmap

-rw-r--r--.  1 root root 191785 8月  26 2012 modules.dep

-rw-r--r--.  1 root root 280509 8月  26 2012 modules.dep.bin

-rw-r--r--.  1 root root     68 12月  7 2011 modules.drm

-rw-r--r--.  1 root root    665 8月  26 2012 modules.ieee1394map

-rw-r--r--.  1 root root    141 8月  26 2012 modules.inputmap

-rw-r--r--.  1 root root   1236 8月  26 2012 modules.isapnpmap

-rw-r--r--.  1 root root     29 12月  7 2011 modules.modesetting

-rw-r--r--.  1 root root   1859 12月  7 2011 modules.networking

-rw-r--r--.  1 root root     74 8月  26 2012 modules.ofmap

-rw-r--r--.  1 root root  73619 12月  7 2011 modules.order

-rw-r--r--.  1 root root 375656 8月  26 2012 modules.pcimap

-rw-r--r--.  1 root root   6259 8月  26 2012 modules.seriomap

-rw-r--r--.  1 root root 193682 8月  26 2012 modules.symbols

-rw-r--r--.  1 root root 247713 8月  26 2012 modules.symbols.bin

-rw-r--r--.  1 root root 773964 8月  26 2012 modules.usbmap

lrwxrwxrwx.  1 root root      5 8月  26 2012 source -> build

drwxr-xr-x.  2 root root   4096 12月  7 2011 updates

drwxr-xr-x.  2 root root   4096 8月  26 2012 vdso

drwxr-xr-x.  2 root root   4096 12月  7 2011 weak-updates


4.3、通过init的配置文件/etc/inittab来决定运行级别,并通过/etc/rc.d/rc?.d/目录下的脚步来选择性的启动/etc/rc.d/init.d/目录下的程序。

[root@37-test ~]# tree /etc/rc.d/

/etc/rc.d/

├── init.d

│   ├── atd

│   ├── auditd

│   ├── blk-availability

│   ├── cento

│   ├── cluster

│   ├── crond

│   ├── crtmp

│   ├── cups

│   ├── disk2n

│   ├── dkms_autoinstaller

│   ├── dnsmasq

│   ├── fcoe

│   ├── fms

│   ├── functions

│   ├── haldaemon

│   ├── halt

│   ├── htcacheclean

│   ├── httpd

│   ├── ip6tables

│   ├── iptables

│   ├── ipvsadm

│   ├── iscsi

│   ├── iscsid

│   ├── killall

│   ├── lldpad

│   ├── lvm2-lvmetad


系统加载的服务默认是存放在/etc/rc.d/init.d/*下,不同级别的用户开机启动的服务是不同的,用户级别通过/etc/rc.d/下的rc0-6.d下的配置文件来决定,而这些配置文件只是连接文件,最终指向的还是/etc/rc.d/init.d/服务配置文件,那么如何决定不同级别启动哪些文件呢,就要使用/etc/rc.d/rc这个文件来决定,他通过循环命令将rc0-6

.d目录下的k开头的文件全部禁用,而s开头的全部启用。而s或k后面的数字表示启动的顺序,这个数值有/etc/rc.d/init.d/目录下的服务的配置文件内容决定。

[root@37-test ~]# more /etc/rc.d/rc |grep -v ^$

#! /bin/bash

#

# rc            This file is responsible for starting/stopping

#               services when the runlevel changes.

#

# Original Author:       

#               Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>

#

set -m

# check a file to be a correct runlevel script

check_runlevel ()

{

# Check if the file exists at all.

[ -x "$1" ] || return 1

is_ignored_file "$1" && return 1

return 0

}



[root@37-test ~]# more /etc/rc.d/init.d/crond 

#!/bin/sh

#

# crond          Start/Stop the cron clock daemon.

#

# chkconfig: 2345 90 60

# description: cron is a standard UNIX program that runs user-specified \

#              programs at periodic scheduled times. vixie cron adds a \

#              number of features to the basic UNIX cron, including better \


#              security and more powerful configuration options.


例如:配置文件的chkconfig:2345 90 60

2345表示在此范围级别按照S启动,其他按照K执行。s后面的优先级为90 ,k后面的优先级是60


# chkconfig: - 25 10 

25位置的数字常用于开启某个程序,而10位置的数字常用于关闭某个程序。

[root@localhost ~]# find /etc/rc.d/ -name *cups

/etc/rc.d/rc5.d/S25cups

/etc/rc.d/rc1.d/K10cups

/etc/rc.d/init.d/cups

/etc/rc.d/rc4.d/S25cups

/etc/rc.d/rc6.d/K10cups

/etc/rc.d/rc0.d/K10cups

/etc/rc.d/rc3.d/S25cups

/etc/rc.d/rc2.d/S25cups

配置文件:

init:/etc/inittab(centos 5)

# Default runlevel. The runlevels used are:

#   0 - halt (Do NOT set initdefault to this)

#   1 - Single user mode

#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)

#   3 - Full multiuser mode

#   4 - unused

#   5 - X11

#   6 - reboot (Do NOT set initdefault to this)

id:3:initdefault:

upstart:/etc/init/*.conf(centos 6)

[root@37-test ~]# ls /etc/init/

cento.conf                  n2disk.conf             rcS-emergency.conf

ck-log-system-restart.conf  nprobe.conf             rcS-sulogin.conf

ck-log-system-start.conf    pf_ring.conf            serial.conf

ck-log-system-stop.conf     plymouth-shutdown.conf  splash-manager.conf

control-alt-delete.conf     prefdm.conf             start-ttys.conf

disk2n.conf                 quit-plymouth.conf      tty.conf

init-system-dbus.conf       rc.conf

kexec-disable.conf          rcS.conf

sysinit:系统初始化脚本,在系统启动时加载的脚本目录位于/etc/rc.d/rc.sysinit。

[root@37-test ~]# more /etc/rc.d/rc.sysinit 

[root@37-test ~]# ll /etc/rc.d/

总用量 60

drwxr-xr-x. 2 root root  4096 12月 30 04:03 init.d

-rwxr-xr-x. 1 root root  2617 12月  9 2011 rc

drwxr-xr-x. 2 root root  4096 12月 30 04:03 rc0.d

drwxr-xr-x. 2 root root  4096 12月 30 04:03 rc1.d

drwxr-xr-x. 2 root root  4096 12月 30 04:03 rc2.d

drwxr-xr-x. 2 root root  4096 1月  10 15:57 rc3.d

drwxr-xr-x. 2 root root  4096 12月 30 04:03 rc4.d

drwxr-xr-x. 2 root root  4096 12月 30 04:03 rc5.d

drwxr-xr-x. 2 root root  4096 12月 30 04:03 rc6.d

-rwxr-xr-x. 1 root root   407 12月  1 13:53 rc.local

-rwxr-xr-x. 1 root root 19285 12月  9 2011 rc.sysinit //系统初始化脚本

主要内容:

激活selinux和udev

根据/etc/sysctl.conf文件设置内核参数

设置系统时钟

装载系统时钟

装载键映射

启动交互分区

设置主机名

根据文件系统检测并重新挂载其为读写

激活raid和lvm

检查和挂载其他文件系统:/etc/fstab中的定义

清理操作

选择执行/etc/rc.d/rc#.conf文件。


六、再说系统引导

1、启动流程:

1、post

2、bootloader(mbr:446,stage2:/boot/grub)

3、kernel+initrd(initramfs)(硬件检测、安装驱动,只读方式挂载根文件系统,执行/sbin/init-->init(5版本/etc/inittab(6版本是/etc/init/*.conf)),通过/etc/inittab设置默认运行级别。

4、执行系统初始化脚本/etc/rc.d/rc.sysinit,按需启动对象级别下的服务,启动终端)

系统的多重引导实际上就是在引导菜单选择的时候,指向不同的引导程序,而这个引导程序可以在不同分区的引导扇区,可以不再MBR中。

由于grub引导文件不支持LVM文件系统,所以在使用lvm进行分区的时候,必须把/boot分区单独分开而不能同根分区在一起。

[root@37-test ~]# ll /boot/grub/

总用量 280

-rw-r--r--. 1 root root     63 8月  26 2012 device.map

-rw-r--r--. 1 root root  13380 8月  26 2012 e2fs_stage1_5

-rw-r--r--. 1 root root  12620 8月  26 2012 fat_stage1_5

-rw-r--r--. 1 root root  11748 8月  26 2012 ffs_stage1_5

-rw-------  1 root root   1118 12月  5 16:01 grub.conf

-rw-r--r--. 1 root root  11756 8月  26 2012 iso9660_stage1_5

-rw-r--r--. 1 root root  13268 8月  26 2012 jfs_stage1_5

lrwxrwxrwx. 1 root root     11 8月  26 2012 menu.lst -> ./grub.conf

-rw-r--r--. 1 root root  11956 8月  26 2012 minix_stage1_5

-rw-r--r--. 1 root root  14412 8月  26 2012 reiserfs_stage1_5

-rw-r--r--. 1 root root   1341 11月 15 2010 splash.xpm.gz

-rw-r--r--. 1 root root    512 8月  26 2012 stage1

-rw-r--r--. 1 root root 125976 8月  26 2012 stage2

-rw-r--r--. 1 root root  12024 8月  26 2012 ufs2_stage1_5

-rw-r--r--. 1 root root  11364 8月  26 2012 vstafs_stage1_5

-rw-r--r--. 1 root root  13964 8月  26 2012 xfs_stage1_5

grub引导:

1、第一阶段:

stage1(系统引导第一阶段):磁盘的MBR的446字节内容,用于引导系统。

2、第二阶段:

stage1_5:stage1过渡到stage2所需要加载的内容。

[root@37-test ~]# ll /boot/grub/

总用量 280

-rw-r--r--. 1 root root     63 8月  26 2012 device.map

-rw-r--r--. 1 root root  13380 8月  26 2012 e2fs_stage1_5

-rw-r--r--. 1 root root  12620 8月  26 2012 fat_stage1_5

-rw-r--r--. 1 root root  11748 8月  26 2012 ffs_stage1_5

-rw-------  1 root root   1118 12月  5 16:01 grub.conf

-rw-r--r--. 1 root root  11756 8月  26 2012 iso9660_stage1_5

-rw-r--r--. 1 root root  13268 8月  26 2012 jfs_stage1_5

lrwxrwxrwx. 1 root root     11 8月  26 2012 menu.lst -> ./grub.conf

-rw-r--r--. 1 root root  11956 8月  26 2012 minix_stage1_5

-rw-r--r--. 1 root root  14412 8月  26 2012 reiserfs_stage1_5

-rw-r--r--. 1 root root   1341 11月 15 2010 splash.xpm.gz //启动时的背景图片

-rw-r--r--. 1 root root    512 8月  26 2012 stage1

-rw-r--r--. 1 root root 125976 8月  26 2012 stage2

-rw-r--r--. 1 root root  12024 8月  26 2012 ufs2_stage1_5

-rw-r--r--. 1 root root  11364 8月  26 2012 vstafs_stage1_5

-rw-r--r--. 1 root root  13964 8月  26 2012 xfs_stage1_5

3、第三阶段:

stage2:/boot/grub(/etc/grub.conf)。显示图形界面依赖于grub.conf配置文件。开机的启动菜单。

主要功能:

1选择启动的内核映像或操作系统

2 传递参数:

e:编辑模式

b:引导

3、基于密码保护:启动内核映像或者传递参数都可以设置密码保护。

Linux的引导分析(分区表与stage1):http://blog.csdn.net/sdcxyz/article/details/23601491

2、grub.conf配置文件讲解:

[root@37-test ~]# more /boot/grub/grub.conf 

# grub.conf generated by anaconda

#

# Note that you do not have to rerun grub after making changes to this file

# NOTICE:  You have a /boot partition.  This means that

#          all kernel and initrd paths are relative to /boot/, eg.

#          root (hd0,0)

#          kernel /vmlinuz-version ro root=/dev/sda3

#          initrd /initrd-[generic-]version.img

#boot=/dev/sda

default=0 #指定使用的title,0表示第一个(从0计数)

timeout=5 #设置超时时间,等待输入参数时间

splashimage=(hd0,0)/grub/splash.xpm.gz #背景照片名称及位置

hiddenmenu#隐藏菜单

password --md5 $1$N8Tao$FX6tTV1fVWauh/8sYtuSa0 #开机编辑grub时加密

title CentOS (2.6.32-642.11.1.el6.x86_64)


root (hd0,0) #指定根所在的磁盘分区,所以硬盘均已hd进行标识,hd0,0表示第一块磁盘的第一个分区。

kernel /vmlinuz-2.6.32-642.11.1.el6.x86_64 ro root=UUID=c0863086-d6e1-4670-b46

e-20aeaf76962e rd_NO_LUKS rd_NO_LVM LANG=zh_CN.UTF-8 rd_NO_MD quiet SYSFONT=lat0-sun16

 rhgb crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM biosdevname=0

password -md5   $1$OVR18/$WJiEp4GZJT.tXybTyABRZ1 #开机引导系统加密

#ro 只读方式读取内核,root=表示根分区挂载路径

root=/path/somefile :真正的根文件系统设备文件路径

quiet:静默模式,不显示详细加载信息。

selinux=0:禁用内核中的selinux。

password —md5sfis$1$OVR18/$WJiEp4GZJT.tXybTyABRZ1    用来指定密码为md5加密的,放在title里面表示引导时需要输入密码,放在所有title外面,表示编辑需要输入密码。

initrd /initramfs-2.6.32-642.11.1.el6.x86_64.img

title CentOS (2.6.32-220.el6.x86_64)

root (hd0,0)

kernel /vmlinuz-2.6.32-220.el6.x86_64 ro root=UUID=c0863086-d6e1-4670-b46e-20a

eaf76962e rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD quiet SYSFONT=latarcyrheb-sun

16 rhgb crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM biosdevname=0

initrd /initramfs-2.6.32-220.el6.x86_64.img#临时根文件系统,在启动时先加载她,等加载正是内核后这个虚拟根就不存在了。


md5加密方法:grub-md5-crypt

开源图片编辑软件:gimp,windows的xshell可以打开图形化的gimp软件。


grub如何识别分区:

所有种类的硬盘均以hd开头,不同的硬盘使用后面的第一个数字来表示,比如hd0、hd1,同一个硬盘的不同分区用第二个数字表示:hd0,0表示第一个硬盘的第一个分区。

自定义grub背景图片

要求:16色、xpm格式、640*480大小。

3、grub故障处理

问题1:grub文件丢失,损坏

1、重启进入编辑模式,按“c”键进入grub模式。

2、指定root分区:root (hd0,0)

可以通过cat (hd        然后按tab键的方式查找尝试,通过查看/etc/fstab可以确定根分区和boot引导分区是否在一起。cat (hd0,0)/etc/fstab

3、确定内核位置 kernel /vm   TAB补全,ro root=/dev/hda0 或者 ro root=LABLE=/ 或者ro root=UUID=来确定。

4、确定initrd的位置 initrd /boot/  TAB补全。

5、boot引导。

问题2:

由于重新安装Windows或其它未知原因而导致GRUB的丢失;

您可以通过系统安装盘、livecd进入修复模式;

请参考:

《Linux 系统的单用户模式、修复模式、跨控制台登录在系统修复中的运用》

首先:您根据前面所说grub-install来安装GRUB到/boot所在的分区;要仔细看文档,/boot是不是处于一个独立的分区是重要的,执行的命令也不同;

其次:要执行grub ,然后通过 root (hd[0-n],y)来指定/boot所位于的分区,然后接着执行 setup (hd0),这样就写入MBR了,比如下面的例子;

grub>root (hd0,6)

grub>setup (hd0)

grub>quit

重新引导就会再次出现MBR的菜单了或命令行的提示符了;

通常是给其他的损坏的硬盘进行安装,也就是说可以把不能启动的硬盘去下,挂到其他硬盘上引导安装grub。

方法二:

1、创建一个名为boot的目录,例如:/mnt/boot

2、确保目标磁盘的分区存在,并且已经格式化。

3、挂载目标硬盘上要安装的grub的分区至新建的boot目录   mount /dev/sdb1 /mnt/boot

4、grub-install --root-directory=/mnt /dev/sdb

作业:模拟挂载一块硬盘,安装grub。

[root@localhost ~]# fdisk /dev/sdb //对磁盘进行分区

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0x6582832e.

Changes will remain in memory only, until you decide to write them.

After that, of course, the previous content won't be recoverable.


Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)


WARNING: DOS-compatible mode is deprecated. It's strongly recommended to

         switch off the mode (command 'c') and change display units to

         sectors (command 'u').


Command (m for help): p


Disk /dev/sdb: 6442 MB, 6442450944 bytes

255 heads, 63 sectors/track, 783 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x6582832e


   Device Boot      Start         End      Blocks   Id  System


Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 1

First cylinder (1-783, default 1): 1

Last cylinder, +cylinders or +size{K,M,G} (1-783, default 783): 200M


Command (m for help): p


Disk /dev/sdb: 6442 MB, 6442450944 bytes

255 heads, 63 sectors/track, 783 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x6582832e


   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         200     1606468+  83  Linux


Command (m for help): p       


Disk /dev/sdb: 6442 MB, 6442450944 bytes

255 heads, 63 sectors/track, 783 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x6582832e


   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         200     1606468+  83  Linux


Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 2

First cylinder (201-783, default 201): 

Using default value 201

Last cylinder, +cylinders or +size{K,M,G} (201-783, default 783): 1G

Value out of range.

Last cylinder, +cylinders or +size{K,M,G} (201-783, default 783): 400M


Command (m for help): w

The partition table has been altered!


Calling ioctl() to re-read partition table.

Syncing disks.

[root@localhost ~]# mke2fs /dev/sdb1 //格式化分区

mke2fs 1.41.12 (17-May-2010)

文件系统标签=

操作系统:Linux

块大小=4096 (log=2)

分块大小=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

100464 inodes, 401617 blocks

20080 blocks (5.00%) reserved for the super user

第一个数据块=0

Maximum filesystem blocks=415236096

13 block groups

32768 blocks per group, 32768 fragments per group

7728 inodes per group

Superblock backups stored on blocks: 

32768, 98304, 163840, 229376, 294912


正在写入inode表: 完成                            

Writing superblocks and filesystem accounting information: 完成


This filesystem will be automatically checked every 32 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@localhost ~]# mke2fs /dev/sdb2

mke2fs 1.41.12 (17-May-2010)

文件系统标签=

操作系统:Linux

块大小=4096 (log=2)

分块大小=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

100464 inodes, 401625 blocks

20081 blocks (5.00%) reserved for the super user

第一个数据块=0

Maximum filesystem blocks=415236096

13 block groups

32768 blocks per group, 32768 fragments per group

7728 inodes per group

Superblock backups stored on blocks: 

32768, 98304, 163840, 229376, 294912


正在写入inode表: 完成                            

Writing superblocks and filesystem accounting information: 完成


This filesystem will be automatically checked every 23 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@localhost ~]# fdisk -l /dev/sdb


Disk /dev/sdb: 6442 MB, 6442450944 bytes

255 heads, 63 sectors/track, 783 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x6582832e


   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         200     1606468+  83  Linux

/dev/sdb2             201         400     1606500   83  Linux

[root@localhost ~]# mkdir /mnt/boot //创建挂载点

[root@localhost ~]# mount /dev/sdb1 /mnt/boot/ //将分区挂载到挂载点

[root@localhost ~]# grub-install --root-directory=/mnt /dev/sdb //安装grub,注意不要写错

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@localhost ~]# ll /mnt/boot/

总用量 24

drwxr-xr-x. 2 root root  4096 1月  12 10:21 grub

drwx------. 2 root root 16384 1月  12 10:18 lost+found

内核探测硬件的信息,被记录在日志文件/var/log/dmesg中。

通过dmesg可以查看。

Initializing cgroup subsys cpuset

Initializing cgroup subsys cpu

Linux version 2.6.32-573.el6.x86_64 (mockbuild@c6b9.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) ) #1 SMP Thu Jul 23 15:44:03 UTC 2015

Command line: ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet

KERNEL supported cpus:

  Intel GenuineIntel

  AMD AuthenticAMD

  Centaur CentaurHauls

grub命令详解:http://www.cnblogs.com/uptownBoy/articles/2041631.html

七、内核

linux内核是单内核设计,所有模块都存放在/lib/modules目录下,文件类型为ko。

如果内核需要加载的内容正是系统启动需要的,那么势必会造成两者的冲突,如何解决这个问题呢?

这就涉及到了两个方面,一方面可以在内核中集成需要的模块,而不是把它单独拿出来。另一方面需要内核在加载模块之前就可以启动,这就需要把他先加载到内存中ram disk(initrd)或者ram fs(initramfs)中,这个就类似一个临时的虚拟的根(/),这个虚拟根可以加载文件系统,这个很重要,然后再通过它切换的实际的根上。

引导内核的组成部分

/boot/vmlinuz-`uname -r`      redhat5版本中的文件名称

/boot/initrd-`uname -r`.img     redhat5版本中的文件名称

/boot/initramfs-`uname -r`.img   redhat6版本中的文件名称

/

lib/modules/内核模块

/sbin/init内核组成部分,启动的第一个程序,所有程序的父进程

/bin/bashshell语言程序,人机交互语言

[root@37-test ~]# ll /boot/

总用量 45808

-rw-r--r--  1 root root   108108 11月 19 03:30 config-2.6.32-642.11.1.el6.x86_64

drwxr-xr-x. 3 root root     1024 8月  26 2012 efi

drwxr-xr-x. 2 root root     1024 12月  8 09:52 grub

-rw-------  1 root root 20006313 12月  1 13:52 initramfs-2.6.32-642.11.1.el6.x86_64.img

drwx------. 2 root root     1024 8月  26 2012 lost+found

-rw-r--r--  1 root root   215530 11月 19 03:30 symvers-2.6.32-642.11.1.el6.x86_64.gz

-rw-r--r--  1 root root  2618532 11月 19 03:30 System.map-2.6.32-642.11.1.el6.x86_64


-rwxr-xr-x  1 root root  4269936 11月 19 03:30 vmlinuz-2.6.32-642.11.1.el6.x86_64


引导内核文件包含的内容:

一下就是linux6版本的引导内核经过解压后包含的内容,可以看出来里面就是一个精简的linux系统,包含常见的目录。

[root@37-test kernel]# mv initramfs-2.6.32-220.el6.x86_64.img initramfs-2.6.32-220.el6.x86_64.gz 将/boot目录下的引导内核复制到其他目录并重命名为*.gz。

[root@37-test kernel]# gunzip initramfs-2.6.32-220.el6.x86_64.gz  解压缩

[root@37-test kernel]# file initramfs-2.6.32-220.el6.x86_64  查看文件类型

initramfs-2.6.32-220.el6.x86_64: ASCII cpio archive (SVR4 with no CRC)

[root@37-test kernel]# cpio kernel<initramfs-2.6.32-220.el6.x86_64  再次解压缩

cpio: 你必须指定 -oipt 选项中的一个。

尝试 `cpio --help' 或者 `cpio --usage' 来寻找更多信息。


[root@37-test kernel]# cpio -id <initramfs-2.6.32-220.el6.x86_64 

74369 块

[root@37-test kernel]# ls 解压缩后文件内容

bin                 initqueue                        mount        sysroot

cmdline             initqueue-finished               pre-pivot    tmp

dev                 initqueue-settled                pre-trigger  usr

dracut-004-256.el6  initqueue-timeout                pre-udev     var

emergency           proc

etc                 lib                              sbin

init                lib64                            sys

[root@37-test kernel]# 



本文转自 blackstome 51CTO博客,原文链接:http://blog.51cto.com/137783/1916958,如需转载请自行联系原作者

相关文章
|
2天前
|
负载均衡 Linux 应用服务中间件
Linux系统中前后端分离项目部署指南
Linux系统中前后端分离项目部署指南
|
21小时前
|
存储 算法 Linux
【Linux】线程的内核级理解&&详谈页表以及虚拟地址到物理地址之间的转化
【Linux】线程的内核级理解&&详谈页表以及虚拟地址到物理地址之间的转化
|
22小时前
|
Ubuntu Linux 编译器
【Linux】详解动静态库的制作和使用&&动静态库在系统中的配置步骤
【Linux】详解动静态库的制作和使用&&动静态库在系统中的配置步骤
|
1天前
|
Linux Windows
虚拟机添加新硬盘之linux系统篇
虚拟机添加新硬盘之linux系统篇
|
1天前
|
Linux 编译器 调度
xenomai内核解析--双核系统调用(二)--应用如何区分xenomai/linux系统调用或服务
本文介绍了如何将POSIX应用程序编译为在Xenomai实时内核上运行的程序。
15 1
xenomai内核解析--双核系统调用(二)--应用如何区分xenomai/linux系统调用或服务
|
1天前
|
算法 Linux 调度
xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(一)--实时端socket创建流程
xenomai与普通linux进程之间通讯XDDP(一)--实时端socket创建流程
6 1
xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(一)--实时端socket创建流程
|
1天前
|
Linux 调度 数据库
|
1天前
|
存储 缓存 Linux
xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(三)--实时与非实时数据交互
本文介绍了Xenomai中的XDDP(Xenomai Distributed Data Protocol)通信机制,XDDP用于实时和非实时进程之间的数据交换。XDDP在Xenomai内核中涉及的数据结构和管理方式,以及创建XDDP通道后的实时端和非实时端连接过程。
7 0
xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(三)--实时与非实时数据交互
|
1天前
|
监控 安全 Linux
Linux系统入侵排查(三)
本文介绍了Linux系统入侵排查的相关知识。首先解释了进行系统入侵排查的原因,即当企业遭受黑客攻击、系统崩溃或其他安全事件时,需要迅速恢复系统并找出入侵来源。接着,重点讲述了日志入侵排查的重要性,因为日志文件记录了系统的重要活动,可以提供入侵行为的线索。
|
1天前
|
安全 Linux Shell
Linux系统入侵排查(二)
本文介绍了Linux系统入侵排查的步骤,包括检查历史命令记录、可疑端口和进程、开机启动项以及定时任务。作者强调了了解这些技能对于攻防两端的重要性,并提供了相关命令示例,如查看`/root/.bash_history`记录、使用`netstat`分析网络连接、检查`/etc/rc.local`和`/etc/cron.*`目录下的可疑脚本等。此外,还提到了如何查看和管理服务的自启动设置,以判断是否被恶意篡改。文章旨在帮助读者掌握Linux服务器安全维护的基本技巧。