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 <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2024-10-14 18:00:00 +02:00
parent 7346f122fd
commit a14fada0ee
4 changed files with 41 additions and 5 deletions
+11
View File
@@ -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
+1 -1
View File
@@ -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;
+13 -2
View File
@@ -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");
+16 -2
View File
@@ -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 {