pxe使用GRUB作为引导的方法

安装上面这些软件包

1
dnf install dhcp-server tftp-server grub2-efi-x64-modules grub2-tools-extra grub2-pc-modules -y

配置dhcp在/etc/dhcp/dhcpd.conf,增加下面配置 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
option pxe-system-type code 93 = unsigned integer 16;
option rfc3442-classless-static-routes code 121 = array of integer 8;
option ms-classless-static-routes code 249 = array of integer 8;

option space pxelinux;
option pxelinux.magic code 208 = string;
option pxelinux.configfile code 209 = text;
option pxelinux.pathprefix code 210 = text;
option pxelinux.reboottime code 211 = unsigned integer 32;
option architecture-type code 93 = unsigned integer 16;
option pxelinux.mtftp-ip code 1 = ip-address;
option pxelinux.mtftp-cport code 2 = unsigned integer 16;
option pxelinux.mtftp-sport code 3 = unsigned integer 16;
option pxelinux.mtftp-tmout code 4 = unsigned integer 8;
option pxelinux.mtftp-delay code 5 = unsigned integer 8;

在subnet { } 里面添加这些内容

subnet 0.0.0.0 mask 255.255.255.0 {

xxxxxx

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class "pxeclients" {
match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
# x86_64 EFI
if option pxe-system-type = 00:07 {
filename "boot/grub2/x86_64-efi/core.efi";
} else if option pxe-system-type = 00:08 {
filename "boot/grub2/x86_64-efi/core.efi";
} else if option pxe-system-type = 00:09 {
filename "boot/grub2/x86_64-efi/core.efi";
} else {
# BIOS boot only
filename "boot/grub2/i386-pc/core.0";
}
}

合并配置大概就是这样的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
ddns-update-style interim;

allow booting;
allow bootp;
authoritative;
log-facility local6;

ignore client-updates;
set vendorclass = option vendor-class-identifier;

## Allowing EFI Clients
option pxe-system-type code 93 = unsigned integer 16;
option rfc3442-classless-static-routes code 121 = array of integer 8;
option ms-classless-static-routes code 249 = array of integer 8;

option space pxelinux;
option pxelinux.magic code 208 = string;
option pxelinux.configfile code 209 = text;
option pxelinux.pathprefix code 210 = text;
option pxelinux.reboottime code 211 = unsigned integer 32;
option architecture-type code 93 = unsigned integer 16;

option pxelinux.mtftp-ip code 1 = ip-address;
option pxelinux.mtftp-cport code 2 = unsigned integer 16;
option pxelinux.mtftp-sport code 3 = unsigned integer 16;
option pxelinux.mtftp-tmout code 4 = unsigned integer 8;
option pxelinux.mtftp-delay code 5 = unsigned integer 8;

subnet 10.100.0.0 netmask 255.255.255.0 {
interface eth0;
# option routers 10.100.0.1;
option domain-name-servers 10.100.0.1, 10.100.0.231;
# option domain-name "angelsofclockwork.net";
option subnet-mask 255.255.255.0;
range 10.100.0.110 10.100.0.199;
## EFI Client Catch
class "pxeclients" {
match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
if option pxe-system-type = 00:07 {
filename "grub/x86_64-efi/core.efi";
} else if option pxe-system-type = 00:08 {
filename "grub/x86_64-efi/core.efi";
} else if option pxe-system-type = 00:09 {
filename "grub/x86_64-efi/core.efi";
# } else if option pxe-system-type = 00:0a {
# filename "boot/grub2/armv7a-efi/core.efi";
# } else if option pxe-system-type = 00:0b {
# filename "boot/grub2/aarch64-efi/core.efi";
} else {
filename "grub/i386-pc/core.0";
}
}
default-lease-time 21600;
max-lease-time 43200;
next-server 10.100.0.1;
}
# Ensure that the dhcpd service is rest

在tftp目录生成grub引导文件

1
grub2-mknetdir  --net-directory=/var/lib/tftpboot --subdir=grub

生成grub.cfg配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
nano /var/lib/tftpboot/grub/grub.cfg
set default=0
set timeout=60
menuentry 'EFI Firmware System Setup' $menuentry_id_option 'uefi-firmware' {
fwsetup
}

menuentry 'Reboot' {
reboot
}

menuentry 'Shutdown' {
halt
}

建立好文件引用关系,将bios和efi配置文件指向一个

mv /var/lib/tftpboot/grub/i386-pc/grub.cfg /var/lib/tftpboot/grub/i386-pc/grub.cfg.bk

mv /var/lib/tftpboot/grub/x86_64-efi/grub.cfg /var/lib/tftpboot/grub/x86_64-efi/grub.cfg.bk

ln -s /var/lib/tftpboot/grub/grub.cfg /var/lib/tftpboot/grub/i386-pc/grub.cfg

ln -s /var/lib/tftpboot/grub/grub.cfg /var/lib/tftpboot/grub/x86_64-efi/grub.cfg

启动服务并测试

systemctl start dhcpd tftp-server

这篇文章参考了

https://linuxguideandhints.com/el/pxeboot.html

https://wiki.ubuntu.com/UEFI/SecureBoot/PXE-IPv6

pxe boot uefi for ubuntu

https://cloud.189.cn/web/share?code=fEZB3qaAvMr2(访问码:zuc6)