From a14fada0ee3d94940913ca69724a19f8f4c49253 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Mon, 14 Oct 2024 17:17:07 +0200 Subject: [PATCH] confd: add support for container hostname format specifiers This patch makes it possible to use format specifiers to "uniqiefy" a container's hostname in the same way as with ietf-system hostname. The following specifiers are currently supported: - %h default hostname from /etc/os-release - %i value of ID from /etc/os-release - %m last three octets of base MAC, e.g., c0-ff-ee Signed-off-by: Joachim Wiberg --- doc/container.md | 11 +++++++++++ src/confd/src/ietf-system.c | 2 +- src/confd/src/infix-containers.c | 15 +++++++++++++-- src/confd/yang/infix-containers.yang | 18 ++++++++++++++++-- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/doc/container.md b/doc/container.md index d092068d..56e40d7d 100644 --- a/doc/container.md +++ b/doc/container.md @@ -609,6 +609,17 @@ container ID (hash) is used, but this can be easily changed: admin@example:/> container shell system root@sys101:/# +In fact, the container `hostname` setting supports the same format +specifiers as the host's `hostname` setting: + + - `%i`: OS ID, from `/etc/os-release`, from Menuconfig branding + - `%h`: Default hostname, from `/etc/os-release`, from branding + - `%m`: NIC specific part of base MAC, e.g., to `c0-ff-ee` + - `%%`: Literal % + +The most useful combination is probably `"container-name-%m"`, which in +this example give the container hostname `container-name-c0-ff-ee`. + [^1]: this does not apply to the admin-exec command `container run`. This command is intended to be used for testing and evaluating container images. Such containers are given a private network diff --git a/src/confd/src/ietf-system.c b/src/confd/src/ietf-system.c index 39ef7dfd..c95b8b7b 100644 --- a/src/confd/src/ietf-system.c +++ b/src/confd/src/ietf-system.c @@ -1625,7 +1625,7 @@ static char *get_mac(struct confd *confd, char *mac, size_t len) * * XXX: PLEASE REFACTOR THIS INTO A PYTHON HELPER FOR FUTURE EXTENSIONS, OR BUGS! */ -static int hostnamefmt(struct confd *confd, char **fmt) +int hostnamefmt(struct confd *confd, char **fmt) { size_t hostlen, fmt_len; char *hostname; diff --git a/src/confd/src/infix-containers.c b/src/confd/src/infix-containers.c index 320070ef..ef576c8f 100644 --- a/src/confd/src/infix-containers.c +++ b/src/confd/src/infix-containers.c @@ -21,6 +21,9 @@ #define ACTIVE_QUEUE "/var/lib/containers/active" #define LOGGER "logger -t container -p local1.notice" +int hostnamefmt(struct confd *confd, char **fmt); + + static int add(const char *name, struct lyd_node *cif) { const char *image = lydx_get_cattr(cif, "image"); @@ -46,8 +49,16 @@ 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); + if ((string = lydx_get_cattr(cif, "hostname"))) { + char *fmt = (char *)string; + + if (hostnamefmt(&confd, &fmt)) { + ERRNO("%s: failed setting custom hostname", name); + } else { + fprintf(fp, " --hostname %s", fmt); + free(fmt); + } + } if (lydx_is_enabled(cif, "read-only")) fprintf(fp, " --read-only"); diff --git a/src/confd/yang/infix-containers.yang b/src/confd/yang/infix-containers.yang index 61c50baf..beb86601 100644 --- a/src/confd/yang/infix-containers.yang +++ b/src/confd/yang/infix-containers.yang @@ -22,6 +22,17 @@ module infix-containers { prefix infix-if; } + import infix-system { + prefix infix-sys; + } + + revision 2024-10-14 { + description "Two major changes: + - Allow changing name of host interfaces inside container + - Support hostname format specifiers, like ietf-system"; + reference "internal"; + } + revision 2024-03-27 { description "Add support for capabilities."; reference "internal"; @@ -160,8 +171,11 @@ module infix-containers { } leaf hostname { - description "Sets the container host name that is available inside the container."; - type inet:domain-name; + description "Sets the container host name, available inside the container. + + Format specifiers are for, default hostname, ID, and the + last three octets in base MAC, e.g., c0-ff-ee"; + type infix-sys:hostname; } leaf privileged {