89421950 2017-10-02
本博主要目的为实现自定义页面自动化安装系统,基于PXE支持的网卡,目前实现环境为VMware虚拟机。所用镜像为CentOS6.9,CentOS7.3及rhel5.4。支持中小规模自动化系统统一安装。
另:如果最近常见到pxe相关博文,多半为本人的同期,还请不要在意内容的重复性。不同人眼中的pxe自动化安装,如此理解吧。
环境准备
1.网络环境:独立网络环境配备
实验中需要配置dhcp服务器,所以多余的网络会和本地dhcp服务器造成冲突。
本人的虚拟机里仅选用了一个独立网卡,同时主机配置静态IP。

2.外部环境:这里主要指防火墙和SElinux,二者会对实验造成影响,所以这里,关掉会省去很多麻烦。
3.安装必要软件包:这里syslinux包里的几个文件是必须的,dhcpd包提供dhcp服务器环境配置,tftp-server是轻文本传输用到的。httpd和ftpd选装一个就OK,主要是我们从网站或ftp服务器上获取数据使用。
yum install httpd dhcp tftp-server syslinux --skip-broken
原理及过程
从网络获取镜像文件,并独立配置安装菜单选项,网络引导。
当某机使用完成后的环境安装系统时,首先网卡启动,然后向本机,即dhcp服务器申请IP。IP获得后,tftp负责文本传输,得到网站上的pxe启动文档等信息,并根据引导文件安装系统。
这里为了简便,dhcp服务器及http网站等均由本机担任。
实现步骤
1.dhcpd文件配置
[root@CentOS7 ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf 
#上面的第一个文件是模板,第二个是配置文件 
[root@CentOS7 ~]# vim /etc/dhcp/dhcpd.conf 
#查照此文件中的模板添加下信息,此段信息最好放在原文件中第一个subnet上方。 
subnet 192.168.23.0 netmask 255.255.255.0{ 
  range 192.168.23.10 192.168.234.100; 
  option routes 192.168.23.7; 
  next-server 192.168.23.7;        #此处为本机IP 
  filename "pxelinux.0";        #pxe启动文档 
}
2.开启服务
systemctl enable dhcpd httpd tftp 将服务设为开机启动
systemctl start dhcpd httpd tftp 启动服务
systemctl status dhcpd httpd tftp 查看服务状态
3.挂载镜像文件到http
确保三个镜像都已连接,然后按顺序挂好。不要弄错了。
cd /var/www/html/
    mkdir centos{6,7}
    mkdir rhel5
    echo '- - -' > /sys/class/scsi_host/host2/scan 
    mount /dev/sr0 /var/www/html/centos7
    mount /dev/sr1 /var/www/html/centos6
    mount /dev/sr2 /var/www/html/rhel5
    也可以把挂载写入/etc/fstab文件。
4.编辑安装引导文件ks.cfg
可以简单编辑root家目录下的anaconda-ks.cfg,然后把三个系统的引导文件复制到本机的/var/html/www/下。并注意此文件权限为644。
centos7的.cfg文件参考如下:
[root@centos7 ~]# cat /var/www/html/ks7.cfg  
#version=DEVEL 
# System authorization information 
auth --enableshadow --passalgo=sha512 
# Use CDROM installation media 
url --url=http://192.168.23.7/centos7
# Use graphical install 
#graphical 
text 
reboot 
# 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 --activate 
network --hostname=centos7.magedu.com 
  
# Root password 
rootpw --iscrypted $6$/BVcMpadhqgy0l4J$FQ1fqYddKm1zqjxEkXn3XctFXiJ2LLWnVfRPi1u1oF6Wx5ebfKwe.8W0wHfF7oxeqDLJbkJJYTnry7W9o/6KP/
# System services 
services --disabled="chronyd"
  
# System timezone 
timezone Asia/Shanghai --isUtc --nontp 
# X Window System configuration information 
xconfig  --startxonboot 
# System bootloader configuration 
zerombr 
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda 
# Partition clearing information 
clearpart --all --initlabel --drives=sda 
# Disk partitioning information 
part / --fstype="xfs" --ondisk=sda --size=47683 
part /boot --fstype="xfs" --ondisk=sda --size=953 
part swap --fstype="swap" --ondisk=sda --size=3814 
part /app --fstype="xfs" --ondisk=sda --size=47683 
  
%packages 
@base 
@core 
@dial-up 
@fonts 
@guest-agents 
@network-file-system-client 
@networkmanager-submodules 
@x11 
  
%end 
%addon com_RedHat_kdump --disable --reserve-mb='auto'
%end 
%anaconda 
pwpolicy root --minlen=6 --minquality=50 --notstrict --nochanges --notempty 
pwpolicy user --minlen=6 --minquality=50 --notstrict --nochanges --notempty 
pwpolicy luks --minlen=6 --minquality=50 --notstrict --nochanges --notempty 
%end
centos6的.cfg参考如下:
[root@centos7 html]# cat /var/www/html/ksdir/ks6.cfg  
# Kickstart file automatically generated by anaconda. 
  
#version=DEVEL 
install
url --url=http://httpsrv/centos/6
lang en_US.UTF-8 
keyboard us 
network --onboot yes --device eth0 --bootproto dhcp --noipv6 
rootpw  --iscrypted $6$CxpwSUg0oCIQZX52$0yfD8CXU4Q.60uYDqSHWz5.1enxwnhrv9esPRYSix4U1cWoaN.hOpgSLqCd22yjRdEhAwwUdxAIbn.mxMn.kx/ 
firewall --disable 
authconfig --enableshadow --passalgo=sha512 
selinux --disabled 
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
# 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 
reboot 
text 
part /boot --fstype=ext4 --size=1000 
part / --fstype=ext4 --size=100000 
part /app --fstype=ext4 --size=50000 
part swap --size=2048 
  
%packages 
@base 
@core 
@basic-desktop 
@desktop-platform 
@fonts 
@general-desktop 
@graphical-admin-tools 
@input-methods 
@internet-applications 
@internet-browser 
@network-file-system-client 
@office-suite 
@print-client 
@remote-desktop-clients 
@server-policy 
@workstation-policy 
@x11 
mtools
pax 
%end
rhel5的.cfg文件如下:
# Kickstart file automatically generated by anaconda. 
  
install
url --url=http://192.168.23.7/rhel5
key --skip 
lang zh_CN.UTF-8 
keyboard us 
xconfig --startxonboot 
network --device eth0 --bootproto dhcp --hostname rhel5.4.centos.com 
rootpw --iscrypted $1$YntMaKAR$vXCZ6J8hGwcRtfso7lk9o. 
firewall --enabled --port=22:tcp 
authconfig --enableshadow --enablemd5 
selinux --disabled 
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=sda --append="rhgb quiet"
# 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 
#clearpart --linux 
zerombr 
clearpart --all 
reboot 
  
text 
part /boot --fstype ext3 --size=100 
part / --fstype ext3 --size=51200 
part /app --fstype ext3 --size=10240 
part swap --size=1024 
  
%packages 
@admin-tools 
@base 
@core 
@dialup 
@editors 
@graphical-internet 
@graphics 
@legacy-software-support 
@office 
@printing 
@text-internet 
@base-x 
kexec-tools 
fipscheck 
sgpio 
emacs 
libsane-hpaio 
xorg-x11-utils 
xorg-x11-server-Xnest
5.准备相关文件,必备文件如下:
pxelinux.0(pex引导文件),menu.c32(图形菜单),vmlinuz内核文件,initrd.img初始根文件系统
配置文件:isolinux.cfg改名为pxelinux.cfg/default
    cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
    mkdir /var/lib/tftpboot/centos{6,7}
    mkdir /var/lib/tftpboot/rhel5
     cp /var/www/html/rhel5/isolinux/{initrd.img,vmlinuz}  /var/lib/tftpboot/rhel5
    cp /var/www/html/centos6/isolinux/{initrd.img,vmlinuz}  /var/lib/tftpboot/centos6
    cp /var/www/html/centos7/isolinux/{initrd.img,vmlinuz}  /var/lib/tftpboot/centos7
    mkdir /var/lib/tftpboot/pxelinux.cfg/
    cp /var/www/html/centos/7/isolinux/isolinux.cfg  /var/lib/tftpboot/pxelinux.cfg/default
此环节准备的文件均在/var/lib/tftpboot下,以便通过tftp获取文件。
[root@centos7 tftpboot]# tree /var/lib/tftpboot 
. 
|-- centos6 
|   |-- initrd.img 
|   `-- vmlinuz 
|-- centos7 
|   |-- initrd.img 
|   `-- vmlinuz 
|-- menu.c32 
|-- pxelinux.0 
|-- pxelinux.cfg 
|   `-- default 
`-- rhel5 
    |-- initrd.img 
    `-- vmlinuz 
  
4 directories, 9 files
6.编辑自动化安装页面菜单
即编辑/var/lib/tftpboot/pxelinux.cfg/default文件。其基本信息如下:
[root@centos7 ~]# vim /var/lib/tftpboot/pxelinux.cfg/default 
default menu.c32 
timeout 600 
  
menu title CentOS Linux 7 
label centos7 
  menu label ^Auto Install CentOS Linux 7 
  kernel centos7/vmlinuz
  append initrd=centos7/initrd.img ks=http://httpsrv/ksdir/ks7.cfg 
label centos6 
  menu label Auto install CentOS Linux ^6 
  kernel centos6/vmlinuz
  append initrd=centos6/initrd.img  ks=http://httpsrv/ksdir/ks6.cfg 
label manualcentos6 
  menu label ^Manual install CentOS Linux 6 
  kernel centos6/vmlinuz
  append initrd=centos6/initrd.img  inst.repo=http://httpsrv/centos/6/
label local
  menu default 
  menu label Boot from ^local drive 
  localboot 0xffff 
menu end
这样制作过程便OK了。期间可能出现小的错误导致服务不能正常重启。还要就事论事,一项项排除。主要步骤就是这些了。
安装测试
设置网卡与服务器(即上面配置的主机)为同一网段,即仅能连接到服务器端,不能连接到其他网络环境。
如果是没有装过操作系统的新机开机就会自动装载系统,不过,需要你手动选择要安装的系统哦。默认可是按本地磁盘启动的。
安装过操作系统���若想覆盖安装,则要在bios里调整启动顺序,将网络引导调至最上面。
本人测试后,centos6与rhel5都能成功安装,centos7还有些小bug。麻烦知道问题所在的亲们略微指点了。或者本人找出解决办法后会再来更新的。
对了,rhel5的中文包没装,图形化可能不舒服,需要的暂自行解决下。十一快乐哦。命令行没事的哦。