PXE实现系统自动化安装

项目实战:PXE

PXE 介绍

对于运维人员来说,如何安装操作系统想必并不陌生。当我们需要大批量安装系统时,就需要自动化安装系统了,PXE技术可以实现这一需求。

PXE(Pre-boot Execution Environment,预启动执行环境):

  • 由 Intel 公司开发
  • C/S(Client/Server)网络模式
  • PXE通过网络引导和安装Windows,linux等多种操作系统。

工作原理

在这里插入图片描述

  1. Client向PXE Server上的DHCP发送IP地址请求消息,DHCP检测Client是否合法(主要是检测Client的网卡MAC地址),如果合法则返回Client的 IP地址,同时将TFTP服务器IP和启动文件pxelinux.0的位置信息一并传送给Client。
  2. Client向PXE Server上的TFTP发送获取pxelinux.0请求消息,TFTP接收到消息之后再向Client发送pxelinux.0大小信息,试探Client是否满意。当TFTP收到Client发回的同意大小信息之后,正式向Client发送pxelinux.0。Client执行接收到的pxelinux.0文件。
  3. Client向TFTP Server发送针对本机的配置信息文件(在TFTP 服务的 pxelinux.cfg目录下),TFTP将配置文件发回Client,继而Client根据配置文件执行后续操作。
  4. Client向TFTP发送Linux内核请求信息,TFTP接收到消息之后将内核文件发送给Client。
  5. Client向TFTP发送initrd.img文件请求信息,TFTP接收到消息之后返回initrd.img。
  6. Client启动Linux内核,加载initrd.img,读取系统启动参数,进而向文件服务器请求系统安装应答文件。文件服务器返回系统安装应答文件。
  7. Client继续向文件服务器请求安装源文件,文件服务器返回安装源文件,开始安装。

简化流程如下:

在这里插入图片描述

实验环境

实验环境说明:所有的服务都部署在server节点。

部署 DHCP 服务器

部署服务端
# 安装软件包dhcp
[root@server ~]# yum install -y dhcp

# 配置DHCP服务器
[root@server ~]# hostnamectl set-hostname dhcp.shizhan.cloud

[root@dhcp ~ 15:03:34]# /bin/cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf 
[root@dhcp ~ 15:29:29]# vim /etc/dhcp/dhcpd.conf
# A slightly different configuration for an internal subnet.
subnet 10.1.8.0 netmask 255.255.255.0 {  #分配网段
  range 10.1.8.101 10.1.8.200; #分配的IP地址范围
  option domain-name-servers 223.5.5.5,223.6.6.6; #分配的DNS
  option domain-name "shizhan.cloud";
  option routers 10.1.8.2;  #分配的网关地址
  option broadcast-address 10.1.8.255;
  default-lease-time 600;  # 默认IP地址租约期限
  max-lease-time 7200;  # IP地址最大租约期限
  filename "pxelinux.0";   # 指定网卡引导文件名称
  next-server tftp.shizhan.cloud;   # 指定 tftp 服务器
}

# Hosts which require special configuration options can be listed in
# host statements.   If no address is specified, the address will be
# allocated dynamically (if possible), but the host-specific information
# will still come from the host declaration.

host web.shizhan.cloud {
  hardware ethernet 00:0C:29:68:51:38; # web服务器mac地址
  fixed-address 10.1.8.100; # 给web服务器分配固定ip
}

# 启用并启动服务
[root@dhcp ~ 15:23:01]# systemctl enable dhcpd --now

# 配置防火墙,放行67/UDP
[root@dhcp ~]# firewall-cmd --permanent --add-service=dhcp
[root@dhcp ~]# firewall-cmd --reload
客户端测试

如果虚拟机连接的网络是VMnet8,则需要关闭VMnet8提供的DHCP功能。

在这里插入图片描述

新建一个全新的虚拟机,从网络启动。

在这里插入图片描述

部署 TFTP 服务器

TFTP(Trivial File Transfer Protocol,普通文件传输协议),是一个传输文件的简单协议,基于UDP协议而实现,端口号为69,默认共享数据路径:/var/lib/tftpboot。

TFTP协议是为进行小文件传输设计的。因此它不具备通常的FTP的许多功能,它只能从文件服务器上获得或写入文件,不能列出目录,不进行认证,它传输8位数据。

使用场景:

  • 提供不复杂、开销不大的文件传输服务。
  • 提供操作系统网络引导服务。
部署服务端
# 安装软件包
[root@server ~]# yum install -y tftp-server

# 启用并启动服务
[root@server ~]# systemctl enable tftp.service --now

# 设置防火墙
[root@server ~]# firewall-cmd --add-service=tftp
[root@server ~]# firewall-cmd --add-service=tftp --permanent

# 安装syslinux,提供pxelinux.0文件
[root@server ~]# yum install -y syslinux

# 准备引导文件
[root@server ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
部署菜单文件
# 挂载系统镜像到 /dvd/
[root@tftp ~ 15:50:19]# mkdir /dvd
[root@server ~]# vim /etc/fstab
......
# 添加如下记录
/dev/sr0 /dvd iso9660 defaults 0 0

[root@tftp ~ 16:04:36]# mount /dev/sr0 /dvd/
mount: /dev/sr0 is write-protected, mounting read-only

[root@tftp ~ 16:14:14]# df /dev/sr0 
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sr0         4635056 4635056         0 100% /dvd

# 部署引导文件
[root@server ~]# ls /var/www/html/dvd/isolinux/
boot.cat  grub.conf   isolinux.bin  memtest     TRANS.TBL     vmlinuz
boot.msg  initrd.img  isolinux.cfg  splash.png  vesamenu.c32
[root@server ~]# cp /var/www/html/dvd/isolinux/* /var/lib/tftpboot/

引导文件说明:

  • vmlinuz,内核
  • initrd.img,驱动程序
  • vesamenu.c32,图形模块
  • splash.png,背景图片
# 部署网络引导菜单文件
[root@server ~]# mkdir /var/lib/tftpboot/pxelinux.cfg
[root@server ~]# cp /var/lib/tftpboot/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default

# 修改网络引导菜单文件,大部分参数保持默认
[root@server ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
default vesamenu.c32 # 默认加载运行图形模块
timeout 30 # 设置为3秒,读秒的时间,单位1/10秒

display boot.msg

# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png # 背景图片

# 修改标题内容如下
menu title PXE Server For Rocky Linux 8 

......

label linux
  menu label PXE ^Install Rocky Linux 8
  menu default # 添加此条目,设置该label为默认启动条目
  kernel vmlinuz
  # append参数,ks指向应答文件位置
  append initrd=initrd.img inst.ks=http://10.1.8.10/ks.cfg

label check
  menu label Test this ^media & install Rocky Linux 8
  # menu default # 注释该条目,取消作为默认启动条目
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=Rocky-8-10-x86_64-dvd nomodeset quiet
......
客户端测试

在这里插入图片描述

部署文件服务器

文件服务器可以使用以下服务实现:

  • httpd
  • vsftpd
  • nfs

我们这里使用 httpd 实现。

部署服务端
# 安装软件包
[root@server ~]# yum install -y httpd

# 准备测试主页
[root@server ~]# echo Hello World > /var/www/html/index.html

# 启用并启动服务
[root@server ~]# systemctl enable httpd.service --now

# 设置防火墙
[root@server ~]# firewall-cmd --add-service=http
[root@server ~]# firewall-cmd --add-service=http --permanent
客户端测试
  • 测试web服务器,访问http://10.1.8.100/。
  • 测试安装镜像源,访问服务器http://10.1.8.23/dvd/

在这里插入图片描述

部署应答文件

示例:自定义一个web服务器应答文件。

[root@server ~]# cat /var/www/html/ks.cfg
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$yXvOeQqO$dk8Ms2JsWaatjNSSzJcdI.
# System language
lang en_US
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
firstboot --disable
# SELinux configuration
selinux --disabled

# Firewall configuration
firewall --enabled --http --service=http,https
# Network information
network --bootproto=static --device=ens160 --ip=10.1.8.100  --netmask=255.255.255.0 --gateway=10.1.8.2 --nameserver=223.5.5.5
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url="http://10.1.8.10/dvd"
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --size=1024
part swap --fstype="swap" --size=2048
part / --fstype="xfs" --grow --size=1

%post --interpreter=/bin/bash
yum install -y httpd
systemctl enable httpd
echo Hello World > /var/www/html/index.html
echo "
This is a web server.
Be careful." >> /etc/issue
echo webapp.shizhan.cloud > /etc/hostname
%end

%packages
@^minimal-environment
%end

图形化安装应答文件:

lang en_US

keyboard us

timezone Asia/Shanghai

rootpw $1$9thMdh$MgRkBPqQuEz6QeFaQDX6p0 --iscrypted

url --url=http://10.1.8.23/dvd

bootloader --append="rhgb quiet crashkernel=0" --iscrypted --password=grub.pbkdf2.sha512.10000.CF4F81F0F06696FBF1B7BDCCDE97DF0E3E8DD595A1A965AFC868407D12C7F2972BD785B1789CC835D2B3C0CDE388A30E468FB1E9846AF9375EEB92A54FBDC40A.F4F67CEEF96CC52148BCE8075BFB21419D48EEBE6A970502DC271A6D95B04C415619D0739A1E567AADFA51A6BF5DF414F10703B1E981214D19E974812D259F3F

zerombr

clearpart --all --initlabel

# Disk partitioning information
part /boot --fstype="xfs" --size=1024
part swap --fstype="swap" --size=2048
part / --fstype="xfs" --grow --size=1

auth --passalgo=sha512 --useshadow

network --device=ens32 --hostname=webapp.laoma.cloud --bootproto=static --ip=10.1.8.100 --netmask=255.255.255.0 --gateway=10.1.8.2 --nameserver=223.5.5.5

skipx

firstboot --enable

selinux --disabled

firewall --enabled --http --ssh
# 密码通过以下命令获得
# python -c 'import crypt; print(crypt.crypt("123", crypt.mksalt(crypt.METHOD_SHA512)))'
group --name=laoma
user --name=laoma --groups=wheel --password='$6$7Xg1hjxQEPoMq/Di$qg6XHPSWjCQ2qQCK9XgeBCsY8ODClbigcbAVWmmdHl3A4CNrS3G5TmtMzxFchl04LkjEqGbGB6E646L5EH8EK.'

reboot

%post  --interpreter=/bin/bash
curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install -y httpd
systemctl enable httpd
echo Hello World > /var/www/html/index.html
echo "
This is a web server.
Be careful." >> /etc/issue
echo webapp.laoma.cloud > /etc/hostname
%end

%packages
@^minimal
kexec-tools
%end

客户端测试

登录界面

在这里插入图片描述

web页面访问测试
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值