diskimage-builder/bin/disk-image-create

104 lines
3.0 KiB
Plaintext
Raw Normal View History

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.
#
# 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.
2012-11-09 11:04:13 +00:00
set -e
SCRIPTNAME=$(basename $0)
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"
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 " -p package[,package,package] -- list of packages to install in the image"
2012-11-09 11:04:13 +00:00
exit 0
}
INSTALL_PACKAGES=""
COMPRESS_IMAGE="true"
TEMP=`getopt -o a:ho:xup: -n $SCRIPTNAME -- "$@"`
2012-11-09 11:04:13 +00:00
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
echo "XXX $TEMP"
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 ;;
-h) show_options;;
-x) shift; set -x;;
-u) shift; export COMPRESS_IMAGE="";;
-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
for arg do IMAGE_ELEMENT="$IMAGE_ELEMENT $arg" ; done
2012-11-09 11:04:13 +00:00
source $_LIB/img-defaults
source $_LIB/common-functions
2012-11-09 11:04:13 +00:00
source $_LIB/img-functions
IMAGE_ELEMENT=$($SCRIPT_HOME/element-info --expand-dependencies $IMAGE_ELEMENT)
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."
mkdir -p $IMG_PATH
# TODO: make an element.
2012-11-09 11:04:13 +00:00
ensure_nbd
mk_build_dir
ensure_base_available
eval_run_d block-device-size "IMAGE_SIZE="
2012-11-09 11:04:13 +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
if [[ $(qemu-nbd --help | grep cache) == *writeback* ]] ; then
CACHE="--cache=writeback"
else
echo "Warning: qemu-nbd without --cache=writeback is /slow/."
CACHE=""
fi
sudo qemu-nbd -c $NBD_DEV $CACHE $TMP_IMAGE_PATH
export EXTRA_UNMOUNT="sudo qemu-nbd -d $NBD_DEV"
export IMAGE_BLOCK_DEVICE=$NBD_DEV
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
do_pre_install
do_extra_package_install
2012-11-09 11:04:13 +00:00
do_install
prepare_first_boot
finalise_base
unmount_image
compress_image
save_image $IMAGE_NAME.$IMAGE_TYPE