pxe+kickstart 无人值守安装及自动化运维

1.原理

Client向 PXE Server上的 DHCP发送IP地址请求消息,DHCP检测Client是否合法(主要是检测Client的网卡MAC地址),如果合法则返回Client的IP地址,同时将启动文件pxelinux.0的位置信息一并传送给Client。

​ 2. Client向PXE Server上的TFTP发送获取pxelinux.0请求消息,TFTP接收到消息之后再向Client发送pxelinux.0大小信息,试探Client是否满意,当TFTP收到Client发回的同意大小信息之后,正式向Client发送pxelinux.0。 ​

3. Client执行接收到的pxelinux.0文件。 ​

4. Client向TFTP发送针对本机的配置信息(记录在TFTP的pxelinux.cfg目录下),TFTP将配置文件发回Client,继而Client根据配置文件执行后续操作。 ​

5. Client向TFTP发送Linux内核请求信息,TFTP接收到消息之后将内核文件发 送给Client。 ​

6. Client向TFTP发送根文件请求信息,TFTP接收到消息之后返回Linux根文件 系统。 ​

7. Client启动Linux内核(启动参数已经在4中的配置文件中设置好了)。 ​

pxelinux.0是用于PXE启动过程中的引导程序,它在客户端计算机加载操作系统之前运行并提供了一个菜单系统,用于选择要加载的操作系统或其他相关工具。

pxelinux.cfg是pxelinux.0的配置文件目录,其中包含了PXELinux引导菜单的配置文件。在该目录中,可以编写多个菜单项的配置文件,用于选择不同的操作系统或相关工具。每个文件都应该包含菜单项的配置信息,例如菜单项名称、菜单显示名称、引导内核文件等。

ks.cfg(Kickstart配置文件)是一个自动安装Linux系统的配置文件。它包含安装过程中的各种选项,例如安装软件包、自动配置系统和网络设置等。ks.cfg 配置文件通常也可以与 PXE 配合使用,以实现无人值守安装操作系统的目的。

这三者的关系是:PXE启动过程中,客户端计算机从网络中获取pxelinux.0文件并执行,

pxelinux.0引导程序读取pxelinux.cfg目录下的配置文件,提供选择操作系统等菜单,然后通过网络加载指定的内核文件和初始化内存文件系统(initrd.img)来安装操作系统。ks.cfg配置文件可以在自动安装过程中提供操作系统配置和自动化设置。

pxelinux.0 是引导程序,pxelinux.cfg 是菜单的配置文件目录,ks.cfg 是自动安装操作系统的配置文件。通过它们的配合使用,你可以实现自动化网络安装多个操作系统的目的。

 2.操作

 2.1首先配置 DHCP 服务

[root@10.0.0.15 ~]#yum install dhcp -y
[root@10.0.0.15 ~]#vim /etc/dhcp/dhcpd.conf
subnet 10.0.0.0 netmask 255.255.255.0 {
  range 10.0.0.10 10.0.0.100;
  option domain-name-servers 10.0.0.2;
  option routers 10.0.0.2;
  default-lease-time 600;
  max-lease-time 7200;
  next-server 10.0.0.128;        # #TFTP Server 的IP地址,也就是本机
  filename "pxelinux.0";             # pxelinux 启动文件位置;
}

@@@ 配置文件说明 @@@
subnet 10.0.0.0 netmask 255.255.255.0 { -> 整个网段
range 10.0.0.10 10.0.0.100; DHCP分配IP范围
option domain-name-servers 10.0.0.2; 这里填写网关的地址
option routers 10.0.0.2; 路由的地址,网关即路由
default-lease-time 600; dhcp默认租约时间(单位:秒)
max-lease-time 7200; dhcp默认最大租约时间(单位:秒)
next-server 10.0.0.128; 这里配置为本机的IP地址
filename "pxelinux.0"; 默认写法

@@@@@@@@@@@@@@@@@

启动服务:
[root@10.0.0.15 ~]#systemctl start dhcpd; systemctl enable dhcpd

2.2配置TFTP服务

[root@10.0.0.15 ~]#yum install tftp-server -y

[root@10.0.0.15 ~]#yum install syslinux -y
[root@10.0.0.15 ~]#mkdir -pv /mnt/centos/
[root@10.0.0.15 ~]#mount /dev/cdrom /mnt/centos/	# 挂载 cdrom ,也可直接挂载 iso 文件

[root@10.0.0.15 ~]#cd /var/lib/tftpboot/
[root@10.0.0.15 /var/lib/tftpboot]#cp -a /mnt/centos/isolinux/boot.msg ./
[root@10.0.0.15 /var/lib/tftpboot]#cp -a /mnt/centos/images/pxeboot/{initrd.img,vmlinuz} ./
[root@10.0.0.15 /var/lib/tftpboot]#cp -a /usr/share/syslinux/pxelinux.0 ./
[root@10.0.0.15 /var/lib/tftpboot]#ll -tsh 
total 60M
4.0K -rw-r--r-- 1 root root   84 Apr 21  2020 boot.msg
 53M -rw-r--r-- 1 root root  53M Apr 21  2020 initrd.img
6.5M -rwxr-xr-x 1 root root 6.5M Apr  1  2020 vmlinuz
 28K -rw-r--r-- 1 root root  27K Oct 31  2018 pxelinux.0
 
[root@10.0.0.15 /var/lib/tftpboot]#mkdir pxelinux.cfg
[root@10.0.0.15 /var/lib/tftpboot]#cd pxelinux.cfg/
[root@10.0.0.15 /var/lib/tftpboot/pxelinux.cfg]#cp -a /mnt/centos/isolinux/isolinux.cfg ./default
[root@10.0.0.15 ~]#umount /mnt/centos/	

配置 PXE 启动引导
syslinux 注解:

  • syslinux是一个功能强大的引导加载程序,并且兼容各种介质
  • syslinux是一个小型的linux操作系统,目的是简化linux首次安装的时间,并建立维护或其他特殊用途的启动盘
  • 如果没有找到pxelinux.0这个文件,可以安装下syslinux

添加 default 文件内容,注意只是添加了 ks 部分的内容 

# 开头部分修改:
default ks
prompt 1
timeout 6

...
label ks
  kernel vmlinuz
  append initrd=initrd.img ks=http://10.0.0.105/ks.cfg
...

default 全文:

default ks
prompt 1
timeout 6
display boot.msg
menu clear
menu background splash.png
menu title CentOS 7
menu vshift 8
menu rows 18
menu margin 8
menu helpmsgrow 15
menu tabmsgrow 13
menu color border * #00000000 #00000000 none
menu color sel 0 #ffffffff #00000000 none
menu color title 0 #ff7ba3d0 #00000000 none
menu color tabmsg 0 #ff3a6496 #00000000 none
menu color unsel 0 #84b8ffff #00000000 none
menu color hotsel 0 #84b8ffff #00000000 none
menu color hotkey 0 #ffffffff #00000000 none
menu color help 0 #ffffffff #00000000 none
menu color scrollbar 0 #ffffffff #ff355594 none
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none
menu tabmsg Press Tab for full configuration options on menu items.
menu separator # insert an empty line
menu separator # insert an empty line
label linux
  menu label ^Install CentOS 7
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet
label check
  menu label Test this ^media & install CentOS 7
  menu default
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rd.live.check quiet
menu separator # insert an empty line
menu begin ^Troubleshooting
  menu title Troubleshooting
label vesa
  menu indent count 5
  menu label Install CentOS 7 in ^basic graphics mode
  text help
	Try this option out if you're having trouble installing
	CentOS 7.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 xdriver=vesa nomodeset quiet
label ks
  kernel vmlinuz
  append initrd=initrd.img ks=http://10.0.0.105/ks.cfg
label rescue
  menu indent count 5
  menu label ^Rescue a CentOS system
  text help
	If the system will not boot, this lets you access files
	and edit config files to try to get it booting again.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rescue quiet
label memtest
  menu label Run a ^memory test
  text help
	If your system is having issues, a problem with your
	system's memory may be the cause. Use this utility to
	see if the memory is working correctly.
  endtext
  kernel memtest
menu separator # insert an empty line
label local
  menu label Boot from ^local drive
  localboot 0xffff
menu separator # insert an empty line
menu separator # insert an empty line
label returntomain
  menu label Return to ^main menu
  menu exit
menu end

注意:修改完成,启动 tftp服务

[root@10.0.0.15 ~]#systemctl start tftp; systemctl enable tftp

2.3基于HTTP协议的PXE环境

[root@10.0.0.15 ~]#yum install httpd -y
[root@10.0.0.15 ~]#mkdir -pv /var/www/html/centos/
[root@10.0.0.15 ~]#mount /dev/cdrom /var/www/html/centos/
[root@10.0.0.15 ~]#systemctl start httpd ;systemctl enable httpd

 接下来,需要配置 ks.cfg 文件:

 这里直接提供一个 最小化安装的centos7.X 的通用文件:

#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Firewall configuration
firewall --disabled
# Install OS instead of upgrade
install
# Use network installation
url --url="http://10.0.0.105/centos" ##### 这里需要手动修改为自己主机的http链接
# Root password
rootpw --iscrypted $1$x1wkiXJv$45UMW./5aUCEkfymzt4WQ/   #### 默认root密码为:123456
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use graphical install
text
# System keyboard
keyboard us
# System language
lang en_US
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
# Installation logging level
logging --level=info
# Reboot after installation
reboot
# System timezone
timezone --utc Asia/Shanghai
# Network information
network  --bootproto=dhcp --device=eth0 --onboot=on
# System bootloader configuration
#bootloader --location=gpt
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel 
# Disk partitioning information
part biosboot --fstype=biosboot --size=1
part /boot/efi --fstype="efi" --ondisk=sda --size=1024 --fsoptions="defaults,uid=0,gid=0,umask=0077,shortname=winnt"
part /boot --fstype="xfs" --size=500
part swap --fstype="swap" --size=1000
part pv.01 --size=1 --grow
volgroup vg00 pv.01
logvol / --vgname=vg00 --size=8192 --name=lv_root

%pre
parted -s /dev/sda mklabel gpt
%end

%packages 
openssh-clients
@core
%end

 将 ks.cfg 文件放置到 http 的根目录下

[root@10.0.0.15 ~]#cp -a ks.cfg /var/www/html/

最后,检查下服务是否启动正常,有以下三个服务:

[root@10.0.0.15 ~]#systemctl status tftp httpd dhcpd

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值