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 18:18:08 +00:00
|
|
|
function mk_build_dir () {
|
2013-04-12 14:43:53 +00:00
|
|
|
export TMP_BUILD_DIR=$(mktemp -t -d --tmpdir=${TMP_DIR:-/tmp} image.XXXXXXXX)
|
2012-11-09 18:18:08 +00:00
|
|
|
[ $? -eq 0 ] || die "Failed to create tmp directory"
|
|
|
|
trap cleanup EXIT
|
|
|
|
echo Building in $TMP_BUILD_DIR
|
|
|
|
export TMP_IMAGE_PATH=$TMP_BUILD_DIR/image
|
|
|
|
export TMP_HOOKS_PATH=$TMP_BUILD_DIR/hooks
|
|
|
|
}
|
|
|
|
|
|
|
|
function save_image () {
|
|
|
|
# TODO: this really should rename the old file
|
|
|
|
if [ -f $1 ] ; then
|
|
|
|
echo "Old Image file Found REMOVING"
|
|
|
|
rm -f $1
|
|
|
|
fi
|
|
|
|
|
|
|
|
cp $TMP_IMAGE_PATH $1
|
|
|
|
rm -r $TMP_BUILD_DIR
|
|
|
|
# All done!
|
|
|
|
trap EXIT
|
|
|
|
echo "Image file $1 created..."
|
|
|
|
}
|
|
|
|
|
|
|
|
function generate_hooks () {
|
|
|
|
mkdir -p $TMP_HOOKS_PATH
|
2013-01-25 22:23:56 +00:00
|
|
|
for _ELEMENT in $IMAGE_ELEMENT ; do
|
2013-02-25 09:17:32 +00:00
|
|
|
for dir in $(echo $ELEMENTS_PATH | tr ":" " ") ; do
|
|
|
|
[ -d $dir/$_ELEMENT ] || continue
|
|
|
|
cp -t $TMP_HOOKS_PATH -a $dir/$_ELEMENT/* ;
|
|
|
|
break
|
|
|
|
done
|
2012-11-09 18:18:08 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2012-12-14 07:17:00 +00:00
|
|
|
# Call the supplied break-in routine if the named point is listed in the break
|
|
|
|
# list.
|
|
|
|
# $1 the break point.
|
|
|
|
# $2.. what to call if a break is needed
|
|
|
|
function check_break () {
|
|
|
|
if echo "$break" | egrep -e "(,|^)$1(,|$)" -q; then
|
|
|
|
echo "Starting debug shell. Exit to resume building." >&2
|
|
|
|
echo At stage $1 >&2
|
|
|
|
shift
|
|
|
|
"$@"
|
|
|
|
echo "Resuming" >&2
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2012-11-30 20:47:57 +00:00
|
|
|
# Check that a real element has been chosen (prevents foot-guns)
|
|
|
|
function check_element () {
|
2012-11-09 18:18:08 +00:00
|
|
|
[ -d $TMP_HOOKS_PATH ] || generate_hooks
|
|
|
|
}
|
|
|
|
|
2012-11-11 22:47:26 +00:00
|
|
|
# Run a hook, looking for a regex in its stdout, and eval the matched lines.
|
|
|
|
# $1 is the hook to run
|
|
|
|
# $2 is the regex to look for
|
|
|
|
function eval_run_d () {
|
|
|
|
local TEMP=`run_d $1`
|
|
|
|
echo "$TEMP"
|
2012-11-12 00:45:57 +00:00
|
|
|
if [ `echo "$TEMP" | grep -s "$2"` ]; then
|
|
|
|
TEMP=`echo "$TEMP" | grep "$2"`
|
|
|
|
eval "$TEMP"
|
|
|
|
fi
|
2012-11-11 22:47:26 +00:00
|
|
|
}
|
2012-11-17 02:30:15 +00:00
|
|
|
|
|
|
|
# Usage: map_nbd $image
|
|
|
|
# Returns nbd device path
|
|
|
|
function map_nbd {
|
|
|
|
(lsmod | grep '^nbd ') || sudo modprobe nbd max_part=16
|
|
|
|
|
2013-01-25 22:23:56 +00:00
|
|
|
if [[ $(qemu-nbd --help | grep cache) == *writeback* ]] ; then
|
2012-11-17 02:30:15 +00:00
|
|
|
CACHE="--cache=writeback"
|
2013-01-25 22:23:56 +00:00
|
|
|
else
|
2012-11-17 02:30:15 +00:00
|
|
|
echo "Warning: qemu-nbd without --cache=writeback is /slow/."
|
|
|
|
CACHE=""
|
|
|
|
fi
|
|
|
|
NBD_DEV=
|
|
|
|
for i in `seq 0 15`; do
|
|
|
|
if [ ! -e /sys/block/nbd$i/pid ]; then
|
|
|
|
NBD_DEV=/dev/nbd$i
|
|
|
|
# Connect to nbd and wait till it is ready
|
|
|
|
sudo qemu-nbd -c $NBD_DEV $CACHE $1
|
|
|
|
if ! timeout 60 sh -c "while ! [ -e /sys/block/nbd$i/pid ]; do sleep 1; done"; then
|
|
|
|
echo "Couldn't connect $NBD_DEV"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ -z "$NBD_DEV" ]; then
|
|
|
|
echo "No free NBD slots"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Delete and unmount the working dir used in extracting kernel/initrd
|
|
|
|
function unmount_qcow_image () {
|
|
|
|
sudo umount $WORK_DIR || true
|
|
|
|
sudo qemu-nbd -d $NBD_DEV || true
|
|
|
|
sudo rm -rf $WORK_DIR
|
|
|
|
|
|
|
|
trap - SIGHUP SIGINT SIGTERM EXIT
|
|
|
|
}
|
|
|
|
|
|
|
|
function mount_qcow_image() {
|
|
|
|
trap unmount_qcow_image SIGHUP SIGINT SIGTERM EXIT
|
|
|
|
|
|
|
|
WORK_DIR=$(mktemp -d)
|
|
|
|
map_nbd $1
|
2012-11-22 03:00:53 +00:00
|
|
|
if [ -e "${NBD_DEV}p1" ]; then
|
|
|
|
sudo mount ${NBD_DEV}p1 $WORK_DIR
|
|
|
|
else
|
|
|
|
sudo mount ${NBD_DEV} $WORK_DIR
|
|
|
|
fi
|
2012-11-17 02:30:15 +00:00
|
|
|
}
|