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
|
|
|
function unmount_image () {
|
|
|
|
# unmount from the chroot
|
|
|
|
# Don't use TMP_MOUNT_PATH here, it might not have been set.
|
|
|
|
sudo umount -f $TMP_BUILD_DIR/mnt/sys || true
|
|
|
|
sudo umount -f $TMP_BUILD_DIR/mnt/proc || true
|
|
|
|
sudo umount -f $TMP_BUILD_DIR/mnt/dev || true
|
|
|
|
sudo umount -f $TMP_BUILD_DIR/mnt/tmp/in_target.d || true
|
|
|
|
# give it a second (ok really 5) to umount XXX - why? should instead track
|
|
|
|
# the mount data / lsof etc.
|
|
|
|
sleep 5
|
|
|
|
# oh ya don't want to forget to unmount the image
|
|
|
|
sudo umount -f $TMP_BUILD_DIR/mnt || true
|
|
|
|
# having disk corruption issues; one possibility is qemu-nbd not flush dirty
|
|
|
|
# pages on disconnect?
|
|
|
|
sync
|
|
|
|
if [ -n "$EXTRA_UNMOUNT" ]; then
|
|
|
|
$EXTRA_UNMOUNT
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanup () {
|
|
|
|
unmount_image
|
|
|
|
rm -rf $TMP_BUILD_DIR
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensure_nbd () {
|
2012-12-13 17:20:55 +00:00
|
|
|
NBD=`which qemu-nbd` || true
|
2012-11-09 11:04:13 +00:00
|
|
|
if [ -z "$NBD" ]; then
|
|
|
|
echo "Need qemu-nbd to build qcow2 files."
|
|
|
|
sudo apt-get install qemu-utils
|
|
|
|
fi
|
|
|
|
# prep nbd for mounting
|
|
|
|
(lsmod | grep '^nbd ') || sudo modprobe nbd max_part=16
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensure_sudo () {
|
|
|
|
sudo echo "Ensuring sudo is available"
|
|
|
|
}
|
|
|
|
|
|
|
|
function mount_tmp_image () {
|
|
|
|
mkdir $TMP_BUILD_DIR/mnt
|
|
|
|
sudo mount $@ $TMP_BUILD_DIR/mnt
|
|
|
|
[ $? -eq 0 ] || die "Failed to mount image"
|
|
|
|
export TMP_MOUNT_PATH=$TMP_BUILD_DIR/mnt
|
|
|
|
}
|
|
|
|
|
|
|
|
function create_base () {
|
2013-02-11 04:01:36 +00:00
|
|
|
# Copy data in to the root.
|
|
|
|
TARGET_ROOT=$TMP_MOUNT_PATH run_d root
|
2013-02-13 00:58:35 +00:00
|
|
|
if [ -z "$(ls $TMP_MOUNT_PATH | grep -v lost+found)" ] ; then
|
2013-02-11 04:01:36 +00:00
|
|
|
# Nothing copied in, use Ubuntu.
|
|
|
|
echo "Adding ubuntu element as / had no contents"
|
|
|
|
IMAGE_ELEMENT=$($SCRIPT_HOME/element-info --expand-dependencies $IMAGE_ELEMENT ubuntu)
|
|
|
|
generate_hooks
|
|
|
|
echo "Now building: $IMAGE_ELEMENT"
|
|
|
|
TARGET_ROOT=$TMP_MOUNT_PATH run_d root
|
|
|
|
fi
|
2012-11-09 11:04:13 +00:00
|
|
|
|
|
|
|
# Configure Image
|
|
|
|
# Setup resolv.conf so we can chroot to install some packages
|
|
|
|
# XXXX: Should store the old state rather than unlink; then restore later.
|
|
|
|
if [ -L $TMP_MOUNT_PATH/etc/resolv.conf ] ; then
|
|
|
|
sudo unlink $TMP_MOUNT_PATH/etc/resolv.conf
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -f $TMP_MOUNT_PATH/etc/resolv.conf ] ; then
|
|
|
|
sudo rm -f $TMP_MOUNT_PATH/etc/resolv.conf
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Recreate resolv.conf
|
|
|
|
sudo touch $TMP_MOUNT_PATH/etc/resolv.conf
|
|
|
|
sudo chmod 777 $TMP_MOUNT_PATH/etc/resolv.conf
|
2012-11-30 15:40:24 +00:00
|
|
|
# use system configured resolv.conf if available to support internal proxy resolving
|
|
|
|
if [ -e /etc/resolv.conf ]
|
|
|
|
then
|
|
|
|
cat /etc/resolv.conf > $TMP_MOUNT_PATH/etc/resolv.conf
|
|
|
|
else
|
|
|
|
echo nameserver 8.8.8.8 > $TMP_MOUNT_PATH/etc/resolv.conf
|
|
|
|
fi
|
2012-11-09 11:04:13 +00:00
|
|
|
|
|
|
|
# supporting kernel file systems
|
|
|
|
sudo mount -t proc none $TMP_MOUNT_PATH/proc
|
|
|
|
sudo mount --bind /dev $TMP_MOUNT_PATH/dev
|
|
|
|
sudo mount -t sysfs none $TMP_MOUNT_PATH/sys
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Helper function to run a command inside the chroot
|
|
|
|
function run_in_target() {
|
|
|
|
# -E to preserve http_proxy
|
|
|
|
sudo -E chroot $TMP_MOUNT_PATH "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Helper function to run a directory of scripts inside the chroot
|
|
|
|
function run_d_in_target() {
|
2012-11-30 20:47:57 +00:00
|
|
|
check_element
|
2012-11-09 11:04:13 +00:00
|
|
|
# If we can find a directory of hooks to run in the target filesystem, bind
|
|
|
|
# mount it into the target and then execute run-parts in a chroot
|
|
|
|
if [ -d ${TMP_HOOKS_PATH}/$1.d ] ; then
|
|
|
|
sudo mkdir $TMP_MOUNT_PATH/tmp/in_target.d
|
|
|
|
sudo mount --bind ${TMP_HOOKS_PATH} $TMP_MOUNT_PATH/tmp/in_target.d
|
|
|
|
sudo mount -o remount,ro,bind ${TMP_HOOKS_PATH} $TMP_MOUNT_PATH/tmp/in_target.d
|
2012-12-14 07:17:00 +00:00
|
|
|
check_break before-$1 run_in_target bash
|
2013-02-13 22:16:00 +00:00
|
|
|
run_in_target run-parts /tmp/in_target.d/$1.d
|
2012-12-14 07:17:00 +00:00
|
|
|
check_break after-$1 run_in_target bash
|
2012-11-09 11:04:13 +00:00
|
|
|
sudo umount -f $TMP_MOUNT_PATH/tmp/in_target.d
|
|
|
|
sudo rmdir $TMP_MOUNT_PATH/tmp/in_target.d
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Run a directory of hooks outside the target.
|
|
|
|
function run_d() {
|
2012-11-30 20:47:57 +00:00
|
|
|
check_element
|
2012-12-14 07:17:00 +00:00
|
|
|
check_break before-$1 bash
|
2012-11-09 11:04:13 +00:00
|
|
|
if [ -d ${TMP_HOOKS_PATH}/$1.d ] ; then
|
|
|
|
run-parts ${TMP_HOOKS_PATH}/$1.d
|
|
|
|
fi
|
2012-12-14 07:17:00 +00:00
|
|
|
check_break after-$1 bash
|
2012-11-09 11:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function prepare_first_boot () {
|
2012-12-14 07:17:00 +00:00
|
|
|
check_break before-first-boot run_in_target bash
|
2012-11-09 11:04:13 +00:00
|
|
|
if [ -d ${TMP_HOOKS_PATH}/first-boot.d ] ; then
|
|
|
|
sudo cp -t $TMP_MOUNT_PATH/etc/ -a $TMP_HOOKS_PATH/first-boot.d
|
|
|
|
run_in_target mv /etc/rc.local /etc/rc.local.REAL
|
|
|
|
sudo dd of=$TMP_MOUNT_PATH/etc/rc.local <<EOF
|
|
|
|
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
set -o xtrace
|
|
|
|
|
2013-02-13 22:16:00 +00:00
|
|
|
run-parts /etc/first-boot.d
|
2012-11-09 11:04:13 +00:00
|
|
|
rm -fr /etc/first-boot.d
|
|
|
|
mv /etc/rc.local.REAL /etc/rc.local
|
|
|
|
exit 0
|
|
|
|
EOF
|
|
|
|
run_in_target chmod 755 /etc/rc.local
|
|
|
|
fi
|
2012-12-14 07:17:00 +00:00
|
|
|
check_break after-first-boot run_in_target bash
|
2012-11-09 11:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function finalise_base () {
|
2013-02-13 22:16:00 +00:00
|
|
|
TARGET_ROOT=$TMP_MOUNT_PATH run_d cleanup
|
2012-11-09 11:04:13 +00:00
|
|
|
# Now remove the resolv.conf we created above
|
|
|
|
sudo rm -f $TMP_MOUNT_PATH/etc/resolv.conf
|
|
|
|
# The we need to recreate it as a link
|
|
|
|
sudo ln -sf ../run/resolvconf/resolv.conf $TMP_MOUNT_PATH/etc/resolv.conf
|
|
|
|
}
|
|
|
|
|
|
|
|
function compress_image () {
|
|
|
|
# Recreate our image to throw away unnecessary data
|
2012-12-14 00:25:18 +00:00
|
|
|
qemu-img convert ${COMPRESS_IMAGE:+-c} $TMP_IMAGE_PATH -O qcow2 $TMP_IMAGE_PATH-new
|
2012-11-09 11:04:13 +00:00
|
|
|
rm $TMP_IMAGE_PATH
|
|
|
|
mv $TMP_IMAGE_PATH-new $TMP_IMAGE_PATH
|
|
|
|
}
|
|
|
|
|
2012-12-18 20:45:02 +00:00
|
|
|
function do_extra_package_install () {
|
|
|
|
# Install any packages that were requested with the -p command line option
|
|
|
|
if [ "$INSTALL_PACKAGES" != "" ]; then
|
2013-01-28 05:41:54 +00:00
|
|
|
run_in_target install-packages ${INSTALL_PACKAGES[@]}
|
2012-12-18 20:45:02 +00:00
|
|
|
fi
|
|
|
|
}
|
2013-01-28 05:41:54 +00:00
|
|
|
|
|
|
|
function copy_elements_lib () {
|
|
|
|
sudo mkdir -p $TMP_MOUNT_PATH/lib/diskimage-builder
|
|
|
|
sudo cp -t $TMP_MOUNT_PATH/lib/diskimage-builder $_LIB/elements-functions
|
2013-02-11 04:01:36 +00:00
|
|
|
}
|