PXEサーバ構築

PXEサーバをCentos7で構築する。

基本的にはRed Hatのオンラインドキュメント通り。 第23章 ネットワークからのインストールの準備 Red Hat Enterprise Linux 7 | Red Hat Customer Portal

# yum install dhcp tftp-server xinetd httpd

/etc/xinetd/tftpを編集し、tftpサーバがxinetd経由で制御されるようにする。

# /etc/xinetd.d/tftp
#
# default: off
# description: The tftp server serves files using the trivial file transfer \
#       protocol.  The tftp protocol is often used to boot diskless \
#       workstations, download configuration files to network-aware printers, \
#       and to start the installation process for some operating systems.
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s <span style="color: #ff0000">-v</span> /var/lib/tftpboot  # add -v so it logs i
n /var/log/message
        disable                 = <span style="color: #ff0000">no</span>   # change "yes" to "no"
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}

/etc/xinetd/tftpの編集が終わったら、xinetdを起動して自動実行を設定し、chkconfigでtftpがxinetdで制御されるようにになっていることを確認し、ファイアウォールを通しておく。

# systemctl start xinetd
# systemctl enable xinetd
# chkconfig

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off

xinetd based services:
    chargen-dgram:  off
    chargen-stream: off
    daytime-dgram:  off
    daytime-stream: off
    discard-dgram:  off
    discard-stream: off
    echo-dgram:     off
    echo-stream:    off
    tcpmux-server:  off
    tftp:           on
    time-dgram:     off
    time-stream:    off
#
# firewall-cmd --permanent --add-service=tftp
success
# firewall-cmd --reload
success

httpでisoイメージを配布するようにする。複数のOSを配布できるようにするのでhttpルート配下にisoフォルダを作成し、各OSのisoのマウントポイントを用意する。 リブートしてもisoが自動マウントされるよう、/etc/fstabや/etc/rc.d/rc.localに追記しておくと良い。rc.localに追記した場合はファイルに実行権限を足しておく。

# mkdir -p /var/www/html/iso/centos7
# mount -t iso9660 -o loop /path/to/iso /var/www/html/iso/centos7
#
# echo "mount -t iso9660 -o loop /path/to/iso /var/www/html/iso/centos7/" >> /etc/rc.d/rc.local 
# chmod +x /etc/rc.d/rc.local 
# 
# systemctl start httpd
# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
# firewall-cmd --permanent --add-service=http
success
# firewall-cmd --reload
success

dhcpサーバの設定ファイルを編集して起動、自動実行を設定し、ファイアウォールを通しておく。PXEクライアントのブート用ファイルは以下に配置する。

  • レガシーBIOSブート: /var/lib/tftpboot/legacy

  • UEFIブート: /var/lib/tftpboot/uefi

# vi /etc/dhcp/dhcpd.conf
#

# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.example
#   see dhcpd.conf(5) man page
#
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;

subnet x.x.x.0 netmask 255.255.255.0 {
        option routers x.x.x.x;
        range y.y.y.y y.y.y.z;

        class "pxeclients" {
          match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
          next-server w.w.w.w;

          if option architecture-type = 00:07 {
            filename "uefi/shim.efi";
            } else {
            filename "legacy/pxelinux.0";
          }
        }
}

# systemctl start dhcpd
# systemctl enable dhcpd
Created symlink from /etc/systemd/system/multi-user.target.wants/dhcpd.service to /usr/lib/systemd/system/dhcpd.service.
# firewall-cmd --permanent --add-service=dhcp
success
# firewall-cmd --reload
success

/var/lib/tftpboot配下にlegacyとuefiブート用のフォルダをそれぞれ用意し、起動に必要なファイルをisoから抜き取ってくる。ここで実行しているのはuefiブートに必要なshim.efi、grubx64.efiの配置。tftpサーバがアクセスできるよう、一般ユーザーに読み取り権限を与えること。

また、複数のOSを配布できるようにするので、/legacyと/uefi配下に各OSのフォルダを作成し、カーネルとinitirdはそこに配置する。tftpサーバがアクセスできるよう、一般ユーザーの読み取り権限を与えること。

# mkdir /var/lib/tftpboot/legacy
# mkdir /var/lib/tftpboot/uefi
#cd /var/lib/tftpboot/uefi/
# 
# rpm2cpio /var/www/html/iso/centos7/Packages/shim-x64-15-2.el7.centos.x86_64.rpm | cpio -dimv
./boot/efi/EFI/BOOT/BOOTX64.EFI
./boot/efi/EFI/BOOT/fallback.efi
./boot/efi/EFI/BOOT/fbx64.efi
./boot/efi/EFI/centos/BOOT.CSV
./boot/efi/EFI/centos/BOOTX64.CSV
./boot/efi/EFI/centos/MokManager.efi
./boot/efi/EFI/centos/mmx64.efi
./boot/efi/EFI/centos/shim.efi
./boot/efi/EFI/centos/shimx64-centos.efi
./boot/efi/EFI/centos/shimx64.efi
15305 blocks
# rpm2cpio /var/www/html/iso/centos7/Packages/grub2-efi-x64-2.02-0.80.el7.centos.x86_64.rpm | cpio -dimv
./boot/efi/EFI/centos
./boot/efi/EFI/centos/fonts
./boot/efi/EFI/centos/fonts/unicode.pf2
./boot/efi/EFI/centos/grubx64.efi
./boot/grub2/grubenv
./etc/grub2-efi.cfg
7133 blocks
# ls
boot  etc
#
# cp boot/efi/EFI/centos/shim.efi .
# cp boot/efi/EFI/centos/grubx64.efi .
# rm -rf boot/
# rm -rf etc/
#
# mkdir centos7
# cp /var/www/html/iso/centos7/images/pxeboot/initrd.img centos7/
# cp /var/www/html/iso/centos7/images/pxeboot/vmlinuz centos7/
#
# chmod 704 *
# ls -l
total 2252
drwx---r--. 2 root root      39 Nov 27 17:27 centos7
-rwx---r--. 1 root root     159 Nov 27 17:24 grub.cfg
-rwx---r--. 1 root root 1090976 Nov 27 15:58 grubx64.efi
-rwx---r--. 1 root root 1205248 Nov 27 15:58 shim.efi
#
# chmod 704 centos7/*
# ls -l centos7/
total 60364
-rwx---r--. 1 root root 55073584 Nov 27 15:59 initrd.img
-rwx---r--. 1 root root  6734016 Nov 27 15:59 vmlinuz
#  

PXEクライアントがブートできるようgrub.cfgを/var/lib/tftpboot/uefi/に作成する。内容は下記。

set timeout=60
menuentry 'centos7' {
  linuxefi uefi/centos7/vmlinuz ip=dhcp inst.repo=http://w.w.w.w/iso/centos7
  initrdefi uefi/centos7/initrd.img
}

/var/lib/tftpboot配下のフォルダツリー

# cd /var/lib/tftpboot/
# tree
.
├── legacy
└── uefi
    ├── centos7
    │   ├── initrd.img
    │   └── vmlinuz
    ├── grub.cfg
    ├── grubx64.efi
    └── shim.efi

3 directories, 5 files

ここまでくれば、PXEクライアントからブートできる。 f:id:mailstop:20191128104229p:plain