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 () {
|
2013-08-15 16:45:31 +00:00
|
|
|
# Calling sync before helps ensure the mount isn't busy when you unmount it.
|
|
|
|
# Previously observing having disk corruption issues; one possibility is
|
|
|
|
# qemu-nbd not flushing dirty pages on disconnect?
|
|
|
|
# https://bugs.launchpad.net/diskimage-builder/+bug/1214388
|
|
|
|
sync
|
|
|
|
|
2012-11-09 11:04:13 +00:00
|
|
|
# unmount from the chroot
|
|
|
|
# Don't use TMP_MOUNT_PATH here, it might not have been set.
|
2013-12-31 09:44:48 +00:00
|
|
|
unmount_dir "$TMP_BUILD_DIR/mnt"
|
2012-11-09 11:04:13 +00:00
|
|
|
if [ -n "$EXTRA_UNMOUNT" ]; then
|
|
|
|
$EXTRA_UNMOUNT
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-10-11 00:01:37 +00:00
|
|
|
function trap_cleanup() {
|
|
|
|
exitval=$?
|
|
|
|
cleanup
|
|
|
|
exit $exitval
|
|
|
|
}
|
|
|
|
|
2012-11-09 11:04:13 +00:00
|
|
|
function cleanup () {
|
|
|
|
unmount_image
|
2013-04-02 00:25:25 +00:00
|
|
|
cleanup_dirs
|
|
|
|
}
|
|
|
|
|
2012-11-09 11:04:13 +00:00
|
|
|
function ensure_nbd () {
|
2012-12-13 17:20:55 +00:00
|
|
|
NBD=`which qemu-nbd` || true
|
2014-07-01 09:27:41 +00:00
|
|
|
if [ -z "$NBD" ]; then
|
2013-06-11 14:03:10 +00:00
|
|
|
echo "qemu-nbd is not found in your PATH"
|
|
|
|
echo "Please install it on your system"
|
|
|
|
exit 1
|
2012-11-09 11:04:13 +00:00
|
|
|
fi
|
|
|
|
# prep nbd for mounting
|
2013-12-09 12:58:25 +00:00
|
|
|
(lsmod | grep '^nbd ' >/dev/null) || sudo modprobe nbd max_part=16
|
2012-11-09 11:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ensure_sudo () {
|
|
|
|
sudo echo "Ensuring sudo is available"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Helper function to run a command inside the chroot
|
2013-05-06 16:11:49 +00:00
|
|
|
function run_in_target () {
|
2013-05-07 12:20:35 +00:00
|
|
|
# Force the inclusion of /usr/local/bin in PATH, this is needed for some
|
|
|
|
# distros that does not include /usr/local/bin in the sudoers secure_path.
|
|
|
|
# Note that we're not expanding PATH during argument processing, the \$
|
|
|
|
# will preserve the PATH syntax until after the sh command runs
|
|
|
|
cmd="PATH=\$PATH:/usr/local/bin ; $@"
|
|
|
|
# -E to preserve http_proxy
|
2013-08-09 13:15:18 +00:00
|
|
|
ORIG_HOME=$HOME
|
|
|
|
export HOME=/root
|
2013-05-07 12:20:35 +00:00
|
|
|
sudo -E chroot $TMP_MOUNT_PATH sh -c "$cmd"
|
2013-08-09 13:15:18 +00:00
|
|
|
export HOME=$ORIG_HOME
|
2012-11-09 11:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Helper function to run a directory of scripts inside the chroot
|
2013-05-06 16:11:49 +00:00
|
|
|
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-08-01 14:50:17 +00:00
|
|
|
trap "check_break after-error run_in_target bash" ERR
|
2013-02-14 18:20:30 +00:00
|
|
|
run_in_target dib-run-parts /tmp/in_target.d/$1.d
|
2013-08-01 14:50:17 +00:00
|
|
|
trap - ERR
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
2014-04-25 23:24:04 +00:00
|
|
|
# If the file has been set immutable, we probably want to keep it
|
|
|
|
if lsattr $TMP_MOUNT_PATH/etc/resolv.conf | grep '^....i' >/dev/null ; then
|
|
|
|
# We're keeping the contents of resolv.conf set in the elements,
|
|
|
|
# so remove the old saved file
|
|
|
|
sudo rm -f $TMP_MOUNT_PATH/etc/resolv.conf.ORIG
|
|
|
|
else
|
|
|
|
# Remove the resolv.conf we created above
|
|
|
|
sudo rm -f $TMP_MOUNT_PATH/etc/resolv.conf
|
|
|
|
# Move the original back
|
|
|
|
if [ -L $TMP_MOUNT_PATH/etc/resolv.conf.ORIG ] || [ -f $TMP_MOUNT_PATH/etc/resolv.conf.ORIG ] ; then
|
|
|
|
sudo mv $TMP_MOUNT_PATH/etc/resolv.conf.ORIG $TMP_MOUNT_PATH/etc/resolv.conf
|
|
|
|
fi
|
2013-06-14 13:32:14 +00:00
|
|
|
fi
|
2012-11-09 11:04:13 +00:00
|
|
|
}
|
|
|
|
|
2013-08-26 09:59:41 +00:00
|
|
|
function compress_and_save_image () {
|
2012-11-09 11:04:13 +00:00
|
|
|
# Recreate our image to throw away unnecessary data
|
2013-02-20 12:07:23 +00:00
|
|
|
test $IMAGE_TYPE != qcow2 && COMPRESS_IMAGE=""
|
2014-06-13 12:20:17 +00:00
|
|
|
if [ -n "$QEMU_IMG_OPTIONS" ]; then
|
|
|
|
EXTRA_OPTIONS="-o $QEMU_IMG_OPTIONS"
|
|
|
|
else
|
|
|
|
EXTRA_OPTIONS=""
|
|
|
|
fi
|
|
|
|
qemu-img convert ${COMPRESS_IMAGE:+-c} -f raw $TMP_IMAGE_PATH -O $IMAGE_TYPE $EXTRA_OPTIONS $1-new
|
2013-08-26 09:59:41 +00:00
|
|
|
|
2014-10-01 19:34:21 +00:00
|
|
|
OUT_IMAGE_PATH=$1-new
|
2014-03-25 01:16:06 +00:00
|
|
|
finish_image $1
|
2013-07-01 16:20:15 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2014-03-12 07:23:00 +00:00
|
|
|
|
|
|
|
# Dig up the initrd and kernel.
|
|
|
|
function select_boot_kernel_initrd () {
|
|
|
|
TARGET_ROOT=$1
|
|
|
|
BOOTDIR=$TARGET_ROOT/boot
|
|
|
|
if [ -n "${DIB_BAREMETAL_KERNEL_PATTERN:-}" -a -n "${DIB_BAREMETAL_INITRD_PATTERN:-}" ]; then
|
2014-09-05 17:00:06 +00:00
|
|
|
KERNEL=$(basename $(eval ls -1rv "$BOOTDIR/${DIB_BAREMETAL_KERNEL_PATTERN}" | head -1))
|
|
|
|
RAMDISK=$(basename $(eval ls -1rv "$BOOTDIR/${DIB_BAREMETAL_INITRD_PATTERN}" | head -1))
|
2014-03-12 07:23:00 +00:00
|
|
|
elif [ -f $TARGET_ROOT/etc/redhat-release ]; then
|
|
|
|
|
|
|
|
# Prioritize PAE if present
|
|
|
|
KERNEL=$(ls -1rv $BOOTDIR/vmlinuz* | grep PAE | grep -v debug | head -1 || echo "")
|
|
|
|
KERNEL=${KERNEL:-$(ls -1rv $BOOTDIR/vmlinuz* | grep -v debug | head -1 || echo "")}
|
|
|
|
if [ ! $KERNEL ]; then
|
|
|
|
echo "No suitable kernel found."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
KERNEL=$(basename $KERNEL)
|
|
|
|
KERNEL_VERSION=${KERNEL#vmlinuz-}
|
2014-09-05 17:00:06 +00:00
|
|
|
RAMDISK=$(basename $(ls $BOOTDIR/initramfs-$KERNEL_VERSION.img) || echo "")
|
2014-03-12 07:23:00 +00:00
|
|
|
if [ ! $RAMDISK ]; then
|
|
|
|
echo "Can't find an initramfs for the $KERNEL_VERSION version of the kernel."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
elif [ -f $TARGET_ROOT/etc/debian_version ]; then
|
2014-06-27 09:11:16 +00:00
|
|
|
KERNEL=$(basename $(ls -1rv $BOOTDIR/vmlinuz*generic 2>/dev/null || ls -1rv $BOOTDIR/vmlinuz* | head -1))
|
|
|
|
RAMDISK=$(basename $(ls -1rv $BOOTDIR/initrd*generic 2>/dev/null || ls -1rv $BOOTDIR/initrd* | head -1))
|
2014-03-12 07:23:00 +00:00
|
|
|
elif [ -f $TARGET_ROOT/etc/SuSE-release ]; then
|
2014-08-31 15:25:04 +00:00
|
|
|
KERNEL=$(basename $(readlink -e $BOOTDIR/vmlinuz))
|
|
|
|
RAMDISK=$(basename $(readlink -e $BOOTDIR/initrd))
|
2014-03-12 07:23:00 +00:00
|
|
|
else
|
|
|
|
echo "ERROR: Unable to detect operating system"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|