From aa7ea66a0d8af8c34433125a6cdd0cfb0235d2f0 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 18 Jan 2024 22:52:18 +0100 Subject: [PATCH] confd: add support for setting container hostname Signed-off-by: Joachim Wiberg --- board/common/rootfs/usr/sbin/container | 9 +++++++-- src/confd/src/infix-containers.c | 5 ++++- src/confd/yang/infix-containers@2023-12-14.yang | 7 ++++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/board/common/rootfs/usr/sbin/container b/board/common/rootfs/usr/sbin/container index b90fb4b9..9bd59f7d 100755 --- a/board/common/rootfs/usr/sbin/container +++ b/board/common/rootfs/usr/sbin/container @@ -46,8 +46,8 @@ create() fi # --syslog --log-level info - log "Calling \"podman create --name $name --conmon-pidfile=$pidfn $env $network $port $args $image\"" - if podman create --name "$name" --conmon-pidfile="$pidfn" $env $network $port $args "$image"; then + log "Calling \"podman create --name $name --conmon-pidfile=$pidfn $hostname $env $network $port $args $image\"" + if podman create --name "$name" --conmon-pidfile="$pidfn" $hostname $env $network $port $args "$image"; then log "Successfully created container $name from $image" rm "/run/containers/env/${name}.env" initctl show "container:$name" |grep -q manual:yes || initctl -bq start "container:$name" @@ -87,6 +87,7 @@ options: -e, --env FILE Environment variables when creating container -f, --force Force operation, e.g. remove -h, --help Show this help text + --hostname NAME Set hostname when creating container -p, --publish PORT Publish ports when creating container Syntax: [[ip:][hostPort]:]containerPort[/protocol] -s, --simple Show output in simplified format @@ -129,6 +130,10 @@ while [ "$1" != "" ]; do usage exit 0 ;; + --hostname) + shift + hostname="--hostname $1" + ;; -p | --publish) shift port="$port -p $1" diff --git a/src/confd/src/infix-containers.c b/src/confd/src/infix-containers.c index 4484b256..820bdbca 100644 --- a/src/confd/src/infix-containers.c +++ b/src/confd/src/infix-containers.c @@ -29,7 +29,7 @@ static const struct srx_module_requirement reqs[] = { static int add(const char *name, struct lyd_node *cif) { const char *image = lydx_get_cattr(cif, "image"); - const char *restart_policy; + const char *restart_policy, *string; struct lyd_node *node; FILE *fp, *ep = NULL; char *restart = ""; /* Default restart:10 */ @@ -52,6 +52,9 @@ static int add(const char *name, struct lyd_node *cif) LYX_LIST_FOR_EACH(lyd_child(cif), node, "search") fprintf(fp, " --dns-search %s", lyd_get_value(node)); + if ((string = lydx_get_cattr(cif, "hostname"))) + fprintf(fp, " --hostname %s", string); + LYX_LIST_FOR_EACH(lyd_child(cif), node, "publish") fprintf(fp, " -p %s", lyd_get_value(node)); diff --git a/src/confd/yang/infix-containers@2023-12-14.yang b/src/confd/yang/infix-containers@2023-12-14.yang index 5a11e87c..c86f0f33 100644 --- a/src/confd/yang/infix-containers@2023-12-14.yang +++ b/src/confd/yang/infix-containers@2023-12-14.yang @@ -83,6 +83,11 @@ module infix-containers { } } + leaf hostname { + description "Sets the container host name that is available inside the container."; + type inet:domain-name; + } + leaf image { description "Docker image used for the container"; mandatory true; @@ -104,7 +109,7 @@ module infix-containers { description "Select network mode: none, host, or container network interfaces."; case host-network { - leaf host { + leaf host-network { description "Run in same network namespace as host."; type boolean; }