1. 程式人生 > >制作一張屬於DIY的CnetOS 7安裝光盤

制作一張屬於DIY的CnetOS 7安裝光盤

mkisofs 自動化安裝

為什麽我們要自動化安裝系統呢?管理一些重復性的操作呢,這樣可以減輕自己的操作,並且提高了工作效率,假如公司中又有十幾臺甚至更多的服務器,不可能自己一臺一臺的安裝,這樣就需要自動化安裝。

一、光盤內容簡介

1、CentOS系統的啟動流程

POST-->boot Sequence-->bootloader(MBR)-->kernel(initramdisk)-->rootfs-->/sbin/init/

2、光盤內容

(1)[[email protected]/media/cdrom/isolinux]#ls

內容解釋
boot.cat相當於啟動菜單顯示內容
grub.conf啟動系統啟動與boot.cat顯示內容配置文件
isolinux.bin相當於系統安裝的stage2階段
memtest內存檢測,這是個獨立程序
vmlinuz內核映像
boot.msg
initrd.img虛擬文件系統
isolinux.cfgisolinux.cat的配置文件
splash.png背景圖片
vesamenu.c32顯示背景界面

(2)isolinux.cfg詳解

default vesamenu.c32      #支持菜單功能
timeout 600    #菜單倒計時,十分之算

display boot.msg    #顯示一條信息

# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear  #清除菜單選項
menu background splash.png  #顯示背景圖片
menu title CentOS Linux 7   #顯示title
menu vshift 8   
menu rows 18
menu margin 8
#menu hidden    
menu helpmsgrow 15
# Border Area
menu color border * #00000000 #00000000 none

# Selected item
menu color sel 0 #ffffffff #00000000 none

# Title bar
menu color title 0 #ff7ba3d0 #00000000 none

# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none

# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none


# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none

# Help text
menu color help 0 #ffffffff #00000000 none

# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none

# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none

# Command prompt text


menu tabmsg Press Tab for full configuration options on menu items.     #鍵入TAB進入配置模式

menu separator # insert an empty line
menu separator # insert an empty line

label linux     #相當於標簽
  menu label ^Install CentOS Linux 7        #安裝linux
  kernel vmlinuz    #內核文件
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet     #向內核傳遞參數

label check
  menu label Test this ^media & install CentOS Linux 7
  menu default      #默認啟動項
  kernel vmlinuz

menu separator # insert an empty line

# utilities submenu
menu begin ^Troubleshooting
  menu title Troubleshooting

label vesa
  menu indent count 5
  menu label Install CentOS Linux 7 in ^basic graphics mode
  text help
        Try this option out if you‘re having trouble installing
        CentOS Linux 7.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 xdriver=vesa nomodeset quiet

label rescue
  menu indent count 5
  menu label ^Rescue a CentOS Linux system
  text help
        If the system will not boot, this lets you access files
        and edit config files to try to get it booting again.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rescue quiet

label memtest
  menu label Run a ^memory test
  text help
        If your system is having issues, a problem with your
        system‘s memory may be the cause. Use this utility to
        see if the memory is working correctly.
  endtext
  kernel memtest

menu separator # insert an empty line

label local
  menu label Boot from ^local drive
  localboot 0xffff

menu separator # insert an empty line
menu separator # insert an empty line

label returntomain
  menu label Return to ^main menu
  menu exit

menu end

配置文件中有“^”相當於快捷鍵。

3、anaconda程序

(1)概念

anaconda是系統安裝程序。啟動系統安裝過程。

(2)anaconda安裝過程

anaconda安裝分為三個階段:

(a)安裝前配置階段

安裝過程使用的語言
鍵盤類型
安裝目標存儲設備
    basic storage 本地磁盤
    特殊設備 iscsI
設定主機名
配置網絡接口
時區設置
管理員密碼
設定分區方式及MBR的安裝位置
創建一個普通用戶
選定要安裝的程序包

(b)安裝階段在目標磁盤

創建分區,執行格式化操作等
選定的程序包安裝至目標位置
安裝BootLoader和initramfs

(c)圖形模式首次啟動

iptables
selinux
core dump

(3)安裝程序包的選擇

(a)本地光盤
(b)本地硬盤
(c)NFS
(d)URL
    ftp server :yum repository
    http server:yum repository
(e)手動指定安裝源
    在系統啟動選項菜單界面進入TAB鍵,進入boot命令行模式輸入`linux askmethod`,如果需要網絡後續會跳出來一個對話框填入IP即可。

(4)anaconda配置方式

(a)交互式配置
(b)通過讀取事先給定的配置文件自動完成配置(kickstart文件)

4、kickstart文件

(1)配置文件詳解

#version=DEVEL  #指明版本
# System authorization information   
auth --enableshadow --passalgo=sha512 
# Install OS instead of upgrade
install
# Use text install
text
# Firewall configuration
firewall --disabled
firstboot --disable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=cn --xlayouts=‘cn‘
# System language
lang en_US.UTF-8
# Use network installation
#url --url="http://172.16.0.1/cblr/links/CentOS-7-1511-x86_64"
#repo --name="source-1" --baseurl=http://172.16.0.1/cobbler/ks_mirror/CentOS-7-1511-x86_64
#url --url="http://172.16.0.1/cobbler/ks_mirror/7/"
url --url=$tree

# Network information
network  --bootproto=dhcp --device=ens33 --onboot=yes  --ipv6=auto
network  --hostname=localhost.localdomain
# Reboot after installation
reboot
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx

# Root password
rootpw --iscrypted $6$dGARWYghrvhD9W7P$4af2uw8A4tHvNLe2F6bDrk0J69dt.uYoV4SneKG4kzIsc/nF3JpfnuHg7D5lVE.jxC3p6.K29FCjwtom9VXWf.
# System timezone
timezone Asia/Shanghai
user --groups=wheel --name=wang --password=$6$S0t9SxXkvSI0Npz5$YDVonz69YGGGkGyZDy/KesO4CI1HUBLNThcHj6PXDntQGS0Cl0SNpN6zGASuzYE.z5veu1XAzMvO4xLWbKE.n0 --iscrypted --gecos="123456"
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --ondisk=sda --size=500
part swap --fstype="swap" --ondisk=sda --size=2049
part / --fstype="xfs" --ondisk=sda --size=40960
part /app --fstype="xfs" --ondisk=sda --size=20480

%post
systemctl set-default multi-user.target

[ ! -d /root/.ssh ] && mkdir /root/.ssh && chmod og=--- /root/.ssh

cat >> /root/.ssh/authorized_keys << EOF
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAlmq1z0G/7wbGuSUewfXlFnwzqCg/myqTi/AwP8LP+JJ49xzIKMzpeWXHD8RWIf5RlDzo+6N7uPK5O22x/QtMosi0egz4shavEJeUkO0EH+KygXXgBIGuMWmAsL+yzbgWXT9H3zdzXi/qWcrBeBv2nYB5mpYSf7o0xqdhCst1MTfcYLD8qxvkwC8RiqBA/1u9N6jeDFbHO+UzZYYCr9zgk9uz4Rrhb9BU7c1GhjUCgRwBDAuo47IHw/OT6KS9lb8lT2R/ujVoDARy/eOhw8cAFXo+QcvzNSW2qKf/Qo21uR/wz2u9SRV0lvUDNSvC2PYtR+iPlDwHY81md430yiNf9w== [email protected]
EOF

ClientName=`ifconfig enoens33 | awk ‘/inet\>/{print $2}‘ | awk -F. ‘{print $NF}‘`
hostnamectl set-hostname "stu$ClientName.wang.com"

#mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
#wget http://172.18.0.1/repos/CentOS-Base.repo -O /etc/yum.repos.d/CentOS-Base.repo

%end

%packages
@base
@core
@fonts
@input-methods
@kde-apps
@kde-desktop
@network-file-system-client
@networkmanager-submodules
@x11
lftp
screen

%end

%addon com_redhat_kdump --disable --reserve-mb=‘auto‘

%end

(2)kickstart文件格式

(a)命令端:指明各種安裝前配置,如果鍵盤類型,時區等 必備命令 authconfig:認證方式配置 authconfig --useshadow --passalgo=sha512 bootloader:bootloader的安裝位置及相關配置 bootloader --location=mbr --driverorder=sda --append=“crashkernel=auto rhgb quiet” keyboard:設定鍵盤類型 lang:語言類型
part:創建分區 rootpw:指明root的密碼 timezone:時區 可選命令 install OR update text :文本界面安裝 network 設定網絡 firewall 設置防火墻 selinux 設置selinux halt
poweroff reboot repo user:安裝完成後為系統創建新用戶 url:指明安裝源 key -skip:跳過安裝號碼,適用於rhel版本

(b)程序包段:指明要安裝的程序包或程序包組,不安裝的包等

%packages #表明開始安裝程序包或包組
@group_name #安裝包組
package #安裝程序包
-package #不安裝程序包
%end 指明安裝結束

(c)腳本段:指明運行的腳本

%pre #安裝前腳本
    運行環境:運行於安裝介質上的微型linux環境
%post   #安裝後腳本
    運行環境:運行於安裝完成後的系統

註意:在每段中一定要有開始有結束%end表明結束安裝。

(3)kickstart文件創建方式

(a)直接手動編輯,可以依賴模板進行自行修改

(b)可使用創建工具:system-config-kickstart命令,依賴某模板修改並生成新的配置文件 註意:在CentOS7中,如果要使用此命令必須把yum倉庫光盤的名稱修改為development。 檢查ks文件的語法錯誤:ksvalidator 格式:ksvalidator /PATH/TO/SOMEFILE

(4)指明kickstart位置

DVD drive:ks=cdrom:/PATH/TO/KICKSTART_FILE
Hard drive:ks=hd:/PATH/TO/KICKSTART_FILE
FTP server:ks=ftp://host:port/PATH/TO/KICKSTART_FILE
HTTP server:ks=http://host:port/PATH/TO/KICKSTART_FILE
HTTPS server:ks=https://host:port/PATH/TO/KICKSTART_FILE
NFS server:ks=nfs:host:/PATH/TO/KICKSTART_FILE

如果想理解更多,請查考詳細內容《Installation Guide》。

(5)系統啟動的不同方式

(a)text:文本安裝模式

(b)askmethod:手動指定安裝方法 在系統啟動界面,鍵入ESC進入boot命令行模式:輸入linux askmethod 與網絡相關的引導選項: ip=IPADDR netmask=NETMAKS gateway=GATEWAY dns=DNS_SERVER_IP ifname=NAME:MAC_ADDR (c)與遠程功能相關的引導選項 vns vncpassword=“PASSWORD”

實戰

手動指定kickstart文件安裝系統

方式一:

(1)在CentOS7上搭建ftp服務

[[email protected]~]#cd /var/ftp/pub/[[email protected]/var/ftp/pub]#cp /root/ks*.cfg .[[email protected]/var/ftp/pub]#ls

ks6.cfg  ks7.cfg

(2)創建一塊虛擬機

(3)手動指定按啟動,選定光盤啟動

技術分享當出現上面圖示時,我們手動指定kickstart啟動。

方法一: 按TAB鍵,輸入ks=ftp://192.168.4.135/pub/ks6.cfg;回車啟動 技術分享方法二: 按ESC鍵進入boot命令行,輸入linux ks=ftp://192.168.4.135/pub/ks6.cfg

方式二:

在CentOS7搭建http服務

[[email protected]/var/www/html]#mkdir ks[[email protected]/var/www/html]#mv ks*.cfg ks

其余步驟同上述方法類似。

註意:上述過程必須使ks文件權限為644.上述方法前提為橋接模式。

上述方法是沒有辦法安裝包等後續工作的,我們可以搭建網絡yum源進行安裝。

方式一:創建網絡yum源安裝

(1)安裝vsftpd服務,並啟動服務

(2)將ks7.cfg文件復制到 /var/ftp/pub目錄下

(3)在/var/ftp/pub目錄下創建centos/7

[[email protected]/var/ftp/pub]#mkdir centos/7 -pv

(4)將光盤掛載到centos/7上

[[email protected]/var/ftp/pub]#mount /dev/sr0 /var/ftp/pub/centos/7/(5)測試是否能在網頁中打開

輸入地址ftp://192.168.4.135/pub 技術分享

(6)新建一臺虛擬機測試

按ESC鍵進入boot命令行,輸入linux ks=ftp://192.168.4.135/pub/ks6.cfg ip=192.168.4.138 netmask=255.255.255.0

或 按TAB鍵在行後追加 ks=ftp://192.168.4.135/pub/ks6.cfg ip=192.168.4.138 netmask=255.255.255.0

(7)出現下面說明成功 技術分享

方式二:

(1)在CentOS7上搭建http服務

(2)在/var/www/html目錄下創建ks目錄和centos/7目錄

[[email protected]/var/www/html]#mkdir testcentos/7 -pv

(3)將光盤掛載至centos/7目錄下[[email protected]/var/www/html]#mount /dev/sr0 testcentos/7/

(4)重啟服務測試 在網站輸入http://192.168.4.135/test 技術分享(5)新建一臺虛擬機重新測試,出現下面內容說明成功

按ESC鍵進入boot命令行,輸入linux ks=ftp://192.168.4.135/pub/ks7.cfg ip=192.168.4.138 netmask=255.255.255.0

按TAB鍵在行後追加 ks=ftp://192.168.4.135/pub/ks7.cfg ip=192.168.4.138 netmask=255.255.255.0 技術分享

二、制作光盤或U盤

在日常的生產活動中,使用光驅的已經很少了,但是U盤自身的特點得到了大多數人的使用,這樣讓我掌握如何制作U盤變得特別重要,如果我們公司有的服務器沒有光盤咋辦,?U盤就很好的幫我們解決了這個問題。

1、如何制作光盤

(1)mkisofs命令有關包genisoimage 制作光盤需要使用命令才能執行。

選項解釋
-o指定影響文件的名稱
-b指定在制作可開機光盤是所需要的開機映像文件
-c制作可開機光盤時,會將開機映像文件中的no-eltorito-catalog全部內容做成一個文件
-no-emul-boot非模擬模式啟動
-boot-load-size 4設置載入部分的數量
-boot-info-table在啟動的圖像中顯示信息
-R,-rock使用Rock RidgeExtensions
-J,-joliet使用Joliet格式的目錄與文件名稱
-v,-verbose執行時顯示過程
-T,-translation-table建立文件名的裝換表,適用於不支持Rock RidgeExtensions的系統上

(2)制作光盤過程

(a)在CentOS7主機上搭建yum源,並綁定到ftp服務上。

(b)創建光盤引導文件

[[email protected]~]#mkdir /tmp/testiso[[email protected]~]#mkdir /tmp/testiso/isolinux[[email protected]~]#cp /media/cdrom/isolinux/* /tmp/testiso/isolinux[[email protected]/tmp/testiso]#cp /var/ftp/pub/ks7.cfg .[[email protected]/tmp/testiso]#vim ks7.cfg #編輯配置文件 技術分享[[email protected]/tmp/testiso]#tree

.
├── isolinux
│   ├── boot.cat
│   ├── boot.msg
│   ├── grub.conf
│   ├── initrd.img
│   ├── isolinux.bin
│   ├── isolinux.cfg
│   ├── memtest
│   ├── splash.png
│   ├── TRANS.TBL
│   ├── vesamenu.c32
│   └── vmlinuz
└── ks7.cfg

註意:此處創建使用的是相對路徑,而和工作目錄無關。必須在isolinux下。

(c)創建光盤[[email protected]/tmp]#mkisofs -R -J -T -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "CentOS 6.9 x86_64 boot" -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/boot.iso myiso/

(d)將光盤下載至本機,安裝到虛擬中嘗試啟動 技術分享

說明我們制作的光盤成功

(3)制作U盤

(a)我們這裏虛擬一塊U盤,添加一塊硬盤

(b)將光盤拷貝至硬盤中

[[email protected]~]#dd if=boot.iso of=/dev/sdb

(c)新建虛擬機掛載此光盤啟動 技術分享出現上述情況說明我們安裝成功。但是這種方法還需要通過網絡才能安裝,如果我們去到一個地方沒有網絡,怎麽辦呢?有沒有想過我們把rpm包到拷貝至一個U盤或光盤上呢?這樣是不是很方便我們辦公呢?

(4)制作完整的光盤和U盤

(a)創建一個臨時目錄並拷貝文件至此目錄下[[email protected] ~]#mkdir /tmp/fulliso[[email protected] ~]# cp -a /media/cdrom/* /tmp/fulliso/[[email protected] ~]# cd /tmp/fulliso/[[email protected] fulliso]# cp /media/cdrom/.treeinfo .[[email protected] fulliso]# cp /media/cdrom/.discinfo .

(b) 刪除repodate目錄非*comps.xml結尾的文件。[[email protected] fulliso]# find repodata/ -not -name *-comps.xml -delete[[email protected] fulliso]# find -name TRANS.TBL -delete

(c)以上述那個文件制作倉庫[[email protected] fulliso]# ls repodata/ d918936f5019be3fb66e9981a28cb2a41477a2963d741d454f79377a22214f43-c7-x86_64-comps.xml

(d)編輯kickstart配置文件,並復制fulliso目錄下[[email protected] fulliso]# vim ks7.cfg

auth --enableshadow --passalgo=sha512
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=us --xlayouts=‘us‘
# System language
lang en_US.UTF-8

# Network information
network  --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto 
network  --hostname=localhost.localdomain

# Root password
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai
# X Window System configuration information
xconfig  --startxonboot
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
zerombr
reboot
# Partition clearing information
clearpart --all
part /boot --fstype="xfs" --ondisk=sda --size=1024
part swap --fstype="swap" --ondisk=sda --size=2048
part / --fstype="xfs" --ondisk=sda --size=50000
part /app --fstype="xfs" --ondisk=sda --size=50000

%packages
@^gnome-desktop-environment
@base
@core
@desktop-debugging
@dial-up
@directory-client
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@networkmanager-submodules
@print-client
@x11
kexec-tools
autofs
lftp
dhcp
vsftpd

%end
%post
#Create repo
mkdir /media/cdrom
mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
cat > /etc/yum.repos.d/base <<EOF
[base]
name=centos 7
baseurl=file:///media/cdrom
gpgcheck=0
enabled=1
EOF

cat >> /etc/fstab << EOF
/dev/sr0        /media/cdrom    iso9660 defaults 0 0

cat >> /root/.bashrc << EOF
alias cdnet="cd /etc/sysconfig/network-scripts/"
EOF

cat >> /etc/profile.d/env.sh
export PS1=‘\[\e[31m\][\[email protected]\h\w]\$\[\e[0m\]‘
EOF

sed -i ‘s/quiet/& net.ifnames=0/‘ /etc/default/grub
grub2-config -o /boot/grub2/grub.cfg

%end

(e)編輯isolinux.cfg文件[[email protected] fulliso]# vim isolinux/isolinux.cfg

default vesamenu.c32
timeout 600

display boot.msg

# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png
menu title CentOS Linux 7
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13

# Border Area
# Selected item
menu color sel 0 #ffffffff #00000000 none

# Title bar
menu color title 0 #ff7ba3d0 #00000000 none

# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none

# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none

# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none

# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none

# Help text
menu color help 0 #ffffffff #00000000 none

# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none

# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none

# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none

# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.

menu tabmsg Press Tab for full configuration options on menu items.

menu separator # insert an empty line
menu separator # insert an empty line

label desktop
  menu label desktop Linux 7
  kernel vmlinuz
  append initrd=initrd.img ks=cdrom:/ks7.cfg

label mini
  menu label mini linux 7
  menu default
  kernel vmlinuz
  append initrd=initrd.img ks=cdrom:/ks7mini.cfg

menu end

(f)制作光盤[[email protected] ~]# mkisofs -R -T -J -v --no-emul-boot --boot-load-size 4 --boot-info-table -V "CentOS 7 custom" -b isolinux/isolinux.bin -c isolinux/boot.cat -o /root/centos7.iso /tmp/fulliso/

(g)測試是否可成功,新建一臺虛擬機,測試 技術分享出現上述界面,表明制作光盤已經成功。


本文出自 “Joah” 博客,請務必保留此出處http://merit.blog.51cto.com/10757694/1966072

制作一張屬於DIY的CnetOS 7安裝光盤