From 2c5a0af4ab551c9c4f1a48de7ad5fe3ac80b53d5 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Fri, 17 Jan 2025 15:59:34 +0900 Subject: [PATCH] systemd: build: Add sethostname workaround for AmazonLinux2 due to coreos/bugs#1272 Signed-off-by: Hiroshi Hatake --- CMakeLists.txt | 18 ++++++++++++++++++ init/az2-sethostname.in | 12 ++++++++++++ init/az2-systemd.in | 17 +++++++++++++++++ src/CMakeLists.txt | 31 ++++++++++++++++++++++++------- 4 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 init/az2-sethostname.in create mode 100644 init/az2-systemd.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 41904bffd0f..7343a8fe7a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,24 @@ if(CMAKE_SYSTEM_NAME MATCHES "Linux") set(FLB_LINUX_ON_AARCH64 On) add_definitions(-DFLB_LINUX_ON_AARCH64) endif() + + find_program(CAT_EXEC cat) + if (CAT_EXEC) + set(RELEASE_FILE "/etc/system-release") + if (EXISTS ${RELEASE_FILE}) + execute_process(COMMAND ${CAT_EXEC} ${RELEASE_FILE} + OUTPUT_VARIABLE CAT_SYSTEM_RELEASE + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + + message(STATUS "##### cat /etc/system-release -- OK") + + if (CAT_SYSTEM_RELEASE STREQUAL "Amazon Linux release 2 (Karoo)") + set(FLB_AMAZON_LINUX2 On) + add_definitions(-DFLB_AMAZON_LINUX2) + endif() + endif() + endif() endif() # Update CFLAGS diff --git a/init/az2-sethostname.in b/init/az2-sethostname.in new file mode 100644 index 00000000000..dcce08bf2d9 --- /dev/null +++ b/init/az2-sethostname.in @@ -0,0 +1,12 @@ +[Unit] +Description=Set Hostname Workaround coreos/bugs#1272 +Wants=network-online.target +After=network-online.target + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/bin/sh -c "/usr/bin/hostnamectl set-hostname $(curl -s http://169.254.169.254/latest/meta-data/hostname)" + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/init/az2-systemd.in b/init/az2-systemd.in new file mode 100644 index 00000000000..191e2e0c974 --- /dev/null +++ b/init/az2-systemd.in @@ -0,0 +1,17 @@ +[Unit] +Description=@FLB_PROG_NAME@ +Documentation=https://docs.fluentbit.io/manual/ +Requires=network.target +After=network.target +Requires=sethostname.service +After=sethostname.service + +[Service] +Type=simple +EnvironmentFile=-/etc/sysconfig/@FLB_OUT_NAME@ +EnvironmentFile=-/etc/default/@FLB_OUT_NAME@ +ExecStart=@CMAKE_INSTALL_FULL_BINDIR@/@FLB_OUT_NAME@ -c /@CMAKE_INSTALL_SYSCONFDIR@/@FLB_OUT_NAME@/@FLB_OUT_NAME@.conf +Restart=always + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8a790166680..9edfff59c3d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -546,13 +546,30 @@ if(FLB_BINARY) endif() if(SYSTEMD_UNITDIR) - set(FLB_SYSTEMD_SCRIPT "${PROJECT_SOURCE_DIR}/init/${FLB_OUT_NAME}.service") - configure_file( - "${PROJECT_SOURCE_DIR}/init/systemd.in" - ${FLB_SYSTEMD_SCRIPT} - ) - install(FILES ${FLB_SYSTEMD_SCRIPT} COMPONENT binary DESTINATION ${SYSTEMD_UNITDIR}) - install(DIRECTORY DESTINATION ${FLB_INSTALL_CONFDIR} COMPONENT binary) + if (FLB_AMAZON_LINUX2) + set(FLB_SYSTEMD_SCRIPT "${PROJECT_SOURCE_DIR}/init/${FLB_OUT_NAME}.service") + configure_file( + "${PROJECT_SOURCE_DIR}/init/az2-systemd.in" + ${FLB_SYSTEMD_SCRIPT} + ) + install(FILES ${FLB_SYSTEMD_SCRIPT} COMPONENT binary DESTINATION ${SYSTEMD_UNITDIR}) + install(DIRECTORY DESTINATION ${FLB_INSTALL_CONFDIR} COMPONENT binary) + set(FLB_HOSTNAME_SYSTEMD_SCRIPT "${PROJECT_SOURCE_DIR}/init/sethostname.service") + configure_file( + "${PROJECT_SOURCE_DIR}/init/az2-sethostname.in" + ${FLB_HOSTNAME_SYSTEMD_SCRIPT} + ) + install(FILES ${FLB_HOSTNAME_SYSTEMD_SCRIPT} COMPONENT binary DESTINATION ${SYSTEMD_UNITDIR}) + install(DIRECTORY DESTINATION ${FLB_INSTALL_CONFDIR} COMPONENT binary) + else() + set(FLB_SYSTEMD_SCRIPT "${PROJECT_SOURCE_DIR}/init/${FLB_OUT_NAME}.service") + configure_file( + "${PROJECT_SOURCE_DIR}/init/systemd.in" + ${FLB_SYSTEMD_SCRIPT} + ) + install(FILES ${FLB_SYSTEMD_SCRIPT} COMPONENT binary DESTINATION ${SYSTEMD_UNITDIR}) + install(DIRECTORY DESTINATION ${FLB_INSTALL_CONFDIR} COMPONENT binary) + endif() elseif(IS_DIRECTORY /usr/share/upstart) set(FLB_UPSTART_SCRIPT "${PROJECT_SOURCE_DIR}/init/${FLB_OUT_NAME}.conf") configure_file(