From e5fcb277906cde94ad8a5ca25135867888d1cd27 Mon Sep 17 00:00:00 2001 From: Devananda van der Veen Date: Fri, 16 Nov 2012 18:30:15 -0800 Subject: [PATCH] add some tooling that baremetal-devstack wants - add bin/disk-image-get-kernel to extract vmlinuz/initrd from .qcow2 - add corresponding methods to lib/common-functions - map_nbd - (un)mount_qcow_image --- bin/disk-image-get-kernel | 86 +++++++++++++++++++++++++++++++++++++++ lib/common-functions | 47 +++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100755 bin/disk-image-get-kernel diff --git a/bin/disk-image-get-kernel b/bin/disk-image-get-kernel new file mode 100755 index 00000000..faaaac1b --- /dev/null +++ b/bin/disk-image-get-kernel @@ -0,0 +1,86 @@ +#!/bin/bash +# +# Copyright 2012 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 -e +set -o xtrace + +SCRIPTNAME=$(basename $0) +export _LIB=$(dirname $0)/../lib +source $_LIB/die + +function show_options () { + echo "Usage: $SCRIPTNAME -i [-d ] [-o &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 + +echo "Extracting kernel + ramdisk from $IMAGE_FILE and writing them to $OUT_DIR" + +# sets WORK_DIR +mount_qcow_image $IMAGE_FILE + +# Dig up the initrd and kernel to use. +BOOTDIR="$WORK_DIR/boot" +KERNEL=$(basename `ls -1 $BOOTDIR/vmlinuz*generic | sort -n | tail -1`) +RAMDISK=$(basename `ls -1 $BOOTDIR/initrd*generic | sort -n | tail -1`) +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 + +echo "$OUT_PFX-vmlinuz,$OUT_PFX-initrd" diff --git a/lib/common-functions b/lib/common-functions index 86b311bf..29dd25fd 100644 --- a/lib/common-functions +++ b/lib/common-functions @@ -60,3 +60,50 @@ function eval_run_d () { eval "$TEMP" fi } + +# Usage: map_nbd $image +# Returns nbd device path +function map_nbd { + (lsmod | grep '^nbd ') || sudo modprobe nbd max_part=16 + + if [[ $(qemu-nbd --help | grep cache) == *writeback* ]] ; then + CACHE="--cache=writeback" + else + 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 + sudo mount $NBD_DEV $WORK_DIR +}