设为首页 收藏本站
查看: 1212|回复: 0

[经验分享] Linux系统的PXE

[复制链接]

尚未签到

发表于 2018-5-16 11:58:49 | 显示全部楼层 |阅读模式
  一.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  #需要安装这些软件
DSC0000.jpg
(2)打开端口
[root@server ~]# vim /etc/xinetd.d/tftp  #守护进程打开
disable no
DSC0001.jpg
DSC0002.jpg
[root@server ~]# systemctl start xinetd.service
DSC0003.jpg
[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
DSC0004.jpg
(3)安装所需的工具配置
[root@server ~]# cd /var/lib/tftpboot/    #切换到此目录
[root@server tftpboot]# cp /usr/share/syslinux/pxelinux.0 .  #在pexlinux.text文件中已指出,建立文件/tftpboot,并把/usr/share/syslinux/pxelinux.0放到此目录中
DSC0005.jpg
[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
DSC0006.jpg
DSC0007.jpg
[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
DSC0008.jpg
  [root@server tftpboot]# cp isolinux.cfg pxelinux.cfg/default
[root@server ~]# systemctl start httpd
DSC0009.jpg
[root@server ~]# systemctl stop firewalld.service
DSC00010.jpg
(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
DSC00011.jpg
[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 }
DSC00012.jpg
DSC00013.jpg
  [root@server ~]# systemctl restart dhcpd  #重启
DSC00014.jpg                                       
(5)启动预安装环境
安装系统:模拟安装虚拟机
从网卡启动
DSC00015.jpg
获取dhcp
DSC00016.jpg
读取pxelinux.0里面所写的内容
DSC00017.jpg
2.修改预安装环境的标题为HELLO
  [root@server tftpboot]# vim pxelinux.cfg/default  #预安装时读取文件
11 menu title HELLO   #修改预安装环境的标题为HELLO
DSC00018.jpg
DSC00019.jpg
获取dhcp
DSC00020.jpg
预安装环境的标题改为HELLO
DSC00021.jpg
  3.启动http服务得到镜像安装系统
[root@server tftpboot]# vim pxelinux.cfg/default
64   append initrd=initrd.img  repo=http://172.25.254.60/rhel7.0  #镜像文件所在位置
DSC00022.jpg
DSC00023.jpg
安装过程:
(1)获取dhcp
DSC00024.jpg
(2)安装系统,手动调到第一个标题,默认是第二个标题
DSC00025.jpg
(3)安装过程显示
DSC00026.jpg
(4)回答问题页面
DSC00027.jpg
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
DSC00028.jpg
DSC00029.jpg
  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
DSC00030.jpg
DSC00031.jpg
(1)获取dhcp
DSC00032.jpg
(2)启动预安装环境,时间变为20s
DSC00033.jpg
6.修改预安装环境的背景图片
ls
DSC00034.jpg
可以知道预安装环境的图片是
splash.png
[root@server tftpboot]# file splash.png  #预安装环境使用的图片,此图片可作更改
splash.png: PNG image data, 640 x 480, 1-bit colormap, non-interlaced
DSC00035.jpg
[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
DSC00036.jpg
[root@server tftpboot]# file westos.png
westos.png: PNG image data, 558 x 646, 8-bit/color RGBA, non-interlaced  #此图片像素与要求图片像素不符,需要进行裁剪,安装裁剪工具
DSC00037.jpg
[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
DSC00038.jpg
安装 :yum install mageMagick-6.7.8.9-10.el7.x86_64
[root@server tftpboot]# convert -resize 640x480! westos.png haha.png #裁剪成符合要求的图片大小,!表示强制裁剪成要求的大小
DSC00039.jpg
[root@server tftpboot]# vim pxelinux.cfg/default
10 menu background haha.png  #更改安装环境图片为haha.png
DSC00040.jpg
DSC00041.jpg
(1)获取dhcp
DSC00042.jpg
(2)启动预安装环境:图片更改
DSC00043.jpg
7.kickstart自动安装脚本
  kickstart
使用 kickstart, 系统管理员可以创建一个包含安装期间所有常见问题的答案的文件,以自动安装 Red Hat Enterprise Linux Kickstart
  手动编写kickstart脚本难度较大,采用系统自带软件包工具来制作
  软件包system-config-kickstart
(1)安装软件
[root@server ~]# yum install system-config-kickstart  #安装
DSC00044.jpg
(2)生成ks.cfg文件
[root@server ~]# system-config-kickstart  #常见问题答案回答,问题的答案生成ks.cfg,保存在/var/www/html/目录中,安装时,使用http可以访问到
DSC00045.jpg
(1)基础设定
语言,时区,密码,是否安装图形
DSC00046.jpg
(2)安装方式
安装源位置
http://172.25.254.60/rhel7.0
DSC00047.jpg
(3)启动方式选项
DSC00048.jpg
(4)自定义分区
DSC00049.jpg
/boot分区:200M
DSC00050.jpg
swap分区:500M
DSC00051.jpg
*/分区:剩下的所有空间
DSC00052.jpg
(5)安装是的网络配置
eth0,dhcp
DSC00053.jpg
(6)火墙状态:选择关闭,selinux关闭
DSC00054.jpg
(7)保存
点击左上角的file选择save,在选择保存的位置,我将它保存在/var/www/html便于http访问
DSC00055.jpg
DSC00056.jpg
  8.编辑生成的ks.cfg文件
安装一个新分区
[root@server ~]# cd /var/www/html/  
[root@server html]# ls
ks.cfg
[root@server html]# vim ks.cfg   #可以添加内容
DSC00057.jpg
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
DSC00058.jpg
DSC00059.jpg
[root@server html]# ksvalidator ks.cfg  #检测该脚本是否有语法错误
是否能被访问到
DSC00060.jpg
无语法错误后
http是否能访问到
DSC00061.jpg
镜像文件是否能访问到
DSC00062.jpg
9.回答系统提出的问题,答案脚本写入文件
[root@server html]# cd /var/lib/tftpboot/
[root@server tftpboot]# vim pxelinux.cfg/default
DSC00063.jpg
65   append initrd=initrd.img  repo=http://172.25.254.60/rhel7.0 ks=http://172.25.254.125/ks.    cfg
DSC00064.jpg
安装系统:
(1)获取dhcp
DSC00065.jpg
(2)进入预安装环境:读取内核文件启动初始化程序
DSC00066.jpg
(3)读取镜像,进入安装环境
DSC00067.jpg
系统就安装完成啦

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-460974-1-1.html 上篇帖子: Linux中iscsi 下篇帖子: 我的linux起步
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表