diff --git a/bin/disk-image-get-kernel b/bin/disk-image-get-kernel
index 5eb487f71..7fa6785f9 100755
--- a/bin/disk-image-get-kernel
+++ b/bin/disk-image-get-kernel
@@ -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
diff --git a/elements/baremetal/cleanup.d/99-extract-kernel-and-ramdisk b/elements/baremetal/cleanup.d/99-extract-kernel-and-ramdisk
index 61f0bf9ae..2f3bd5a53 100755
--- a/elements/baremetal/cleanup.d/99-extract-kernel-and-ramdisk
+++ b/elements/baremetal/cleanup.d/99-extract-kernel-and-ramdisk
@@ -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
diff --git a/elements/vm/cleanup.d/51-bootloader b/elements/vm/cleanup.d/51-bootloader
new file mode 100755
index 000000000..16514d5c7
--- /dev/null
+++ b/elements/vm/cleanup.d/51-bootloader
@@ -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
diff --git a/elements/vm/finalise.d/51-bootloader b/elements/vm/finalise.d/51-bootloader
index 1938200d3..46b401715 100755
--- a/elements/vm/finalise.d/51-bootloader
+++ b/elements/vm/finalise.d/51-bootloader
@@ -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 {
diff --git a/lib/img-functions b/lib/img-functions
index 6db7e1b89..7b17e1281 100644
--- a/lib/img-functions
+++ b/lib/img-functions
@@ -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
+}