Refactor code to select boot kernel

The script to find best kernel from image (used by vm
and baremetal element) is duplicated and is not in sync
with each other. Moving the code to img-functions as a
function will reduce duplicated code and make it reusable
in future.

Since img-functions is not accessible in chroot env,
kernel selection is being moved from finalise.d to
cleanup.d in the vm element.

Change-Id: I8fbccc13a2c61a5191ef9ea5d2a8302a3e43b000
This commit is contained in:
Om Kumar 2014-03-12 12:53:00 +05:30
parent 20ea513854
commit bccffc8bfd
5 changed files with 80 additions and 100 deletions

View file

@ -81,41 +81,7 @@ ensure_nbd
# sets WORK_DIR
mount_qcow_image $IMAGE_FILE
# Dig up the initrd and kernel to use.
BOOTDIR="$WORK_DIR/boot"
KERNEL=
RAMDISK=
if [ -f $WORK_DIR/etc/redhat-release ]; then
# Prioritize PAE if present
KERNEL=$(ls -1rv $BOOTDIR/vmlinuz* | grep PAE | grep -v debug | head -1)
if [ ! $KERNEL ]; then
KERNEL=$(ls -1rv $BOOTDIR/vmlinuz* | grep -v debug | head -1)
if [ ! $KERNEL ]; then
echo "No suitable kernel found."
exit 1
fi
fi
KERNEL=$(basename $KERNEL)
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
elif [ -f $WORK_DIR/etc/debian_version ]; then
KERNEL=$(basename `ls -1rv $BOOTDIR/vmlinuz*generic | head -1`)
RAMDISK=$(basename `ls -1rv $BOOTDIR/initrd*generic | head -1`)
elif [ -f $WORK_DIR/etc/SuSE-release ]; then
KERNEL=$(basename `ls -1rv $BOOTDIR/vmlinuz`)
RAMDISK=$(basename `ls -1rv $BOOTDIR/initrd`)
else
echo "ERROR: Unable to detect operating system"
exit 1
fi
select_boot_kernel_initrd $WORK_DIR
sudo cp $BOOTDIR/$KERNEL $OUT_DIR/$OUT_PFX-vmlinuz
sudo cp $BOOTDIR/$RAMDISK $OUT_DIR/$OUT_PFX-initrd

View file

@ -19,45 +19,10 @@ set -o pipefail
[ -n "$TARGET_ROOT" ]
source $_LIB/img-functions
# Dig up the initrd and kernel to use.
BOOTDIR="$TARGET_ROOT/boot"
KERNEL=
RAMDISK=
if [ -n "${DIB_BAREMETAL_KERNEL_PATTERN:-}" -a -n "${DIB_BAREMETAL_INITRD_PATTERN:-}" ]; then
KERNEL=$(basename `eval ls -1rv "$BOOTDIR/${DIB_BAREMETAL_KERNEL_PATTERN}" | head -1`)
RAMDISK=$(basename `eval ls -1rv "$BOOTDIR/${DIB_BAREMETAL_INITRD_PATTERN}" | head -1`)
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 "")
if [ ! $KERNEL ]; then
KERNEL=$(ls -1rv $BOOTDIR/vmlinuz* | grep -v debug | head -1 || echo "")
if [ ! $KERNEL ]; then
echo "No suitable kernel found."
exit 1
fi
fi
KERNEL=$(basename $KERNEL)
KERNEL_VERSION=`echo $KERNEL | sed 's/vmlinuz-//g'`
RAMDISK=$(basename `ls $BOOTDIR/initramfs-$KERNEL_VERSION.img` || echo "")
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
KERNEL=$(basename `ls -1rv $BOOTDIR/vmlinuz*generic | head -1`)
RAMDISK=$(basename `ls -1rv $BOOTDIR/initrd*generic | head -1`)
elif [ -f $TARGET_ROOT/etc/SuSE-release ]; then
KERNEL=vmlinuz
RAMDISK=initrd
else
echo "ERROR: Unable to detect operating system"
exit 1
fi
select_boot_kernel_initrd $TARGET_ROOT
sudo cp $BOOTDIR/$KERNEL ${IMAGE_NAME}.vmlinuz
sudo cp $BOOTDIR/$RAMDISK ${IMAGE_NAME}.initrd
sudo chmod a+r ${IMAGE_NAME}.vmlinuz

View file

@ -0,0 +1,37 @@
#!/bin/bash
#
# Copyright 2014 Hewlett-Packard Development Company, L.P.
# 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 -eux
set -o pipefail
[ -n "$TARGET_ROOT" ]
source $_LIB/img-functions
# Dig up the initrd and kernel to use.
if [ -d $TARGET_ROOT/boot/syslinux ] ; then
select_boot_kernel_initrd $TARGET_ROOT
cat > $TARGET_ROOT/boot/syslinux/syslinux.cfg<<_EOF_
DEFAULT linux
LABEL linux
KERNEL $KERNEL
APPEND ro root=LABEL=cloudimg-rootfs console=tty0 console=ttyS0,115200
INITRD $RAMDISK
_EOF_
fi

View file

@ -29,33 +29,6 @@ function install_extlinux {
mkdir -p /boot/syslinux
extlinux --install /boot/syslinux
if [ -f /etc/redhat-release ]; then
kernel=$(ls -1rv /boot/vmlinuz* | head -1)
initrd=$(ls -1rv /boot/initramfs* | head -1)
elif [ -f /etc/SuSE-release ]; then
kernel=$(ls -1rv /boot/vmlinuz* | head -1)
initrd=$(ls -1rv /boot/initrd* | head -1)
elif [ -f /etc/debian_version ]; then
kernel=$(ls -1rv /boot/vmlinuz*generic | head -1)
initrd=$(ls -1rv /boot/initrd*generic | head -1)
# in case files with "generic" suffix were not found, fall back to default
kernel=${kernel:-$(ls -1rv /boot/vmlinuz* | head -1)}
initrd=${initrd:-$(ls -1rv /boot/initrd* | head -1)}
else
echo "Unable to find kernel and initram"
exit 1
fi
cat > /boot/syslinux/syslinux.cfg<<_EOF_
DEFAULT linux
LABEL linux
KERNEL $kernel
APPEND ro root=LABEL=cloudimg-rootfs console=tty0 console=ttyS0,115200
INITRD $initrd
_EOF_
}
function install_grub2 {

View file

@ -127,3 +127,42 @@ 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
}
# 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
KERNEL=$(basename `eval ls -1rv "$BOOTDIR/${DIB_BAREMETAL_KERNEL_PATTERN}" | head -1`)
RAMDISK=$(basename `eval ls -1rv "$BOOTDIR/${DIB_BAREMETAL_INITRD_PATTERN}" | head -1`)
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-}
RAMDISK=$(basename `ls $BOOTDIR/initramfs-$KERNEL_VERSION.img` || echo "")
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
KERNEL=$(basename `ls -1rv $BOOTDIR/vmlinuz*generic | head -1`)
RAMDISK=$(basename `ls -1rv $BOOTDIR/initrd*generic | head -1`)
# in case files with "generic" suffix were not found, fall back to default
KERNEL=${KERNEL:-$(basename `ls -1rv $BOOTDIR/vmlinuz* | head -1`)}
RAMDISK=${RAMDISK:-$(basename `ls -1rv $BOOTDIR/initrd* | head -1`)}
elif [ -f $TARGET_ROOT/etc/SuSE-release ]; then
KERNEL=vmlinuz
RAMDISK=initrd
else
echo "ERROR: Unable to detect operating system"
exit 1
fi
}