blob: cb59b9aa4d66447f8a5ab46152f56299ad88a76e [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
Dean Troyer55c02732011-11-01 17:44:03 -050029# Clean up any resources that may be in use
30cleanup() {
31 set +o errexit
32
33 # Mop up temporary files
34 if [ -n "$IMG_FILE_TMP" -a -e "$IMG_FILE_TMP" ]; then
35 rm -f $IMG_FILE_TMP
36 fi
37
38 # Release NBD devices
39 if [ -n "$NBD" ]; then
40 qemu-nbd -d $NBD
41 fi
42
43 # Kill ourselves to signal any calling process
44 trap 2; kill -2 $$
45}
46
Dean Troyere753fdf2011-10-25 15:45:26 -050047while getopts f:hmr: c; do
48 case $c in
49 f) FORMAT=$OPTARG
50 ;;
51 h) usage
52 ;;
53 m) MINIMAL=1
54 ;;
55 r) ROOTSIZE=$OPTARG
Jesse Andrews04156db2011-10-31 11:59:55 -070056 if [[ $ROOTSIZE < 2000 ]]; then
Dean Troyere753fdf2011-10-25 15:45:26 -050057 echo "root size must be greater than 2000MB"
58 exit 1
59 fi
60 ;;
61 esac
62done
63shift `expr $OPTIND - 1`
64
65if [ ! "$#" -eq "2" ]; then
66 usage
67fi
68
69# Default args
70DIST_NAME=$1
71IMG_FILE=$2
Dean Troyer71745fe2011-10-31 16:59:02 -050072IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX`
Dean Troyere753fdf2011-10-25 15:45:26 -050073
74case $FORMAT in
75 kvm|qcow2) FORMAT=qcow2
76 QFORMAT=qcow2
77 ;;
78 vmserver|vmdk)
79 FORMAT=vmdk
80 QFORMAT=vmdk
81 ;;
82 vbox|vdi) FORMAT=vdi
83 QFORMAT=vdi
84 ;;
85 vhd|vpc) FORMAT=vhd
86 QFORMAT=vpc
87 ;;
88 xen) FORMAT=raw
89 QFORMAT=raw
90 ;;
91 raw) FORMAT=raw
92 QFORMAT=raw
93 ;;
94 *) echo "Unknown format: $FORMAT"
95 usage
96esac
97
98case $DIST_NAME in
99 oneiric) ;;
100 natty) ;;
101 maverick) ;;
102 lucid) ;;
103 karmic) ;;
104 jaunty) ;;
105 *) echo "Unknown release: $DIST_NAME"
106 usage
107 ;;
108esac
109
Dean Troyer55c02732011-11-01 17:44:03 -0500110trap cleanup SIGHUP SIGINT SIGTERM
111
Dean Troyere753fdf2011-10-25 15:45:26 -0500112# Prepare the base image
113
114# Get the UEC image
115UEC_NAME=$DIST_NAME-server-cloudimg-amd64
116if [ ! -e $CACHEDIR/$UEC_NAME-disk1.img ]; then
Anthony Young23324e92011-11-03 13:47:15 -0700117 mkdir -p $CACHEDIR
Jesse Andrewsd6ee73f2011-10-30 19:40:38 -0700118 (cd $CACHEDIR && wget -N http://uec-images.ubuntu.com/$DIST_NAME/current/$UEC_NAME-disk1.img)
Dean Troyere753fdf2011-10-25 15:45:26 -0500119fi
120
121if [ "$FORMAT" = "qcow2" ]; then
122 # Just copy image
Dean Troyer71745fe2011-10-31 16:59:02 -0500123 cp -p $CACHEDIR/$UEC_NAME-disk1.img $IMG_FILE_TMP
Dean Troyere753fdf2011-10-25 15:45:26 -0500124else
125 # Convert image
Dean Troyer71745fe2011-10-31 16:59:02 -0500126 qemu-img convert -O $QFORMAT $CACHEDIR/$UEC_NAME-disk1.img $IMG_FILE_TMP
Dean Troyere753fdf2011-10-25 15:45:26 -0500127fi
128
129# Resize the image if necessary
130if [ $ROOTSIZE -gt 2000 ]; then
131 # Resize the container
Dean Troyer71745fe2011-10-31 16:59:02 -0500132 qemu-img resize $IMG_FILE_TMP +$((ROOTSIZE - 2000))M
Dean Troyerae605562011-10-26 12:35:22 -0500133fi
Dean Troyere753fdf2011-10-25 15:45:26 -0500134
Dean Troyerdccd6b92011-11-01 15:46:14 -0500135# Finds the next available NBD device
136# Exits script if error connecting or none free
137# map_nbd image
138# returns full nbd device path
139function map_nbd {
140 for i in `seq 0 15`; do
141 if [ ! -e /sys/block/nbd$i/pid ]; then
142 NBD=/dev/nbd$i
143 # Connect to nbd and wait till it is ready
144 qemu-nbd -c $NBD $1
145 if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
146 echo "Couldn't connect $NBD"
147 exit 1
148 fi
149 break
150 fi
151 done
152 if [ -z "$NBD" ]; then
153 echo "No free NBD slots"
154 exit 1
155 fi
156 echo $NBD
157}
158
Dean Troyer71745fe2011-10-31 16:59:02 -0500159# Set up nbd
160modprobe nbd max_part=63
Dean Troyerdccd6b92011-11-01 15:46:14 -0500161NBD=`map_nbd $IMG_FILE_TMP`
Dean Troyere753fdf2011-10-25 15:45:26 -0500162
Dean Troyerae605562011-10-26 12:35:22 -0500163# Resize partition 1 to full size of the disk image
164echo "d
Dean Troyere753fdf2011-10-25 15:45:26 -0500165n
166p
1671
1682
169
170t
17183
172a
1731
174w
175" | fdisk $NBD
Dean Troyer2c1a2d52011-10-31 15:32:54 -0500176e2fsck -f -p ${NBD}p1
Dean Troyerae605562011-10-26 12:35:22 -0500177resize2fs ${NBD}p1
Dean Troyere753fdf2011-10-25 15:45:26 -0500178
Dean Troyerc2ddc972011-10-31 15:04:27 -0500179# Do some preliminary installs
180MNTDIR=`mktemp -d mntXXXXXXXX`
181mount -t ext4 ${NBD}p1 $MNTDIR
182
183# Install our required packages
184cp -p files/sources.list $MNTDIR/etc/apt/sources.list
Dean Troyer2dfdbc92011-10-31 15:46:49 -0500185sed -e "s,%DIST%,$DIST_NAME,g" -i $MNTDIR/etc/apt/sources.list
Dean Troyerc2ddc972011-10-31 15:04:27 -0500186cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
187chroot $MNTDIR apt-get update
188chroot $MNTDIR apt-get install -y $MIN_PKGS
189rm -f $MNTDIR/etc/resolv.conf
190
191umount $MNTDIR
192rmdir $MNTDIR
Dean Troyerae605562011-10-26 12:35:22 -0500193qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500194NBD=""
Dean Troyer71745fe2011-10-31 16:59:02 -0500195
196mv $IMG_FILE_TMP $IMG_FILE