This article is a continuation of Part 2. This time, when building a PiServer alternative system, it will be the part of building a PXE server with dnsmasq, which is the key.
It's a selfish guess, but if you have a lot of Raspberry Pis, I think that the surplus machines that you can use freely are lying somewhere at home or at work / university. And I think it's a good idea to install a UNIX-like OS that you're used to on a surplus machine, so I won't describe any how-tos about installing the OS itself.
Hereafter, we will assume that the host name of the PXE server is "hogeHost" and the IP is 192.168.172.16/24.
# /etc/hosts(Example)
#192 fixed IP for PXE server.168.172.16
#When the host name is hogeHost
127.0.0.1 localhost
192.168.172.16 hogeHost
[^ 20200608 Added]: Added after receiving a comment from @kakinaguru_zo (2020/06/08) [^ 20200608 Added 2]: 2020/06/08 Added
Each distribution has a different way to install the package, but if you have a UNIX-like OS that uses a package system, you should definitely distribute the binary package on the official repository, so you don't have to compile it yourself. [^ GENTOO]. I will also omit how to install the package for each distribution. Install dnsmasq using the commands you normally use, such as apt-get, pacman, or dnf. If you forgot the command, refer to the link below. Reference: ArchLinux Pacman Comparison Table
[^ GENTOO]: Except for Gentoo, which requires you to compile any software yourself (bitter smile)
Normally I can write all the settings to the /etc/dnsmasq.conf file, so I take the style of writing everything to the dnsmasq.conf file. Depending on the distribution, in addition to /etc/dnsmasq.conf, there is also an environment where there is a subdirectory /etc/dnsmasq.d/ and all files under this directory are included, so in this case everyone's preference Please save the setting file separately with. [^ DIV_EX]
The dnsmasq.conf file described in this article is a modified version of the one spit out by PiServer.
[^ DIV_EX]: Example: Divide files by DNS, DHCP, tftp functions, divide by MAC address of Raspberry Pi, etc.
The sample (default) dnsmasq.conf file is commented out with a line to open the DNS service on port 5353. However, if you run dnsmasq in this state, it will open port 53, which is used by normal DNS services, so do not open it intentionally. Keep the comment line and add port = 0 line
/etc/dnsmasq.conf(part)
・ ・ ・
#port=5353
#Disable DNS(Add line)
port=0
#The following line is port=If 0 is specified, it is a line that is not originally needed,
#In case the DNS service starts by mistake
#Specify DNS as a local service
local-service
#As a tag name when setting DHCP related items
#Since you will be using your own host name, your own host name in advance/Register the address
# [Format] host-record=hostname,Host IP
host-record=hogeHost,192.168.172.16
・ ・ ・
By default, the tftp server feature is disabled, so enable it first. It also enables the ability to identify clients by MAC address and change the download destination tftp route.
/etc/dnsmasq.conf(part)
#Enable tftp server function
enable-tftp
#The location of the reference directory for files provided by tftp(as you like)
#Under this directory, MAC/You will create a tftproot directory for each IP address
# (It's actually a symbolic link)
tftp-root=/srv/tftp
#Enable the ability to automatically sort the tftproot directory by MAC address.
#By the time Raspberry Pi accesses the tftp server, it should already have an IP.
#In an environment where a fixed IP can be assigned, the tftproot directory can be automatically assigned to each IP address.
#In that case, after equal"ip"To
#The environment we are currently building/etc/I plan to use a fixed IP for management by hosts.
#I dare to use mac to maintain compatibility with PiServer functions.
tftp-unique-root=mac
Also, create a base directory to store the tftp root directory to be shown to each Raspberry Pi specified on the tftp-root line.
# mkdir /srv/tftp
By default, the DHCP server function is disabled, so enable it. However, please note that the description method of the DHCP function to be set from now on will change depending on whether or not another DHCP server exists in the environment (subnet) you are currently using. By the way, in my example, it is the one who has an existing DHCP server.
/etc/dnsmasq.conf(part)
# dhcp-The DHCP function will not be enabled without the range line, so use either line according to your environment.
#If there are no other DHCP servers in the subnet
# -------------------
#It is necessary to specify the range of IP given to the client, netmask, etc.
# [Format] dhcp-range=tag:hostname,Start IP,End IP(,Net mask(,Broadcast address(,Lease time)))
#dhcp-range=tag:hogeHost,192.168.172.32,192.168.63,255.255.255.0
#You should also specify the default gateway
# [Format] dhcp-option=tag:hostname,option:router,Gateway address
#dhcp-option=tag:hogeHost,option:router,192.168.172.254
#If there is another DHCP server in the subnet that will give you an IP
# ------------------
#Act as a DHCP Proxy. In the case of Proxy, the format is changed because the range given is only the IP that you own.
# [Format] dhcp-range=tag:hostname,Host IP,proxy
dhcp-range=tag:hogeHost,192.168.172.16,proxy
#It seems that it is better to delay the DHCP response by 1 second to prevent boot failure due to a bug in client-side PXE
dhcp-reply-delay=2
#Log about DHCP
log-dhcp
The person who enables the tftp and dhcp functions is the person who wants the PXE function, so there is a dedicated setting item for such a person. The setting for Raspberry Pi is a "magic" state that requires only one line, but if you want to use the PXE function on a general computer other than Raspberry Pi, please check the details based on the sample file. Note that the dnsmasq PXE server, which does not have this one line, will be ignored by the Raspberry Pi bootloader (bootcode.bin), so it is a required item.
# /etc/dnsmasq.conf(part)
#Raspberry bootcode.One line required to make bin recognize as a tftp server
# ("Raspberry Pi Boot"Character string cannot be modified)
# pxe-service=tag:PXE server name,0,"Raspberry Pi Boot"
pxe-service=tag:hogeHost,0,"Raspberry Pi Boot"
From here, you will be adding one entry for each Raspberry Pi. Find out the MAC address of your Raspberry Pi and add entries line by line.
/etc/dnqmasq.conf(part,MAC address is an appropriate value)
#Settings when there is no other DHCP server
# (Allocate a fixed IP to each MAC address)
# --------------
# [Format] dhcp-host=MAC address of Raspberry Pi,set:hostname,IP address to give,infinite
# Raspi2
#dhcp-host=b8:27:eb:GG:HH:II,set:hogeHost,192.168.172.32,infinite
# Raspi3
#dhcp-host=b8:27:eb:XX:YY:ZZ,set:hogeHost,192.168.172.33,infinite
# Raspi4
#dhcp-host=dc:a6:32:PP:QQ:RR,set:hogeHost,192.168.172.34,infinite
#Settings for Proxy DHCP settings
# -----
# [Format] dhcp-host=MAC address of Raspberry Pi,set:hostname
# Raspi2
dhcp-host=b8:27:eb:GG:HH:II,set:hogeHost
# Raspi3
dhcp-host=b8:27:eb:XX:YY:ZZ,set:hogeHost
# Raspi4
dhcp-host=dc:a6:32:PP:QQ:RR,set:hogeHost
After adding entries, create a subdirectory with a name that changes the colon of the MAC address to a hyphen under the reference directory specified when setting tftp related items so that an error does not occur when dnsmasq is test-started. To do. (In production this will be a symbolic link to the / boot folder of the client OS)
# mkdir /srv/tftp/b8-27-eb-GG-HH-II
# mkdir /srv/tftp/b8-27-eb-XX-YY-ZZ
# mkdir /srv/tftp/dc-a6-32-PP-QQ-RR
This completes the dnsmasq settings.
Perform a syntax check on the completed dnsmasq.conf
# sudo dnsmasq --test
dnsmasq: syntax check OK.
#
That's all for this time. Next time will be the construction of NFS server and the manual import of OS for clients.