averainy's Blog

averainy

16 Oct 2024

Build Your Own PXE Boot Server on Fedora

Build your own PXE boot server on Fedora

Background

通过这篇文章设置可以从BIOS和UEFI启动PXE网络安装操作系统。

安装前提

  1. 假设PXE服务器的ip地址是10.0.0.1

安装步骤

安装软件

  1. 更新PXE启动服务器到最新版本

    [sysadmin@pxeboot-server ~]$ sudo dnf update -y
    
  2. 安装必要软件包

    [sysadmin@pxeboot-server ~]$ dnf install -y ipxe-bootimgs dnsmasq 
    
  3. 创建 TFTP root 目录

    [sysadmin@pxeboot-server ~]$ mkdir /tftpboot
    

    (可选项) 如果SELinux启动的话,设置安全标签

    [sysadmin@pxeboot-server ~]$ sudo chcon -t tftpdir_t /tftpboot
    
  4. 拷贝iPXE启动镜像到TFTP目录

    [sysadmin@pxeboot-server ~]$ sudo cp /usr/share/ipxe/{undionly.kpxe,ipxe*.efi} /tftpboot/
    
  5. 创建iPXE菜单目录和启动菜单

    $ sudo mkdir /tftpboot/menu
    

    创建一个启动到IPXE shell的菜单

    sudo vi /tftpboot/menu/boot.ipxe
    

    内容 :

    #!ipxe
    
    menu PXE Boot Options
    
    item shell iPXE shell
    item exit  Exit to BIOS
    
    choose --default exit --timeout 10000 option && goto ${option}
    
    :shell
    shell
    
    :exit
    exit
    
  6. 创建dnsmasq配置文件 /etc/dnsmasq.conf. 编辑前删除此文件中的原有内容.

    # enable logs if required
    #log-queries
    #log-dhcp
    
    # disable DNS server
    port=0
    
    # listen on PXEBOOT vlan (vlan110) only
    listen-address=10.0.0.1
    interface=eth1
    
    # enable built-in tftp server
    enable-tftp
    tftp-root=/tftpboot
    
    
    # DHCP range 10.0.0.200 ~ 10.0.0.250
    dhcp-range=10.0.0.200,10.0.0.250,255.255.255.0,24h
    
    # Default gateway
    dhcp-option=3,10.0.0.1
    
    # Domain name - homelab.net
    dhcp-option=15,homelab.net
    
    # Broadcast address
    dhcp-option=28,10.0.0.255
    
    # Set interface MTU to 9000 bytes (jumbo frame)
    # Enable only when your network supports it
    # dhcp-option=26,9000
    
    # Tag dhcp request from iPXE
    dhcp-match=set:ipxe,175
    
    # inspect the vendor class string and tag BIOS client
    dhcp-vendorclass=BIOS,PXEClient:Arch:00000
    
    # 1st boot file - Legacy BIOS client
    dhcp-boot=tag:!ipxe,tag:BIOS,undionly.kpxe,10.1.0.1
    
    # 1st boot file - EFI client
    # at the moment all non-BIOS clients are considered
    # EFI client
    dhcp-boot=tag:!ipxe,tag:!BIOS,ipxe.efi,10.1.0.1
    
    # 2nd boot file
    dhcp-boot=tag:ipxe,menu/boot.ipxe
    
  7. 添加防火墙规则

    $ sudo firewall-cmd --add-service=dhcp --permanent
    $ sudo firewall-cmd --add-service=tftp --permanent
    $ sudo firewall-cmd --add-service=dns --permanent
    $ sudo firewall-cmd --reload
    
  8. 启动 dnsmasq 并确保其运行

    $ sudo systemctl start dnsmasq
    $ sudo systemctl status dnsmasq
    ● dnsmasq.service - DNS caching server.
       Loaded: loaded (/usr/lib/systemd/system/dnsmasq.service; disabled; vendor preset: disabled)
       Active: active (running) since Sat 2018-06-09 12:01:23 AWST; 6min ago
     Main PID: 24141 (dnsmasq)
       CGroup: /system.slice/dnsmasq.service
               └─24141 /usr/sbin/dnsmasq -k
    
  9. 现在你可以启动客户端,选择从网络启动应该能看到启动菜单

添加CentOS 7 网络安装选项

  1. 安装Apache或其他你想需要的web服务.

    $ yum install -y httpd
    
  2. 再Apache的默认文档目录创建media子目录并挂载CentOS 7 ISO镜像.

    $ sudo mkdir -p /var/www/html/media/centos7
    
    $ sudo mount -t iso9660 -o ro,loop \
    /tmp/CentOS-7-x86_64-Minimal-1804.iso /var/www/html/media/centos7
    
  3. 更新防火墙规则并启动web 服务

    $ sudo firewall-cmd --add-service=http --permanent
    $ sudo firewall-cmd --reload
    
    $ sudo systemctl start httpd
    
    $ sudo systemctl status httpd
    ● httpd.service - The Apache HTTP Server
       Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
       Active: active (running) since Sat 2018-06-09 14:31:20 AWST; 3h 47min ago
         Docs: man:httpd(8)
               man:apachectl(8)
     Main PID: 1434 (httpd)
       Status: "Total requests: 660; Current requests/sec: 0; Current traffic:   0 B/sec"
       CGroup: /system.slice/httpd.service
               ├─1434 /usr/sbin/httpd -DFOREGROUND
               ├─1435 /usr/sbin/httpd -DFOREGROUND
               ├─1436 /usr/sbin/httpd -DFOREGROUND
               ├─1437 /usr/sbin/httpd -DFOREGROUND
               ├─1438 /usr/sbin/httpd -DFOREGROUND
               ├─1439 /usr/sbin/httpd -DFOREGROUND
               ├─1489 /usr/sbin/httpd -DFOREGROUND
               ├─1731 /usr/sbin/httpd -DFOREGROUND
               ├─1732 /usr/sbin/httpd -DFOREGROUND
               └─1878 /usr/sbin/httpd -DFOREGROUND
    
  4. 添加CentOS安装选项到 /tftpboot/menu/boot.ipxe 文件:

    #!ipxe
    
    :start
    menu PXE Boot Options
    item shell iPXE shell
    item centos7-net CentOS 7 installation
    item exit  Exit to BIOS
    
    choose --default centos7-net --timeout 10000 option && goto ${option}
    
    :shell
    shell
    
    :centos7-net
    set server_root http://10.0.0.1/media/centos7
    initrd ${server_root}/images/pxeboot/initrd.img
    kernel ${server_root}/images/pxeboot/vmlinuz inst.repo=${server_root}/ ip=dhcp ipv6.disable initrd=initrd.img inst.geoloc=0 devfs=nomount
    boot
    
    :exit
    exit
    
  5. 现在重启客户端机器,你应该能看到Centos的选项。

Categories