blob: ca74a030bd082628bc017091a2d6242bc3877e3a [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}
Dean Troyere753fdf2011-10-25 15:45:26 -05008ROOTSIZE=${ROOTSIZE:-2000}
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)"
Dean Troyere753fdf2011-10-25 15:45:26 -050027 echo "release - Ubuntu release: jaunty - oneric"
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
67 oneiric) ;;
68 natty) ;;
69 maverick) ;;
70 lucid) ;;
Dean Troyere753fdf2011-10-25 15:45:26 -050071 *) echo "Unknown release: $DIST_NAME"
72 usage
73 ;;
74esac
75
Jesse Andrewsd7326d22011-11-20 10:02:26 -080076trap cleanup SIGHUP SIGINT SIGTERM SIGQUIT EXIT
Dean Troyer43392f72011-11-05 16:55:15 -050077
Jesse Andrewsd7326d22011-11-20 10:02:26 -080078# Check dependencies
79if [ ! -x "`which qemu-img`" -o -z "`dpkg -l | grep cloud-utils`" ]; then
Dean Troyer43392f72011-11-05 16:55:15 -050080 # Missing KVM?
Jesse Andrewsd7326d22011-11-20 10:02:26 -080081 apt_get install qemu-kvm cloud-utils
Dean Troyer43392f72011-11-05 16:55:15 -050082fi
Dean Troyer55c02732011-11-01 17:44:03 -050083
Jesse Andrewsd7326d22011-11-20 10:02:26 -080084# Find resize script
85RESIZE=`which resize-part-image || which uec-resize-image`
86if [ -z "$RESIZE" ]; then
87 echo "resize tool from cloud-utils not found"
88 exit 1
89fi
Dean Troyere753fdf2011-10-25 15:45:26 -050090
91# Get the UEC image
92UEC_NAME=$DIST_NAME-server-cloudimg-amd64
Jesse Andrewsd7326d22011-11-20 10:02:26 -080093if [ ! -d $CACHEDIR ]; then
94 mkdir -p $CACHEDIR/$DIST_NAME
95fi
96if [ ! -e $CACHEDIR/$DIST_NAME/$UEC_NAME.tar.gz ]; then
97 (cd $CACHEDIR/$DIST_NAME && wget -N http://uec-images.ubuntu.com/$DIST_NAME/current/$UEC_NAME.tar.gz)
98 (cd $CACHEDIR/$DIST_NAME && tar Sxvzf $UEC_NAME.tar.gz)
Dean Troyere753fdf2011-10-25 15:45:26 -050099fi
100
Jesse Andrewsd7326d22011-11-20 10:02:26 -0800101$RESIZE $CACHEDIR/$DIST_NAME/$UEC_NAME.img ${ROOTSIZE} $IMG_FILE_TMP
Dean Troyer71745fe2011-10-31 16:59:02 -0500102mv $IMG_FILE_TMP $IMG_FILE
Jesse Andrewsd7326d22011-11-20 10:02:26 -0800103
104# Copy kernel to destination
105if [ -n "$KERNEL" ]; then
106 cp -p $CACHEDIR/$DIST_NAME/*-vmlinuz-virtual $KERNEL
107fi
108
109trap - SIGHUP SIGINT SIGTERM SIGQUIT EXIT