1、PXE 预启动执行环境
1.1 PXE批量部署的优点
- 规模化:同时装配多台服务器
- 自动化:安装系统、配置各种服务
- 远程实现:不需要光盘、U盘等安装介质
1.2 系统装机的三种引导方式
- 硬盘
- 光驱(u盘)
- 网络装机
小拓展: 光盘,涉密单位存储数据
- 多次性的,可以反复擦写
- 一次性的
1.3 系统安装过程
- 加载boot loader
- 加载启动安装菜单
- 加载内核和initrd系统(精简版的linux系统)
- 加载根系统
- 运行anaconda的安装向导
1.4 安装系统的相关文件
1、光驱安装
光驱安装操作系统,在isolinux目录下有和安装相关的必备文件。
- boot.cat: 相当于grub的第一阶段 446字节
- isolinux.bin:光盘引导程序,在mkisofs的选项中需要明确给出文件路径,这个文件属于SYSLINUX项目
- isolinux.cfg:启动菜单的配置文件,当光盘启动后(即运行isolinux.bin),会自动去找isolinux.cfg文件
- vesamenu.c32:菜单文件,是光盘启动后的启动菜单图形界面,也属于SYSLINUX项目,menu.c32提供纯文本的菜单
- memtest:内存检测程序
- splash.png:光盘启动菜单界面的背景图,默认是黑色
- vmlinuz:是内核映像
- initrd.img:ramfs文件(精简版的linux系统,文件系统驱动等)
2、PXE安装
PXE安装操作系统,只需要四大文件(按顺序):
- pxelinux.0:引导程序。来源于syslinux程序
- default:配置文件。(需要手写,可参考光盘 isolinux 目录下的 isolinux.cfg 文件)
- vmlinuz:内核。从光盘中获取,位于 isolinux 目录
- initrd.img: 系统启动镜像文件。(从光盘中获取,位于 isolinux 目录下)
2、四大服务
- dhcp
- tftp
- vsftpd
- syslinux
2.1 dhcp
提供ip地址,注意 网络 必须要通。
2.2 tftp
简单文件传输协议:内核和引导文件。
提供四大文件,vmlinuz、pxelinux.0、initrd.img、default…
2.3 syslinux
引导加载程序 引导pxelinux.0
2.4 vsftpd
安装系统镜像文件的软件包
3、PXE实现过程
3.1 PXE原理
3.2 实验一:PXE配置
实验目的:
实验环境:
实验流程:
- 安装软件
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install tftp-server vsftpd syslinux dhcp -y //四个软件
- 设置dhcp
[root@localhost ~]# cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
[root@localhost ~]# ls /usr/share/doc/dhcp*/dhcpd.conf.example
/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example
[root@localhost tftpboot]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp:是否覆盖"/etc/dhcp/dhcpd.conf"? yes
[root@localhost tftpboot]# vim /etc/dhcp/dhcpd.conf
subnet 192.168.125.0 netmask 255.255.255.0 {
range 192.168.125.20 192.168.125.50;
option routers 192.168.125.130;
next-server 192.168.125.130;
filename "pxelinux.0";
}
[root@localhost ~]# systemctl start dhcpd
3. 设置tftp:
主配置文件:/etc/xinetd.d/tftp
[root@localhost ~]# vim /etc/xinetd.d/tftp //进入主配置文件
disable = no //yes改成no
tftp根目录:/var/lib/tftpboot,该位置也是四大文件存放的位置
`vmlinuz initrd.img` //前两个文件
[root@localhost ~]# mount /dev/sr0 /mnt //为什么挂载,因为内核和镜像都在mnt文件下。
[root@localhost ~]# cd /var/lib/tftpboot
[root@localhost tftpboot]# cd /mnt/isolinux/
[root@localhost isolinux]# cp vmlinuz initrd.img /var/lib/tftpboot/ //将内核、镜像复制到/var/lib/tftpboot/下
[root@localhost isolinux]# cd /var/lib/tftpboot/
[root@localhost tftpboot]#
[root@localhost tftpboot]# ls
initrd.img vmlinuz //此时,存放位置已有两个文件
- 设置syslinux
`pxelinux.0` //第三个文件
[root@localhost tftpboot]# rpm -ql syslinux |grep "pxelinux.0"
/usr/share/syslinux/gpxelinux.0
/usr/share/syslinux/pxelinux.0 //选这个
[root@localhost tftpboot]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
[root@localhost tftpboot]# ls
initrd.img pxelinux.0 vmlinuz
// 已有三个文件
- 设置vsftpd
`pxelinux.cfg` // 第四个文件 手写
[root@localhost tftpboot]# mkdir pxelinux.cfg
[root@localhost tftpboot]# cd pxelinux.cfg
[root@localhost pxelinux.cfg]# vim default
#要回车
default auto
prompt 1
label auto
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.125.130/centos7
label linux text
kernel vmlinuz //内核叫什么名字
append text initrd =initrd.img method=ftp://192.168.125.130/centos7 //告诉你小型linux叫什么
label linux rescue
kernel vmlinuz
append rescue initrd=initrd.img method=ftp://192.168.125.130/centos7 //method是路径
#不用回车,用这个就行!!!!注意改ip
default linux
prompt 1
label linux
kernel vmlinuz
append initrd=initrd.img method=ftp://192.168.125.130/centos7
label linux text
kernel vmlinuz
append text initrd =initrd.img method=ftp://192.168.125.130/centos7
label linux rescue
kernel vmlinuz
append rescue initrd=initrd.img method=ftp://192.168.125.130/centos7
- 准备完毕,重启服务
[root@localhost pxelinux.cfg]# mkdir /var/ftp/centos7
[root@localhost pxelinux.cfg]# mount /dev/sr0 /var/ftp/centos7/
[root@localhost pxelinux.cfg]# cd /var/lib/tftpboot/
[root@localhost tftpboot]# ls
initrd.img pxelinux.0 pxelinux.cfg vmlinuz
[root@localhost tftpboot]# tree
.
├── initrd.img
├── pxelinux.0
├── pxelinux.cfg
│ └── default
└── vmlinuz
1 directory, 4 files //四大文件已准备好
[root@localhost tftpboot]# cd ~
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start tftp.socket
[root@localhost ~]# systemctl start dhcpd
[root@localhost ~]# systemctl start vsftpd
//只开启三个,syslinux不是软件,无需开启
3.3 测试
创建空白虚拟机测试能否自动装机
测试成功!
4、kickstart 无人值守
图形化界面 自动化
安装系统的配置文件:`vim anaconda-ks.cfg
4.1 kickstart文件的组成
由三部分组成,分别是\color{red}{命令段、程序包段、脚本段。}
- 命令段:
指明各种安装前配置,如键盘类型等
// 命令段中的常见命令
keyboard: 设定键盘类型
lang: 语言类型
zerombr:清除mbr
clearpart:清除分区
part: 创建分区
rootpw: 指明root的密码
timezone: 时区
text: 文本安装界面
network:指定网络设置
firewall:设置防火墙设置
selinux:设置selinux设置
reboot:安装完自动重启
user:安装完成后为系统创建新用户
url: 指明安装源
- 程序包段:
指明要安装的程序包组或程序包,不安装的程序包等
%packages
@^environment group: 指定环境包组,如:@^minimal-environment
@group_name
package
-package
%end
- 脚本段:
- %pre: 安装前脚本
- %post: 安装后脚本
//系统安装后,新建用户zhangsan
%post
useradd zhangsan
%end
//%post与%end 成对出现
4.2 实验二:
实验目的:
实验环境:
实验流程:
- 安装软件
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost tftpboot]# yum install system-config-kickstart -y
- 打开虚拟机(192.168.125.130)的图形化界面
- 基本配置
- 安装方法
- 引导装载程序选项
- 分区信息
- 网络配置,默认即可
- 验证,默认即可
- 防火墙配置
- 显示配置
- 软件包选择,目前先不用改
- 安装后脚本
-
保存设置
- 配置
[root@localhost ~]# cd /var/ftp
[root@localhost ftp]# ll
总用量 6
drwxr-xr-x. 8 root root 2048 9月 5 2017 centos7
-rw-r--r--. 1 root root 983 5月 7 00:45 ks.cfg
drwxr-xr-x. 2 root root 6 6月 10 2021 pub
[root@localhost ftp]# cd /var/lib/tftpboot/pxelinux.cfg
[root@localhost pxelinux.cfg]# vim default
- 测试
重新启动新安装的虚拟机