马哥教育网络21期+第十周练习博客

马哥教育网络21期+第十周练习博客

1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情)
CentOS启动流程:1,加电自检->Boot Sequence-->加载内核文件
BOOT Sequence中包含了MBR和GRUB
    MBR:记录磁盘扇区,共512字节,前446个字节BootLoader 后4*16的64个字节是存放分区信息
    grub:MBR中的前446个字节,她的左右要选择启动的内核。
加载内核启动文件:
    kernel初始化,探测可识别的所有硬件设备,记载硬件驱动,以只读方式挂载根文件系统,运行用户控件的下的第一个启动程序/sbin/init程序;


2、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区;
  (1) 为硬盘新建两个主分区;并为其安装grub;
  (2) 为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs;
  (3) 为rootfs提供bash、ls、cat程序及所依赖的库文件;
  (4) 为grub提供配置文件;
  (5) 将新的硬盘设置为第一启动项并能够正常启动目标主机;
    1,加载新的磁盘使用fdisk修改新的磁盘分区:
    [root@localhost ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x79c69071

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263        2610    18860310   83  Linux
然后将指定磁盘挂载至指定文件系统上
[root@localhost ~]# mkfs.ext4 /dev/sdb1
[root@localhost ~]# mkfs.ext4 /dev/sdb2
[root@localhost mnt]# ls
boot  sysroot
将指定的磁盘挂载至文件系统上:
    [root@localhost mnt]# mount /dev/sdb1 /mnt/boot/
    [root@localhost mnt]# mount /dev/sdb2 /mnt/sysroot/
将原来的grub文件中的内核文件,grub文件以及rootfs文件复制到新的grub文件中!
    [root@localhost ~]# cp -a /boot/vmlinuz-2.6.32-642.4.2.el6.x86_64 /mnt/boot/vmlinuz
    [root@localhost ~]# cp -a /boot/initramfs-2.6.32-642.4.2.el6.x86_64.img /mnt/boot/initramfs.img
安装grub到指定的grub中:
    [root@localhost boot]# grub-install --root-directory=/mnt /dev/sdb
    Probing 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@localhost boot]# cd /mnt/sysroot/
    [root@localhost sysroot]# mkdir -pv etc bin sbin lib lib64 dev proc sys tmp var usr home root mnt media
复制bash;ls;cat;所依赖的库文件
[root@localhost sysroot]# cp /bin/bash /bin/ls /bin/cat /mnt/sysroot/bin/
使用ldd查看所需要的软件的依赖的库文件:
[root@localhost sysroot]# cp `ldd /bin/bash | sed '1d'| grep -Eo "/.*[0-9] "` /mnt/sysroot/lib64/
[root@localhost sysroot]# cp `ldd /bin/ls | sed '1d'| grep -Eo "/.*[0-9] "` /mnt/sysroot/lib64/
[root@localhost sysroot]# cp `ldd /bin/cat | sed '1d'| grep -Eo "/.*[0-9] "` /mnt/sysroot/lib64/
在grub中记得修改配置文件:
     timeout=5
     title CentOS (Expres)
     root (hd0,0)
     kernel /vmlinuz ro root=/dev/sda2 selinux=0 init=/bin/bash
     initrd /initramfs.img



3、制作一个kickstart文件以及一个引导镜像。描述其过程。
1、anaconda的配置方式:
(1) 交互式配置方式;
(2) 通过读取事先给定的配置文件自动完成配置;
a)按特定语法给出的配置选项;
   kickstart文件;
  b)安装引导选项:
    text: 文本安装方式
    method: 手动指定使用的安装方法
  c)与网络相关的引导选项:
    ip=IPADDR
    netmask=MASK
    gateway=GW
    dns=DNS_SERVER_IP
    ifname=NAME:MAC_ADDR
          d)与远程访问功能相关的引导选项:
    vnc
    vncpassword='PASSWORD'
          e)指明kickstart文件的位置
    ks=
    DVD drive: ks=cdrom:/PATH/TO/KICKSTART_FILE
    Hard drive: ks=hd:/device/drectory/KICKSTART_FILE
    HTTP server: ks=http://host:port/path/to/KICKSTART_FILE
    FTP server: ks=ftp://host:port/path/to/KICKSTART_FILE
    HTTPS server: ks=https://host:port/path/to/KICKSTART_FILE
  f)启动紧急救援模式:
    rescue

2、 kickstart文件的格式:
  a)命令段:指明各种安装前配置,如键盘类型等;
  b)程序包段:指明要安装的程序包组或程序包,不安装的程序包等;
        %packages
        @group_name
        package
        -package
        %end
  c)脚本段:
        %pre: 安装前脚本
            d)运行环境:运行于安装介质上的微型Linux环境
       %post: 安装后脚本
   运行环境:安装完成的系统;

命令段中的命令: 必备命令 authconfig: 认证方式配置 authconfig –useshadow –passalgo=sha512 bootloader:bootloader的安装位置及相关配置 bootloader –location=mbr –driveorder=sda –append="crashkernel=auto crashkernel=auto rhgb rhgb quiet quiet" keyboard: 设定键盘类型 lang: 语言类型 part: 创建分区 rootpw: 指明root的密码 timezone: 时区 可选命令 install OR upgrade text: 文本安装界面 network firewall selinux halt poweroff reboot repo user:安装完成后为系统创建新用户 url: 指明安装源 3、创建kickstart文件的方式: (1) 直接手动编辑; 依据某模板修改; (2) 可使用创建工具:system-config-kickstart (CentOS 6) 依据某模板修改并生成新配置; 4、检查ks文件的语法错误:ksvalidator # ksvalidator /PATH/TO/KICKSTARTFILE 5、创建引导光盘: # mkisofs -R -J -T -v –no-emul-boot –boot-load-size 4 – boot-info-table -V “CentOS 6.8 x8664 boot” -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/boot.iso myiso/ [root@localhost ~]# yum install system-config-kickstart [root@localhost ~]# system-config-kickstart # 进入图形界面 4、写一个脚本 4、写一个脚本 (1) 能接受四个参数:start, stop, restart, status start: 输出“starting 脚本名 finished.” … (2) 其它任意参数,均报错退出; [root@localhost bin]# cat 12.sh #!/bin/bash # read -p "Please input parameters such as (start,stop,restart,status) :" option # if [ "$option" = 'start' ];then echo "starting $option finished" elif [ "$option" = 'stop' ];then echo "stop $option finished" elif [ "$option" = 'restart' ];then echo "restart $option finished" elif [ "$option" = 'status' ];then echo "status $option finished" elif [ "$option" != 'start' -a "$option" != 'stop' -a "$option" != 'restart' -a "$option" != 'status' ];then echo "please enter start|stop|restart|status." fi

5、写一个脚本,判断给定的用户是否登录了当前系统;
  (1) 如果登录了,则显示用户登录,脚本终止;
  (2) 每3秒钟,查看一次用户是否登录;
    [root@localhost bin]# cat user1.sh 
    #!/bin/bash
    #
    read -p "Please enter your user name:" username
    while true ;do
       if who | grep "^$username" &> /dev/null ;then
          break
       fi
       sleep 3
    done
    echo "$username logging."

6、写一个脚本,显示用户选定要查看的信息;
   cpu) display cpu info
   mem) display memory info
   disk) display disk info
   quit) quit
   非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;
    [root@localhost bin]# cat canshu2.sh 
    #!/bin/bash
    #
    cat << EOF
    cpu ) show cpu information;
    mem ) show memory information;
    disk) show disk information;
    quit) quit
    ============================
    EOF
    read -p "Please enter the above parameters:" option
    while [ "$option" != 'cpu' -a "$option" != 'mem' -a "$option" != 'disk' -a "$option" != 'quit' ];do
       read -p "Please input parameters again, must give the prompt characters:" option
    done
    case "$option" in
    cpu)
      lscpu
       ;;
    mem)
      cat /proc/meminfo
      ;;
    disk)
      fdisk -l
      ;;
    *)
     echo "Quit..."
     exit 0
      ;;
    esac

7、写一个脚本
  (1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;
  (2) 提示用户输入一个用户名或输入“quit”退出;
    当输入的是用户名,则调用函数显示用户信息;
    当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit:
    [root@localhost bin]# cat user4.sh 
    #!/bin/bash
    info() {
            id $username &> /dev/null
            if [ $? -ne 0 ];then
                    echo "the user is not exist!"
            else
                    grep "^$username" /etc/passwd |awk -F: '{printf "USER ID:%u\nUSER SHELL:%s\n" ,$3,$NF}'
            fi
    }
    read -p "please input a username or quit:" username
    while true;do
            if [ $username == "quit" ];then
                    exit 0
            else
                    info
                    read -p "please input a username or quit:" username
            fi
    done

8、写一个脚本,完成如下功能(使用函数)
   (1) 提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件;
   (2) 复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中;
   (3) 复制此命令依赖的各库文件至/mnt/sysroot目录下的对应的rootfs的路径上;规则同上面命令相关的要求;
    [root@localhost bin]# cat 13.sh 
    #!/bin/bash
    #
    read -p "Please enter a command parameter:" option
    #
    i=`which $option | grep "bin"`
    j () {
       cp $option /mnt/sysroot/$option
       echo "copy $option path to /mnt/sysroot$option over."
    }
    u () {
       lib=`ldd $option | grep -o "\[^[:space:]\{1,\}"`
       for i in $lib ;do
           cp $lib /mnt/sysroot/$option
           echo "copy $option lib to /mnt/sysroot/$option over."
       done
    }
    j
    u

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

(0)
wostopwostop
上一篇 2016-09-19 13:47
下一篇 2016-09-19 13:47

相关推荐

  • 初入linux_基础(一)

    初入Linux知识点总结(一) 一、理论知识 1、时间点: 1946年 第一代计算机ENIAC诞生于美国宾州大学,特点:块头大、效率低 数学家冯诺•依曼提出计算机体系结构组成部分:计算器、运算器、存储器、输入设备、输出设备 1969年 UNIX在美国贝尔实验室诞生 1984年 GNU项目和软件基金会成立 1991年 Linux在芬兰赫尔辛基诞生 2、计算机的…

    Linux干货 2017-02-18
  • Python函数式编程指南(四):生成器

    转自 http://www.cnblogs.com/huxi/archive/2011/07/14/2106863.html 生成器是迭代器,同时也并不仅仅是迭代器,不过迭代器之外的用途实在是不多,所以我们可以大声地说:生成器提供了非常方便的自定义迭代器的途径。 这是函数式编程指南的最后一篇,似乎拖了一个星期才写好,嗯…… 转载请注明原作者和原文地…

    Linux干货 2015-03-09
  • linux发展史——兽人永不为奴

      了解历史才能判断趋势。既然我们已经打算跳入运维这个坑,了解先烈的历史事迹是必须的。为什么说是必须的呢?现在我们处于一个知识时代,资本在追着知识跑,像海银资本这种vc都看不起中国的市场了,已经带着中国的资本在万恶资本主义的老美投资了。 1计算机硬件组成   操作系统成为了非常重要人类创造生产力的场所,很多人都开始追本…

    Linux干货 2016-10-14
  • 第二十一周作业

    1、回顾并详细总结MySQL的存储引擎、索引; 常用存储引擎的对比: 特点 MyISAM InnoDB MEMORY MERGE NDB 存储限制 有 64TB 有 没有 有 事务安全 支持 锁机制 表锁 行锁 表锁 表锁 行锁 B树索引 支持 支持 支持 支持 支持 哈希索引 支持 全文索引 支持 集群索引 支持 数据缓存 支持 支持 支持 索引缓存 支持…

    2017-07-29
  • 压缩打包工具

    linux系统常见的压缩命令:compress、gzip、zcat、bzip2、xz、zip 打包命令:tar、cpio compress 语法:compress  [-dfvcVr]  [-b maxbits]  [file …]        …

    2017-08-12
  • “lnmap实战之负载均衡架构(无高可用)”之新增keepalived高可用

    “lnmap实战之负载均衡架构(无高可用)”之新增keepalived高可用 我之前有一篇”lnmap实战之负载均衡架构(无高可用)“博客是专门部署了lanmap,之前没有做高可用,那么我们现在就把高可用补上去吧 这样我们照着之前的文档从新部署一下 1.机器结构如下: 192.168.42.150 node0 …

    Linux干货 2017-06-25

评论列表(1条)

  • 马哥教育
    马哥教育 2016-09-19 17:36

    格式是怎么搞的啊?别的都非常棒