N21天天第十周课程练习

1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情)

CentOS主机按以下顺序启动
1、POST加电自检
2、BIOS读取CMOS中的BIOS设置的参数来识别基础硬件,寻找到启动设备
3、MBR
1)读取启动设备MBR中前446字节的bootloader
2)读取MBR后的扇区用来识别grub以及内核kernel所在的区域
3)启动grub
4、GRUB显示菜单界面,选择运行内核kernel;配置文件是/boot/grub/grub.conf
1)提供菜单、并提供交互式接口
2)加载用户选择的内核或操作系统
3)为菜单提供了保护机制
5、KERNEL自身初始化
1)探测可识别到的所有硬件设备
2)加载硬件驱动程序(有可能会借助于ramdisk加载驱动)
3)以只读方式挂载根文件系统
4)运行用户空间的第一个应用程序:/sbin/init
6、INIT
运行/sbin/init程序,配置文件/etc/inittab和/etc/init/*.conf 
设置默认运行级别如id:3:initdefault
运行系统初始化脚本/etc/rc.d/rc.sysinit
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/init.d)
设置登录终端

2、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区;

  (1) 为硬盘新建两个主分区;并为其安装grub;

  (2) 为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs;

  (3) 为rootfs提供bash、ls、cat程序及所依赖的库文件;

  (4) 为grub提供配置文件;

  (5) 将新的硬盘设置为第一启动项并能够正常启动目标主机;

为硬盘新建两个主分区;并为其安装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 0x495813a2.
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): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-10443, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +1G
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (133-10443, default 133): 
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-10443, default 10443): +2G
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
[root@localhost ~]# mke2fs -t ext4 /dev/sdb1
[root@localhost ~]# mke2fs -t ext4 /dev/sdb2
[root@localhost ~]# mkdir /mnt/{boot,sysroot}
[root@localhost ~]# mount /dev/sdb1 /mnt/boot/
[root@localhost ~]# mount /dev/sdb2 /mnt/sysroot/
[root@localhost ~]# grub-install --root-directory=/mnt/ /dev/sdb
为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs;
[root@localhost ~]# cp -r /boot/initramfs-2.6.32-504.el6.x86_64.img /mnt/boot/vmlinuz
[root@localhost ~]# cp /boot/initramfs-2.6.32-504.el6.x86_64.img /mnt/boot/initramfs.img
[root@localhost ~]# ls /mnt/boot/
grub  initramfs.img  lost+found  vmlinuz
为rootfs提供bash、ls、cat程序及所依赖的库文件
[root@localhost ~]# mkdir -p /mnt/sysroot/{bin,lib64}
[root@localhost ~]# cp -r /bin/{bash,ls,cat} /mnt/sysroot/bin/
[root@localhost ~]# cp $(ldd /bin/bash | awk '$3~/^\/lib64/{print $3}') /mnt/sysroot/lib64/
[root@localhost ~]# cp $(ldd /bin/ls | awk '$3~/^\/lib64/{print $3}') /mnt/sysroot/lib64/ 
[root@localhost ~]# cp $(ldd /bin/cat | awk '$3~/^\/lib64/{print $3}') /mnt/sysroot/lib64/  
为grub提供配置文件
[root@localhost ~]# vim /mnt/boot/grub/grub.conf
default=0
timeout=5
title CentOS 6.6 (Test)
root (hd0,0)
kernel /vmlinuz ro root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs.img
在BIOS中选择sdb作为启动盘

3、制作一个kickstart文件以及一个引导镜像。描述其过程。

可以直接手动编辑或使用工具在桌面模式下用system-config-kickstart(centos6.x)来创建ks.cfg
#命令段
firewall --disabled//禁用防火墙
install//执行新安装
cdrom//用光盘安装
lang en_US.UTF-8//默认安装语言
keyboard us//选择键盘
rootpw  --iscrypted $6$AF1u4TWzFxa/SzHI$yJvJdbhyw/2rG8dtr.PY6c15sZ.qNc6US/z7PMQ4lADdlIis/qIMO738b9czXK/rX1YDiL7Uv/C6Bi99ig8ov0//管理员加密密码
authconfig --enableshadow --passalgo=sha512
selinux --enforcing//激活selinux
logging --level=info//信息等级
timezone --utc Asia/Shanghai//系统时区
bootloader --location=mbr --driveorder=sdb --append="crashkernel=auto rhgb quiet"
clearpart --all//删除所有现在分区
part /boot --fstype=ext4 --size=200//分区挂载
part / --fstype=ext4 --size=40960
part swap --size=4096
#脚本段
%pre//安装前脚本
echo "start install"
%end
%post//安装后脚本
echo "install end"
#程序包段
%packages
@chinese-support//中文支持
@development//开发工具
@graphical-desktop-clients//图形化工具
@remote-desktop-clients//远程桌面客户端
%end
简单引导镜像光盘制作:
1)复制系统安装光盘除Packages和repodata外的所有目录下的所有文件到一自定义目录(/centos6.6)
mount -r /dev/sr0 /mnt/
mkdir /centos6.6
cp -r /mnt/{CentOS_BuildTag,isolinux,R*,E*,GPL,T*,images} /centos6.6/
cd /centos6.6/
2)创建引导光盘:把/centos6.6目录创建为光盘镜像boot.iso
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 /centos6.6/
简单自动安装系统光盘制作
1)复制安装系统光盘的所有目录下的所有文件和ks.cfg文件到一自制目录(/centos6.6)
cd /centos6.6/
cp -r /mnt/* .
cp /root/ks.cfg .
2)在/centos6.6/isolinux/isolinux.cfg文件中append initrd=initrd.img条目中添加ks文件读取路径 ks=cdrom:/ks.cfg
3)创建引导光盘:把centos6.6目录创建为光盘镜像centos.iso 
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 /centos6.6/

4、写一个脚本

  (1) 能接受四个参数:start, stop, restart, status

   start: 输出“starting 脚本名 finished.”

   …

  (2) 其它任意参数,均报错退出;

#!/bin/bash
#
case $1 in
    start)
        echo "starting $0 finished."
        ;;
    stop)
        echo "stoping $0 finished."
        ;;
    restart)
        echo "restart $0 finished."
        ;;
    *)
        echo "error,exit!"
        exit 1
        ;;
esac

  5、写一个脚本,判断给定的用户是否登录了当前系统;

  (1) 如果登录了,则显示用户登录,脚本终止;

  (2) 每3秒钟,查看一次用户是否登录;

#!/bin/bash
while [ 0 ];do
    if `w | grep "^$1\>" &> /dev/null`;then
        echo  "$1 is login."
        exit 0
    else
        sleep 3
    fi
done

6、写一个脚本,显示用户选定要查看的信息;

   cpu) display cpu info

   mem) display memory info

   disk) display disk info

   quit) quit

   非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;

#!/bin/bash
#
while [ 0 ];do
    echo "please input a option:"
    echo "cpu)  display cpu info"
    echo "mem)  display memory info"
    echo "disk) display disk info"
    echo "quit) quit"
    read test
    case $test in
    cpu)
        lscpu
        ;;
    mem)
        cat /proc/meminfo
        ;;
    disk)
        fdisk -l
        ;;
    quit)
        exit 0
        ;;
    *)
        echo "you have wrong choose."
        continue
        ;;
    esac
done

7、写一个脚本

  (1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;

  (2) 提示用户输入一个用户名或输入“quit”退出;

    当输入的是用户名,则调用函数显示用户信息;

    当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit: 

#!/bin/bash
#
function userinfo {
    if id $username &> /dev/null;then
        id -u $username
        grep "^$username\>" /etc/passwd | cut -d: -f7
    else
        echo "wrong user!"
    fi
echo $username
}
while true;do
    read -p "Please input a username or quit:" username
    if [ $username == "quit" ];then
        exit 0
    else
        userinfo $username
    fi
done

8、写一个脚本,完成如下功能(使用函数)

   (1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件;

   (2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中;

   (3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求;

#!/bin/bash
#
libcp(){
for lib in $(ldd $1 | grep -o "[^[:space:]]*/lib[^[:space:]]*");do
    libdir=$(dirname $lib)
    [ -d $target$libdir ] || mkdir $target$libdir
    [ -f $target$lib ] || cp $lib $target$lib
done
}
target=/mnt/sysroot
[ -d $target ] || mkdir $target
read -p "Input a command:" command
while [ "$command" != 'quit' ];do
    if ! which $command &> /dev/null;then
        read -p "No such command,enter again:" command
        continue
    fi
    command=$(which --skip-alias $command)
    cmnddir=$(dirname $command)
    [ -d $target$cmnddir ] || mkdir -p $target$cmnddir
    [ -f $target$command ] || cp $command $target$command
    libcp $command
    read -p "Another command(quit):" command
done

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

(0)
上一篇 2016-09-26 07:43
下一篇 2016-09-26 07:44

相关推荐

  • Linux学习总结&day07-正则表达式&文本处理工具

    第一部分、正则表达式     1、什么是正则表达式     正则表达式就是处理字符串的方法,它是以行为单位来进行字符串的处理行为,正则表达式通过一些特殊符号的辅助,可以让用户轻易的达到查找、删除、替换某特定字符串的处理程序。     正则表…

    Linux干货 2016-08-08
  • Linux文件权限管理及目录文件的深入理解。

    文件权限及目录 初学Linux,感觉这个东西该复杂,而且逻辑非常的强。难~! 自己根据学习到的理论和实践,得出的对文件权限,进程,以及特殊权限的深入理解。希望能解决初学者对于权限的困惑。如有错误请指正。 文件的权限,指定的是什么? 是文件的权限位上的权限,针对三类用户,任何用户都必须是三类用户中的一种,属主属组和其他人的权限rwx   &…

    Linux干货 2016-08-10
  • 第八周作业

    第八周作业 1、写一个脚本,使用ping命令推测172.16.250.1-172.16.250.254之间的所有主机的在线状态; 在线的主机使用绿色显示; 不在线的主机使用红色显示;   #!/bin/bash for i in {1..254};do if ping -c 1 -w 1 192.168.1.$i &> /dev/nu…

    Linux干货 2017-11-13
  • 【Linux基础】计算机及操作系统基础

    【Linux基础】计算机及操作系统基础 计算机的组成及其功能 计算机的组成 简单来说,计算机应由两部分组成:第一部分:硬件 冯诺依曼结构体系 当前计算机主要是基于冯诺依曼体系结构设计的,在该体系中,计算机硬件由5个部件组成。分别是:运算器、控制器、存储器、输入设备和输出设备。而现今运算器和控制器一般集成在一起,叫中央处理器(Central Processin…

    Linux干货 2018-03-04
  • Linux系统的文件管理命令及bash的相关工作特性

    熟悉Linux系统的文件管理命令,了解bash的工作特性,加强练习并掌握其内容。

    2017-09-30
  • DNS

    简介     DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串。通过主机名,最终得到该主机名对应的IP地址的过程叫做域名解析(或主机名解析)。DNS协议运行在UDP协议之上,使用端口号5…

    Linux干货 2016-11-15

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-27 09:26

    写的很好,最后一个脚本可以优化一下,