2015-08-28 04:49:04 +00:00
|
|
|
#!/bin/bash
|
2012-11-15 03:20:32 +00:00
|
|
|
# Copyright 2012 Hewlett-Packard Development Company, L.P.
|
|
|
|
# All Rights Reserved.
|
2013-01-25 22:23:56 +00:00
|
|
|
#
|
2012-11-15 03:20:32 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
2013-01-25 22:23:56 +00:00
|
|
|
#
|
2012-11-15 03:20:32 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2013-01-25 22:23:56 +00:00
|
|
|
#
|
2012-11-15 03:20:32 +00:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2018-10-11 09:03:17 +00:00
|
|
|
# This is the "internal" verison of dib-run-parts. If you modify
|
|
|
|
# this, be aware that it needs to run both inside and outside the
|
|
|
|
# chroot environment, so it needs to be very generic.
|
2017-03-16 02:58:31 +00:00
|
|
|
DIB_RUN_PARTS=${_LIB}/dib-run-parts
|
|
|
|
|
2013-05-02 21:02:38 +00:00
|
|
|
function tmpfs_check() {
|
2015-05-27 14:11:13 +00:00
|
|
|
local echo_message=${1:-1}
|
2013-05-02 21:02:38 +00:00
|
|
|
[ "$DIB_NO_TMPFS" == "0" ] || return 1
|
|
|
|
[ -r /proc/meminfo ] || return 1
|
|
|
|
total_kB=$(awk '/^MemTotal/ { print $2 }' /proc/meminfo)
|
2013-08-26 11:57:34 +00:00
|
|
|
# tmpfs uses by default 50% of the available RAM, so the RAM should be at least
|
|
|
|
# the double of the minimum tmpfs size required
|
2014-09-30 12:52:46 +00:00
|
|
|
RAM_NEEDED=$(($DIB_MIN_TMPFS * 2))
|
2013-08-26 11:57:34 +00:00
|
|
|
[ $total_kB -lt $(($RAM_NEEDED*1024*1024)) ] || return 0
|
2015-05-27 14:11:13 +00:00
|
|
|
if [ $echo_message == '1' ]; then
|
|
|
|
echo "WARNING: Not enough RAM to use tmpfs for build. Using ${TMP_DIR:-/tmp}. ($total_kB < ${RAM_NEEDED}G)"
|
|
|
|
fi
|
2013-05-02 21:02:38 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2012-11-09 18:18:08 +00:00
|
|
|
function mk_build_dir () {
|
2016-06-03 05:21:03 +00:00
|
|
|
TMP_BUILD_DIR=$(mktemp -t -d --tmpdir=${TMP_DIR:-/tmp} dib_build.XXXXXXXX)
|
|
|
|
TMP_IMAGE_DIR=$(mktemp -t -d --tmpdir=${TMP_DIR:-/tmp} dib_image.XXXXXXXX)
|
2012-11-09 18:18:08 +00:00
|
|
|
[ $? -eq 0 ] || die "Failed to create tmp directory"
|
2013-07-31 18:26:05 +00:00
|
|
|
export TMP_BUILD_DIR
|
2013-05-02 21:02:38 +00:00
|
|
|
if tmpfs_check ; then
|
2014-03-26 10:06:54 +00:00
|
|
|
sudo mount -t tmpfs tmpfs $TMP_BUILD_DIR
|
2014-05-01 09:22:15 +00:00
|
|
|
sudo mount -t tmpfs tmpfs $TMP_IMAGE_DIR
|
2015-06-24 13:21:06 +00:00
|
|
|
sudo chown $(id -u):$(id -g) $TMP_BUILD_DIR $TMP_IMAGE_DIR
|
2013-05-02 21:02:38 +00:00
|
|
|
fi
|
2014-10-11 00:01:37 +00:00
|
|
|
trap trap_cleanup EXIT
|
2012-11-09 18:18:08 +00:00
|
|
|
echo Building in $TMP_BUILD_DIR
|
2016-07-16 20:16:13 +00:00
|
|
|
export TMP_IMAGE_DIR
|
2014-10-01 19:34:21 +00:00
|
|
|
export OUT_IMAGE_PATH=$TMP_IMAGE_PATH
|
2012-11-09 18:18:08 +00:00
|
|
|
export TMP_HOOKS_PATH=$TMP_BUILD_DIR/hooks
|
|
|
|
}
|
|
|
|
|
2014-03-25 01:16:06 +00:00
|
|
|
function finish_image () {
|
2014-06-26 06:22:31 +00:00
|
|
|
if [ -f $1 -a ${OVERWRITE_OLD_IMAGE:-0} -eq 0 ]; then
|
2014-05-13 13:57:35 +00:00
|
|
|
old_image="${1%.*}"-$(date +%Y.%m.%d-%H.%M.%S).${1##*.}
|
2014-06-26 06:22:31 +00:00
|
|
|
echo "Old image found. Renaming it to $old_image"
|
2014-05-13 13:57:35 +00:00
|
|
|
mv "$1" "$old_image"
|
2016-09-30 19:59:46 +00:00
|
|
|
if [ -f "$1.md5" ]; then
|
|
|
|
mv "$1.md5" "$old_image.md5"
|
|
|
|
fi
|
|
|
|
if [ -f "$1.sha256" ]; then
|
|
|
|
mv "$1.sha256" "$old_image.sha256"
|
|
|
|
fi
|
2014-05-13 13:57:35 +00:00
|
|
|
fi
|
|
|
|
|
2014-10-01 19:34:21 +00:00
|
|
|
mv $OUT_IMAGE_PATH $1
|
2023-04-23 19:37:33 +00:00
|
|
|
if [[ -n "$DIB_CHECKSUM" && "$DIB_CHECKSUM" != "0" ]]; then
|
2019-11-21 01:35:04 +00:00
|
|
|
|
|
|
|
# NOTE(pabelanger): Read image into memory once and generate
|
|
|
|
# both checksum files.
|
|
|
|
# NOTE(ianw): we've seen issues with this waiting for
|
|
|
|
# our outfilter.py wrapper when containerised (probably due to
|
|
|
|
# no tty). Waiting for just these processes is a bit of hacky
|
|
|
|
# workaround ...
|
|
|
|
declare -a wait_for
|
2023-04-23 19:37:33 +00:00
|
|
|
[[ "$DIB_CHECKSUM" == "1" ]] && DIB_CHECKSUM="md5,sha256"
|
2023-08-30 11:04:13 +00:00
|
|
|
if [[ "$DIB_CHECKSUM" == *md5* ]]; then md5sum $1 > $1.md5 & wait_for+=($!); fi
|
|
|
|
if [[ "$DIB_CHECKSUM" == *sha256* ]]; then sha256sum $1 > $1.sha256 & wait_for+=($!); fi
|
2019-11-21 01:35:04 +00:00
|
|
|
wait "${wait_for[@]}"
|
2016-09-30 19:59:46 +00:00
|
|
|
fi
|
2014-03-25 01:16:06 +00:00
|
|
|
echo "Image file $1 created..."
|
|
|
|
}
|
|
|
|
|
2012-11-09 18:18:08 +00:00
|
|
|
function save_image () {
|
2014-06-26 06:22:31 +00:00
|
|
|
finish_image $1
|
2012-11-09 18:18:08 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 10:43:32 +00:00
|
|
|
function copy_hooks_not_overwrite () {
|
|
|
|
_DIR=$(basename $1)
|
|
|
|
test -d $TMP_HOOKS_PATH/$_DIR || mkdir $TMP_HOOKS_PATH/$_DIR
|
|
|
|
for _HOOK in $(ls $1); do
|
|
|
|
if [ ! -f $TMP_HOOKS_PATH/$_DIR/$_HOOK ]; then
|
2017-02-07 04:21:09 +00:00
|
|
|
echo "Copying hooks $1/$_HOOK"
|
2014-10-08 10:43:32 +00:00
|
|
|
cp -t $TMP_HOOKS_PATH/$_DIR -a $1/$_HOOK
|
|
|
|
else
|
|
|
|
echo "There is a duplicated hook in your elements: $_ELEMENT/$_DIR/$_HOOK"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2012-11-09 18:18:08 +00:00
|
|
|
function generate_hooks () {
|
2018-10-17 23:55:07 +00:00
|
|
|
local xtrace
|
|
|
|
xtrace=$(set +o | grep xtrace)
|
|
|
|
set +o xtrace
|
|
|
|
|
2016-06-28 05:57:55 +00:00
|
|
|
local dir
|
|
|
|
local file
|
|
|
|
|
|
|
|
eval declare -A image_elements=($(get_image_element_array))
|
|
|
|
|
|
|
|
mkdir -p $TMP_HOOKS_PATH
|
|
|
|
|
|
|
|
for i in "${!image_elements[@]}"; do
|
|
|
|
local element=$i
|
|
|
|
local element_dir=${image_elements[$i]}
|
|
|
|
|
2018-10-17 23:55:07 +00:00
|
|
|
echo "Copying hooks for ${element}"
|
|
|
|
|
2016-06-28 05:57:55 +00:00
|
|
|
for dir in $(find $element_dir \
|
|
|
|
-follow -mindepth 1 -maxdepth 1 \
|
2018-11-22 00:05:33 +00:00
|
|
|
-type d \
|
|
|
|
-not -name tests \
|
|
|
|
-not -name __pycache__); do
|
2016-06-28 05:57:55 +00:00
|
|
|
copy_hooks_not_overwrite $dir
|
|
|
|
done
|
|
|
|
for file in $(find $element_dir \
|
2018-11-22 00:05:33 +00:00
|
|
|
-follow -maxdepth 1 \
|
|
|
|
-type f \
|
|
|
|
-not -name '*.pyc'); do
|
2016-06-28 05:57:55 +00:00
|
|
|
cp -t $TMP_HOOKS_PATH -a $file
|
|
|
|
done
|
2013-02-25 09:17:32 +00:00
|
|
|
done
|
2018-10-17 23:55:07 +00:00
|
|
|
|
|
|
|
$xtrace
|
2012-11-09 18:18:08 +00:00
|
|
|
}
|
|
|
|
|
2012-12-14 07:17:00 +00:00
|
|
|
# Call the supplied break-in routine if the named point is listed in the break
|
|
|
|
# list.
|
|
|
|
# $1 the break point.
|
|
|
|
# $2.. what to call if a break is needed
|
|
|
|
function check_break () {
|
2014-04-03 22:49:18 +00:00
|
|
|
if echo "${break:-}" | egrep -e "(,|^)$1(,|$)" -q; then
|
2012-12-14 07:17:00 +00:00
|
|
|
echo "Starting debug shell. Exit to resume building." >&2
|
|
|
|
echo At stage $1 >&2
|
|
|
|
shift
|
|
|
|
"$@"
|
|
|
|
echo "Resuming" >&2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2012-11-30 20:47:57 +00:00
|
|
|
# Check that a real element has been chosen (prevents foot-guns)
|
|
|
|
function check_element () {
|
2012-11-09 18:18:08 +00:00
|
|
|
[ -d $TMP_HOOKS_PATH ] || generate_hooks
|
|
|
|
}
|
|
|
|
|
2012-11-11 22:47:26 +00:00
|
|
|
# Run a hook, looking for a regex in its stdout, and eval the matched lines.
|
|
|
|
# $1 is the hook to run
|
|
|
|
# $2 is the regex to look for
|
|
|
|
function eval_run_d () {
|
2016-05-16 23:25:12 +00:00
|
|
|
local run_output=$(mktemp)
|
|
|
|
trap "rm -f $run_output; check_break after-error ${break_cmd:-bash}" ERR
|
|
|
|
run_d $1 $run_output
|
|
|
|
if grep -q "$2" $run_output; then
|
|
|
|
local temp=$(grep "$2" $run_output)
|
|
|
|
eval "$temp"
|
|
|
|
fi
|
|
|
|
rm $run_output
|
|
|
|
trap - ERR
|
2012-11-11 22:47:26 +00:00
|
|
|
}
|
2012-11-17 02:30:15 +00:00
|
|
|
|
2016-12-13 01:47:46 +00:00
|
|
|
# Get any process that appears to be running in $TMP_BUILD_DIR
|
|
|
|
function _get_chroot_processes () {
|
|
|
|
# Deselect kernel threads, and use a python script to avoid
|
|
|
|
# forking lots and lots of readlink / grep processes on a busy
|
|
|
|
# system.
|
2017-06-22 01:19:39 +00:00
|
|
|
ps --ppid 2 -p 2 --deselect -o pid= | xargs ${DIB_PYTHON_EXEC:-python} -c '
|
2016-12-13 01:47:46 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
for pid in sys.argv[2:]:
|
|
|
|
try:
|
|
|
|
root = os.readlink("/proc/%s/root" % pid)
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
if sys.argv[1] in root:
|
|
|
|
print("%s" % pid)
|
|
|
|
' $TMP_BUILD_DIR
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-31 21:43:47 +00:00
|
|
|
function kill_chroot_processes () {
|
2016-11-18 03:49:21 +00:00
|
|
|
local xtrace
|
|
|
|
xtrace=$(set +o | grep xtrace)
|
|
|
|
set +o xtrace
|
|
|
|
|
2016-12-13 01:47:46 +00:00
|
|
|
local pidname
|
|
|
|
|
2016-10-31 21:43:47 +00:00
|
|
|
if [ -z "${1}" ]; then
|
|
|
|
echo "ERROR: no chroot directory specified"
|
|
|
|
exit 1
|
|
|
|
fi
|
2016-12-13 01:47:46 +00:00
|
|
|
|
|
|
|
for pid in $(_get_chroot_processes); do
|
2016-10-31 21:43:47 +00:00
|
|
|
# If there are open files from the chroot, just kill the process using
|
2016-12-13 01:47:46 +00:00
|
|
|
# these files. This is racy, but good enough
|
|
|
|
pidname=$(cat $piddir/comm 2>/dev/null || echo "unknown")
|
|
|
|
echo "Killing chroot process: '${pidname}($pid)'"
|
|
|
|
sudo kill $pid
|
2016-10-31 21:43:47 +00:00
|
|
|
done
|
2016-11-18 03:49:21 +00:00
|
|
|
|
|
|
|
$xtrace
|
2016-10-31 21:43:47 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 08:46:18 +00:00
|
|
|
function cleanup_build_dir () {
|
2017-08-11 14:28:14 +00:00
|
|
|
if ! timeout 10 sh -c " while ! sudo rm -rf $TMP_BUILD_DIR/built; do sleep 1; done"; then
|
2015-12-18 04:16:17 +00:00
|
|
|
echo "ERROR: unable to cleanly remove $TMP_BUILD_DIR/built"
|
|
|
|
exit 1
|
|
|
|
fi
|
2013-05-06 16:11:49 +00:00
|
|
|
sudo rm -rf $TMP_BUILD_DIR/mnt
|
2016-10-31 21:43:47 +00:00
|
|
|
kill_chroot_processes $TMP_BUILD_DIR
|
2015-05-27 14:11:13 +00:00
|
|
|
if tmpfs_check 0; then
|
2016-10-31 21:43:47 +00:00
|
|
|
# If kill_chroot_processes did not succeed then we have to wait for
|
|
|
|
# init to reap the orphaned chroot processes
|
|
|
|
if ! timeout 120 sh -c "while ! sudo umount -f $TMP_BUILD_DIR; do sleep 1; done"; then
|
|
|
|
echo "ERROR: failed to umount the $TMP_BUILD_DIR tmpfs mount point"
|
|
|
|
exit 1
|
|
|
|
fi
|
2015-03-24 18:30:55 +00:00
|
|
|
fi
|
2015-06-04 08:46:18 +00:00
|
|
|
rm -rf --one-file-system $TMP_BUILD_DIR
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanup_image_dir () {
|
2016-10-31 21:43:47 +00:00
|
|
|
kill_chroot_processes $TMP_IMAGE_DIR
|
2015-06-04 08:46:18 +00:00
|
|
|
if tmpfs_check 0; then
|
2016-10-31 21:43:47 +00:00
|
|
|
if ! timeout 120 sh -c "while ! sudo umount -f $TMP_IMAGE_DIR; do sleep 1; done"; then
|
|
|
|
echo "ERROR: failed to umount the $TMP_IMAGE_DIR tmpfs mount point"
|
|
|
|
exit 1
|
|
|
|
fi
|
2015-06-04 08:46:18 +00:00
|
|
|
fi
|
|
|
|
rm -rf --one-file-system $TMP_IMAGE_DIR
|
2013-05-06 16:11:49 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 06:59:49 +00:00
|
|
|
# Run a directory of hooks outside the target (that is, no chrooting).
|
2013-05-06 16:11:49 +00:00
|
|
|
function run_d() {
|
|
|
|
check_element
|
2016-05-16 23:25:12 +00:00
|
|
|
check_break before-$1 ${break_cmd:-bash}
|
2013-05-06 16:11:49 +00:00
|
|
|
if [ -d ${TMP_HOOKS_PATH}/$1.d ] ; then
|
2017-02-07 04:21:09 +00:00
|
|
|
echo "Running hooks from ${TMP_HOOKS_PATH}/$1.d"
|
2016-05-16 23:25:12 +00:00
|
|
|
if [ -n "$2" ]; then
|
2017-03-16 02:58:31 +00:00
|
|
|
${DIB_RUN_PARTS} ${TMP_HOOKS_PATH}/$1.d | tee $2
|
2016-05-16 23:25:12 +00:00
|
|
|
if [[ ${PIPESTATUS[0]} != 0 ]]; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
else
|
2017-03-16 02:58:31 +00:00
|
|
|
${DIB_RUN_PARTS} ${TMP_HOOKS_PATH}/$1.d
|
2016-05-16 23:25:12 +00:00
|
|
|
fi
|
2013-05-06 16:11:49 +00:00
|
|
|
fi
|
|
|
|
check_break after-$1 bash
|
|
|
|
}
|
|
|
|
|
2018-01-30 23:56:57 +00:00
|
|
|
function _arg_defaults_hack() {
|
|
|
|
# The block-device configuration looks in all elements for a
|
|
|
|
# "block-device-default.yaml" file. The "vm" element used to
|
|
|
|
# provide the default block-device, which was fine when there was
|
|
|
|
# only one option; but now we have mbr, gpt & efi versions.
|
|
|
|
#
|
|
|
|
# So now the vm element has a dependency on the block-device
|
|
|
|
# element, which several different elements can provide. However,
|
|
|
|
# for backwards compatability we need to ensure you can still
|
|
|
|
# build without specifying it. Thus if we see the vm element, but
|
|
|
|
# no block-device-* element, we will automatically add the old
|
|
|
|
# default MBR.
|
|
|
|
#
|
|
|
|
# Note that you can still override this by setting
|
|
|
|
# DIB_BLOCK_DEVICE_CONFIG; any value there will be taken over the
|
|
|
|
# element defaults. In this case you'd have "block-device-mbr" as
|
|
|
|
# an element, but it wouldn't actually be used for configuration.
|
|
|
|
#
|
|
|
|
# XXX: if this is becoming a common problem, we could have some
|
|
|
|
# sort of "element-defaults" that maps a "element-deps" entry to a
|
|
|
|
# default.
|
|
|
|
local vm_seen
|
|
|
|
local blockdev_seen
|
|
|
|
local elements
|
|
|
|
|
|
|
|
for arg do
|
2021-05-06 23:58:46 +00:00
|
|
|
if [[ $arg = vm ]]; then
|
2018-01-30 23:56:57 +00:00
|
|
|
vm_seen=1
|
2021-05-06 23:58:46 +00:00
|
|
|
elif [[ $arg = block-device-* ]]; then
|
2018-01-30 23:56:57 +00:00
|
|
|
blockdev_seen=1
|
|
|
|
fi
|
|
|
|
elements="$elements $arg"
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ -n "${vm_seen}" && -z "${blockdev_seen}" ]]; then
|
2020-04-14 10:11:35 +00:00
|
|
|
if [[ "arm64 aarch64" =~ $ARCH ]] ; then
|
|
|
|
elements="$elements block-device-efi"
|
|
|
|
else
|
|
|
|
elements="$elements block-device-mbr"
|
|
|
|
fi
|
2018-01-30 23:56:57 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
echo $elements
|
|
|
|
}
|
|
|
|
|
2013-06-20 03:48:37 +00:00
|
|
|
function arg_to_elements() {
|
|
|
|
for arg do IMAGE_ELEMENT="$IMAGE_ELEMENT $arg" ; done
|
|
|
|
|
2018-01-30 23:56:57 +00:00
|
|
|
IMAGE_ELEMENT="$(_arg_defaults_hack $IMAGE_ELEMENT)"
|
|
|
|
|
2013-06-20 03:48:37 +00:00
|
|
|
if [ "$SKIP_BASE" != "1" ]; then
|
|
|
|
IMAGE_ELEMENT="base $IMAGE_ELEMENT"
|
|
|
|
fi
|
2013-07-01 16:20:15 +00:00
|
|
|
if [ "$IS_RAMDISK" == "1" ]; then
|
2014-09-26 05:12:32 +00:00
|
|
|
IMAGE_ELEMENT="$RAMDISK_ELEMENT $IMAGE_ELEMENT"
|
2013-07-01 16:20:15 +00:00
|
|
|
fi
|
2013-06-20 03:48:37 +00:00
|
|
|
echo "Building elements: $IMAGE_ELEMENT"
|
2016-06-17 05:45:20 +00:00
|
|
|
export IMAGE_ELEMENT
|
2013-06-20 03:48:37 +00:00
|
|
|
|
2016-06-28 05:57:55 +00:00
|
|
|
# element-info will output bash code to create
|
|
|
|
# * IMAGE_ELEMENT
|
|
|
|
# legacy list of elements
|
|
|
|
#
|
|
|
|
# * IMAGE_ELEMENT_YAML
|
|
|
|
# YAML dictionary with key=element, value=path
|
|
|
|
#
|
|
|
|
# import os
|
|
|
|
# import yaml
|
|
|
|
# yaml.load(os.getenv('IMAGE_ELEMENT_YAML')
|
|
|
|
#
|
|
|
|
# * function get_image_element_array
|
|
|
|
# Function to create Bash associative-array. Since bash can not
|
|
|
|
# export array variables, we provide a function to populate the
|
|
|
|
# variables.
|
|
|
|
#
|
|
|
|
# # we need the eval, it expands the string for the array create
|
|
|
|
# eval declare -A image_elements=($(get_image_element_array))
|
|
|
|
# for i in ${!image_elements[@]}; do
|
|
|
|
# element=$i
|
|
|
|
# path=${image_elements[$i]
|
|
|
|
# done
|
2020-11-09 05:36:18 +00:00
|
|
|
elinfo_out="$(${DIB_PYTHON_EXEC} ${_LIB}/element-info.py --env ${IMAGE_ELEMENT})"
|
2016-11-02 00:23:58 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "ERROR: element-info failed to expand elements."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
eval "$elinfo_out"
|
2013-06-20 03:48:37 +00:00
|
|
|
echo "Expanded element dependencies to: $IMAGE_ELEMENT"
|
|
|
|
}
|
2013-06-20 04:47:22 +00:00
|
|
|
|
|
|
|
function create_base () {
|
|
|
|
mkdir $TMP_BUILD_DIR/mnt
|
2019-01-10 02:27:59 +00:00
|
|
|
# Make sure the / inside the chroot is owned by root
|
|
|
|
# If it is not owned by root, some Ubuntu bionic packages will fail
|
|
|
|
# path validation at install time.
|
|
|
|
sudo chown root.root $TMP_BUILD_DIR/mnt
|
2013-06-20 04:47:22 +00:00
|
|
|
export TMP_MOUNT_PATH=$TMP_BUILD_DIR/mnt
|
|
|
|
# Copy data in to the root.
|
|
|
|
TARGET_ROOT=$TMP_MOUNT_PATH run_d root
|
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
2013-10-08 12:53:16 +00:00
|
|
|
if [ -z "$(ls $TMP_MOUNT_PATH | grep -v '^lost+found\|tmp$')" ] ; then
|
2013-11-26 18:59:14 +00:00
|
|
|
# No root element copied in. Note the test above allows
|
|
|
|
# root.d elements to put things in /tmp
|
2014-04-30 15:59:49 +00:00
|
|
|
echo "Failed to deploy the root element."
|
2013-11-26 18:59:14 +00:00
|
|
|
exit 1
|
2013-06-20 04:47:22 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Configure Image
|
2018-04-03 05:20:42 +00:00
|
|
|
|
|
|
|
# Save resolv.conf as created by the initial install. Note the
|
|
|
|
# .ORIG file is an exported interface -- it may be modified and we
|
|
|
|
# will copy it back in during finalisation of the image.
|
2018-04-06 22:04:56 +00:00
|
|
|
# Note that we use -L and -f to test here as test (and bash [[)
|
|
|
|
# return false with -e if the link target does not exist.
|
|
|
|
if [ -L $TMP_MOUNT_PATH/etc/resolv.conf ] || [ -f $TMP_MOUNT_PATH/etc/resolv.conf ] ; then
|
2013-06-20 04:47:22 +00:00
|
|
|
sudo mv $TMP_MOUNT_PATH/etc/resolv.conf $TMP_MOUNT_PATH/etc/resolv.conf.ORIG
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Recreate resolv.conf
|
|
|
|
sudo touch $TMP_MOUNT_PATH/etc/resolv.conf
|
|
|
|
sudo chmod 777 $TMP_MOUNT_PATH/etc/resolv.conf
|
|
|
|
# use system configured resolv.conf if available to support internal proxy resolving
|
2014-07-01 09:27:41 +00:00
|
|
|
if [ -e /etc/resolv.conf ]; then
|
2013-06-20 04:47:22 +00:00
|
|
|
cat /etc/resolv.conf > $TMP_MOUNT_PATH/etc/resolv.conf
|
|
|
|
else
|
|
|
|
echo nameserver 8.8.8.8 > $TMP_MOUNT_PATH/etc/resolv.conf
|
|
|
|
fi
|
|
|
|
mount_proc_dev_sys
|
|
|
|
}
|
|
|
|
|
Fix /dev/pts mount options handling
The current implementation - as introduced in
Iee44703297a15b14c715f4bfb7bae67f613aceee - has some shortcomings / bugs,
like:
* the 'grep' check is too sloppy
* when /dev/pts is already mounted multiple times the current implementation
fails:
$ mount | grep devpts | sed 's/.*(\(.*\))/\1/'
rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000
rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000
rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000
* code duplication
* Using the undocumented and non-robust output
of 'mount'.
This patch fixed the above problems.
Change-Id: Ib0c7358772480c56d405659a6a32afd60c311686
Signed-off-by: Andreas Florath <andreas@florath.net>
2017-11-23 19:50:31 +00:00
|
|
|
# Get mount options for mounting /dev/pts
|
|
|
|
# Kernel commit https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=eedf265aa003b4781de24cfed40a655a664457e6
|
|
|
|
# introduced in v4.7 allows multiple instances of devpts. However,
|
|
|
|
# some distributions are running older kernels so we need to take
|
|
|
|
# care on what options we use to mount a new instance of devpts
|
|
|
|
# filesystem since it's not completely independent. The best thing
|
|
|
|
# to do is to simply use the existing mount options.
|
|
|
|
function mount_dev_pts_options() {
|
|
|
|
echo "-o $(findmnt --first-only /dev/pts --noheadings --output OPTIONS)"
|
|
|
|
}
|
|
|
|
|
2013-06-20 04:47:22 +00:00
|
|
|
function mount_proc_dev_sys () {
|
|
|
|
# supporting kernel file systems
|
|
|
|
sudo mount -t proc none $TMP_MOUNT_PATH/proc
|
|
|
|
sudo mount --bind /dev $TMP_MOUNT_PATH/dev
|
Fix /dev/pts mount options handling
The current implementation - as introduced in
Iee44703297a15b14c715f4bfb7bae67f613aceee - has some shortcomings / bugs,
like:
* the 'grep' check is too sloppy
* when /dev/pts is already mounted multiple times the current implementation
fails:
$ mount | grep devpts | sed 's/.*(\(.*\))/\1/'
rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000
rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000
rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000
* code duplication
* Using the undocumented and non-robust output
of 'mount'.
This patch fixed the above problems.
Change-Id: Ib0c7358772480c56d405659a6a32afd60c311686
Signed-off-by: Andreas Florath <andreas@florath.net>
2017-11-23 19:50:31 +00:00
|
|
|
sudo mount -t devpts $(mount_dev_pts_options) devpts $TMP_MOUNT_PATH/dev/pts
|
2021-07-01 23:26:20 +00:00
|
|
|
# /sys is mounted RO inside non-privledged containers, thus
|
|
|
|
# mounting this RO in the chroot here is an indication to
|
|
|
|
# systemd/udev and other things that you are inside a container.
|
|
|
|
# This is generically safe and can help avoid issues where things
|
|
|
|
# we don't control like pre/post scripts try to do things that
|
|
|
|
# don't work when building inside a dib container like udevadm
|
|
|
|
# --settle calls, etc.
|
|
|
|
sudo mount -o ro -t sysfs none $TMP_MOUNT_PATH/sys
|
2013-06-20 04:47:22 +00:00
|
|
|
}
|
|
|
|
|
2016-01-28 02:14:57 +00:00
|
|
|
# Recursively unmount directories under a given directory DIR
|
|
|
|
# usage:
|
|
|
|
# unmount_dir DIR
|
|
|
|
function unmount_dir {
|
|
|
|
local dir="$1"
|
|
|
|
local real_dir
|
|
|
|
local mnts
|
2017-05-12 16:41:37 +00:00
|
|
|
local split_mounts
|
|
|
|
local found_mountpoint
|
2016-01-28 02:14:57 +00:00
|
|
|
|
|
|
|
if [ ! -d $dir ]; then
|
|
|
|
echo "*** $dir is not a directory"
|
2017-03-16 16:31:23 +00:00
|
|
|
return 0
|
2013-12-24 08:24:59 +00:00
|
|
|
fi
|
2016-01-28 02:14:57 +00:00
|
|
|
|
|
|
|
# get rid of any symlink elements in the incoming path, because
|
|
|
|
# /proc/mounts is the real path
|
|
|
|
real_dir=$(readlink -e $dir)
|
|
|
|
|
2017-05-12 16:41:37 +00:00
|
|
|
# populate the exported mountpoints
|
|
|
|
IFS='|' read -ra split_mounts <<< "$DIB_MOUNTPOINTS"
|
|
|
|
|
|
|
|
|
2017-01-29 23:52:40 +00:00
|
|
|
# note the "/" on real_dir ... we are just looking for things
|
|
|
|
# mounted *underneath* this directory.
|
|
|
|
mnts=$(awk '{print $2}' < /proc/mounts | grep "^$real_dir/" | sort -r)
|
2016-01-28 02:14:57 +00:00
|
|
|
for m in $mnts; do
|
2017-05-12 16:41:37 +00:00
|
|
|
# check if suffix is in array
|
|
|
|
found_mountpoint=false
|
|
|
|
for mountpoint in "${split_mounts[@]}"; do
|
|
|
|
if [[ "$mountpoint" != "/" ]]; then
|
|
|
|
if [[ "$m" == *$mountpoint ]]; then
|
|
|
|
echo "Mountpoint $m managed by block device; skipping"
|
|
|
|
found_mountpoint=true
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ $found_mountpoint == false ]; then
|
|
|
|
# unmount the directory as it is not managed by block device
|
|
|
|
echo "Unmount $m"
|
|
|
|
sudo umount -fl $m || true
|
|
|
|
fi
|
2016-01-28 02:14:57 +00:00
|
|
|
done
|
2013-12-24 08:24:59 +00:00
|
|
|
}
|
2017-04-22 07:49:22 +00:00
|
|
|
|
|
|
|
# Create YAML config file for the block device layer
|
|
|
|
# The order here is: use the one the user provides - if there is
|
|
|
|
# none provided, fall back to the possible one element which
|
|
|
|
# defines a fallback configuration.
|
|
|
|
# Parameters:
|
|
|
|
# - name of the to be created config file
|
|
|
|
function block_device_create_config_file {
|
2017-06-05 23:43:44 +00:00
|
|
|
# nosiy; we manually trace
|
|
|
|
local xtrace
|
|
|
|
xtrace=$(set +o | grep xtrace)
|
|
|
|
set +o xtrace
|
|
|
|
|
|
|
|
local config_yaml="$1"
|
2017-04-22 07:49:22 +00:00
|
|
|
|
|
|
|
if [[ ${DIB_BLOCK_DEVICE_CONFIG:-} == file://* ]]; then
|
2017-06-05 23:43:44 +00:00
|
|
|
cp $(echo ${DIB_BLOCK_DEVICE_CONFIG} | cut -c 8-) ${config_yaml}
|
|
|
|
echo "Using file-based block-device config: ${DIB_BLOCK_DEVICE_CONFIG}"
|
2018-04-03 04:33:09 +00:00
|
|
|
$xtrace
|
2017-04-22 07:49:22 +00:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
if [ -n "${DIB_BLOCK_DEVICE_CONFIG:-}" ]; then
|
2017-06-05 23:43:44 +00:00
|
|
|
printf "%s" "${DIB_BLOCK_DEVICE_CONFIG}" >${config_yaml}
|
|
|
|
echo "User specified block-device config from DIB_BLOCK_DEVICE_CONFIG"
|
2018-04-03 04:33:09 +00:00
|
|
|
$xtrace
|
2017-04-22 07:49:22 +00:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2017-06-05 23:43:44 +00:00
|
|
|
# Search the elements for a matching block-device config.
|
|
|
|
# XXX: first match wins?
|
|
|
|
echo "Searching elements for block-device[-${ARCH}].yaml ..."
|
2017-04-22 07:49:22 +00:00
|
|
|
eval declare -A image_elements=($(get_image_element_array))
|
|
|
|
for i in ${!image_elements[@]}; do
|
2017-06-05 23:43:44 +00:00
|
|
|
local cfg
|
|
|
|
# look for arch specific version first, then default
|
2017-06-26 03:13:25 +00:00
|
|
|
if [[ "ppc64le ppc64el" =~ $ARCH ]] ; then
|
|
|
|
# NOTE(tonyb): ppc64el and ppc64le are the same archttechture, it's
|
|
|
|
# just different distro's have different names. So if we're either
|
|
|
|
# of them pick the block-device-ppc64el.yaml file
|
|
|
|
cfg=${image_elements[$i]}/block-device-ppc64el.yaml
|
|
|
|
else
|
|
|
|
cfg=${image_elements[$i]}/block-device-${ARCH}.yaml
|
|
|
|
fi
|
2017-06-05 23:43:44 +00:00
|
|
|
if [ -e ${cfg} ]; then
|
|
|
|
cp ${cfg} ${config_yaml}
|
|
|
|
echo "Using block-device config: ${cfg}"
|
2018-04-03 04:33:09 +00:00
|
|
|
$xtrace
|
2017-06-05 23:43:44 +00:00
|
|
|
return
|
2017-04-22 07:49:22 +00:00
|
|
|
else
|
2017-06-05 23:43:44 +00:00
|
|
|
cfg=${image_elements[$i]}/block-device-default.yaml
|
|
|
|
if [ -e ${cfg} ]; then
|
|
|
|
cp ${cfg} ${config_yaml}
|
|
|
|
echo "Using block-device config: ${cfg}"
|
2018-04-03 04:33:09 +00:00
|
|
|
$xtrace
|
2017-06-05 23:43:44 +00:00
|
|
|
return
|
2017-04-22 07:49:22 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2017-06-05 23:43:44 +00:00
|
|
|
echo "... done"
|
|
|
|
|
|
|
|
# how did this get here?
|
|
|
|
if [ -e ${config_yaml} ]; then
|
|
|
|
die "${config_yaml} exists?"
|
|
|
|
fi
|
2017-04-22 07:49:22 +00:00
|
|
|
|
2017-06-05 23:43:44 +00:00
|
|
|
echo "Using default block-device fallback config"
|
2017-04-22 07:49:22 +00:00
|
|
|
# If no config is there (until now) use the default config
|
2017-06-05 23:43:44 +00:00
|
|
|
|
|
|
|
cat >${config_yaml} <<EOF
|
2017-04-22 07:49:22 +00:00
|
|
|
- local_loop:
|
|
|
|
name: image0
|
|
|
|
mkfs:
|
|
|
|
name: mkfs_root
|
|
|
|
mount:
|
|
|
|
mount_point: /
|
|
|
|
fstab:
|
|
|
|
options: "defaults"
|
|
|
|
fsck-passno: 1
|
|
|
|
EOF
|
2017-06-05 23:43:44 +00:00
|
|
|
|
|
|
|
$xtrace
|
2017-04-22 07:49:22 +00:00
|
|
|
}
|