2012-11-17 02:30:15 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# Copyright 2012 Hewlett-Packard Development Company, L.P.
|
2013-05-24 11:50:11 +00:00
|
|
|
#
|
2012-11-17 02:30:15 +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-05-24 11:50:11 +00:00
|
|
|
#
|
2012-11-17 02:30:15 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2013-05-24 11:50:11 +00:00
|
|
|
#
|
2012-11-17 02:30:15 +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.
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
SCRIPTNAME=$(basename $0)
|
2013-07-30 18:23:31 +00:00
|
|
|
SCRIPT_HOME=$(dirname $0)
|
|
|
|
if [ -d $SCRIPT_HOME/../share/diskimage-builder ]
|
|
|
|
then
|
|
|
|
export _PREFIX=$SCRIPT_HOME/../share/diskimage-builder
|
|
|
|
else
|
|
|
|
export _PREFIX=$SCRIPT_HOME/..
|
|
|
|
fi
|
|
|
|
export _LIB=$_PREFIX/lib
|
2012-11-17 02:30:15 +00:00
|
|
|
source $_LIB/die
|
|
|
|
|
|
|
|
function show_options () {
|
|
|
|
echo "Usage: $SCRIPTNAME -i <image> [-d <outdir>] [-o <prefix]"
|
|
|
|
echo
|
|
|
|
echo "Options:"
|
|
|
|
echo " -i filename -- extract kernel + ramdisk from this file"
|
|
|
|
echo " -d dir -- save files in \$dir; defaults to /tmp"
|
|
|
|
echo " -o prefix -- set the prefix of the output files; defaults to 'baremetal'"
|
|
|
|
echo " eg, baremetal-vmlinuz and baremetal-initrd"
|
|
|
|
echo " -x -- turn on tracing"
|
|
|
|
echo " -h -- print this message"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2014-01-20 12:00:21 +00:00
|
|
|
echo 'DEPRECATED: Please consider using the `baremetal` element.'
|
|
|
|
echo
|
|
|
|
|
2012-11-17 02:30:15 +00:00
|
|
|
TEMP=`getopt -o hd:i:o:x -n $SCRIPTNAME -- "$@"`
|
|
|
|
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
|
|
|
|
|
|
|
|
# Note the quotes around `$TEMP': they are essential!
|
|
|
|
eval set -- "$TEMP"
|
|
|
|
|
|
|
|
export OUT_DIR=/tmp
|
|
|
|
export OUT_PFX=baremetal
|
|
|
|
|
|
|
|
while true ; do
|
|
|
|
case "$1" in
|
|
|
|
-d) export OUT_DIR=$2; shift 2 ;;
|
|
|
|
-i) export IMAGE_FILE=$2; shift 2 ;;
|
|
|
|
-o) export OUT_PFX=$2; shift 2 ;;
|
|
|
|
-h) show_options;;
|
|
|
|
-x) shift; set -x;;
|
|
|
|
--) shift ; break ;;
|
|
|
|
*) echo "Internal error!" ; exit 1 ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [[ ! -n "$IMAGE_FILE" ]]; then
|
|
|
|
die "You must specify an image to read from with -i \$filename"
|
|
|
|
elif [[ ! -r "$IMAGE_FILE" ]]; then
|
|
|
|
die "Image file $IMAGE_FILE is not readable"
|
|
|
|
fi
|
|
|
|
|
|
|
|
source $_LIB/img-defaults
|
|
|
|
source $_LIB/common-functions
|
|
|
|
source $_LIB/img-functions
|
|
|
|
|
2013-12-09 12:58:25 +00:00
|
|
|
echo "Extracting kernel + ramdisk from $IMAGE_FILE to $OUT_PFX-vmlinuz and $OUT_PFX-initrd in $OUT_DIR"
|
2012-11-17 02:30:15 +00:00
|
|
|
|
2013-05-20 10:59:21 +00:00
|
|
|
ensure_nbd
|
|
|
|
|
2012-11-17 02:30:15 +00:00
|
|
|
# sets WORK_DIR
|
|
|
|
mount_qcow_image $IMAGE_FILE
|
|
|
|
|
|
|
|
# Dig up the initrd and kernel to use.
|
|
|
|
BOOTDIR="$WORK_DIR/boot"
|
2013-05-24 11:50:11 +00:00
|
|
|
KERNEL=
|
|
|
|
RAMDISK=
|
|
|
|
if [ -f $WORK_DIR/etc/redhat-release ]; then
|
2013-10-17 09:12:01 +00:00
|
|
|
|
|
|
|
# Prioritize PAE if present
|
2013-12-09 12:58:25 +00:00
|
|
|
KERNEL=$(ls -1rv $BOOTDIR/vmlinuz* | grep PAE | grep -v debug | head -1)
|
2013-10-17 09:12:01 +00:00
|
|
|
if [ ! $KERNEL ]; then
|
2013-12-09 12:58:25 +00:00
|
|
|
KERNEL=$(ls -1rv $BOOTDIR/vmlinuz* | grep -v debug | head -1)
|
2013-10-17 09:12:01 +00:00
|
|
|
if [ ! $KERNEL ]; then
|
|
|
|
echo "No suitable kernel found."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2013-12-09 12:58:25 +00:00
|
|
|
KERNEL=$(basename $KERNEL)
|
2013-10-17 09:12:01 +00:00
|
|
|
KERNEL_VERSION=`echo $KERNEL | sed 's/vmlinuz-//g'`
|
|
|
|
|
|
|
|
RAMDISK=$(basename `ls $BOOTDIR/initramfs-$KERNEL_VERSION.img`)
|
|
|
|
if [ ! $RAMDISK ]; then
|
|
|
|
echo "Can't find an initramfs for the $KERNEL_VERSION version of the kernel."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2013-05-24 11:50:11 +00:00
|
|
|
elif [ -f $WORK_DIR/etc/debian_version ]; then
|
2013-09-12 09:26:57 +00:00
|
|
|
KERNEL=$(basename `ls -1rv $BOOTDIR/vmlinuz*generic | head -1`)
|
|
|
|
RAMDISK=$(basename `ls -1rv $BOOTDIR/initrd*generic | head -1`)
|
2014-02-06 22:46:28 +00:00
|
|
|
elif [ -f $WORK_DIR/etc/SuSE-release ]; then
|
|
|
|
KERNEL=$(basename `ls -1rv $BOOTDIR/vmlinuz`)
|
|
|
|
RAMDISK=$(basename `ls -1rv $BOOTDIR/initrd`)
|
2013-05-24 11:50:11 +00:00
|
|
|
else
|
|
|
|
echo "ERROR: Unable to detect operating system"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2012-11-17 02:30:15 +00:00
|
|
|
sudo cp $BOOTDIR/$KERNEL $OUT_DIR/$OUT_PFX-vmlinuz
|
|
|
|
sudo cp $BOOTDIR/$RAMDISK $OUT_DIR/$OUT_PFX-initrd
|
|
|
|
sudo chmod a+r $OUT_DIR/$OUT_PFX-vmlinuz
|
|
|
|
sudo chmod a+r $OUT_DIR/$OUT_PFX-initrd
|
|
|
|
|
|
|
|
unmount_qcow_image
|