PXE 安装ubuntu22.04自动判断UEFI或者Legacy引导

news/2025/2/26 15:26:31

UEFI引导安装:https://blog.csdn.net/qq_50247813/article/details/145777563
Legacy引导安装:https://blog.csdn.net/qq_50247813/article/details/145730754

本篇根据上两篇快速部署pxe服务器,并自动判断uefi或者legacy引导

一、服务器必要软件下载

root@pxeserver:~# apt-get install dnsmasq -y
root@pxeserver:~# apt-get install apache2 -y

二、dnsmasq配置修改

将uefi和Legacy 引导文件都加上去 , tftp-root=/srv/tftp 目录需要自己创建
mkdir -p /srv/tftp

root@local:/srv/tftp# cat /etc/dnsmasq.d/pxe.conf
port=0
interface=ens33
bind-interfaces
dhcp-range=ens33,192.168.25.100,192.168.25.200,255.255.255.0,12h
dhcp-boot=pxelinux.0
dhcp-match=set:efi-x86_64,option:client-arch,7
dhcp-boot=tag:efi-x86_64,bootx64.efi
enable-tftp
tftp-root=/srv/tftp

重启dnsmasq

root@pxeserver:~# systemctl restart dnsmasq

三、上传到iso镜像到web目录,并创建uefi 和 Legacy 的 autoinstall 文件

root@pxeserver:/var/www/html# mkdir legacy-autoinstall  uefi-autoinstall iso
root@pxeserver:/var/www/html# touch uefi-autoinstall/meta-data legacy-autoinstall/meta-data
root@pxeserver:/var/www/html# cat uefi-autoinstall/user-data
#cloud-config
autoinstall:
  version: 1
  # 设置语言
  locale: en_US.UTF-8
  # 设置时区
  timezone: Asia/Shanghai
  keyboard:
    layout: us
  # 安装ubuntu服务器版,最小化选择 ubuntu-mini
  source:
    id: ubuntu-server
    search_drivers: false
  # 网络配置,根据具体接口名称修改。
  network:
    version: 2
    ethernets:
      ens33: {dhcp4: true}
  # 是否安装第三方驱动
  drivers:
    install: false
  # 内核安装,linux-generic 通用内核
  kernel:
    package: linux-generic
  # 设置存储库,这里使用阿里云
  apt:
    fallback: offline-install
    mirror-selection:
      primary:
      - arches: [amd64]
        uri: http://mirrors.aliyun.com/ubuntu/
      - arches: [arm64]
        uri: http://mirrors.aliyun.com/ubuntu-ports
    preserve_sources_list: false
  # 配置存储,分区
  storage:
    config:
    - {ptable: gpt, path: /dev/sda, wipe: superblock-recursive, preserve: false, name: '', grub_device: false, type: disk, id: disk-sda}
    - {device: disk-sda, size: 300M, wipe: superblock, flag: boot , number: 1, preserve: false, grub_device: true, type: partition, id: partition-0}
    - {device: disk-sda, size: 300M, wipe: superblock, number: 2, preserve: false, type: partition, id: partition-1}
    - {device: disk-sda, size: -1 , wipe: superblock, number: 3, preserve: false, type: partition, id: partition-2}
    - {fstype: fat32, volume: partition-0, preserve: false, type: format, id: format-0}
    - {fstype: ext4, volume: partition-1, preserve: false, type: format, id: format-1}
    - {fstype: ext4, volume: partition-2, preserve: false, type: format, id: format-2}
    - {device: format-0, path: /boot/efi, type: mount, id: mount-0}
    - {device: format-1, path: /boot, type: mount, id: mount-1}
    - {device: format-2, path: /, type: mount, id: mount-2}
  # 配置账号信息
  identity:
    hostname: ubuntu
    username: ubuntu
    password: $6$t0CWYjTG8Nyq8pNv$2Gws/1V7ImPm/Pc8i6.KZU.epvF.wAyHuwrgbTj.pCD/smAgELHCfNYcdx6r/MKQkZgQyjMXRyYntgFRqxPUY.
  # ssg 配置
  ssh:
    allow-pw: true
    install-server: true
  # 软件安装
  packages:
    - vim
    - git
  # 重启前要运行的命令,curtin in-target --target=/target -- $shell_commandcurtin 格式
  late-commands:
    - curtin in-target --target=/target -- apt-get -y update
  
root@pxeserver:/var/www/html# cat legacy-autoinstall/user-data
#cloud-config
autoinstall:
  version: 1
  # 设置语言
  locale: en_US.UTF-8
  # 设置时区
  timezone: Asia/Shanghai
  keyboard:
    layout: us
  # 安装ubuntu服务器版,最小化选择 ubuntu-mini
  source:
    id: ubuntu-server
    search_drivers: false
  # 网络配置,根据具体接口名称修改。
  network:
    version: 2
    ethernets:
      ens33: {dhcp4: true}
  # 是否安装第三方驱动
  drivers:
    install: false
  # 内核安装,linux-generic 通用内核
  kernel:
    package: linux-generic
  # 设置存储库,这里使用阿里云
  apt:
    fallback: offline-install
    mirror-selection:
      primary:
      - arches: [amd64]
        uri: http://mirrors.aliyun.com/ubuntu/
      - arches: [arm64]
        uri: http://mirrors.aliyun.com/ubuntu-ports
    preserve_sources_list: false
  # 配置存储,分区
  storage:
    config:
    - {ptable: gpt, path: /dev/sda, wipe: superblock-recursive, preserve: false, name: '', grub_device: true,type: disk, id: disk-sda}
    - {device: disk-sda, size: 1M, flag: bios_grub, number: 1, preserve: false,type: partition, id: partition-0}
    - {device: disk-sda, size: 300M, wipe: superblock, flag: '', number: 2,preserve: false, type: partition, id: partition-1}
    - {device: disk-sda, size: -1, wipe: superblock, flag: '', number: 3, preserve: false, type: partition, id: partition-2}
    - {fstype: ext4, volume: partition-1, preserve: false, type: format, id: format-0}
    - {fstype: ext4, volume: partition-2, preserve: false, type: format, id: format-1}
    - {device: format-0, path: /boot, type: mount, id: mount-0}
    - {device: format-1, path: /, type: mount, id: mount-1}
  # 配置账号信息
  identity:
    hostname: ubuntu
    username: ubuntu
    password: $6$t0CWYjTG8Nyq8pNv$2Gws/1V7ImPm/Pc8i6.KZU.epvF.wAyHuwrgbTj.pCD/smAgELHCfNYcdx6r/MKQkZgQyjMXRyYntgFRqxPUY.
  # ssg 配置
  ssh:
    allow-pw: true
    install-server: true
  # 软件安装
  packages:
    - vim
    - git
  # 重启前要运行的命令,curtin in-target --target=/target -- $shell_commandcurtin 格式
  late-commands:
    - curtin in-target --target=/target -- apt-get -y update
 

四、将Legacy引导所需文件 复制到 tftp目录下

1. 获取内核和镜像文件

root@pxeserver:/var/www/html# mount -o loop /var/www/html/iso/ubuntu-22.04.5-live-server-amd64.iso  /mnt
root@pxeserver:/var/www/html# cp /mnt/casper/{vmlinuz,initrd} /srv/tftp/

2. 获取引导文件

从 Syslinux 官方网站下载 pxelinux.0 文件
Syslinux 官网地址:https://www.syslinux.org
下载地址:https://www.kernel.org/pub/linux/utils/boot/syslinu

pxelinux.0文件的位置在 syslinux-6.03/bios/core/pxelinux.0

root@pxeserver:~# wget https://www.kernel.org/pub/linux/utils/boot/syslinux/syslinux-6.03.tar.gz
root@pxeserver:~# tar -zxvf syslinux-6.03.tar.gz
root@pxeserver:~# cp syslinux-6.03/bios/core/pxelinux.0 /srv/tftp/

3. 创建引导配置文件

注意自己的web服务器地址,自动安装目录为 legacy-autoinstall

root@pxeserver:~# cd /srv/tftp/
root@pxeserver:/srv/tftp# mkdir -p pxelinux.cfg
root@pxeserver:/srv/tftp# cat  pxelinux.cfg/default
DEFAULT menu.c32
MENU TITLE PXE Boot Menu
PROMPT 0
TIMEOUT 30

LABEL Ubuntu 22.04 BIOS Install
    MENU LABEL Install Ubuntu 22.04 (BIOS)
    KERNEL vmlinuz
    INITRD initrd
    APPEND root=/dev/ram0 ramdisk_size=1024 ip=dhcp url=http://192.168.25.144/iso/ubuntu-22.04.5-live-server-amd64.iso autoinstall ds=nocloud-net;s=http://192.168.25.144/legacy-autoinstall/ cloud-config-url=/dev/null

4. 拷贝Syslinux 引导加载程序中的关键文件到tftp目录

root@pxeserver:~# apt-get install syslinux-common -y
root@pxeserver:~# cp /usr/lib/syslinux/modules/bios/{ldlinux.c32,libutil.c32,menu.c32} /srv/tftp

五、 将UEFI引导文件、内核、镜像放到tftp工作目录下

1. 获取内核和镜像文件

复用Legacy的内核和镜像文件

2. 获取引导文件

# bootx64.efi
root@pxeserver:~# apt download shim-signed
root@pxeserver:~# dpkg -x shim-signed*deb shim
root@pxeserver:~# cp shim/usr/lib/shim/shimx64.efi.signed.latest /srv/tftp/bootx64.efi
# grubx64.efi
root@pxeserver:~# apt download grub-efi-amd64-signed
root@pxeserver:~# dpkg -x grub-efi-amd64-signed*deb grub
root@pxeserver:~# sudo cp grub/usr/lib/grub/x86_64-efi-signed/grubnetx64.efi.signed /srv/tftp/grubx64.efi

3. GRUB 还需要一个字体通过tftp获取

root@pxeserver:~# apt download grub-common
root@pxeserver:~# dpkg -x grub-common*deb grub-common
root@pxeserver:~# cp grub-common/usr/share/grub/unicode.pf2 /srv/tftp/

4. 创建引导配置文件

UEFI 引导配置文件 ds=nocloud-net;s=http://192.168.25.144/autoinstall/ cloud-config-url=/dev/null 需要转义一下。自动安装目录为uefi-autoinstall

root@pxeserver:~# cd /srv/tftp/
root@pxeserver:/srv/tftp# mkdir grub
root@pxeserver:/srv/tftp# cat grub/grub.cfg
default=0
set timeout=3

if loadfont unicode ; then
  set gfxmode=auto
  set locale_dir=$prefix/locale
  set lang=en_US
fi
terminal_output gfxterm

if background_color 44,0,30; then
  clear
fi

set menu_color_normal=white/black
set menu_color_highlight=black/light-gray

menuentry "Ubuntu server 22.04 autoinstall" {
        set gfxpayload=keep
        # 修改为实际地址
        linux  vmlinuz root=/dev/ram0 ramdisk_size=1024 ip=dhcp url=http://192.168.25.144/iso/ubuntu-22.04.5-live-server-amd64.iso autoinstall ds=nocloud-net\;s=http://192.168.25.144/uefi-autoinstall/ cloud-config-url=/dev/null ---
        initrd initrd
}

最后查看一下tftp目录结构

root@pxeserver:/srv/tftp# tree
.
├── bootx64.efi
├── grub
│   └── grub.cfg
├── grubx64.efi
├── initrd
├── ldlinux.c32
├── libutil.c32
├── menu.c32
├── pxelinux.0
├── pxelinux.cfg
│   └── default
└── vmlinuz

2 directories, 10 files

六、客户端测试

已测试,客户端内存最少需要4G!

自行测试。


http://www.niftyadmin.cn/n/5868884.html

相关文章

C/C++后端开发面试表述、技术点摸底——基础组件篇

前端时间笔者系统学习了基础组件中的池式结构(包括线程池、内存池、连接池),原子操作,锁,无锁队列,网络缓冲区,定时器设计,分布式锁,有些内容笔者已经总结整理成了相关技…

linux centos8 安装redis 卸载redis

准备环境 系统:linux CentOS8 安装步骤 一、下载redis 1.进入官网找到下载地址 https://redis.io/download 2.右键点击复制链接地址 3.进入到Xshell控制台(默认当前是root根目录),,输入wget 加你复制的地址 (示例 &#xff…

爱普生汽车用显示控制器IC:ScalerIC,汽车接口IC,相机接口IC

爱普生汽车显示控制器IC,汽车显示控制器芯片可以分为三类:爱普生显示控制芯片Scaler IC ,爱普生汽车接口IC,爱普生相机接口IC。下面就给大家分别介绍下这三类芯片的具体型号的特征及用途。 爱普生显示控制芯片 Scaler IC Scaler…

在 JMeter 中使用 Python 脚本

JMeter本身基于Java开发,但可以借助JSR223元件编写Python脚本实现更灵活的功能,以下为你详细介绍具体实现方式: 1. 配置Python环境 在使用Python脚本前,需要确保你的系统已经安装了Python环境,同时还需要安装jpype1库,它允许Python程序调用Java代码,这对于在JMeter中使…

DeepSeek-R1 满血版和蒸馏版鉴别方法

大家好,我是herosunly。985院校硕士毕业,现担任算法研究员一职,热衷于大模型算法的研究与应用。曾担任百度千帆大模型比赛、BPAA算法Q大赛评委,编写微软OpenAI考试认证指导手册。曾获得阿里云天池比赛第一名,CCF比赛第二名,科大讯飞比赛第名。授权多项发明专利。对机器学…

《Flink学习攻略:从入门到进阶的奇妙之旅》

《Flink学习攻略:从入门到进阶的奇妙之旅》 嘿,各位小伙伴!今天咱们来聊聊怎么学习Flink这个在大数据领域超火的玩意儿。相信很多小伙伴都对它感兴趣,但又不知道从哪儿下手,别担心,咱一步步来。 一、学习…

Redisson 分布式锁底层原理实现详解

1、概述 Redisson 是基于 Redis 实现的分布式锁,其核心思想是利用 Redis 的 SET NX(SET if Not eXists) PX(过期时间) 来实现锁的互斥性,同时通过 Lua 脚本 处理加锁、解锁、续期等原子操作,保证分布式环境下的安全性…

idea创建第一个springboot程序

说明: 我计划用idea,创建第一个springboot程序,但是作为新手完全不会弄,今天我就亲自尝试一边,并且出一期详细,完美的教程,亲测可以运行 step1. 点击file , 选new, 选…