Created
June 19, 2014 15:40
-
-
Save metahertz/3736737804d228528634 to your computer and use it in GitHub Desktop.
Converts a BOSH Stemcell (Default QCow2) into RAW for use with OpenStack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Dirty script to take a CloudFoundry BOSH stemcell and convert to RAW and repackage (default in QCOW2) | |
# Matt Johnson <[email protected]> | |
# Set some vars | |
MINPARAMS=1 | |
WORKDIR=`date | sha1sum | awk '{print $1}'` | |
# Check we're being used correctly | |
if [ $# -lt "$MINPARAMS" ] | |
then | |
echo "Need a minimum of one!" | |
echo "----------------------------------------------------" | |
echo "Usage:" | |
echo "$0 <PATH TO source-stemcell.tgz>" | |
echo "----------------------------------------------------" | |
exit | |
fi | |
# If we dont source the openstackRC none of the openstack CLI commands will login | |
echo "Installing qemu-tools" | |
apt-get install -y qemu-utils | |
echo "Checking for qemu-img" | |
command -v qemu-img >/dev/null 2>&1 || { echo "Cannot run qemu-img. Exiting " >&2; exit 1; } | |
echo ".... Good" | |
# Create Environment | |
echo "Creating work dir.." | |
mkdir ${WORKDIR} | |
cd ${WORKDIR} && WORKPATH=`pwd` | |
echo "We will work in: $WORKPATH " | |
# Unpack Stemcell | |
echo "Unpacking Stemcell" | |
tar -xzvf $1 -C $WORKPATH | |
ORIGSHA=`sha1sum ${WORKPATH}/image | awk '{print $1}'` | |
echo "Original SHA1 of compressed image: $ORIGSHA " | |
# Unpack root image | |
echo "Unpacking Root Image" | |
tar -xzvf $WORKPATH/image -C $WORKPATH && rm $WORKPATH/image | |
# Convert | |
echo "Converting root image to RAW" | |
qemu-img convert -f qcow2 -O raw $WORKPATH/root.img $WORKPATH/root.img.raw && rm $WORKPATH/root.img && mv $WORKPATH/root.img.raw $WORKPATH/root.img | |
# Compress converted image | |
echo "Re-Compressing root Image" | |
cd $WORKPATH && tar -czvf image root.img | |
# Get SHA1 of new image | |
NEWSHA=`sha1sum ${WORKPATH}/image | awk '{print $1}'` | |
echo "New SHA for our root image is: $NEWSHA old SHA was: $ORIGSHA" | |
# Modify stemcell manifest | |
echo "Changing SHA in stemcell manifest" | |
sed -e s/$ORIGSHA/$NEWSHA/g $WORKPATH/stemcell.MF > $WORKPATH/tmp.MF && mv $WORKPATH/tmp.MF $WORKPATH/stemcell.MF | |
# Change Stemcell name in manifest and disk format | |
echo "Editing other Stemcell Manifest Metadata" | |
sed -e s/qcow2/raw/g $WORKPATH/stemcell.MF > $WORKPATH/tmp.MF && mv $WORKPATH/tmp.MF $WORKPATH/stemcell.MF | |
STEMNAME=`grep name "$WORKPATH/stemcell.MF" | head -n 1 | awk '{print $2}' ` | |
NEWSTEMNAME=$STEMNAME-ft | |
sed -e s/$STEMNAME/$NEWSTEMNAME/g $WORKPATH/stemcell.MF > $WORKPATH/tmp.MF && mv $WORKPATH/tmp.MF $WORKPATH/stemcell.MF | |
NEWFILENAME="bosh-stemcell-`grep version stemcell.MF | head -n 1 | awk -F"'" '{print $2}'`-` echo "bosh-openstack-kvm-ubuntu" | awk -F"bosh-" '{print $2}'`-ft" | |
echo "Our NEW Manifest:" | |
cat $WORKPATH/stemcell.MF | |
echo "Repacking..." | |
cd $WORKPATH && tar -czvf $NEWFILENAME.tgz apply_spec.yml stemcell.MF image | |
echo "Done. New file should be: $WORKPATH/$NEWFILENAME.tgz" | |
file $WORKPATH/$NEWFILENAME.tgz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment