第十周作业

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

第十周作业

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
filename=$(basename $0)
if [ $# -lt 1 ];then
    echo "Usage $filename {start | stop | restart| status}"
    exit 1
fi

case $1 in
    start)
        echo "starting $filename finished."
        ;;
    stop)
        echo "stopping $filename finished."
        ;;
    restart)
        echo "restarting $filename finished."
        ;;
    status)
        echo "$filename is running..."
        ;;
    *)
        echo "Invalid argument!"
        exit 1
esac
执行结果:
[root@centos6 script]# ./10_4.sh
Usage 10_4.sh {start | stop | restart| status}
[root@centos6 script]# ./10_4.sh start
starting 10_4.sh finished.
[root@centos6 script]# ./10_4.sh haha
Invalid argument!

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

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

#!/bin/bash
if [ $# -lt 1 ];then
    echo "Usage $0 USERNAME"
    exit 1
fi

while true;do
    if w|grep "^$1\>" &>/dev/null;then
        echo "User $1 is logged in."
        break
    else
        echo "User $1 is not logged in."
        sleep 3
    fi
done
执行结果:
[root@centos6 script]# ./10_5.sh magedu
User magedu is not logged in.
User magedu is not logged in.
User magedu is not logged in.
User magedu is not logged in.
User magedu is not logged in.
User magedu is not logged in.
User magedu is logged in.

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 -p "Input your choice: " opt
    case $opt in
        cpu)
            lscpu
            exit 0
            ;;
        mem)
            free -m
            exit 0
            ;;
        disk)
            df -h
            exit 0
            ;;
        quit)
            exit 1
            ;;
        *)
            echo "Invalid input,please choice again!"
            continue
    esac
done
执行结果:
[root@centos6 script]# ./10_6.sh 
cpu) Display cpu info
mem) Display memory info
disk) Display disk info
quit) Quit
Input your choice: mem
             total       used       free     shared    buffers     cached
Mem:           726        362        364          0         24        243
-/+ buffers/cache:         94        631
Swap:         2047          0       2047
[root@centos6 script]# ./10_6.sh 
cpu) Display cpu info
mem) Display memory info
disk) Display disk info
quit) Quit
Input your choice: hahaha
Invalid input,please choice again!
Input your choice: quit

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

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

#!/bin/bash
userinfo() {
    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
    fi
done
执行结果:
[root@centos6 script]# ./10_7.sh  
Input username[input 'quit' if you don't want to continue]: magedu
UID: 500
SHELL: /bin/bash
Input username[input 'quit' if you don't want to continue]: hahaha
hahaha not exists!
Input username[input 'quit' if you don't want to continue]: 
Blank not allowed!
Input username[input 'quit' if you don't want to continue]: quit

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

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

#!/bin/bash
chpath() {
    if which --skip-alias $1 &>/dev/null;then
        abscmd=$(which --skip-alias $1)
        cp --parents $abscmd $newroot
        ldd $abscmd|grep -o "/.*\.[[:digit:]]"|xargs -I {} cp --parents {} $newroot
        if [ $? -eq 0 ];then
            return 0
        else
            return 2
        fi
    else
        echo "Invalid command,please input again!"
        return 1
    fi
}

newroot=/mnt/sysroot
[ ! -d $newroot ] && mkdir $newroot
while true;do
    read -p "Input command: " cmd
    chpath "$cmd"
    RETVAL=$?
    [ $RETVAL -ne 0 ] && continue || exit 0
done
[root@centos6 script]# ./10_8.sh 
Input command: haha
Invalid command,please input again!
Input command: bash
[root@centos6 script]# ./10_8.sh 
Input command: ls

#命令二进制文件和相关的动态链接库文件都已copy到新的根下
[root@centos6 script]# cd /mnt/sysroot/
[root@centos6 sysroot]# tree
.
├── bin
│   ├── bash
│   └── ls
└── lib64
    ├── ld-linux-x86-64.so.2
    ├── libacl.so.1
    ├── libattr.so.1
    ├── libcap.so.2
    ├── libc.so.6
    ├── libdl.so.2
    ├── libpthread.so.0
    ├── librt.so.1
    ├── libselinux.so.1
    └── libtinfo.so.5

2 directories, 12 files

#chroot到新的根下后,之前输入的命令能够正常执行
[root@centos6 sysroot]# chroot /mnt/sysroot/
bash-4.1# ls
bin  lib64

原创文章,作者:N26-西安-方老喵,如若转载,请注明出处:http://www.178linux.com/70498

(0)
N26-西安-方老喵N26-西安-方老喵
上一篇 2017-03-12
下一篇 2017-03-13

相关推荐

  • Linux磁盘管理(一)之分区、格式化、挂载使用

    磁盘管理(一)   本章节内容: 磁盘结构 分区类型   管理分区   管理文件系统   挂载设备   一、磁盘结构: 1、设备号码:  主设备号:major number, 标识设备类型 次设备号:minor number, 标识同一类型下的不同设备  &n…

    Linux干货 2016-08-26
  • Shell脚本-循环基础

    Shell脚本-循环基础 背景: 正在学习Shell脚本之循环,发现Shell的循环和其他编程语言大同小异,逻辑上都是相通的,但在使用格式上却有点不同,在学习完Shell循环后,将学习的心得体会记录下来,以备今后复习。 介绍: 什么是Shell脚本:       shell script是利用shell的功能…

    2017-08-26
  • 马哥教育网络班20期+第2周课程练习

    1、Linux上的文件管理命令有哪些?其常用的使用方法及相关示例演示。 答: cp 复制、 mv 移动、rm 删除 (1)cp :复制文件或目录         用法:    cp [OPTION]… [-T] SOURCE DEST    cp [OPTI…

    Linux干货 2016-06-16
  • 小练习题。【第四周】

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户均没有任何访问权限。 home]# chmod g-rwx,o-rwx -R tuser1 2、编辑/etc/group文件,添加组hadoop。 /]# vim /etc/group …

    Linux干货 2016-11-26
  • 2班jackcui20160802作业

    1、每日课堂笔记总结 2、预习 3、每日课堂pdf练习 4、在/data/testdir里创建的新文件自动属于g1组,组g2的成员如:alice能对这些新文件有读写权限,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。 [root@centos7 /]# groupadd g1 [root@centos7 /…

    Linux干货 2016-08-05
  • 马哥教育网络班20期+第3周课程练习

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 # who |cut -d" " -f1 | uniq 2、取出最后登录到当前系统的用户的相关信息。 # last | head -1 3…

    Linux干货 2016-06-26

评论列表(1条)

  • 马哥教育
    马哥教育 2017-03-14 08:35

    从系统启动到自动化装机~再到脚本的例子,写的不错~~加油!