blob: 3d62bbac7cb5b3e832149d51acd5fa9599dc6905 [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
Jesse Andrewsd6ee73f2011-10-30 19:40:38 -0700117 (cd $CACHEDIR && wget -N http://uec-images.ubuntu.com/$DIST_NAME/current/$UEC_NAME-disk1.img)
Dean Troyere753fdf2011-10-25 15:45:26 -0500118fi
119
120if [ "$FORMAT" = "qcow2" ]; then
121 # Just copy image
Dean Troyer71745fe2011-10-31 16:59:02 -0500122 cp -p $CACHEDIR/$UEC_NAME-disk1.img $IMG_FILE_TMP
Dean Troyere753fdf2011-10-25 15:45:26 -0500123else
124 # Convert image
Dean Troyer71745fe2011-10-31 16:59:02 -0500125 qemu-img convert -O $QFORMAT $CACHEDIR/$UEC_NAME-disk1.img $IMG_FILE_TMP
Dean Troyere753fdf2011-10-25 15:45:26 -0500126fi
127
128# Resize the image if necessary
129if [ $ROOTSIZE -gt 2000 ]; then
130 # Resize the container
Dean Troyer71745fe2011-10-31 16:59:02 -0500131 qemu-img resize $IMG_FILE_TMP +$((ROOTSIZE - 2000))M
Dean Troyerae605562011-10-26 12:35:22 -0500132fi
Dean Troyere753fdf2011-10-25 15:45:26 -0500133
Dean Troyerdccd6b92011-11-01 15:46:14 -0500134# Finds the next available NBD device
135# Exits script if error connecting or none free
136# map_nbd image
137# returns full nbd device path
138function map_nbd {
139 for i in `seq 0 15`; do
140 if [ ! -e /sys/block/nbd$i/pid ]; then
141 NBD=/dev/nbd$i
142 # Connect to nbd and wait till it is ready
143 qemu-nbd -c $NBD $1
144 if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
145 echo "Couldn't connect $NBD"
146 exit 1
147 fi
148 break
149 fi
150 done
151 if [ -z "$NBD" ]; then
152 echo "No free NBD slots"
153 exit 1
154 fi
155 echo $NBD
156}
157
Dean Troyer71745fe2011-10-31 16:59:02 -0500158# Set up nbd
159modprobe nbd max_part=63
Dean Troyerdccd6b92011-11-01 15:46:14 -0500160NBD=`map_nbd $IMG_FILE_TMP`
Dean Troyere753fdf2011-10-25 15:45:26 -0500161
Dean Troyerae605562011-10-26 12:35:22 -0500162# Resize partition 1 to full size of the disk image
163echo "d
Dean Troyere753fdf2011-10-25 15:45:26 -0500164n
165p
1661
1672
168
169t
17083
171a
1721
173w
174" | fdisk $NBD
Dean Troyer2c1a2d52011-10-31 15:32:54 -0500175e2fsck -f -p ${NBD}p1
Dean Troyerae605562011-10-26 12:35:22 -0500176resize2fs ${NBD}p1
Dean Troyere753fdf2011-10-25 15:45:26 -0500177
Dean Troyerc2ddc972011-10-31 15:04:27 -0500178# Do some preliminary installs
179MNTDIR=`mktemp -d mntXXXXXXXX`
180mount -t ext4 ${NBD}p1 $MNTDIR
181
182# Install our required packages
183cp -p files/sources.list $MNTDIR/etc/apt/sources.list
Dean Troyer2dfdbc92011-10-31 15:46:49 -0500184sed -e "s,%DIST%,$DIST_NAME,g" -i $MNTDIR/etc/apt/sources.list
Dean Troyerc2ddc972011-10-31 15:04:27 -0500185cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
186chroot $MNTDIR apt-get update
187chroot $MNTDIR apt-get install -y $MIN_PKGS
188rm -f $MNTDIR/etc/resolv.conf
189
190umount $MNTDIR
191rmdir $MNTDIR
Dean Troyerae605562011-10-26 12:35:22 -0500192qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500193NBD=""
Dean Troyer71745fe2011-10-31 16:59:02 -0500194
195mv $IMG_FILE_TMP $IMG_FILE