2011-09-16 17:22:21 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# make_image.sh - Create Ubuntu images in various formats
|
|
|
|
#
|
2011-09-16 22:22:23 +00:00
|
|
|
# Supported formats: qcow (kvm), vmdk (vmserver), vdi (vbox), vhd (vpc), raw
|
2011-09-16 17:22:21 +00:00
|
|
|
#
|
|
|
|
# Requires sudo to root
|
|
|
|
|
|
|
|
ROOTSIZE=${ROOTSIZE:-8192}
|
|
|
|
SWAPSIZE=${SWAPSIZE:-1024}
|
2011-09-29 21:35:43 +00:00
|
|
|
MIN_PKGS=${MIN_PKGS:-"apt-utils gpgv openssh-server"}
|
2011-09-16 17:22:21 +00:00
|
|
|
|
|
|
|
usage() {
|
2011-09-29 21:35:43 +00:00
|
|
|
echo "Usage: $0 - Create Ubuntu images"
|
|
|
|
echo ""
|
|
|
|
echo "$0 [-m] [-r rootsize] [-s swapsize] release format"
|
|
|
|
echo "$0 -C [-m] release chrootdir"
|
|
|
|
echo "$0 -I [-r rootsize] [-s swapsize] chrootdir format"
|
2011-09-16 17:22:21 +00:00
|
|
|
echo ""
|
2011-09-29 21:35:43 +00:00
|
|
|
echo "-C - Create the initial chroot dir"
|
|
|
|
echo "-I - Create the final image from a chroot"
|
|
|
|
echo "-m - minimal installation"
|
|
|
|
echo "-r size - root fs size in MB"
|
|
|
|
echo "-s size - swap fs size in MB"
|
|
|
|
echo "release - Ubuntu release: jaunty - oneric"
|
|
|
|
echo "format - image format: qcow2, vmdk, vdi, vhd, xen, raw, fs"
|
2011-09-16 17:22:21 +00:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:35:43 +00:00
|
|
|
while getopts CIhmr:s: c; do
|
2011-09-16 17:22:21 +00:00
|
|
|
case $c in
|
2011-09-29 21:35:43 +00:00
|
|
|
C) CHROOTONLY=1
|
|
|
|
;;
|
|
|
|
I) IMAGEONLY=1
|
|
|
|
;;
|
2011-09-16 21:16:34 +00:00
|
|
|
h) usage
|
|
|
|
;;
|
2011-09-29 21:35:43 +00:00
|
|
|
m) MINIMAL=1
|
2011-09-16 21:16:34 +00:00
|
|
|
;;
|
|
|
|
r) ROOTSIZE=$OPTARG
|
|
|
|
;;
|
|
|
|
s) SWAPSIZE=$OPTARG
|
|
|
|
;;
|
2011-09-16 17:22:21 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift `expr $OPTIND - 1`
|
|
|
|
|
2011-09-30 02:30:27 +00:00
|
|
|
if [ ! "$#" -eq "2" -o -n "$CHROOTONLY" -a -n "$IMAGEONLY" ]; then
|
2011-09-29 21:35:43 +00:00
|
|
|
usage
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Default args
|
2011-09-16 17:22:21 +00:00
|
|
|
RELEASE=$1
|
|
|
|
FORMAT=$2
|
2011-09-29 21:35:43 +00:00
|
|
|
CHROOTDIR=""
|
|
|
|
|
|
|
|
if [ -n "$CHROOTONLY" ]; then
|
|
|
|
RELEASE=$1
|
|
|
|
CHROOTDIR=$2
|
|
|
|
FORMAT="pass"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$IMAGEONLY" ]; then
|
|
|
|
CHROOTDIR=$1
|
|
|
|
FORMAT=$2
|
|
|
|
RELEASE="pass"
|
|
|
|
fi
|
2011-09-16 17:22:21 +00:00
|
|
|
|
2011-09-16 21:16:34 +00:00
|
|
|
case $FORMAT in
|
|
|
|
kvm|qcow2) FORMAT=qcow2
|
2011-09-16 22:22:23 +00:00
|
|
|
QFORMAT=qcow2
|
|
|
|
HYPER=kvm
|
2011-09-16 21:16:34 +00:00
|
|
|
;;
|
|
|
|
vmserver|vmdk)
|
|
|
|
FORMAT=vmdk
|
2011-09-16 22:22:23 +00:00
|
|
|
QFORMAT=vmdk
|
|
|
|
HYPER=vmserver
|
2011-09-16 21:16:34 +00:00
|
|
|
;;
|
2011-09-16 22:22:23 +00:00
|
|
|
vbox|vdi) FORMAT=vdi
|
|
|
|
QFORMAT=vdi
|
|
|
|
HYPER=kvm
|
2011-09-16 21:16:34 +00:00
|
|
|
;;
|
2011-09-16 22:22:23 +00:00
|
|
|
vhd|vpc) FORMAT=vhd
|
|
|
|
QFORMAT=vpc
|
|
|
|
HYPER=kvm
|
2011-09-16 21:16:34 +00:00
|
|
|
;;
|
|
|
|
xen) FORMAT=raw
|
2011-09-16 22:22:23 +00:00
|
|
|
QFORMAT=raw
|
|
|
|
HYPER=xen
|
|
|
|
;;
|
|
|
|
raw) FORMAT=raw
|
|
|
|
QFORMAT=raw
|
|
|
|
HYPER=kvm
|
2011-09-16 21:16:34 +00:00
|
|
|
;;
|
2011-09-29 21:35:43 +00:00
|
|
|
pass) ;;
|
2011-09-16 21:16:34 +00:00
|
|
|
*) echo "Unknown format: $FORMAT"
|
|
|
|
usage
|
2011-09-16 17:22:21 +00:00
|
|
|
esac
|
|
|
|
|
2011-09-16 21:16:34 +00:00
|
|
|
case $RELEASE in
|
|
|
|
natty) ;;
|
|
|
|
maverick) ;;
|
|
|
|
lucid) ;;
|
|
|
|
karmic) ;;
|
|
|
|
jaunty) ;;
|
2011-09-29 21:35:43 +00:00
|
|
|
pass) ;;
|
2011-09-16 21:16:34 +00:00
|
|
|
*) echo "Unknown release: $RELEASE"
|
|
|
|
usage
|
|
|
|
;;
|
2011-09-16 17:22:21 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
# Install stuff if necessary
|
|
|
|
if [ -z `which vmbuilder` ]; then
|
2011-09-16 21:16:34 +00:00
|
|
|
sudo apt-get install ubuntu-vm-builder
|
2011-09-16 17:22:21 +00:00
|
|
|
fi
|
|
|
|
|
2011-09-29 21:35:43 +00:00
|
|
|
if [ -n "$CHROOTONLY" ]; then
|
|
|
|
# Build a chroot directory
|
|
|
|
HYPER=kvm
|
|
|
|
if [ "$MINIMAL" = 1 ]; then
|
|
|
|
ARGS="--variant=minbase"
|
|
|
|
for i in $MIN_PKGS; do
|
|
|
|
ARGS="$ARGS --addpkg=$i"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
sudo vmbuilder $HYPER ubuntu $ARGS \
|
|
|
|
--suite $RELEASE \
|
|
|
|
--only-chroot \
|
|
|
|
--chroot-dir=$CHROOTDIR \
|
|
|
|
--overwrite \
|
|
|
|
--addpkg=$MIN_PKGS \
|
|
|
|
|
|
|
|
sudo cp -p files/sources.list $CHROOTDIR/etc/apt/sources.list
|
|
|
|
sudo chroot $CHROOTDIR apt-get update
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2011-09-16 17:22:21 +00:00
|
|
|
# Build the image
|
2011-09-29 21:35:43 +00:00
|
|
|
TMPDIR=tmp
|
2011-09-16 22:22:23 +00:00
|
|
|
TMPDISK=`mktemp imgXXXXXXXX`
|
|
|
|
SIZE=$[$ROOTSIZE+$SWAPSIZE+1]
|
2011-09-29 21:35:43 +00:00
|
|
|
dd if=/dev/null of=$TMPDISK bs=1M seek=$SIZE count=1
|
|
|
|
|
|
|
|
if [ -n "$IMAGEONLY" ]; then
|
|
|
|
# Build image from chroot
|
|
|
|
sudo vmbuilder $HYPER ubuntu $ARGS \
|
|
|
|
--existing-chroot=$CHR \
|
|
|
|
--overwrite \
|
|
|
|
--rootsize=$ROOTSIZE \
|
|
|
|
--swapsize=$SWAPSIZE \
|
|
|
|
--tmpfs - \
|
|
|
|
--raw=$TMPDISK \
|
|
|
|
|
|
|
|
else
|
|
|
|
# Do the whole shebang in one pass
|
|
|
|
ARGS="--variant=minbase"
|
|
|
|
for i in $MIN_PKGS; do
|
|
|
|
ARGS="$ARGS --addpkg=$i"
|
|
|
|
done
|
|
|
|
sudo vmbuilder $HYPER ubuntu $ARGS \
|
|
|
|
--suite $RELEASE \
|
|
|
|
--overwrite \
|
|
|
|
--rootsize=$ROOTSIZE \
|
|
|
|
--swapsize=$SWAPSIZE \
|
|
|
|
--tmpfs - \
|
|
|
|
--raw=$TMPDISK \
|
|
|
|
|
|
|
|
fi
|
2011-09-16 17:22:21 +00:00
|
|
|
|
2011-09-16 22:22:23 +00:00
|
|
|
if [ "$FORMAT" = "raw" ]; then
|
2011-09-16 17:22:21 +00:00
|
|
|
# Get image
|
2011-09-16 22:22:23 +00:00
|
|
|
mv $TMPDISK $RELEASE.$FORMAT
|
2011-09-16 17:22:21 +00:00
|
|
|
else
|
|
|
|
# Convert image
|
2011-09-16 22:22:23 +00:00
|
|
|
qemu-img convert -O $QFORMAT $TMPDISK $RELEASE.$FORMAT
|
|
|
|
rm $TMPDISK
|
2011-09-16 17:22:21 +00:00
|
|
|
fi
|
2011-09-16 22:22:23 +00:00
|
|
|
rm -rf ubuntu-$HYPER
|