FROM debian:buster

RUN apt update
RUN apt install -y make debootstrap supervisor isc-dhcp-server tftpd-hpa busybox squashfs-tools

# Downloads base Debian 10 to /linux/build/cache. The Makefile in linux/ would
# download base debian system to linux/build/cache itself, but to speed up
# rebuilding this Docker container the download is run separately to avoid
# repeating it in future builds.
ADD linux /linux
RUN mkdir -p linux/build && debootstrap buster linux/build/cache

# Copies src/tftpboot directory to /tftpboot
ADD src/tftpboot /tftpboot

# Downloads Syslinux and moves ldlinux.c32 and pxelinux.0 files to /tftpboot
RUN wget https://mirrors.edge.kernel.org/pub/linux/utils/boot/syslinux/syslinux-6.03.tar.gz && \
	tar -xzvf syslinux-6.03.tar.gz && \
	cp syslinux-6.03/bios/core/pxelinux.0 /tftpboot/pxelinux.0 && \
	cp syslinux-6.03/bios/com32/elflink/ldlinux/ldlinux.c32 /tftpboot/ldlinux.c32

# Builds Linux with its Makefile
RUN cd /linux && make

# Creates and populates /tftpboot directory. Files stored there will be served
# by TFTP and HTTP (on port 8014).
RUN cp /linux/build/rootfs/initrd.img /tftpboot/initrd.img && \
	cp /linux/build/rootfs/vmlinuz /tftpboot/vmlinuz && \
	mksquashfs /linux/build/rootfs /tftpboot/root.squashfs

# Supervisor's config
COPY src/supervisor.conf /supervisor.conf

# DHCP server config
RUN mkdir -p /var/lib/dhcp && touch /var/lib/dhcp/dhcpd.leases
COPY src/dhcpd.conf /etc/dhcp/dhcpd.conf

# The run.sh script setups networking and starts supervisor.
COPY src/run.sh /run.sh
RUN chmod +x /run.sh

# Interface on which the server will be listening. Set with `-e INTERFACE=eth1`
ENV INTERFACE=eth0

CMD ["/run.sh"]
