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

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!

自行测试。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值