blob: d81cef530445026e9cd2cc4ad72b09cd4aacbd96 [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 Troyer4dd420e2011-09-16 16:16:34 -050068case $FORMAT in
69 kvm|qcow2) FORMAT=qcow2
Dean Troyer57794d42011-09-16 17:22:23 -050070 QFORMAT=qcow2
71 HYPER=kvm
Dean Troyer4dd420e2011-09-16 16:16:34 -050072 ;;
73 vmserver|vmdk)
74 FORMAT=vmdk
Dean Troyer57794d42011-09-16 17:22:23 -050075 QFORMAT=vmdk
76 HYPER=vmserver
Dean Troyer4dd420e2011-09-16 16:16:34 -050077 ;;
Dean Troyer57794d42011-09-16 17:22:23 -050078 vbox|vdi) FORMAT=vdi
79 QFORMAT=vdi
80 HYPER=kvm
Dean Troyer4dd420e2011-09-16 16:16:34 -050081 ;;
Dean Troyer57794d42011-09-16 17:22:23 -050082 vhd|vpc) FORMAT=vhd
83 QFORMAT=vpc
84 HYPER=kvm
Dean Troyer4dd420e2011-09-16 16:16:34 -050085 ;;
86 xen) FORMAT=raw
Dean Troyer57794d42011-09-16 17:22:23 -050087 QFORMAT=raw
88 HYPER=xen
89 ;;
90 raw) FORMAT=raw
91 QFORMAT=raw
92 HYPER=kvm
Dean Troyer4dd420e2011-09-16 16:16:34 -050093 ;;
Dean Troyera41f9c72011-09-29 16:35:43 -050094 pass) ;;
Dean Troyer4dd420e2011-09-16 16:16:34 -050095 *) echo "Unknown format: $FORMAT"
96 usage
Dean Troyer65cf6082011-09-16 12:22:21 -050097esac
98
Dean Troyer4dd420e2011-09-16 16:16:34 -050099case $RELEASE in
Dean Troyerfcfa9c32011-10-24 16:18:52 -0500100 oneiric) ;;
Dean Troyer4dd420e2011-09-16 16:16:34 -0500101 natty) ;;
102 maverick) ;;
103 lucid) ;;
104 karmic) ;;
105 jaunty) ;;
Dean Troyera41f9c72011-09-29 16:35:43 -0500106 pass) ;;
Dean Troyer4dd420e2011-09-16 16:16:34 -0500107 *) echo "Unknown release: $RELEASE"
108 usage
109 ;;
Dean Troyer65cf6082011-09-16 12:22:21 -0500110esac
111
112# Install stuff if necessary
113if [ -z `which vmbuilder` ]; then
Anthony Youngfa4b5eb2011-10-19 11:27:02 -0700114 sudo apt-get install -y ubuntu-vm-builder
Dean Troyer65cf6082011-09-16 12:22:21 -0500115fi
116
Dean Troyera41f9c72011-09-29 16:35:43 -0500117if [ -n "$CHROOTONLY" ]; then
118 # Build a chroot directory
119 HYPER=kvm
120 if [ "$MINIMAL" = 1 ]; then
121 ARGS="--variant=minbase"
122 for i in $MIN_PKGS; do
123 ARGS="$ARGS --addpkg=$i"
124 done
125 fi
126 sudo vmbuilder $HYPER ubuntu $ARGS \
127 --suite $RELEASE \
128 --only-chroot \
129 --chroot-dir=$CHROOTDIR \
130 --overwrite \
131 --addpkg=$MIN_PKGS \
132
133 sudo cp -p files/sources.list $CHROOTDIR/etc/apt/sources.list
134 sudo chroot $CHROOTDIR apt-get update
135
136 exit 0
137fi
138
Dean Troyer65cf6082011-09-16 12:22:21 -0500139# Build the image
Dean Troyera41f9c72011-09-29 16:35:43 -0500140TMPDIR=tmp
Dean Troyer57794d42011-09-16 17:22:23 -0500141TMPDISK=`mktemp imgXXXXXXXX`
142SIZE=$[$ROOTSIZE+$SWAPSIZE+1]
Dean Troyera41f9c72011-09-29 16:35:43 -0500143dd if=/dev/null of=$TMPDISK bs=1M seek=$SIZE count=1
144
145if [ -n "$IMAGEONLY" ]; then
146 # Build image from chroot
147 sudo vmbuilder $HYPER ubuntu $ARGS \
Dean Troyerc0a67392011-10-03 16:31:36 -0500148 --existing-chroot=$CHROOTDIR \
Dean Troyera41f9c72011-09-29 16:35:43 -0500149 --overwrite \
150 --rootsize=$ROOTSIZE \
151 --swapsize=$SWAPSIZE \
152 --tmpfs - \
153 --raw=$TMPDISK \
154
155else
156 # Do the whole shebang in one pass
157 ARGS="--variant=minbase"
158 for i in $MIN_PKGS; do
159 ARGS="$ARGS --addpkg=$i"
160 done
161 sudo vmbuilder $HYPER ubuntu $ARGS \
162 --suite $RELEASE \
163 --overwrite \
164 --rootsize=$ROOTSIZE \
165 --swapsize=$SWAPSIZE \
166 --tmpfs - \
167 --raw=$TMPDISK \
168
169fi
Dean Troyer65cf6082011-09-16 12:22:21 -0500170
Dean Troyer57794d42011-09-16 17:22:23 -0500171if [ "$FORMAT" = "raw" ]; then
Dean Troyer65cf6082011-09-16 12:22:21 -0500172 # Get image
Dean Troyer57794d42011-09-16 17:22:23 -0500173 mv $TMPDISK $RELEASE.$FORMAT
Dean Troyer65cf6082011-09-16 12:22:21 -0500174else
175 # Convert image
Dean Troyer57794d42011-09-16 17:22:23 -0500176 qemu-img convert -O $QFORMAT $TMPDISK $RELEASE.$FORMAT
177 rm $TMPDISK
Dean Troyer65cf6082011-09-16 12:22:21 -0500178fi
Dean Troyer57794d42011-09-16 17:22:23 -0500179rm -rf ubuntu-$HYPER