[libvirt] [PATCH] send 'container=libvirt' in env to container init

This allows upstart scripts to detect that they are in a container and modify their behavior accordingly. In this way, the same ubuntu maverick or natty image with the 'lxcguest' package installed can be booted on bare metal, in kvm, or as a libvirt or liblxc container. Signed-off-by: Chuck Short <zulcss@ubuntu.com> Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com> --- src/lxc/lxc_container.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 9ae93b5..a70aeeb 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -112,6 +112,7 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef) virCommandAddEnvString(cmd, "PATH=/bin:/sbin"); virCommandAddEnvString(cmd, "TERM=linux"); + virCommandAddEnvString(cmd, "container=libvirt"); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name); -- 1.7.4.1

On 05/16/2011 08:47 AM, Serge Hallyn wrote:
This allows upstart scripts to detect that they are in a container and modify their behavior accordingly.
In this way, the same ubuntu maverick or natty image with the 'lxcguest' package installed can be booted on bare metal, in kvm, or as a libvirt or liblxc container.
Signed-off-by: Chuck Short <zulcss@ubuntu.com> Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com> --- src/lxc/lxc_container.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 9ae93b5..a70aeeb 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -112,6 +112,7 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef)
virCommandAddEnvString(cmd, "PATH=/bin:/sbin"); virCommandAddEnvString(cmd, "TERM=linux"); + virCommandAddEnvString(cmd, "container=libvirt");
POSIX reserves lowercase env names for the user. Is upstart really using a lower case name, or should this be an upper case name?
virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name);
Can upstart be taught to look for LIBIVRT_LXC_UUID instead? -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

Quoting Eric Blake (eblake@redhat.com):
On 05/16/2011 08:47 AM, Serge Hallyn wrote:
This allows upstart scripts to detect that they are in a container and modify their behavior accordingly.
In this way, the same ubuntu maverick or natty image with the 'lxcguest' package installed can be booted on bare metal, in kvm, or as a libvirt or liblxc container.
Signed-off-by: Chuck Short <zulcss@ubuntu.com> Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com> --- src/lxc/lxc_container.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 9ae93b5..a70aeeb 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -112,6 +112,7 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef)
virCommandAddEnvString(cmd, "PATH=/bin:/sbin"); virCommandAddEnvString(cmd, "TERM=linux"); + virCommandAddEnvString(cmd, "container=libvirt");
POSIX reserves lowercase env names for the user. Is upstart really using a lower case name, or should this be an upper case name?
No upstart isn't doing anything itself :) Just blame me - the upstart scripts I put into lxcguest do in fact check for lower-case. What do you mean by 'for the user'? For the user to type into the boot args, or something else?
virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name);
Can upstart be taught to look for LIBIVRT_LXC_UUID instead?
That's a possibility, yes. It just makes it less symmetric to the liblxc case, but if you prefer that I see no reason why it wouldn't work. thanks, -serge

On 05/16/2011 09:14 AM, Serge Hallyn wrote:
@@ -112,6 +112,7 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef)
virCommandAddEnvString(cmd, "PATH=/bin:/sbin"); virCommandAddEnvString(cmd, "TERM=linux"); + virCommandAddEnvString(cmd, "container=libvirt");
POSIX reserves lowercase env names for the user. Is upstart really using a lower case name, or should this be an upper case name?
No upstart isn't doing anything itself :) Just blame me - the upstart scripts I put into lxcguest do in fact check for lower-case.
What do you mean by 'for the user'? For the user to type into the boot args, or something else?
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html Environment variable names used by the utilities in the Shell and Utilities volume of POSIX.1-2008 consist solely of uppercase letters, digits, and the <underscore> ( '_' ) from the characters defined in Portable Character Set and do not begin with a digit. Other characters may be permitted by an implementation; applications shall tolerate the presence of such names. Uppercase and lowercase letters shall retain their unique identities and shall not be folded together. The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities. That is, if your startup scripts make anything in the system, including /bin/sh, behave differently because it is in a container, then it's probably better to use an upper-case name, since /bin/sh should not behave differently regardless of what lower-case names are in the environment. But this is a borderline case - upstart is outside the realm of POSIX, and you can easily arrange for $container to be reset before any standard applications are ever invoked from within the container.
virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name);
Can upstart be taught to look for LIBIVRT_LXC_UUID instead?
That's a possibility, yes. It just makes it less symmetric to the liblxc case, but if you prefer that I see no reason why it wouldn't work.
Do you have a pointer to the liblxc code that's inspecting its environment? -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

Quoting Eric Blake (eblake@redhat.com):
On 05/16/2011 09:14 AM, Serge Hallyn wrote:
@@ -112,6 +112,7 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef)
virCommandAddEnvString(cmd, "PATH=/bin:/sbin"); virCommandAddEnvString(cmd, "TERM=linux"); + virCommandAddEnvString(cmd, "container=libvirt");
POSIX reserves lowercase env names for the user. Is upstart really using a lower case name, or should this be an upper case name?
No upstart isn't doing anything itself :) Just blame me - the upstart scripts I put into lxcguest do in fact check for lower-case.
What do you mean by 'for the user'? For the user to type into the boot args, or something else?
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
Environment variable names used by the utilities in the Shell and Utilities volume of POSIX.1-2008 consist solely of uppercase letters, digits, and the <underscore> ( '_' ) from the characters defined in Portable Character Set and do not begin with a digit. Other characters may be permitted by an implementation; applications shall tolerate the presence of such names. Uppercase and lowercase letters shall retain their unique identities and shall not be folded together. The name space of environment variable names containing lowercase letters is reserved for applications. Applications can define any environment variables with names from this name space without modifying the behavior of the standard utilities.
That is, if your startup scripts make anything in the system, including /bin/sh, behave differently because it is in a container, then it's
Ah, this is about 'proper' environment variables. But environment variables are just being used to pass name=value boot arguments to init. So I wouldn't have thought this would apply. It would apply from the point of view of libvirt starting a program, but not from the point of view of init trying to parse its command line arguments. Very interesting. When you consider the init=/bin/sh case, I can see where it would make sense.
probably better to use an upper-case name, since /bin/sh should not behave differently regardless of what lower-case names are in the environment.
But this is a borderline case - upstart is outside the realm of POSIX, and you can easily arrange for $container to be reset before any standard applications are ever invoked from within the container.
virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name);
Can upstart be taught to look for LIBIVRT_LXC_UUID instead?
That's a possibility, yes. It just makes it less symmetric to the liblxc case, but if you prefer that I see no reason why it wouldn't work.
Do you have a pointer to the liblxc code that's inspecting its environment?
Not liblxc - liblxc just sends 'container=lxc' to init. One (of two) of the upstart scripts which use that is here: http://bazaar.launchpad.net/~serge-hallyn/ubuntu/natty/lxc/lxc-clone/view/he... thanks, -serge

On Mon, May 16, 2011 at 09:47:02AM -0500, Serge Hallyn wrote:
This allows upstart scripts to detect that they are in a container and modify their behavior accordingly.
In this way, the same ubuntu maverick or natty image with the 'lxcguest' package installed can be booted on bare metal, in kvm, or as a libvirt or liblxc container.
Signed-off-by: Chuck Short <zulcss@ubuntu.com> Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com> --- src/lxc/lxc_container.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 9ae93b5..a70aeeb 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -112,6 +112,7 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef)
virCommandAddEnvString(cmd, "PATH=/bin:/sbin"); virCommandAddEnvString(cmd, "TERM=linux"); + virCommandAddEnvString(cmd, "container=libvirt"); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name);
I've no objection to this, but IMHO the value should be container=libvirt-lxc 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 :|

Quoting Daniel P. Berrange (berrange@redhat.com):
On Mon, May 16, 2011 at 09:47:02AM -0500, Serge Hallyn wrote:
This allows upstart scripts to detect that they are in a container and modify their behavior accordingly.
In this way, the same ubuntu maverick or natty image with the 'lxcguest' package installed can be booted on bare metal, in kvm, or as a libvirt or liblxc container.
Signed-off-by: Chuck Short <zulcss@ubuntu.com> Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com> --- src/lxc/lxc_container.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 9ae93b5..a70aeeb 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -112,6 +112,7 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef)
virCommandAddEnvString(cmd, "PATH=/bin:/sbin"); virCommandAddEnvString(cmd, "TERM=linux"); + virCommandAddEnvString(cmd, "container=libvirt"); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name);
I've no objection to this, but IMHO the value should be
container=libvirt-lxc
So long as it is guaranteed that henceforth LIBVIRT_LXC_UUID=x will always be passed to init, that suffices. But I worry about it changing in 6 or 12 months to a slightly different name, or not always being passed. Do you think there is any chance of that happening? thanks, -serge

On Tue, May 17, 2011 at 09:40:01AM -0500, Serge Hallyn wrote:
Quoting Daniel P. Berrange (berrange@redhat.com):
On Mon, May 16, 2011 at 09:47:02AM -0500, Serge Hallyn wrote:
This allows upstart scripts to detect that they are in a container and modify their behavior accordingly.
In this way, the same ubuntu maverick or natty image with the 'lxcguest' package installed can be booted on bare metal, in kvm, or as a libvirt or liblxc container.
Signed-off-by: Chuck Short <zulcss@ubuntu.com> Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com> --- src/lxc/lxc_container.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 9ae93b5..a70aeeb 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -112,6 +112,7 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef)
virCommandAddEnvString(cmd, "PATH=/bin:/sbin"); virCommandAddEnvString(cmd, "TERM=linux"); + virCommandAddEnvString(cmd, "container=libvirt"); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name);
I've no objection to this, but IMHO the value should be
container=libvirt-lxc
So long as it is guaranteed that henceforth LIBVIRT_LXC_UUID=x will always be passed to init, that suffices. But I worry about it changing in 6 or 12 months to a slightly different name, or not always being passed. Do you think there is any chance of that happening?
No, we intend the LIBVIRT_LXC_UUID/NAME vars to be a permanent part of the environment given to containers. 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 :|

On 05/17/2011 08:40 AM, Serge Hallyn wrote:
+++ b/src/lxc/lxc_container.c @@ -112,6 +112,7 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef)
virCommandAddEnvString(cmd, "PATH=/bin:/sbin"); virCommandAddEnvString(cmd, "TERM=linux"); + virCommandAddEnvString(cmd, "container=libvirt"); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name);
I've no objection to this, but IMHO the value should be
container=libvirt-lxc
So long as it is guaranteed that henceforth LIBVIRT_LXC_UUID=x will always be passed to init, that suffices. But I worry about it changing in 6 or 12 months to a slightly different name, or not always being passed. Do you think there is any chance of that happening?
No risk of change. Libvirt is documented as providing a stable library, and LIBVIRT_LXC_UUID is externally visible, therefore it is now part of our documented interface and we cannot change it without breaking backwards compatibility. You can safely use $LIBVIRT_LXC_UUID. Since Daniel was okay with also adding $container, do you want to keep this patch under consideration, or are you okay dropping it in favor of $LIBVIRT_LXC_UUID? -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

Quoting Eric Blake (eblake@redhat.com):
On 05/17/2011 08:40 AM, Serge Hallyn wrote:
+++ b/src/lxc/lxc_container.c @@ -112,6 +112,7 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef)
virCommandAddEnvString(cmd, "PATH=/bin:/sbin"); virCommandAddEnvString(cmd, "TERM=linux"); + virCommandAddEnvString(cmd, "container=libvirt"); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr); virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name);
I've no objection to this, but IMHO the value should be
container=libvirt-lxc
So long as it is guaranteed that henceforth LIBVIRT_LXC_UUID=x will always be passed to init, that suffices. But I worry about it changing in 6 or 12 months to a slightly different name, or not always being passed. Do you think there is any chance of that happening?
No risk of change. Libvirt is documented as providing a stable library, and LIBVIRT_LXC_UUID is externally visible, therefore it is now part of our documented interface and we cannot change it without breaking backwards compatibility. You can safely use $LIBVIRT_LXC_UUID.
Since Daniel was okay with also adding $container, do you want to keep this patch under consideration, or are you okay dropping it in favor of $LIBVIRT_LXC_UUID?
I'm ok dropping it - thanks! -serge
participants (3)
-
Daniel P. Berrange
-
Eric Blake
-
Serge Hallyn