From eff5b2312b5ae34a85be77ee733f58a0d0fe43ea Mon Sep 17 00:00:00 2001
From: Tobias Henkel <tobias.henkel@bmw.de>
Date: Thu, 18 Oct 2018 16:49:40 +0200
Subject: [PATCH] Add a post-root.d phase

While trying to get docker image pre-caching to work we couldn't get a
docker daeomon to run within the chrooted environment. However we got
docker running with the help of bwrap outside of the chrooted
environment. The only option so far for this is the block-device.d
phase. But this has the problem that it runs after the image size has
been calculated. This leads to broken builds if the docker images
being pulled are big.

This can be solved by adding a post-root.d phase that runs outside the
chroot but before the image size calculation.

Change-Id: I36c2a81e2d9f5069f18ce5b0d52c5f1c7212c3ae
---
 diskimage_builder/lib/disk-image-create                  | 1 +
 doc/source/developer/developing_elements.rst             | 9 +++++++++
 releasenotes/notes/post-root-stage-51da051bcdfbc55f.yaml | 7 +++++++
 3 files changed, 17 insertions(+)
 create mode 100644 releasenotes/notes/post-root-stage-51da051bcdfbc55f.yaml

diff --git a/diskimage_builder/lib/disk-image-create b/diskimage_builder/lib/disk-image-create
index 8400ae709..89aa88188 100644
--- a/diskimage_builder/lib/disk-image-create
+++ b/diskimage_builder/lib/disk-image-create
@@ -345,6 +345,7 @@ run_d_in_target pre-install
 run_d_in_target install
 do_extra_package_install
 run_d_in_target post-install
+run_d post-root
 # ensure we do not have a lost+found directory in the root folder
 # that could cause copy to fail (it will be created again later
 # when creating the file system, if it needs such directory)
diff --git a/doc/source/developer/developing_elements.rst b/doc/source/developer/developing_elements.rst
index d37bc09c1..342bbb721 100644
--- a/doc/source/developer/developing_elements.rst
+++ b/doc/source/developer/developing_elements.rst
@@ -69,7 +69,9 @@ The phases are:
 #. ``pre-install.d``
 #. ``install.d``
 #. ``post-install.d``
+#. ``post-root.d``
 #. ``block-device.d``
+#. ``pre-finalise.d``
 #. ``finalise.d``
 #. ``cleanup.d``
 
@@ -124,6 +126,13 @@ The phases are:
 
   * runs: **in chroot**
 
+``post-root.d``
+  Run code outside the chroot. This is a good place to perform tasks that
+  cannot run inside the chroot and must run after installing things. The
+  root filesystem content is rooted at ``$TMP_BUILD_DIR/mnt``.
+
+  * runs: **outside chroot**
+
 ``block-device.d``
   Customise the block device that the image will be made on (for example to
   make partitions). Runs after the target tree has been fully populated but
diff --git a/releasenotes/notes/post-root-stage-51da051bcdfbc55f.yaml b/releasenotes/notes/post-root-stage-51da051bcdfbc55f.yaml
new file mode 100644
index 000000000..c4f8b3d8e
--- /dev/null
+++ b/releasenotes/notes/post-root-stage-51da051bcdfbc55f.yaml
@@ -0,0 +1,7 @@
+---
+features:
+  - |
+    A ``post-root`` stage is added that runs outside the chroot and
+    before the image size calculation.  For example, this may be useful
+    for running things that copy much data into the image and need to be
+    outside of the chroot and after install stages which run inside the chroot.