blob: a69f5e337498a39a370a23e8030418ccbf0214b3 [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
Jesse Andrews97c84742011-10-31 11:45:17 -0700141 sed -e "s,%DIST%,$RELEASE,g" -i $CHROOTDIR/etc/apt/sources.list
Dean Troyera41f9c72011-09-29 16:35:43 -0500142 sudo chroot $CHROOTDIR apt-get update
143
144 exit 0
145fi
146
Dean Troyer65cf6082011-09-16 12:22:21 -0500147# Build the image
Dean Troyera41f9c72011-09-29 16:35:43 -0500148TMPDIR=tmp
Dean Troyer57794d42011-09-16 17:22:23 -0500149TMPDISK=`mktemp imgXXXXXXXX`
150SIZE=$[$ROOTSIZE+$SWAPSIZE+1]
Dean Troyera41f9c72011-09-29 16:35:43 -0500151dd if=/dev/null of=$TMPDISK bs=1M seek=$SIZE count=1
152
153if [ -n "$IMAGEONLY" ]; then
154 # Build image from chroot
155 sudo vmbuilder $HYPER ubuntu $ARGS \
Dean Troyerc0a67392011-10-03 16:31:36 -0500156 --existing-chroot=$CHROOTDIR \
Dean Troyera41f9c72011-09-29 16:35:43 -0500157 --overwrite \
158 --rootsize=$ROOTSIZE \
159 --swapsize=$SWAPSIZE \
160 --tmpfs - \
161 --raw=$TMPDISK \
162
163else
164 # Do the whole shebang in one pass
165 ARGS="--variant=minbase"
166 for i in $MIN_PKGS; do
167 ARGS="$ARGS --addpkg=$i"
168 done
169 sudo vmbuilder $HYPER ubuntu $ARGS \
170 --suite $RELEASE \
171 --overwrite \
172 --rootsize=$ROOTSIZE \
173 --swapsize=$SWAPSIZE \
174 --tmpfs - \
175 --raw=$TMPDISK \
176
177fi
Dean Troyer65cf6082011-09-16 12:22:21 -0500178
Dean Troyer57794d42011-09-16 17:22:23 -0500179if [ "$FORMAT" = "raw" ]; then
Dean Troyer65cf6082011-09-16 12:22:21 -0500180 # Get image
Dean Troyer57794d42011-09-16 17:22:23 -0500181 mv $TMPDISK $RELEASE.$FORMAT
Dean Troyer65cf6082011-09-16 12:22:21 -0500182else
183 # Convert image
Dean Troyer57794d42011-09-16 17:22:23 -0500184 qemu-img convert -O $QFORMAT $TMPDISK $RELEASE.$FORMAT
185 rm $TMPDISK
Dean Troyer65cf6082011-09-16 12:22:21 -0500186fi
Dean Troyer57794d42011-09-16 17:22:23 -0500187rm -rf ubuntu-$HYPER