hh / dnsmasq-hawser

An attempt at making a minimal statically built dnsmasq Docker container
2 stars 0 forks source link

Build serf as a static executeable #1

Closed hh closed 9 years ago

hh commented 9 years ago

I have a standalone dnsmasq, but serf is linked against libc threading and some others... I'm trying to make a static build of most of my components... to eventually embed in things like hanlon micro-kernel (initrd) etc.

@sethvargo any thoughts on that magic?

[hh@b1 dnsmasq-hawser]$ ldd serf dnsmasq
serf:
        linux-vdso.so.1 (0x00007fffa57cf000)
        libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007fe65de96000)
        libc.so.6 => /usr/lib/libc.so.6 (0x00007fe65daf3000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fe65e0b2000)
dnsmasq:
        not a dynamic executable

And the path to get there:

[hh@b1 dnsmasq-hawser]$ make
docker run -rm -v /home/hh/hanlon/dnsmasq-hawser:/out -e CC=gcc gcc:4.9 /bin/bash /out/build-dnsmasq.sh
Warning: '-rm' is deprecated, it will be replaced by '--rm' soon. See usage.
+ set -e
+ cd /tmp
+ exec
+ echo -e 'GET /dnsmasq/dnsmasq-2.72.tar.gz'
+ tar xz
+ cd dnsmasq-2.72
+ sed -i 's/^LDFLAGS.*/LDFLAGS = -static -static-libgcc/' Makefile
+ make
make[1]: Entering directory `/tmp/dnsmasq-2.72/src'
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c cache.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c rfc1035.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c util.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c option.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c forward.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c network.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c dnsmasq.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c dhcp.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c lease.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c rfc2131.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c netlink.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c dbus.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c bpf.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c helper.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c tftp.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c log.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c conntrack.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c dhcp6.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c rfc3315.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c dhcp-common.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c outpacket.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c radv.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c slaac.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c auth.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c ipset.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c domain.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c dnssec.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c blockdata.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c tables.c
gcc -Wall -W -O2   -DVERSION='"2.72"'          -c loop.c
gcc -static -static-libgcc -o dnsmasq cache.o rfc1035.o util.o option.o forward.o network.o dnsmasq.o dhcp.o lease.o rfc2131.o netlink.o dbus.o bpf.o helper.o tftp.o log.o conntrack.o dhcp6.o rfc3315.o dhcp-common.o outpacket.o radv.o slaac.o auth.o ipset.o domain.o dnssec.o blockdata.o tables.o loop.o   
dnsmasq.o: In function `main':
dnsmasq.c:(.text.startup+0x1abd): warning: Using 'getgrgid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
dnsmasq.c:(.text.startup+0x577): warning: Using 'getgrnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
dnsmasq.c:(.text.startup+0x519): warning: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
make[1]: Leaving directory `/tmp/dnsmasq-2.72/src'
+ strip src/dnsmasq
+ cp src/dnsmasq /out
docker run -t -v /home/hh/hanlon/dnsmasq-hawser:/out golang:1.3.3 /bin/bash /out/build-serf.sh
+ set -e
+ cd /go/src
+ git clone https://github.com/hashicorp/serf.git
Cloning into 'serf'...
remote: Counting objects: 6945, done.
remote: Total 6945 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (6945/6945), 2.52 MiB | 1.54 MiB/s, done.
Resolving deltas: 100% (4426/4426), done.
Checking connectivity... done.
+ cd serf
+ git checkout v0.6.3
Note: checking out 'v0.6.3'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 5e0771b... Cutting v0.6.3
+ make
go get -d -v ./...
github.com/hashicorp/serf (download)
github.com/armon/go-metrics (download)
github.com/hashicorp/go-msgpack (download)
github.com/hashicorp/logutils (download)
github.com/hashicorp/memberlist (download)
github.com/armon/circbuf (download)
github.com/armon/mdns (download)
code.google.com/p/go.net (download)
github.com/miekg/dns (download)
github.com/hashicorp/go-syslog (download)
github.com/mitchellh/cli (download)
github.com/mitchellh/mapstructure (download)
github.com/ryanuber/columnize (download)
github.com/ugorji/go (download)
echo  | xargs -n1 go get -d
--> Installing dependencies to speed up builds...
--> Building...
serf
+ strip bin/serf
+ cp bin/serf /out
[hh@b1 dnsmasq-hawser]$ ls -alh
total 9.0M
drwxr-xr-x 1 hh   hh     104 Oct 24 22:33 .
drwxr-xr-x 1 hh   hh     602 Oct 24 20:42 ..
-rw-r--r-- 1 hh   hh     293 Oct 24 22:31 build-dnsmasq.sh
-rw-r--r-- 1 hh   hh     150 Oct 24 22:32 build-serf.sh
-rwxr-xr-x 1 root root 1023K Oct 24 22:32 dnsmasq
drwxr-xr-x 1 hh   hh     144 Oct 24 22:32 .git
-rw-r--r-- 1 hh   hh     478 Oct 24 22:29 Makefile
-rwxr-xr-x 1 root root  8.0M Oct 24 22:33 serf
[hh@b1 dnsmasq-hawser]$ ldd serf dnsmasq
serf:
        linux-vdso.so.1 (0x00007fffa57cf000)
        libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007fe65de96000)
        libc.so.6 => /usr/lib/libc.so.6 (0x00007fe65daf3000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fe65e0b2000)
dnsmasq:
        not a dynamic executable
hh commented 9 years ago

After building serf, run the go build command with the following:

CGO_ENABLED=0 GOOS=linux go build -a -tags netgo -ldflags "-w" -v -o /out/serf

Thanks @kelseyhightower

https://medium.com/@kelseyhightower/optimizing-docker-images-for-static-binaries-b5696e26eb07

hh commented 9 years ago

Interesting to note that dnsmasq includes libc, so it'll only run on 2.6.26 or later, I wonder how far back a kernel serf will run on?

$ ls -lh dnsmasq serf 
-rwxr-xr-x 1 root root 1023K Oct 24 22:49 dnsmasq
-rwxr-xr-x 1 root root  7.9M Oct 24 22:50 serf
$ ldd dnsmasq serf 
dnsmasq:
        not a dynamic executable
serf:
        not a dynamic executable
$ file dnsmasq serf 
dnsmasq: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, for GNU/Linux 2.6.26, stripped
serf:    ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped
sethvargo commented 9 years ago

Whoops! Sorry @hh I just saw this :smile: