Skip to contentSkip to conversation

docker: Support building qemu-user powered docker test images

PATCH In review Alex Bennée 2 participants 11 messages Jul 12, 2016 – Jul 19, 2016

lore
Share & tools
lore
#0 lore raw

v5 cover docker: Support building qemu-user powered docker test images

Hi,

This is v5 of the series (Fam posted the last revision). I've dropped all the fancy chroot-in-docker stuff in favour of making the debootstrap.pre script more platform agnostic. If it can't find debootstrap on the system it just clones the upstream SCM and uses the script from there.

There is a patch pending in Debian to remove one minor niggle that stops that working at the moment, see:

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=830869

The pre script also does a bunch of additional checking of the pre-requisites including if binfmt_misc is setup for qemu-$ARCH. It doesn't go quite as far as checking the setup path though but it should be sufficient.

The only remaining problem is how to handle the flexibility of multiple architectures in the Make system. As the dockerfiles live in the tests/docker/dockerfile directory all the normal docker test constructions work. However if you want a cross-arch docker image you need to build it "by hand" with the appropriate DEB_ARCH/DEB_TYPE and --include-executable stanza.

So any ideas about that?

Alex Bennée (5):
tests/docker/docker.py: docker_dir outside build tests/docker/docker.py: support --include-executable tests/docker/docker.py: check and run .pre script tests/docker/dockerfiles: new debian-bootstrap.docker tests/docker/docker.py: add update operation

Fam Zheng (2):
docker: More sensible run script
docker: Fix exit code if $CMD failed

 tests/docker/Makefile.include                    |   4 +-
 tests/docker/docker.py                           | 144 +++++++++++++++++++++--
 tests/docker/dockerfiles/debian-bootstrap.docker |  21 ++++
 tests/docker/dockerfiles/debian-bootstrap.pre    |  82 +++++++++++++
 tests/docker/run                                 |  16 ++-
 5 files changed, 252 insertions(+), 15 deletions(-)
 create mode 100644 tests/docker/dockerfiles/debian-bootstrap.docker
 create mode 100755 tests/docker/dockerfiles/debian-bootstrap.pre
Signature · 1 line
2.7.4
#1 lore raw

PATCH v5 1/7 tests/docker/docker.py: docker_dir outside build

Instead of letting the build_image create the temporary working dir we move the creation to the build command. This is preparation for the later patches where additional files can be added to the build context before the build step is run.

We also ensure we remove the build context after we are done (mkdtemp doesn't do this automatically for you).

---
v2
- new for v2
v3
- add r-b tag

---
 tests/docker/docker.py | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
tests/docker/docker.py +13 -8
index 0151362..ae40bb3 100755
@@ -20,7 +20,7 @@ import atexit
 import uuid
 import argparse
 import tempfile
-from shutil import copy
+from shutil import copy, rmtree
 
 def _text_checksum(text):
     """Calculate a digest string unique to the text content"""
@@ -87,20 +87,20 @@ class Docker(object):
         labels = json.loads(resp)[0]["Config"].get("Labels", {})
         return labels.get("com.qemu.dockerfile-checksum", "")
 
-    def build_image(self, tag, dockerfile, df_path, quiet=True, argv=None):
+    def build_image(self, tag, docker_dir, dockerfile, quiet=True, argv=None):
         if argv == None:
             argv = []
-        tmp_dir = tempfile.mkdtemp(prefix="docker_build")
 
-        tmp_df = tempfile.NamedTemporaryFile(dir=tmp_dir, suffix=".docker")
+        tmp_df = tempfile.NamedTemporaryFile(dir=docker_dir, suffix=".docker")
         tmp_df.write(dockerfile)
 
         tmp_df.write("\n")
         tmp_df.write("LABEL com.qemu.dockerfile-checksum=%s" %
                      _text_checksum(dockerfile))
         tmp_df.flush()
+
         self._do(["build", "-t", tag, "-f", tmp_df.name] + argv + \
-                 [tmp_dir],
+                 [docker_dir],
                  quiet=quiet)
 
     def image_matches_dockerfile(self, tag, dockerfile):
@@ -164,10 +164,15 @@ class BuildCommand(SubCommand):
         if dkr.image_matches_dockerfile(tag, dockerfile):
             if not args.quiet:
                 print "Image is up to date."
-            return 0
+        else:
+            # Create a docker context directory for the build
+            docker_dir = tempfile.mkdtemp(prefix="docker_build")
+
+            dkr.build_image(tag, docker_dir, dockerfile,
+                            quiet=args.quiet, argv=argv)
+
+            rmtree(docker_dir)
 
-        dkr.build_image(tag, dockerfile, args.dockerfile,
-                        quiet=args.quiet, argv=argv)
         return 0
 
 class CleanCommand(SubCommand):
Signature · 1 line
2.7.4
#2 lore raw

PATCH v5 2/7 tests/docker/docker.py: support --include-executable

In reply to Alex Bennée

When passed the path to a binary we copy it and any linked libraries (if it is dynamically linked) into the docker build context. These can then be included by a dockerfile with the line:

# Copy all of context into container
ADD . /

This is mainly intended for setting up foreign architecture docker images which use qemu-$arch to do cross-architecture linux-user execution. It also relies on the host and guest file-system following reasonable multi-arch layouts so the copied libraries don't clash with the guest ones.

---
v2
- change name of option
- require full path to executable
- clean-up the copy code
v3
- safely deal with static builds
- quietly pass if makedirs not needed
- split copy routine and so lib resolver

---
 tests/docker/docker.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
tests/docker/docker.py +58 -0
index ae40bb3..96d906e 100755
@@ -20,6 +20,7 @@ import atexit
 import uuid
 import argparse
 import tempfile
+import re
 from shutil import copy, rmtree
 
 def _text_checksum(text):
@@ -38,6 +39,54 @@ def _guess_docker_command():
     raise Exception("Cannot find working docker command. Tried:\n%s" % \
                     commands_txt)
 
+def _copy_with_mkdir(src, root_dir, sub_path):
+    """Copy src into root_dir, creating sub_path as needed."""
+    dest_dir = os.path.normpath("%s/%s" % (root_dir, sub_path))
+    try:
+        os.makedirs(dest_dir)
+    except OSError:
+        # we can safely ignore already created directories
+        pass
+
+    dest_file = "%s/%s" % (dest_dir, os.path.basename(src))
+    copy(src, dest_file)
+
+
+def _get_so_libs(executable):
+    """Return a list of libraries associated with an executable.
+
+    The paths may be symbolic links which would need to be resolved to
+    ensure theright data is copied."""
+
+    libs = []
+    ldd_re = re.compile(r"(/.*/)(\S*)")
+    try:
+        ldd_output = subprocess.check_output(["ldd", executable])
+        for line in ldd_output.split("\n"):
+            search = ldd_re.search(line)
+            if search and len(search.groups()) == 2:
+                so_path = search.groups()[0]
+                so_lib = search.groups()[1]
+                libs.append("%s/%s" % (so_path, so_lib))
+    except subprocess.CalledProcessError:
+        print "%s had no associated libraries (static build?)" % (executable)
+
+    return libs
+
+def _copy_binary_with_libs(src, dest_dir):
+    """Copy a binary executable and all its dependant libraries.
+
+    This does rely on the host file-system being fairly multi-arch
+    aware so the file don't clash with the guests layout."""
+
+    _copy_with_mkdir(src, dest_dir, "/usr/bin")
+
+    libs = _get_so_libs(src)
+    if libs:
+        for l in libs:
+            so_path = os.path.dirname(l)
+            _copy_with_mkdir(l , dest_dir, so_path)
+
 class Docker(object):
     """ Running Docker commands """
     def __init__(self):
@@ -151,6 +200,10 @@ class BuildCommand(SubCommand):
     """ Build docker image out of a dockerfile. Arguments: <tag> <dockerfile>"""
     name = "build"
     def args(self, parser):
+        parser.add_argument("--include-executable", "-e",
+                            help="""Specify a binary that will be copied to the
+                            container together with all its dependent
+                            libraries""")
         parser.add_argument("tag",
                             help="Image Tag")
         parser.add_argument("dockerfile",
@@ -168,6 +221,11 @@ class BuildCommand(SubCommand):
             # Create a docker context directory for the build
             docker_dir = tempfile.mkdtemp(prefix="docker_build")
 
+            # Do we include a extra binary?
+            if args.include_executable:
+                _copy_binary_with_libs(args.include_executable,
+                                       docker_dir)
+
             dkr.build_image(tag, docker_dir, dockerfile,
                             quiet=args.quiet, argv=argv)
 
Signature · 1 line
2.7.4
#3 lore raw

PATCH v5 3/7 tests/docker/docker.py: check and run .pre script

In reply to Alex Bennée

The docker script will now search for an associated $dockerfile.pre script which gets run in the same build context as the dockerfile will be. This is to support pre-seeding the build context before running the docker build.

---
v3
- new for v3, split from previous patch
- use check_call when running the pre-script

---
 tests/docker/docker.py | 6 ++++++
 1 file changed, 6 insertions(+)
tests/docker/docker.py +6 -0
index 96d906e..244901d 100755
@@ -221,6 +221,12 @@ class BuildCommand(SubCommand):
             # Create a docker context directory for the build
             docker_dir = tempfile.mkdtemp(prefix="docker_build")
 
+            # Is there a .pre file to run in the build context?
+            docker_pre = os.path.splitext(args.dockerfile)[0]+".pre"
+            if os.path.exists(docker_pre):
+                subprocess.check_call(os.path.realpath(docker_pre),
+                                      cwd=docker_dir)
+
             # Do we include a extra binary?
             if args.include_executable:
                 _copy_binary_with_libs(args.include_executable,
Signature · 1 line
2.7.4
#4 lore raw

PATCH v5 4/7 tests/docker/dockerfiles: new debian-bootstrap.docker

In reply to Alex Bennée

Together with the debian-bootstrap.pre script can now build an arbitrary architecture of Debian using debootstrap. This allows debootstrap to set up its first stage before the container is built.

To build a container you need a command line like:

  DEB_ARCH=armhf DEB_TYPE=testing \
    ./tests/docker/docker.py build \
    --include-executable=arm-linux-user/qemu-arm debian:armhf \
    ./tests/docker/dockerfiles/debian-bootstrap.docker

Although a number of non-debian systems package the debootstrap script it is fairly portable in itself. Assuming we have some sort of fakeroot implementation we can just clone the upstream repository and use the script from there.

---
v2
- use .pre script instead of embedded HOST_CMD
- make default image include all QEMU build-deps
v3
- split docker.py from introduction of bootstrap
v4
- handle no installed debootstrap [see BTS #830869]
- add a bunch of pre-requisite checking (fakeroot, binfmt_misc)
- add mapping from DEB_ARCH to QEMU_BIN

---
 tests/docker/dockerfiles/debian-bootstrap.docker | 21 ++++++
 tests/docker/dockerfiles/debian-bootstrap.pre    | 82 ++++++++++++++++++++++++
 2 files changed, 103 insertions(+)
 create mode 100644 tests/docker/dockerfiles/debian-bootstrap.docker
 create mode 100755 tests/docker/dockerfiles/debian-bootstrap.pre
tests/docker/dockerfiles/debian-bootstrap.docker new file +21 -0
new file mode 100644
index 0000000..3a9125e
@@ -0,0 +1,21 @@
+# Create Debian Bootstrap Image
+#
+# This is intended to be pre-poluated by:
+#  - a first stage debootstrap (see debian-bootstrap.pre)
+#  - a native qemu-$arch that binfmt_misc will run
+FROM scratch
+
+# Add everything from the context into the container
+ADD . /
+
+# Patch all mounts as docker already has stuff set up
+RUN sed -i 's/in_target mount/echo not for docker in_target mount/g' /debootstrap/functions
+
+# Run stage 2
+RUN /debootstrap/debootstrap --second-stage
+
+# At this point we can install additional packages if we want
+# Duplicate deb line as deb-src
+RUN cat /etc/apt/sources.list | sed "s/deb/deb-src/" >> /etc/apt/sources.list
+RUN apt-get update
+RUN apt-get -y build-dep qemu
tests/docker/dockerfiles/debian-bootstrap.pre new file +82 -0

82 lines not on this page · raw message

Signature · 1 line
2.7.4
#5 lore raw

PATCH v5 5/7 tests/docker/docker.py: add update operation

In reply to Alex Bennée

This adds a new operation to the docker script to allow updating of binaries in an existing container. This is because it would be inefficient to re-build the whole container just for an update to the QEMU binary.

To update the executable run:

    ./tests/docker/docker.py update \
        debian:armhf ./arm-linux-user/qemu-arm

---
v3
- new in v3

---
 tests/docker/docker.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 58 insertions(+), 1 deletion(-)
tests/docker/docker.py +58 -1
index 244901d..a8753f8 100755
@@ -21,6 +21,8 @@ import uuid
 import argparse
 import tempfile
 import re
+from tarfile import TarFile, TarInfo
+from StringIO import StringIO
 from shutil import copy, rmtree
 
 def _text_checksum(text):
@@ -94,9 +96,11 @@ class Docker(object):
         self._instances = []
         atexit.register(self._kill_instances)
 
-    def _do(self, cmd, quiet=True, **kwargs):
+    def _do(self, cmd, quiet=True, infile=None, **kwargs):
         if quiet:
             kwargs["stdout"] = subprocess.PIPE
+        if infile:
+            kwargs["stdin"] = infile
         return subprocess.call(self._command + cmd, **kwargs)
 
     def _do_kill_instances(self, only_known, only_active=True):
@@ -152,6 +156,11 @@ class Docker(object):
                  [docker_dir],
                  quiet=quiet)
 
+    def update_image(self, tag, tarball, quiet=True):
+        "Update a tagged image using "
+
+        self._do(["build", "-t", tag, "-"], quiet=quiet, infile=tarball)
+
     def image_matches_dockerfile(self, tag, dockerfile):
         try:
             checksum = self.get_image_dockerfile_checksum(tag)
@@ -239,6 +248,54 @@ class BuildCommand(SubCommand):
 
         return 0
 
+class UpdateCommand(SubCommand):
+    """ Update a docker image with new executables. Arguments: <tag> <executable>"""
+    name = "update"
+    def args(self, parser):
+        parser.add_argument("tag",
+                            help="Image Tag")
+        parser.add_argument("executable",
+                            help="Executable to copy")
+
+    def run(self, args, argv):
+        # Create a temporary tarball with our whole build context and
+        # dockerfile for the update
+        tmp = tempfile.NamedTemporaryFile(suffix="dckr.tar.gz")
+        tmp_tar = TarFile(fileobj=tmp, mode='w')
+
+        # Add the executable to the tarball
+        bn = os.path.basename(args.executable)
+        ff = "/usr/bin/%s" % bn
+        tmp_tar.add(args.executable, arcname=ff)
+
+        # Add any associated libraries
+        libs = _get_so_libs(args.executable)
+        if libs:
+            for l in libs:
+                tmp_tar.add(os.path.realpath(l), arcname=l)
+
+        # Create a Docker buildfile
+        df = StringIO()
+        df.write("FROM %s\n" % args.tag)
+        df.write("ADD . /\n")
+        df.seek(0)
+
+        df_tar = TarInfo(name="Dockerfile")
+        df_tar.size = len(df.buf)
+        tmp_tar.addfile(df_tar, fileobj=df)
+
+        tmp_tar.close()
+
+        # reset the file pointers
+        tmp.flush()
+        tmp.seek(0)
+
+        # Run the build with our tarball context
+        dkr = Docker()
+        dkr.update_image(args.tag, tmp, quiet=args.quiet)
+
+        return 0
+
 class CleanCommand(SubCommand):
     """Clean up docker instances"""
     name = "clean"
Signature · 1 line
2.7.4
#6 lore raw

PATCH v5 6/7 docker: More sensible run script

In reply to Alex Bennée

From: Fam Zheng <[email protected]>

It is very easy to figure out current directory and bash option from the execution, so do less in the Makefile invocation command line, and figure both options in the script.

This makes the next patch easier.

---
 tests/docker/Makefile.include |  4 +---
 tests/docker/run              | 12 +++++++++---
 2 files changed, 10 insertions(+), 6 deletions(-)
tests/docker/Makefile.include +1 -3
index f88c0a7..c5546ee 100644
@@ -114,10 +114,8 @@ docker-run-%: docker-qemu-src
 				-e CCACHE_DIR=/var/tmp/ccache \
 				-v $$(realpath $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
 				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
-				-w /var/tmp/qemu \
 				qemu:$(IMAGE) \
-				$(if $V,/bin/bash -x ,) \
-				./run \
+				/var/tmp/qemu/run \
 				$(CMD); \
 			, "  RUN $(CMD) in $(IMAGE)")))
 
tests/docker/run +9 -3
index ec3d119..575e732 100755
@@ -11,6 +11,12 @@
 # or (at your option) any later version. See the COPYING file in
 # the top-level directory.
 
+if test -n "$V"; then
+    set -x
+fi
+
+BASE="$(dirname $(realpath $0))"
+
 # Prepare the environment
 . /etc/profile || true
 export PATH=/usr/lib/ccache:$PATH
@@ -24,10 +30,10 @@ export TEST_DIR=/tmp/qemu-test
 mkdir -p $TEST_DIR/{src,build,install}
 
 # Extract the source tarballs
-tar -C $TEST_DIR/src -xzf qemu.tgz
+tar -C $TEST_DIR/src -xzf $BASE/qemu.tgz
 for p in dtc pixman; do
-    if test -f $p.tgz; then
-        tar -C $TEST_DIR/src/$p -xzf $p.tgz
+    if test -f $BASE/$p.tgz; then
+        tar -C $TEST_DIR/src/$p -xzf $BASE/$p.tgz
         export FEATURES="$FEATURES $p"
     fi
 done
Signature · 1 line
2.7.4
#7 lore raw

PATCH v5 7/7 docker: Fix exit code if $CMD failed

In reply to Alex Bennée

From: Fam Zheng <[email protected]>

---
 tests/docker/run | 4 ++++
 1 file changed, 4 insertions(+)
tests/docker/run +4 -0
index 575e732..38ce789 100755
@@ -11,6 +11,8 @@
 # or (at your option) any later version. See the COPYING file in
 # the top-level directory.
 
+set -e
+
 if test -n "$V"; then
     set -x
 fi
@@ -61,4 +63,6 @@ elif test -n "$DEBUG"; then
     echo
     # Force error after shell exits
     $SHELL && exit 1
+else
+    exit 1
 fi
Signature · 1 line
2.7.4
#8 lore raw
In reply to Alex Bennée
Quoted 18 lines · Alex Bennée

Hi,

This is v5 of the series (Fam posted the last revision). I've dropped all the fancy chroot-in-docker stuff in favour of making the debootstrap.pre script more platform agnostic. If it can't find debootstrap on the system it just clones the upstream SCM and uses the script from there.

There is a patch pending in Debian to remove one minor niggle that stops that working at the moment, see:

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=830869

The pre script also does a bunch of additional checking of the pre-requisites including if binfmt_misc is setup for qemu-$ARCH. It doesn't go quite as far as checking the setup path though but it should be sufficient.

Quoted 8 lines

The only remaining problem is how to handle the flexibility of multiple architectures in the Make system. As the dockerfiles live in the tests/docker/dockerfile directory all the normal docker test constructions work. However if you want a cross-arch docker image you need to build it "by hand" with the appropriate DEB_ARCH/DEB_TYPE and --include-executable stanza.

So any ideas about that?

Any thoughts on that Fam? Can we merge it without the final step on the grounds it makes for useful testing?

Quoted 12 lines

Alex Bennée (5):
tests/docker/docker.py: docker_dir outside build tests/docker/docker.py: support --include-executable tests/docker/docker.py: check and run .pre script tests/docker/dockerfiles: new debian-bootstrap.docker tests/docker/docker.py: add update operation

Fam Zheng (2):
docker: More sensible run script
docker: Fix exit code if $CMD failed

Quoted 8 lines
 tests/docker/Makefile.include                    |   4 +-
 tests/docker/docker.py                           | 144 +++++++++++++++++++++--
 tests/docker/dockerfiles/debian-bootstrap.docker |  21 ++++
 tests/docker/dockerfiles/debian-bootstrap.pre    |  82 +++++++++++++
 tests/docker/run                                 |  16 ++-
 5 files changed, 252 insertions(+), 15 deletions(-)
 create mode 100644 tests/docker/dockerfiles/debian-bootstrap.docker
 create mode 100755 tests/docker/dockerfiles/debian-bootstrap.pre

--
Alex Bennée

#9 lore raw
Quoted 1 line · Alex Bennée
Quoted 1 line

So any ideas about that?

Hi Alex,

Sorry for the late reply.

Quoted 3 lines

Any thoughts on that Fam? Can we merge it without the final step on the grounds it makes for useful testing?

Yes, we can, though I'd like to make sure we don't break existing docker tests. Specifically we need to skip building and spawning the debian-bootstrap image if its dependencies are not met. To do that we can define a magic exit code for .pre scripts, and catch it in docker.py.

What do you think?

Fam

Quoted 26 lines
Quoted 13 lines

Alex Bennée (5):
tests/docker/docker.py: docker_dir outside build tests/docker/docker.py: support --include-executable tests/docker/docker.py: check and run .pre script tests/docker/dockerfiles: new debian-bootstrap.docker tests/docker/docker.py: add update operation

Fam Zheng (2):
docker: More sensible run script
docker: Fix exit code if $CMD failed

Quoted 8 lines
 tests/docker/Makefile.include                    |   4 +-
 tests/docker/docker.py                           | 144 +++++++++++++++++++++--
 tests/docker/dockerfiles/debian-bootstrap.docker |  21 ++++
 tests/docker/dockerfiles/debian-bootstrap.pre    |  82 +++++++++++++
 tests/docker/run                                 |  16 ++-
 5 files changed, 252 insertions(+), 15 deletions(-)
 create mode 100644 tests/docker/dockerfiles/debian-bootstrap.docker
 create mode 100755 tests/docker/dockerfiles/debian-bootstrap.pre

--
Alex Bennée

#10 lore raw
Quoted 8 lines · Fam Zheng
Quoted 1 line · Alex Bennée
Quoted 1 line

So any ideas about that?

Hi Alex,

Sorry for the late reply.

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

Any thoughts on that Fam? Can we merge it without the final step on the grounds it makes for useful testing?

Yes, we can, though I'd like to make sure we don't break existing docker tests. Specifically we need to skip building and spawning the debian-bootstrap image if its dependencies are not met. To do that we can define a magic exit code for .pre scripts, and catch it in docker.py.

It does exit if the pre-reqs aren't met, I guess we could default the DEB_ARCH/DEB_TYPE as well.

Quoted 31 lines

What do you think?

Fam

Quoted 26 lines
Quoted 13 lines

Alex Bennée (5):
tests/docker/docker.py: docker_dir outside build tests/docker/docker.py: support --include-executable tests/docker/docker.py: check and run .pre script tests/docker/dockerfiles: new debian-bootstrap.docker tests/docker/docker.py: add update operation

Fam Zheng (2):
docker: More sensible run script
docker: Fix exit code if $CMD failed

Quoted 8 lines
 tests/docker/Makefile.include                    |   4 +-
 tests/docker/docker.py                           | 144 +++++++++++++++++++++--
 tests/docker/dockerfiles/debian-bootstrap.docker |  21 ++++
 tests/docker/dockerfiles/debian-bootstrap.pre    |  82 +++++++++++++
 tests/docker/run                                 |  16 ++-
 5 files changed, 252 insertions(+), 15 deletions(-)
 create mode 100644 tests/docker/dockerfiles/debian-bootstrap.docker
 create mode 100755 tests/docker/dockerfiles/debian-bootstrap.pre

--
Alex Bennée

--
Alex Bennée