PXE 基於網路自動安裝CentOS 6/7/8

我來烤烤你鴨發表於2020-09-30

PXE 基於網路自動安裝CentOS 6/7/8

說明:

  • 下載好相關映象檔案,新建虛擬機器安裝系統
  • 使用 CentOS 8 作為http、dhcp、tftp 伺服器
  • 要求作為伺服器的 CentOS 8 的 ip 為 10.0.0.8
  • 在VM上依次新增光碟機並選擇對應的映象檔案:8 > 7 > 6

設定服務端

CentOS 8 正常開機,確保網路暢通,執行以下指令碼:

#!/bin/bash
. /etc/init.d/functions
echo -e "\e[1;32m開始配置伺服器!請等待。。。\e[0m"
yum -y install httpd tftp-server dhcp-server syslinux &> /dev/null

cat > /etc/dhcp/dhcpd.conf <<EOF
subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.100 10.0.0.200;
option routers 10.0.0.2;
next-server 10.0.0.8;
filename "pxelinux.0";
}
EOF

systemctl start httpd tftp.socket dhcpd
ss -ntul | grep -e 67 -e 69 -e 80 &> /dev/null && action '成功啟用各項服務'
sleep 3
echo '- - -' > /sys/class/scsi_host/host0/scan
echo '- - -' > /sys/class/scsi_host/host1/scan
echo '- - -' > /sys/class/scsi_host/host2/scan

mkdir -p /var/www/html/centos/{6,7,8}/os/x86_64/

mount /dev/sr0 /var/www/html/centos/8/os/x86_64/
mount /dev/sr1 /var/www/html/centos/7/os/x86_64/
mount /dev/sr2 /var/www/html/centos/6/os/x86_64/ && action '成功掛載各個光碟'
sleep 3
mkdir /var/www/html/ks/
cd /var/www/html/ks/
#################################################
# 寫入centos8的應答檔案
cat > centos8.cfg <<EOF
# 基本設定
text    # 文字安裝介面
lang en_US.UTF-8
keyboard --vckeymap=us --xlayouts=us
timezone Asia/Shanghai --isUtc --nontp  # 必須是大寫U
rootpw --plaintext centos
selinux --disabled      # 禁用SELinux
firewall --disabled     # 禁用firewalld
firstboot --enable      # 初始化設定
reboot  # 安裝完自動重啟
skipx
services --disabled="chronyd"

# 網路
url --url=http://10.0.0.8/centos/8/os/x86_64/
network  --device eth0 --bootproto dhcp --ipv6=auto --activate
network  --hostname=centos8

# 分割槽
zerombr     # 清除mbr
ignoredisk --only-use=sda    # 選擇在sda安裝系統【此行必須指定】
bootloader --append="net.ifnames=0" --location=mbr --boot-drive=sda  # 此行必須指定
clearpart --all --initlabel # 清除分割槽資訊
part /     --fstype="ext4" --ondisk=sda --size=102400
part /data --fstype="ext4" --ondisk=sda --size=51200
part swap  --fstype="swap" --ondisk=sda --size=2048
part /boot --fstype="ext4" --ondisk=sda --size=1024

# 軟體安裝
%packages
@^minimal-environment
kexec-tools
vim
curl
wget
tree
%end

# 指令碼
%post
useradd jacklee
echo centos passwd --stdin jacklee &> /dev/null

sed -i.bak 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
systemctl disable --now firewalld.service
systemctl stop firewalld.service

mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo  /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/jack.repo <<EOFA
[BaseOS] 
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/
        https://mirrors.tuna.tsinghua.edu.cn/centos/8/BaseOS/x86_64/os/
gpgcheck=0
enabled=1

[AppStream]
name=AppStream
baseurl=https://mirrors.aliyun.com/centos/\$releasever/AppStream/x86_64/os/
        https://mirrors.tuna.tsinghua.edu.cn/centos/8/AppStream/x86_64/os/
gpgcheck=0
enabled=1

[EPEL]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/
        https://mirrors.tuna.tsinghua.edu.cn/epel/8/x86_64
gpgcheck=0
enabled=1
EOFA
yum makecache

yum -y install gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl* openssh* systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man bash-completion

cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << EOFB
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.0.0.88
PREFIX=24
GATEWAY=10.0.0.2
DNS1=180.76.76.76
DNS2=223.6.6.6
EOFB

cat > /etc/profile.d/env.sh <<EOFC
PS1="\[\e[1;\$[RANDOM%7+31]m\][\u@\h \W]\\\\\\\\$\[\e[0m\]" 
export EDITOR=vim
EOFC

cat > /root/.vimrc << EOFD
set nu
set cul
set ai
set paste
set et
set ts=4
syntax on
EOFD

cat >> /root/.vimrc << EOFF
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
    if expand ("%:e") == 'sh'                                                         
        call setline(1, "#!/bin/bash")
        call setline(2, "#")
        call setline(3, "#********************************************")
        call setline(4, "#Author:       jacklee")
        call setline(5, "#QQ:           1227163339")
        call setline(6, "#Time:         ".strftime("%F_%T"))
        call setline(7, "#FileName:     ".expand("%"))
        call setline(8, "#Copyright:    ".strftime("%Y")." All rights reserved")
        call setline(9, "#Description:   ")
        call setline(10,"#*********************************************")
    endif
endfunc
EOFF

yum -y install chrony*
sed -i.bak 's/^pool 2.centos.*/server ntp.aliyun.com iburst/' /etc/chrony.conf
systemctl enable --now chronyd.service
%end
EOF
################################################
# 寫入centos7的應答檔案
cat > centos7.cfg <<EOF
text    # 文字安裝介面
lang en_US.UTF-8
keyboard 'us'
timezone Asia/Shanghai --nontp
rootpw --plaintext centos
selinux --disabled      # 禁用SELinux
firewall --disabled     # 禁用firewalld
firstboot --enable      # 初始化設定
reboot  # 安裝完自動重啟
services --disabled="chronyd"

# 網路
url --url=http://10.0.0.8/centos/7/os/x86_64/
network  --device eth0 --bootproto dhcp
network  --hostname=centos7

# 分割槽
zerombr     # 清除mbr
ignoredisk --only-use=sda    # 選擇在sda安裝系統【此行必須指定】
bootloader --append="net.ifnames=0" --location=mbr --boot-drive=sda  # 此行必須指定
clearpart --all --initlabel # 清除分割槽資訊
part /     --fstype="ext4" --ondisk=sda --size=102400
part /boot --fstype="ext4" --ondisk=sda --size=1024
part /data --fstype="ext4" --ondisk=sda --size=51200
part swap  --fstype="swap" --ondisk=sda --size=2048

# 軟體安裝
%packages
@^minimal
kexec-tools
vim
curl
wget
tree
%end

# 指令碼
%post
useradd jacklee
echo centos passwd --stdin jacklee &> /dev/null

sed -i.bak 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
systemctl disable --now firewalld.service
systemctl stop firewalld.service

mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo  /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/jack.repo <<EOFA
[BaseOS] 
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
        https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/
gpgcheck=0
enabled=1

[EPEL]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
        https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/
gpgcheck=0
enabled=1
EOFA
yum makecache

yum -y install gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl* openssh* systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man bash-completion

sed -r -i "s/quiet/& net.ifnames=0/" /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg

cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << EOFB
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.0.0.77
PREFIX=24
GATEWAY=10.0.0.2
DNS1=180.76.76.76
DNS2=223.6.6.6
EOFB

cat > /etc/profile.d/env.sh <<EOFC
PS1="\[\e[1;\$[RANDOM%7+31]m\][\u@\h \W]\\\\\\\\$\[\e[0m\]" 
export EDITOR=vim
EOFC

cat > /root/.vimrc << EOFD
set nu
set cul
set ai
set paste
set et
set ts=4
syntax on
EOFD

cat >> /root/.vimrc << EOFF
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
    if expand ("%:e") == 'sh'                                                         
        call setline(1, "#!/bin/bash")
        call setline(2, "#")
        call setline(3, "#********************************************")
        call setline(4, "#Author:       jacklee")
        call setline(5, "#QQ:           1227163339")
        call setline(6, "#Time:         ".strftime("%F_%T"))
        call setline(7, "#FileName:     ".expand("%"))
        call setline(8, "#Copyright:    ".strftime("%Y")." All rights reserved")
        call setline(9, "#Description:   ")
        call setline(10,"#*********************************************")
    endif
endfunc
EOFF

yum -y install chrony*
sed -i.bak 's/^server 0.centos.*/server ntp.aliyun.com iburst/' /etc/chrony.conf
sed -i.bak 's/^server 1.centos.*/server ntp1.aliyun.com iburst/' /etc/chrony.conf
sed -i.bak 's/^server 2.centos.*/server time1.cloud.tencent.com iburst/' /etc/chrony.conf
sed -i.bak 's/^server 3.centos.*/server time2.cloud.tencent.com iburst/' /etc/chrony.conf
systemctl enable --now chronyd.service
%end
EOF
###################################################
# 寫入centos6的應答檔案
cat > centos6.cfg <<EOF
# 基本設定
text    # 文字安裝介面
lang en_US.UTF-8
keyboard 'us'
timezone Asia/Shanghai
rootpw --plaintext centos
selinux --disabled      # 禁用SELinux
firewall --disabled     # 禁用firewalld
firstboot --enable      # 初始化設定
reboot  # 安裝完自動重啟

# 網路
url --url=http://10.0.0.8/centos/6/os/x86_64/
network  --device eth0 --bootproto dhcp
network  --hostname=centos6

# 分割槽
zerombr     # 清除mbr
ignoredisk --only-use=sda    # 選擇在sda安裝系統【此行必須指定】
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
clearpart --all --initlabel # 清除分割槽資訊
part /     --fstype="ext4" --ondisk=sda --size=102400
part /boot --fstype="ext4" --ondisk=sda --size=1024
part /data --fstype="ext4" --ondisk=sda --size=51200
part swap  --fstype="swap" --ondisk=sda --size=2048

# 軟體安裝
%packages
@core
@server-policy
@workstation-policy
autofs
vim-enhanced
%end

# 指令碼
%post
useradd jacklee
echo centos passwd --stdin jacklee &> /dev/null

sed -i.bak 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
chkconfig iptables off
service iptables stop

mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo  /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/jack.repo <<EOFA
[BaseOS] 
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/6/os/x86_64/
        https://mirrors.tuna.tsinghua.edu.cn/centos/6/os/x86_64/
gpgcheck=0
enabled=1

[EPEL]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/6/x86_64/
        https://mirrors.tuna.tsinghua.edu.cn/epel/6/x86_64/
gpgcheck=0
enabled=1
EOFA
yum makecache

yum -y install gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl* openssh* systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man bash-completion

cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << EOFB
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.0.0.66
PREFIX=24
GATEWAY=10.0.0.2
DNS1=180.76.76.76
DNS2=223.6.6.6
EOFB

cat > /etc/profile.d/env.sh <<EOFC
PS1="\[\e[1;\$[RANDOM%7+31]m\][\u@\h \W]\\\\\\\\$\[\e[0m\]" 
export EDITOR=vim
EOFC

cat > /root/.vimrc << EOFD
set nu
set cul
set ai
set paste
set et
set ts=4
syntax on
EOFD

cat >> /root/.vimrc << EOFF
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
    if expand ("%:e") == 'sh'                                                         
        call setline(1, "#!/bin/bash")
        call setline(2, "#")
        call setline(3, "#********************************************")
        call setline(4, "#Author:       jacklee")
        call setline(5, "#QQ:           1227163339")
        call setline(6, "#Time:         ".strftime("%F_%T"))
        call setline(7, "#FileName:     ".expand("%"))
        call setline(8, "#Copyright:    ".strftime("%Y")." All rights reserved")
        call setline(9, "#Description:   ")
        call setline(10,"#*********************************************")
    endif
endfunc
EOFF

yum -y install ntp* &> /dev/null
sed -i.bak 's/^server 0.*/server ntp.aliyun.com iburst/' /etc/ntp.conf
sed -i.bak 's/^server 1.*/server ntp1.aliyun.com iburst/' /etc/ntp.conf
sed -i.bak 's/^server 2.*/server time1.cloud.tencent.com iburst/' /etc/ntp.conf
sed -i.bak 's/^server 3.*/server time2.cloud.tencent.com iburst/' /etc/ntp.conf
chkconfig --add ntpd
chkconfig ntpd on
service ntpd restart
%end
EOF
# 應答檔案完成
##############################
mkdir /var/lib/tftpboot/centos{6,7,8}
cp /var/www/html/centos/6/os/x86_64/isolinux/{vmlinuz,initrd.img}  /var/lib/tftpboot/centos6
cp /var/www/html/centos/7/os/x86_64/isolinux/{vmlinuz,initrd.img}  /var/lib/tftpboot/centos7
cp /var/www/html/centos/8/os/x86_64/isolinux/{vmlinuz,initrd.img}  /var/lib/tftpboot/centos8
cp /usr/share/syslinux/{pxelinux.0,menu.c32}   /var/lib/tftpboot/
cp /var/www/html/centos/8/os/x86_64/isolinux/{ldlinux.c32,libcom32.c32,libutil.c32} /var/lib/tftpboot/

mkdir /var/lib/tftpboot/pxelinux.cfg/
cat > /var/lib/tftpboot/pxelinux.cfg/default << EOF
default menu.c32
timeout 600
menu title Install centos Linux

label linux8
menu label Auto Install centos Linux ^8
kernel centos8/vmlinuz
append initrd=centos8/initrd.img ks=http://10.0.0.8/ks/centos8.cfg

label linux7
menu label Auto Install centos Linux ^7
kernel centos7/vmlinuz
append initrd=centos7/initrd.img ks=http://10.0.0.8/ks/centos7.cfg

label linux6
menu label Auto Install centos Linux ^6
kernel centos6/vmlinuz
append initrd=centos6/initrd.img ks=http://10.0.0.8/ks/centos6.cfg

label manual
menu label ^ManualInstall Centos Linux 8.0 
kernel centos8/vmlinuz
append initrd=centos8/initrd.img
inst.repo=http://10.0.0.8/centos/8/os/x86_64/

label rescue
menu label ^Rescue a CentOS Linux system 8
kernel centos8/vmlinuz
append initrd=centos8/initrd.img
inst.repo=http://10.0.0.8/centos/8/os/x86_64/   rescue

label local
menu default
menu label Boot from ^local drive
localboot Oxffff
EOF
echo -e "\e[1;32m伺服器以及相關檔案配置完成!\e[0m"

安裝系統

  1. 指令碼執行完畢,伺服器端配置成功
  2. 在VM新建虛擬機器,選擇需要安裝的版本,並設定硬碟大小為200G(至少150G)
  3. 注意,CentOS 7/8 應設定記憶體至少為 2G
  4. 開機後迅速按esc選擇第四項 網路啟動(實際上預設也會跳轉到安裝選單)
  5. 隨即進入安裝選單介面,根據VM中選擇的版本在此處選擇對應的選項
  6. 等待安裝

備註

  • 指令碼內容較長,是因為3個版本的應答檔案中都包含了大篇幅的初始化指令碼內容
  • 用此方式安裝,最終得到的ip為
    • CentOS 6 = 10.0.0.66
    • CentOS 7 = 10.0.0.77
    • CentOS 8 = 10.0.0.88
  • 若需要自定義ip,可在開始安裝之前直接修改指令碼內容
  • 若有自己寫好的應答檔案,可直接刪除指令碼的【30~407行】內容
    並將應答檔案放入伺服器CentOS 8的/var/www/html/ks/目錄下
    但檔名必須為centos6/7/8.cfg
  • 硬碟大小至少設定150G的原因:應答檔案中分割槽策略使用了150G

相關文章