From 43c0082aa764eb5b83f71648764e969bbd153799 Mon Sep 17 00:00:00 2001 From: Tim Serong Date: Tue, 8 Oct 2013 23:53:16 +1100 Subject: [PATCH] Move /tmp/ccache setup to base element Commit c7d80dd (Cleanup mount points automatically) removed the unmount of $TMP_MOUNT_PATH/tmp/ccache in run_d_in_target() and moved the "rm /tmp/ccache" to elements/base/finalise.d/02-remove-ccache. There are two problems with this: 1) Not unmounting at the end of run_d_in_target() results in tmp/ccache being bind mounted muliple times on top of itself (three times, if you just run `disk-image-create base`). It is eventually unmounted, but somehow the auto unmount code is confused, and tries to unmount it one more time than it was mounted, which results in an error like "umount: /tmp/image.THQkZxQa/mnt/tmp/ccache: not mounted". This doesn't actually break anything, but it's a little messy. 2) "rm /tmp/ccache" in elements/base/finalise.d/02-remove-ccache never succeeds in removing /tmp/ccache, because that hook is invoked by run_d_in_target(), *while* /tmp/ccache is mounted. This present commit solves the above by moving the ccache setup glue out of img-functions and into the base element's root.d. This has the following implications: 1) lib/img-functions is a little cleaner. 2) /tmp/ccache is available in the chroot during the root, extra-data, pre-install, install and post-install stages. It is not available during block-device, finalise and cleanup stages as it will have been automatically unmounted by then. 3) /tmp/ccache won't be setup if you're building an image that doesn't include the base element. Change-Id: Ief4c0a6f4ec622db6c6f652776215684178d8943 --- elements/base/README.md | 5 +++++ elements/base/cleanup.d/01-ccache | 7 +++++++ elements/base/finalise.d/02-remove-ccache | 4 ---- elements/base/root.d/01-ccache | 15 +++++++++++++++ lib/common-functions | 6 ++++-- lib/img-defaults | 1 - lib/img-functions | 3 --- 7 files changed, 31 insertions(+), 10 deletions(-) create mode 100755 elements/base/cleanup.d/01-ccache delete mode 100755 elements/base/finalise.d/02-remove-ccache create mode 100755 elements/base/root.d/01-ccache diff --git a/elements/base/README.md b/elements/base/README.md index b88a603f..e1746c29 100644 --- a/elements/base/README.md +++ b/elements/base/README.md @@ -10,3 +10,8 @@ Notes: This may be as simple as having language-pack-XX installed in the pre-install stage + * This element ensures /tmp/ccache will be available in the chroot + during the root, extra-data, pre-install, install and post-install + stages. /tmp/ccache is unavailable during block-device, finalise + and cleanup stages as it will have been automatically unmounted + by then. diff --git a/elements/base/cleanup.d/01-ccache b/elements/base/cleanup.d/01-ccache new file mode 100755 index 00000000..fd23437a --- /dev/null +++ b/elements/base/cleanup.d/01-ccache @@ -0,0 +1,7 @@ +#!/bin/bash + +set -e + +# As this is run in cleanup, it's already been automatically unmounted, +# so all we need to do here is remove the directory +sudo rmdir $TMP_MOUNT_PATH/tmp/ccache || true diff --git a/elements/base/finalise.d/02-remove-ccache b/elements/base/finalise.d/02-remove-ccache deleted file mode 100755 index b550f726..00000000 --- a/elements/base/finalise.d/02-remove-ccache +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -set -e - -rmdir /tmp/ccache || true diff --git a/elements/base/root.d/01-ccache b/elements/base/root.d/01-ccache new file mode 100755 index 00000000..2bad4646 --- /dev/null +++ b/elements/base/root.d/01-ccache @@ -0,0 +1,15 @@ +#!/bin/bash + +set -e + +# Don't do anything if already mounted (if disk-image-create is invoked with +# no elements specified, this hook actually fires twice, once during +# `run_d root` for the base element, then again when `run_d root` is called +# after automatically pulling in the Ubuntu element) +grep " $TMP_MOUNT_PATH/tmp/ccache" /proc/mounts && exit + +DIB_CCACHE_DIR=${DIB_CCACHE_DIR:-$HOME/.cache/image-create/ccache} +mkdir -p $DIB_CCACHE_DIR + +sudo mkdir -p $TMP_MOUNT_PATH/tmp/ccache +sudo mount --bind $DIB_CCACHE_DIR $TMP_MOUNT_PATH/tmp/ccache diff --git a/lib/common-functions b/lib/common-functions index 19df7c4e..712f2c87 100644 --- a/lib/common-functions +++ b/lib/common-functions @@ -241,8 +241,10 @@ function create_base () { export TMP_MOUNT_PATH=$TMP_BUILD_DIR/mnt # Copy data in to the root. TARGET_ROOT=$TMP_MOUNT_PATH run_d root - if [ -z "$(ls $TMP_MOUNT_PATH | grep -v lost+found)" ] ; then - # Nothing copied in, use Ubuntu. + if [ -z "$(ls $TMP_MOUNT_PATH | grep -v '^lost+found\|tmp$')" ] ; then + # Nothing copied in, use Ubuntu. Note the test above allows + # root.d elements to put things in /tmp, and still have the + # automatic Ubuntu behaviour. echo "Adding ubuntu element as / had no contents" IMAGE_ELEMENT=$($SCRIPT_HOME/element-info --expand-dependencies $IMAGE_ELEMENT ubuntu) generate_hooks diff --git a/lib/img-defaults b/lib/img-defaults index 8054a539..31419515 100644 --- a/lib/img-defaults +++ b/lib/img-defaults @@ -20,4 +20,3 @@ FS_TYPE=${FS_TYPE:-ext4} # Used to set the file extension only at this stage. IMAGE_TYPE=${IMAGE_TYPE:-qcow2} IMAGE_NAME=${IMAGE_NAME:-image} -DIB_CCACHE_DIR=${DIB_CCACHE_DIR:-~/.cache/image-create/ccache} diff --git a/lib/img-functions b/lib/img-functions index cfb3f548..7d0f51e9 100644 --- a/lib/img-functions +++ b/lib/img-functions @@ -75,9 +75,6 @@ function run_d_in_target () { sudo mkdir $TMP_MOUNT_PATH/tmp/in_target.d sudo mount --bind ${TMP_HOOKS_PATH} $TMP_MOUNT_PATH/tmp/in_target.d sudo mount -o remount,ro,bind ${TMP_HOOKS_PATH} $TMP_MOUNT_PATH/tmp/in_target.d - sudo mkdir -p $TMP_MOUNT_PATH/tmp/ccache - mkdir -p $DIB_CCACHE_DIR - sudo mount --bind $DIB_CCACHE_DIR $TMP_MOUNT_PATH/tmp/ccache check_break before-$1 run_in_target bash trap "check_break after-error run_in_target bash" ERR run_in_target dib-run-parts /tmp/in_target.d/$1.d