Skip to content

Commit 448dab0

Browse files
committed
nftables: drop busybox, replace nftd with nft-helper, reduce size
Simplify container even further, to a strict application container, dropping BusyByx and replacing nftd (shell script) with nft-helper. The resulting container is a ¼ of the previous size, while retaining the functionality, making it a lot more sane to bundle with Infix images. Signed-off-by: Joachim Wiberg <[email protected]>
1 parent daf67d4 commit 448dab0

File tree

10 files changed

+145
-25
lines changed

10 files changed

+145
-25
lines changed

board/nftables/rootfs/sbin/nftd

Lines changed: 0 additions & 17 deletions
This file was deleted.

configs/curios-nftables_amd64_defconfig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ BR2_TOOLCHAIN_EXTERNAL_BOOTLIN_X86_64_UCLIBC_STABLE=y
44
BR2_DL_DIR="${BR2_EXTERNAL_CURIOS_PATH}/dl"
55
BR2_CCACHE=y
66
BR2_CCACHE_DIR="${BR2_EXTERNAL_CURIOS_PATH}/.ccache"
7+
BR2_STATIC_LIBS=y
78
BR2_TARGET_GENERIC_HOSTNAME="curiOS"
89
BR2_TARGET_GENERIC_ISSUE="Welcome to curiOS"
910
BR2_INIT_NONE=y
11+
# BR2_TARGET_ENABLE_ROOT_LOGIN is not set
1012
BR2_SYSTEM_BIN_SH_NONE=y
1113
# BR2_TARGET_GENERIC_GETTY is not set
1214
# BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set
1315
BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_CURIOS_PATH)/board/nftables/rootfs"
1416
BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_CURIOS_PATH)/board/common/post-build.sh"
15-
BR2_PACKAGE_BUSYBOX_CONFIG="${BR2_EXTERNAL_CURIOS_PATH}/board/common/busybox-tiny_defconfig"
16-
BR2_PACKAGE_BUSYBOX_SHOW_OTHERS=y
17+
# BR2_PACKAGE_BUSYBOX is not set
1718
# BR2_PACKAGE_IFUPDOWN_SCRIPTS is not set
1819
BR2_PACKAGE_NFTABLES=y
19-
# BR2_PACKAGE_URANDOM_SCRIPTS is not set
2020
BR2_PACKAGE_TINI=y
2121
BR2_TARGET_ROOTFS_OCI=y
2222
BR2_TARGET_ROOTFS_OCI_AUTHOR="curiOS"
2323
BR2_TARGET_ROOTFS_OCI_TAG="edge"
2424
BR2_TARGET_ROOTFS_OCI_ENTRYPOINT="/usr/bin/tini --"
25-
BR2_TARGET_ROOTFS_OCI_CMD="/sbin/nftd"
25+
BR2_TARGET_ROOTFS_OCI_CMD="/usr/sbin/nft-helper /etc/nftables.conf"
2626
BR2_TARGET_ROOTFS_OCI_LABELS=".url=https://github.com/kernelkit/curiOS .title=curiOS-nftables"
27+
BR2_PACKAGE_NFT_HELPER=y

configs/curios-nftables_arm64_defconfig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@ BR2_TOOLCHAIN_EXTERNAL_BOOTLIN_AARCH64_UCLIBC_STABLE=y
55
BR2_DL_DIR="${BR2_EXTERNAL_CURIOS_PATH}/dl"
66
BR2_CCACHE=y
77
BR2_CCACHE_DIR="${BR2_EXTERNAL_CURIOS_PATH}/.ccache"
8+
BR2_STATIC_LIBS=y
89
BR2_TARGET_GENERIC_HOSTNAME="curiOS"
910
BR2_TARGET_GENERIC_ISSUE="Welcome to curiOS"
1011
BR2_INIT_NONE=y
12+
# BR2_TARGET_ENABLE_ROOT_LOGIN is not set
1113
BR2_SYSTEM_BIN_SH_NONE=y
1214
# BR2_TARGET_GENERIC_GETTY is not set
1315
# BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set
1416
BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_CURIOS_PATH)/board/nftables/rootfs"
1517
BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_CURIOS_PATH)/board/common/post-build.sh"
16-
BR2_PACKAGE_BUSYBOX_CONFIG="${BR2_EXTERNAL_CURIOS_PATH}/board/common/busybox-tiny_defconfig"
17-
BR2_PACKAGE_BUSYBOX_SHOW_OTHERS=y
18+
# BR2_PACKAGE_BUSYBOX is not set
1819
# BR2_PACKAGE_IFUPDOWN_SCRIPTS is not set
1920
BR2_PACKAGE_NFTABLES=y
20-
# BR2_PACKAGE_URANDOM_SCRIPTS is not set
2121
BR2_PACKAGE_TINI=y
2222
BR2_TARGET_ROOTFS_OCI=y
2323
BR2_TARGET_ROOTFS_OCI_AUTHOR="curiOS"
2424
BR2_TARGET_ROOTFS_OCI_TAG="edge"
2525
BR2_TARGET_ROOTFS_OCI_ENTRYPOINT="/usr/bin/tini --"
26-
BR2_TARGET_ROOTFS_OCI_CMD="/sbin/nftd"
26+
BR2_TARGET_ROOTFS_OCI_CMD="/usr/sbin/nft-helper /etc/nftables.conf"
2727
BR2_TARGET_ROOTFS_OCI_LABELS=".url=https://github.com/kernelkit/curiOS .title=curiOS-nftables"
28+
BR2_PACKAGE_NFT_HELPER=y

package/Config.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
menu "Networking applications"
2+
source "$BR2_EXTERNAL_CURIOS_PATH/package/nft-helper/Config.in"
23
source "$BR2_EXTERNAL_CURIOS_PATH/package/openresty/Config.in"
34
endmenu

package/nft-helper/Config.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
config BR2_PACKAGE_NFT_HELPER
2+
bool "nft-helper"
3+
help
4+
Wrapper for nft running under tini. At startup it takes
5+
its first command line argument as the nftables.conf to
6+
load with: 'nft -f /path/to/nftables.conf' on any signal
7+
it shuts down by calling 'nft flush ruleset'.

package/nft-helper/nft-helper.mk

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
################################################################################
2+
#
3+
# nft-helper
4+
#
5+
################################################################################
6+
7+
NFT_HELPER_VERSION = 1.0
8+
NFT_HELPER_SITE_METHOD = local
9+
NFT_HELPER_SITE = $(BR2_EXTERNAL_CURIOS_PATH)/src/nft-helper
10+
NFT_HELPER_LICENSE = ISC
11+
NFT_HELPER_LICENSE_FILES = LICENSE
12+
13+
define NFT_HELPER_BUILD_CMDS
14+
$(TARGET_CONFIGURE_OPTS) $(MAKE) -C $(@D) all
15+
endef
16+
17+
define NFT_HELPER_INSTALL_TARGET_CMDS
18+
$(INSTALL) -D -m 0755 $(@D)/nft-helper $(TARGET_DIR)/usr/sbin/
19+
endef
20+
21+
$(eval $(generic-package))

src/nft-helper/LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2024 The KernelKit Authors
2+
3+
Permission to use, copy, modify, and/or distribute this software for any
4+
purpose with or without fee is hereby granted, provided that the above
5+
copyright notice and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

src/nft-helper/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
all: nft-helper
2+
3+
clean:
4+
rm nft-helper *.o
5+
6+
distclean: clean
7+
rm *~
8+
9+
nft-helper: main.c Makefile
10+
$(CC) -o $@ $(CFLAGS) $< $(LDFLAGS)

src/nft-helper/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
NFT Helper
2+
==========
3+
4+
This is a wrapper for nft running under tini. Its only purpose is to
5+
listen to signals from tini, load nft at start, flush ruleset at exit.
6+
7+
We could use a simple script for this, as shown below, and that would
8+
be fine if we had a POSIX shell in the container. This helper is for
9+
the case when there is nothing.
10+
11+
```
12+
#!/bin/sh
13+
# nft wrapper to load rules at startup and flush at shutdown
14+
15+
flush()
16+
{
17+
echo "Got signal, stopping ..."
18+
nft flush ruleset
19+
exit 0
20+
}
21+
22+
trap flush INT TERM QUIT EXIT
23+
24+
# Load ruleset
25+
nft -f /etc/nftables.conf
26+
27+
# sleep may exit on known signal, so we cannot use 'set -e'
28+
sleep infinity
29+
```

src/nft-helper/main.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* SPDX-License-Identifier: ISC */
2+
3+
#include <err.h>
4+
#include <signal.h>
5+
#include <stdlib.h>
6+
#include <unistd.h>
7+
#include <sys/wait.h>
8+
9+
int run(char *cmd[])
10+
{
11+
pid_t pid;
12+
int rc;
13+
14+
pid = fork();
15+
if (pid == -1) {
16+
err(1, "failed fork()");
17+
}
18+
19+
if (!pid) {
20+
setsid();
21+
_exit(execvp(cmd[0], cmd));
22+
}
23+
24+
if (waitpid(pid, &rc, 0))
25+
return -1;
26+
27+
return WEXITSTATUS(rc);
28+
}
29+
30+
void cb(int signo)
31+
{
32+
warnx("got signal %d, calling nft flush ruleset and exit.", signo);
33+
}
34+
35+
int main(int argc, char *argv[])
36+
{
37+
char *load[] = { "nft", "-f", NULL, NULL };
38+
char *flush[] = { "nft", "flush", "ruleset", NULL };
39+
40+
if (argc < 2 || access(argv[1], F_OK))
41+
errx(1, "Missing nft.conf argument.\nUsage:\n\t%s /path/to/nftables.conf", argv[0]);
42+
43+
signal(SIGTERM, cb);
44+
signal(SIGQUIT, cb);
45+
signal(SIGINT, cb);
46+
signal(SIGHUP, cb);
47+
48+
load[2] = argv[1];
49+
run(load);
50+
pause();
51+
run(flush);
52+
53+
return 0;
54+
}

0 commit comments

Comments
 (0)