CentOS 7: Install PXE Boot server for thin client with NFS

This article will run PXE Boot server with NFS for thin client, and run CentOS 7 on client which is connected to PXE Boot server via network. You do not need to install CentOS 7 on client.

1 Install PXE Boot server

The following script will install PXE Boot server. Change the variables to your environment.

  • SERVER_IPADDR is IP adress of PXE Boot server.
  • DHCP_SERVER_INTERFACESv4 is network interface name for DHCP. DHCP will be provided via this network interface.
  • DHCP_SUBNET is network address for DHCP.
  • DHCP_NETMASK is subnet for DHCP.
  • DHCP_DNS is DNS for DHCP.
  • DHCP_ROUTER is gateway for DHCP.
  • DHCP_CLIENT_HOSTNAME is hostname of client.
  • DHCP_CLIENT_IPADDR is IP address of client.
  • DHCP_CLIENT_MACADDR is MAC address of client.

NFS root filesystem is set to the following.

  • Set root user password to "centos".
  • Create user which name is "centos" and password is "centos".
#!/bin/sh

set -e

# Change the following variable to yours.
[ -z "${SERVER_IPADDR}" ] && \
  SERVER_IPADDR=$(hostname -I | awk '{ print $1 }')
[ -z "${DHCP_SUBNET}" ] && DHCP_SUBNET=192.168.11.0
[ -z "${DHCP_NETMASK}" ] && DHCP_NETMASK=255.255.255.0
[ -z "${DHCP_DOMAIN}" ] && DHCP_DOMAIN=hiroom2.com
[ -z "${DHCP_DNS}" ] && DHCP_DNS="192.168.11.2, 192.168.11.1"
[ -z "${DHCP_ROUTER}" ] && DHCP_ROUTER=192.168.11.1
[ -z "${DHCP_CLIENT_HOSTNAME}" ] && \
  DHCP_CLIENT_HOSTNAME=centos-7-pxeboot-client
[ -z "${DHCP_CLIENT_IPADDR}" ] && DHCP_CLIENT_IPADDR=192.168.11.254
[ -z "${DHCP_CLIENT_MACADDR}" ] && DHCP_CLIENT_MACADDR=52:54:00:5e:7a:a4

# The mirror.centos.org is slow. You should use your country mirror server.
# e.g.) MIRROR_URL=http://ftp.riken.jp/Linux/centos/7/os/x86_64
[ -z "${MIRROR_URL}" ] && \
  MIRROR_URL=http://mirror.centos.org/centos-7/7/os/x86_64

tftp_server_install()
{
  sudo yum install -y tftp-server
  sudo firewall-cmd --add-service=tftp --permanent
  sudo firewall-cmd --reload
  sudo systemctl enable tftp
  sudo systemctl restart tftp
}

dhcp_install()
{
  sudo yum install -y dhcp

  cat <<EOF | sudo tee /etc/dhcp/dhcpd.conf
subnet ${DHCP_SUBNET} netmask ${DHCP_NETMASK} {
  option domain-name "${DHCP_DOMAIN}";
  option domain-name-servers ${DHCP_DNS};
  option routers ${DHCP_ROUTER};
  next-server ${SERVER_IPADDR};
  filename "pxelinux.0";
}

host ${DHCP_CLIENT_HOSTNAME} {
  hardware ethernet ${DHCP_CLIENT_MACADDR};
  fixed-address ${DHCP_CLIENT_IPADDR};
}
EOF

  sudo firewall-cmd --add-service=dhcp --permanent
  sudo firewall-cmd --reload
  sudo systemctl enable dhcpd
  sudo systemctl restart dhcpd
}

nfs_utils_install()
{
  sudo yum install -y nfs-utils
  sudo firewall-cmd --add-service=nfs --permanent
  sudo firewall-cmd --reload
  sudo systemctl enable nfs
  sudo systemctl restart nfs

  sudo mkdir /var/lib/nfsroot
  cat <<EOF | sudo tee /etc/exports
/var/lib/nfsroot *(rw,sync,no_root_squash,no_subtree_check)
EOF
  sudo exportfs -ra

  # Create root filesystem.
  pkgs="
@^gnome-desktop-environment
@base
@core
@desktop-debugging
@development
@dial-up
@directory-client
@fonts
@gnome-apps
@gnome-desktop
@guest-agents
@guest-desktop-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@networkmanager-submodules
@print-client
@x11
"
  # shellcheck disable=SC2086
  sudo yum install -y --releasever=7 --installroot=/var/lib/nfsroot ${pkgs}

  # https://bugzilla.redhat.com/show_bug.cgi?id=1374427
  sudo restorecon /var/lib/nfsroot/etc/passwd* /var/lib/nfsroot/usr/bin/passwd

  sudo systemd-nspawn -D /var/lib/nfsroot sh -c "
# Set root password and create user.
yes centos | passwd
useradd -m -s /bin/bash centos
yes centos | passwd centos

# Locale setting.
echo LANG=\"en_US.UTF-8\" | tee /etc/locale.conf
"
}

syslinux_install()
{
  sudo yum install -y syslinux

  cd /var/lib/tftpboot
  sudo cp /usr/share/syslinux/pxelinux.0 .
  sudo cp -a /usr/share/syslinux .

  sudo wget -q ${MIRROR_URL}/isolinux/initrd.img
  sudo wget -q ${MIRROR_URL}/isolinux/vmlinuz

  sudo mkdir pxelinux.cfg
  cat <<EOF | sudo tee pxelinux.cfg/default
path syslinux
include menu.cfg
default syslinux/vesamenu.c32
prompt 0
timeout 10
EOF

  cat <<EOF | sudo tee menu.cfg
menu hshift 13
menu width 49
menu margin 8
menu tabmsg

menu title Thin client boot menu
label centos-7-thin-client
  menu label ^CentOS 7 thin client
  kernel vmlinuz
  append vga=788 initrd=initrd.img ip=dhcp \
    root=/dev/nfs nfsroot=${SERVER_IPADDR}:/var/lib/nfsroot rw selinux=0
menu end
EOF
}

pxeboot_main()
{
  tftp_server_install
  dhcp_install
  nfs_utils_install
  syslinux_install
}

pxeboot_main

2 Run CentOS 7 thin client with NFS

This article runs CentOS 7 thin client on virtual machine on KVM. Virtual machine on VirtualBox and real machine too can run CentOS 7 thin client with enabling network boot. Please check your BIOS setting.

Enable "NIC" of "Boot device order" and make it top of order with virt-manager.

0001_BootDeviceOrder.png

iPXE sends DHCP requests, receives DHCP response from PXE Boot server, download and run boot image.

0002_iPXE.png

syslinux's menu is displayed. After 1 second, "CentOS 7 thin client" will be selected automatically.

0003_syslinux.png

CentOS 7 thin client is started.

0004_BootCentos.png

Login is displayed.

0005_Login.png