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).

Preparation


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.

wget https://storage.googleapis.com/golang/go1.12.linux-armv6l.tar.gz

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.

Compiling

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.

#!/bin/sh
set -e

# custom install version of go >= 1.12
GOLANG_PATH=$HOME/apps/go/bin
# which version to build
CLOUDFLARED_VERSION=2019.11.0

export GOPATH=$(pwd)
export GOOS=linux
export GOARCH=arm
export GOARM=6
export CGO_ENABLED=1
export CC=gcc
export PATH=${GOLANG_PATH}:$PATH
CLOUDFLARED_BUILDTIME=$(date)

go get -v github.com/cloudflare/cloudflared/cmd/cloudflared
cd src/github.com/cloudflare/cloudflared
git checkout tags/${CLOUDFLARED_VERSION}
cd ../../../../

go build -v "-ldflags=-X 'main.Version=${CLOUDFLARED_VERSION}' -X 'main.BuildTime=${CLOUDFLARED_BUILDTIME}'" github.com/cloudflare/cloudflared/cmd/cloudflared


Installation

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.