Mount bind external distfiles into the chroot.

Purpose of this is to allow us to avoid re-downloading everything
every time we rebuild the chroot.

This maintains two directories; host and target.  Future enhancement
involves collapsing this into one- this requires some host work however,
and has some potential gotchas in doing so.

Meanwhile, we now store distfiles in repo/distfiles/{host,target},
and mount bind repo/distfiles into /var/cache/distfiles.

An upgrade script in turn optimistically tries to move the content
into the new location; if it can't complete the move, it wipes the
content and the user has to redownload it (acceptable, if annoying).

BUG=chromium-os:13115
TEST=cbuildbot x86-generic-full

Change-Id: Iea96429df0e1fdc4ac0860fbce0daabc90c4c2a3
Reviewed-on: https://gerrit.chromium.org/gerrit/15189
Reviewed-by: Brian Harring <ferringb@chromium.org>
Tested-by: Brian Harring <ferringb@chromium.org>
Commit-Ready: Brian Harring <ferringb@chromium.org>
diff --git a/sdk_lib/enter_chroot.sh b/sdk_lib/enter_chroot.sh
index e6acc97..0339a76 100755
--- a/sdk_lib/enter_chroot.sh
+++ b/sdk_lib/enter_chroot.sh
@@ -25,6 +25,8 @@
   "The root of your chrome browser source. Should contain a 'src' subdir."
 DEFINE_string chrome_root_mount "/home/$USER/chrome_root" \
   "The mount point of the chrome broswer source in the chroot."
+DEFINE_string distfiles "" \
+  "Override the destination dir used for distfiles."
 
 DEFINE_boolean official_build $FLAGS_FALSE \
   "Set CHROMEOS_OFFICIAL=1 for release builds."
@@ -67,6 +69,9 @@
    CHROMEOS_OFFICIAL=1
 fi
 
+[ -z "${FLAGS_distfiles}" ] && \
+  FLAGS_distfiles="${FLAGS_trunk}/distfiles"
+
 # Only now can we die on error.  shflags functions leak non-zero error codes,
 # so will die prematurely if 'set -e' is specified before now.
 # TODO: replace shflags with something less error-prone, or contribute a fix.
@@ -290,6 +295,11 @@
     fi
     unset REFERENCE_DIR
 
+    debug "Setting up shared distfiles directory."
+    mkdir -p "${FLAGS_distfiles}"/{target,host}
+    sudo mkdir -p "${FLAGS_chroot}/var/cache/distfiles/"
+    queue_mount "${FLAGS_distfiles}" "--bind" "/var/cache/distfiles"
+
     if [ $FLAGS_ssh_agent -eq $FLAGS_TRUE ]; then
       if [ -n "${SSH_AUTH_SOCK}" -a -d "${HOME}/.ssh" ]; then
         TARGET_DIR="${FLAGS_chroot}/home/${USER}/.ssh"