马哥教育网络19期+第十周课程练习

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

a).POST加电自检;
b).读取BIOS来识别启动系统的存储设备
c).设备中的MBR,bootloader的前446字节,是选择启动的grub,后面的4*16个字节是存放分区信息的,最后两个字节是校验信息的;
d).启动init进程:
    读取/etc/inittab,选择默认级别;
    执行初始化系统脚本,/etc/rc.d/rc.sysinit;
    执行/etc/rc.d/rcX.d/下的脚本,我选择的默认级别是3,所以读取rc5.d下的所有脚本,来控制系统启动的时候启动或关闭那些服务。

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

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

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

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

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

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

[root@Hao ~]# 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 0x7d5160c9.
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-2610, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +5G

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (655-2610, default 655): 
Using default value 655
Last cylinder, +cylinders or +size{K,M,G} (655-2610, default 2610): +5G

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@Hao ~]# mke2fs -t ext4 /dev/sdb1
[root@Hao ~]# mke2fs -t ext4 /dev/sdb2
[root@Hao ~]# mkdir /newdir
[root@Hao ~]# mount /dev/sdb1 /newdir/
[root@Hao ~]# grub-install --root-directory=/newdir/ /dev/sdb1
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 /newdir//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@Hao ~]# cp /boot/initramfs-2.6.32-431.el6.x86_64.img /newdir/initramfs
[root@Hao ~]# cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /newdir/vmlinuz
[root@Hao ~]# mkdir -pv /newdir/boot/grub/
[root@Hao ~]# vim /newdir/boot/grub//grub.conf
        default=0
        timeout=3
        title CentOS6.5
        root (hd0,0)
        kernel /vmlinuz ro root=/dev/sda2 selinux=0 init=/bin/bash
        initrd /initramfs
[root@Hao ~]# umount /dev/sdb1
[root@Hao ~]# mount /dev/sdb2 /newdir/
[root@Hao ~]# mkdir -p /newdir/{bin,sbin,lib,lib64,etc,home,root,media,mnt,dev,tmp}
[root@Hao ~]# mkdir -p /newdir/{usr/{bin,sbin,lib,lib64},var{lib,lib64,log,local,cache},proc,sys,selinux}
[root@Hao ~]# cp /bin/{bash,ls,cat} /mnt/bin
[root@Hao ~]# cp `ldd /bin/{bash,ls,cat}| grep -eo "/lib.*[[:space:]]"  sort -u` /newdir/lib                                             
[root@Hao ~]# sync

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

1.anaconda应用程序支持通过读取配置文件的内容来完成安装过程的配置,配置文件称kickstart文件(在centos或红帽系列),文件在/root目录下,这种方法批量部署时需要用到。       
2.kickstart文件的组成:
  命令段
  程序包段:%packages, %end      #表示要安装的程序包
       @group                                   #安装包组
       package                                   #要装的包
       -package                                  #不安装package包
  脚本段:
       %pre                                    #安装前脚本:安装过程开始之前要执行的脚本;
       %post                                    # 安装后脚本:安装过程完成之后要执行的脚本;             
3.kickstart命令段,分为两类:必备命令 和 可选命令(辅助完成一些功能) 
   1.配置kickstart的方式:
       (1) 手动编辑配置文件;       
       (2) system-config-kickstart,GUI工具;安装命令system-config-kickstart的程序包名与命令名相同;                          
   2.安装过程如何获取并使用kickstart文件:
       (1)放置于安装程序能够获取到的位置;
         a.放置于光盘镜像;
         b.网络文件服务器;   
   3.安装过程过程中的安装引导选项
       (1).boot:
         linux     #启动默认安装模式
           linux text     #启动文本安装模式
                   
       (2).与网络相关的引导选项:
         ip=IPADDR
         netmask=MASK
         gateway=GW
         dns=DNS_SERVER_IP
         ifname=NAME:MAC_ADDR     #网卡名                              
       (3).指明获取 kickstart 文件的路径
         DVD drive  ks=cdrom:/path/to/kickstart_file
         HTTP Server  ks=http://server_ip:port/path/to/kickstart_file
         HTTPS Server ks=https://server_ip:port/path/to/kickstart_file
          FTP Server   ks=ftp://serverip:port/path/to/kickstart_file
         NFS Server   ks=nfs:server_ip:/path/to/kickstart_file
下面演示怎样创建一个引导光盘:
  (1) 首先准备工作目录,例如这里使用/tmp/cdrom;
      <span style="font-size:16px;font-family:'微软雅黑', 'Microsoft YaHei';">#mkdir /tmp/cdrom<br></span>    
               
  (2) 挂载系统发行光盘,复制isolinux目录至/tmp/cdrom;
     #mount /dev/sr0 /media/Centos-VERSION    
     #cp /media/Centos-VERSION  /tmp/cdrom/    
  (3)用命令system-config-keckstart制作kickstart文件, 复制制作好的kickstart文件ks.cfg至/tmp/cdrom某位置; 
      #system-config-kickstart#cp ks.cfg /tmp/cdrom    
  (4) 如果需要,可以编辑/tmp/cdrom/isolinux/isolinux.cfg,在某label后的append指令后附加ks=信息,从而实现自动调用光盘上提供的ks文件;
  (5) 创建iso镜像,采用命令mkios创建好之后会生成一个boot.iso文件,boot.iso文件就是我们的引导镜像,到这儿我们的镜像就制作完毕了 
    # mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "CentOS 6.6 X86_64 boot disk" -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/boot.iso cdrom/    
  (6)如果需要的话,可以用sftp将制作好的boot.iso镜像复制到windows上面,然后新建一个虚拟机,在虚拟机中调整启动次序为光盘启动,即我们的boot.iso,引导启动即可

4、写一个脚本

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

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

   …

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

#!/bin/bash

if [ $# -eq 1 ];then
case $1 in
start)
echo "starting $0 finished."
;;
stop)
echo "stopping $0 finished."
;;
restart)
echo "restart $0 finished."
;;
status)
echo "status $0 finished."
;;
*)
echo "Wrong input"
exit 1
;;
esac
else
echo "Can not be empty!"
fi

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

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

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

#!/bin/bash
#
read -p "input username: " username
while true; do
    if who | grep "^$username" &> /dev/null; then
        break
    fi
    sleep 3
done
 
echo "$username already logged in."

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

   cpu) display cpu info

   mem) display memory info

   disk) display disk info

   quit) quit

#!/bin/bash
#
cat << EOF
cpu) show cpu information;
mem) show memory information;
disk) show disk information;
quit) quit
============================
EOF
 
read -p "Enter a option: " option
while [ "$option" != 'cpu' -a "$option" != 'mem' -a "$option" != 'disk' -a "$option" != 'quit' ]; do
    read -p "Wrong option, Enter again: " 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: 

#!/bin/bash
#
function getinfo {
 
if id $op &> /dev/null;then
    grep -E '^('$op')\>' /etc/passwd | cut -d: -f1,3,7;read -p "Enter a username or quit:" op
else 
    read -p  "wrong argument,plsease enter a username or quit:" op
fi
}
 
read -p "Enter a username or quit:" op
while [[ $op != "quit" ]];do
    getinfo $op
done

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

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

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

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

#!/bin/bash
read -p "Enter a commond:" cmd
path() {
     whereis $cmd | cut -d' ' -f2
}
ldd $(path $1)| sed '1d'| grep -Eo "/.*[0-9] " > path.txt
cp $(path $1) /mnt/sysroot$(path $1)
while read line;do
    cp $line /mnt/sysroot$line
done < ./path.txt

原创文章,作者:Net19_口香糖,如若转载,请注明出处:http://www.178linux.com/23910

(0)
Net19_口香糖Net19_口香糖
上一篇 2016-07-16 22:26
下一篇 2016-07-16 22:26

相关推荐

  • python第二周

    #python数据结构(list) ## 分类 数值型:int、float、complex、bool 序列对象:字符串 str   列表  list   元组  tuple 键值对: 集合 set   字典 dict   ## 数字的处理函数 math.e  math.pi: 自如常数和π round():  四舍六入五去偶 floor():  取…

    Linux干货 2017-09-23
  • Linux之查找命令find简介

    查找命令find简介    Linux使用过程中难免会遇到查找功能,类似于Windows中的搜索功能,如果想要找某个文件在什么地方,什么格式的文件等等。在Linux中我们可以使用find、locate、which、whereis等命令。接下来我们就简单说一下如何使用这几个命令查找文件。 一、which which命令的作用是,在PATH变…

    Linux干货 2015-05-18
  • 内核与内核模块

    内核与内核模块 先来看一下,内核与内核模块放在哪?这里以centos6为例 内核: /boot/vmlinuz或/boot/vmlinuz-version 内核解压所需要RAMDisk: /boot/initrc(/boot/initrc-version) 内核模块:/lib/modules/version/kernel或/lib/modules/uname…

    2017-09-04
  • 文本处理工具及grep

    在日常的linux运维工作当中,我们经常要在一些文本当中抽取过滤出我们所需要的信息,从而达到我们的需求,需要特定的文本处理工具来帮我们完成此类操作 本章节主要讲解的内容有: 文件查看:cat 分页查看文本:less、more 抽取文件特定行数:head、tail 抽取文本特定列:cut 合并文本:paste 文本统计:wc 文本排序并统计:sort、uniq…

    Linux干货 2016-08-08
  • CentOS 6启动过程总结与GRUB问题修复

    一、CentOS 6 的启动流程 第一步:加电自检(POST) 主要检查硬件设备是否存在并能正常运行,如:CPU、内存、硬盘、风扇、输入输出设备等。自检功能主要是通过BIOS来实现的,BIOS程序是装载在一个硬件芯片CMOS上的,加电过程就是给CMOS通电,然后启动BIOS程序,BIOS程序会根据CMOS上面的一些配置信息区读取其他的硬件设备信息并检测其是否…

    Linux干货 2016-09-16
  • 第六周学习

    Linux学习第六周

    2018-08-04

评论列表(1条)

  • 马哥教育
    马哥教育 2016-07-17 20:24

    写的很好,排版也很漂亮,加油,第六题的lscpu是什么意思那?