blob: 09630740dcdd6a2eecc1fe3e6f98283f8e5d3462 [file] [log] [blame]
Dean Troyere753fdf2011-10-25 15:45:26 -05001#!/bin/bash
Jesse Andrewsd7326d22011-11-20 10:02:26 -08002# get_uec_image.sh - Prepare Ubuntu UEC images
Dean Troyere753fdf2011-10-25 15:45:26 -05003
Jesse Andrewsd7326d22011-11-20 10:02:26 -08004CACHEDIR=${CACHEDIR:-/opt/stack/cache}
Dean Troyere753fdf2011-10-25 15:45:26 -05005ROOTSIZE=${ROOTSIZE:-2000}
Dean Troyere753fdf2011-10-25 15:45:26 -05006
Jesse Andrews04156db2011-10-31 11:59:55 -07007# Keep track of the current directory
8TOOLS_DIR=$(cd $(dirname "$0") && pwd)
Dean Troyer7f9aa712012-01-31 12:11:56 -06009TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
10
11# Import common functions
12. $TOP_DIR/functions
Jesse Andrews04156db2011-10-31 11:59:55 -070013
Dean Troyer43392f72011-11-05 16:55:15 -050014# exit on error to stop unexpected errors
15set -o errexit
Jesse Andrews9c7c9082011-11-23 10:10:53 -080016set -o xtrace
Dean Troyer43392f72011-11-05 16:55:15 -050017
Dean Troyere753fdf2011-10-25 15:45:26 -050018usage() {
Jesse Andrewsd7326d22011-11-20 10:02:26 -080019 echo "Usage: $0 - Fetch and prepare Ubuntu images"
Dean Troyere753fdf2011-10-25 15:45:26 -050020 echo ""
Jesse Andrewsd7326d22011-11-20 10:02:26 -080021 echo "$0 [-r rootsize] release imagefile [kernel]"
Dean Troyere753fdf2011-10-25 15:45:26 -050022 echo ""
Jesse Andrewsd7326d22011-11-20 10:02:26 -080023 echo "-r size - root fs size (min 2000MB)"
Dean Troyere753fdf2011-10-25 15:45:26 -050024 echo "release - Ubuntu release: jaunty - oneric"
Dean Troyera03b99d2011-10-25 16:28:49 -050025 echo "imagefile - output image file"
Jesse Andrewsd7326d22011-11-20 10:02:26 -080026 echo "kernel - output kernel"
Dean Troyere753fdf2011-10-25 15:45:26 -050027 exit 1
28}
29
Dean Troyer55c02732011-11-01 17:44:03 -050030# Clean up any resources that may be in use
31cleanup() {
32 set +o errexit
33
34 # Mop up temporary files
35 if [ -n "$IMG_FILE_TMP" -a -e "$IMG_FILE_TMP" ]; then
36 rm -f $IMG_FILE_TMP
37 fi
38
Dean Troyer55c02732011-11-01 17:44:03 -050039 # Kill ourselves to signal any calling process
40 trap 2; kill -2 $$
41}
42
Jesse Andrewsd7326d22011-11-20 10:02:26 -080043while getopts hr: c; do
Dean Troyere753fdf2011-10-25 15:45:26 -050044 case $c in
Dean Troyere753fdf2011-10-25 15:45:26 -050045 h) usage
46 ;;
Dean Troyere753fdf2011-10-25 15:45:26 -050047 r) ROOTSIZE=$OPTARG
Dean Troyere753fdf2011-10-25 15:45:26 -050048 ;;
49 esac
50done
51shift `expr $OPTIND - 1`
52
Jesse Andrewsd7326d22011-11-20 10:02:26 -080053if [[ ! "$#" -eq "2" && ! "$#" -eq "3" ]]; then
Dean Troyere753fdf2011-10-25 15:45:26 -050054 usage
55fi
56
57# Default args
58DIST_NAME=$1
59IMG_FILE=$2
Dean Troyer71745fe2011-10-31 16:59:02 -050060IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX`
Jesse Andrewsd7326d22011-11-20 10:02:26 -080061KERNEL=$3
Dean Troyere753fdf2011-10-25 15:45:26 -050062
63case $DIST_NAME in
64 oneiric) ;;
65 natty) ;;
66 maverick) ;;
67 lucid) ;;
Dean Troyere753fdf2011-10-25 15:45:26 -050068 *) echo "Unknown release: $DIST_NAME"
69 usage
70 ;;
71esac
72
Jesse Andrewsd7326d22011-11-20 10:02:26 -080073trap cleanup SIGHUP SIGINT SIGTERM SIGQUIT EXIT
Dean Troyer43392f72011-11-05 16:55:15 -050074
Jesse Andrewsd7326d22011-11-20 10:02:26 -080075# Check dependencies
76if [ ! -x "`which qemu-img`" -o -z "`dpkg -l | grep cloud-utils`" ]; then
Dean Troyer43392f72011-11-05 16:55:15 -050077 # Missing KVM?
Jesse Andrewsd7326d22011-11-20 10:02:26 -080078 apt_get install qemu-kvm cloud-utils
Dean Troyer43392f72011-11-05 16:55:15 -050079fi
Dean Troyer55c02732011-11-01 17:44:03 -050080
Jesse Andrewsd7326d22011-11-20 10:02:26 -080081# Find resize script
82RESIZE=`which resize-part-image || which uec-resize-image`
83if [ -z "$RESIZE" ]; then
84 echo "resize tool from cloud-utils not found"
85 exit 1
86fi
Dean Troyere753fdf2011-10-25 15:45:26 -050087
88# Get the UEC image
89UEC_NAME=$DIST_NAME-server-cloudimg-amd64
Jesse Andrewsd7326d22011-11-20 10:02:26 -080090if [ ! -d $CACHEDIR ]; then
91 mkdir -p $CACHEDIR/$DIST_NAME
92fi
93if [ ! -e $CACHEDIR/$DIST_NAME/$UEC_NAME.tar.gz ]; then
94 (cd $CACHEDIR/$DIST_NAME && wget -N http://uec-images.ubuntu.com/$DIST_NAME/current/$UEC_NAME.tar.gz)
95 (cd $CACHEDIR/$DIST_NAME && tar Sxvzf $UEC_NAME.tar.gz)
Dean Troyere753fdf2011-10-25 15:45:26 -050096fi
97
Jesse Andrewsd7326d22011-11-20 10:02:26 -080098$RESIZE $CACHEDIR/$DIST_NAME/$UEC_NAME.img ${ROOTSIZE} $IMG_FILE_TMP
Dean Troyer71745fe2011-10-31 16:59:02 -050099mv $IMG_FILE_TMP $IMG_FILE
Jesse Andrewsd7326d22011-11-20 10:02:26 -0800100
101# Copy kernel to destination
102if [ -n "$KERNEL" ]; then
103 cp -p $CACHEDIR/$DIST_NAME/*-vmlinuz-virtual $KERNEL
104fi
105
106trap - SIGHUP SIGINT SIGTERM SIGQUIT EXIT