SHA256
1
0
forked from pool/libvirt
libvirt/faec1958-lxc-set-hostname-based-on-container-name.patch
Cédric Bosdonnat d3c3ef874c Accepting request 561313 from home:cbosdonnat:branches:Virtualization
- Fix apparmor rules for virt-aa-helper (bsc#1074265)
  fix-virt-aa-helper-profile.patch
- Update upstreamed patches
  Removed patches:
  * daemon-close-crasher.patch
  * lxc-hostname.patch
  Added patches:
  * 2089ab21-netserver-close-clients-before-stopping-all-drivers.patch
  * b475a91b-add-virStringFilterChars-string-utility.patch
  * faec1958-lxc-set-hostname-based-on-container-name.patch

OBS-URL: https://build.opensuse.org/request/show/561313
OBS-URL: https://build.opensuse.org/package/show/Virtualization/libvirt?expand=0&rev=655
2018-01-03 12:04:09 +00:00

70 lines
2.0 KiB
Diff

From faec1958614bfcdb535b1bcc0ddac8cde4516e1a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Bosdonnat?= <cbosdonnat@suse.com>
Date: Mon, 18 Dec 2017 15:48:33 +0100
Subject: [PATCH 2/2] lxc: set a hostname based on the container name
Set a transient hostname on containers. The hostname is computed from
the container name, only keeping the valid characters [a-zA-Z0-9-] in it.
This filtering is based on RFC 1123 and allows a digit to start the
hostname.
---
src/lxc/lxc_container.c | 35 +++++++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index b7216d6ee..96fceaf1b 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -2159,6 +2159,37 @@ static int lxcContainerSetUserGroup(virCommandPtr cmd,
return 0;
}
+static const char hostname_validchars[] =
+ "abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "0123456789-";
+
+static int lxcContainerSetHostname(virDomainDefPtr def)
+{
+ int ret = -1;
+ char *name = NULL;
+ char *hostname = NULL;
+
+ /* Filter the VM name to get a valid hostname */
+ if (VIR_STRDUP(name, def->name) < 0)
+ goto cleanup;
+
+ /* RFC 1123 allows 0-9 digits as a first character in hostname */
+ virStringFilterChars(name, hostname_validchars);
+ hostname = name;
+ if (strlen(name) > 0 && name[0] == '-')
+ hostname = name + 1;
+
+ if (sethostname(hostname, strlen(hostname)) < 0) {
+ virReportSystemError(errno, "%s", _("Failed to set hostname"));
+ goto cleanup;
+ }
+ ret = 0;
+
+ cleanup:
+ VIR_FREE(name);
+ return ret;
+}
/**
* lxcContainerChild:
@@ -2269,6 +2300,10 @@ static int lxcContainerChild(void *data)
goto cleanup;
}
+ if (lxcContainerSetHostname(vmDef) < 0)
+ goto cleanup;
+
+
/* drop a set of root capabilities */
if (lxcContainerDropCapabilities(vmDef, !!hasReboot) < 0)
goto cleanup;
--
2.15.1