第十周作业

第十周作业

 

1、CentOS系统启动流程:

1.加电自检:由bios去检测各硬件是否存在且是否正常运行,然后进行硬件初始化

2.选择启动顺序,加载mbr:根据bios设置的设备启动顺序,找到第一个安装有bootloader的设备进行启动,bootloader去识别加载操作系统中的核心文件,并提交到内存中运行,进而来启动相对应的操作系统

由于bootloader处于mbr中,而mbr只有446bytes用来存储bootloader,而446bytes无法容纳下bootloader,因此linux将bootloader运行和加载分成三个阶段:

stage1:运行bootloader主程序,这个程序必须要被安装在启动区,即MBR中。因为mbr空间有限,因此在mbr当中仅安装bootloader的最小主程序,并没有安装bootloader的相关配置文件

stage1.5:在mbr随后的扇区中存放,主要用于与stage2所在的分区的文件系统进行交互。

stage2:通过bootloader加载所有配置文件及相关的环境参数信息,这些配置文件及相关的环境参数都存放于磁盘分区上的/boot目录下。

3.加载系统内核,执行系统初始化信息:bootloader加载内核和initrd,然后在内存中让initrd解压缩成根目录,然后内核可以在这个虚拟的根文件系统之上加载合适的驱动程序,来加载硬盘等设备,之后释放虚拟的根文件系统,并以只读的方式挂载磁盘上的真实的根文件系统,之后就开始正常的启动过程

4.启动用户空间第一个执行程序/sbin/init:init主要功能是准备软件运行环境,包括系统的主机名称,网络配置、文件系统格式等其他服务的启动管理

init是通过自上而下读取inittab文件来依次执行的,因此init的处理流程是:

1、读取默认的runlevel,

2、使用/etc/rc/d/rc.sysinit进行系统初始化。

1、根据配置文件中的设定来设定主机名(读取/etc/sysconfig/network文件中的HOSTNAME进行)

2、打印文本欢迎信息;

3、激活SELinux和udev;

4、挂在所有定义在/etc/fstab文件中的文件系统

5、激活swap设备

6、检测根文件系统,并实现以读写方式重新挂载;

7、设置系统时钟

8、更具/etc/sysctl.conf文件设定内核参数的值

9、激活LVM和RAID设备

10、加载额外的设备启动

11、清理操作

3、由于采用的的是3级别,因此只进行l3:3:wait:/etc/rc.d/rc 3,其他的则忽略

4、配置好ctrl+alt+del组合件的功能

5、配置不断电系统pf、pr两种机制

6、启动mingetty 6个终端

7、如果是5级别,则会以/etc/X11/prefdm-nodaemon启动图形化界面

 

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

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

#创建两个分区,/dev/sdb1为500M,/dev/sdb2为5G

[root@centos6 mnt]# fdisk -l /dev/sdb

Disk /dev/sdb: 53.7 GB, 53687091200 bytes

255 heads, 63 sectors/track, 6527 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: 0x473aab9e

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1          65      522081   83  Linux

/dev/sdb2              66         719     5253255   83  Linux

#格式化分区为ext4格式

[root@centos6 script]# mke2fs -t ext4 /dev/sdb1

[root@centos6 script]# mke2fs -t ext4 /dev/sdb2

#创建挂载目录并挂载分区

[root@centos6 script]# mkdir -p /mnt/{boot,sysroot}

[root@centos6 script]# mount /dev/sdb1 /mnt/boot/

[root@centos6 script]# mount /dev/sdb2 /mnt/sysroot/

[root@centos6 grub]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda2       116G  1.7G  109G   2% /

tmpfs           364M     0  364M   0% /dev/shm

/dev/sda1       477M   32M  420M   7% /boot

/dev/sdb1       486M   29M  432M   7% /mnt/boot

/dev/sdb2       4.9G   11M  4.6G   1% /mnt/sysroot

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

#复制核心文件和虚拟镜像文件到新的boot目录下

[root@centos6 grub]#cp /boot/vmlinuz-2.6.32-642.el6.x86_64 /mnt/boot/vmlinuz

[root@centos6 grub]#cp /boot/initramfs-2.6.32-642.el6.x86_64.img /mnt/boot/initramfs.img

#创建新的根目录下必要的文件夹

[root@centos6 grub]# cd /mnt/sysroot/

[root@centos6 sysroot]# mkdir bin dev etc home lib lib64 media mnt opt proc root sbin selinux srv sys tmp usr var

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

#查看bash、ls、cat命令所需要用到的动态链接库文件

[root@centos6 sysroot]# ldd /bin/bash

linux-vdso.so.1 =>  (0x00007fffc89c6000)

libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007f688f4e3000)

libdl.so.2 => /lib64/libdl.so.2 (0x00007f688f2df000)

libc.so.6 => /lib64/libc.so.6 (0x00007f688ef4a000)

/lib64/ld-linux-x86-64.so.2 (0x00007f688f70d000)

[root@centos6 sysroot]# ldd $(which –skip-alias ls)

linux-vdso.so.1 =>  (0x00007ffc5dd97000)

libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f8dd942a000)

librt.so.1 => /lib64/librt.so.1 (0x00007f8dd9222000)

libcap.so.2 => /lib64/libcap.so.2 (0x00007f8dd901d000)

libacl.so.1 => /lib64/libacl.so.1 (0x00007f8dd8e15000)

libc.so.6 => /lib64/libc.so.6 (0x00007f8dd8a81000)

libdl.so.2 => /lib64/libdl.so.2 (0x00007f8dd887c000)

/lib64/ld-linux-x86-64.so.2 (0x00007f8dd9652000)

libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f8dd865f000)

libattr.so.1 => /lib64/libattr.so.1 (0x00007f8dd845a000)

[root@centos6 sysroot]# ldd $(which –skip-alias cat)

linux-vdso.so.1 =>  (0x00007ffc04752000)

libc.so.6 => /lib64/libc.so.6 (0x00007f6754cba000)

/lib64/ld-linux-x86-64.so.2 (0x00007f6755057000)

#复制动态链接库文件到新的根目录下

[root@centos6 bin]# cp /bin/cat /mnt/sysroot/bin/

[root@centos6 bin]# cp /bin/ls /mnt/sysroot/bin/

[root@centos6 sysroot]# ldd $(which –skip-alias bash) |grep -o “/.*\.[[:digit:]]”|xargs -I {} cp {} /mnt/sysroot/lib64

[root@centos6 sysroot]# ldd $(which –skip-alias ls) |grep -o “/.*\.[[:digit:]]”|xargs -I {} cp {} /mnt/sysroot/lib64

[root@centos6 sysroot]# ldd $(which –skip-alias cat) |grep -o “/.*\.[[:digit:]]”|xargs -I {} cp {} /mnt/sysroot/lib64

[root@centos6 sysroot]# ll /mnt/sysroot/lib64/

total 2560

-rwxr-xr-x 1 root root  154664 Mar  1 05:38 ld-linux-x86-64.so.2

-rwxr-xr-x 1 root root   31280 Mar  1 05:38 libacl.so.1

-rwxr-xr-x 1 root root   18712 Mar  1 05:38 libattr.so.1

-rwxr-xr-x 1 root root   16600 Mar  1 05:38 libcap.so.2

-rwxr-xr-x 1 root root 1923352 Mar  1 05:38 libc.so.6

-rwxr-xr-x 1 root root   19536 Mar  1 05:38 libdl.so.2

-rwxr-xr-x 1 root root  142688 Mar  1 05:38 libpthread.so.0

-rwxr-xr-x 1 root root   43944 Mar  1 05:38 librt.so.1

-rwxr-xr-x 1 root root  122056 Mar  1 05:38 libselinux.so.1

-rwxr-xr-x 1 root root  132408 Mar  1 05:21 libtinfo.so.5

#使用chroot命令切换根目录到/mnt/sysroot进行测试

[root@centos6 bin]# chroot /mnt/sysroot/

bash-4.1# ls

bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var

bash-4.1# bash

bash-4.1# cat <<EOF

> hello world

> EOF

hello world

4、为grub提供配置文件;

#创建grub配置文件

[root@centos6 sysroot]# vim /mnt/boot/grub/grub.conf

default=0

timeout=5

title CentOS (MyDIY)

root (hd0,0)

kernel /vmlinuz ro root=/dev/sdb2 init=/bin/bash

initrd /initramfs.img

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

在BIOS中将新创建的磁盘作为第一启动设备

重启后进入grub,编辑kernel参数,设置selinux=0 (注意:该参数要放置在init之前)

编辑完成后,按b键启动后即可进入新建的系统。

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

1、创建镜像生成目录并将光盘下isolinux目录copy到该目录下,并对其下的文件赋予写权限。

[root@centos6 ~]# mkdir -p /myboot/

[root@centos6 ~]# cp -a /mnt/cdrom/isolinux/ /myboot/

[root@centos6 ~]# cd /myboot/

[root@centos6 myboot]# chmod -R 777 isolinux/

2、创建kickstart配置文件

[root@centos6 myboot]# vim 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=”http://mirrors.aliyun.com/centos/6/os/x86_64″

# Root password

rootpw –iscrypted $1$ifhHlqT/$mZ5IcE3P2Nn54UG3i/SI//

# System authorization information

auth  –useshadow  –passalgo=sha512

# Use text mode install

text

firstboot –disable

# System keyboard

keyboard us

# System language

lang en_US

# SELinux configuration

selinux –disabled

# Installation logging level

logging –level=info

# Reboot after installation

reboot

# System timezone

timezone  Asia/Shanghai

# Network information

network  –bootproto=dhcp –device=eth0 –onboot=on

# System bootloader configuration

bootloader –append=”crashkernel=auto rhgb quiet” –location=mbr –driveorder=”sda”

# Partition clearing information

clearpart –all  –drives=sda

# Disk partitioning information

part /boot –fstype=ext4 –size=500

part pv.01 –size=100000

volgroup myvg –pesize=4096 pv.01

logvol /home –fstype=ext4 –name=lv_home –vgname=myvg –size=5000

logvol / –fstype=ext4 –name=lv_root –vgname=myvg –size=50000

logvol swap –name=lv_swap –vgname=myvg –size=2000

logvol /usr –fstype=ext4 –name=lv_usr –vgname=myvg –size=15000

logvol /var –fstype=ext4 –name=lv_var –vgname=myvg –size=10000

%packages

@core

@server-policy

@workstation-policy

%end

3、创建光盘引导镜像

[root@centos6 ~]# cd /myboot/

[root@centos6 myboot]# mkisofs -R -J -T -v –no-emul-boot –boot-load-size 4 –boot-info-table -V “CentOS 6 x86_64 boot” -c isolinux/boot.cat -b isolinux/isolinux.bin -o /root/boot.iso ../myboot/

4、新建一台虚拟机并使用上一步创建的光盘引导镜像进行安装,在光盘启动菜单输入下面参数指定使用kickstart配置文件进行一键安装。

4、写一个脚本   (1) 能接受四个参数:start, stop, restart, status    start: 输出“starting 脚本名 finished.”    …   (2) 其它任意参数,均报错退出;

#!/bin/bash

# testsrv       service testing script

#

# chkconfig:    2345 50 60

# discrption: test

#

#

prog=$(basename $0)

 

if [ $# -lt 1 ];then

echo “usage:$prog {start|stop|restart|status}”

exit 1

fi

 

if [ $1 == start ];then

echo “starting $prog finished”

elif [ $1 == stop ];then

echo “stopping $prog finished”

elif [ $1 == restart ];then

echo “restart $prog finished”

elif [ $1 == status ];then

if pidof $prog &> /dev/null;then

echo “$prog is running”

else

echo “$prog is stopped”

fi

else

echo “usage:$prog {start|stop|restart|status}”

exit 2

fi

把次脚本复制到/etc/rc.d/init.d 下

然后执行 chkconfig –add SCRIPTNAME

服务就开启了。

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

#!/bin/bash

read -p “send a username:” UNAME

while true ;do

if who | grep “$UNAME” > /dev/null ;then

break

fi

sleep 3

done

6、写一个脚本,显示用户选定要查看的信息;    cpu) display cpu info    mem) display memory info    disk) display disk info    quit) quit    非此四项选择,则提示错误,并要求用户重新选择,只到其给出正确的选择为止;

cat << eof

cpu) display cpu info

mem) display memory info

disk) display disk info

quit) quit

eof

 

while true; do

read PARA

if [ $PARA == cpu ];then

lscpu

break

elif [ $PARA == mem ];then

free

break

elif [ $PARA == disk ];then

fdisk -l

break

elif [ $PARA == quit ];then

break

else

echo “need a right choose”

fi

done

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

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

#!/bin/bashuserinfo() {    uid=$(id -u $1)    shell=$(cat /etc/passwd|grep fangtao|awk -F: ‘{print $NF}’)    echo “UID: $uid”    echo “SHELL: $shell”} while true;do    read -p “Input username[input ‘quit’ if you don’t want to continue]: ” input    [ “$input” == “quit” ] && exit 0    if [ -z “$input” ];then        echo “Blank not allowed!”        continue    else        if id $input &>/dev/null;then            userinfo “$input”            continue        else            echo “$input not exists!”            continue        fi    fidone

 

本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/88333

(0)
N27_yangjifengN27_yangjifeng
上一篇 2017-11-13 10:53
下一篇 2017-11-13 12:32

相关推荐

  • shell脚本1

    shell脚本基础 shell脚本: 包含一些命令或声明,并符合一定格式的文本文件 格式要求:首行shebang机制 #!/bin/bash #!/usr/bin/python #!/usr/bin/perl shell脚本的用途有: 自动化常用命令 执行系统管理和故障排除 创建简单的应用程序 处理文本或文件 创建shell脚本 第一步:使用文本编辑器来创建…

    2017-08-05
  • LVS:三种模式的原理、调度算法、及应用介绍

    LVS三种模式原理(nat/dr/tun) LVS/NAT:   如上图,客户通过virtual IP (虚拟服务的IP地址,公网地址),访问网络服务时,请求报文到达调度器,调度器根据连接调度算法从一组真实服务器中选出一台服务器,将报文的目标地址VIP,改写成选定服务器的地址(RIP),报文的目标端口改写成选定服务器的相应端口,最后将修改…

    Linux干货 2016-10-30
  • 第三周作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次既可。 [root@director ~]# who | cut -d' ' -f 1 | sort | uniq &nbsp…

    Linux干货 2016-12-13
  • Linux之网络管理

    Linux之网络管理     在学习linux的过程中,Linux网络的管理和配置中是很重要的,几乎学习的后期都离不开网络的概念和配置,如集群中的使用,学习好Linux网络至关重要。     Linux网络IP地址有两种配置方式:静态指定和动态分配 动态分配就是利用DHCP服务器,动态的给linux主机分配IP地址。静…

    Linux干货 2016-09-05
  • vim 文本编辑器

    vim 文本编辑器 vi 简介 vi命令是UNIX操作系统和类UNIX操作系统中最通用的全屏幕纯文本编辑器。Linux中的vi编辑器叫vim,它是vi的增强版(vi Improved),与vi编辑器完全兼容,而且实现了很多增强功能。 vi编辑器支持编辑模式和命令模式,编辑模式下可以完成文本的编辑功能,命令模式下可以完成对文件的操作命令,要正确使用vi编辑器就…

    Linux干货 2016-08-16
  • keepalived高可用实例

    1、keepalived单实例 1)实验环境为VMware虚拟机,两台centos7.2,主机1IP地址为10.1.252.55,主机2IP地址为10.1.252.34,虚拟路由IP为10.1.252.246, 2)确保iptables和selinux不影响实验 (1)确保两台主机的时间同步 [root@localhost ~]# vim…

    Linux干货 2016-11-01

评论列表(1条)

  • 马哥教育
    马哥教育 2017-12-02 09:09

    写的很好,命令可以考虑用脚本语言表姐,排版出来就会很好看。