Linux系统的PXE

简介:

一.PXE概述
PXE(preboot execute environment)预启动执行环境,工作于Client/Server的网络模式,支持工作站通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统,在启动过程中,终端要求服务器分配IP地址,再用TFTP(trivial file transfer protocol)或MTFTP(multicast trivial file transfer protocol)协议下载一个启动软件包到本机内存中执行,由这个启动软件包完成终端(客户端)基本软件设置,从而引导预先安装在服务器中的终端操作系统。PXE可以引导多种操作系统。
二.PXE启动原理
当计算机引导时,BIOS把 PXE Client 调入内存中执行,然后由 PXE Client 将放置在远端的文件通过网络下载到本地运行。
三.PXE无盘工作站的启动过程分析如下
¨ 客户端个人电脑开机后, 在 TCP/IP Bootrom 获得控制权之前先做自我测试。
¨ Bootprom 送出DHCP 要求以取得 IP。
¨ 如果服务器收到个人电脑所送出的要求, 就会送回 DHCP 回应,内容包括
客户端的 IP 地址, 预设网关, 及开机镜像文件
¨ Bootprom 由 TFTP 通讯协议从服务器下载开机镜像文件。
¨ 个人电脑通过这个开机镜像文件开机, 这个开机文件可以只是单纯的开机程式也可以是操作系统。
¨ 开机映像文件将包含 kernel loader 及压缩过的 kernel,此 kernel 将支持NTFS root系统。
¨ 远程客户端根据下载的文件启动机器。
三.安装PXE所需的软件
根据以上的原理解释,知道启动linux系统需要网卡PXE bootrom程序(写入主板bios,如果网卡已有PXE bootrom则不需要),DHCP服务器软件,PXE服务软件,TFTP服务软件,工作站上传工具,还要有
Linux操作系统软件。
我筛选的软件:网卡写入主板的bootrom程序, DHCP Server, sys(包含TFTP服务与PXE服务等),下载软件(http),syslinux(syslinux是一个功能强大的引导加载程序,它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘。它的安装很简单,一旦安装syslinux好之后,sysLinux启动盘就可以引导各种基于DOS的工具。
四.安装PXE所需要的工具
[root@foundation60 ~]# cd /var/www/html/rhel7.0 #切换到镜像文件中
[root@foundation60 rhel7.0]# ls
addons images Packages RPM-GPG-KEY-redhat-release
EFI isolinux release-notes TRANS.TBL
EULA LiveOS repodata
GPL media.repo RPM-GPG-KEY-redhat-beta
[root@foundation60 rhel7.0]# cd isolinux/
[root@foundation60 isolinux]# ls
boot.cat grub.conf isolinux.bin memtest TRANS.TBL vesamenu.c32
boot.msg initrd.img isolinux.cfg splash.png upgrade.img vmlinuz
[root@foundation60 isolinux]# vim isolinux.cfg #此文件中所写内容就是加载内核启>动初始化程序进入预安装环境
label linux
menu label ^Install Red Hat Enterprise Linux 7.0 #预安装环境的标题
kernel vmlinuz #内核文件
append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.0\x20Server.x86_64 quiet #镜像

label check
menu label Test this ^media & install Red Hat Enterprise Linux 7.0 #预安装环>境的标题
menu default
kernel vmlinuz
append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.0\x20Server.x86_64 rd.live.check quiet #镜像,quiet:安装时不显示过程
[root@server ~]# yum whatprovides */pxelinux.0
Filename : /usr/share/syslinux/pxelinux.0
[root@server ~]# rpm -ql syslinux
/usr/share/syslinux/pxelinux.0
/usr/share/doc/syslinux-4.05
[root@server syslinux-4.05]# less README #此文件中写了看如下文件
See the files in the doc directory for documentation about SYSLINUX:
syslinux.txt - Usage instructions; manual.
distrib.txt - For creators of Linux distributions.
pxelinux.txt - Documentation specific to PXELINUX.
isolinux.txt - Documentation specific to ISOLINUX.
extlinux.txt - Documentation specific to EXTLINUX.
menu.txt - About the menu systems.
usbkey.txt - About using SYSLINUX on USB keys.
comboot.txt - About the extension API.
memdisk.txt - Documentation about MEMDISK.
[root@server syslinux-4.05]# less pxelinux.txt #看此文件
n the TFTP server, create the directory "/tftpboot", and copy the
following files to it: #新建目录/tftpboot ,并且复制pxelinux.0 文件到/tftpboot>中

    pxelinux.0              - from the Syslinux distribution

    any kernel or initrd images you want to boot  #启动时加载内核启动初始化>程序pxelinux.0这个文件的内容

create the directory "/tftpboot/pxelinux.cfg". #创建/tftpboot/pxelinux.cfg目录
As an example, if the boot file name is /mybootdir/pxelinux.0, the
UUID is b8945908-d6a6-41a9-611d-74a6ab80b83d, the Ethernet MAC
address is 88:99:AA:BB:CC:DD and the IP address 192.0.2.91, it will
try:
/mybootdir/pxelinux.cfg/default #/mybootdir/pxelinux.0读取的是/mybootdir/pxelinux.cfg/default中的内容
++++ SETTING UP THE TFTP SERVER ++++

PXELINUX currently requires that the boot server has a TFTP server
which supports the "tsize" TFTP option (RFC 1784/RFC 2349). The
"tftp-hpa" TFTP server, which support options, is available at:

    http://www.kernel.org/pub/software/network/tftp/
    ftp://www.kernel.org/pub/software/network/tftp/

and on any kernel.org mirror (see http://www.kernel.org/mirrors/). #镜像安装>源指定为网络安装源

Another TFTP server which supports this is atftp by Jean-Pierre
Lefebvre:

    ftp://ftp.mamalinux.com/pub/atftp/

那么问题来了,系统怎么回去读取/tftpboot/pxelinux.0这个文件呢?
网络安装需要获取ip,当获取ip时让它读取此文件即可,因此需要动态网络dhcp,在此文件中写入该文件,获取ip后读取该文件,该文件内容中又写了让它加载内核文件,启动初始化程序,进入预安装环境,此时镜像文件源可以是http协议的也可以是vsftp协议的,将此镜像
源地址写入/tftpboot/pxelinux.cfg/default中,此时并不是全自动的需要手动选择分区等等内容,使用自动化安装时,可以写一个回答问题的脚本,也将他写入/tftpboot/pxelinux.cfg/default中,会自动读取,实现安装全自动。
五.安装PXE
1.(1)安装所需软件
[root@server ~]# yum install dhcp tftp-server httpd syslinux -y #需要安装这些软件
Linux系统的PXE
(2)打开端口
[root@server ~]# vim /etc/xinetd.d/tftp #守护进程打开
disable no
Linux系统的PXE
Linux系统的PXE
[root@server ~]# systemctl start xinetd.service
Linux系统的PXE
[root@server ~]# netstat -antlupe | grep 69 #查看xinetd端口是否打开
udp 0 0 0.0.0.0:69 0.0.0.0:* 0 94269 936/xinetd #打开

0 94269 936/xinetd
Linux系统的PXE
(3)安装所需的工具配置
[root@server ~]# cd /var/lib/tftpboot/ #切换到此目录
[root@server tftpboot]# cp /usr/share/syslinux/pxelinux.0 . #在pexlinux.text文件中已指出,建立文件/tftpboot,并把/usr/share/syslinux/pxelinux.0放到此目录中
Linux系统的PXE
[root@server tftpboot]# yum install lftp
[root@server tftpboot]# lftp 172.25.254.60
lftp 172.25.254.60:/pub> cd isolinux/ #安装需要的文件都在此目录
lftp 172.25.254.60:/pub/isolinux> mget *
74555827 bytes transferred in 1 second (57.48M/s)
Total 12 files transferred
lftp 172.25.254.60:/pub/isolinux> quit
Linux系统的PXE
Linux系统的PXE
[root@server tftpboot]# ls
boot.cat initrd.img memtest splash.png vesamenu.c32
boot.msg isolinux.bin pxelinux.0 TRANS.TBL vmlinuz
grub.conf isolinux.cfg pxelinux.cfg upgrade.img
Linux系统的PXE

[root@server tftpboot]# cp isolinux.cfg pxelinux.cfg/default
[root@server ~]# systemctl start httpd
Linux系统的PXE
[root@server ~]# systemctl stop firewalld.service
Linux系统的PXE
(4)配置dhcp
[root@server ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp: overwrite ‘/etc/dhcp/dhcpd.conf’? y
Linux系统的PXE
[root@server ~]# vim /etc/dhcp/dhcpd.conf #配置dhcp
1 option domain-name "example.com"; #域名
2 option domain-name-servers 172.25.254.125; #从哪台主机得到ip
3 default-lease-time 600;
4 max-lease-time 7200;
5 log-facility local7;
6 subnet 172.25.254.0 netmask 255.255.255.0 { #网段,子网掩码
7 range 172.25.254.1 172.25.254.254; #获取ip网段
8 option routers 172.25.254.125; #网关
9 next-server 172.25.254.125; #pxelinux.0这个文件在哪台主机上就时哪台主机ip
10 filename "pxelinux.0"; #读取此文件
11 }
Linux系统的PXE
Linux系统的PXE

[root@server ~]# systemctl restart dhcpd #重启
Linux系统的PXE 
(5)启动预安装环境
安装系统:模拟安装虚拟机
从网卡启动
Linux系统的PXE
获取dhcp
Linux系统的PXE
读取pxelinux.0里面所写的内容
Linux系统的PXE
2.修改预安装环境的标题为HELLO

[root@server tftpboot]# vim pxelinux.cfg/default #预安装时读取文件
11 menu title HELLO #修改预安装环境的标题为HELLO
Linux系统的PXE
Linux系统的PXE
获取dhcp
Linux系统的PXE
预安装环境的标题改为HELLO
Linux系统的PXE

3.启动http服务得到镜像安装系统
[root@server tftpboot]# vim pxelinux.cfg/default 
64 append initrd=initrd.img repo=http://172.25.254.60/rhel7.0 #镜像文件所在位置
Linux系统的PXE
Linux系统的PXE
安装过程:
(1)获取dhcp
Linux系统的PXE
(2)安装系统,手动调到第一个标题,默认是第二个标题
Linux系统的PXE
(3)安装过程显示
Linux系统的PXE
(4)回答问题页面
Linux系统的PXE
4.修改默认启动: 把menu default加到第一个标题下面,就从第一个标题启动 
[root@server tftpboot]# vim pxelinux.cfg/default 
61 label linux
62 menu label ^Install Red Hat Enterprise Linux 7.0
63 menu default #预安装环境中默认从第一个标题启动,原来默认从第二个标题启动
64 kernel vmlinuz
65 append initrd=initrd.img repo=http://172.25.254.60/rhel7.0
66 label check
67 menu label Test this ^media & install Red Hat Enterprise Linux 7.0
68 kernel vmlinuz
69 append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.0\x20Server.x86_64 rd.live.ch eck quiet
Linux系统的PXE
Linux系统的PXE

5.修改预安装环境的等待时间
[root@server tftpboot]# vim pxelinux.cfg/default 
2 timeout 200 #默认安装时等待60s,改为20s
[root@server tftpboot]# ls
boot.cat grub.conf isolinux.bin memtest pxelinux.cfg TRANS.TBL vesamenu.c32
boot.msg initrd.img isolinux.cfg pxelinux.0 splash.png upgrade.img vmlinuz
Linux系统的PXE
Linux系统的PXE
(1)获取dhcp
Linux系统的PXE
(2)启动预安装环境,时间变为20s
Linux系统的PXE
6.修改预安装环境的背景图片
ls
Linux系统的PXE
可以知道预安装环境的图片是
splash.png
[root@server tftpboot]# file splash.png #预安装环境使用的图片,此图片可作更改
splash.png: PNG image data, 640 x 480, 1-bit colormap, non-interlaced
Linux系统的PXE
[root@server tftpboot]# ls #得到一张图片westos.png
boot.cat initrd.img memtest splash.png vesamenu.c32
boot.msg isolinux.bin pxelinux.0 TRANS.TBL vmlinuz
grub.conf isolinux.cfg pxelinux.cfg upgrade.img westos.png
Linux系统的PXE
[root@server tftpboot]# file westos.png 
westos.png: PNG image data, 558 x 646, 8-bit/color RGBA, non-interlaced #此图片像素与要求图片像素不符,需要进行裁剪,安装裁剪工具
Linux系统的PXE
[root@server tftpboot]# yum whatprovides */convert #查看软件
mageMagick-6.7.8.9-10.el7.x86_64 : An X application for displaying and manipulating
: images
Repo : rhel_dvd
Matched from:
Filename : /usr/bin/convert
Linux系统的PXE
安装 :yum install mageMagick-6.7.8.9-10.el7.x86_64
[root@server tftpboot]# convert -resize 640x480! westos.png haha.png #裁剪成符合要求的图片大小,!表示强制裁剪成要求的大小
Linux系统的PXE
[root@server tftpboot]# vim pxelinux.cfg/default 
10 menu background haha.png #更改安装环境图片为haha.png
Linux系统的PXE
Linux系统的PXE
(1)获取dhcp
Linux系统的PXE
(2)启动预安装环境:图片更改
Linux系统的PXE
7.kickstart自动安装脚本

kickstart
使用 kickstart, 系统管理员可以创建一个包含安装期间所有常见问题的答案的文件,以自动安装 Red Hat Enterprise Linux Kickstart

手动编写kickstart脚本难度较大,采用系统自带软件包工具来制作

软件包system-config-kickstart
(1)安装软件
[root@server ~]# yum install system-config-kickstart #安装
Linux系统的PXE
(2)生成ks.cfg文件
[root@server ~]# system-config-kickstart #常见问题答案回答,问题的答案生成ks.cfg,保存在/var/www/html/目录中,安装时,使用http可以访问到
Linux系统的PXE
(1)基础设定
语言,时区,密码,是否安装图形
Linux系统的PXE
(2)安装方式
安装源位置
http://172.25.254.60/rhel7.0
Linux系统的PXE
(3)启动方式选项
Linux系统的PXE
(4)自定义分区
Linux系统的PXE
/boot分区:200M
Linux系统的PXE
swap分区:500M
Linux系统的PXE
*/分区:剩下的所有空间
Linux系统的PXE
(5)安装是的网络配置
eth0,dhcp
Linux系统的PXE
(6)火墙状态:选择关闭,selinux关闭
Linux系统的PXE
(7)保存
点击左上角的file选择save,在选择保存的位置,我将它保存在/var/www/html便于http访问
Linux系统的PXE
Linux系统的PXE

8.编辑生成的ks.cfg文件
安装一个新分区
[root@server ~]# cd /var/www/html/ 
[root@server html]# ls
ks.cfg
[root@server html]# vim ks.cfg #可以添加内容
Linux系统的PXE
1 #platform=x86, AMD64, or Intel EM64T
2 #version=DEVEL
3 # Install OS instead of upgrade
4 install #指定安装系统,还有升级系统选项
5 # Keyboard layouts
6 keyboard 'us' #keyboard指定键盘内型,keyboard us:美式键盘
7 # Halt after installation
8 halt #halt/reboot:安装完成后关机还是reboot,默认halt
9 # Root password
10 rootpw --iscrypted $1$bFQSWMpe$ZUyHLv0JV.IoM0HLarKfX1 #rootpw:root用户密码使用默认sha-512
11 #user:在系统中生成一个新用户
12 # System timezone
13 timezone Asia/Shanghai #时区
14 # Use network installation
15 url --url="http://172.25.254.60/rhel7.0" #安装源
16 # System language
17 lang en_US #lang指定安装过程使用的语言
18 # Firewall configuration
19 firewall --disabled #禁用防火墙
20 # Network information
21 network --bootproto=dhcp --device=eth0
22 # System authorization information
23 auth --useshadow --passalgo=sha512 #auth:验证选项,--useshadow:启用shadow文件来验证

System authorization information

24 # Use graphical install
25 graphical #在图形模式下根据kickstart执行安装,默认该选项.text:文本模式下根据kickstart执行
安装
26 firstboot --disable #--disable:安装后第一次启动默认会给出很多需要手动配置的界面,禁用它
27 # SELinux configuration
28 selinux --disabled
29 skipx:不安装图形工具
30 # System bootloader configuration
31 bootloader --location=mbr #bootloader:指定如何安装引导程序,要求必须已选择分区,已选择引
导程序,已选择软件包,如果没选择将会停止而不会询问。--location=mbr:指定引导程序的位置,默
认为mbr
32 # Clear the Master Boot Record
33 zerombr #清除磁盘的mbr
34 # Partition clearing information
35 clearpart --all --initlabel #在安装系统前清除分区,--all:清除所有分区,--linux:清除linux分
区,--none:不清除分区,--initlabel:创建标签,对于没有mbr和gpt的新硬盘,该选项是必须的,--dir vers=sdb:清除指定的分区
36 # Disk partitioning information
37 part /boot --fstype="xfs" --size=200
38 part swap --fstype="swap" --size=500
39 part / --fstype="xfs" --grow --size=1
#part:创建分区,--asprimary:强制指定为主分区,--grow:使用所有可用空间,即为其分配所有剩余> 空间。对于根分区至少需要3G空间(--grow,也要指定--size)

40 %packages #该段内容
41 @base
42 lftp
43 %end #结束
44 
45 %post #安装结束做的事情
46 cat >> /etc/yum.repos.d/yum.repo <<EOF #配置yum源
47 [rhel7.0]
48 name=rhel7.0
49 baseurl=http://172.25.254.60/rhel7.0
50 gpgcheck=0
51 EOF
52 rm -fr /etc/sysconfig/network-scripts/ifcfg-eth0
53 cat >>/etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF #配置动态网络
54 DEVICE=eth0
55 ONBOOT=yes
56 BOOTPROTO=none
57 IPADDR=172.25.254.225
58 PREFIX=24
59 EOF
60 systemctl restart network
61 
62 yum install httpd -y #安装http服务
63 systemctl start httpd #重启
64 systemctl enable httpd #开机自启
65 systemctl stop firewalld #关闭火墙
66 echo 172.25.254.225 > /var/www/html/index.html
67 %end
Linux系统的PXE
Linux系统的PXE
[root@server html]# ksvalidator ks.cfg #检测该脚本是否有语法错误
是否能被访问到
Linux系统的PXE
无语法错误后
http是否能访问到
Linux系统的PXE
镜像文件是否能访问到
Linux系统的PXE
9.回答系统提出的问题,答案脚本写入文件
[root@server html]# cd /var/lib/tftpboot/
[root@server tftpboot]# vim pxelinux.cfg/default 
Linux系统的PXE
65 append initrd=initrd.img repo=http://172.25.254.60/rhel7.0 ks=http://172.25.254.125/ks. cfg
Linux系统的PXE
安装系统:
(1)获取dhcp
Linux系统的PXE
(2)进入预安装环境:读取内核文件启动初始化程序
Linux系统的PXE
(3)读取镜像,进入安装环境
Linux系统的PXE
系统就安装完成啦














本文转自Uniqueh51CTO博客,原文链接:http://blog.51cto.com/13363488/2050165 ,如需转载请自行联系原作者



相关文章
|
7天前
|
资源调度 JavaScript 搜索推荐
Linux系统之部署envlinks极简个人导航页
【4月更文挑战第11天】Linux系统之部署envlinks极简个人导航页
41 2
|
10天前
|
缓存 Linux 测试技术
安装【银河麒麟V10】linux系统--并挂载镜像
安装【银河麒麟V10】linux系统--并挂载镜像
61 0
|
10天前
|
监控 Unix Linux
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
26 0
|
17天前
|
存储 前端开发 Linux
Linux系统之部署ToDoList任务管理工具
【4月更文挑战第1天】Linux系统之部署ToDoList任务管理工具
61 1
|
18天前
|
存储 传感器 运维
linux系统资源统计工具
【4月更文挑战第1天】Linux系统监控工具如dstat、htop、glances、vmstat、top、iostat、mpstat、sar和atop,用于跟踪CPU、内存、磁盘I/O、网络和进程性能。这些工具提供实时、交互式和历史数据分析,助力管理员优化系统性能和故障排查。例如,dstat是vmstat等工具的增强版,htop提供彩色界面的进程管理,而atop则结合了多种功能并记录历史数据。
27 5
linux系统资源统计工具
|
8天前
|
存储 算法 Linux
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
24 6
|
18天前
|
Ubuntu 架构师 Java
Linux系统常用命令非常详细建议收藏
Linux系统常用命令非常详细建议收藏
49 0
|
3天前
|
运维 网络协议 Unix
18.系统知识-Linux常用命令
18.系统知识-Linux常用命令
|
12天前
|
Prometheus 监控 Cloud Native
【Linux】查看系统内存命令(详细讲解)
【Linux】查看系统内存命令(详细讲解)
|
14天前
|
Linux API C语言
FFmpeg开发笔记(一)搭建Linux系统的开发环境
本文指导初学者如何在Linux上搭建FFmpeg开发环境。首先,由于FFmpeg依赖第三方库,可以免去编译源码的复杂过程,直接安装预编译的FFmpeg动态库。推荐网站<https://github.com/BtbN/FFmpeg-Builds/releases>提供适用于不同系统的FFmpeg包。但在安装前,需确保系统有不低于2.22版本的glibc库。详细步骤包括下载glibc-2.23源码,配置、编译和安装。接着,下载Linux版FFmpeg安装包,解压至/usr/local/ffmpeg,并设置环境变量。最后编写和编译简单的C或C++测试程序验证FFmpeg环境是否正确配置。
35 8
FFmpeg开发笔记(一)搭建Linux系统的开发环境