<div dir="ltr">Hello,<div><br></div><div>For non root user, I check for /dev/lxc/console</div><div><br></div><div><div>[grid@ol6 ~]$ [ -c /dev/lxc/console ] && echo "inside lxc container"</div><div>inside lxc container</div>
</div><div><br></div><div>Alvaro.</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Dec 26, 2013 at 9:52 AM, Stéphane Graber <span dir="ltr"><<a href="mailto:stgraber@ubuntu.com" target="_blank">stgraber@ubuntu.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Wed, Dec 25, 2013 at 10:44:21PM +0200, David Shwatrz wrote:<br>
> Hello, lxc users,<br>
><br>
> Is there a way for a user who is inside some shell in a container<br>
> to know that he is inside a container? I am not talking about setting<br>
> specific different host names to containers, but the question is: is<br>
> there something general/inherent to containers, which indicates that<br>
> a user is inside a container ?<br>
><br>
> Best,<br>
> DavidS<br>
<br>
</div>Yes, there are a few ways to do so.<br>
<br>
If on Ubuntu, the easiest way is to call "running-in-container" which<br>
will return 0 if you are and 1 if you're not.<br>
On Ubuntu you can then read /run/container_type to know what technology<br>
is used (we detect lxc, libvirt-lxc, openvz and vserver).<br>
<br>
If not on Ubuntu, you can basically use the same trick we use on Ubuntu<br>
to detect containers which is roughly:<br>
<br>
cat /proc/1/environ | tr '\0' '\n' | grep ^container<br>
<br>
Which wiill return "container=lxc" in a LXC container. For other<br>
container types we need a few more tricks, here are the ones we<br>
currently use:<br>
<br>
# Detect old-style libvirt<br>
if [ -z "$container" ]; then<br>
[ -n "$LIBVIRT_LXC_UUID" ] && container=lxc-libvirt<br>
fi<br>
<br>
# Detect OpenVZ containers<br>
if [ -z "$container" ]; then<br>
[ -d /proc/vz ] && [ ! -d /proc/bc ] && container=openvz<br>
fi<br>
<br>
# Detect vserver<br>
if [ -z "$container" ]; then<br>
VXID="$(cat /proc/self/status | grep ^VxID | cut -f2)" || true<br>
[ "${VXID:-0}" -gt 1 ] && container=vserver<br>
fi<br>
<br>
<br>
Between the container env variable and those, you should be able to<br>
detect pretty much all kind of containers (unless they are tweaked to<br>
hide those information from you).<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Stéphane Graber<br>
Ubuntu developer<br>
<a href="http://www.ubuntu.com" target="_blank">http://www.ubuntu.com</a><br>
</font></span><br>_______________________________________________<br>
lxc-users mailing list<br>
<a href="mailto:lxc-users@lists.linuxcontainers.org">lxc-users@lists.linuxcontainers.org</a><br>
<a href="http://lists.linuxcontainers.org/listinfo/lxc-users" target="_blank">http://lists.linuxcontainers.org/listinfo/lxc-users</a><br></blockquote></div><br></div>