2012-11-09 11:04:13 +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.
|
2012-11-09 11:04:13 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2013-01-28 18:08:50 +00:00
|
|
|
# Prevent perl from complaining a lot, but also remove any unexpected side-effects
|
|
|
|
# of $LANG varying between build hosts
|
|
|
|
export LANG=C
|
|
|
|
|
2013-04-04 19:38:19 +00:00
|
|
|
# Store our initial environment and command line args for later
|
|
|
|
export DIB_ARGS="$@"
|
|
|
|
export DIB_ENV=$(export)
|
|
|
|
|
2012-11-09 11:04:13 +00:00
|
|
|
SCRIPTNAME=$(basename $0)
|
2013-01-22 01:35:31 +00:00
|
|
|
SCRIPT_HOME=$(dirname $0)
|
2012-11-09 11:04:13 +00:00
|
|
|
export _LIB=$(dirname $0)/../lib
|
|
|
|
source $_LIB/die
|
|
|
|
|
|
|
|
function show_options () {
|
|
|
|
echo "Options:"
|
|
|
|
echo " -a i386|amd64 -- set the architecture of the image"
|
2013-01-25 22:23:56 +00:00
|
|
|
echo " -o filename -- set the name of the output file"
|
2012-11-09 11:04:13 +00:00
|
|
|
echo " -x -- turn on tracing"
|
2012-12-14 00:25:18 +00:00
|
|
|
echo " -u -- uncompressed; do not compress the image - larger but faster"
|
2013-01-30 18:24:24 +00:00
|
|
|
echo " -c -- clear environment before starting work"
|
2013-01-28 18:24:05 +00:00
|
|
|
echo " -n skip the default inclusion of the 'base' element"
|
2012-12-18 20:45:02 +00:00
|
|
|
echo " -p package[,package,package] -- list of packages to install in the image"
|
2013-02-25 09:17:32 +00:00
|
|
|
echo
|
|
|
|
echo "ELEMENTS_PATH will allow you to specify multiple locations for the elements."
|
2012-11-09 11:04:13 +00:00
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2012-12-18 20:45:02 +00:00
|
|
|
INSTALL_PACKAGES=""
|
2012-12-14 00:25:18 +00:00
|
|
|
COMPRESS_IMAGE="true"
|
2013-01-28 18:24:05 +00:00
|
|
|
TEMP=`getopt -o a:ho:xucnp: -n $SCRIPTNAME -- "$@"`
|
2012-11-09 11:04:13 +00:00
|
|
|
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
|
|
|
|
|
|
|
|
# Note the quotes around `$TEMP': they are essential!
|
|
|
|
eval set -- "$TEMP"
|
|
|
|
|
|
|
|
while true ; do
|
|
|
|
case "$1" in
|
|
|
|
-a) export ARCH=$2; shift 2 ;;
|
|
|
|
-o) export IMAGE_NAME=$2; shift 2 ;;
|
|
|
|
-h) show_options;;
|
|
|
|
-x) shift; set -x;;
|
2012-12-14 00:25:18 +00:00
|
|
|
-u) shift; export COMPRESS_IMAGE="";;
|
2013-01-30 18:24:24 +00:00
|
|
|
-c) shift ; export CLEAR_ENV=1;;
|
2013-01-28 18:24:05 +00:00
|
|
|
-n) shift; export SKIP_BASE="1";;
|
2012-12-18 20:45:02 +00:00
|
|
|
-p) IFS="," read -a INSTALL_PACKAGES <<< "$2"; export INSTALL_PACKAGES ; shift 2 ;;
|
2012-11-09 11:04:13 +00:00
|
|
|
--) shift ; break ;;
|
|
|
|
*) echo "Internal error!" ; exit 1 ;;
|
|
|
|
esac
|
|
|
|
done
|
2013-01-30 18:24:24 +00:00
|
|
|
|
2013-02-13 22:16:00 +00:00
|
|
|
if [ "$CLEAR_ENV" = "1" -a "$HOME" != "" ]; then
|
2013-01-30 18:24:24 +00:00
|
|
|
echo "Re-execing to clear environment."
|
|
|
|
echo "(note this will prevent much of the local_config element from working)"
|
|
|
|
exec -c $0 "$@"
|
|
|
|
fi
|
|
|
|
|
2012-11-30 20:47:57 +00:00
|
|
|
for arg do IMAGE_ELEMENT="$IMAGE_ELEMENT $arg" ; done
|
2012-11-09 11:04:13 +00:00
|
|
|
|
2013-01-28 18:24:05 +00:00
|
|
|
if [ "$SKIP_BASE" != "1" ]; then
|
|
|
|
IMAGE_ELEMENT="base $IMAGE_ELEMENT"
|
|
|
|
fi
|
|
|
|
|
2012-11-09 11:04:13 +00:00
|
|
|
source $_LIB/img-defaults
|
2012-11-09 18:18:08 +00:00
|
|
|
source $_LIB/common-functions
|
2012-11-09 11:04:13 +00:00
|
|
|
source $_LIB/img-functions
|
|
|
|
|
2013-03-14 17:56:18 +00:00
|
|
|
IMAGE_NAME=${IMAGE_NAME%%\.${IMAGE_TYPE}}
|
|
|
|
|
2013-01-22 01:35:31 +00:00
|
|
|
IMAGE_ELEMENT=$($SCRIPT_HOME/element-info --expand-dependencies $IMAGE_ELEMENT)
|
|
|
|
|
2012-11-30 20:47:57 +00:00
|
|
|
echo "Building elements: $IMAGE_ELEMENT"
|
2012-11-09 11:04:13 +00:00
|
|
|
echo "If prompted for sudo, install sudoers.d/img-build-sudoers into /etc/sudoers.d and restart the build."
|
|
|
|
|
2013-02-11 04:01:36 +00:00
|
|
|
# TODO: make into an element.
|
2012-11-09 11:04:13 +00:00
|
|
|
ensure_nbd
|
|
|
|
|
|
|
|
mk_build_dir
|
2012-11-11 22:47:26 +00:00
|
|
|
eval_run_d block-device-size "IMAGE_SIZE="
|
2012-11-09 11:04:13 +00:00
|
|
|
|
2012-12-14 00:25:18 +00:00
|
|
|
qemu-img create -f qcow2 -o preallocation=metadata $TMP_IMAGE_PATH ${IMAGE_SIZE}G
|
2012-11-09 11:04:13 +00:00
|
|
|
|
|
|
|
# Should have a grab-next-dev helper ?
|
|
|
|
NBD_DEV=/dev/nbd0
|
2013-01-25 22:23:56 +00:00
|
|
|
if [[ $(qemu-nbd --help | grep cache) == *writeback* ]] ; then
|
2012-11-09 11:04:13 +00:00
|
|
|
CACHE="--cache=writeback"
|
2013-01-25 22:23:56 +00:00
|
|
|
else
|
2012-11-09 11:04:13 +00:00
|
|
|
echo "Warning: qemu-nbd without --cache=writeback is /slow/."
|
|
|
|
CACHE=""
|
|
|
|
fi
|
2013-01-25 22:23:56 +00:00
|
|
|
sudo qemu-nbd -c $NBD_DEV $CACHE $TMP_IMAGE_PATH
|
2012-11-09 11:04:13 +00:00
|
|
|
export EXTRA_UNMOUNT="sudo qemu-nbd -d $NBD_DEV"
|
|
|
|
export IMAGE_BLOCK_DEVICE=$NBD_DEV
|
2012-11-11 22:47:26 +00:00
|
|
|
eval_run_d block-device "IMAGE_BLOCK_DEVICE="
|
2012-11-09 11:04:13 +00:00
|
|
|
|
|
|
|
sudo mkfs -F -t $FS_TYPE -L cloudimg-rootfs ${IMAGE_BLOCK_DEVICE}
|
|
|
|
|
|
|
|
mount_tmp_image ${IMAGE_BLOCK_DEVICE}
|
|
|
|
|
|
|
|
create_base
|
|
|
|
run_d extra-data
|
2013-02-13 22:16:00 +00:00
|
|
|
# Run pre-install scripts. These do things that prepare the chroot for package installs
|
|
|
|
run_d_in_target pre-install
|
2012-12-18 20:45:02 +00:00
|
|
|
do_extra_package_install
|
2013-02-13 22:16:00 +00:00
|
|
|
# Call install scripts to pull in the software users want.
|
|
|
|
run_d_in_target install
|
2012-11-09 11:04:13 +00:00
|
|
|
prepare_first_boot
|
|
|
|
finalise_base
|
|
|
|
unmount_image
|
|
|
|
compress_image
|
|
|
|
save_image $IMAGE_NAME.$IMAGE_TYPE
|