PXE自动化安装centos6和centos7

3 篇文章 0 订阅
1 篇文章 0 订阅

PXE自动化安装centos6和centos7



PXE介绍

PXE(Preboot Excution Environment): 预启动执行环境,Intel公司研发,基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统。
PXE可以引导和安装Windows,linux等多种操作系统
PXE工作原理:

  1. 客户端向PXE服务器上的dhcp服务发送IP地址请求消息,dhcp服务检测客户端请求是否合法,主要是检测客户机的网卡MAC地址,如果合法则分配给客户端IP地址,同时将启动文件名pxelinux.0和启动文件位置信息一并传送给客户端。
  2. 客户端向PXE 服务器上的tftp服务发送获取启动文件pxelinux.0请求消息,tftp服务在接收到消息之后向客户端发送pxelinux.0大小信息,询问客户端是否同意使用,当tftp服务收到客户端发回的同意使用信息之后,正式向客户端发送pxelinux.0文件。
  3. 客户端执行接收到的pxelinux.0文件。
  4. 客户端向tftp服务器发送针对本机的配置信息文件(在tftp 服务器的pxelinux.cfg目录下),tftp服务器将配置文件发回客户端,继而客户端根据配置文件执行后续操作。
  5. 客户端向tftp服务器发送Linux内核请求信息,tftp服务器在接收到消息之后将内核文件发送给客户端
  6. 客户端向tftp服务器发送根文件请求信息,tftp服务器接收到消息之后返回Linux根文件系统给客户端。
  7. 客户端启动Linux内核
  8. 客户端下载安装源文件,读取自动化安装脚本

准备工作

准备一台主机作为网络环境内的dhcp服务器、tftp服务器、http服务器,在此主机上配置PXE环境,为网络中的其它主机提供自动化系统安装服务。

关闭防火墙
root&Centos7: ~#systemctl disable firewalld.service
#禁止防火墙开机自启动
root&Centos7: ~#systemctl stop firewalld.service
#关闭防火墙
root&Centos7: ~#iptables -vnL
关闭SELINUX
root&Centos7: ~#sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
#禁用SElinux策略
root&Centos7: ~# grep SELINUX=disabled /etc/selinux/config
#确定SELINUX=disabled,已经将SElinux策略禁用
root&Centos7: ~#setenforce 0
#设置SELinux当前状态为Permissive
root&Centos7: ~# getenforce
#获取SELinux当前状态
将做DHCP服务器的主机设置为静态IP
root&Centos7: ~# nmcli connection show
#查看网卡名
NAME                UUID                                  TYPE            DEVICE
ens32               af3fea87-cb81-4a24-8ae4-afb54afa551c  802-3-ethernet  ens32
root&Centos7: ~#nmcli connection modify "ens32" ipv4.addresses 192.168.10.43/24 ipv4.gateway 192.168.10.1
#设置ens32网卡的IP地址为静态地址

安装相关软件包

root&Centos7: ~#yum install -y dhcp tftp-server httpd syslinux
#一次性安装完成dhcp tftp-server httpd syslinux四个软件包

配置文件共享服务和yum源

启动httpd服务
root&Centos7: ~#systemctl enable httpd.service
root&Centos7: ~#systemctl start httpd.service
root&Centos7: ~#ss -ntl |grep 80
制作centos7和centos6的yum源
root&Centos7: ~#mkdir -pv  /var/www/html/centos/{6,7}
root&Centos7: ~#mount /dev/sr0 /var/www/html/centos/6/
root&Centos7: ~#mount /dev/sr1 /var/www/html/centos/7/
root&Centos7: ~#ls /var/www/html/centos/{6,7}

制作centos6和centos7的kickstart文件

生成kickstart文件的两种方法
修改anaconda.cfg文件,此文件会在每次装完系统后生成于/root/目录下.
使用system-config-kickstart命令生成.
复制应答文件到共享目录下的/ksdir/目录下
root&Centos7: ~#mkdir /var/www/html/ksdir/
root&Centos7: ~#cp -p /root/anaconda7-ks.cfg /var/www/html/ksdir/ks7-1.cfg
root&Centos7: ~#cp -p /root/anaconda6-ks.cfg /var/www/html/ksdir/ks6-1.cfg
root&Centos7: ~#ls /var/www/html/ksdir/
root&Centos7: ~#chmod +r /var/www/html/ksdir/*.cfg
#为应答文件增加读权限,此步骤很重要,请确保执行。
编辑centos7的应答文件
root&Centos7: ~#vim /var/www/html/ksdir/ks7-1.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
#cdrom
url --url=http://192.168.10.43/centos/7 #指定安装使用的yum源路径
# Use graphical install
#graphical
text    #使用text文本安装方式
# 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=ens32 --onboot=on --ipv6=auto --activate
network  --hostname=localhost.localdomain

# Root password         #设置root密码
rootpw --iscrypted $6$KvKpJji3uieQkGBS$5s1fuvxc0WcOW77438w.bLZJwTxV8afFC.NL6X0zCfRj8pfrldm37lKgR5iSsY.z9pNd7Q9rxvqfIU0O1CUnX.
# System services
services --enabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --ntpservers=0.centos.pool.ntp.org,1.centos.pool.ntp.org,2.centos.pool.ntp.org,3.centos.pool.ntp.org
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
# Partition clearing information
#clearpart --none --initlabel
zerombr     #安装时自动清空MBR
clearpart --all     #清空全部分区
reboot  #安装完成后自动重启
# Disk partitioning information #分区信息
part swap --fstype="swap" --ondisk=sda --size=2048
part /app --fstype="xfs" --ondisk=sda --size=51200
part / --fstype="xfs" --ondisk=sda --size=51200
part /boot --fstype="xfs" --ondisk=sda --size=1024

%packages   #要安装的软件包
@^minimal
@core
chrony
kexec-tools
autofs
%end

%addon com_redhat_kdump --enable --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

%post   #安装完成后要执行的脚本
systemctl enable autofs
rm -rf /etc/yum.repos.d/*
cat > /etc/yum.repos.d/base.repo <<eof
[base]
name=base
baseurl=file:///misc/cd
gpgcheck=0
eof
%end
编辑centos6的应答文件
root&Centos7: ~#vim /var/www/html/ksdir/ks6-1.cfg
# Kickstart file automatically generated by anaconda.

#version=DEVEL
install
#cdrom
url --url=http://192.168.10.43/centos/6
text
reboot
zerombr
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto dhcp --noipv6
rootpw  --iscrypted $6$fo5PVsYpQzE2RC..$vZ2FT3sHNJBR2aopg8uzWWM2.59BykYcelOD7rBryYUuYpeNKpqAneREqaaO4x3btGdBJGbc4vHvjwaGLBGVG1
firewall --service=ssh
authconfig --enableshadow --passalgo=sha512
selinux --enforcing
timezone --utc 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
#clearpart --none
clearpart --all
part /boot --fstype=ext4 --size=1000
part / --fstype=ext4 --size=50000
part /app --fstype=ext4 --size=50000
part swap --size=2000

#repo --name="CentOS"  --baseurl=cdrom:sr0 --cost=100

%packages
@core
@server-policy
@workstation-policy
%end
检查应答文件格式
root&Centos7: ~#yum install system-config-kickstart -y
root&Centos7: ~#ksvalidator /var/www/html/ksdir/ks7-1.cfg
root&Centos7: ~#ksvalidator /var/www/html/ksdir/ks6-1.cfg

配置tftp服务

root&Centos7: ~#systemctl enable tftp.socket
root&Centos7: ~#systemctl start tftp.socket
root&Centos7: ~#ss -nul|grep 69

配置DHCP服务

默认dhcp配置文件内没有配置信息,可以使用配置示例文件将配置文件覆盖后进行修改,dhcp服务必须在更改配置文件后才能启动。dhcp配置文件必须要dhcp服务器所在网段的地址池。

root&Centos7: ~#cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
#制作配置文件
root&Centos7: ~#vim /etc/dhcp/dhcpd.conf
#编辑配置文件
# DHCP server to understand the network topology.
subnet 192.168.10.0 netmask 255.255.255.0 {  #设置地址段
    range 192.168.10.50 192.168.10.100;     #设置地址池
    option routers 192.168.10.1;    #设置网关
    next-server 192.168.10.43;      #提供引导文件的服务器ip ,用于网络安装操作系统时
    filename "pxelinux.0";    #指明引导文件名称
}
root&Centos7: ~#systemctl enable dhcpd
#将dhcp服务设置为开机自启动
root&Centos7: ~#systemctl start dhcpd
#启动dhcp服务
root&Centos7: ~#ss -nul|grep 67
#检查确认dhcp服务已经启动

制作PXE相关文件

root&Centos7: ~#mkdir /var/lib/tftpboot/pxelinux.cfg/
root&Centos7: ~#mkdir /var/lib/tftpboot/centos{6,7}
root&Centos7: ~#cp /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
root&Centos7: ~#cp /var/www/html/centos/7/isolinux/{initrd.img,vmlinuz}  /var/lib/tftpboot/centos7
root&Centos7: ~#cp /var/www/html/centos/6/isolinux/{initrd.img,vmlinuz}  /var/lib/tftpboot/centos6
root&Centos7: ~#cp /misc/cd/isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
root&Centos7: ~#tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── centos6
│   ├── initrd.img
│   └── vmlinuz
├── centos7
│   ├── initrd.img    #伪文件系统文件
│   └── vmlinuz     #内核文件
├── menu.c32    #纯文本菜单  
├── pxelinux.0  #引导文件,相当于grub。
└── pxelinux.cfg
    └── default       #启动菜单文件

制作启动菜单

root&Centos7: ~#vim /var/lib/tftpboot/pxelinux.cfg/default
default menu.c32
timeout 600
menu title CentOS Linux  PXE Install

label centos7     #自动安装centos7
      menu label Auto Install CentOS Linux ^7
      kernel centos7/vmlinuz
      append initrd=centos7/initrd.img ks=http://192.168.10.43/ksdir/ks7-1.cfg  #应答文件所在路径

label centos6      #自动安装centos6
      menu label Auto Install CentOS Linux ^6
      kernel centos6/vmlinuz
      append initrd=centos6/initrd.img ks=http://192.168.10.43/ksdir/ks6-1.cfg

label manual7     #手动安装centos7
      menu label ^Manual Install CentOS Linux 7
      kernel centos7/vmlinuz
      append initrd=centos7/initrd.img inst.repo=http://192.168.10.43/centos/7

label manual6      #手动安装centos6
      menu label Manual ^Install CentOS Linux 6
      kernel centos6/vmlinuz
      append initrd=centos6/initrd.img inst.repo=http://192.168.10.43/centos/6

label local     #本地硬盘启动
     menu default    #默认启动项
     menu label Boot from ^local drive
     localboot 0xffff
    menu end

系统安装

使用网络引导启动,下图是启动后的安装界面:

这里写图片描述

注意:使用虚拟机安装时,centos7内存要大于1024M,否则无法成功安装。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值