马哥教育网络班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)
上一篇 2016-08-15 12:10
下一篇 2016-08-15 12:10

相关推荐

  • 编译内核

    先把下载的内核解压展开归档 tar -xvf linux-4.12.tar.xz   把linux系统中的/boot/config-3.10.0-514.el7.x86_64 copy到归档文件中并命名为.config   cp config-3.10.0-514.el7.x86_64 /app/linux-4.12/.conf…

    2017-07-12
  • 运维成长记

     在学校里学到的是知识,而企业更看重的是技能和能力。我们需要把知识先转化为技能,然后实践中通过总结提升,转变为自己的能力。这个过程或许会很漫长,但是时间会证明一切。成长的过程注定是痛苦的,欲戴王冠,必承其重!我们有理由相信:运气也是实力的一部分! 第一次接触linux还是在大学的课堂上,看着老师在物理机上用VM虚拟出一台Ubuntu的时候,真实感觉…

    Linux干货 2017-07-11
  • 高级文件系统之逻辑卷和btrfs文件系统

    逻辑卷管理器快照 逻辑卷可以实现对分区的动态的扩展,快照可以看成是特殊的逻辑卷,它是在生成快照是存在的逻辑卷的准确拷贝。 快照只有在它们和原来的逻辑卷不同时才会消耗空间:     在生成快照时会分配给他一定的空间,但只有在原来的逻辑卷或者快照有所改变时才会使用这些空间     当…

    Linux干货 2016-09-02
  • CentOS 7 tomcat 7.0.54 的功能实现及详解

    一、 jdk 安装配置 # yum install java-1.8.0-openjdk-devel (依赖的java-1.8.0-openjdk,java-1.8.0-openjdk,headless也会被安装 ) # alternatives -h # vim /e…

    2016-08-19
  • Week9’s homework

    Week9's homework 1、写一个脚本(1)判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin)(2)分别统计两类用户的个数;通过字符串比较来实现。 #!/bin/bash # for i in $(cat /etc/passwd |…

    Linux干货 2016-12-24
  • sed命令及vim编辑器

    sed[option]… 'script' inputfile…选项:-n:不输出模式空间内容的自动打印-e: 多点编辑-f /PATH/TO/SCRIPT_FILE: 从指定文件中读取编辑脚本-r: 支持使用扩展正则表达式-i: 原处编辑script:'地址命令' 1.地址定界:(1) 不给地…

    Linux干货 2016-08-11

评论列表(1条)

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

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