马哥教育网络班21期-第十周课程练习

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

 a,post:加电自检,通电搜索并检查物理设备的状态

 b,bootsequeence(BIOS):选择启动顺序

 c,bootloader(MBR):加载主引导程序 bootloader,硬盘分区表到内存

 d,加载内核kernel

 e,只读方式挂载根分区文件系统(rootfs)

 f,系统初始化(运行/sbin/init)

 g,根据 /etc/inittab 中的启动级别,运行系统初始化脚本 /etc/rc.d/rc.sysinit,打开或关闭对应启动级别下的服务(运行 /etc/rc.d/rcx.d/ 下的脚本)

 h,启动终端

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

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

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

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

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

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

[root@server ~]# 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 0x9056db41.

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-2088, default 1): 

Using default value 1

Last cylinder, +cylinders or +size{K,M,G} (1-2088, default 2088): +256M

Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 2

First cylinder (35-2088, default 35):         

Using default value 35

Last cylinder, +cylinders or +size{K,M,G} (35-2088, default 2088): +4G

Command (m for help): w

The partition table has been altered!

[root@server ~]# mkfs.ext4 /dev/sdb1;mkfs.ext4 /dev/sdb2

[root@server ~]# mkdir /mnt/{boot,sysroot}

[root@server ~]# mount /dev/sdb1 /mnt/boot/ ; mount /dev/sdb2 /mnt/sysroot/

[root@server ~]# ls /mnt/{boot,sysroot}

[root@server ~]# grub-install –root-directory=/mnt /dev/sdb #复制 GRUB 镜像到 /mnt 的 boot 目录下

[root@server ~]# cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/boot/

[root@server ~]# cp /boot/initramfs-2.6.32-431.el6.x86_64.img /mnt/boot/

[root@server ~]# vim /mnt/boot/grub/grub.conf #新建一个 grub 配置文件

default=0

timeout=15

title CentOS6.5 homemake

  root (hd0,0)

  kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/sdb2 selinux=0 init=/bin/bash

  initrd /initramfs-2.6.32-431.el6.x86_64.img

使用第8题的脚本复制bash、ls、cat响应文件到 rootfs

新建一个虚拟机挂载此虚拟机的sdb磁盘启动

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

####制作 ks 文件的方法

  a 通过 system-config-kickstart 生成ks配置文件

  b 通过复制已有系统ks配置文件并自定义配置

[root@cent7-server ~]# cp /root/anaconda-ks.cfg  myks.cfg    #复制root目录下的ks配置文件,修改其中选项

[root@cent7-server ~]# vim myks.cfg 

#version=RHEL7

# System authorization information

auth –enableshadow –passalgo=sha512

# Use CDROM installation media

cdrom

# Run the Setup Agent on first boot

firstboot –enable

ignoredisk –only-use=sda

# Keyboard layouts

keyboard –vckeymap=us –xlayouts='us'

# System language

lang en_US.UTF-8

# Network information

network  –bootproto=static –device=ens160 –onboot=on –ipv6=auto

network  –hostname=localhost.localdomain

# System timezone

timezone China/Beijing –isUtc

# System bootloader configuration

bootloader –location=mbr –boot-drive=sda

# Partition clearing information

clearpart –none –initlabel

part  /boot –fstype=ext4  –size=200

part  / –fstype=ext4  –size=16000

part /swap –size=1024

repo –name="CentOS"  –baseurl=cdrom:sr0 –cost=100

%packages

@base

@core

%end

[root@cent7-server ~]# ksvalidator myks.cfg    #检查配置文件语法

#### 制作引导盘

[root@cent7-server ~]# mkdir /media/test-cdrom

[root@cent7-server ~]# mount /dev/cdrom /media/test-cdrom/

mount: /dev/sr0 is write-protected, mounting read-only

[root@cent7-server ~]# mkdir /tmp/test-iso

[root@cent7-server ~]# cd /tmp/test-iso/

[root@cent7-server test-iso]# cp -a -r /media/test-cdrom/isolinux/ .

[root@cent7-server test-iso]# cp ~/myks.cfg .

[root@cent7-server test-iso]# ll

total 8

drwxr-xr-x. 2 root root 4096 Jul  4  2014 isolinux

-rw——-. 1 root root  804 Oct 17 10:03 myks.cfg

[root@cent7-server test-iso]# mkisofs -R -J -T -v –no-emul-boot –boot-load-size 4 –boot-info-table -V "my-test-Cent7-boot" -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/boot.iso /tmp/test-iso/

4、写一个脚本

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

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

   …

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

#!/bin/bash
#

if [ $# -ne 1 ];then
  echo "Usage: $0 start|stop|restart|status"
  exit
fi
case $1 in
  start)
    echo "starting $0 finished" ;;
  stop)
    echo "stop $0 finished" ;;
  restart)
    echo "restart $0 finished" ;;
  status)
    echo "$0 status" ;;
  *)
    echo "wrong argument"
    echo "Usage: $0 start|stop|restart|status" ;;
esac

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

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

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

#!/bin/bash
#
if [ $# -ne 1 ];then
  echo "Usage: $0 username"
  exit
fi

while id $1 &> /dev/null ;do
  if who | grep "\<$1\>" &> /dev/null;then
     echo "User $1 is online"
     exit
  else
     sleep 3
     echo "User $1 is offline"
  fi
done

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

   cpu) display cpu info

   mem) display memory info

   disk) display disk info

   quit) quit

   非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;

#!/bin/bash
#
while true;do
  read  -p "input your select: cpu|mem|disk|quit  " text
  case $text in
    cpu)
      cat /proc/cpuinfo && exit;;
    mem)
      free -m && exit;;
    disk)
      df -h && exit;;
    quit)
      exit ;;
    *)
      echo "wrong choice"
      continue ;;
  esac
done

7、写一个脚本

  (1) 用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;

  (2) 提示用户输入一个用户名或输入“quit”退出;

    当输入的是用户名,则调用函数显示用户信息;

    当用户输入quit,则退出脚本;进一步地:显示键入的用户相关信息后,再次提醒输出用户名或quit: 

#!/bin/bash
#
UserInfo () {
  id -u $username
  sed -n "/\<$username\>/p" /etc/passwd | awk -F: '{print $7}'
}
while true;do
  read -p "pls input username:  "  username
  if [ -z "$username" ];then
     echo "you must input username"
     exit
  fi
  if id $username &> /dev/null;then
     UserInfo $username
     continue
  elif [ "$username" == "quit" ];then
     exit
  else
     echo "invaild username"
  fi
done

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

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

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

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

#!/bin/bash
#
Judge () {
  if [ -z "$cmd" ] || ! which $cmd &> /dev/null ;then
    echo "wrong command!"
    exit
  fi
}
Copy_cmd () {
  [ ! -d $cmd_dir ] && mkdir -p $cmd_dir &> /dev/null
  cp $(which $cmd) $cmd_dir
}
Copy_lib () {
  echo $lib_path
  mkdir -p /mnt/sysroot/lib{,64}
  for list in $lib_path;do
    cp  $list  /mnt/sysroot$list
  done
}

read -p "pls input an executable command: " cmd
cmd_path=$(which $cmd | grep  -o "^/.*\/")
cmd_dir=/mnt/sysroot$(dirname $(which $cmd))
lib_path=$(ldd $(which $cmd) | grep -o "/[^[:space:]]\{1,\}")
Judge
Copy_cmd
Copy_lib

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

(0)
Net21_木头Net21_木头
上一篇 2016-10-24 09:10
下一篇 2016-10-24 09:10

相关推荐

  • Bash 的配置文件

    有时候,在定义了别名 alilas cls=clear  , 或者定义了 umask=033 等等,在logout 用户后再登录,会失效,因为这些本地变量,仅对当前shell生效。而要保留配置,使其永久生效,就需要将这些定义的写入配置文件中保存。下面介绍下bash的配置文件:  Bash的配置文件可以分为: profile类: /etc/…

    Linux干货 2015-04-04
  • LVM应用

    1、创建一个2G的文件系统,块大小为2048byte,预留1%可用空间,文件系统ext4,卷标为TEST,要求此分区开机后自动挂载至/test目录,且默认有acl挂载选项 ·首先在虚拟机当中添加指定大小的硬盘,添加完成后,系统不会自动扫描添加的硬盘设备,键入命令重新扫描SCSI总线来添加设备   echo "- – -&quo…

    Linux干货 2016-08-30
  • 路由器解析路由表

    1.定义——>选择最佳路径;他只完成发送到下一个路径上就结束,任务到达就撤了(就像快递员一样,当他将物件发往到下一个目的地,那他的任务就结束了)2.分类:主机路由、网络路由、默认路由3.每个路由记录由四项主要组成部分目标地址:主机IP、网络ID号、未知地址(0.0.0.0)子网掩码接口interface:从哪个口发往目标地址 网…

    2017-09-05
  • Linux的学习之路-第一周

    >N21-Keen-第一周作业 ### 1. 计算机的组成及其原理     >计算机的组成是根据冯诺依曼体系设计的,主要分为五大部分:控制器、运算器、存储器、输入设备和输出设备。          >其中控制器和运算器分布在CPU上,主要用来做控制和运算作用。存储器也就是…

    Linux干货 2016-07-16
  • keepalive配置文件详解

    第一部分:全局定义块 1、email通知。作用:有故障,发邮件报警。 2、Lvs负载均衡器标识(lvs_id)。在一个网络内,它应该是唯一的。 3、花括号“{}”。用来分隔定义块,因此必须成对出现。如果写漏了,keepalived运行时,不会得到预期的结果。由于定义块内存在嵌套关系,因此很容易遗漏结尾处的花括号,这点要特别注意。 global_defs{ n…

    2017-09-17
  • ip ss route nmcli netstart 命令汇总

    route命令 路由管理命令 查看:route -n [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.101.0 0.0.0.0 255.255.255.0 U 1 0 0 e…

    Linux干货 2017-05-07

评论列表(1条)

  • 马哥教育
    马哥教育 2016-10-25 12:52

    开机启动流程 如果能用图来说明就更好了,还有你的每一条命令,如果是为同一问题的答案,可以用“代码”美化一下