blob: 192d2f55ba0513e14b41bffe70caa43a2032dd0c [file] [log] [blame]
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""This module provides some utilities used by LXC and its tools.
"""
import netifaces
import common
from autotest_lib.client.bin import utils
from autotest_lib.client.common_lib import error
def is_in_container():
"""Check if the process is running inside a container.
@return: True if the process is running inside a container, otherwise False.
"""
try:
utils.run('cat /proc/1/cgroup | grep "/lxc/" || false')
return True
except error.CmdError:
return False
def path_exists(path):
"""Check if path exists.
If the process is not running with root user, os.path.exists may fail to
check if a path owned by root user exists. This function uses command
`test -e` to check if path exists.
@param path: Path to check if it exists.
@return: True if path exists, otherwise False.
"""
try:
utils.run('sudo test -e "%s"' % path)
return True
except error.CmdError:
return False
def get_host_ip():
"""Get the IP address of the host running containers on lxcbr*.
This function gets the IP address on network interface lxcbr*. The
assumption is that lxc uses the network interface started with "lxcbr".
@return: IP address of the host running containers.
"""
lxc_network = None
for name in netifaces.interfaces():
if name.startswith('lxcbr'):
lxc_network = name
break
if not lxc_network:
raise error.ContainerError('Failed to find network interface used by '
'lxc. All existing interfaces are: %s' %
netifaces.interfaces())
return netifaces.ifaddresses(lxc_network)[netifaces.AF_INET][0]['addr']