Description: fix handling of non-precise cloud image format
Author: Serge Hallyn <serge.hallyn@ubuntu.com>
Forwarded: yes

Index: lxc/templates/lxc-ubuntu-cloud.in
===================================================================
--- lxc.orig/templates/lxc-ubuntu-cloud.in	2012-03-15 10:07:54.292159000 -0500
+++ lxc/templates/lxc-ubuntu-cloud.in	2012-03-15 17:59:04.691747231 -0500
@@ -1,6 +1,6 @@
 #!/bin/bash
 
-# template script for generating ubuntu container for LXC based on daily cloud
+# template script for generating ubuntu container for LXC based on released cloud
 # images
 #
 # Copyright © 2012 Serge Hallyn <serge.hallyn@canonical.com>
@@ -147,6 +147,10 @@
     elif [ "$arch" = "x86_64" ]; then
         arch="amd64"
     elif [ "$arch" = "armv7l" ]; then
+        # note: arm images don't exist before oneiric;  are called armhf in
+        # precise;  and are not supported by the query, so we don't actually
+        # support them yet (see check later on).  When Query2 is available,
+        # we'll use that to enable arm images.
         arch="armel"
     fi
 fi
@@ -177,7 +181,7 @@
     esac
 done
 
-if [ $debug -eq ]; then
+if [ $debug -eq 1 ]; then
     set -x
 fi
 
@@ -219,12 +223,47 @@
 if [ -n "$tarball" ]; then
 	url2="$tarball"
 else
-	url1=`ubuntu-cloudimg-query precise daily $arch --format "%{url}\n"`
+	url1=`ubuntu-cloudimg-query $release released $arch --format "%{url}\n"`
 	url2=`echo $url1 | sed -e 's/.tar.gz/-root\0/'`
 fi
 
 filename=`basename $url2`
 
+buildcleanup()
+{
+    cd $rootfs
+    umount -l $cache/$xdir || true
+    rm -rf $cache
+}
+
+# if the release doesn't have a *-rootfs.tar.gz, then create one from the
+# cloudimg.tar.gz by extracting the .img, mounting it loopback, and creating
+# a tarball from the mounted image.
+build_root_tgz()
+{
+    url=$1
+    filename=$2
+
+    xdir=`mktemp -d -p .`
+    tarname=`basename $url`
+    imgname="`basename $tarname .tar.gz`.img"
+    trap buildcleanup EXIT
+    if [ $flushcache -eq 1 -o ! -f $cache/$tarname ]; then
+        rm -f $tarname
+        echo "Downloading cloud image from $url"
+        wget $url || { echo "Couldn't find cloud image $url."; exit 1; }
+    fi
+    echo "Creating new cached cloud image rootfs"
+    tar zxf $tarname $imgname
+    mount -o loop $imgname $xdir
+    (cd $xdir; tar zcf ../$filename .)
+    umount $xdir
+    rm -f $tarname $imgname
+    rmdir $xdir
+    echo "New cloud image cache created"
+    trap EXIT
+}
+
 mkdir -p /var/lock/subsys/
 (
     flock -n -x 200
@@ -236,10 +275,10 @@
     fi
 
     if [ ! -f $filename ]; then
-       wget $url2
+       wget $url2 || build_root_tgz $url1 $filename
     fi
 
-    echo "Extracting rootfs"
+    echo "Extracting container rootfs"
     mkdir -p $rootfs
     cd $rootfs
     tar -zxf $cache/$filename
@@ -294,6 +333,7 @@
 	fi
 
     	chroot $rootfs /usr/sbin/usermod -U ubuntu
+    	echo "ubuntu:ubuntu" | chroot $rootfs chpasswd
 	echo "Please login as user ubuntu with password ubuntu."
 
    else
