Centos7搭建PXE挂载启动系统

制作人:全心全意

Centos7搭建PXE挂载启动系统

系统服务器:通过DHCP+TFTP+NFS服务向无盘工作站提供系统支持

 DHCP服务:

 向PXE客户端分发IP地址、子网掩码、网关等,并指定启动引导文件所在服务器(TFTP服务器)的地址和PXE启动文件(pxelinux.0)

TFTP服务:

 向PXE客户端传输PXE启动文件、PXE引导配置文件、linux内核vmlinuz,以及系统启动文件initrd.img

NFS服务:

 向PXE客户端发布工作站的系统(整个根目录“/”的克隆);为了避免磁盘IO资源的冲突,建议将克隆的系统部署在存储服务器上

PXE客户端:PXE客户端无需硬盘,但需要一块支持PXE启动的网卡,不过其他硬件比如主板、内存条、电源等,还是必须要的;将“网卡启动”设置为首选

 

PXE启动流程图:

centos pe盘 centos pe盘启动_客户端

 

 

搭建pxe远程服务

 

客户端:


准备系统模板

mkdir /nodiskos                               #系统模板+启动文件存放目录
mkdir /nodiskos/tftpboot                      # 工作站系统启动文件存放目录
mkdir /nodiskos/workstation                 # 工作站系统模板存放目录

  

rsync -avz --exclude='/client' --exclude='/proc' --exclude='/sys' --exclude='/tmp' --exclude='/var/tmp' --exclude='/media' --exclude='/mnt' --exclude='/lost+found' --exclude='/etc/mtab' --exclude='/run' --exclude='/opt' --exclude='/dev' --exclude='/opt' --exclude='/nodiskos'  /* /nodiskos/workstation

#创建未拷贝的目录
cd /nodiskos/workstation
mkdir -p proc sys tmp var/tmp media mnt opt run dev

  

配置客户端挂载(删除swap相关的挂载)

vi /nodiskos/workstation/etc/fstab
172.16.1.86:/nodiskos/workstation /  nfs    defaults      0 0

  

 删除所有ifcfg-eth*网卡配置文件,只需保留ifcfg-lo

rm -f /nodiskos/workstation/etc/sysconfig/network-scripts/ifcfg-eth*
#请按照网卡类型(eth、ens等)删除

  

将整个工作站系统模板打包

cd /nodiskos
tar -cvf workstation.tar workstation

  

准备引导所需文件

安装syslinux 和dracut 软件包

yum -y install syslinux dracut dracut-network

  

拷贝PXE启动文件(由syslinux 程序提供)

cp /usr/share/syslinux/pxelinux.0 /nodiskos/tftpboot

  

拷贝用linux内核文件vmlinuz

cp /boot/vmlinuz-3.10.0-514.el7.x86_64 /nodiskos/tftpboot/

  

创建用于系统启动 镜像文件initrd.img(先执行命令 uname -r 查看内核版本,如:2.6.32-431.el6.x86_64)

#其它的教程都没有加--add,反正我默认nfs是打包打不进去的
dracut --add nfs initrd-2.6.32-431.el6.x86_64.img 2.6.32-431.el6.x86_64
chmod 644 initrd-2.6.32-431.el6.x86_64.img
mv initrd-2.6.32-431.el6.x86_64.img /nodiskos/tftpboot

  

在/nodiskos/tftpboot/pxelinux.cfg/目录下创建默认的PXE引导配置文件"default"

mkdir -p /nodiskos/tftpboot/pxelinux.cfg/
vi /nodiskos/tftpboot/pxelinux.cfg/default
default auto
label auto
prompt 0
kernel vmlinuz-3.10.0-514.el7.x86_64
append initrd=initrd-3.10.0-514.el7.x86_64.img root=nfs:172.16.1.86:/nodiskos/workstation selinux=0 ip=dhcp rw

  

将引导文件打包

cd /nodiskos
tar -cvf tftpboot.tar tftpboot

  

将两个打好的引导文件和系统模板上传到pxe服务器

 

服务器:


准备工作目录

mkdir /nodiskos
cd /nodiskos
tar xf tftpboot.tar
tar xf workstation.tar

  

安装和配置DHCP

yum -y install dhcp
vi /etc/dhcp/dhcpd.conf
# dhcpd.conf 部分参数说明:
# default-lease-time      // 指定确认租赁时长,单位为秒,-1 表示无限制
# max-lease-time          // 指定最大租赁时长
# authritative            // 拒绝不正确的IP 地址的要求
# subnet netmask {}       // 设置dhcp 区域
# range                   // 提供动态分配IP 的范围;若所有工作站都是绑定的固定IP,可删除此配置
# option routers          // 设置网关/路由器地址,多个地址用逗号隔开;若不想让客户端上网,可删除此配置
# domain-name-servers     // 设置DNS,若不想让客户端上网,可删除此配置;多个地址用逗号隔开

# next-server             // 告知工作站TFTP 服务器的地址,TFTP 服务提供启动引导
# filename                // 告知工作站PXE 引导程序名
# host XXX {}             // 此处是根据工作站的MAC 地址绑定固定的IP 地址,前提是知道MAC 地址
# hardware ethernet       // 工作站的MAC 地址,一定要小写
# fixed-address           // 绑定固定的IP 地址,和range 的不会有冲突,优先以它为主

ddns-update-style none;
ignore client-updates;
default-lease-time -1;
max-lease-time -1;
authritative;

subnet 172.16.1.0 netmask 255.255.255.0 {
	range 172.16.1.90 172.16.1.95;
	next-server 172.16.1.86;               
	filename "pxelinux.0";
}

#将DHCP服务器绑定在固定网卡上
DHCPDARGS="ens32"

systemctl restart dhcpd

#关闭防火墙
systemctl stop firewalld

#关闭selinux
vi /etc/selinux/config
SELINUX=disabled

  

安装和配置TFTP

yum install -y tftp-server tftp

vi /etc/xinetd.d/tftp	#centos7中不生效
# and to start the installation process for some operating systems.
service tftp
{
  ...... ........
  server_args = -s /nodiskos/tftpboot             # 改成启动文件的存放目录
  Disable = no                                    # 将yes 改成no,以激活此服务
  ...... ........
}

#===================================
vi /usr/lib/systemd/system/tftp.service #centos7中修改这里

systemctl daemon-reload 
#=======================================

systemctl restart tftp

  

安装和配置NFS

yum install -y nfs-utils rpcbind

vim /etc/exports
/nodiskos/workstation	172.16.1.0/24(rw,async,no_root_squash,insecure)

exportfs -rv #更新nfs

systemctl restart rpcbind
systemctl restart nfs

  

设置开机自启

systemctl enable dhcpd
systemctl enable tftp
systemctl enable nfs
systemctl enable rpcbind
systemctl disable firewalld

  

搭建完成

 

 

如果想更换服务器(ip地址更换,需要更改的文件)

/nodiskos/tftpboot/pxelinux.cfg/default
/etc/dhcp/dhcpd.conf
/etc/exports
/nodiskos/workstation/etc/fstab