[libvirt] [PATCH] Expose name + UUID to LXC containers via env variables

When spawning 'init' in the container, set LIBVIRT_LXC_UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX LIBVIRT_LXC_NAME=YYYYYYYYYYYY to allow guest software to detect & identify that they are in a container * src/lxc/lxc_container.c: Set LIBVIRT_LXC_UUID and LIBVIRT_LXC_NAME env vars --- src/lxc/lxc_container.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-) diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index d973984..269dc97 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -52,6 +52,7 @@ #include "util.h" #include "memory.h" #include "veth.h" +#include "uuid.h" #include "files.h" #define VIR_FROM_THIS VIR_FROM_LXC @@ -102,6 +103,20 @@ struct __lxc_child_argv { */ static int lxcContainerExecInit(virDomainDefPtr vmDef) { + char *uuidenv, *nameenv; + char uuidstr[VIR_UUID_STRING_BUFLEN]; + + virUUIDFormat(vmDef->uuid, uuidstr); + + if (virAsprintf(&uuidenv, "LIBVIRT_LXC_UUID=%s", uuidstr) < 0) { + virReportOOMError(); + return -1; + } + if (virAsprintf(&nameenv, "LIBVIRT_LXC_NAME=%s", vmDef->name) < 0) { + virReportOOMError(); + return -1; + } + const char *const argv[] = { vmDef->os.init, NULL, @@ -109,6 +124,8 @@ static int lxcContainerExecInit(virDomainDefPtr vmDef) const char *const envp[] = { "PATH=/bin:/sbin", "TERM=linux", + uuidenv, + nameenv, NULL, }; -- 1.7.4

On 02/22/2011 07:08 AM, Daniel P. Berrange wrote:
When spawning 'init' in the container, set
LIBVIRT_LXC_UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX LIBVIRT_LXC_NAME=YYYYYYYYYYYY
to allow guest software to detect & identify that they are in a container
Sounds reasonable. Does virt-what know to look for these yet?
@@ -109,6 +124,8 @@ static int lxcContainerExecInit(virDomainDefPtr vmDef) const char *const envp[] = { "PATH=/bin:/sbin", "TERM=linux", + uuidenv, + nameenv, NULL, };
ACK, but why aren't we using virCommand yet? -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

On Tue, Feb 22, 2011 at 07:50:43AM -0700, Eric Blake wrote:
On 02/22/2011 07:08 AM, Daniel P. Berrange wrote:
When spawning 'init' in the container, set
LIBVIRT_LXC_UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX LIBVIRT_LXC_NAME=YYYYYYYYYYYY
to allow guest software to detect & identify that they are in a container
Sounds reasonable. Does virt-what know to look for these yet?
@@ -109,6 +124,8 @@ static int lxcContainerExecInit(virDomainDefPtr vmDef) const char *const envp[] = { "PATH=/bin:/sbin", "TERM=linux", + uuidenv, + nameenv, NULL, };
ACK, but why aren't we using virCommand yet?
Containers are very special in their startup requirements. You can't use a plain fork()+exec() to create them. We use clone() call with a bunch of magic flags, and then do crazy stuff pivot_root/mount setup and finally execve() the container init process. Regards, 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 (2)
-
Daniel P. Berrange
-
Eric Blake