Wednesday, November 13, 2019

Compiling cloudflared for armv6

I have a Raspberry Pi 1 running pi-hole for ads filtering.  Recently I tried to configure it to use Cloudflare DNS-over-https but it isn't stable.  Not to mention that the latest "official" release of cloudflared is broken for Raspberry Pi.  So here are the steps to compile it on Raspberry Pi (Note that the build process will take 30+ minutes on RPi.  So you may want to cross-compile it on a PC instead.  Just change the env variable CGO_ENABLED to 0 in the build script.  It only takes a few seconds to compile on my Ryzen 2200G.  Compiling it on RPi is just for fun).


We need golang 1.12 or later to compile cloudflared.  But the official go version on Raspbian is 1.7.  Use this to download golang 1.12.  Extract the content and take note of the path.


Also, the build process needs more than 1GB of memory.  You may want to create a temporary swap file:

sudo fallocate -l 1G swapfile

sudo dd if=/dev/zero of=swapfile bs=1M count=1024
sudo chmod 600 swapfile
sudo mkswap swapfile
sudo swapon swapfile

After building cloudflared, you can use "swapoff" to remove the temporary swap space and then delete the physical file.


Here is the script to compile the cloudflared binary.  Save and run it under a working folder.  Edit the definition of the first two variables to point to the golang version you are using and the cloudflared version to compile.  If you are corss-compiling, change the "CC" environment to the cross compiler.

set -e

# custom install version of go >= 1.12
# which version to build

export GOPATH=$(pwd)
export GOOS=linux
export GOARCH=arm
export GOARM=6
export CGO_ENABLED=1
export CC=gcc

go get -v
cd src/
git checkout tags/${CLOUDFLARED_VERSION}
cd ../../../../

go build -v "-ldflags=-X 'main.Version=${CLOUDFLARED_VERSION}' -X 'main.BuildTime=${CLOUDFLARED_BUILDTIME}'"


Follow the instructions on pi-hole web site on how to configure pi-hole to use DNS-over-https

1 comment:

Anonymous said...

Thanks for these instructions and script. They worked perfectly today to compile Cloudflared 20202.8.0 using golang 14.7 on my pi zero. I was getting the segfault on the prebuilt binary from Cloudflare.