Description: try to better handle out of date container caches.
 For a lucid container, apt-get update before installing the source package for
 add-apt-repository, so that apt-get does not fail.
 .
 If apt-get dist-upgrade fails, suggest running lxc-create with -F.
Author: Serge Hallyn <serge.hallyn@ubuntu.com>
Forwarded: yes
Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/lxc/+bug/942862

Index: lxc/templates/lxc-ubuntu.in
===================================================================
--- lxc.orig/templates/lxc-ubuntu.in	2012-09-14 11:34:25.616537000 -0500
+++ lxc/templates/lxc-ubuntu.in	2012-09-14 13:04:45.657607740 -0500
@@ -153,6 +153,12 @@
     rm -rf $cache/rootfs-$arch
 }
 
+suggest_flush()
+{
+    echo "Container upgrade failed.  The container cache may be out of date,"
+    echo "in which case flushing the case (see -F in the hep output) may help."
+}
+
 download_ubuntu()
 {
     cache=$1
@@ -200,15 +206,9 @@
 EOF
     chmod +x "$1/partial-${arch}"/usr/sbin/policy-rc.d
 
-    lxc-unshare -s MOUNT -- chroot "$1/partial-${arch}" apt-get dist-upgrade -y
-    ret=$?
+    lxc-unshare -s MOUNT -- chroot "$1/partial-${arch}" apt-get dist-upgrade -y || { suggest_flush; false; }
     rm -f "$1/partial-${arch}"/usr/sbin/policy-rc.d
 
-    if [ $ret -ne 0 ]; then
-        echo "Failed to upgrade the cache"
-        return 1
-    fi
-
     chroot "$1/partial-${arch}" apt-get clean
 
     mv "$1/partial-$arch" "$1/rootfs-$arch"
@@ -473,6 +473,7 @@
         # for lucid, if not trimming, then add the ubuntu-virt
         # ppa and install lxcguest
         if [ $release = "lucid" ]; then
+            chroot $rootfs apt-get update
             chroot $rootfs apt-get install --force-yes -y python-software-properties
             chroot $rootfs add-apt-repository ppa:ubuntu-virt/ppa
         fi
