Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 1 | #!/bin/bash |
Dean Troyer | e62ba4d | 2012-06-27 22:07:34 -0500 | [diff] [blame] | 2 | |
| 3 | # **get_uec_image.sh** |
| 4 | |
| 5 | # Download and prepare Ubuntu UEC images |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 6 | |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 7 | CACHEDIR=${CACHEDIR:-/opt/stack/cache} |
dmitriybudnik | 0c49539 | 2012-10-21 02:00:07 +0300 | [diff] [blame] | 8 | ROOTSIZE=${ROOTSIZE:-2000M} |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 9 | |
Jesse Andrews | 04156db | 2011-10-31 11:59:55 -0700 | [diff] [blame] | 10 | # Keep track of the current directory |
| 11 | TOOLS_DIR=$(cd $(dirname "$0") && pwd) |
Dean Troyer | 7f9aa71 | 2012-01-31 12:11:56 -0600 | [diff] [blame] | 12 | TOP_DIR=$(cd $TOOLS_DIR/..; pwd) |
| 13 | |
| 14 | # Import common functions |
| 15 | . $TOP_DIR/functions |
Jesse Andrews | 04156db | 2011-10-31 11:59:55 -0700 | [diff] [blame] | 16 | |
Dean Troyer | e62ba4d | 2012-06-27 22:07:34 -0500 | [diff] [blame] | 17 | # Exit on error to stop unexpected errors |
Dean Troyer | 43392f7 | 2011-11-05 16:55:15 -0500 | [diff] [blame] | 18 | set -o errexit |
Jesse Andrews | 9c7c908 | 2011-11-23 10:10:53 -0800 | [diff] [blame] | 19 | set -o xtrace |
Dean Troyer | 43392f7 | 2011-11-05 16:55:15 -0500 | [diff] [blame] | 20 | |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 21 | usage() { |
Dean Troyer | e62ba4d | 2012-06-27 22:07:34 -0500 | [diff] [blame] | 22 | echo "Usage: $0 - Download and prepare Ubuntu UEC images" |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 23 | echo "" |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 24 | echo "$0 [-r rootsize] release imagefile [kernel]" |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 25 | echo "" |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 26 | echo "-r size - root fs size (min 2000MB)" |
dmitriybudnik | 0c49539 | 2012-10-21 02:00:07 +0300 | [diff] [blame] | 27 | echo "release - Ubuntu release: lucid - quantal" |
Dean Troyer | a03b99d | 2011-10-25 16:28:49 -0500 | [diff] [blame] | 28 | echo "imagefile - output image file" |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 29 | echo "kernel - output kernel" |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 30 | exit 1 |
| 31 | } |
| 32 | |
Dean Troyer | 55c0273 | 2011-11-01 17:44:03 -0500 | [diff] [blame] | 33 | # Clean up any resources that may be in use |
| 34 | cleanup() { |
| 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 Troyer | 55c0273 | 2011-11-01 17:44:03 -0500 | [diff] [blame] | 42 | # Kill ourselves to signal any calling process |
| 43 | trap 2; kill -2 $$ |
| 44 | } |
| 45 | |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 46 | while getopts hr: c; do |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 47 | case $c in |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 48 | h) usage |
| 49 | ;; |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 50 | r) ROOTSIZE=$OPTARG |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 51 | ;; |
| 52 | esac |
| 53 | done |
| 54 | shift `expr $OPTIND - 1` |
| 55 | |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 56 | if [[ ! "$#" -eq "2" && ! "$#" -eq "3" ]]; then |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 57 | usage |
| 58 | fi |
| 59 | |
| 60 | # Default args |
| 61 | DIST_NAME=$1 |
| 62 | IMG_FILE=$2 |
Dean Troyer | 71745fe | 2011-10-31 16:59:02 -0500 | [diff] [blame] | 63 | IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX` |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 64 | KERNEL=$3 |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 65 | |
| 66 | case $DIST_NAME in |
dmitriybudnik | 0c49539 | 2012-10-21 02:00:07 +0300 | [diff] [blame] | 67 | quantal) ;; |
Sean Dague | 93923eb | 2012-11-30 17:51:12 -0500 | [diff] [blame] | 68 | precise) ;; |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 69 | oneiric) ;; |
| 70 | natty) ;; |
| 71 | maverick) ;; |
| 72 | lucid) ;; |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 73 | *) echo "Unknown release: $DIST_NAME" |
| 74 | usage |
| 75 | ;; |
| 76 | esac |
| 77 | |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 78 | trap cleanup SIGHUP SIGINT SIGTERM SIGQUIT EXIT |
Dean Troyer | 43392f7 | 2011-11-05 16:55:15 -0500 | [diff] [blame] | 79 | |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 80 | # Check dependencies |
| 81 | if [ ! -x "`which qemu-img`" -o -z "`dpkg -l | grep cloud-utils`" ]; then |
Dean Troyer | 43392f7 | 2011-11-05 16:55:15 -0500 | [diff] [blame] | 82 | # Missing KVM? |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 83 | apt_get install qemu-kvm cloud-utils |
Dean Troyer | 43392f7 | 2011-11-05 16:55:15 -0500 | [diff] [blame] | 84 | fi |
Dean Troyer | 55c0273 | 2011-11-01 17:44:03 -0500 | [diff] [blame] | 85 | |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 86 | # Find resize script |
| 87 | RESIZE=`which resize-part-image || which uec-resize-image` |
| 88 | if [ -z "$RESIZE" ]; then |
| 89 | echo "resize tool from cloud-utils not found" |
| 90 | exit 1 |
| 91 | fi |
Dean Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 92 | |
| 93 | # Get the UEC image |
| 94 | UEC_NAME=$DIST_NAME-server-cloudimg-amd64 |
dmitriybudnik | 0c49539 | 2012-10-21 02:00:07 +0300 | [diff] [blame] | 95 | if [ ! -d $CACHEDIR/$DIST_NAME ]; then |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 96 | mkdir -p $CACHEDIR/$DIST_NAME |
| 97 | fi |
| 98 | if [ ! -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 Troyer | e753fdf | 2011-10-25 15:45:26 -0500 | [diff] [blame] | 101 | fi |
| 102 | |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 103 | $RESIZE $CACHEDIR/$DIST_NAME/$UEC_NAME.img ${ROOTSIZE} $IMG_FILE_TMP |
Dean Troyer | 71745fe | 2011-10-31 16:59:02 -0500 | [diff] [blame] | 104 | mv $IMG_FILE_TMP $IMG_FILE |
Jesse Andrews | d7326d2 | 2011-11-20 10:02:26 -0800 | [diff] [blame] | 105 | |
| 106 | # Copy kernel to destination |
| 107 | if [ -n "$KERNEL" ]; then |
| 108 | cp -p $CACHEDIR/$DIST_NAME/*-vmlinuz-virtual $KERNEL |
| 109 | fi |
| 110 | |
| 111 | trap - SIGHUP SIGINT SIGTERM SIGQUIT EXIT |