马哥教育网络班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)
上一篇 2016-10-27 09:41
下一篇 2016-10-27 10:29

相关推荐

  • ☞Web服务器之apache

    Web服务器之apache http协议 telnet的使用 curl命令 httpd的相关配置 welcome.conf — 403 forbidden 修改监听的端口和地址 保持连接 DSO 定义物理主机站点文档 资源访问授权 路径别名Alias 本地httpd-manual 开启status 日志设定 虚拟主机 基于用户的访问控制 httpd压力测试 …

    Linux干货 2016-10-08
  • 七.Linux博客-2016年8月4日cat、cut、less、head、tail、wc、sort、uniq、grep

    格式说明: 操作 概念 命令 说明及举例 七.cat、cut、less、head、tail、wc、sort、uniq、grep cat cat -A a.txt 查看隐藏内容 cat -n a.txt 显示行号 cat -s a.txt 压缩空行,把多行空行压缩为一行 …

    Linux干货 2016-08-24
  • shell脚本基础

    编程基础 v程序:指令+数据 v程序编程风格: 过程式:以指令为中心,数据服务于指令(适合小型程序) 对象式:以数据为中心,指令服务于数据(适合大型程序) vshell程序:提供了编程能力,解释执行 程序的执行方式 v计算机:运行二进制指令; v编程语言: 低级:汇编 高级: 编译:高级语言–>编译器–>目标代码 java…

    Linux干货 2016-08-15
  • N25_第四周作业(补)

    1、复制/etc/skel目录到/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@EASTED tmp]# cp -r /etc/skel/ /home/tuser1/ [root@EASTED tmp]# cp …

    Linux干货 2017-01-02
  • Linux获取帮助的途径、history命令及文件系统结构

        在学习Linux的过程中,往往会遇到一些难以理解的问,这时我们就需要寻求帮助,下面是获取帮助的几个途径。 一、man(manual)手册(命令) 1、man手册介绍 (1)man手册是系统自带的联机帮助手册,善于利用man命令,可以帮我们解决遇到的大部分问题。 (2)man手册分为九个章节,每个章节是独立的。 章节1:表…

    Linux干货 2016-07-29
  • 磁盘管理

    磁盘设备 一切皆文件 所有的Linux中的设备都可以使用下面命令进行操作:open(), read(), write(), close()(这些是C的函数) 块设备:随机访问 字符设备:线性访问 设备号码: 主设备号:major number,标识设备类型 8是主要设备编号,代表类型 次设备号:minor number…

    Linux干货 2016-09-01