22期第十周课堂练习

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

Centos启动过程 .png

    2、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区;
      (1) 为硬盘新建两个主分区;并为其安装grub;
      (2) 为硬盘的第一个主分区提供内核和ramdisk文件;   为第二个分区提供rootfs;
      (3)   为rootfs提供bash、ls、cat程序及所依赖的库文件;
      (4) 为grub提供配置文件;

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

1.创建2个主分区,sdb1,sdb2
[root@bogon ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 11.8 GB, 11811160064 bytes
255 heads, 63 sectors/track, 1435 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa5b8f2f4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         654     5253223+  83  Linux
/dev/sdb2             655        1308     5253255   83  Linux
2.格式化分区,格式为ext4
[root@bogon ~]# mke2fs -t ext4 /dev/sdb{1,2}
3.挂载分区sdb1到/mnt下
[root@bogon ~]# mount /dev/sdb1  /mnt/boot
4.安装grub到sdb1上
[root@bogon ~]# grub-install  --root-directory=/mnt /dev/sdb
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 /mnt/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
5.复制内核及ramdisk文件到sdb1上
[root@bogon ~]# cp /boot/initramfs-2.6.32-431.el6.x86_64.img  /mnt/boot/initramfs.img
[root@bogon ~]# cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/boot/vmlinuz
6.编辑grub.conf文件,/mnt/boot/grub/grub.conf
[root@bogon grub]# vim grub.conf
default=0
timeout=5
title CentOS6(test)
root (hd0,0)
kernel /vmlinuz ro root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs.img
7.卸载/mnt目录
[root@bogon /]# umount /mnt
8.挂载分区/dev/sdb2到/mnt下
[root@bogon /]# mount /dev/sdb2 /mnt/sysroot
9.创建系统必须的目录
[root@bogon mnt]# mkdir -p /mnt/sysroot{bin,dev,etc/{rc.d/init.d,sysconfig/network-scripts},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}}
10.拷贝命令的到/mnt下
[root@bogon boot]# cp /bin/{bash,ls,cat} /mnt/bin/
10.拷贝命令的依赖文件
[root@bogon boot]# cp `ldd /bin/{bash,ls,cat}  |grep -oe "/lib.*[[:space:]]"|sort -u` /mnt/lib64/
12.测试并同步
[root@bogon boot]# chroot /mnt/sysroot
bash-4.1# ls
bin  boot  dev	etc  lib  lib64  lost+found  proc  sbin  sys  tmp  usr	var
bash-4.1# ls
bin  boot  dev	etc  lib  lib64  lost+found  proc  sbin  sys  tmp  usr	var
bash-4.1# cat bin
cat: bin: Is a directory
bash-4.1# cd bin 
bash-4.1# ls
bash  cat  ls
bash-4.1# exit
exit
[root@bogon boot]# sync
13.重启后,将这块硬盘添加到新创建的虚拟机做引导盘

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

可以直接手动编辑或使用工具在桌面模式下用system-config-kickstart(centos6.x)来创建ks.cfg
#命令段
firewall --disabled//禁用防火墙
install//执行新安装
cdrom//用光盘安装
lang en_US.UTF-8//默认安装语言
keyboard us//选择键盘
rootpw --iscrypted $1$gWoGuWzn$E7polUWsUlS6qPXzQryRm //管理员密码
authconfig --enableshadow --passalgo=sha512
selinux --enforcing//激活selinux
logging --level=info//信息等级
timezone --utc Asia/Shanghai//系统时区
bootloader --location=mbr --driveorder=sdb --append="crashkernel=auto rhgb quiet"
clearpart --all//删除所有现在分区
part /boot --fstype=ext4 --size=500//分区挂载
part / --fstype=ext4 --size=20480
part swap --size=4096
#脚本段
%pre//安装前脚本
echo "start install"
%end
%post//安装后脚本
echo "install end"
#程序包段
%packages
@chinese-support//中文支持
@development//开发工具
@graphical-desktop-clients//图形化工具
@remote-desktop-clients//远程桌面客户端
%end
简单引导镜像光盘制作:
1)复制系统安装光盘除Packages和repodata外的所有目录下的所有文件到一自定义目录(/centos6.5)
mount -r /dev/cdrom /mnt/
mkdir /centos6.5
cp -arf /mnt/{CentOS_BuildTag,isolinux,R*,E*,GPL,T*,images} /centos6.5/
cd /centos6.6/
2)创建引导光盘:把/centos6.5目录创建为光盘镜像boot.iso
mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V 
 "CentOS 6.5 x86_64 boot" -b isolinux/isolinux.bin -c isolinux/boot.cat 
  -o /root/boot.iso /centos6.5/
简单自动安装系统光盘制作
1)复制安装系统光盘的所有目录下的所有文件和ks.cfg文件到一自制目录(/centos6.5)
cd /centos6.6/
cp -r /mnt/* .
cp /root/ks.cfg .
2)在/centos6.5/isolinux/isolinux.cfg文件中append initrd=initrd.img条目中添加ks文件读取路径 ks=cdrom:/ks.cfg
3)创建引导光盘:把centos6.5目录创建为光盘镜像centos.iso 
mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table 
 -V "CentOS 6.5 x86_64 boot"  -b isolinux/isolinux.bin -c isolinux/boot.cat 
 -o /root/boot.iso /centos6.5

    4、写一个脚本
      (1) 能接受四个参数:start, stop, restart,   status
       start: 输出“starting 脚本名   finished.”
       …

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

#!/bin/bash
case $1 in
start)
echo "start script finished "
;;
stop)
echo "stop script stop"
;;
restart)
echo "restart script restart"
;;
*)
echo "unknow"
;;
esac

    5、写一个脚本,判断给定的用户是否登录了当前系统;
      (1) 如果登录了,则显示用户登录,脚本终止;

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

#!/bin/bash
while true;do
if who | grep "^user1\>" &> /dev/null;then
    echo "user1 login"  
   break
fi
 sleep 3
done

    6、写一个脚本,显示用户选定要查看的信息;
       cpu) display cpu info
       mem) display memory info
       disk) display disk info
       quit) quit

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

#!/bin/bash
cat << EOR
cpu) display cpu info
mem) display memory info
disk) diskplay disk info
quit) quit
EOR
read -p  "your choice:" i

while [ "$i" != "cpu" -a "$i" != "mem" -a "$i" != "disk" -a "$i" != "quit" ]; do
echo "cpu, mem, disk, quit"
read -p "Enter your option again: " i
done

case $i in
cpu)
    lscpu
;;
men)
 free -m
;;
disk)
 fdisk -l /dev/[hs]d[a-z]
;;
quit)

    7、写一个脚本
      (1)   用函数实现返回一个用户的UID和SHELL;用户名通过参数传递而来;
      (2) 提示用户输入一个用户名或输入“quit”退出;
        当输入的是用户名,则调用函数显示用户信息;

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

#!/bin/bash
cat << EOR
user) show user uid and shell
quit) quit
EOR


while true; do
read -p "Please input username or quit:"  username
  if id $username &> /dev/null; then
   uid=`cat /etc/passwd |grep "^$username\>"|cut -d: -f3`
   shell=`cat /etc/passwd |grep "$username\>"|cut -d: -f7`
   echo "userid is:" $uid
   echo "usershell is:" $shell
   continue
   elif [ $username == quit ];then
    exit 0
   else
    echo "ERROR option"
   fi
done

     8、写一个脚本,完成如下功能(使用函数)
       (1)   提示用户输入一个可执行命令的名字;获取此命令依赖的所有库文件;
       (2)   复制命令文件至/mnt/sysroot目录下的对应的rootfs的路径上,例如,如果复制的文件原路径是/usr/bin/useradd,则复制到/mnt/sysroot/usr/bin/目录中;

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

#!/bin/bash
read -p "input a common:"  common
      a=`which $common`
      b=`ldd $a |grep -oe "/[^[:space:]]*"`
      cpfile(){
          cp  $a  /mnt/sysroot$a
        for i in `ldd $a |grep -oe "/[^[:space:]]*"`;do
          cp $i  /mnt/sysroot$i
          echo "copy finshed"
        done
      }
 cpfile

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

(0)
a295053193a295053193
上一篇 2016-10-24 09:10
下一篇 2016-10-24 09:11

相关推荐

  • 【招聘】北京/互联网/运维工程师/7-15K/双休,五险一金

    岗位职责:   1、负责服务器的规划、调试优化、日常监控、故障处理、数据备份、日志分析等工作;   2、参与运维流程制定,确保任何突发情况都能高效响应;  3、负责服务器部署,对服务器构架和网络进行优化和改进;  4、负责运维相关数据的收集、分析和总结;  5、负责技术运维相关的文档、手册…

    Linux干货 2016-04-16
  • scp命令介绍

    scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的。可能会稍微影响一下速度。当你服务器硬盘变为只读 read only system时,用scp可以帮你把文件移出来。另外,scp还非常不占资源,不会提高多少…

    Linux干货 2017-07-24
  • Linux的常见文件类型

      在linux中,文件的类型和权限通常以【-rwxr-xr-x】来表示,文件以第一个字符来表示不同的文件类型,具体如下。   – :普通文件;颜色为灰白色   d :目录文件;颜色为蓝色   b :块设备文件;颜色为黄色   c :字符设备文件;颜色为黄色   p :管道文件;颜色为绿…

    Linux干货 2016-10-20
  • N26-第二周

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。(盗图了,哈哈哈) 文件查看类命令:cat, tac, head, tail, more, less,touch    cat 查看文件内容   tac命令   反向查看文件内容  注:可对照上图 分屏查看命令:more  …

    Linux干货 2017-01-07
  • mount命令使用详解

    一、挂载(mount)初识     1.什么是挂载         将额外文件系统与根文件系统某现存的目录建立起关联关系,进而使得此目录作为其它文件访问入口的行为。     2.常用的文件系统类型 &…

    Linux干货 2016-09-01
  • 8-12 文件查找

    8-12 文件查找   8–1该节主要分为三部分,分别是作业,自己对德·摩根定律的了解及find常用选项   一、作业 1、查找/var目录下属主为root,且属组为mail的所有文件 2、查找/var目录下不属于root、lp、gdm的所有文件 3、查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是pos…

    Linux干货 2016-08-15

评论列表(1条)

  • 马哥教育
    马哥教育 2016-11-02 14:35

    博客写得非常的好,32个赞,给出了详细操作步骤,脚本思路清晰,排版需要注意一下哈,加油!