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