blob: f66f2bc2fb6d29503b0754921bb5f67b304a1163 [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)
9TOP_DIR=`cd $TOOLS_DIR/..; pwd`
10
Dean Troyer43392f72011-11-05 16:55:15 -050011# exit on error to stop unexpected errors
12set -o errexit
Jesse Andrews9c7c9082011-11-23 10:10:53 -080013set -o xtrace
Dean Troyer43392f72011-11-05 16:55:15 -050014
Dean Troyere753fdf2011-10-25 15:45:26 -050015usage() {
Jesse Andrewsd7326d22011-11-20 10:02:26 -080016 echo "Usage: $0 - Fetch and prepare Ubuntu images"
Dean Troyere753fdf2011-10-25 15:45:26 -050017 echo ""
Jesse Andrewsd7326d22011-11-20 10:02:26 -080018 echo "$0 [-r rootsize] release imagefile [kernel]"
Dean Troyere753fdf2011-10-25 15:45:26 -050019 echo ""
Jesse Andrewsd7326d22011-11-20 10:02:26 -080020 echo "-r size - root fs size (min 2000MB)"
Dean Troyere753fdf2011-10-25 15:45:26 -050021 echo "release - Ubuntu release: jaunty - oneric"
Dean Troyera03b99d2011-10-25 16:28:49 -050022 echo "imagefile - output image file"
Jesse Andrewsd7326d22011-11-20 10:02:26 -080023 echo "kernel - output kernel"
Dean Troyere753fdf2011-10-25 15:45:26 -050024 exit 1
25}
26
Dean Troyer55c02732011-11-01 17:44:03 -050027# Clean up any resources that may be in use
28cleanup() {
29 set +o errexit
30
31 # Mop up temporary files
32 if [ -n "$IMG_FILE_TMP" -a -e "$IMG_FILE_TMP" ]; then
33 rm -f $IMG_FILE_TMP
34 fi
35
Dean Troyer55c02732011-11-01 17:44:03 -050036 # Kill ourselves to signal any calling process
37 trap 2; kill -2 $$
38}
39
Jesse Andrewsd7326d22011-11-20 10:02:26 -080040while getopts hr: c; do
Dean Troyere753fdf2011-10-25 15:45:26 -050041 case $c in
Dean Troyere753fdf2011-10-25 15:45:26 -050042 h) usage
43 ;;
Dean Troyere753fdf2011-10-25 15:45:26 -050044 r) ROOTSIZE=$OPTARG
Dean Troyere753fdf2011-10-25 15:45:26 -050045 ;;
46 esac
47done
48shift `expr $OPTIND - 1`
49
Jesse Andrewsd7326d22011-11-20 10:02:26 -080050if [[ ! "$#" -eq "2" && ! "$#" -eq "3" ]]; then
Dean Troyere753fdf2011-10-25 15:45:26 -050051 usage
52fi
53
54# Default args
55DIST_NAME=$1
56IMG_FILE=$2
Dean Troyer71745fe2011-10-31 16:59:02 -050057IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX`
Jesse Andrewsd7326d22011-11-20 10:02:26 -080058KERNEL=$3
Dean Troyere753fdf2011-10-25 15:45:26 -050059
60case $DIST_NAME in
61 oneiric) ;;
62 natty) ;;
63 maverick) ;;
64 lucid) ;;
Dean Troyere753fdf2011-10-25 15:45:26 -050065 *) echo "Unknown release: $DIST_NAME"
66 usage
67 ;;
68esac
69
Jesse Andrewsd7326d22011-11-20 10:02:26 -080070trap cleanup SIGHUP SIGINT SIGTERM SIGQUIT EXIT
Dean Troyer43392f72011-11-05 16:55:15 -050071
Jesse Andrewsd7326d22011-11-20 10:02:26 -080072# Check dependencies
73if [ ! -x "`which qemu-img`" -o -z "`dpkg -l | grep cloud-utils`" ]; then
Dean Troyer43392f72011-11-05 16:55:15 -050074 # Missing KVM?
Jesse Andrewsd7326d22011-11-20 10:02:26 -080075 apt_get install qemu-kvm cloud-utils
Dean Troyer43392f72011-11-05 16:55:15 -050076fi
Dean Troyer55c02732011-11-01 17:44:03 -050077
Jesse Andrewsd7326d22011-11-20 10:02:26 -080078# Find resize script
79RESIZE=`which resize-part-image || which uec-resize-image`
80if [ -z "$RESIZE" ]; then
81 echo "resize tool from cloud-utils not found"
82 exit 1
83fi
Dean Troyere753fdf2011-10-25 15:45:26 -050084
85# Get the UEC image
86UEC_NAME=$DIST_NAME-server-cloudimg-amd64
Jesse Andrewsd7326d22011-11-20 10:02:26 -080087if [ ! -d $CACHEDIR ]; then
88 mkdir -p $CACHEDIR/$DIST_NAME
89fi
90if [ ! -e $CACHEDIR/$DIST_NAME/$UEC_NAME.tar.gz ]; then
91 (cd $CACHEDIR/$DIST_NAME && wget -N http://uec-images.ubuntu.com/$DIST_NAME/current/$UEC_NAME.tar.gz)
92 (cd $CACHEDIR/$DIST_NAME && tar Sxvzf $UEC_NAME.tar.gz)
Dean Troyere753fdf2011-10-25 15:45:26 -050093fi
94
Jesse Andrewsd7326d22011-11-20 10:02:26 -080095$RESIZE $CACHEDIR/$DIST_NAME/$UEC_NAME.img ${ROOTSIZE} $IMG_FILE_TMP
Dean Troyer71745fe2011-10-31 16:59:02 -050096mv $IMG_FILE_TMP $IMG_FILE
Jesse Andrewsd7326d22011-11-20 10:02:26 -080097
98# Copy kernel to destination
99if [ -n "$KERNEL" ]; then
100 cp -p $CACHEDIR/$DIST_NAME/*-vmlinuz-virtual $KERNEL
101fi
102
103trap - SIGHUP SIGINT SIGTERM SIGQUIT EXIT