1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情)
系统启动流程:POST -> BootSequence(BIOS)-> BootLoader(MBR)-> Kernel(ramdisk)-> rootfs(readonly)-> /sbin/init()
1>,加电自检:POST 通电后主板CMOS中的BIOS将进行硬件自检并简单设置,根据硬件设备等相关情况进入下一步; 2>,BIOS,引导加载次序:BootSequence 按次序查找各引导设备,第一个有引导程序的设备即为本次启动用动的设备;即:根据在BIOS中设置的系统启动顺序来搜索用于启动系统的驱动器(如:硬盘,BIOS会读取硬盘的第0磁道的第1扇区(MBR)的内容来引导启动)。 3>,Bootloader:引导加载器,用来引导系统加载的程序; 提供菜单,允许用户选择要启动的系统或不同的内核版本;把选定的内核装载到内存中的特定空间中,解压、展开,并把系统控制权限移交给内核; 4>,GRUB引导加载器 GRUB是BootLoader阶段的引导程序,CentOS主要通过GRUB引导; stage1:MBR,446Bytes; stage1_5:MBR之后的扇区,目的是让stage1中的BootLoader能识别到stage2所在的分区上的文件系统; stage2:磁盘分区; MBR:Master Boot Record,主引导记录,大小为512Bytes,包含: BootLoader:446Bytes 存储分区记录:64Bytes Magic Number:2Bytes 配置文件: /boot/grub/grub.conf 5>,Kernel初始化操作 探测可识别的所有硬件设备; 加载硬件驱动; 以只读方式挂在根文件系统; 运行用户空间的第一个应用程序:/sbin/init 6>,init:运行用户空间的第一个应用程序:/sbin/init进行初始化操作 CentOS6.x的7个运行级别: 0:关机 1:单用户模式,single,维护模式; 2:多用户模式,会启动网络功能,但不会启动NFS,维护模式; 3:多用户文本模式; 4:预留级别; 5:多用户图形化界面模式; 6:重启 修改运行级别的修改,修改配置文件:/etc/inittab 7>,运行初始化脚本 系统初始化脚本对应文件:/etc/rc.d/rc.sysinit 作用: 设置主机名; 设置欢迎信息; 激活udev和selinux; 挂载/etc/fstab文件中定义的文件系统; 检测根文件系统,并以读写方式重新挂载根文件系统; 设置系统时钟; 激活swap设备; 根据/etc/sysctl.conf文件设置内核参数; 激活lvm有软raid设备; 加载额外的驱动程序; 清理操作; 8>,启动系统服务 CentOS6下的系统服务脚本都放在/etc/rc.d/init.d/下; 查看及设置服务开机启动: # chkconfig --list # chkconfig iptables on 9>,设置登录终端 根据终端配置打印登录提示符;
2、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区;
(1) 为硬盘新建两个主分区;并为其安装grub;
(2) 为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs;
(3) 为rootfs提供bash、ls、cat程序及所依赖的库文件;
(4) 为grub提供配置文件;
(5) 将新的硬盘设置为第一启动项并能够正常启动目标主机;
[root@www boot]# fdisk /dev/sdbCommand (m for help): nPartition number (1-4): 1First cylinder (1-522, default 1): Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-522, default 522): +200M
Command (m for help): nPartition number (1-4): 2First cylinder (27-522, default 27): Using default value 27Last cylinder, +cylinders or +size{K,M,G} (27-522, default 522): +3G
Command (m for help): pDisk /dev/sdb: 4294 MB, 4294967296 bytes255 heads, 63 sectors/track, 522 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0xb38f53ee
Device Boot Start End Blocks Id System/dev/sdb1 1 26 208813+ 83 Linux/dev/sdb2 27 419 3156772+ 83 Linux
Command (m for help): wThe partition table has been altered![root@www boot]# cat /proc/partitionsmajor minor #blocks name
8 0 8388608 sda 8 1 204800 sda1 8 2 6144000 sda2 8 3 614400 sda3 8 16 4194304 sdb 8 17 208813 sdb1 8 18 3156772 sdb2[root@www ~]# mke2fs -t ext4 /dev/sdb1[root@www ~]# mke2fs -t ext4 /dev/sdb2[root@www ~]# mkdir /mnt/boot[root@www ~]# mount /dev/sdb1 /mnt/boot[root@www ~]# grub-install --root-directory=/mnt /dev/sdbProbing 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@www ~]# cd /mnt[root@www mnt]# ll总用量 5drwxr-xr-x 4 root root 1024 8月 16 22:14 bootdrwxr-xr-x. 4 root root 4096 8月 14 19:21 sysroot[root@www mnt]# ll boot总用量 13drwxr-xr-x 2 root root 1024 8月 16 22:14 grubdrwx------ 2 root root 12288 8月 16 22:08 lost+found [root@www mnt]# cp /boot/vmlinuz-2.6.32-642.el6.i686 /mnt/boot/vmlinuz-2.6.32-l42.el6.i686[root@www mnt]# cp /boot/initramfs-2.6.32-642.el6.i686.img /mnt/boot/initramfs-2.6.32-642.el6.i686.img[root@www mnt]# vi /mnt/boot/grub/grub.conf 编辑如下:defaut=0timeout=5title CenOS root (hd0,0) kernel /vmlinuz-2.6.32-l42.el6.i686 ro root=/dev/sda2 selinux=0 init=/bin/bash initrd /initramfs-2.6.32-642.el6.i686.img[root@www boot]#[root@www ~]# mount -t ext4 /dev/sdb2 /mnt/sysroot [root@www boot]#[root@www ~]# cd /mnt/sysroot[root@www sysroot]# mkdir -p bin sbin usr tmp root dev proc sys var mnt media lib lib64 home [root@www sysroot]# ll总用量 56drwxr-xr-x. 2 root root 4096 8月 14 19:25 bindrwxr-xr-x 2 root root 4096 8月 16 22:32 devdrwxr-xr-x 2 root root 4096 8月 16 22:32 homedrwxr-xr-x. 2 root root 4096 8月 14 19:21 libdrwxr-xr-x 2 root root 4096 8月 16 22:32 lib64drwxr-xr-x 2 root root 4096 8月 16 22:32 mediadrwxr-xr-x 2 root root 4096 8月 16 22:32 mntdrwxr-xr-x 2 root root 4096 8月 16 22:32 procdrwxr-xr-x 2 root root 4096 8月 16 22:32 rootdrwxr-xr-x 2 root root 4096 8月 16 22:32 sbindrwxr-xr-x 2 root root 4096 8月 16 22:32 sysdrwxr-xr-x 2 root root 4096 8月 16 22:32 tmpdrwxr-xr-x 2 root root 4096 8月 16 22:32 usrdrwxr-xr-x 2 root root 4096 8月 16 22:32 var 运用题8中的脚本把bash、ls、cat复制到/mnt/sysroot,我这里把它编辑为了脚本shell25.sh[root@www ~]# bash shell25.shplease input a command which you want to cp to /mnt/sysroot,and "quit" is over:bashAnother command(quit): lsAnother command(quit): catAnother command(quit): quit然后退出当前虚拟机,新建一个虚拟机,然后在BIOS处设定该硬盘为第一启动项,启动。3、制作一个kickstart文件以及一个引导镜像。描述其过程。
系统安装完成后,anaconda一般都会在root 家目录生成一个当前系统的kickstart 文件,我参考这个文件制作我自己的kickstart文件。[root@www ~]# cp anaconda-ks.cfg myks.cfg[root@www ~]# vi myks.cfg 下面是myks.cfg 内容:# Kickstart file automatically generated by anaconda.and I modify it by myself
#version=DEVELinstallcdromlang zh_CN.UTF-8keyboard usnetwork --onboot no --device eth0 --bootproto dhcp --noipv6rootpw --iscrypted $6$IjI6cJDojRCvIVR6$I59v2xYd8ikloSXoQH6MVZdUv/ikm0yNqBhrO8jXrlHY7iqbnE86sPPJ8sg.yfqs6oYCuG8TvogX.kjGY15nI1firewall --service=sshauthconfig --enableshadow --passalgo=sha512selinux --enforcingtimezone --utc Asia/Shanghaibootloader --location=mbr --driveorder=sda --append="nomodeset crashkernel=auto rhgb quiet"# The following is the partition information you requested# Note that any partitions you deleted are not expressed# here so unless you clear all partitions first, this is# not guaranteed to workclearpart --all
part /boot --fstype=ext4 --size=200part / --fstype=ext4 --size=6000part swap --size=600
repo --name="CentOS" --baseurl=cdrom:sr0 --cost=100
%packages --nobase@core%end
制作引导镜像的过程:1.把安装光盘放入光驱2.[root@www ~]# mkdir /media/cdrom[root@www ~]# mount /dev/cdrom /media/cdrommount: block device /dev/sr0 is write-protected, mounting read-only[root@www ~]# ls /media/cdromCentOS_BuildTag GPL isolinux RELEASE-NOTES-en-US.html RPM-GPG-KEY-CentOS-6 RPM-GPG-KEY-CentOS-Security-6 TRANS.TBLEULA images Packages repodata RPM-GPG-KEY-CentOS-Debug-6 RPM-GPG-KEY-CentOS-Testing-6[root@www ~]# mkdir /tmp/myiso[root@www myiso]#cd /tmp/myiso[root@www myiso]# cp /media/cdrom/isolinux /tmp/myiso -a -r[root@www myiso]# cp -r -a /root/myks.cfg /tmp/myiso[root@www myiso]# ll总用量 8dr-xr-xr-x. 2 root root 4096 5月 22 12:17 isolinux-rw-------. 1 root root 903 8月 17 19:20 myks.cfg[root@www myiso]# mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "CentOS 6.6 x86_64 boot" -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/boot.iso myiso/这样我们就在/root目录下创建了名为boot.iso的光盘引导镜像4、写一个脚本
(1) 能接受四个参数:start, stop, restart, status
start: 输出“starting 脚本名 finished.”
…
(2) 其它任意参数,均报错退出;
[root@zhouyong148 N21_10]# vi 04.sh #!/bin/bash # read -p "please input start|stop|restat|status :" case $1 in start) echo "starting $0 finished." exit 0 ;; stop) echo "stoping $0 finished." exit 0 ;; restart) echo "restarting $0 finished." exit 0 ;; status) echo "status $0 finished." exit 0 ;; *) echo "error, Usage:$0 start|stop|restart|status" exit 1 ;; esac
[root@zhouyong148 N21_10]# 04.sh stop please input start|stop|restat|status :stop stoping ./04.sh finished. [root@zhouyong148 N21_10]# 04.sh start please input start|stop|restat|status :start starting ./04.sh finished. [root@zhouyong148 N21_10]# 04.sh start stop please input start|stop|restat|status :start starting ./04.sh finished. [root@zhouyong148 N21_10]# 04.sh please input start|stop|restat|status :status error, Usage:./04.sh start|stop|restart|status [root@zhouyong148 N21_10]# 04.sh please input start|stop|restat|status :start error, Usage:./04.sh start|stop|restart|status [root@zhouyong148 N21_10]# 04.sh please input start|stop|restat|status :stop error, Usage:./04.sh start|stop|restart|status
5、写一个脚本,判断给定的用户是否登录了当前系统;
(1) 如果登录了,则显示用户登录,脚本终止;
(2) 每3秒钟,查看一次用户是否登录;
[root@zhouyong148 N21_10]# vi 05.sh #!/bin/bash # read -p "Enter a User Name:" userName until who | grep "^$userName" &> /dev/null; do sleep 3 echo "is $userName login ? checked per 3 second." done echo "$userName is already login!"
[root@zhouyong148 N21_10]# 05.sh Enter a User Name:test is test login ? checked per 3 second. is test login ? checked per 3 second.
[root@zhouyong148 N21_10]# 05.sh Enter a User Name:root root is already login!
6、写一个脚本,显示用户选定要查看的信息;
cpu) display cpu info
mem) display memory info
disk) display disk info
quit) quit
非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;
[root@zhouyong148 N21_10]# vi 06.sh #!/bin/bash # cat << EOF cpu) display cpu information; mem) display memory information; disk) display disk information; quit) quit EOF read -p "input a option cpu|mem|disk|quit: " option while [ "$option" != 'cpu' -a "$option" != 'mem' -a "$option" != 'disk' -a "$option" != 'quit' ];do read -p "Error, Please input cpu|mem|disk|quit:" option done case "$option" in cpu) lscpu ;; mem) cat /proc/meminfo ;; disk) disk -l ;; *) echo "quit..." exit 0 ;;
[root@zhouyong148 N21_10]# 06.sh cpu) display cpu information; mem) display memory information; disk) display disk information; quit) quit input a option cpu|mem|disk|quit: test Error, Please input cpu|mem|disk|quit:cpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 2 On-line CPU(s) list: 0,1 Thread(s) per core: 1 Core(s) per socket: 2 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 62 Stepping: 4 CPU MHz: 2500.000 BogoMIPS: 5000.00 Hypervisor vendor: VMware Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 10240K NUMA node0 CPU(s): 0,1
7、写一个脚本
(1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;
(2) 提示用户输入一个用户名或输入“quit”退出;
当输入的是用户名,则调用函数显示用户信息;
当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit:
[root@zhouyong148 N21_10]# vi 07.sh
#!/bin/bash
#
func1() {
useruid=$(grep "^$1" /etc/passwd|cut -d: -f3)
usershell=$(grep "^$1" /etc/passwd|cut -d: -f7)
echo -e -n "user $1 UID is : $useruid\n SHELL is : $usershell\n"
}
read -p "Please input a userName|quit: " option
while [ $option != "quit" ];do
id $option &> /dev/null && result=0 || result=1
case $result in
0)
func1 $option
;;
1)
read -p "Please input a userName|quit: " option
continue
;;
esac
read -p "Please input a userName|quit: " option
done
[root@zhouyong148 N21_10]# sh 07.sh Please input a userName|quit: root user root UID is : 0 SHELL is : /bin/bash Please input a userName|quit: test Please input a userName|quit: quit
8、写一个脚本,完成如下功能(使用函数)
(1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件;
(2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中;
(3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求;
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
target=/mnt/sysroot
[ -d $target ] || mkdir /mnt/sysroot
read -p "please input a command which you want to cp to /mnt/sysroot,and \"quit\" is over:" cmdname
libcp() {
for lib in $(ldd $1 | grep -o "[^[:space:]]*/lib[^[:space:]]*"); do
libdir=$(dirname $lib)
[ -d $target$libdir ] || mkdir -p $target$libdir
[ -f $target$lib ] || cp $lib $target$lib
done
}
while [ "$cmdname" != 'quit' ]; do
if ! which $cmdname &> /dev/null; then
read -p "No such command, enter again: " cmdname
continue
fi
cmdname=$(which --skip-alias $cmdname)
cmnddir=$(dirname $cmdname)
[ -d $target$cmnddir ] || mkdir -p $target$cmnddir
[ -f $target$cmdname ] || cp $cmdname $target$cmdname
libcp $cmdname
read -p "Another command(quit): " cmdname
done
原创文章,作者:365,如若转载,请注明出处:http://www.178linux.com/58890

