blob: e957c75639c304845ff57dc3eeb5d09e87d25ddd [file] [log] [blame]
Dean Troyer65cf6082011-09-16 12:22:21 -05001#!/bin/bash
2# make_image.sh - Create Ubuntu images in various formats
3#
Dean Troyer57794d42011-09-16 17:22:23 -05004# Supported formats: qcow (kvm), vmdk (vmserver), vdi (vbox), vhd (vpc), raw
Dean Troyer65cf6082011-09-16 12:22:21 -05005#
6# Requires sudo to root
7
8ROOTSIZE=${ROOTSIZE:-8192}
9SWAPSIZE=${SWAPSIZE:-1024}
Dean Troyera41f9c72011-09-29 16:35:43 -050010MIN_PKGS=${MIN_PKGS:-"apt-utils gpgv openssh-server"}
Dean Troyer65cf6082011-09-16 12:22:21 -050011
12usage() {
Dean Troyera41f9c72011-09-29 16:35:43 -050013 echo "Usage: $0 - Create Ubuntu images"
Dean Troyer65cf6082011-09-16 12:22:21 -050014 echo ""
Dean Troyera41f9c72011-09-29 16:35:43 -050015 echo "$0 [-m] [-r rootsize] [-s swapsize] release format"
16 echo "$0 -C [-m] release chrootdir"
17 echo "$0 -I [-r rootsize] [-s swapsize] chrootdir format"
18 echo ""
19 echo "-C - Create the initial chroot dir"
20 echo "-I - Create the final image from a chroot"
21 echo "-m - minimal installation"
22 echo "-r size - root fs size in MB"
23 echo "-s size - swap fs size in MB"
24 echo "release - Ubuntu release: jaunty - oneric"
25 echo "format - image format: qcow2, vmdk, vdi, vhd, xen, raw, fs"
Dean Troyer65cf6082011-09-16 12:22:21 -050026 exit 1
27}
28
Dean Troyera41f9c72011-09-29 16:35:43 -050029while getopts CIhmr:s: c; do
Dean Troyer65cf6082011-09-16 12:22:21 -050030 case $c in
Dean Troyera41f9c72011-09-29 16:35:43 -050031 C) CHROOTONLY=1
32 ;;
33 I) IMAGEONLY=1
34 ;;
Dean Troyer4dd420e2011-09-16 16:16:34 -050035 h) usage
36 ;;
Dean Troyera41f9c72011-09-29 16:35:43 -050037 m) MINIMAL=1
Dean Troyer4dd420e2011-09-16 16:16:34 -050038 ;;
39 r) ROOTSIZE=$OPTARG
40 ;;
41 s) SWAPSIZE=$OPTARG
42 ;;
Dean Troyer65cf6082011-09-16 12:22:21 -050043 esac
44done
45shift `expr $OPTIND - 1`
46
Dean Troyera4b58772011-09-29 21:30:27 -050047if [ ! "$#" -eq "2" -o -n "$CHROOTONLY" -a -n "$IMAGEONLY" ]; then
Dean Troyera41f9c72011-09-29 16:35:43 -050048 usage
49fi
50
51# Default args
Dean Troyer65cf6082011-09-16 12:22:21 -050052RELEASE=$1
53FORMAT=$2
Dean Troyera41f9c72011-09-29 16:35:43 -050054CHROOTDIR=""
55
56if [ -n "$CHROOTONLY" ]; then
57 RELEASE=$1
58 CHROOTDIR=$2
59 FORMAT="pass"
60fi
61
62if [ -n "$IMAGEONLY" ]; then
63 CHROOTDIR=$1
64 FORMAT=$2
65 RELEASE="pass"
66fi
Dean Troyer65cf6082011-09-16 12:22:21 -050067
Dean Troyerecc5a902011-10-25 15:49:47 -050068# Make sure that we have the proper version of ubuntu
69UBUNTU_VERSION=`cat /etc/lsb-release | grep CODENAME | sed 's/.*=//g'`
70if [ "$UBUNTU_VERSION" = "natty" -a "$RELEASE" = "oneiric" ]; then
71 echo "natty installs can't build oneiric images"
72 exit 1
73fi
74
Dean Troyer4dd420e2011-09-16 16:16:34 -050075case $FORMAT in
76 kvm|qcow2) FORMAT=qcow2
Dean Troyer57794d42011-09-16 17:22:23 -050077 QFORMAT=qcow2
78 HYPER=kvm
Dean Troyer4dd420e2011-09-16 16:16:34 -050079 ;;
80 vmserver|vmdk)
81 FORMAT=vmdk
Dean Troyer57794d42011-09-16 17:22:23 -050082 QFORMAT=vmdk
83 HYPER=vmserver
Dean Troyer4dd420e2011-09-16 16:16:34 -050084 ;;
Dean Troyer57794d42011-09-16 17:22:23 -050085 vbox|vdi) FORMAT=vdi
86 QFORMAT=vdi
87 HYPER=kvm
Dean Troyer4dd420e2011-09-16 16:16:34 -050088 ;;
Dean Troyer57794d42011-09-16 17:22:23 -050089 vhd|vpc) FORMAT=vhd
90 QFORMAT=vpc
91 HYPER=kvm
Dean Troyer4dd420e2011-09-16 16:16:34 -050092 ;;
93 xen) FORMAT=raw
Dean Troyer57794d42011-09-16 17:22:23 -050094 QFORMAT=raw
95 HYPER=xen
96 ;;
97 raw) FORMAT=raw
98 QFORMAT=raw
99 HYPER=kvm
Dean Troyer4dd420e2011-09-16 16:16:34 -0500100 ;;
Dean Troyera41f9c72011-09-29 16:35:43 -0500101 pass) ;;
Dean Troyer4dd420e2011-09-16 16:16:34 -0500102 *) echo "Unknown format: $FORMAT"
103 usage
Dean Troyer65cf6082011-09-16 12:22:21 -0500104esac
105
Dean Troyer4dd420e2011-09-16 16:16:34 -0500106case $RELEASE in
Dean Troyerfcfa9c32011-10-24 16:18:52 -0500107 oneiric) ;;
Dean Troyer4dd420e2011-09-16 16:16:34 -0500108 natty) ;;
109 maverick) ;;
110 lucid) ;;
111 karmic) ;;
112 jaunty) ;;
Dean Troyera41f9c72011-09-29 16:35:43 -0500113 pass) ;;
Dean Troyer4dd420e2011-09-16 16:16:34 -0500114 *) echo "Unknown release: $RELEASE"
115 usage
116 ;;
Dean Troyer65cf6082011-09-16 12:22:21 -0500117esac
118
119# Install stuff if necessary
120if [ -z `which vmbuilder` ]; then
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700121 sudo apt-get install -y ubuntu-vm-builder
Dean Troyer65cf6082011-09-16 12:22:21 -0500122fi
123
Dean Troyera41f9c72011-09-29 16:35:43 -0500124if [ -n "$CHROOTONLY" ]; then
125 # Build a chroot directory
126 HYPER=kvm
127 if [ "$MINIMAL" = 1 ]; then
128 ARGS="--variant=minbase"
129 for i in $MIN_PKGS; do
130 ARGS="$ARGS --addpkg=$i"
131 done
132 fi
133 sudo vmbuilder $HYPER ubuntu $ARGS \
134 --suite $RELEASE \
135 --only-chroot \
136 --chroot-dir=$CHROOTDIR \
137 --overwrite \
138 --addpkg=$MIN_PKGS \
139
140 sudo cp -p files/sources.list $CHROOTDIR/etc/apt/sources.list
141 sudo chroot $CHROOTDIR apt-get update
142
143 exit 0
144fi
145
Dean Troyer65cf6082011-09-16 12:22:21 -0500146# Build the image
Dean Troyera41f9c72011-09-29 16:35:43 -0500147TMPDIR=tmp
Dean Troyer57794d42011-09-16 17:22:23 -0500148TMPDISK=`mktemp imgXXXXXXXX`
149SIZE=$[$ROOTSIZE+$SWAPSIZE+1]
Dean Troyera41f9c72011-09-29 16:35:43 -0500150dd if=/dev/null of=$TMPDISK bs=1M seek=$SIZE count=1
151
152if [ -n "$IMAGEONLY" ]; then
153 # Build image from chroot
154 sudo vmbuilder $HYPER ubuntu $ARGS \
Dean Troyerc0a67392011-10-03 16:31:36 -0500155 --existing-chroot=$CHROOTDIR \
Dean Troyera41f9c72011-09-29 16:35:43 -0500156 --overwrite \
157 --rootsize=$ROOTSIZE \
158 --swapsize=$SWAPSIZE \
159 --tmpfs - \
160 --raw=$TMPDISK \
161
162else
163 # Do the whole shebang in one pass
164 ARGS="--variant=minbase"
165 for i in $MIN_PKGS; do
166 ARGS="$ARGS --addpkg=$i"
167 done
168 sudo vmbuilder $HYPER ubuntu $ARGS \
169 --suite $RELEASE \
170 --overwrite \
171 --rootsize=$ROOTSIZE \
172 --swapsize=$SWAPSIZE \
173 --tmpfs - \
174 --raw=$TMPDISK \
175
176fi
Dean Troyer65cf6082011-09-16 12:22:21 -0500177
Dean Troyer57794d42011-09-16 17:22:23 -0500178if [ "$FORMAT" = "raw" ]; then
Dean Troyer65cf6082011-09-16 12:22:21 -0500179 # Get image
Dean Troyer57794d42011-09-16 17:22:23 -0500180 mv $TMPDISK $RELEASE.$FORMAT
Dean Troyer65cf6082011-09-16 12:22:21 -0500181else
182 # Convert image
Dean Troyer57794d42011-09-16 17:22:23 -0500183 qemu-img convert -O $QFORMAT $TMPDISK $RELEASE.$FORMAT
184 rm $TMPDISK
Dean Troyer65cf6082011-09-16 12:22:21 -0500185fi
Dean Troyer57794d42011-09-16 17:22:23 -0500186rm -rf ubuntu-$HYPER