diskimage-builder/bin/disk-image-create
Robert Collins 0b1ff7d4b0 Factor out element processing.
This reduces code duplication for hygiene, no functional changes.

Change-Id: I10c96da4f07bcd1e5aabfcfbd115ecaef7bb65a1
2013-06-24 16:30:25 +01:00

130 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
#
# Copyright 2012 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
set -e
# Prevent perl from complaining a lot, but also remove any unexpected side-effects
# of $LANG varying between build hosts
export LANG=C
# Store our initial environment and command line args for later
export DIB_ARGS="$@"
export DIB_ENV=$(export)
SCRIPTNAME=$(basename $0)
SCRIPT_HOME=$(dirname $0)
export _LIB=$(dirname $0)/../lib
source $_LIB/die
function show_options () {
echo "Options:"
echo " -a i386|amd64|armhf -- set the architecture of the image"
echo " -o filename -- set the name of the output file"
echo " -x -- turn on tracing"
echo " -u -- uncompressed; do not compress the image - larger but faster"
echo " -c -- clear environment before starting work"
echo " -n skip the default inclusion of the 'base' element"
echo " -p package[,package,package] -- list of packages to install in the image"
echo " --no-tmpfs -- do not use tmpfs to speed image build"
echo
echo "ELEMENTS_PATH will allow you to specify multiple locations for the elements."
exit 0
}
INSTALL_PACKAGES=""
COMPRESS_IMAGE="true"
TEMP=`getopt -o a:ho:xucnp: -l no-tmpfs -n $SCRIPTNAME -- "$@"`
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;;
-u) shift; export COMPRESS_IMAGE="";;
-c) shift ; export CLEAR_ENV=1;;
-n) shift; export SKIP_BASE="1";;
-p) IFS="," read -a INSTALL_PACKAGES <<< "$2"; export INSTALL_PACKAGES ; shift 2 ;;
--no-tmpfs) shift; export DIB_NO_TMPFS=1;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
if [ "$CLEAR_ENV" = "1" -a "$HOME" != "" ]; then
echo "Re-execing to clear environment."
echo "(note this will prevent much of the local_config element from working)"
exec -c $0 "$@"
fi
source $_LIB/img-defaults
source $_LIB/common-functions
source $_LIB/img-functions
arg_to_elements "$@"
IMAGE_NAME=${IMAGE_NAME%%\.${IMAGE_TYPE}}
mk_build_dir
create_base
run_d extra-data
# Run pre-install scripts. These do things that prepare the chroot for package installs
run_d_in_target pre-install
do_extra_package_install
# Call install scripts to pull in the software users want.
run_d_in_target install
run_d_in_target post-install
prepare_first_boot
# Free up /mnt
unmount_image
mv $TMP_BUILD_DIR/mnt $TMP_BUILD_DIR/built
MKFS_OPTS=""
if [ -n "$DIB_IMAGE_SIZE" ]; then
truncate -s${DIB_IMAGE_SIZE}G $TMP_IMAGE_PATH
else
# in kb*0.75 - underreport to get a slightly bigger device
_NEEDED_SIZE=$(sudo du --block-size=750 -x -s ${TMP_BUILD_DIR}/built | awk ' { print $1 } ')
truncate -s${_NEEDED_SIZE}K $TMP_IMAGE_PATH
if [ "$FS_TYPE" = "ext4" ] ; then
# Very conservative to handle images being resized a lot
MKFS_OPTS="-i 4096"
fi
fi
# allow up to 1PB of 4KB blocks.
MKFS_OPTS="$MKFS_OPTS -E resize=274877906944"
LOOPDEV=$(sudo losetup --show -f $TMP_IMAGE_PATH)
export EXTRA_UNMOUNT="detach_loopback $LOOPDEV"
export IMAGE_BLOCK_DEVICE=$LOOPDEV
eval_run_d block-device "IMAGE_BLOCK_DEVICE="
sudo mkfs $MKFS_OPTS -t $FS_TYPE -L cloudimg-rootfs ${IMAGE_BLOCK_DEVICE}
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
finalise_base
unmount_image
compress_image
save_image $IMAGE_NAME.$IMAGE_TYPE