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

Cross compile telegraf depend on libc.so instead of libc.so.6 #15379

Closed
vortexilation opened this issue May 20, 2024 · 7 comments
Closed

Cross compile telegraf depend on libc.so instead of libc.so.6 #15379

vortexilation opened this issue May 20, 2024 · 7 comments
Labels
bug unexpected problem or unintended behavior

Comments

@vortexilation
Copy link

Relevant telegraf.conf

NONE

Logs from Telegraf

NONE

System info

OpenWrt x86_64 SNAPSHOT musl

Docker

No response

Steps to reproduce

I am on latest debian SID x86_64 glibc compiling for/targeting OpenWrt x86_64 musl,
Upon execution of telegraf for getting the default config :

	$(PKG_INSTALL_DIR)/usr/bin/telegraf config > $(PKG_BUILD_DIR)/etc/telegraf.conf

I am getting :

bash: line 1: /home/user/works/openwrt/build_dir/target-x86_64_musl_custom/telegraf-full/telegraf-1.30.2/ipkg-install/usr/bin/telegraf: cannot execute: required file not found

On readelf -d telegraf

# readelf /home/user/works/openwrt/build_dir/target-x86_64_musl_custom/telegraf-full/telegraf-1.30.2/ipkg-install/usr/bin/telegraf -d

Dynamic section at offset 0xd83dc60 contains 24 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so]
 0x000000000000000c (INIT)               0x403000
 0x000000000000000d (FINI)               0x69bcff8
 0x0000000000000019 (INIT_ARRAY)         0xdc3ec50
 0x000000000000001b (INIT_ARRAYSZ)       8 (bytes)
 0x000000000000001a (FINI_ARRAY)         0xdc3ec58
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x0000000000000004 (HASH)               0x400318
 0x000000006ffffef5 (GNU_HASH)           0x400678
 0x0000000000000005 (STRTAB)             0x401348
 0x0000000000000006 (SYMTAB)             0x400850
 0x000000000000000a (STRSZ)              1685 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000007 (RELA)               0x401ae8
 0x0000000000000008 (RELASZ)             1320 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x0000000000000018 (BIND_NOW)           
 0x000000006ffffffb (FLAGS_1)            Flags: NOW
 0x000000006ffffffe (VERNEED)            0x401ac8
 0x000000006fffffff (VERNEEDNUM)         1
 0x000000006ffffff0 (VERSYM)             0x4019de
 0x0000000000000000 (NULL)               0x0

the /lib/x86_64-linux-gnu/libc.so itself is not a binary but a somekind of script :

/* GNU ld script
   Use the shared library, but some functions are only in
   the static library, so try that secondarily.  */
OUTPUT_FORMAT(elf64-x86-64)
GROUP ( /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/x86_64-linux-gnu/libc_nonshared.a  AS_NEEDED ( /lib64/ld-linux-x86-64.so.2 ) )

On searching, the go should be linking with "/lib/x86_64-linux-gnu/libc.so.6" instead of libc.so.6. How to make go linking with "/lib/x86_64-linux-gnu/libc.so.6" by default and delete libc.so dependency.

On below attached the OpenWrt Makefile for telegraf.

Expected behavior

telegraf binary able to run

Actual behavior

telegraf binary binary not able to run

Additional info

OpenWrt telegraf Makefile :

# SPDX-License-Identifier: GPL-2.0-or-later

include $(TOPDIR)/rules.mk

PKG_NAME:=telegraf
PKG_VERSION:=1.30.2
PKG_RELEASE:=1

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://codeload.github.com/influxdata/telegraf/tar.gz/v$(PKG_VERSION)?
PKG_HASH:=3514d870fe1899f20c5d1f1545233413cbe11061b23a0cafbd44b861a9295dc6

PKG_MAINTAINER:=Jonathan Pagel <jonny_tischbein@systemli.org>
PKG_LICENSE:=MIT
PKG_LICENSE_FILES:=LICENSE

PKG_BUILD_DEPENDS:=golang/host
PKG_BUILD_PARALLEL:=1
PKG_BUILD_FLAGS:=no-mips16

GO_PKG:=github.com/influxdata/telegraf
GO_PKG_BUILD_PKG:=github.com/influxdata/telegraf/cmd/telegraf
GO_PKG_LDFLAGS_X:=main.version=$(PKG_VERSION)

include $(INCLUDE_DIR)/package.mk
include ../../lang/golang/golang-package.mk

define Package/telegraf/Default
	SECTION:=utils
	CATEGORY:=Utilities
	TITLE:=Telegraf
	DEPENDS:=$(GO_ARCH_DEPENDS)
endef

define Package/telegraf-full
	$(call Package/telegraf/Default)
	TITLE+= (Full)
	VARIANT:=full
endef

define Package/telegraf
	$(call Package/telegraf/Default)
	TITLE+= (Small)
	VARIANT:=small
endef

define Package/telegraf/description/Default
	Telegraf is a plugin-driven agent for collecting and sending metrics and events.
	It supports various inputs (including prometheus endpoints) and is able to send data into InfluxDB.
endef

define Package/telegraf/description
	$(call Package/telegraf/description/Default)
	(Small build. Most plugins excluded)
endef

define Package/telegraf-full/description
	$(call Package/telegraf/description/Default)
	(Full build. including all plugins)
endef

define Package/telegraf/conffiles
/etc/telegraf.conf
endef

Package/telegraf-full/conffiles = $(Package/telegraf/conffiles)

define Build/Prepare
	$(call Build/Prepare/Default)
ifeq ($(BUILD_VARIANT),small)
	$(CP) ./variant-$(BUILD_VARIANT)/* $(PKG_BUILD_DIR)/
endif
endef

#export LD_LIBRARY_PATH=$(TOOLCHAIN_DIR)/lib:$$$$LD_LIBRARY_PATH

MAKE_VARS += \
	GOOS="$(GO_OS)" \
	GOARCH="$(GO_ARCH)" \
	GOPATH=$(GO_PKG_BUILD_DIR) \
	GOCACHE=$(GO_BUILD_CACHE_DIR) \
	GOROOT_FINAL=$(GO_TARGET_ROOT) \
	CC="$(TARGET_CC)" \
	CGO_ENABLED=1 \
	CXX="$(TARGET_CXX)" \
	$(GO_PKG_VARS)

define Package/telegraf/install
	$(call GoPackage/Package/Install/Bin,$(PKG_INSTALL_DIR))
	$(INSTALL_DIR) $(1)/etc/init.d $(1)/etc/config
	$(INSTALL_BIN) ./files/etc/init.d/telegraf $(1)/etc/init.d/telegraf
	$(PKG_INSTALL_DIR)/usr/bin/telegraf config > $(PKG_BUILD_DIR)/etc/telegraf.conf
	$(INSTALL_CONF) $(PKG_BUILD_DIR)/etc/telegraf.conf $(1)/etc/telegraf.conf
endef

define Package/telegraf-full/install
	$(call Package/telegraf/install,$(PKG_INSTALL_DIR))
endef

$(eval $(call GoBinPackage,telegraf))
$(eval $(call BuildPackage,telegraf))

$(eval $(call GoBinPackage,telegraf-full))
$(eval $(call BuildPackage,telegraf-full))
@vortexilation vortexilation added the bug unexpected problem or unintended behavior label May 20, 2024
@powersj
Copy link
Contributor

powersj commented May 20, 2024

Hi,

Your build file is building with CGO enabled, which is not what we support or do for any of our official builds:

CGO_ENABLED=1

By building with CGO enabled, you building a dynamic binary which will depend on external dependencies rather than a static binary.

@powersj powersj added the waiting for response waiting for response from contributor label May 20, 2024
@vortexilation
Copy link
Author

@powersj
I don't know much about golang, I guess in traditional sense, you've meant static linkage?. By "CGO_ENABLED=1" is dynamic linkage?.

And there is no workaround regarding this ?.

@telegraf-tiger telegraf-tiger bot removed the waiting for response waiting for response from contributor label May 20, 2024
@powersj
Copy link
Contributor

powersj commented May 20, 2024

I guess in traditional sense, you've meant static linkage?. By "CGO_ENABLED=1" is dynamic linkage?.

Yes, there are a few other consequences as well, but that is the general difference. You can see:

And there is no workaround regarding this ?.

Why do you need to build a dynamic binary? By generating a static binary you avoid this all together.

@powersj powersj added the waiting for response waiting for response from contributor label May 20, 2024
@vortexilation
Copy link
Author

The go app that are used in OpenWrt are mostly/all dynamic i guess, "CGO_ENABLED=1" is hardcoded in golang compilation supports on OpenWrt.

For compiling telegraf with static linkage, what do I need to do manually?, what is the command line?, does passing "CGO_ENABLED=0" is enough?, it doesn't seems to work for my case.

What is the complete command lines , other than simple "make build" ?.

@telegraf-tiger telegraf-tiger bot removed the waiting for response waiting for response from contributor label May 21, 2024
@powersj
Copy link
Contributor

powersj commented May 21, 2024

The go app that are used in OpenWrt are mostly/all dynamic i guess, "CGO_ENABLED=1" is hardcoded in golang compilation supports on OpenWrt.

I thought someone was already building openwrt builds before. Did something change such that it broke?

other than simple "make build" ?.

That is how we produce all our artifacts. That roughly translates to:

branch := $(shell git rev-parse --abbrev-ref HEAD)
commit := $(shell git rev-parse --short=8 HEAD)
INTERNAL_PKG=github.com/influxdata/telegraf/internal
LDFLAGS := $(LDFLAGS) -X $(INTERNAL_PKG).Commit=$(commit) -X $(INTERNAL_PKG).Branch=$(branch)
CGO_ENABLED=0 go build -ldflags "-w -s $(LDFLAGS)" ./cmd/telegraf

You can add additional GO flags if required, like GOOS, GOARCH, etc. Or add LDFLAGS

@powersj powersj added the waiting for response waiting for response from contributor label May 21, 2024
@vortexilation
Copy link
Author

I am not a OpenWrt developer, only a user, but a PR that's comes in here .

$(1)/usr/bin/telegraf config > $(1)/etc/telegraf.conf

The compilation is going fine for the targeted devices but It's only for getting telegraf.conf, it's problematic if the libc or target is different than build system, I have tested that PR, if i am building targeting for glibc x86_64 then of course binary running fine.

Thank you for those snippet, it will be coming handy if just like old time, there is already extracted or given telegraf.conf somewhere, other than needs to run telegraf binary first.

@telegraf-tiger telegraf-tiger bot removed the waiting for response waiting for response from contributor label May 22, 2024
@powersj
Copy link
Contributor

powersj commented May 22, 2024

$(1)/usr/bin/telegraf config &gt; $(1)/etc/telegraf.conf

Keep in mind the user, you, can generate this later on. The configuration that is produced is not necessary to run telegraf as it is incomplete and does not include a working output. If a user needs this they can run the command and then go through it.

Our own builds will run go run ./cmd/telegraf config > etc/telegraf.conf to generate the full config. This avoids running the locally built binary which as you discovered may be built for a different architecture.

Hopefully all these various steps help. I am going to close this as I don't think this is actually an issue with Telegraf itself.

@powersj powersj closed this as not planned Won't fix, can't repro, duplicate, stale May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants