第十周作業

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.www58058.com/70498

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

相關推薦

  • N25第一周學習總結

    第一周學習總結 按照課程要求,本周完成2天的課時,總計8課時。 因為是剛開課,馬哥為我們介紹了上課環境,課程體系,還有一些計算機基礎理論。內容主要是:          計算機的工作機制        &nbs…

    Linux干貨 2016-12-04
  • Linux基礎之shell腳本編程(二)

    1、寫一個腳本,完成以下功能    (1) 假設某目錄(/etc/rc.d/rc3.d/)下分別有K開頭的文件和S開頭的文件若干;    (2) 顯示所有以K開頭的文件的文件名,并且給其附加一個stop字符串;    (3) 顯示所有以S開頭的文件的文件名,并且給其附加一個start字符串; &nb…

    Linux干貨 2016-11-17
  • 管理systemd

    管理systemd init(系統的第一個進程): CentOS 5: SysV initCentOS 6: UpstartCentOS 7: Systemd Systemd:系統啟動和服務器守護進程管理器,負責在系統啟動或運行時,激活系統資源,服務器進程和其它進程 Systemd新特性: 系統引導時實現服務并行啟動 按需啟動守護進程 系統狀態快照 自動化的…

    Linux干貨 2016-09-22
  • iptables

    iptables 一、基礎概念 1、防火墻概念 Firewall:隔離工具;Packets Filter Firewall;工作于主機或網絡的邊緣,對經由的報文根據預先定義的規則(匹配條件)進行檢測,對于能夠被規則匹配到的報文實行某預定義的處理機制的一套組件; 如果沒有防火墻,你的本機的所有端口都會被別人訪問到! 2、分類 硬件防火墻:在硬件級別實現部分功能…

    2016-10-26
  • ansible的入門使用手冊

    ansible1

    2018-01-15
  • 【盤點】2017年9-11月運維大會(時間+地點)

    到底未來的運維模式是什么?如今運維人員面臨著怎樣的轉變? 活動家為您精心挑選整理了9-11月七場運維大會希望本文能幫您! CNUTCon 全球運維技術大會2017 大會簡介: CNUTCon全球運維技術大會是由InfoQ主辦的運維&容器技術盛會。大會為期2天,主要面向各行業對運維&容器技術感興趣的中高端技術人員。秉承著“同步前沿技術、共享實戰…

    2017-08-31

評論列表(1條)

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

    從系統啟動到自動化裝機~再到腳本的例子,寫的不錯~~加油!

欧美性久久久久