On Wed, Dec 25, 2013 at 10:44:21PM +0200, David Shwatrz wrote:
> Hello, lxc users,
> 
> Is there a way for a user who is inside some shell in a container
> to know that he is inside a container? I am not talking about setting
> specific different host names to containers, but the question is: is
> there something general/inherent to containers,  which indicates that
> a user is  inside a container ?
> 
> Best,
> DavidS

Yes, there are a few ways to do so.

If on Ubuntu, the easiest way is to call "running-in-container" which
will return 0 if you are and 1 if you're not.
On Ubuntu you can then read /run/container_type to know what technology
is used (we detect lxc, libvirt-lxc, openvz and vserver).

If not on Ubuntu, you can basically use the same trick we use on Ubuntu
to detect containers which is roughly:

cat /proc/1/environ | tr '\0' '\n' | grep ^container

Which wiill return "container=lxc" in a LXC container. For other
container types we need a few more tricks, here are the ones we
currently use:

    # Detect old-style libvirt
    if [ -z "$container" ]; then
        [ -n "$LIBVIRT_LXC_UUID" ] && container=lxc-libvirt
    fi

    # Detect OpenVZ containers
    if [ -z "$container" ]; then
        [ -d /proc/vz ] && [ ! -d /proc/bc ] && container=openvz
    fi

    # Detect vserver
    if [ -z "$container" ]; then
        VXID="$(cat /proc/self/status | grep ^VxID | cut -f2)" || true
        [ "${VXID:-0}" -gt 1 ] && container=vserver
    fi


Between the container env variable and those, you should be able to
detect pretty much all kind of containers (unless they are tweaked to
hide those information from you).

-- 
Stéphane Graber
Ubuntu developer
http://www.ubuntu.com

Attachment: signature.asc
Description: Digital signature

_______________________________________________
lxc-users mailing list
[email protected]
http://lists.linuxcontainers.org/listinfo/lxc-users

Reply via email to