[libvirt] [PATCH] Set the 'container_ttys' env variable for LXC consoles

From: "Daniel P. Berrange" <berrange@redhat.com> Systemd specified that any /dev/pts/NNN device on which it is expected to spawn a agetty login, should be listed in the 'container_ttys' env variable. It should just contain the relative paths, eg 'pts/0' not '/dev/pts/0' and should be space separated. http://cgit.freedesktop.org/systemd/systemd/commit/?id=1d97ff7dd71902a5604c2... Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- src/lxc/lxc_container.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 51fa1b3..f0b526f 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -196,10 +196,30 @@ int lxcContainerHasReboot(void) * * Returns a virCommandPtr */ -static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef) +static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef, + char **ttyPaths, + size_t nttyPaths) { char uuidstr[VIR_UUID_STRING_BUFLEN]; virCommandPtr cmd; + virBuffer buf = VIR_BUFFER_INITIALIZER; + size_t i; + + for (i = 0 ; i < nttyPaths ; i++) { + if (!STRPREFIX(ttyPaths[0], "/dev/")) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Expected a /dev path for '%s'"), + ttyPaths[0]); + virBufferFreeAndReset(&buf); + return NULL; + } + if (i) + virBufferAddLit(&buf, " "); + virBufferAdd(&buf, ttyPaths[i] + 5, -1); + } + + if (virBufferError(&buf)) + return NULL; virUUIDFormat(vmDef->uuid, uuidstr); @@ -214,9 +234,11 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef) virCommandAddEnvPair(cmd, "container_uuid", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name); + virCommandAddEnvPair(cmd, "container_ttys", virBufferCurrentContent(&buf)); if (vmDef->os.cmdline) virCommandAddEnvPair(cmd, "LIBVIRT_LXC_CMDLINE", vmDef->os.cmdline); + virBufferFreeAndReset(&buf); return cmd; } @@ -1789,7 +1811,9 @@ static int lxcContainerChild(void *data) if ((hasReboot = lxcContainerHasReboot()) < 0) goto cleanup; - cmd = lxcContainerBuildInitCmd(vmDef); + cmd = lxcContainerBuildInitCmd(vmDef, + argv->ttyPaths, + argv->nttyPaths); virCommandWriteArgLog(cmd, 1); if (lxcContainerSetID(vmDef) < 0) -- 1.8.3.1

On 12/13/2013 09:52 AM, Daniel P. Berrange wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
Systemd specified that any /dev/pts/NNN device on which it is expected to spawn a agetty login, should be listed in
s/ a / an /
the 'container_ttys' env variable. It should just contain the relative paths, eg 'pts/0' not '/dev/pts/0' and should be space separated.
http://cgit.freedesktop.org/systemd/systemd/commit/?id=1d97ff7dd71902a5604c2...
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- src/lxc/lxc_container.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-)
ACK with nits:
+ virBuffer buf = VIR_BUFFER_INITIALIZER; + size_t i; + + for (i = 0 ; i < nttyPaths ; i++) {
Doesn't this violate 'make syntax-check' for space before ';'?
+ if (!STRPREFIX(ttyPaths[0], "/dev/")) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Expected a /dev path for '%s'"), + ttyPaths[0]); + virBufferFreeAndReset(&buf); + return NULL; + } + if (i) + virBufferAddLit(&buf, " ");
virBufferAddChar is more efficient.
+ virBufferAdd(&buf, ttyPaths[i] + 5, -1); + }
I kind of like the style of: for (...) { add element and separator } use virBufferTrim() to undo the final separator as slightly shorter than: for (...) { add element if (not last) add separator } or: for (...) { if (not first) add separator add element } but it's not necessary to change that. -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On 12/14/2013 12:52 AM, Daniel P. Berrange wrote:
From: "Daniel P. Berrange" <berrange@redhat.com>
Systemd specified that any /dev/pts/NNN device on which it is expected to spawn a agetty login, should be listed in the 'container_ttys' env variable. It should just contain the relative paths, eg 'pts/0' not '/dev/pts/0' and should be space separated.
Just FYI systemd will skrip off /dev,so /dev/pts/0 is ok too.
http://cgit.freedesktop.org/systemd/systemd/commit/?id=1d97ff7dd71902a5604c2...
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- src/lxc/lxc_container.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-)
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 51fa1b3..f0b526f 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -196,10 +196,30 @@ int lxcContainerHasReboot(void) * * Returns a virCommandPtr */ -static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef) +static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef, + char **ttyPaths, + size_t nttyPaths) { char uuidstr[VIR_UUID_STRING_BUFLEN]; virCommandPtr cmd; + virBuffer buf = VIR_BUFFER_INITIALIZER; + size_t i; + + for (i = 0 ; i < nttyPaths ; i++) { + if (!STRPREFIX(ttyPaths[0], "/dev/")) {
^^^^^^^^^^ ttyPaths[i]
+ virReportError(VIR_ERR_INTERNAL_ERROR, + _("Expected a /dev path for '%s'"), + ttyPaths[0]); + virBufferFreeAndReset(&buf); + return NULL; + } + if (i) + virBufferAddLit(&buf, " "); + virBufferAdd(&buf, ttyPaths[i] + 5, -1);
had better to make sure there is no /dev//pts/x case.
+ } + + if (virBufferError(&buf)) + return NULL;
virUUIDFormat(vmDef->uuid, uuidstr);
@@ -214,9 +234,11 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef) virCommandAddEnvPair(cmd, "container_uuid", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name); + virCommandAddEnvPair(cmd, "container_ttys", virBufferCurrentContent(&buf)); if (vmDef->os.cmdline) virCommandAddEnvPair(cmd, "LIBVIRT_LXC_CMDLINE", vmDef->os.cmdline);
+ virBufferFreeAndReset(&buf); return cmd; }
@@ -1789,7 +1811,9 @@ static int lxcContainerChild(void *data) if ((hasReboot = lxcContainerHasReboot()) < 0) goto cleanup;
- cmd = lxcContainerBuildInitCmd(vmDef); + cmd = lxcContainerBuildInitCmd(vmDef, + argv->ttyPaths, + argv->nttyPaths); virCommandWriteArgLog(cmd, 1);
if (lxcContainerSetID(vmDef) < 0)
ACK
participants (3)
-
Daniel P. Berrange
-
Eric Blake
-
Gao feng