[libvirt] [PATCH v2] 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... In v2: - Rewrite loop with virBufferTrim - Don't include first pty in env - Only set env if at least one pty is listed Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- src/lxc/lxc_container.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 51fa1b3..c885782 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -196,10 +196,33 @@ 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; + + /* 'container_ptys' must exclude the PTY associated with + * the /dev/console device, hence start at 1 not 0 + */ + for (i = 1; 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; + } + virBufferAdd(&buf, ttyPaths[i] + 5, -1); + virBufferAddChar(&buf, ' '); + } + virBufferTrim(&buf, NULL, 1); + + if (virBufferError(&buf)) + return NULL; virUUIDFormat(vmDef->uuid, uuidstr); @@ -212,11 +235,14 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef) virCommandAddEnvString(cmd, "TERM=linux"); virCommandAddEnvString(cmd, "container=lxc-libvirt"); virCommandAddEnvPair(cmd, "container_uuid", uuidstr); + if (nttyPaths > 1) + virCommandAddEnvPair(cmd, "container_ttys", virBufferCurrentContent(&buf)); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name); if (vmDef->os.cmdline) virCommandAddEnvPair(cmd, "LIBVIRT_LXC_CMDLINE", vmDef->os.cmdline); + virBufferFreeAndReset(&buf); return cmd; } @@ -1789,7 +1815,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/16/2013 12:04 PM, 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.
http://cgit.freedesktop.org/systemd/systemd/commit/?id=1d97ff7dd71902a5604c2...
In v2: - Rewrite loop with virBufferTrim - Don't include first pty in env - Only set env if at least one pty is listed
The v2 info doesn't need to be stored in git.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- src/lxc/lxc_container.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)
ACK -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On 12/17/2013 03:04 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.
http://cgit.freedesktop.org/systemd/systemd/commit/?id=1d97ff7dd71902a5604c2...
In v2: - Rewrite loop with virBufferTrim - Don't include first pty in env - Only set env if at least one pty is listed
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- src/lxc/lxc_container.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 51fa1b3..c885782 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -196,10 +196,33 @@ 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; + + /* 'container_ptys' must exclude the PTY associated with + * the /dev/console device, hence start at 1 not 0 + */ + for (i = 1; i < nttyPaths; i++) { + if (!STRPREFIX(ttyPaths[0], "/dev/")) {
So the ttyPaths[0] is right here??
+ virReportError(VIR_ERR_INTERNAL_ERROR, + _("Expected a /dev path for '%s'"), + ttyPaths[0]); + virBufferFreeAndReset(&buf); + return NULL; + } + virBufferAdd(&buf, ttyPaths[i] + 5, -1); + virBufferAddChar(&buf, ' '); + } + virBufferTrim(&buf, NULL, 1); + + if (virBufferError(&buf)) + return NULL;
virUUIDFormat(vmDef->uuid, uuidstr);
@@ -212,11 +235,14 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef) virCommandAddEnvString(cmd, "TERM=linux"); virCommandAddEnvString(cmd, "container=lxc-libvirt"); virCommandAddEnvPair(cmd, "container_uuid", uuidstr); + if (nttyPaths > 1) + virCommandAddEnvPair(cmd, "container_ttys", virBufferCurrentContent(&buf)); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name); if (vmDef->os.cmdline) virCommandAddEnvPair(cmd, "LIBVIRT_LXC_CMDLINE", vmDef->os.cmdline);
+ virBufferFreeAndReset(&buf); return cmd; }
@@ -1789,7 +1815,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)

On Tue, Dec 17, 2013 at 08:39:17AM +0800, Gao feng wrote:
On 12/17/2013 03:04 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.
http://cgit.freedesktop.org/systemd/systemd/commit/?id=1d97ff7dd71902a5604c2...
In v2: - Rewrite loop with virBufferTrim - Don't include first pty in env - Only set env if at least one pty is listed
Signed-off-by: Daniel P. Berrange <berrange@redhat.com> --- src/lxc/lxc_container.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 51fa1b3..c885782 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -196,10 +196,33 @@ 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; + + /* 'container_ptys' must exclude the PTY associated with + * the /dev/console device, hence start at 1 not 0 + */ + for (i = 1; i < nttyPaths; i++) { + if (!STRPREFIX(ttyPaths[0], "/dev/")) {
So the ttyPaths[0] is right here??
Doh, will fix that. Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
participants (3)
-
Daniel P. Berrange
-
Eric Blake
-
Gao feng