blob: de378422a3aaf564ffc377bf7b98d2b41ccd5e79 [file] [log] [blame]
Dean Troyere753fdf2011-10-25 15:45:26 -05001#!/bin/bash
2# get_uec_image.sh - Prepare Ubuntu images in various formats
3#
4# Supported formats: qcow (kvm), vmdk (vmserver), vdi (vbox), vhd (vpc), raw
5#
Dean Troyera03b99d2011-10-25 16:28:49 -05006# Required to run as root
Dean Troyere753fdf2011-10-25 15:45:26 -05007
8CACHEDIR=${CACHEDIR:-/var/cache/devstack}
9FORMAT=${FORMAT:-qcow2}
10ROOTSIZE=${ROOTSIZE:-2000}
11MIN_PKGS=${MIN_PKGS:-"apt-utils gpgv openssh-server"}
12
Jesse Andrews04156db2011-10-31 11:59:55 -070013# Keep track of the current directory
14TOOLS_DIR=$(cd $(dirname "$0") && pwd)
15TOP_DIR=`cd $TOOLS_DIR/..; pwd`
16
Dean Troyere753fdf2011-10-25 15:45:26 -050017usage() {
18 echo "Usage: $0 - Prepare Ubuntu images"
19 echo ""
20 echo "$0 [-f format] [-r rootsize] release imagefile"
21 echo ""
22 echo "-f format - image format: qcow2 (default), vmdk, vdi, vhd, xen, raw, fs"
23 echo "-r size - root fs size in MB (min 2000MB)"
24 echo "release - Ubuntu release: jaunty - oneric"
Dean Troyera03b99d2011-10-25 16:28:49 -050025 echo "imagefile - output image file"
Dean Troyere753fdf2011-10-25 15:45:26 -050026 exit 1
27}
28
29while getopts f:hmr: c; do
30 case $c in
31 f) FORMAT=$OPTARG
32 ;;
33 h) usage
34 ;;
35 m) MINIMAL=1
36 ;;
37 r) ROOTSIZE=$OPTARG
Jesse Andrews04156db2011-10-31 11:59:55 -070038 if [[ $ROOTSIZE < 2000 ]]; then
Dean Troyere753fdf2011-10-25 15:45:26 -050039 echo "root size must be greater than 2000MB"
40 exit 1
41 fi
42 ;;
43 esac
44done
45shift `expr $OPTIND - 1`
46
47if [ ! "$#" -eq "2" ]; then
48 usage
49fi
50
51# Default args
52DIST_NAME=$1
53IMG_FILE=$2
Dean Troyer71745fe2011-10-31 16:59:02 -050054IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX`
Dean Troyere753fdf2011-10-25 15:45:26 -050055
56case $FORMAT in
57 kvm|qcow2) FORMAT=qcow2
58 QFORMAT=qcow2
59 ;;
60 vmserver|vmdk)
61 FORMAT=vmdk
62 QFORMAT=vmdk
63 ;;
64 vbox|vdi) FORMAT=vdi
65 QFORMAT=vdi
66 ;;
67 vhd|vpc) FORMAT=vhd
68 QFORMAT=vpc
69 ;;
70 xen) FORMAT=raw
71 QFORMAT=raw
72 ;;
73 raw) FORMAT=raw
74 QFORMAT=raw
75 ;;
76 *) echo "Unknown format: $FORMAT"
77 usage
78esac
79
80case $DIST_NAME in
81 oneiric) ;;
82 natty) ;;
83 maverick) ;;
84 lucid) ;;
85 karmic) ;;
86 jaunty) ;;
87 *) echo "Unknown release: $DIST_NAME"
88 usage
89 ;;
90esac
91
Dean Troyere753fdf2011-10-25 15:45:26 -050092# Prepare the base image
93
94# Get the UEC image
95UEC_NAME=$DIST_NAME-server-cloudimg-amd64
96if [ ! -e $CACHEDIR/$UEC_NAME-disk1.img ]; then
Jesse Andrewsd6ee73f2011-10-30 19:40:38 -070097 (cd $CACHEDIR && wget -N http://uec-images.ubuntu.com/$DIST_NAME/current/$UEC_NAME-disk1.img)
Dean Troyere753fdf2011-10-25 15:45:26 -050098fi
99
100if [ "$FORMAT" = "qcow2" ]; then
101 # Just copy image
Dean Troyer71745fe2011-10-31 16:59:02 -0500102 cp -p $CACHEDIR/$UEC_NAME-disk1.img $IMG_FILE_TMP
Dean Troyere753fdf2011-10-25 15:45:26 -0500103else
104 # Convert image
Dean Troyer71745fe2011-10-31 16:59:02 -0500105 qemu-img convert -O $QFORMAT $CACHEDIR/$UEC_NAME-disk1.img $IMG_FILE_TMP
Dean Troyere753fdf2011-10-25 15:45:26 -0500106fi
107
108# Resize the image if necessary
109if [ $ROOTSIZE -gt 2000 ]; then
110 # Resize the container
Dean Troyer71745fe2011-10-31 16:59:02 -0500111 qemu-img resize $IMG_FILE_TMP +$((ROOTSIZE - 2000))M
Dean Troyerae605562011-10-26 12:35:22 -0500112fi
Dean Troyere753fdf2011-10-25 15:45:26 -0500113
Dean Troyerdccd6b92011-11-01 15:46:14 -0500114# Finds the next available NBD device
115# Exits script if error connecting or none free
116# map_nbd image
117# returns full nbd device path
118function map_nbd {
119 for i in `seq 0 15`; do
120 if [ ! -e /sys/block/nbd$i/pid ]; then
121 NBD=/dev/nbd$i
122 # Connect to nbd and wait till it is ready
123 qemu-nbd -c $NBD $1
124 if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
125 echo "Couldn't connect $NBD"
126 exit 1
127 fi
128 break
129 fi
130 done
131 if [ -z "$NBD" ]; then
132 echo "No free NBD slots"
133 exit 1
134 fi
135 echo $NBD
136}
137
Dean Troyer71745fe2011-10-31 16:59:02 -0500138# Set up nbd
139modprobe nbd max_part=63
Dean Troyerdccd6b92011-11-01 15:46:14 -0500140NBD=`map_nbd $IMG_FILE_TMP`
Dean Troyere753fdf2011-10-25 15:45:26 -0500141
Dean Troyerae605562011-10-26 12:35:22 -0500142# Resize partition 1 to full size of the disk image
143echo "d
Dean Troyere753fdf2011-10-25 15:45:26 -0500144n
145p
1461
1472
148
149t
15083
151a
1521
153w
154" | fdisk $NBD
Dean Troyer2c1a2d52011-10-31 15:32:54 -0500155e2fsck -f -p ${NBD}p1
Dean Troyerae605562011-10-26 12:35:22 -0500156resize2fs ${NBD}p1
Dean Troyere753fdf2011-10-25 15:45:26 -0500157
Dean Troyerc2ddc972011-10-31 15:04:27 -0500158# Do some preliminary installs
159MNTDIR=`mktemp -d mntXXXXXXXX`
160mount -t ext4 ${NBD}p1 $MNTDIR
161
162# Install our required packages
163cp -p files/sources.list $MNTDIR/etc/apt/sources.list
Dean Troyer2dfdbc92011-10-31 15:46:49 -0500164sed -e "s,%DIST%,$DIST_NAME,g" -i $MNTDIR/etc/apt/sources.list
Dean Troyerc2ddc972011-10-31 15:04:27 -0500165cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
166chroot $MNTDIR apt-get update
167chroot $MNTDIR apt-get install -y $MIN_PKGS
168rm -f $MNTDIR/etc/resolv.conf
169
170umount $MNTDIR
171rmdir $MNTDIR
Dean Troyerae605562011-10-26 12:35:22 -0500172qemu-nbd -d $NBD
Dean Troyer71745fe2011-10-31 16:59:02 -0500173
174mv $IMG_FILE_TMP $IMG_FILE