[lxc-users] How to determine whether you are inside container (LXC/LXD) or not

Roidev KohaLappi roikohadev at gmail.com
Thu Sep 15 21:59:27 UTC 2016


Hi Linuxcontainers.org, :)

this is my first post to this list and I just wanted to reply to a post
https://lists.linuxcontainers.org/pipermail/lxc-users/2016-August/012083.html.


Because I just subscribed to the list, I can not reply to that post, so
here is a short script
I've been using and it seems to be working ok in LXC/LXD envs that I'm on.

---------------
#!/bin/bash
#####################################################################
## _Bash function to determine the 'host_role'
# Check whether we are a LXC-container or not (i.e host)?
# - = (no args)
# - return (echo): 0 = not container/host, 1 = LXC-container
## Author: Jukka Aaltonen, Koha-Lappi -migration project
#####################################################################
function _host-role {
    rc=0
    host_role="Host/platform"

    ## Check whether we are a LXC-container
    sudo grep -qa "container=lxc" /proc/1/environ
    if [[ $? -eq 0 ]]; then
        host_role="LXC-container"; rc=1
    else
        host_role="Host/platform"; rc=0
    fi
    echo "Hostname (LXC-guest/host):$(hostname) ($host_role)"

    return $rc
}
---------------

HTH

-jukka
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linuxcontainers.org/pipermail/lxc-users/attachments/20160916/75349708/attachment.html>


More information about the lxc-users mailing list