Skip to content

Commit

Permalink
Add dockerized PXE server
Browse files Browse the repository at this point in the history
  • Loading branch information
foxeng committed Jun 10, 2021
1 parent a193eb8 commit 1565728
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions pxe/.gitignore
@@ -0,0 +1 @@
archiso/
26 changes: 26 additions & 0 deletions pxe/Dockerfile
@@ -0,0 +1,26 @@
FROM archlinux

# Reference:
# - https://wiki.archlinux.org/title/Syslinux#PXELINUX
# - https://wiki.syslinux.org/wiki/index.php?title=PXELINUX
# - https://wiki.syslinux.org/wiki/index.php?title=PXELINUX-Multi-Arch
# - https://www.saminiir.com/boot-arch-linux-from-pxe/

RUN pacman -Syu --noconfirm dnsmasq darkhttpd syslinux \
&& yes | pacman -Scc \
&& mkdir -p /srv/tftp \
&& ln -s /usr/lib/syslinux/bios/lpxelinux.0 /srv/tftp/lpxelinux.0 \
&& ln -s /usr/lib/syslinux/bios/ldlinux.c32 /srv/tftp/ldlinux.c32 \
&& ln -s /usr/lib/syslinux/efi64/syslinux.efi /srv/tftp/syslinux.efi \
&& ln -s /usr/lib/syslinux/efi64/ldlinux.e64 /srv/tftp/ldlinux.e64 \
&& mkdir -p /srv/tftp/pxelinux.cfg \
&& ln -s /archiso/arch/boot/x86_64/vmlinuz-linux /srv/tftp/vmlinuz-linux \
&& ln -s /archiso/arch/boot/intel-ucode.img /srv/tftp/intel-ucode.img \
&& ln -s /archiso/arch/boot/amd-ucode.img /srv/tftp/amd-ucode.img \
&& ln -s /archiso/arch/boot/x86_64/initramfs-linux.img /srv/tftp/initramfs-linux.img

COPY entrypoint.sh /usr/local/bin/entrypoint.sh

RUN chmod +x /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
31 changes: 31 additions & 0 deletions pxe/README.md
@@ -0,0 +1,31 @@
# Docker container for PXE booting Arch ISO images

This provides a simple docker container to allow serving an archlinux ISO image
for booting from the network via PXE.

## Build container image

First of all one needs to build the container image:

```sh
docker build -t arch-pxe .
```

## Serve ISO with PXE

Having built the container image, we may now use it to serve the contents of an
Arch ISO using PXE. Assuming we want to serve the ISO located at
`./archlinux-x86_64.iso` and a DHCP server is already running at `192.168.1.1`:

1. Mount the ISO on the host:

```sh
mkdir archiso
mount -o loop,ro ./archlinux-x86_64.iso ./archiso
```

1. Run the PXE server:

```sh
docker run -it --rm --privileged --net host -v $(pwd)/archiso:/archiso:ro arch-pxe --dhcp 192.168.1.1
```
95 changes: 95 additions & 0 deletions pxe/entrypoint.sh
@@ -0,0 +1,95 @@
#!/bin/bash

set -o pipefail

# Return the first IPv4 address of the first ethernet interface
get_ip() {
local iface
local ip

iface=$(basename $(ls -d /sys/class/net/enp*)) &&
ip=$(ip address show dev ${iface} |
awk '$1 == "inet" { split($2, a, "/"); print a[1] }') || return 1

echo ${ip}
}


HTTP_ADDR=$(get_ip) || echo "Failed to get own IP. Please set it with --http."

USAGE="Usage: ${0} [flags]
flags:
--dhcp addr The address of the DHCP server to proxy. Required.
--http addr The address of the HTTP server. Default ${HTTP_ADDR}."

usage() {
echo "${USAGE}"
exit 1
}


while [ ! -z ${1} ]; do
case ${1} in
--dhcp)
shift
if [ -z ${1} ]; then
usage
fi
DHCP_ADDR=${1}
shift
;;
--http)
shift
if [ -z ${1} ]; then
usage
fi
HTTP_ADDR=${1}
shift
;;
*)
usage
;;
esac
done

if [ -z ${DHCP_ADDR} ] || [ -z ${HTTP_ADDR} ]; then
usage
fi


cat > /etc/dnsmasq.conf <<EOF
# Don't function as dns server
port=0
# Function as a proxy DHCP
dhcp-range=${DHCP_ADDR},proxy
dhcp-no-override
# tftp server setup
enable-tftp
tftp-root=/srv/tftp
# Log extra information about dhcp transactions (for debug purposes)
log-dhcp
# NOTE: For some reason, dhcp-boot doesn't seem to work.
pxe-service=x86PC,"PXELINUX (BIOS)",/lpxelinux
pxe-service=X86-64_EFI,"PXELINUX (EFI)",/syslinux.efi
EOF

cat > /srv/tftp/pxelinux.cfg/default <<EOF
DEFAULT archlinux
LABEL archlinux
MENU LABEL Arch Linux x86_64 PXE
LINUX /vmlinuz-linux
INITRD /intel-ucode.img,/amd-ucode.img,/initramfs-linux.img
APPEND archiso_http_srv=http://${HTTP_ADDR}/
SYSAPPEND 3
EOF

# NOTE: dnsmasq launches in the background by default
dnsmasq

darkhttpd /archiso

0 comments on commit 1565728

Please sign in to comment.