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

相关推荐

  • 用户组和权限管理

    一、3A认证     Authentication:认证     Autherization:授权     Accoutiong|Audition:审计 二、用户user      linu…

    Linux干货 2016-08-04
  • LINUX–命令的格式、类型、别名的概述及运用

    命令格式       COMMAND [OPTIONS…] [ARGUMENTS..]      [OPTIONS..] 选项:用于启用或关闭命令的某个或某些功能                &n…

    2017-05-21
  • 实现NFS为lamp环境web站点提供共享存储

    1.实验需求 (1)nfs server导出/data/application/web,在目录中提供wordpress; (2)nfs client挂载nfs server导出的文件系统,至/var/www/html; (3)客户端1(lamp)部署wordpress,并让其正常访问,要确保正常发文章,上传图片。 (4)客户端2(lamp),挂载nfs se…

    Linux干货 2017-05-02
  • 软件包管理

    软件运行环境 ABI(应用程序的二进制接口):Application Binary Interface Windows 与Linux 不兼容 ELF(Executable and Linkable Format)           —&…

    Linux干货 2017-04-23
  • ansible高级应用

    上一篇博客介绍了ansible的基础应用Modules,博客地址 http://www.178linux.com/14860 。这次介绍下Ansible的另一个核心模块Playbooks。   Ansible的核心模块:     Ansible core:   &nbs…

    Linux干货 2016-04-18
  • sed编辑器

    sed: Stream EDitor, 行编辑器;  用法: sed [option]… ‘script’ inputfile… script: ‘地址命令‘  常用选项: -n:不输出模式中的内容至屏幕; -e: 多点编辑; -f /PATH/TO/SCRIPT_F…

    Linux干货 2015-07-06

评论列表(1条)

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

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