马哥教育网络班22期+第10周课程练习

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

    系统启动流程:
      POST --> BootSequence(BIOS) --> BootLoader (MBR) --> Kernel (ramdisk) --> rootfs (readonly) --> /sbin/init ()
      1. POST: 加电自检
      2. BootSequence:按次序查找各引导设备,第一个有引导程序的设备即为本次启动要用的设备
      3. BootLoader:加载设备的引导程序
      4. Kernel:探测可识别到的所有硬件设备,加载硬件驱动程序(有可能借助于ramdisk加载驱动)
      5. rootfs:以只读方式挂载根文件系统
      6. init:运行用户空间的第一个应用程序:/sbin/init 进行初始化操作

2、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区;
  (1) 为硬盘新建两个主分区;并为其安装grub;
  (2) 为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs;
  (3) 为rootfs提供bash、ls、cat程序及所依赖的库文件;
  (4) 为grub提供配置文件;
  (5) 将新的硬盘设置为第一启动项并能够正常启动目标主机;

    为硬盘新建两个主分区
      [root@localhost ~]# fdisk /dev/sdg
      Command (m for help): n
      Command action
         e   extended
         p   primary partition (1-4)
      p
      Partition number (1-4): 1
      First cylinder (1-3916, default 1): 1
      Last cylinder, +cylinders or +size{K,M,G} (1-3916, default 3916): +200M

      Command (m for help): n
      Command action
         e   extended
         p   primary partition (1-4)
      p
      Partition number (1-4): 2
      First cylinder (27-3916, default 27): 
      Using default value 27
      Last cylinder, +cylinders or +size{K,M,G} (27-3916, default 3916): +10G
      [root@localhost ~]# partx -a /dev/sdg
      [root@localhost ~]# mke2fs -t ext4 /dev/sdg1
      [root@localhost ~]# mke2fs -t ext4 /dev/sdg2

    挂载第一个分区,并安装grub
      [root@localhost mnt]# mkdir /mnt/boot
      [root@localhost mnt]# mount /dev/sdg1 /mnt/boot
      [root@localhost mnt]# grub-install --root-directory=/mnt /dev/sdg

    复制内核和ramdisk文件到第一个分区
      [root@localhost mnt]# cp /boot/vmlinuz-2.6.32-642.el6.x86_64 /mnt/boot/vmlinuz
      [root@localhost mnt]# cp /boot/initramfs-2.6.32-642.el6.x86_64.img /mnt/boot/initramfs.img

    配置grub.conf文件
      [root@localhost mnt]# vim /mnt/boot/grub/grub.conf 
      default=0
      timeout=5
      title CentOS (Express)
              root (hd0,0)
              kernel /vmlinuz ro root=/dev/sda2 selinux=0 init=/bin/bash
              initrd /initramfs.img

    为第二个分区提供rootfs
      [root@localhost mnt]# mkdir /mnt/sysroot
      [root@localhost mnt]# mount /dev/sdg2 /mnt/sysroot
      [root@localhost sysroot]# mkdir -pv /mnt/sysroot/{etc,bin,sbin,lib,lib64,dev,proc,sys,tmp,var,usr,home,root,mnt,media}

    复制命令及其库文件
      [root@localhost mnt]# vim /tmp/cpcmd2.sh
      #!/bin/bash

      dst="/mnt/sysroot"
      [ ! -d $dst ] && mkdir $dst

      copycmd(){
              if `which $1 &>/dev/null`;then
                      cmddir=`which --skip-alias $1`
                      dir=`dirname $cmddir`
                      mkdir -pv ${dst}${dir}
                      cp $cmddir ${dst}${dir}/
              else
                      echo "command not found"
                      return 0
              fi
      }

      copylib(){
              liblist=$(ldd `which --skip-alias $1`|grep -Eo "/[^[:space:]]+")
              for libfile in $liblist;do
                      libdir=`dirname $libfile`
                      [ ! -d ${dst}${libdir} ] && mkdir ${dst}${libdir}
                      cp $libfile ${dst}${libdir}
              done
      }


      read -p "please input a command:" cmd

      while [ "$cmd" != "quit" ];do
              copycmd $cmd
              copylib $cmd
              echo "files copied"
              read -p "please input a command:" cmd

      done

      [root@localhost mnt]# bash /tmp/cpcmd2.sh
      [root@test tmp]# bash cpcmd.sh
      please input a command:bash      
      files copied
      please input a command:ls
      files copied
      please input a command:cat
      files copied
      please input a command:quit
 
    新建虚拟机,将新的硬盘设置为第一启动项,启动bash并测试ls,cat命令

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

    安装kickstart程序
      [root@localhost ~]# yum install system-config-kickstart

    制作ks.cfg文件
      [root@localhost ~]# system-config-kickstart &  #将ks.cfg保存在root 目录下
      [root@localhost ~]# cat ks.cfg    
      #platform=x86, AMD64, or Intel EM64T
      #version=DEVEL
      # Firewall configuration
      firewall --enabled --service=ssh
      # Install OS instead of upgrade
      install
      # Use CDROM installation media
      cdrom
      # Root password
      rootpw --iscrypted $1$7e5bbhUU$5ikWQctYMuPQc3O9Wko9d/
      # System authorization information
      auth  --useshadow  --passalgo=sha512
      # Use graphical install
      graphical
      firstboot --disable
      # System keyboard
      keyboard us
      # System language
      lang en_US
      # SELinux configuration
      selinux --enforcing
      # Installation logging level
      logging --level=info

      # System timezone
      timezone  Asia/Shanghai
      # System bootloader configuration
      bootloader --append="crashkernel=auto rhgb quiet" --location=mbr --driveorder="sda"
      # Partition clearing information
      clearpart --all --initlabel 
      # Disk partitioning information
      part /boot --fstype="ext4" --size=200
      part / --fstype="ext4" --size=10000
      part swap --fstype="swap" --size=1024

    挂载安装光盘
      [root@localhost ~]# mount /dev/cdrom /media/cdrom/
      [root@localhost ~]# mkdir -p /root/auto_install
      [root@localhost ~]# cp -arf /media/cdrom /root/auto_install/

    定位ks.cfg文件
      [root@localhost ~]# cp /root/ks.cfg /root/auto_install/cdrom/ks.cfg
      [root@localhost ~]# chmod +w /root/auto_install/cdrom/isolinux/isolinux.cfg
      [root@localhost ~]# vim /root/auto_install/cdrom/isolinux/isolinux.cfg
      label linux
        menu label ^Install or upgrade an existing system
        menu default
        kernel vmlinuz
        append initrd=initrd.img ks=cdrom:/ks.cfg  # 在这里添加ks文件路径参数
       
    制作自动安装光盘
      [root@localhost ~]# mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 \
      --boot-info-table -V "CentOS 6  Auto install" \
      -b isolinux/isolinux.bin -c isolinux/boot.cat \
      -o /root/centos6_auto_install.iso \
      /root/auto_install/cdrom/

4、写一个脚本
  (1) 能接受四个参数:start, stop, restart, status
   start: 输出“starting 脚本名 finished.”
   ...
  (2) 其它任意参数,均报错退出;

      [root@mail tmp]# vim start.sh

      #!/bin/bash

      case $1 in
              start)
                      echo "starting $0 finished."
                      ;;
              stop)
                      echo "stop $0 finished"
                      ;;
              restart)
                      echo "restart $0 finished"
                      ;;
              status)
                      echo "$0 status"
                      ;;
              *)
                      echo "error,please input {start|stop|restart|status}"
                      exit 1
      esac
      [root@mail tmp]# bash /tmp/start.sh start

5、写一个脚本,判断给定的用户是否登录了当前系统;
  (1) 如果登录了,则显示用户登录,脚本终止;
  (2) 每3秒钟,查看一次用户是否登录;

      [root@mail tmp]# vim login.sh
      #!/bin/bash

      read -p "Please input a user:" user
      id $user &> /dev/null
      if [ $? -eq 0 ];then
              while true;do
                      if who |grep "^$user" &> /dev/null;then
                              break
                      fi
                      sleep 3
              done
              echo "$user is login"
      else
              echo "$user is not exist"
      fi
      [root@mail tmp]# bash login.sh

6、写一个脚本,显示用户选定要查看的信息;
   cpu) display cpu info
   mem) display memory info
   disk) display disk info
   quit) quit
   非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;
   
      [root@mail tmp]# vim info.sh
      #!/bin/bash

      read -p "input cpu|mem|disk|quit :" option
      while [ "$option" != "cpu" -a "$option" != "mem" -a "$option" != "disk" -a "$option" != "quit" ];do
              read -p "input cpu|mem|disk|quit :" option
      done

      if [ "$option" == "cpu" ];then
              lscpu
      elif [ "$option" == "mem" ];then
              free -m
      elif [ "$option" == "disk" ];then
              fdisk -l /dev/[hs]d[a-z]
      else
              echo "quit"
              exit 0
      fi
      [root@mail tmp]# bash info.sh

7、写一个脚本
  (1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;
  (2) 提示用户输入一个用户名或输入“quit”退出;
    当输入的是用户名,则调用函数显示用户信息;
    当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit: 

      [root@test tmp]# vim userinfo.sh   
      #!/bin/bash

      userinfor(){
              uid=`id -u $username`
              ushell=`grep "^$username\>" /etc/passwd |cut -d: -f7`
              echo "USERNAME:$username UID:$uid SHELL:$ushell"
      }

      read -p "Please input a username|quit :" username

      while [ "$username" != "quit" ];do
              id $username &>/dev/null
              if [ $? -eq 0 ] ;then

                      userinfor $username
                      read -p "Please input a username|quit :" username
              else
                      echo "user not exist"
                      read -p "Please input a username|quit :" username

              fi

      done
      [root@test tmp]# bash userinfo.sh

8、写一个脚本,完成如下功能(使用函数)
   (1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件;
   (2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中;
   (3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求;

      [root@localhost mnt]# vim /tmp/cpcmd2.sh
      #!/bin/bash

      dst="/mnt/sysroot"
      [ ! -d $dst ] && mkdir $dst

      copycmd(){
              if `which $1 &>/dev/null`;then
                      cmddir=`which --skip-alias $1`
                      dir=`dirname $cmddir`
                      mkdir -pv ${dst}${dir}
                      cp $cmddir ${dst}${dir}/
              else
                      echo "command not found"
                      return 0
              fi
      }

      copylib(){
              liblist=$(ldd `which --skip-alias $1`|grep -Eo "/[^[:space:]]+")
              for libfile in $liblist;do
                      libdir=`dirname $libfile`
                      [ ! -d ${dst}${libdir} ] && mkdir ${dst}${libdir}
                      cp $libfile ${dst}${libdir}
              done
      }


      read -p "please input a command:" cmd

      while [ "$cmd" != "quit" ];do
              copycmd $cmd
              copylib $cmd
              echo "files copied"
              read -p "please input a command:" cmd

      done
      [root@localhost mnt]# bash /tmp/cpcmd2.sh

原创文章,作者:N22_上海_长清,如若转载,请注明出处:http://www.178linux.com/54836

(0)
N22_上海_长清N22_上海_长清
上一篇 2016-10-27 09:41
下一篇 2016-10-27 10:29

相关推荐

  • Nginx lnmp环境及https的实现

    一、http事务简明  request: <method> <URL> <VERSION> MHADERS <body> response: <version><status><reason phrase> <HEADERS> … <body&…

    2016-07-27
  • btrfs管理及应用

    一. 简介     btrfs有着强大的功能,它支持在多个及各种物理设备(包括RAID)上创建一个文件系统,并支持动态扩展或减小,支持快照功能,甚至快照的快照,支持子卷功能。 二. 如何使用btrfs文件系统?     我的准备材料:3块20G的硬盘 [root@localhost ~]#…

    Linux干货 2015-12-07
  • Shell脚本编程中的变量

    一、什么是变量?   变量来源于数学,是计算机语言中能储存计算结果或能表示值抽象概念。变量可以通过变量名访问 二、变量的种类有哪些? 本地变量 生效范围为当前shell进程;对当前shell之外的其它shell进程,包括当前shell的子shell进程均无效 环境变量 生效范围为当前shell进程及其子进程 局部变量 生效范围为当前shell进程中…

    Linux干货 2016-08-13
  • 数据结构-线性表

    1. 线性表:n个数据元素的有序集合。 线性表是一种常用的数据结构。在实际应用中,线性表都是以栈、队列、字符串、数组等特殊线性表的形式来使用的。由于这些特殊线性表都具有各自的特性,因此,掌握这些特殊线性表的特性,对于数据运算的可靠性和提高操作效率都是至关重要的。  线性表是一个线性结构,它是一个含有n≥0个结点的有限序列,对于其中的结点,有且仅有一个开始结点…

    Linux干货 2015-04-07
  • bash工作特性之命令执行状态返回值和命令行展开的讲解和实例

    命令执行状态返回值 在Linux bash中可以用命令执行状态返回值来判断命令是否运行成功,而不需去关心命令返回的结果是多少。 bash通过状态返回值来得到输出结果; 成功:返回值 0 [root@localhost tmp]# ls aaa [root@localhost tmp]# mkdir $(date +%H-%m-%S) [root@localh…

    Linux干货 2018-03-01
  • Linux进程管理及作业管理

    进程管理 进程概念 Linux内核:抢占式多任务 内核的功用:进程管理、文件系统、网络功能、内存管理、驱动程序、安全功能等 Process: 运行中的程序的一个副本,是被载入内存的一个指令集合 进程ID(Process ID,PID)号码被用来标记各个进程 UID、GID、和SELinux语境决定对文件系统的存取和访问权限 通常从执行进程的用户来继承 进程存…

    Linux干货 2017-05-08