Port old baremetal-mkinitrd.sh to the new image creation standards

This commit is contained in:
Chris Jones 2012-11-09 18:18:08 +00:00 committed by Robert Collins
parent 7da463e617
commit eb584f14ed
10 changed files with 254 additions and 193 deletions

View File

@ -1,157 +0,0 @@
#!/bin/bash
# Copyright (c) 2012 NTT DOCOMO, INC.
# 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.
if [ $# -lt 2 ]; then
cat <<EOF
usage: $0 OUTFILE KERNEL_VERSION [MODULE_ROOT]
EOF
exit 1
fi
FLAVOUR=${FLAVOUR:-baremetal}
echo "Building flavour(s): ${FLAVOUR}"
if [[ "$FLAVOUR" == *hwdiscovery* ]] ; then
HWDISCOVERY_BINARIES="lshw"
for binary in ${HWDISCOVERY_BINARIES} ; do
LOCATION=$(which $binary)
if [ -z "$LOCATION" ]; then
echo "$binary is not found in PATH" 1>&2
echo "Please install it"
exit 1
fi
done
FLAVOUR_BINARIES="${FLAVOUR_BINARIES} ${HWDISCOVERY_BINARIES}"
fi
if [[ "$FLAVOUR" == *hwburnin* ]] ; then
HWDISCOVERY_BINARIES="spew memtester"
for binary in ${HWDISCOVERY_BINARIES} ; do
LOCATION=$(which $binary)
if [ -z "$LOCATION" ]; then
echo "$binary is not found in PATH" 1>&2
echo "Please install it"
exit 1
fi
done
FLAVOUR_BINARIES="${FLAVOUR_BINARIES} ${HWDISCOVERY_BINARIES}"
fi
BUSYBOX=${BUSYBOX:-$(which busybox)}
if [ -z "$BUSYBOX" ]; then
echo "busybox is not found in PATH" 1>&2
echo "Please set environment variable BUSYBOX to path to busybox executable" 1>&2
exit 1
fi
function fullpath() {
local f=$1
if [ "${f#/}" = "$f" ]; then
echo `pwd`/"$f"
else
echo "$f"
fi
}
DIR=`dirname "$0"`
INIT="$DIR/scripts/init"
FUNCTIONS_D="$DIR/scripts/d"
LIB_UDEV="$DIR/udev"
DEST=`fullpath "$1"`
KERNEL_VERSION=$2
MODULE_ROOT=${3:-""}
MODULE_DIR=$MODULE_ROOT/lib/modules/$KERNEL_VERSION
FIRMWARE_DIR=$MODULE_ROOT/lib/firmware
if [ ! -d "$MODULE_DIR" ]; then
echo "ERROR: kernel module directory not found at $MODULE_DIR"
return 1
fi
INITRD_DIR=`mktemp -t -d baremetal-mkinitrd.XXXXXXXX`
if [ $? -ne 0 ]; then
exit 1
fi
function cleanup() {
rm -r "$INITRD_DIR"
}
trap cleanup EXIT
echo "working in $INITRD_DIR"
mkdir -p "$INITRD_DIR/bin"
ln -s bin "$INITRD_DIR/sbin"
mkdir -p "$INITRD_DIR/lib"
ln -s lib "$INITRD_DIR/lib64"
mkdir -p "$INITRD_DIR/lib/modules"
mkdir -p "$INITRD_DIR/etc"
mkdir -p "$INITRD_DIR/etc/udev"
cp -a "$LIB_UDEV" "$INITRD_DIR/lib/udev"
mkdir -p "$INITRD_DIR/etc/modprobe.d"
echo "blacklist evbug" > "$INITRD_DIR/etc/modprobe.d/blacklist.conf"
mkdir -p "$INITRD_DIR/etc/udev"
cat >"$INITRD_DIR/etc/udev/udev.conf" <<EOF
udev_root="/dev"
udev_rules="/lib/udev/rules.d"
udev_log="no"
EOF
libs=
for i in "$BUSYBOX" bash modprobe udevd udevadm wget tgtd tgtadm reboot shutdown $FLAVOUR_BINARIES; do
if "$BUSYBOX" --list | grep "^$i\$" >/dev/null; then
continue
fi
path=`which $i 2>/dev/null` || path=$i
if ! [ -x "$path" ]; then
echo "$i is not found in PATH" 2>&1
exit 1
fi
cp -L "$path" "$INITRD_DIR/bin/"
if l=`ldd "$path"`; then
l=$( echo "$l" | grep '/' | tr "\t" " " )
l=$( echo "$l" | sed 's/^.* => \([^ ]*\).*$/\1/' )
l=$( echo "$l" | sed 's/^ *\([^ ]*\) *(0x[0-9a-f]*)/\1/' )
l=$( echo "$l" | tr " " "\n" )
libs=$( printf "%s\n%s\n" "$l" "$libs" | sort | uniq )
fi
done
cp $libs "$INITRD_DIR/lib/"
for i in $( "$BUSYBOX" --list ); do
if [ -f "$INITRD_DIR/bin/$i" ]; then
echo "skip $i"
continue
fi
ln -s busybox "$INITRD_DIR/bin/$i"
done
cp -a "$MODULE_DIR" "$INITRD_DIR/lib/modules/$KERNEL_VERSION"
cp -a "$FIRMWARE_DIR" "$INITRD_DIR/lib/firmware"
cp "$INIT" "$INITRD_DIR/init"
chmod +x $INITRD_DIR/init
for F in "$FUNCTIONS_D"/* ; do
cp "$F" "$INITRD_DIR"
done
(cd "$INITRD_DIR"; find . | cpio -o -H newc | gzip > "$DEST" )

View File

@ -34,6 +34,7 @@ done
for arg do IMAGE_FLAVOUR="$IMAGE_FLAVOUR $arg" ; done
source $_LIB/img-defaults
source $_LIB/common-functions
source $_LIB/img-functions
echo "Building flavours: $IMAGE_FLAVOUR"

91
bin/ramdisk-image-create Executable file
View File

@ -0,0 +1,91 @@
#!/bin/bash
# XXX: Only portions of this are the NTT original, much is now HP work
# Copyright (c) 2012 NTT DOCOMO, INC.
# 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.
set -e
SCRIPTNAME=$(basename $0)
export _DIR=$(dirname $0)
export _LIB=${_DIR}/../lib
source $_LIB/die
function show_options () {
echo "Options:"
echo " -m PATH -- Path to find lib/modules. Default /"
echo " -k VERSION -- Kernel version. Default $(uname -r)"
echo " -h -- This help"
echo " -o FILENAME -- Output file"
echo " -x -- turn on tracing"
exit 0
}
TEMP=$(getopt -o m:k:ho:x -n $SCRIPTNAME -- "$@")
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
case "$1" in
-m) export MODULE_ROOT=$2; shift 2 ;;
-k) export KERNEL_VERSION=$2; shift 2 ;;
-o) export IMAGE_NAME=$2; shift 2 ;;
-h) show_options;;
-x) shift; set -x;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
for arg do RAMDISK_FLAVOUR="$RAMDISK_FLAVOUR $arg" ; done
source $_LIB/ramdisk-defaults
source $_LIB/common-functions
source $_LIB/ramdisk-functions
echo "Building flavour(s): ${RAMDISK_FLAVOUR}"
echo "Discovering binary dependencies"
ensure_binaries
mk_build_dir
INIT="$_DIR/../scripts/init"
FUNCTIONS_D="$_DIR/../scripts/d"
LIB_UDEV="$_DIR/../udev"
MODULE_DIR=$MODULE_ROOT/lib/modules/$KERNEL_VERSION
FIRMWARE_DIR=$MODULE_ROOT/lib/firmware
if [ ! -d "$MODULE_DIR" ]; then
echo "ERROR: kernel module directory not found at $MODULE_DIR"
return 1
fi
mk_build_dir
mkdir -p $TMP_BUILD_DIR/mnt
TMP_MOUNT_PATH=$TMP_BUILD_DIR/mnt
echo "working in $TMP_MOUNT_PATH"
create_base
populate_lib
populate_busybox
populate_init
finalise_image
save_image $IMAGE_NAME

View File

@ -0,0 +1 @@
busybox

View File

@ -0,0 +1,2 @@
spew
memtester

View File

@ -0,0 +1 @@
lshw

36
lib/common-functions Normal file
View File

@ -0,0 +1,36 @@
function mk_build_dir () {
export TMP_BUILD_DIR=$(mktemp -t -d image.XXXXXXXX)
[ $? -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
for _FLAVOUR in $IMAGE_FLAVOUR ; do
[ -d $FLAVOURS_DIR/$_FLAVOUR ] || die "The flavour does not exist." ;
cp -t $TMP_HOOKS_PATH -a $FLAVOURS_DIR/$_FLAVOUR/* ;
done
}
# Check that a real flavour has been chosen (prevents foot-guns)
function check_flavour () {
[ -d $TMP_HOOKS_PATH ] || generate_hooks
}

View File

@ -23,15 +23,6 @@ function cleanup () {
rm -rf $TMP_BUILD_DIR
}
function mk_build_dir () {
export TMP_BUILD_DIR=$(mktemp -t -d image.XXXXXXXX)
[ $? -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 ensure_nbd () {
NBD=`which qemu-nbd`
if [ -z "$NBD" ]; then
@ -101,19 +92,6 @@ function run_in_target() {
sudo -E chroot $TMP_MOUNT_PATH "$@"
}
function generate_hooks () {
mkdir -p $TMP_HOOKS_PATH
for _FLAVOUR in $IMAGE_FLAVOUR ; do
[ -d $FLAVOURS_DIR/$_FLAVOUR ] || die "The flavour does not exist." ;
cp -t $TMP_HOOKS_PATH -a $FLAVOURS_DIR/$_FLAVOUR/* ;
done
}
# Check that a real flavour has been chosen (prevents foot-guns)
function check_flavour () {
[ -d $TMP_HOOKS_PATH ] || generate_hooks
}
# Helper function to run a directory of scripts inside the chroot
function run_d_in_target() {
check_flavour
@ -171,20 +149,6 @@ function compress_image () {
mv $TMP_IMAGE_PATH-new $TMP_IMAGE_PATH
}
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 block_apt_translations () {
# Configure APT not to fetch translations files
sudo dd of=$TMP_MOUNT_PATH/etc/apt/apt.conf.d/95no-translations <<EOF

6
lib/ramdisk-defaults Normal file
View File

@ -0,0 +1,6 @@
# options for initrd-create.sh
KERNEL_VERSION=${KERNEL_VERSION:-$(uname -r)}
MODULE_ROOT=${MODULE_ROOT:-""}
BUSYBOX=${BUSYBOX:-$(which busybox)}
IMAGE_NAME=${IMAGE_NAME:-"ramdisk"}
FLAVOURS_DIR=$(dirname $0)/../flavours

116
lib/ramdisk-functions Normal file
View File

@ -0,0 +1,116 @@
function fullpath() {
local f=$1
if [ "${f#/}" = "$f" ]; then
echo `pwd`/"$f"
else
echo "$f"
fi
}
function cleanup () {
rm -rf "$TMP_BUILD_DIR"
}
function ensure_binaries() {
BINARY_DEPS="${BUSYBOX}"
for _FLVR in ${RAMDISK_FLAVOUR} ; do
_FILE="${FLAVOURS_DIR}/${_FLVR}/binary-deps"
if [ -a $_FILE ]; then
for _LINE in $(cat $_FILE) ; do
BINARY_DEPS="${BINARY_DEPS} $_LINE"
done
fi
done
for _BIN in $BINARY_DEPS ; do
_LOCATION=""
_LOCATION="${_LOCATION:-$(which $_BIN)}"
if [ -z "$_LOCATION" ]; then
echo "$_BIN is not found in your PATH" 1>&2
echo "Please install it on your system"
exit 1
fi
done
export BINARY_DEPS
}
function create_base () {
echo "Creating base system"
mkdir -p "$TMP_MOUNT_PATH/bin"
ln -s bin "$TMP_MOUNT_PATH/sbin"
mkdir -p "$TMP_MOUNT_PATH/lib"
ln -s lib "$TMP_MOUNT_PATH/lib64"
mkdir -p "$TMP_MOUNT_PATH/lib/modules"
mkdir -p "$TMP_MOUNT_PATH/etc"
mkdir -p "$TMP_MOUNT_PATH/etc/udev"
cp -a "$LIB_UDEV" "$TMP_MOUNT_PATH/lib/udev"
mkdir -p "$TMP_MOUNT_PATH/etc/modprobe.d"
echo "blacklist evbug" > "$TMP_MOUNT_PATH/etc/modprobe.d/blacklist.conf"
mkdir -p "$TMP_MOUNT_PATH/etc/udev"
cat >"$TMP_MOUNT_PATH/etc/udev/udev.conf" <<EOF
udev_root="/dev"
udev_rules="/lib/udev/rules.d"
udev_log="no"
EOF
}
function populate_lib () {
echo "Populating /lib"
libs=
for i in "$BUSYBOX" bash modprobe udevd udevadm wget tgtd tgtadm reboot shutdown $BINARY_DEPS ; do
if "$BUSYBOX" --list | grep "^$i\$" >/dev/null; then
continue
fi
path=`which $i 2>/dev/null` || path=$i
if ! [ -x "$path" ]; then
echo "$i is not found in PATH" 2>&1
exit 1
fi
cp -L "$path" "$TMP_MOUNT_PATH/bin/"
if l=`ldd "$path"`; then
l=$( echo "$l" | grep '/' | tr "\t" " " )
l=$( echo "$l" | sed 's/^.* => \([^ ]*\).*$/\1/' )
l=$( echo "$l" | sed 's/^ *\([^ ]*\) *(0x[0-9a-f]*)/\1/' )
l=$( echo "$l" | tr " " "\n" )
libs=$( printf "%s\n%s\n" "$l" "$libs" | sort | uniq )
fi
done
cp $libs "$TMP_MOUNT_PATH/lib/"
cp -a "$MODULE_DIR" "$TMP_MOUNT_PATH/lib/modules/$KERNEL_VERSION"
cp -a "$FIRMWARE_DIR" "$TMP_MOUNT_PATH/lib/firmware"
}
function populate_busybox () {
echo "Creating symlinks for busybox binaries"
for i in $( "$BUSYBOX" --list ); do
if [ -f "$TMP_MOUNT_PATH/bin/$i" ]; then
echo "skip $i"
continue
fi
ln -s busybox "$TMP_MOUNT_PATH/bin/$i"
done
}
function populate_init () {
echo "Installing init"
# XXX: This needs to understand flavours and the pre-shipped init needs to split up
cp "$INIT" "$TMP_MOUNT_PATH/init"
chmod +x $TMP_MOUNT_PATH/init
for F in "$FUNCTIONS_D"/* ; do
cp "$F" "$TMP_MOUNT_PATH"
done
}
function finalise_image () {
echo "Finalising image"
(cd "$TMP_MOUNT_PATH"; find . | cpio -o -H newc | gzip > "$TMP_IMAGE_PATH" )
}