1. 程式人生 > >在Centos6/7上實現PXE自動化安裝Centos6/7

在Centos6/7上實現PXE自動化安裝Centos6/7

Centos6上實現PXE自動化安裝Centos6

一、1.安裝相關包

yum install dhcp tftp-server httpd syslinux-nonlinux

設定為開機啟動

chkconfig httpd on
chkconfig dhcpd on
chkconfig tftp on

由於tftp是一個非獨立服務,所以,要讓其生效需要啟動超級守護程序xinetd service xinetd start

二.準備yum倉庫和應答檔案

1.在/var/www/html/下建立資料夾 centos/6和ksdir mkdir /var/www/html/centos/6 ksdir -pv

2.掛載光碟到centos/6

[[email protected] html]#mount /dev/sr0 centos/6

3.將應答檔案放到ksdir目錄下(文末附應答檔案)

三、配置DHCP

1.複製dhcp配置模板檔案dhcpd.conf.sample覆蓋dhcp配置檔案dhcpd.conf,之後修改其中配置引數,在此我只修改了下面一項內容。

subnet 192.168.36.0 netmask 255.255.255.0 {
        range 192.168.36.10 192.168.36.200;
        next-server 192.168.36.6;
        filename "pxelinux.0";
}

重啟dhcp服務 systemctl restart dhcpd

四、準備啟動相關的檔案

1.此時需要準備上一步中的pxelinux.0檔案,此檔案在syslinux-nonlinux中,可以直接從該包中複製(可通過rpm -ql syslinux-nonlinux 檢視)

​​​​​​​cp /usr/share/syslinux/pxelinux.0   /var/lib/tftpboot/

重啟dhcp服務 service dhcpd restart

2.將光碟中的部分必要檔案複製到/var/lib/tftpboot/目錄中。這些檔案包括:isolinux.cfg、boot.msg、 splash.jpg、 initrd.img、  vmlinuz、vesamenu.c32

[[email protected] isolinux]#cp boot.msg splash.jpg initrd.img vesamenu.c32  vmlinuz /var/lib/tftpboot/

其中在/var/lib/tftpbpoot/下建立目錄pxelinux.cfg,將isolinux.cfg複製到其中並重命名為default。修改default中的相關配置引數,在此處我修改了各個選單的相關引數,最終制作了四個啟動選單,如下

default vesamenu.c32
#prompt 1
timeout 60:     #啟動等待時間

display boot.msg

menu background splash.jpg
menu title Welcome to CentOS 6.10!
menu color border 0 #ffffffff #00000000
menu color sel 7 #ffffffff #ff000000
menu color title 0 #ffffffff #00000000
menu color tabmsg 0 #ffffffff #00000000
menu color unsel 0 #ffffffff #00000000
menu color hotsel 0 #ff000000 #ffffffff
menu color hotkey 7 #ffffffff #ff000000
menu color scrollbar 0 #ffffffff #00000000

#自動安裝
label linux
  menu label ^Auto Install an Mini system
  menu default
  kernel vmlinuz
  append initrd=initrd.img ks=http://192.168.36.6/ksdir/ks_centos6.cfg
#手動安裝
label vesa
  menu label ^Manual Install an system 
  kernel vmlinuz
  append initrd=initrd.img nomodeset
#救援模式
label rescue
  menu label ^Rescue installed system
  kernel vmlinuz
  append initrd=initrd.img rescue repo://192.168.36.6/centos/6
#本地光碟啟動
label local
  menu default
  menu label Boot from ^local drive
  localboot 0xffff

 到此在Centos6上實現PXE自動化安裝Centos6就配置完畢了。接下來就可以測試安裝了。

五、測試安裝

【關閉防火牆後開始測試安裝】

 

成功。

在Centos7上實現PXE自動化安裝Centos7

【Centos7上實現PXE自動化安裝Centos7,步驟和上面的centos6大致相同,下面是簡單步驟描述。】

1.安裝相關包,啟動相應服務

yum install httpd dhcp tftp-server syslinux

systemctl enable httpd tftp dhcpd

2.準備yum和應答檔案

mkdir /var/www/html/centos/7 -pv

mount /dev/sr0 /var/www/html/centos/7

準備應答檔案

mkdir /var/www/html/ksdir cp ks_centso7.cfg /var/www/html/ksdir

systemctl restart httpd

3.配置dhcp服務

cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example   /etc/dhcp/dhcpd.conf 修改其中引數 vim /etc/dhcp/dhcpd.conf subnet 192.168.36.0 netmask 255.255.255.0 {         range 192.168.36.10 192.168.36.200;         next-server 192.168.36.7;         filename "pxelinux.0"; }

systemctl restart dhcpd

4.準備啟動相關檔案

cp /usr/share/syslinux/{pxelinux.0 , menu.c32}   /var/lib/tftpboot/ cp /misc/cd/isolinux/{initrd.img,vmlinuz}  /var/lib/tftpboot/ mkdir pxelinux.cfg/ cp /mnt/isolinux/isolinux.cfg  /var/lib/tftpboot/pxelinux.cfg/default vim  /var/lib/tftpboot/pxelinux.cfg/defaultZ

default menu.c32
timeout 60


menu title PXE CentOS 7 Install

label linux
  menu label ^Auto Install Mini CentOS 7
  kernel vmlinuz
  append initrd=initrd.img ks=http://192.168.36.7/ksdir/ks_cent
os7.cfg

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

systemctl start tftp

5.測試,網絡卡啟動

Centos6應答檔案

#centos6應答檔案


# Kickstart file automatically generated by anaconda.

#version=DEVEL
text
reboot
install
url --url=http://192.168.36.6/centos/6
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp --ipv6 auto
rootpw  --iscrypted $6$pWrnXzIOssUEXD0m$DvHzCsm63yh945KMpJw2TZN
WkjVga/Y8iMsI5h0B7itb90cft9grVBHBO2k5ENfxuj29dS9COA3gvra4oFgH50
firewall --disabled
authconfig --enableshadow --passalgo=sha512
selinux --disabled
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="crashkerne
l=auto rhgb quiet"
  kernelnmlinuz
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
zerombr
clearpart --all --initlabel


part /boot --fstype=ext4 --size=1024
part / --fstype=ext4 --size=50000
part /data --fstype=ext4 --size=30000

part swap --size=2048

repo --name="CentOS"  --baseurl=http://192.168.36.6/centos/6 --
cost=100

%packages
@core
@server-policy
@workstation-policy
autofs
vim-enhanced
tree
%end

%post
useradd xie
echo 256521 |passwd --stdin xie &> /dev/null
mkdir /etc/yum.repo.d/bak
mv /etc/yum.repo.d/base.repo <<ENF
[base]
name=base
baseurl=file:///misc/cd
gpgcheck=0
EOF

mkdir /root/.ssh
cat > /root/.ssh/authorized_keys  <<EOF
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAxCW50MEYCIhQvKlThefaNt9GF61QI2WDoE+2v483lpVULyfAAhn9JR0+jFkzWeOeAPIGh23Aq8WRGwJ/kg1MoAdXLLfUQTQdBPLRTxoIAZdiphbNgSahTV56b+zfag22KbMuRDAdfWIC7nRCJS9vMvwiclddDKUepWUPt+lP20Bi06HIoCrmIqsXrXhiyCW1aWAq6/jQOK3SAXQj7lajYQ== [email protected]
EOF
chmod 600 /root/.ssh/authorized_keys
chmod 700 /root/.ssh
%end                       

Centos7應答檔案

#platform=Intel Itanium
#version=DEVEL
# Install OS instead of upgrade
install
# X Window System configuration information
xconfig  --startxonboot
# Keyboard layouts
# old format: keyboard us
# new format:
keyboard --vckeymap=us --xlayouts='us'
# Root password
rootpw --iscrypted $6$pWrnXzIOssUEXD0m$DvHzCsm63yh945KMpJw2TZNW
kjVga/Y8iMsI5h0B7itb90cft9grVBHBO2k5ENfxuj29dS9COA3gvra4oFgH50
# Use network installation
url --url="http://192.168.36.7/centos/7"
# System language
lang en_US
user --name=xie --password=$6$XOdmF.0y/rtl1RSU$uh0W4Hp7UKCb6ofw
jxrCx9d7456d57Peqyo8UO6k9O.WQ94h7nd5PUhf6qW8fbBERQur4Thq32dJey5
FeCv67/ --iscrypted --gecos="xie"
# System authorization information
auth  --useshadow  --passalgo=sha512
<r/www/html/ksdir/ks_centos7.cfg" 58L, 1612C 14,1          Top
#platform=Intel Itanium
#version=DEVEL
# Install OS instead of upgrade
install
# X Window System configuration information
xconfig  --startxonboot
# Keyboard layouts
# old format: keyboard us
# new format:
keyboard --vckeymap=us --xlayouts='us'
# Root password
rootpw --iscrypted $6$pWrnXzIOssUEXD0m$DvHzCsm63yh945KMpJwVga/Y8iMsI5h0b90cft9grVBHBO2k5ENfxuj29dS9COA3gvra4oFgH5
0
# Use network installation
url --url="http://192.168.36.7/centos/7"
# System language
lang en_US
user --name=xie --password=$6$XOdmF.0y/rtl1RSU$uh0W4Hp7UKx9d7456d57Peqyo8UO6k9O.WQ94h7nd5PUhf6qW8fbBERQur4Thq32dJe
y5FeCv67/ --iscrypted --gecos="xie"
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
# Run the Setup Agent on first boot
firstboot --enable
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx

# System services
services --disabled="chronyd"
ignoredisk --only-use=sda
# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=dhcp --device=ens33
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai --nontp
# System bootloader configuration
bootloader --location=mbr --boot-drive=sda
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /data --fstype="xfs" --ondisk=sda --size=30720
part / --fstype="xfs" --ondisk=sda --size=51200
part /boot --fstype="xfs" --ondisk=sda --size=1024
part swap --fstype="swap" --ondisk=sda --size=2048

%post
useradd xyc
%end

%packages
@core
%end