第十周作业

第十周作业

 

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
下一篇 2017-11-13

相关推荐

  • Nginx 常见配置解析

    结构上: 核心模块:HTTP模块、EVENT模块、MAIL模块。 基础模块:HTTP access模块、HTTP FastCGI模块、HTTP Proxy模块、HTTP Rewrite模块。 第三方模块:HTTP Upstream Request Hash模块。 功能上: Handlers…

    Linux干货 2016-12-05
  • linux下的文件查找命令对比(locate,find,grep,sed)

        在linux下,文件系统占据着非常重要的位置,而我们对于文件系统的操作也显得尤为重要。 如果我们想熟悉的操作文件系统,其中,我们需要对文本的查找,截取等命令需要熟悉的掌握。 这里就不得不说几个关于文本操作的几个命令的作用详细介绍和对比。比如: locate,find ,grep ,sed等。 这里,grep ,…

    Linux干货 2016-08-15
  • net25 第15周作业

    1、总结sed和awk的详细用法; SED sed模式空间 默认不编辑源文件,仅对模式空间中的数据做处理:而后,处理结束后,将模式空间打印屏幕 sed [options]’address+command’ file… -n:静默模式,不再默认显示模式空间的内容 -i:直接修改原文件 -e script -e script 同时执行多个脚本 -f /pat…

    Linux干货 2017-05-15
  • 马哥教育网络班25期-第3周作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@localhost ~]# who root     tty1         2016-12-…

    Linux干货 2016-12-19
  • 马哥教育网络班21期+第一周课程练习

    1,描述计算机的组成及其功能。 2.按系列罗列linux的发行版,并描述不同发行版之间的练习与区别。 3.描述linux的哲学思想,并按照自己的理解对其进行解释性描述。 1.       一切皆文件,计算机中所有的文件目录,        包括计算机的硬件设备显示为文件格式。 2…

    Linux干货 2016-07-07
  • awk

    awk是一种编程语言,用于在linux/unix下对文本和数据进行处理。数据可以来自标准输入(stdin)、一个或多个文件,或其它命令的输出。它支持用户自定义函数和动态正则表达式等先进功能,是linux/unix下的一个强大编程工具。它在命令行中使用,但更多是作为脚本来使用。awk有很多内建的功能,比如数组、函数等,这是它和C语言的相同之处,灵活性是awk最大的优势。

    Linux干货 2018-01-02

评论列表(1条)

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

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