马哥教育网络班21期+第10周课程练习

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

POST:加电自检,主板检测系统硬件。

BOOT sequence:依次查找引导设备(装有引导程序)

MBR:找到引导设备的主引导扇区引导记录(446k bootloader 64k DPT 2k 结束标志)

GRUB: 引导程序的一种,提供一个菜单,允许用户选择要启动系统或不同的内核版本;把用户选定的内核装载到内存中的特定空间中,解压、展开,并把系统控制权移交给内核,1stage 运行grub,1.5stage 识别文件系统,2stage 识别磁盘分区

Kernel(ramdisk):Kernel探测硬件设备,如果有自己无法驱动硬件时,ramdisk可以把内存当成硬盘,运行Kernel和initrd或initramfs的微小镜像系统(光盘安装linux后会自动产生)initrd 或initramfs 会辅助Kernel 挂载到自己的根下,找到硬盘驱动,文件系统驱动等。

Rootfs:利用initrd或initramfs可以驱动所有硬件,只读挂载到其根目录下

Switchroot:从initrd或initramfs的根目录下切换挂载到硬盘上的根分区目录下

运行/sbin/init:linux的第一个进程,之后的过程有/sbin/init进程完成

/sbin/init通过(/etc/inittab, /etc/init/*.conf) –> 设定默认运行级别 –> 系统初始化脚本 –> 关闭或启动对应级别下的服务(/etc/rc.d/init.d/* /etc/rc.d/rcN.d/*) –> 启动终端(/usr/sbin/mingetty)

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

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

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

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

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

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

[root@centos ~]# mkfs -t ext3 /dev/sdb1 && mkfs -t ext3  /dev/sdb3 && mkswap /dev/sdb2
[root@centos ~]# mkdir /sdb1/boot/
[root@centos ~]# mount /dev/sdb1 /sdb1/boot/
[root@centos ~]# grub-install --root-directory=/sdb1/ /dev/sdb
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
[root@centos ~]# cp /boot/vmlinuz-2.6.32-504.el6.x86_64 /sdb1/boot/vmlinuz
[root@centos ~]# cp /boot/initramfs-2.6.32-504.el6.x86_64.img /sdb1/boot/initramfs.img
[root@centos ~]# cp /boot/grub/grub.conf /sdb1/boot/grub/grub.conf
[root@centos ~]# vim  /sdb1/boot/grub/grub.conf
[root@centos ~]# vim /sdb1/boot/grub/grub.conf
[root@centos ~]# cat /sdb1/boot/grub/grub.conf
default=0
timeout=5
hiddenmenu
title CentOS 6 (test new)
root (hd0,0)
kernel /vmlinuz ro root=/dev/sda3 selinux=0 init=/bin/bash
initrd /initramfs.img
[root@centos ~]# mount /dev/sdb3 /mnt/sysroot/
[root@centos ~]# mkdir /mnt/sysroot/{var,etc,dev,proc,tmp,sys,root,home,media,lib,lib64,usr,bin,sbin}
[root@centos ~]# cd /test/
[root@centos test]# ./exercise1.sh 
Please input a command:bash
Everything is OK! U can check out /mnt/sysroot!
[root@centos test]# ./exercise1.sh 
Please input a command:ls
Everything is OK! U can check out /mnt/sysroot!
[root@centos test]# ./exercise1.sh 
Please input a command:ifconfig
Everything is OK! U can check out /mnt/sysroot!
[root@centos test]# sync
[root@centos test]# umount /sdb1/boot
[root@centos test]# umount /dev/sdb3
[root@centos ~]# mount /dev/sdb1 /sdb1/boot/
[root@centos ~]# mount  /dev/sdb3 /mnt/sysroot/
[root@centos ~]# chroot /mnt/sysroot/
bash-4.1# ls
bin  etc   lib  lost+found  proc  sbin  tmp  var
dev  home  lib64  media       root  sys   usr

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

利用system-config-kickstart工具生成ks.cfg文件,使用的ks.cfg时,可以结合pxe+kickstart+dhcp实现无人值守自动安装系统

手动指定ks.cfg文件位置实现单台机器的自动安装,需要借助安装光盘中的anaconda程序,选中安装或更新现有系统选项,按Esc键进入boot提示符下:ip=IPADDR netmask=MASK gateway=GW dns=DNS_SERVER_IP 

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

来找到ks.cfg文件

anaconda是光盘自带的程序用来导向系统安装步骤,通过光盘安装系统成功后会有产生ks文件anaconda-ks.cfg,从而记录了光盘安装过程中的安装选项

[root@centos ~]# cat /root/ks.cfg 
#platform=x86, AMD64, or Intel EM64T  基础信息杂项
#version=DEVEL
# Firewall configuration
firewall --disabled
# Install OS instead of upgrade
install
# Use network installation
url --url="ftp://192.168.40.128/var/pub/os/centos6.iso"
# Root password
rootpw --iscrypted $1$JQjb8nT/$2l3ydCrd0k1mInWAL213J/
# 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 --disabled
# Installation logging level
logging --level=info
# System timezone
timezone  Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Partition clearing information
clearpart --all  
# Disk partitioning information    磁盘分区情况
part /boot --fstype="ext4" --size=512
part swap --fstype="swap" --size=1024
part / --fstype="ext4" --size=1024
%post --interpreter=/bin/bash    安装好系统后要执行的脚本
echo "finished!"
%end
%packages         选择要安装的软件包
@additional-devel
@chinese-support
@compat-libraries
@desktop-platform-devel
@development
@kde-desktop
@server-platform-devel
%end

4、写一个脚本

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

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

   …

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

[root@www test]# cat testd 
#!/bin/bash
# chkconfig: 2345 89 10
# description: just testd service
prog=$(basename $0)
lockfile=/var/lock/subsys/$prog
start(){
if [ -e $lockfile ];then
echo "$prog is already running!"
return 0
else 
touch $lockfile
[ $? -eq 0 ] && echo "Starting $prog finised!" 
fi
}
stop(){
if [ -e $lockfile ];then
rm -rf $lockfile && echo " Stop $prog finished!"
else
echo "$prog is stopped yet! "
fi
}
status(){
if [ -e $lockfile ];then
echo "$prog is running!"
else
echo "$prog is stopped!"
fi
}
restart(){
stop && start
}
usage(){
echo "Usage: $prog {stop|start|status|restart}"
}
case $1 in 
   "start")
    start
       ;;
    "stop")
     stop
       ;;
  "status")
    status
       ;;
 "restart")
   restart
       ;;
        *)
   usage
        ;;
      esac

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

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

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

#!/bin/bash
[ $# -eq 0 -o $# -gt 1 ] && echo "only for one user or too many users!"&& exit 12
[ ! id $1 &>/dev/null ] && echo "$1 doesn't exisits!" && exit 13
user=$1
chk_login(){
for i in $(who | awk '{print $1}'| sort -u);do
if [[ "$user" == "$i" ]];then
echo "$user logged in!"
return 0
else
return 1
fi
done
}
while true;do
chk_login()
if [ $? -eq 0 ];then
exit 0
else
sleep3
chk_login
fi
done

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

   cpu) display cpu info

   mem) display memory info

   disk) display disk info

   quit) quit

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

[root@www test]# ./exercise15.sh 
cpu) display cup info
mem) display memory info
disk) display disk info
quit) quit
========================
Enter a option:hehe
Wrong option!
cpu) display cup info
mem) display memory info
disk) display disk info
quit) quit
========================
Enter a option:haha
Wrong option!
cpu) display cup info
mem) display memory info
disk) display disk info
quit) quit
========================
Enter a option:disk
major minor  #blocks  name

   8        0   15728640 sda
   8        1     512000 sda1
   8        2   15215616 sda2
 253        0   13639680 dm-0
 253        1    1572864 dm-1
[root@www test]# ./exercise15.sh 
cpu) display cup info
mem) display memory info
disk) display disk info
quit) quit
========================
Enter a option:quit
[root@www test]# cat exercise15.sh 
#!/bin/bash
tips(){
cat << EOF
cpu) display cup info
mem) display memory info
disk) display disk info
quit) quit
========================
EOF
read -p "Enter a option:" option
}
tips
while true;do
if [ "$option" == "cpu" -o "$option" == "mem" -o "$option" == "disk" -o "$option" == "quit" ];then

if [ "$option" == "cpu" ];then 
        lscpu
       exit 0
elif [ "$option" == "mem" ];then
  cat /proc/meminfo
       exit 0
elif [ "$option" == "disk" ];then
   cat /proc/partitions
        exit 0
else 
    exit 0
fi

else
echo "Wrong option!"
tips     
fi
done

7、写一个脚本

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

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

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

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

[root@www test]# ./exercise16.sh 
  account) display user info
     quit) quit
================================
Enter a option:hehe
user hehe  doesn't exisits!
  account) display user info
     quit) quit
================================
Enter a option:haha
user haha  doesn't exisits!
  account) display user info
     quit) quit
================================
Enter a option:root
root uid is 0,shell is /bin/bash
  account) display user info
     quit) quit
================================
Enter a option:derulo
derulo uid is 500,shell is /bin/bash
  account) display user info
     quit) quit
================================
Enter a option:quit
[root@www test]# cat exercise16.sh 
#!/bin/bash
tips(){ cat << EOF
  account) display user info
     quit) quit
================================
EOF
read -p "Enter a option:" option
}
tips
while true;do
if [ "$option" == "quit" ];then
exit 12
elif ! id $option &>/dev/null;then
echo "user $option  doesn't exisits!"
tips
elif id $option &>/dev/null;then
user=$option
get_uidshell(){
uid=$(id $user | cut -d " " -f1 | cut -d "=" -f2 | tr "()" " " | cut -d " " -f1)
shell=$(finger $user | grep "Shell" | cut -d ":" -f3 | cut -d " " -f2)
echo "$user uid is $uid,shell is $shell"
}
     get_uidshell
     tips
fi
done

 

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

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

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

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

[root@www test]# ./exercise17.sh 
Please input a command:hehe
Wrong command!
[root@www test]# ./exercise17.sh 
Please input a command:cd
cd is builtin command,U don't have to do this!
[root@www test]# ./exercise17.sh 
Please input a command:ls
Everything is OK! U can check out /mnt/sysroot!
[root@www test]# ./exercise17.sh 
Please input a command:ifconfig
Everything is OK! U can check out /mnt/sysroot!
[root@www test]# cd /mnt/sysroot/
[root@www sysroot]# ls -a
.  ..  bin  lib64  sbin
[root@www sysroot]# cat /test/exercise17.sh 
#!/bin/bash
read -p "Please input a command:" command
[ ! -e /mnt/sysroot ] &&  mkdir /mnt/sysroot &>/dev/null 
if  type $command &>/dev/null && ! type $command | grep -i "builtin" &>/dev/null;then
binpath=$(whereis -b $command | cut -d " "  -f2)
for m in $(dirname $binpath);do
mkdir -p /mnt/sysroot/$m/ &>/dev/null
cp $binpath /mnt/sysroot/$m/
done
for i in $(ldd $binpath | grep -o  "/lib.*" | cut -d " " -f1 );do
mkdir -p /mnt/sysroot/$(dirname $i)/ &>/dev/null
cp $i /mnt/sysroot/$(dirname $i)
done
elif  type $command &>/dev/null && type $command | grep -i "builtin" &>/dev/null;then
echo "$command is builtin command,U don't have to do this!" 
exit 12
else
echo "Wrong command!"
exit 13
fi
echo "Everything is OK! U can check out /mnt/sysroot!"

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

(0)
SnooSnoo
上一篇 2016-08-15
下一篇 2016-08-15

相关推荐

  • 文本处理-三剑客-awk

    报告生成器,格式化文本输出
    awk [options] ‘BEGIN{ action;… } pattern{ action;… } END{ action;… }’ file …

    2018-03-16
  • 103-corosync-pacemaker-crmsh

    一、概述: 1.1 什么是AIS和OpenAIS?

    2016-11-26
  • N22-第八周作业

    第八周 1、请描述网桥、集线器、二层交换机、三层交换机、路由器的功能、使用场景与区别。      集线器的主要功能是对接收到的信号进行再生整形放大,以扩大网络的传输距离,同时把所有节点集中在以它为中心的节点上。它工作于OSI(开放系统互联参考模型)参考模型第一层,即“物理层”。    &n…

    Linux干货 2016-10-09
  • Linux磁盘、文件系统管理

    对Linux而言,其哲学思想之一就是一切皆文件,因此Linux之上所有设备的操作都是通过文件接口来实现的,也就是说在访问设备的时候就如同访问一个文件一样,因为他们使用的都是文件系统接口。  设备也分种类,如:     1、块设备:block 其存取单位是“块”,如硬盘    &n…

    Linux干货 2016-08-29
  • Nginx+Keepalived实现站点高可用

    Nginx+Keepalived实现站点高可用 vrrp 虚拟路由冗余协议(Virtual Router Redundancy Protocol,简称VRRP)是由IETF提出的解决局域网中配置静态网关出现单点失效现象的路由协议,1998年已推出正式的RFC2338协议标准。VRRP广泛应用在边缘网络中,它的设计目标是支持特定情况下IP数据流量失败转移不会引…

    Linux干货 2016-11-07
  • 第6天预习xargs命令的用法

    Xargs用法详解 1. 简介   之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了xargs命令,例如: find /sbin -perm +700 |ls -l     &n…

    Linux干货 2016-08-05

评论列表(1条)

  • 马哥教育
    马哥教育 2016-08-17 15:02

    写的很好,排版还可以在漂亮一点,加油,第5题是不是多了一个括号