一、PXE简介
1.1 PXE简介
PXE(Pre-boot Execution Environment)预启动执行环境,是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端服务器下载映像,并由此支持来自网络的操作系统的启动过程。
1.2 PXE工作原理
PXE client是需要安装os的计算机,TFTP Server、DHCP Server及HTTP Server部署在在PXE Server端。Bootstrap文件、配置文件以及Linux根文件系统都放置在Linux Server上TFTP服务器的根目录下,iso和自动化部署放在http server下.
1.3 PXE必要条件
- pxe client必须支持pxe功能,并且开机时选择网络启动,这样系统才能以网络的方式进入pxe启动程序(支持PXE的网卡+实现PXE的BIOS)
- pxe server必须至少提供DHCP及TFTP的服务
- 使用NFS/HTTP/FTP之一的服务器提供pxe client所需程序及软件来源等通讯协议
二、 工作流程及环境
2.1.1 安装环境介绍
ubuntu20.04 server支持用cloud-init的配置实现pxe自动化部署,本文主要介绍在Ubuntu 20.04 desktop上搭建一套自动安装Ubuntu 20.04 Server的部署环境
2.1.2 DHCP、TFTP、HTTP简介
- DHCP简介
动态主机设置协议(Dynamic Host Configuration Protocol,DHCP)是一种使网络管理员能够集中管理和自动分配IP网络地址的通信协议,使用UDP协议工作,DHCP使用统一端口作为BOOTP:服务器端使用67/udp,客户端使用68/udp。主要有两个用途:用于内部网络或网络服务供应商自动分配IP地址给用户;用于内部网络管理员作为对所有电脑作中央管理的手段。
- TFTP简介
TFTP(Trivial File Transfer Protocol) 简单文件传输协议,是一种用来传输文件的简单协议,运行在 UDP(用户数据报协议)上,是一个简化版的FTP。 TFTP 的被设计为小而简单容易的运行,因此,它缺乏标准 FTP 协议的许多特征。 TFTP只能从远程服务器上读、写文件(邮件)或者读、写文件传送给远程服务器。它不能列出目录并且当前不提供用户认证。
- http简介
HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议。。HTTP是一个基于TCP/IP通信协议来传递数据(HTML 文件, 图片文件,查询结果等)。
2.1.3 PXE流程
三、操作流程
3.1 安装server端软件
- 安装DHCP、TFTP服务器
dnsmasq同时实现了DHCP、TFTP、DNS三种服务器
sudo apt-get install dnsmasq
在安装过程中,会看到一些错误和警告 。 在这个阶段,这是一个预期的行为。 后期需要额外配置
- 安装HTTP服务器
apache原指http server程序,后来成为了该程序的组织名,所以把原程序名定成了httpd,在2.0版后又叫apache2。 apache2最流行的Web服务器软件之一
sudo apt-get install apache2
3.2下载启动文件
- 我使用的client,BIOS只支持UEFI
下载uefi 引导文件,shim.signed、grub-efi-amd64-signed 安全启动链加载的引导程序
sudo apt-get download shim.signed
sudo apt-get download grub-efi-amd64-signed
并将内容提取到其专用文件夹(shim 和 grub)中。 pxeboot uefi 客户端需要这些文件…
使用dpkg -x 提取
下载leagcy引导文件 (我这里BIOS只支持UEFI,所以忽略)
wget https://mirrors.edge.kernel.org/pub/linux/utils/boot/syslinux/syslinux-6.03.zip
- 下载ubuntu 20.04 server iso
下载地址
3.3 配置dnsmasq
使用ifconfig确认网卡接口
再配置dnsmsq.conf
- 配置DHCP 地址段
- 配置引导文件目录
- 配置tftp 根目录
- 配置日志路径
- 配置服务网卡,多网卡机器需关注
sudo vim /etc/dnsmasq.conf
#Interface information
#--使用ip addr 查看系统网络接口
## 指定服务的网卡
interface=enp7s0,lo
# 绑定端口
bind-interfaces
domain=c-nergy.local
#--------------------------
#DHCP Settings
#--------------------------
#-- Set dhcp scope
dhcp-range=192.168.1.100,192.168.1.240,255.255.255.0,12h
#-- 指定网关地址 和安装无关,应该可以不配置
dhcp-option=3,192.168.1.1
#-- 指定DNS服务器地址 和安装无关,应该可以不配置
#dhcp-option=6,114.114.114.114
#-- 配置外网DNS服务器地址
#server=114.114.114.114
#----------------------#
# Specify TFTP Options #
#----------------------#
#--设置引导程序相对tftp根目录的路径
#dhcp-boot=/bios/pxelinux.0,pxeserver,192.168.1.10
#--打开tftp服务
enable-tftp
#-- 设置tftp根路径
tftp-root=/tftp
#--检测架构并发送正确的引导加载程序文件
dhcp-match=set:efi-x86_64,option:client-arch,7
dhcp-boot=tag:efi-x86_64,grub/bootx64.efi
service dnsmasq restart
service dnsmasq status
3.4 配置HTTP
apache2的默认服务根目录是 /var/www/html
首先挂载下好的iso文件
mkdir /var/www/html/server
mkdir /var/www/html/server/ubuntu2004
mkdir /var/www/html/iso
cp ubuntu-20.04.3-live-server-amd64.iso /var/www/html/iso/
mount ubuntu-20.04.3-live-server-amd64.iso /mnt/
cp /mnt/* -r /var/www/html/server/ubuntu2004/
mkdir /var/www/html/autoinstall
touch /var/www/html/autoinstall/user-data
touch /var/www/html/autoinstall/meta-data
chmod -R 777 /var/www/html/
- autoinstall 目录存放参数自动配置文件,user-data、meta-data是cloud-init要求的文件名,meta-data无需修改,user-data描述配置如下
- iso 目录存放 操作系统镜像文件
配置user-data
vim /var/www/html/autoinstall/user-data
#cloud-config
autoinstall:
version: 1
# 修改apt 服务地址
apt:
primary:
- arches: [default]
uri: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
user-data:
# 配置时区
timezone: Asia/Shanghai
# 去使能 root账号
disable_root: true
# 配置用户
identity:
hostname: ubuntu-server
password: "123123"
username: ubuntu
# 配置键盘
keyboard: {layout: us, variant: ''}
locale: en_US.UTF-8
# 配置代理
# proxy: http://192.168.1.112:3128
# 默认安装ssh server
ssh:
install-server: true
# 指定安装的包
packages:
# - net-tools
# - python3-pip
# 配置磁盘分区
storage:
grub:
reorder_uefi: False
config:
- {ptable: gpt, path: /dev/sda, wipe: superblock-recursive, preserve: false, name: '',
grub_device: false, type: disk, id: disk-sda}
- {device: disk-sda, size: 536870912, wipe: superblock, flag: boot, number: 1,
preserve: false, grub_device: true, type: partition, id: partition-0}
- {fstype: fat32, volume: partition-0, preserve: false, type: format, id: format-0}
- {device: disk-sda, size: -1, wipe: superblock, flag: '', number: 2,
preserve: false, type: partition, id: partition-1}
- {fstype: ext4, volume: partition-1, preserve: false, type: format, id: format-1}
- {device: format-1, path: /, type: mount, id: mount-1}
- {device: format-0, path: /boot/efi, type: mount, id: mount-0}
3.5 配置TFTP
mkdir /tftp
mkdir /tftp/boot
mkdir /tftp/boot/live-server
mkdir /tftp/grub
见#2.1.4我们下载的uefi引导文件,使用
dpkg -x解压
dpkg -x grub-efi-amd64-signed_1.167.2+2.04-1ubuntu44.2_amd64.deb grub
dpkg -x shim-signed_1.40.7+15.4-0ubuntu9_amd64.deb shim
cp grub/usr/lib/grub/x86_64-efi-signed/grubnetx64.efi.signed /tftp/boot/live-server/grubx64.efi
cp shim/usr/lib/shim/shimx64.efi.signed /tftp/grub/bootx64.efi
拷贝内核镜像文件
cp /var/www/html/server/ubuntu2004/casper/vmlinuz /tftp/boot/live-server/
cp /var/www/html/server/ubuntu2004/casper/initrd /tftp/boot/live-server/
拷贝grub文件
cp /var/www/html/server/ubuntu2004/boot/grub/font.pf2 /tftp/grub/
cp /var/www/html/server/ubuntu2004/boot/grub/grub.cfg /tftp/grub/
chmod -R 777 /tftp/
配置grub.cfg
if loadfont /grub/font.pf2 ; then
set gfxmode=auto
insmod efi_gop
insmod efi_uga
insmod gfxterm
terminal_output gfxterm
fi
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
set timeout=5
menuentry "Autoinstall Ubuntu 20.04 Server" {
set gfxpayload=keep
linux /boot/live-server/vmlinuz root=/dev/ram0 ramdisk_size=1500000 ip=dhcp url='http://192.168.1.10/iso/ubuntu-20.04.3-live-server-amd64.iso' autoinstall ds=nocloud-net\;s=http://192.168.1.10/autoinstall/ ---
initrd /boot/live-server/initrd
}
root=/dev/ram0 ramdisk_size=1500000 为了指定内核镜像挂载空间,是否可删除待确认
ip=dhcp 指定内核镜像挂载后使用DHCP 获取IP地址
url= 指定ISO文件的网络存放路径
‘autoinstall ds=nocloud-net;s=http://192.168.1.9/autoinstall/ —’ 该配置指明参数自动填写,并指明配置文件所在路径
3.6 server端网络设置
3.6.1设置server静态ip
顶部栏右侧 -> 展开 有线连接 ->选择 有线设置
或者
编辑/etc/netplan/01-network-manager-all.yaml
# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
ethernets:
enp7s0:
dhcp4: yes
dhcp6: no
addresses: [192.168.1.10/24]
gateway4: 192.168.1.1
nameservers:
addresses: [114.114.114.114]
3.3.2 关闭防火墙
使用ufw disable关闭防火墙和ufw status 查看关闭状态是否OK
sudo ufw disable 防火墙在系统启动时自动禁用
sudo ufw status 查看状态
sudo ufw enable 开启防火墙,允许访问特定端口
3.8 进行PXE
client端对应网口设置成DHCP,并选择对应网口的网络启动
server端使用service dnsmasq status查看交互状态
结果如下:
本文参考:
https://segmentfault.com/a/1190000040527863
https://c-nergy.be/blog/?p=15664