blob: 3c62064ad68ef377e02ed725427fec479b26935a [file] [log] [blame]
Dean Troyere753fdf2011-10-25 15:45:26 -05001#!/bin/bash
Dean Troyere62ba4d2012-06-27 22:07:34 -05002
3# **get_uec_image.sh**
4
5# Download and prepare Ubuntu UEC images
Dean Troyere753fdf2011-10-25 15:45:26 -05006
Jesse Andrewsd7326d22011-11-20 10:02:26 -08007CACHEDIR=${CACHEDIR:-/opt/stack/cache}
dmitriybudnik0c495392012-10-21 02:00:07 +03008ROOTSIZE=${ROOTSIZE:-2000M}
Dean Troyere753fdf2011-10-25 15:45:26 -05009
Jesse Andrews04156db2011-10-31 11:59:55 -070010# Keep track of the current directory
11TOOLS_DIR=$(cd $(dirname "$0") && pwd)
Dean Troyer7f9aa712012-01-31 12:11:56 -060012TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
13
14# Import common functions
15. $TOP_DIR/functions
Jesse Andrews04156db2011-10-31 11:59:55 -070016
Dean Troyere62ba4d2012-06-27 22:07:34 -050017# Exit on error to stop unexpected errors
Dean Troyer43392f72011-11-05 16:55:15 -050018set -o errexit
Jesse Andrews9c7c9082011-11-23 10:10:53 -080019set -o xtrace
Dean Troyer43392f72011-11-05 16:55:15 -050020
Dean Troyere753fdf2011-10-25 15:45:26 -050021usage() {
Dean Troyere62ba4d2012-06-27 22:07:34 -050022 echo "Usage: $0 - Download and prepare Ubuntu UEC images"
Dean Troyere753fdf2011-10-25 15:45:26 -050023 echo ""
Jesse Andrewsd7326d22011-11-20 10:02:26 -080024 echo "$0 [-r rootsize] release imagefile [kernel]"
Dean Troyere753fdf2011-10-25 15:45:26 -050025 echo ""
Jesse Andrewsd7326d22011-11-20 10:02:26 -080026 echo "-r size - root fs size (min 2000MB)"
dmitriybudnik0c495392012-10-21 02:00:07 +030027 echo "release - Ubuntu release: lucid - quantal"
Dean Troyera03b99d2011-10-25 16:28:49 -050028 echo "imagefile - output image file"
Jesse Andrewsd7326d22011-11-20 10:02:26 -080029 echo "kernel - output kernel"
Dean Troyere753fdf2011-10-25 15:45:26 -050030 exit 1
31}
32
Dean Troyer55c02732011-11-01 17:44:03 -050033# Clean up any resources that may be in use
34cleanup() {
35 set +o errexit
36
37 # Mop up temporary files
38 if [ -n "$IMG_FILE_TMP" -a -e "$IMG_FILE_TMP" ]; then
39 rm -f $IMG_FILE_TMP
40 fi
41
Dean Troyer55c02732011-11-01 17:44:03 -050042 # Kill ourselves to signal any calling process
43 trap 2; kill -2 $$
44}
45
Jesse Andrewsd7326d22011-11-20 10:02:26 -080046while getopts hr: c; do
Dean Troyere753fdf2011-10-25 15:45:26 -050047 case $c in
Dean Troyere753fdf2011-10-25 15:45:26 -050048 h) usage
49 ;;
Dean Troyere753fdf2011-10-25 15:45:26 -050050 r) ROOTSIZE=$OPTARG
Dean Troyere753fdf2011-10-25 15:45:26 -050051 ;;
52 esac
53done
54shift `expr $OPTIND - 1`
55
Jesse Andrewsd7326d22011-11-20 10:02:26 -080056if [[ ! "$#" -eq "2" && ! "$#" -eq "3" ]]; then
Dean Troyere753fdf2011-10-25 15:45:26 -050057 usage
58fi
59
60# Default args
61DIST_NAME=$1
62IMG_FILE=$2
Dean Troyer71745fe2011-10-31 16:59:02 -050063IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX`
Jesse Andrewsd7326d22011-11-20 10:02:26 -080064KERNEL=$3
Dean Troyere753fdf2011-10-25 15:45:26 -050065
66case $DIST_NAME in
dmitriybudnik0c495392012-10-21 02:00:07 +030067 quantal) ;;
Sean Dague93923eb2012-11-30 17:51:12 -050068 precise) ;;
Dean Troyere753fdf2011-10-25 15:45:26 -050069 oneiric) ;;
70 natty) ;;
71 maverick) ;;
72 lucid) ;;
Dean Troyere753fdf2011-10-25 15:45:26 -050073 *) echo "Unknown release: $DIST_NAME"
74 usage
75 ;;
76esac
77
Jesse Andrewsd7326d22011-11-20 10:02:26 -080078trap cleanup SIGHUP SIGINT SIGTERM SIGQUIT EXIT
Dean Troyer43392f72011-11-05 16:55:15 -050079
Jesse Andrewsd7326d22011-11-20 10:02:26 -080080# Check dependencies
81if [ ! -x "`which qemu-img`" -o -z "`dpkg -l | grep cloud-utils`" ]; then
Dean Troyer43392f72011-11-05 16:55:15 -050082 # Missing KVM?
Jesse Andrewsd7326d22011-11-20 10:02:26 -080083 apt_get install qemu-kvm cloud-utils
Dean Troyer43392f72011-11-05 16:55:15 -050084fi
Dean Troyer55c02732011-11-01 17:44:03 -050085
Jesse Andrewsd7326d22011-11-20 10:02:26 -080086# Find resize script
87RESIZE=`which resize-part-image || which uec-resize-image`
88if [ -z "$RESIZE" ]; then
89 echo "resize tool from cloud-utils not found"
90 exit 1
91fi
Dean Troyere753fdf2011-10-25 15:45:26 -050092
93# Get the UEC image
94UEC_NAME=$DIST_NAME-server-cloudimg-amd64
dmitriybudnik0c495392012-10-21 02:00:07 +030095if [ ! -d $CACHEDIR/$DIST_NAME ]; then
Jesse Andrewsd7326d22011-11-20 10:02:26 -080096 mkdir -p $CACHEDIR/$DIST_NAME
97fi
98if [ ! -e $CACHEDIR/$DIST_NAME/$UEC_NAME.tar.gz ]; then
99 (cd $CACHEDIR/$DIST_NAME && wget -N http://uec-images.ubuntu.com/$DIST_NAME/current/$UEC_NAME.tar.gz)
100 (cd $CACHEDIR/$DIST_NAME && tar Sxvzf $UEC_NAME.tar.gz)
Dean Troyere753fdf2011-10-25 15:45:26 -0500101fi
102
Jesse Andrewsd7326d22011-11-20 10:02:26 -0800103$RESIZE $CACHEDIR/$DIST_NAME/$UEC_NAME.img ${ROOTSIZE} $IMG_FILE_TMP
Dean Troyer71745fe2011-10-31 16:59:02 -0500104mv $IMG_FILE_TMP $IMG_FILE
Jesse Andrewsd7326d22011-11-20 10:02:26 -0800105
106# Copy kernel to destination
107if [ -n "$KERNEL" ]; then
108 cp -p $CACHEDIR/$DIST_NAME/*-vmlinuz-virtual $KERNEL
109fi
110
111trap - SIGHUP SIGINT SIGTERM SIGQUIT EXIT