PXE自动批量安装Centos7系统
这是一篇能正确安装并能自动部署系统的PXE文章
PXE批量部署原理:
- PXE Client 从自己的PXE网卡启动,向本网络中的DHCP服务器请求IP;
- DHCP 服务器返回分配给客户机的IP 以及PXE文件的放置位置
(该文件一般是放在一台TFTP服务器上); - PXE Client 向本网络中的TFTP服务器索取pxelinux.0 文件
(在 PXE Client 的 ROM 中,已经存在 TFTP Client); - PXE Client 取得pxelinux.0 文件后之执行该文件;
- 根据pxelinux.0 的执行结果,通过TFTP服务器加载内核和文件系统 ;
- 进入安装画面, 此时可以通过选择HTTP、FTP、NFS 方式之一进行安装;
注:
1、DHCP服务器:为 PXE-client 分配 IP ,获得安装程序文件位置
2、TFTP服务器:传输安装文件、内核、菜单文件等给 PXE-client
3、Xinetd: TFTP服务超级守护进程, 用于唤醒TFTP服务;
4、Kickstart:生成的ks.cfg配置文件
5、HTTP/NFS/FTP 服务中的任意一种,用于传送安装源文件给 PXE-client
关闭并禁用防火墙
# systemctl disable firewalld.service
# systemctl stop firewalld.service
临时禁用Selinux
setenforce 0
安装DHCP服务,并配置
例:将模板文件重定向到配置中
[root@localhost ~]# yum -y install dhcp
[root@localhost ~]# vim /etc/dhcp/dhcpd.conf
default-lease-time 600; #租用时间
max-lease-time 7200; #最长租用时间
subnet 192.168.100.0 netmask 255.255.255.0 { #网络网段
range 192.168.100.100 192.168.100.200; #地址池范围
option routers 192.168.100.10; #网关地址
filename "pxelinux.0"; #pxe引导文件
next-server 192.168.100.10; #TFTP服务器地址(这里使用一台机器)
}
[root@localhost ~]# systemctl restart dhcpd #重启服务
安装tftp服务
[root@localhost ~]# yum -y install tftp-server
[root@localhost ~]# vim /etc/xinetd.d/tftp #TFTP需要xinetd服务的支持
# default: off
# description: The tftp server serves files using the trivial file transfer \
# protocol. The tftp protocol is often used to boot diskless \
# workstations, download configuration files to network-aware printers, \
# and to start the installation process for some operating systems.
service tftp
{
socket_type = dgram
protocol = udp
wait = no #是否等待写入改为no
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot
disable = no #是否关闭服务 改为no 表示开启
per_source = 11
cps = 100 2
flags = IPv4
}
[root@localhost ~]# systemctl restart tftp #重启服务
[root@localhost ~]# systemctl enable tftp #开机自启
安装HTTP服务
[root@localhost ~]# yum install httpd -y
[root@localhost ~]# mkdir /var/www/html/centos #在网页目录下创建centos目录,用来挂载系统
[root@localhost ~]# mkdir /var/www/html/ks #创建ks目录用来存放无人值守文件
[root@localhost ~]# mount /dev/sr0 /var/www/html/centos/ #挂载系统
mount: /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# cp /root/anaconda-ks.cfg /var/www/html/ks/ks.cfg #复制无人值守文件
[root@localhost ~]# ls /var/www/html/ks/
ks.cfg
[root@localhost ~]# vim /var/www/html/ks/ks.cfg #修改无人值守文件
#version=RHEL7
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
#cdrom #注释掉,因为不使用光盘安装
url --url="http://192.168.100.10/centos" #指定web服务器系统镜像位置
# 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
[root@localhost tftpboot]# chmod 777 /var/www/html/ks/ks.cfg #修改无人值守文件权限为777
[root@localhost tftpboot]# ll /var/www/html/ks/ks.cfg
-rwxrwxrwx. 1 root root 954 Aug 18 09:43 /var/www/html/ks/ks.cfg
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# systemctl enable httpd
安装syslinux,并复制所需文件
[root@localhost ~]# cd /var/lib/tftpboot/ #共享目录位置
[root@localhost tftpboot]# cp /var/www/html/centos/isolinux/vmlinuz ./ #复制内核文件到当下
[root@localhost tftpboot]# cp /var/www/html/centos/isolinux/initrd.img ./ #复制初始化镜像文件
[root@localhost tftpboot]# cp /usr/share/syslinux/pxelinux.0 ./ #引导pxe文件
[root@localhost tftpboot]# mkdir pxelinux.cfg #pxe引导目录
[root@localhost tftpboot]# cp /var/www/html/centos/isolinux/isolinux.cfg ./pxelinux.cfg/default #pxe最后引导文件
[root@localhost tftpboot]# vim ./pxelinux.cfg/default #修改文件default
default linux #改为linux
prompt 0 #取消交互
timeout 600
label linux
menu label ^Install CentOS 7
kernel vmlinuz
append initrd=initrd.img ks=http://192.168.100.10/ks/ks.cfg #系统位置与无人值守文件位置
[root@localhost tftpboot]# ll ./pxelinux.cfg/default
-rw-r--r--. 1 root root 3055 Aug 18 13:19 ./pxelinux.cfg/default #default文件权限必须为644
新建虚拟机,开始安装
新建的虚拟机内存不能太小,不然会报错