马哥教育网络班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
下一篇 2016-10-27

相关推荐

  • Linux用户和组管理及相关命令详解

        概要:在学习Linux的初级阶段,用户管理是基础管理的重要部分,任何命令的运行,系统的正常运转,均离不开用户管理的内容,本篇就为大家介绍下Linux用户管理的一些基础概念和一些基础命令的用法。内容主要分为以下三个部分:        &nbs…

    Linux干货 2016-08-02
  • N25-第四周作业

    1、复制/etc/skel目录为/home/tuse1,要求/home/tuse1及其内部文件的属组和其它用户均没有任何访问权限。   cp -r /etc/skel /home/tuser1;chmod go= /home/tuse1 2、编辑/etc/group文件,添加组hadoop    vi /et…

    Linux干货 2016-12-27
  • 阿里云修改hostname主机名的一点小技巧

    CentOS 7以后修改主机名一般使用: hostnamectl set-hostname newhostname 如果仍然无效,使用vim打开/etc/cloud/cloud.cfg,将 preserve_hostname=fale 改为 preserve_hostname=true 即可。 以上在阿里云ECS上亲测有效,使用了网上查阅…

    Linux干货 2017-01-09
  • 搭建属于自己的DNS

    1、搭建主renzituo.com服务器     ①、在192.168.10.4主机上安装bind程序             yum -y install bind   &nbs…

    2017-04-16
  • linux文件管理类命令学习总结练习

    1、常用文件管理类命令:cp, mv, rm   cp: copy命令       单元复制 cp [OPTION]… [-T] SOURCE DEST                     DEST不存在…

    Linux干货 2016-11-06
  • N26 第三周作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。    who | cut -d" " -f1 | sort -u    2、取出最后登录到当前系统的用户的相关信息。    who | tail -1 3、取…

    Linux干货 2017-02-16