Debian 9: Install PXE Boot server for automated install

This article will run PXE Boot server for automated install, and automatically install Debian 9 on client which is connected to PXE Boot server via network.

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.
  • PRESEED_LOCALE is locale for Preseed.
  • PRESEED_LANGUAGE is language for Preseed.
  • PRESEED_COUNTRY is location for Preseed.
  • PRESEED_KEYMAP is keyboard layout for Preseed.
  • PRESEED_MIRROR is mirror server FQDN for Preseed.
  • PRESEED_TIMEZONE is time zone for Preseed.

Preseed runs the following.

  • Set root user password to "debian".
  • Create user which name is "debian" and password is "debian".
  • Install task-gnome-desktop package and openssh-server package.
#!/bin/sh

set -e

# Change the following variable to yours.
[ -z "${SERVER_IPADDR}" ] && \
  SERVER_IPADDR=$(hostname -I | awk '{ print $1 }')
[ -z "${DHCP_SERVER_INTERFACESv4}" ] && DHCP_SERVER_INTERFACESv4=ens3
[ -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=debian-9-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
[ -z "${PRESEED_LOCALE}" ] && PRESEED_LOCALE=en_US
[ -z "${PRESEED_LANGUAGE}" ] && PRESEED_LANGUAGE=en
[ -z "${PRESEED_COUNTRY}" ] && PRESEED_COUNTRY=JP
[ -z "${PRESEED_KEYMAP}" ] && PRESEED_KEYMAP=jp106
[ -z "${PRESEED_MIRROR}" ] && PRESEED_MIRROR=ftp.jp.debian.org
[ -z "${PRESEED_TIMEZONE}" ] && PRESEED_TIMEZONE=Asia/Tokyo

atftpd_install()
{
  sudo apt install -y atftpd
  sudo systemctl enable atftpd
  sudo systemctl restart atftpd
}

isc_dhcp_server_install()
{
  sudo apt install -y isc-dhcp-server

  sudo sed -e 's/^#DHCPDv4_CONF=/DHCPDv4_CONF=/g' \
       -e 's/^#DHCPDv4_PID=/DHCPDv4_PID=/g' \
       -e "s/INTERFACESv4=\"\"/INTERFACESv4=\"${DHCP_SERVER_INTERFACESv4}\"/g" \
       -i /etc/default/isc-dhcp-server

  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 systemctl restart isc-dhcp-server
}

netboot_setup()
{
  cd /srv/tftp
  S=http://ftp.debian.org/debian/dists/stretch
  N=${S}/main/installer-amd64/current/images/netboot/netboot.tar.gz
  wget -q -O - ${N} | sudo tar xzf -

  cat <<EOF | sudo tee debian-installer/amd64/boot-screens/syslinux.cfg
path debian-installer/amd64/boot-screens
include debian-installer/amd64/boot-screens/menu.cfg
default debian-installer/amd64/boot-screens/vesamenu.c32
prompt 0
timeout 50
EOF

  cat <<EOF | sudo tee debian-installer/amd64/boot-screens/menu.cfg
menu hshift 13
menu width 49
menu margin 8
menu tabmsg

menu title Installer boot menu
label debian-9-automated-install
  menu label ^Debian 9 automated install
  kernel debian-installer/amd64/linux
  append auto=true priority=critical vga=788 \
    initrd=debian-installer/amd64/initrd.gz \
    preseed/url=tftp://${SERVER_IPADDR}/preseed/debian-9-preseed.cfg
menu end
EOF

  sudo mkdir preseed
  cat <<EOF | sudo tee preseed/debian-9-preseed.cfg
d-i debian-installer/locale string ${PRESEED_LOCALE}
d-i debian-installer/language string ${PRESEED_LANGUAGE}
d-i debian-installer/country string ${PRESEED_COUNTRY}
d-i keyboard-configuration/xkb-keymap select ${PRESEED_KEYMAP}
d-i passwd/user-fullname string
d-i passwd/username string debian
d-i passwd/root-password password debian
d-i passwd/root-password-again password debian
d-i passwd/user-password password debian
d-i passwd/user-password-again password debian
d-i user-setup/allow-password-weak boolean true
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string unassigned-hostname
d-i netcfg/get_domain string unassigned-domain
d-i mirror/country string manual
d-i mirror/http/hostname string ${PRESEED_MIRROR}
d-i mirror/http/directory string /debian
d-i mirror/http/proxy string
d-i clock-setup/utc boolean true
d-i clock-setup/ntp boolean true
d-i time/zone string ${PRESEED_TIMEZONE}
d-i partman/confirm boolean true
d-i partman/choose_partition select finish
d-i partman/confirm_nooverwrite boolean true
d-i partman-auto/disk string /dev/[sv]da
d-i partman-auto/method string lvm
d-i partman-auto/choose_recipe select atomic
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-auto-lvm/guided_size string max
d-i partman-partitioning/confirm_write_new_label boolean true
d-i grub-installer/grub2_instead_of_grub_legacy boolean true
d-i grub-installer/only_debian boolean true
d-i grub-installer/bootdev string /dev/[sv]da
d-i pkgsel/update-policy select none
d-i pkgsel/include string task-gnome-desktop openssh-server
d-i finish-install/reboot_in_progress note
EOF
}

pxeboot_main()
{
  atftpd_install
  isc_dhcp_server_install
  netboot_setup
}

pxeboot_main

2 Install Debian 9 automatically

This article installs Debian 9 on virtual machine on KVM. Virtual machine on VirtualBox and real machine too can be installed Debian 9 automatically with enabling network boot. Please check your BIOS setting.

Enable "NIC" of "Boot device order" with virt-manager. If CDROM is not empty or Disk is already installed some OS, you need to change order in order to NIC be top.

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 5 seconds, "Debian 9 automated install" will be selected automatically.

0003_syslinux.png

Automated install is started.

0004_AutomatedInstall.png

Virtual machine is rebooted automatically when automated install is completed. Installed Debian 9 is started.

0005_AutomatedInstalledDebian.png