[TOC]
### 總體流程
> 復制官方ISO -> 集成rpm -> 編寫ks.cfg文件 -> 加入自定義業務部署腳本 -> ISO封裝 -> 寫入U盤
### 使用到的工具
`yum -y install anaconda createrepo mkisofs rsync syslinux`
## 具體操作
### 掛載標準鏡像
這里使用CentOS-7-x86_64-Minimal-1511.iso作為基礎鏡像,選擇Minimal是因為鏡像比較小,可以保證最終生成的鏡像大小不至于很大,需要的功能,根據需要添加即可。
掛載光盤鏡像,并復制光盤內容到/tmp/iso目錄下
#創建掛載目錄
`mkdir /mnt/cdrom`
#掛載光盤到/mnt目錄
`mount -o loop /dev/sr0 /mnt/cdrom`
#創建工作目錄
`mkdir -p /tmp/iso`
#復制光盤內容到iso目錄
`cp -r /mnt/cdrom/* /tmp/iso/`
#設置光盤作為yum本地源,方便分析哪些包在光盤中沒有,需要添加
```
cd /etc/yum.repo
mkdir bak
mv *.repo bak
mv bak/CentOS-Media.repo .
vi CentOS-Media.repo
[c7-local]
name=CentOS-7-CD
baseurl=file:///tmp/iso/
gpgcheck=0
enabled=1
gpgkey=file:///tmp/iso/RPM-GPG-KEY-CentOS-7
yum clean all
yum makecache
```
### 編寫ks.cfg
如果希望使用圖形化工具生成ks.cfg可以安裝system-config-kickstart,這邊將直接編輯ks.cfg
```
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password 默認是www.upsoft01.com
rootpw --iscrypted $1$4FH.jrCj$NIpqwnkXalm.HqmrN4bMQ/
# System language
lang en_US
# Firewall configuration
firewall --enabled --ssh
# System authorization information
auth --useshadow --passalgo=sha512
# Use CDROM installation media安裝源
cdrom
# 指定要安裝的軟件包
# 在%packages與%end中間加入需要自定義安裝的包組
# 默認
%packages
@^minimal
@core
#kexec-tools
# 額外添加
#net-tools
#openssl
#pciutils
#percona-xtrabackup-24
%end
# Use graphical install圖形化界面,txt即為命令行模式
graphical
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
# Network information啟動默認eth0的DHCP
network --bootproto=dhcp --device=eth0 --onboot=yes --activate
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
# 分區設置
# 要使用全部空間 使用--size=1 --grow
# 分區要使用所有剩余空間大小 --percent=100
part /boot --fstype="xfs" --ondisk=sda --size=400
part pv.364 --fstype="lvmpv" --ondisk=sda --size=1 --grow
volgroup centos_school --pesize=4096 pv.364
logvol swap --fstype="swap" --size=4096 --name=swap --vgname=centos_school
logvol /home --fstype="xfs" --percent=100 --name=date --vgname=centos_school
logvol / --fstype="xfs" --size=10240 --name=root --vgname=centos_school
#%pre
# 安裝前執行的腳本 注意 此時磁盤尚未格式化 所以能執行的工作有限
# 常用于讀取磁盤信息 生成分區腳本 然后供之后格式化使用
#%end
# 安裝后腳本
# %post --nochroot
# 安裝后腳本有兩種模式 一種是在安裝環境運行 此時系統掛載于/mnt/sysimage 光盤掛載于/mnt/install/repo
# 可以從光盤復制文件到系統
# %post --log=/root/ks-post.log
# 但是如果安裝源不被識別為/dev/sr0 則不能通過掛載的方式把光盤中的文件復制到系統
# 將外部文件拷貝到系統內,需在/ISO目錄下創建plus文件夾,
# 將外部文件(例如二進制文件dota_driver)放進plus目錄下。
# 在安裝系統期間,該文件的絕對路徑為/run/install/repo/plus/,而系統的文件路徑掛載在/mnt/sysimage,
# 所以目的地址為/mnt/sysimage/usr/bin。
%post --nochroot
# 復制安裝光盤plus目錄下所有內容到系統/mnt目錄下
mkdir -p /mnt/sysimage/mnt/plus
cp -fr /mnt/install/repo/plus/* /mnt/sysimage/mnt/plus/
# 復制安裝光盤plus/init.sh文件到系統/root目錄下 作為初始化執行腳本
cp /mnt/install/repo/plus/init.sh /mnt/sysimage/root/
echo "successd read" > /mnt/sysimage/install.log
%end
```
自己編寫的ks.conf文件很有可能存在語法錯誤,可以通過這個命令來檢查
`ksvalidator ks.cfg`
如果沒有任何輸出則表示文件沒有語法錯誤
ks.cfg文件轉換成linux格式,放到/tmp/iso/isolinux下
### 修改isolinux.cfg
```
default vesamenu.c32
timeout 150
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 7
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13
# 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
# 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 linux
menu label ^Install CentOS 7 Manual
kernel vmlinuz
append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet
label linux2
menu label ^Install CentOS 7 Auto
menu default
kernel vmlinuz
append initrd=initrd.img inst.ks=hd:LABEL=CentOS\x207\x20x86_64:/isolinux/ks.cfg inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet
label check
menu label Test this ^media & install CentOS 7
kernel vmlinuz
append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rd.live.check quiet
menu separator # insert an empty line
# utilities submenu
menu begin ^Troubleshooting
menu title Troubleshooting
label vesa
menu indent count 5
menu label Install CentOS 7 in ^basic graphics mode
text help
Try this option out if you're having trouble installing
CentOS 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 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
```
isolinux.cfg文件存在isolinux文件夾下
* timeout 150 表示倒計時15秒將從默認選項開始執行安裝
* 這里的默認選項是 Install CentOS 7 Auto
menu default為默認選項的標識
* 新增一段,添加一個選項
```
label linux2
menu label ^Install CentOS 7 Auto
menu default
kernel vmlinuz
append initrd=initrd.img inst.ks=hd:LABEL=CentOS\x207\x20x86_64:/isolinux/ks.cfg inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet
```
其中指定了ks.cfg的路徑inst.ks=hd:LABEL=CentOS\x207\x20x86_64:/isolinux/ks.cfg
### 自定義業務腳本
* ks.cfg中%post和%end段內定制了安裝完成系統后執行的命令
```
mkdir -p /mnt/sysimage/mnt/plus
cp -fr /mnt/install/repo/plus/* /mnt/sysimage/mnt/plus/
cp /mnt/install/repo/plus/init.sh /mnt/sysimage/root/
```
其中的init.sh就是自定義業務的腳本
```
#!/bin/sh
echo "###########################################################"
echo "++++++++++++++Setup System++Deplay Bigant++++++++++++++++++"
echo "###########################################################"
#主動啟動網卡并設置開機自啟動
interface=$(ls /sys/class/net| grep -v "lo" | head -1)
ipaddr=$(ip route show | grep -v default | awk '{print $9}')
ifup $interface
sed -i "s/^ONBOOT=no/ONBOOT=yes/g" /etc/sysconfig/network-scripts/ifcfg-${interface}
#添加防火墻規則
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
#修改系統資源限制
sed -i '51a* soft noproc 40960' /etc/security/limits.conf
sed -i '51a* hard noproc 40960' /etc/security/limits.conf
sed -i '51a* soft nofile 40960' /etc/security/limits.conf
sed -i '51a* hard nofile 40960' /etc/security/limits.conf
#安裝bigant
cd /mnt/plus
tar zxvf *.tar.gz
cd oneinstack
./install.sh -a && /etc/init.d/bigant_root && echo -e "\e[1;31mPlace reboot system and deplay with Browser! \n Open http://${ipaddr}:8000\e[0m"
```
### 打包ISO
1. 制作iso文件
`mkisofs -o /tmp/CentOS7-v1.0.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -joliet-long -R -J -v -V "CentOS 7 x86\_64" -T /tmp/iso/`
* mkisofs參數說明:
\-o /tmp/My-Server-CentOS7-v1.0.iso,設置輸出文件名,-output
\-b isolinux/isolinux.bin,指定開機映像文件
\-c isolinux/boot.cat,制作啟動光盤時,mkisofs會將開機映像文件中的全-eltorito-catalog\*文件的全部內容作成一個文件
\-no-emul-boot,非模擬模式啟動
\-boot-load-size 4,
\-boot-info-table,
\-joliet-long,
\-R,使用Rock Ridge Extensions,是用于linux環境下的光盤,文件名區分大小寫同時記錄文件長度,-rock
\-J,使用Joliet格式的目錄與文件名稱,Jolient是windows下使用的光盤,-joliet
\-v,執行時顯示詳細的信息,-verbose
\-V "CentOS 7 x86\_64",設置卷標Volume ID,-volid
\-T,建立文件名的轉換表,適用于不支持Rock Ridge Extensions的系統,-translation-table
/tmp/iso/,光盤源文件目錄
2. 轉換為ISOhybrid (該命令由syslinux提供)
`isohybrid /tmp/CentOS7-v1.0.iso`
3. 嵌入md5校驗碼 (該命令由isomd5sum提供)
`implantisomd5 /tmp/CentOS7-v1.0.iso`
4. 檢驗md5
`checkisomd5 /tmp/CentOS7-v1.0.iso`