Dean Troyer | 65cf608 | 2011-09-16 12:22:21 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # make_image.sh - Create Ubuntu images in various formats |
| 3 | # |
| 4 | # make_image.sh release format |
| 5 | # |
| 6 | # Supported formats: qcow (kvm), vmdk (vmserver), vdi (vbox), vhd (vpc) |
| 7 | # |
| 8 | # Requires sudo to root |
| 9 | |
| 10 | ROOTSIZE=${ROOTSIZE:-8192} |
| 11 | SWAPSIZE=${SWAPSIZE:-1024} |
| 12 | |
| 13 | usage() { |
| 14 | echo "$0 - Create Ubuntu images" |
| 15 | echo "" |
| 16 | echo "$0 [-r rootsize] [-s swapsize] release format" |
| 17 | exit 1 |
| 18 | } |
| 19 | |
| 20 | while getopts hm:r:s: c; do |
| 21 | case $c in |
Dean Troyer | 4dd420e | 2011-09-16 16:16:34 -0500 | [diff] [blame] | 22 | h) usage |
| 23 | ;; |
| 24 | m) MIRROR=$OPTARG |
| 25 | ;; |
| 26 | r) ROOTSIZE=$OPTARG |
| 27 | ;; |
| 28 | s) SWAPSIZE=$OPTARG |
| 29 | ;; |
Dean Troyer | 65cf608 | 2011-09-16 12:22:21 -0500 | [diff] [blame] | 30 | esac |
| 31 | done |
| 32 | shift `expr $OPTIND - 1` |
| 33 | |
| 34 | RELEASE=$1 |
| 35 | FORMAT=$2 |
| 36 | |
Dean Troyer | 4dd420e | 2011-09-16 16:16:34 -0500 | [diff] [blame] | 37 | case $FORMAT in |
| 38 | kvm|qcow2) FORMAT=qcow2 |
| 39 | TARGET=kvm |
| 40 | ;; |
| 41 | vmserver|vmdk) |
| 42 | FORMAT=vmdk |
| 43 | TARGET=vmserver |
| 44 | ;; |
| 45 | vbox|vdi) FORMAT=qcow2 |
| 46 | TARGET=kvm |
| 47 | FINAL_FORMAT=vdi |
| 48 | ;; |
| 49 | vhd|vpc) FORMAT=qcow2 |
| 50 | TARGET=kvm |
| 51 | FINAL_FORMAT=vhd |
| 52 | ;; |
| 53 | xen) FORMAT=raw |
| 54 | TARGET=xen |
| 55 | ;; |
| 56 | *) echo "Unknown format: $FORMAT" |
| 57 | usage |
Dean Troyer | 65cf608 | 2011-09-16 12:22:21 -0500 | [diff] [blame] | 58 | esac |
| 59 | |
Dean Troyer | 4dd420e | 2011-09-16 16:16:34 -0500 | [diff] [blame] | 60 | case $RELEASE in |
| 61 | natty) ;; |
| 62 | maverick) ;; |
| 63 | lucid) ;; |
| 64 | karmic) ;; |
| 65 | jaunty) ;; |
| 66 | *) echo "Unknown release: $RELEASE" |
| 67 | usage |
| 68 | ;; |
Dean Troyer | 65cf608 | 2011-09-16 12:22:21 -0500 | [diff] [blame] | 69 | esac |
| 70 | |
| 71 | # Install stuff if necessary |
| 72 | if [ -z `which vmbuilder` ]; then |
Dean Troyer | 4dd420e | 2011-09-16 16:16:34 -0500 | [diff] [blame] | 73 | sudo apt-get install ubuntu-vm-builder |
Dean Troyer | 65cf608 | 2011-09-16 12:22:21 -0500 | [diff] [blame] | 74 | fi |
| 75 | |
| 76 | # Build the image |
| 77 | sudo vmbuilder $TARGET ubuntu --suite $RELEASE \ |
| 78 | -o \ |
| 79 | --rootsize=$ROOTSIZE \ |
| 80 | --swapsize=$SWAPSIZE \ |
| 81 | --tmpfs - \ |
| 82 | --addpkg=openssh-server \ |
| 83 | |
| 84 | # --mirror=$MIRROR \ |
| 85 | |
| 86 | if [ -z "$FINAL_FORMAT" ]; then |
| 87 | # Get image |
| 88 | mv ubuntu-$TARGET/tmp*.$FORMAT $RELEASE.$FORMAT |
| 89 | else |
| 90 | # Convert image |
Dean Troyer | 4dd420e | 2011-09-16 16:16:34 -0500 | [diff] [blame] | 91 | tgt=$FINAL_FORMAT |
| 92 | if [ "$tgt" = "vhd" ]; then |
| 93 | tgt=vpc |
| 94 | fi |
| 95 | qemu-img convert -O $tgt ubuntu-$TARGET/tmp*.$FORMAT $RELEASE.$FINAL_FORMAT |
Dean Troyer | 65cf608 | 2011-09-16 12:22:21 -0500 | [diff] [blame] | 96 | fi |
| 97 | rm -rf ubuntu-$TARGET |