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.
|
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
|
|
|
|
2013-08-01 14:50:17 +00:00
|
|
|
set -eE
|
2012-11-09 11:04:13 +00:00
|
|
|
|
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="$@"
|
2013-11-29 06:17:02 +00:00
|
|
|
export DIB_ENV=$(export | grep ' DIB_.*=')
|
2013-04-04 19:38:19 +00:00
|
|
|
|
2012-11-09 11:04:13 +00:00
|
|
|
SCRIPTNAME=$(basename $0)
|
2014-07-10 15:40:11 +00:00
|
|
|
SCRIPT_HOME=$(dirname $(readlink -f $0))
|
2014-07-01 09:27:41 +00:00
|
|
|
if [ -d $SCRIPT_HOME/../share/diskimage-builder ]; then
|
2013-07-30 18:23:31 +00:00
|
|
|
export _PREFIX=$SCRIPT_HOME/../share/diskimage-builder
|
|
|
|
else
|
|
|
|
export _PREFIX=$SCRIPT_HOME/..
|
|
|
|
fi
|
|
|
|
export _LIB=$_PREFIX/lib
|
2012-11-09 11:04:13 +00:00
|
|
|
source $_LIB/die
|
|
|
|
|
2013-07-01 16:20:15 +00:00
|
|
|
IS_RAMDISK=0
|
|
|
|
if [ "$SCRIPTNAME" == "ramdisk-image-create" ]; then
|
|
|
|
IS_RAMDISK=1
|
|
|
|
fi
|
|
|
|
|
2012-11-09 11:04:13 +00:00
|
|
|
function show_options () {
|
2013-11-08 19:35:51 +00:00
|
|
|
echo "Usage: ${SCRIPTNAME} [OPTION]... [ELEMENT]..."
|
|
|
|
echo
|
2012-11-09 11:04:13 +00:00
|
|
|
echo "Options:"
|
2013-11-08 19:35:51 +00:00
|
|
|
echo " -a i386|amd64|armhf -- set the architecture of the image(default amd64)"
|
|
|
|
echo " -o imagename -- set the imagename of the output image file(default image)"
|
2014-10-01 19:34:21 +00:00
|
|
|
echo " -t qcow2,tar -- set the image types of the output image files (default qcow2)"
|
|
|
|
echo " File types should be comma separated"
|
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-11-25 18:00:01 +00:00
|
|
|
echo " --image-size size -- image size in GB for the created image"
|
2013-11-08 19:35:51 +00:00
|
|
|
echo " --image-cache directory -- location for cached images(default ~/.cache/image-create)"
|
2014-02-16 04:06:11 +00:00
|
|
|
echo " --max-online-resize size -- max number of filesystem blocks to support when resizing."
|
|
|
|
echo " Useful if you want a really large root partition when the image is deployed."
|
|
|
|
echo " Using a very large value may run into a known bug in resize2fs."
|
|
|
|
echo " Setting the value to 274877906944 will get you a 1PB root file system."
|
|
|
|
echo " Making this value unnecessarily large will consume extra disk space "
|
|
|
|
echo " on the root partition with extra file system inodes."
|
2013-08-26 11:57:34 +00:00
|
|
|
echo " --min-tmpfs size -- minimum size in GB needed in tmpfs to build the image"
|
2015-03-17 17:06:38 +00:00
|
|
|
echo " --mkfs-options -- option flags to be passed directly to mkfs."
|
|
|
|
echo " Options should be passed as a single string value."
|
2013-05-02 21:02:38 +00:00
|
|
|
echo " --no-tmpfs -- do not use tmpfs to speed image build"
|
2013-07-17 21:17:01 +00:00
|
|
|
echo " --offline -- do not update cached resources"
|
2014-06-13 12:20:17 +00:00
|
|
|
echo " --qemu-img-options -- option flags to be passed directly to qemu-img."
|
|
|
|
echo " Options need to be comma separated, and follow the key=value pattern."
|
2014-09-16 18:11:07 +00:00
|
|
|
echo " --root-label label -- label for the root filesystem. Defaults to 'cloudimg-rootfs'."
|
2014-09-26 05:12:32 +00:00
|
|
|
echo " --ramdisk-element -- specify the main element to be used for building ramdisks."
|
2014-12-01 16:21:13 +00:00
|
|
|
echo " Defaults to 'ramdisk'. Should be set to 'dracut-ramdisk' for platforms such"
|
|
|
|
echo " as RHEL and CentOS that do not package busybox."
|
2014-10-27 16:49:30 +00:00
|
|
|
echo " --install-type -- specify the default installation type. Defaults to 'source'. Set to 'package' to use package based installations by default."
|
2013-07-01 16:20:15 +00:00
|
|
|
if [ "$IS_RAMDISK" == "0" ]; then
|
|
|
|
echo " -n skip the default inclusion of the 'base' element"
|
|
|
|
echo " -p package[,package,package] -- list of packages to install in the image"
|
|
|
|
fi
|
2013-11-08 19:35:51 +00:00
|
|
|
echo " -h|--help -- display this help and exit"
|
2013-02-25 09:17:32 +00:00
|
|
|
echo
|
|
|
|
echo "ELEMENTS_PATH will allow you to specify multiple locations for the elements."
|
2013-11-08 19:35:51 +00:00
|
|
|
echo
|
2013-11-26 18:59:14 +00:00
|
|
|
echo "NOTE: At least one distribution root element must be specified."
|
|
|
|
echo
|
2013-11-08 19:35:51 +00:00
|
|
|
echo "Examples:"
|
2013-12-05 10:10:28 +00:00
|
|
|
if [ "$IS_RAMDISK" == "0" ]; then
|
|
|
|
echo " ${SCRIPTNAME} -a amd64 -o ubuntu-amd64 vm ubuntu"
|
|
|
|
echo " export ELEMENTS_PATH=~/source/tripleo-image-elements/elements"
|
|
|
|
echo " ${SCRIPTNAME} -a amd64 -o fedora-amd64-heat-cfntools vm fedora heat-cfntools"
|
|
|
|
else
|
|
|
|
echo " ${SCRIPTNAME} -a amd64 -o fedora-deploy deploy fedora"
|
|
|
|
echo " ${SCRIPTNAME} -a amd64 -o ubuntu-ramdisk ramdisk ubuntu"
|
|
|
|
fi
|
2012-11-09 11:04:13 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 20:45:02 +00:00
|
|
|
INSTALL_PACKAGES=""
|
2014-10-01 19:34:21 +00:00
|
|
|
IMAGE_TYPES=("qcow2")
|
2012-12-14 00:25:18 +00:00
|
|
|
COMPRESS_IMAGE="true"
|
2014-10-22 11:49:37 +00:00
|
|
|
export DIB_ROOT_LABEL=""
|
2014-10-27 16:49:30 +00:00
|
|
|
DIB_DEFAULT_INSTALLTYPE=${DIB_DEFAULT_INSTALLTYPE:-"source"}
|
2015-03-17 17:06:38 +00:00
|
|
|
MKFS_OPTS=""
|
|
|
|
TEMP=`getopt -o a:ho:t:xucnp: -l no-tmpfs,offline,help,min-tmpfs:,image-size:,image-cache:,max-online-resize:,mkfs-options:,qemu-img-options:,ramdisk-element:,root-label:,install-type: -n $SCRIPTNAME -- "$@"`
|
2013-11-08 19:35:51 +00:00
|
|
|
if [ $? -ne 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
|
2012-11-09 11:04:13 +00:00
|
|
|
|
|
|
|
# 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 ;;
|
2014-10-01 19:34:21 +00:00
|
|
|
-t) IFS="," read -a IMAGE_TYPES <<< "$2"; export IMAGE_TYPES ; shift 2 ;;
|
2013-11-08 19:35:51 +00:00
|
|
|
-h|--help) show_options; exit 0;;
|
2014-09-04 04:56:29 +00:00
|
|
|
-x) shift; export DIB_DEBUG_TRACE=$(( $DIB_DEBUG_TRACE + 1 )); 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 ;;
|
2013-11-25 18:00:01 +00:00
|
|
|
--image-size) export DIB_IMAGE_SIZE=$2; shift 2;;
|
2013-09-19 13:50:48 +00:00
|
|
|
--image-cache) export DIB_IMAGE_CACHE=$2; shift 2;;
|
2014-02-16 04:06:11 +00:00
|
|
|
--max-online-resize) export MAX_ONLINE_RESIZE=$2; shift 2;;
|
2015-03-17 17:06:38 +00:00
|
|
|
--mkfs-options) MKFS_OPTS=$2; shift 2;;
|
2013-08-26 11:57:34 +00:00
|
|
|
--min-tmpfs) export DIB_MIN_TMPFS=$2; shift 2;;
|
2013-05-02 21:02:38 +00:00
|
|
|
--no-tmpfs) shift; export DIB_NO_TMPFS=1;;
|
2013-07-17 21:17:01 +00:00
|
|
|
--offline) shift; export DIB_OFFLINE=1;;
|
2014-06-13 12:20:17 +00:00
|
|
|
--qemu-img-options) QEMU_IMG_OPTIONS=$2; shift 2;;
|
2014-09-16 18:11:07 +00:00
|
|
|
--root-label) export DIB_ROOT_LABEL=$2; shift 2;;
|
2014-09-26 05:12:32 +00:00
|
|
|
--ramdisk-element) RAMDISK_ELEMENT=$2; shift 2;;
|
2014-10-27 16:49:30 +00:00
|
|
|
--install-type) DIB_DEFAULT_INSTALLTYPE=$2; 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-09-19 13:50:48 +00:00
|
|
|
export DIB_IMAGE_CACHE=${DIB_IMAGE_CACHE:-~/.cache/image-create}
|
2014-07-29 14:43:12 +00:00
|
|
|
mkdir -p $DIB_IMAGE_CACHE
|
2013-09-19 13:50:48 +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-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-07-01 16:20:15 +00:00
|
|
|
if [ "$IS_RAMDISK" == "1" ]; then
|
|
|
|
source $_LIB/ramdisk-defaults
|
|
|
|
source $_LIB/ramdisk-functions
|
|
|
|
fi
|
|
|
|
|
2014-11-21 18:30:21 +00:00
|
|
|
# If no elements are specified theres no way we can succeed
|
2014-11-25 21:25:31 +00:00
|
|
|
if [ -z "$*" ]; then
|
2014-11-21 18:30:21 +00:00
|
|
|
echo "ERROR: At least one distribution root element must be specified"
|
|
|
|
exit 1
|
|
|
|
fi
|
2013-06-20 03:48:37 +00:00
|
|
|
arg_to_elements "$@"
|
2013-01-22 01:35:31 +00:00
|
|
|
|
2014-10-01 19:34:21 +00:00
|
|
|
if [ "${#IMAGE_TYPES[@]}" = "1" ]; then
|
|
|
|
export IMAGE_NAME=${IMAGE_NAME%%\.${IMAGE_TYPES[0]}}
|
|
|
|
fi
|
|
|
|
|
2014-10-09 09:54:19 +00:00
|
|
|
# NOTE: Tuning the rootfs uuid works only for ext filesystems.
|
|
|
|
# Rely on the below environment variable only for ext filesystems.
|
|
|
|
export DIB_IMAGE_ROOT_FS_UUID=$(uuidgen -r)
|
2014-12-15 23:40:48 +00:00
|
|
|
if echo "$FS_TYPE" | grep -q "^ext" && [ -z "${DIB_IMAGE_ROOT_FS_UUID}" ]; then
|
|
|
|
echo "ext filesystem detected but no DIB_IMAGE_ROOT_FS_UUID found."
|
|
|
|
echo "Is the uuidgen utility installed on your system?"
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-10-09 09:54:19 +00:00
|
|
|
|
2014-09-16 18:11:07 +00:00
|
|
|
# FS_TYPE isn't available until after we source img-defaults
|
|
|
|
if [ -z "$DIB_ROOT_LABEL" ]; then
|
|
|
|
# NOTE(bnemec): XFS has a limit of 12 characters for filesystem labels
|
|
|
|
# Not changing the default for other filesystems to maintain backwards compatibility
|
|
|
|
if [ "$FS_TYPE" = "xfs" ]; then
|
|
|
|
DIB_ROOT_LABEL="img-rootfs"
|
|
|
|
else
|
|
|
|
DIB_ROOT_LABEL="cloudimg-rootfs"
|
|
|
|
fi
|
|
|
|
fi
|
2012-11-09 11:04:13 +00:00
|
|
|
|
|
|
|
mk_build_dir
|
|
|
|
create_base
|
2014-10-27 16:49:30 +00:00
|
|
|
# This variable needs to be propagated into the chroot
|
|
|
|
echo "export DIB_DEFAULT_INSTALLTYPE=\"${DIB_DEFAULT_INSTALLTYPE}\"" > $TMP_HOOKS_PATH/environment.d/11-dib-install-type.bash
|
2012-11-09 11:04:13 +00:00
|
|
|
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
|
2013-04-15 10:31:04 +00:00
|
|
|
run_d_in_target post-install
|
2014-08-04 09:40:47 +00:00
|
|
|
# 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)
|
|
|
|
if [ -e "$TMP_BUILD_DIR/mnt/lost+found" ]; then
|
|
|
|
sudo rm -rf "$TMP_BUILD_DIR/mnt/lost+found"
|
|
|
|
fi
|
2013-04-02 00:25:25 +00:00
|
|
|
# Free up /mnt
|
|
|
|
unmount_image
|
|
|
|
mv $TMP_BUILD_DIR/mnt $TMP_BUILD_DIR/built
|
2013-05-06 19:17:42 +00:00
|
|
|
|
|
|
|
if [ -n "$DIB_IMAGE_SIZE" ]; then
|
|
|
|
truncate -s${DIB_IMAGE_SIZE}G $TMP_IMAGE_PATH
|
|
|
|
else
|
2014-01-11 21:44:53 +00:00
|
|
|
# in kb*0.60 - underreport to get a slightly bigger device
|
2013-09-08 00:03:19 +00:00
|
|
|
# Rounding down size so that is is a multiple of 64, works around a bug in
|
|
|
|
# qemu-img that may occur when compressing raw images that aren't a multiple
|
|
|
|
# of 64k. https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1180021
|
2014-01-11 21:44:53 +00:00
|
|
|
_NEEDED_SIZE=$(sudo du --block-size=600 -x -s ${TMP_BUILD_DIR}/built | \
|
2013-09-08 00:03:19 +00:00
|
|
|
awk ' { print $1 - ( $1 % 64) } ')
|
2013-05-06 19:17:42 +00:00
|
|
|
truncate -s${_NEEDED_SIZE}K $TMP_IMAGE_PATH
|
|
|
|
if [ "$FS_TYPE" = "ext4" ] ; then
|
|
|
|
# Very conservative to handle images being resized a lot
|
2013-09-30 07:41:37 +00:00
|
|
|
# Without -J option specified, default journal size will be set to 32M
|
|
|
|
# and online resize will be failed with error of needs too many credits.
|
2015-03-17 17:06:38 +00:00
|
|
|
MKFS_OPTS="-i 4096 -J size=64 $MKFS_OPTS"
|
2013-05-06 19:17:42 +00:00
|
|
|
fi
|
|
|
|
fi
|
2014-02-16 04:06:11 +00:00
|
|
|
|
|
|
|
if [ -n "$MAX_ONLINE_RESIZE" ]; then
|
2015-03-17 17:06:38 +00:00
|
|
|
MKFS_OPTS="-E resize=$MAX_ONLINE_RESIZE $MKFS_OPTS"
|
2014-02-16 04:06:11 +00:00
|
|
|
fi
|
2013-05-06 19:17:42 +00:00
|
|
|
|
2013-04-02 00:25:25 +00:00
|
|
|
LOOPDEV=$(sudo losetup --show -f $TMP_IMAGE_PATH)
|
2013-05-09 16:34:53 +00:00
|
|
|
export EXTRA_UNMOUNT="detach_loopback $LOOPDEV"
|
2013-04-02 00:25:25 +00:00
|
|
|
export IMAGE_BLOCK_DEVICE=$LOOPDEV
|
|
|
|
eval_run_d block-device "IMAGE_BLOCK_DEVICE="
|
2014-09-16 18:11:07 +00:00
|
|
|
sudo mkfs $MKFS_OPTS -t $FS_TYPE -L ${DIB_ROOT_LABEL} ${IMAGE_BLOCK_DEVICE}
|
2014-10-09 09:54:19 +00:00
|
|
|
# Tuning the rootfs uuid works only for ext filesystems.
|
|
|
|
if echo "$FS_TYPE" | grep -q "^ext"; then
|
|
|
|
sudo tune2fs ${IMAGE_BLOCK_DEVICE} -U ${DIB_IMAGE_ROOT_FS_UUID}
|
|
|
|
fi
|
2013-04-02 00:25:25 +00:00
|
|
|
mkdir $TMP_BUILD_DIR/mnt
|
|
|
|
sudo mount ${IMAGE_BLOCK_DEVICE} $TMP_BUILD_DIR/mnt
|
|
|
|
sudo mv -t $TMP_BUILD_DIR/mnt ${TMP_BUILD_DIR}/built/*
|
|
|
|
mount_proc_dev_sys
|
|
|
|
run_d_in_target finalise
|
2012-11-09 11:04:13 +00:00
|
|
|
finalise_base
|
2013-07-01 16:20:15 +00:00
|
|
|
|
2014-10-01 19:34:21 +00:00
|
|
|
for X in ${!IMAGE_TYPES[@]} ; do
|
|
|
|
if [ "${IMAGE_TYPES[$X]}" == "tar" ]; then
|
|
|
|
sudo tar -C ${TMP_BUILD_DIR}/mnt -cf $IMAGE_NAME.tar --exclude ./sys \
|
|
|
|
--exclude ./proc --xattrs --xattrs-include=\* .
|
|
|
|
sudo chown $USER: $IMAGE_NAME.tar
|
|
|
|
unset IMAGE_TYPES[$X]
|
|
|
|
fi
|
|
|
|
done
|
2014-04-28 22:33:59 +00:00
|
|
|
|
2012-11-09 11:04:13 +00:00
|
|
|
unmount_image
|
2013-07-01 16:20:15 +00:00
|
|
|
|
2014-10-01 19:34:21 +00:00
|
|
|
if [ "$IS_RAMDISK" == "0" ]; then
|
|
|
|
for IMAGE_TYPE in ${IMAGE_TYPES[@]} ; do
|
|
|
|
compress_and_save_image $IMAGE_NAME.$IMAGE_TYPE
|
|
|
|
done
|
2013-07-01 16:20:15 +00:00
|
|
|
fi
|
2014-06-27 11:54:29 +00:00
|
|
|
|
2015-01-27 18:45:55 +00:00
|
|
|
# Always cleanup after ourselves
|
|
|
|
rm $TMP_IMAGE_PATH
|
|
|
|
cleanup_dirs
|
|
|
|
|
2014-06-27 11:54:29 +00:00
|
|
|
case "$IMAGE_ELEMENT" in
|
|
|
|
*ironic-agent*)
|
|
|
|
rm $IMAGE_NAME.$IMAGE_TYPE
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2014-10-01 19:34:21 +00:00
|
|
|
# All done!
|
|
|
|
trap EXIT
|