mahsank / pi-router

Tool used to generate a custom Raspberry Pi OS image that transforms an RPi board into a secure router
BSD 3-Clause "New" or "Revised" License
5 stars 0 forks source link
debian dnscrypt-proxy dnsmasq nftables openwrt pi-os-router pi-router raspberry-pi raspbian-router router rpi-board

Pi-router

Pi-router is a tool used to generate a custom Raspberry Pi OS image that transforms an RPi board into a secure router(strictly speaking an access point). Pi-router is derived from pi-gen and is based on 2020-12-02 release.

The resultant Pi OS can be considered an alternative to OpenWrt for RPi boards. It is secured with nftables and dnscrypt-proxy.

TL;DR

Dependencies

Pi-router build is tested with Debian bullseye, buster, Ubuntu Focal Fossa, and Fedora 33.

To install the required dependencies for pi-router on Debian, bullseye, buster, run:

$ apt-get install coreutils quilt parted qemu-user-static debootstrap zerofree \
zip dosfstools bsdtar libcap2-bin grep rsync xz-utils file git curl bc

To achieve the same on Ubuntu Focal Fossa, replace the bsdtar package with libarchive-tools.

Similarly, on Fedora 33, run:

$ dnf install coreutils quilt parted qemu-user-static debootstrap zerofree \
zip dosfstools bsdtar libcap grep rsync xz file git curl bc

Other distributions should work but not tested. Feel free to give pi-router a spin on your favorite distro and let me know the results.

Router-Config

Upon execution, build.sh will source the file router-config in the current working directory. This bash shell fragment is intended to set needed environment variables.

The following environment variables are supported:

A minimal router-config file is included in the build and is the default for build.sh.If needed, this file can be customized further.

Build Process

The image is built with the following process:

Please refer to build.sh for finer details.

Docker Build

Docker can be used to perform the build inside a container. This partially isolates the build from the host system, and allows using the script on distributions other than Debian or Fedora. It might be worth noting that Docker build can be used on Debian or Fedora as well. Running Docker build is as simple as issuing the command below:

  $ ./build-docker.sh

If everything goes well, the final image will be in deploy/ directory. The build container can be removed after the build with the command:

$ docker rm -v pirouter_work

Similar to build.sh, build-docker.sh can be continued from where it left during an interruption:

$ CONTINUE=1 ./build-docker.sh

In case of a failure, the container can be examined by issuing the following command:

$ sudo docker run -it --privileged --volumes-from=pirouter_work pi-router /bin/bash

In case of successful build, the build container is by default removed. This can be changed by issuing the command:

PRESERVE_CONTAINER=1 ./build-docker.sh

Stage Anatomy

The build process is divided up into several stages for logical clarity and modularity. This causes some initial complexity, but it simplifies maintenance and allows for more easy customization.

Stage 0 - bootstrap. The primary purpose of this stage is to create a usable filesystem. This is accomplished largely through the use of debootstrap, which creates a minimal filesystem suitable for use as a base.tgz on Debian systems. This stage also configures apt settings and installs raspberrypi-bootloader which is missed by debootstrap. The minimal core is installed but not configured, and the system will not quite boot yet.

Stage 1 - truly minimal system. This stage makes the system bootable by installing system files like /etc/fstab, configures the bootloader, makes the network operable, and installs packages like raspi-config. At this stage the system should boot to a local console from which you have the means to perform basic tasks needed to configure and install the system. This is as minimal as a system can possibly get, and its arguably not really usable in a traditional sense yet. Still, if you want minimal, this is minimal and the rest you could reasonably do yourself as sysadmin.

Stage 2 - router system. This stage produces the router image. It installs some optimized memory functions, sets timezone and charmap defaults, installs fake-hwclock and ntp, wireless LAN and bluetooth support, dphys-swapfile, and other basics for managing the hardware. It also creates necessary groups and gives the pi user access to sudo and the standard console hardware permission groups.

All the customizations needed to transform RPi into a secure router are done at this stage. Contrary to pi-gen build stages, pi-router does not need to go beyond this stage.

Network Configuration Details

Pi-router makes use of dnsmasq, dhcpcd, and hostapd, to transform the RPi into a router. The default LAN side gateway ip address is 172.31.31.1/24 and the connected clients are assigned addresses in the range of 172.31.31.2-254. WAN side address is supplied by the RJ-45 connector on RPi board.

Known Limitations

Troubleshooting

binfmt_misc

Linux is able execute binaries from other architectures, meaning that it should be possible to make use of pi-router on an x86_64 system, even though it will be running ARM binaries. This requires support from the binfmt_misc kernel module.

You may see the following error:

update-binfmts: warning: Could not load the binfmt_misc module.

To resolve this, make sure that binfmt_misc module is loaded and qemu-arm-static binary is available.

$ lsmod | grep binfmt_misc
$ command -v qemu-arm-static

If you find this work useful, consider buying me a coffee.