Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to build a static nethogs binary ? #219

Open
quaggalinux opened this issue Jan 4, 2022 · 5 comments
Open

How to build a static nethogs binary ? #219

quaggalinux opened this issue Jan 4, 2022 · 5 comments

Comments

@quaggalinux
Copy link

Hi,

Could you tell me how to build a static nethogs binary ?
Or, is there any parameters to make static nethogs binary ?

Thank you!

@flajr
Copy link

flajr commented Nov 10, 2022

Late to the party, but this can be done like this. Important part is in Containerfile:

# https://pkgs.alpinelinux.org/packages
# https://git.alpinelinux.org/aports/tree/main/nethogs/APKBUILD

FROM alpine:latest
ENV pkgver 0.8.7
ENV pkgname nethogs

WORKDIR /root

RUN apk update && apk --no-cache add git wget autoconf automake build-base linux-headers ncurses-dev ncurses-static libpcap-dev
    
RUN git clone https://github.com/raboof/nethogs "$pkgname-$pkgver"
# RUN wget "https://github.com/raboof/nethogs/archive/v$pkgver/$pkgname-$pkgver.tar.gz" && tar -xzf "$pkgname-$pkgver.tar.gz"

WORKDIR /root/$pkgname-$pkgver

RUN VERSION="$pkgver" CC="gcc -static" LDFLAGS="-static" make nethogs

@quaggalinux
Copy link
Author

@flajr Thank you for your help!
But I used Ubuntu 20.04.5

Following is my procedure:

apt update -y && apt install build-essential libncurses5-dev libpcap-dev autoconf automake && apt upgrade -y

cd /

git clone https://github.com/raboof/nethogs

cd /nethogs

CC="gcc -static" LDFLAGS="-static" make

Ended up with many undefined error.

Would you help me figure out how to make static nethogs in ubuntu?

@flajr
Copy link

flajr commented Nov 11, 2022

Well, sir, Ubuntu is problem. Before you even try to do it, be sure to check possibility of running docker or podman on some machine (friends or yours). In Alpine you can build static binary and than copy it from container to your ubuntu and it just works.

If you are absolutely sure to try it on Ubuntu brace yourself. This is not full guide, unfortunatelly:

Build ncurses (not with musl or zig but standard gcc on machine should be fine):

cd / && wget ftp://ftp.gnu.org/gnu/ncurses/ncurses-6.3.tar.gz && tar -xzf ncurses-6.3.tar.gz && cd ncurses-6.3/
./configure --prefix=/opt/ncurses-6.3 --enable-widec --without-manpages --enable-shared --enable-ext-colors --enable-const && make -j4 && make install

Building libpcap you should figure out yourself, lots of knobs in configure script and Ubuntu do not provide static build:

git clone https://github.com/the-tcpdump-group/libpcap && cd libpcap
./configure --prefix=/opt/libpcap ... && make -j4 && make install

Then I would suggest to use zig project with dev branch to have at least chance to compile something together.
cd /root && wget https://ziglang.org/builds/zig-linux-x86_64-0.11.0-dev.144+892fb0fc8.tar.xz -O /root/zig.tar.xz && tar -xJf zig.tar.xz && ln -s /root/zig-linux-x86_64-0.11.0-dev.144+892fb0fc8/zig /usr/local/bin

Then if you at least compile ncurses you should be able to try this (This will NOT compile if libpcap is not build and properly linked.):

CC="zig cc -static" NCURSES_LIBS="-L/opt/ncurses-6.3/lib" LDFLAGS="-static" make nethogs

I do not see possibility to adjust libpcap with something like PCAP_LIBS (referring to NCURSES_LIBS)... so you need to edit Makefile. You could also try other build pipelines. Other than zig: musl, cmake. There are lots of possibilities but it takes time to look around and try them.

Happy compilling!

@quaggalinux
Copy link
Author

@flajr Thank you for your great great help !!!
I successfully compiled a static nethogs executive file.

Following is my procedure:

I installed ubuntu-20.04.5-live-server-amd64.iso

and then installed docker on ubuntu

#docker pull alpine:latest

#docker run -dit -p 2222:22 --name=ok -v /tmp:/tmp alpine

#docker ps -a

#docker attach e901a5205bc3 (e901a5205bc3 is alpine CONTAINER ID)

#cd /

#apk update && apk --no-cache add git wget autoconf automake build-base linux-headers ncurses-dev ncurses-static libpcap-dev

#git clone https://github.com/raboof/nethogs

#cd /nethogs

#CC="gcc -static" LDFLAGS="-static" make nethogs

#cd src

#cp nethogs /tmp/

#exit

Finally I can copy the static nethogs executive file form ubuntu host's tmp directory.

Really appreciate your comment !!!

@quaggalinux
Copy link
Author

quaggalinux commented Nov 11, 2022

@flajr Would you please take a look at project:
https://github.com/rolandriegel/nload

I also open an issues "rolandriegel/nload#15" asking help for static compile.
I used above way but can not output a static executive file.
I already edit "run_autotools" file to use "#!/bin/sh" to fit alpine.

Thank you again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants