blob: 4368013fecef448eb3381c455f81338b1a4e3858 [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 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:'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
if not lxc_network:
raise error.ContainerError('Failed to find network interface used by '
'lxc. All existing interfaces are: %s' %
return netifaces.ifaddresses(lxc_network)[netifaces.AF_INET][0]['addr']