[autotest] Reland: Change to make lxc-info call work with lxc 3.0.0
There is a change to the config syntax between lxc 2.x.x and
lxc 3.x.x, determine the version of lxc we are running and
make the correct call.
I did it this way rather than just catch the error as I feel
that version differences are going to continue to happen and this
should make it easier to make future changes.
Reland fix:
Changed to ensure that the code will not crash in the event of being
called with on a machine where lxc is not installed.
BUG=chromium:844050
TEST=tested on moblab running lxc.3.x and tryjobs
Change-Id: I9ccb41ae40bbe173d5c03494aee102443ce8533e
Reviewed-on: https://chromium-review.googlesource.com/1110556
Commit-Ready: Keith Haddow <haddowk@chromium.org>
Tested-by: Keith Haddow <haddowk@chromium.org>
Reviewed-by: Keith Haddow <haddowk@chromium.org>
Reviewed-by: Aviv Keshet <akeshet@chromium.org>
diff --git a/site_utils/lxc/container.py b/site_utils/lxc/container.py
index 302b0ad..3dfcf01 100644
--- a/site_utils/lxc/container.py
+++ b/site_utils/lxc/container.py
@@ -126,6 +126,8 @@
The attributes available are defined in ATTRIBUTES constant.
"""
+ _LXC_VERSION = None
+
def __init__(self, container_path, name, attribute_values, src=None,
snapshot=False):
"""Initialize an object of LXC container with given attribute values.
@@ -177,6 +179,9 @@
self.name)
self._id = None
+ if not Container._LXC_VERSION:
+ Container._LXC_VERSION = lxc_utils.get_lxc_version()
+
@classmethod
def create_from_existing_dir(cls, lxc_path, name, **kwargs):
@@ -290,8 +295,14 @@
@return: Path to the rootfs of the container.
"""
+ lxc_rootfs_config_name = 'lxc.rootfs'
+ # Check to see if the major lxc version is 3 or greater
+ if Container._LXC_VERSION:
+ logging.info("Detected lxc version %s", Container._LXC_VERSION)
+ if Container._LXC_VERSION[0] >= 3:
+ lxc_rootfs_config_name = 'lxc.rootfs.path'
if not self._rootfs:
- lxc_rootfs = self._get_lxc_config('lxc.rootfs')[0]
+ lxc_rootfs = self._get_lxc_config(lxc_rootfs_config_name)[0]
cloned_from_snapshot = ':' in lxc_rootfs
if cloned_from_snapshot:
self._rootfs = lxc_rootfs.split(':')[-1]
diff --git a/site_utils/lxc/utils.py b/site_utils/lxc/utils.py
index d1e3947..05e0ae2 100644
--- a/site_utils/lxc/utils.py
+++ b/site_utils/lxc/utils.py
@@ -7,6 +7,7 @@
import logging
import os
+import re
import shutil
import tempfile
import unittest
@@ -223,6 +224,31 @@
for command in commands:
result = utils.run("sudo %s" % command)
+
+def get_lxc_version():
+ """Gets the current version of lxc if available."""
+ cmd = 'sudo lxc-info --version'
+ result = utils.run(cmd)
+ if result and result.exit_status == 0:
+ version = re.split("[.-]", result.stdout.strip())
+ if len(version) < 3:
+ logging.error("LXC version is not expected format %s.",
+ result.stdout.strip())
+ return None
+ return_value = []
+ for a in version[:3]:
+ try:
+ return_value.append(int(a))
+ except ValueError:
+ logging.error(("LXC version contains non numerical version "
+ "number %s (%s)."), a, result.stdout.strip())
+ return None
+ return return_value
+ else:
+ logging.error("Unable to determine LXC version.")
+ return None
+
+
class LXCTests(unittest.TestCase):
"""Thin wrapper to call correct setup for LXC tests."""