dev_install script ready

This change makes the dev_install script functional. It will download the
necessary files to bootstrap python and emerge on a base image.

BUG=chromium-os:11502
TEST=Build base image and run dev_install in the target. Developer packages
should install correctly.

Change-Id: If9975ed8342d4924e8b0096b649847b2944a5ca0
Reviewed-on: http://gerrit.chromium.org/gerrit/5317
Tested-by: Arkaitz Ruiz Alvarez <arkaitzr@chromium.org>
Reviewed-by: Chris Sosa <sosa@chromium.org>
diff --git a/dev-install/99devinstall b/dev-install/99devinstall
new file mode 100644
index 0000000..c3f4c64
--- /dev/null
+++ b/dev-install/99devinstall
@@ -0,0 +1,7 @@
+# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+LDPATH="/usr/local/lib"
+PATH="/usr/local/bin"
+LD_LIBRARY_PATH="/usr/local/lib"
+PORTAGE_CONFIGROOT="/usr/local"
diff --git a/dev-install/dev_install b/dev-install/dev_install
index cc79ac1..d05e595 100644
--- a/dev-install/dev_install
+++ b/dev-install/dev_install
@@ -7,17 +7,13 @@
 # in developer mode. It also takes care of some configuration details
 # that arise from not havin write access to the root filesystem.
 
-# TODO(arkaitzr): more details (and more code) to come.
-
 # Constant definitions.
-PKG_BZIP2_FILE="dev-installer-latest.bz2"
-# Python is not in the base image, so we use a statically linked python
-# interpreter. The only version available for download is 2.7.
-PYTHON_BINARY="python2.7-static"
+EMERGE_PACKAGES="/etc/portage/bootstrap.packages"
+# TODO(arkaitzr): Fix the color output if time allows.
 # Color the script output a little.
-ERROR_S_COLOR="\E[47;44m\033[1m"
+ERROR_S_COLOR="\033[47m\033[44m\033[1m"
 ERROR_E_COLOR="\033[0m"
-S_COLOR="\E[40;32m\033[1m"
+S_COLOR="\033[40m\033[32m\033[1m"
 E_COLOR="\033[0m"
 
 # This script should only run in developer mode or for developer images.
@@ -25,100 +21,113 @@
 CROS_DEBUG=$?
 
 if [ ${CROS_DEBUG} -ne 1 ]; then
-  echo -en "${ERROR_S_COLOR}ERROR: Can not run script."
-  echo -en "Chrome OS is not in developer mode.${ERROR_E_COLOR}"
+  echo -e "${ERROR_S_COLOR}ERROR: Can not run script."
+  echo -e "Chrome OS is not in developer mode.${ERROR_E_COLOR}"
   exit 1
 fi
 
 # Check if we are root.
-if [[ $(/usr/bin/id -u) -ne 0 ]]; then
-  echo -en "${ERROR_S_COLOR}ERROR: Can not run script."
-  echo -en "You are not root or you did not use sudo.${ERROR_E_COLOR}"
+if [ $(/usr/bin/id -u) -ne 0 ]; then
+  echo -e "${ERROR_S_COLOR}ERROR: Can not run script."
+  echo -e "You are not root or you did not use sudo.${ERROR_E_COLOR}"
   exit 1
 fi
 
 # Create work directory.
 WORKDIR="$(mktemp -d /tmp/dev-installer.XXXX)"
+# Copy emerge configuration to /usr/local.
+mkdir -p -m 0755 /usr/local/etc/portage
+cp /etc/portage/make.profile/make.conf /usr/local/etc/portage
+ln -s /etc/portage/make.profile /usr/local/etc/portage/make.profile
+# Get the portage configuration variables.
+. /etc/portage/make.profile/make.defaults
+
+# Create the directories defined in the portage config files. Permissions are
+# consistent with the other directories in /usr/local, which is a bind mount
+# for /mnt/stateful_partition/dev_image.
+mkdir -p -m 0755 "${PORTDIR}"
+mkdir -p -m 0755 "${PKGDIR}"
+mkdir -p -m 0755 "${DISTDIR}"
+mkdir -p -m 0755 "${RPMDIR}"
+mkdir -p -m 0755 "${PORTAGE_TMPDIR}"
+mkdir -p -m 0755 "${BUILD_PREFIX}"
+
+# Create this loop so uncompressed files in /usr/local/usr/* will be reachable
+# through /usr/local*.
+if [ ! -d /usr/local/usr ]; then
+  ln -s /usr/local /usr/local/usr
+fi
 
 # Get the static Python binary and portage (emerge script and python modules).
-download_bootstrap_package() {
+download_bootstrap_packages() {
   # Obtain the BOOTSTRAP and BINHOST variables.
-  source "/etc/portage/repository.conf"
-  local url
-  local tar_exit_code
-  url="${BOOTSTRAP}/${PKG_BZIP2_FILE}"
+  . /etc/portage/repository.conf
 
-  wget -N -p "${WORKDIR}" $"{url}"
-  if [ ! -f "${WORKDIR}/${PKG_BZIP2_FILE}" ]; then
-    echo -en "${ERROR_S_COLOR}ERROR: Can not download dev-installer package."
-    echo -en "Check network connection and access to ${url}.${ERROR_E_COLOR}"
-    exit 1
-  fi
-
-  tar -C "${WORKDIR}" -xjf "${WORKDIR}/${PKG_BZIP2_FILE}"
-  tar_exit_code=$?
-  if [ tar_exit_code -ne 0 ]; then
-    echo -en "$ERROR_S_COLOR}ERROR: Can not extract dev-installer package."
-    echo -en "Command tar -C ${WORKDIR}-xjf ${PKG_BZIP2_FILE} failed." \
-             "${ERROR_E_COLOR}"
-    exit 1
-  fi
+  # Download packages that python/emerge require into /usr/local/bootsrap.
+  while read line; do
+    local package_url
+    local directory
+    local package_file
+    package_url="${BINHOST}packages/$line.tbz2"
+    directory=`echo "${line}" | cut -d"/" -f1`
+    package_file="${PKGDIR}/$line.tbz2"
+    mkdir -p -m 0755 "/usr/local/portage/packages/${directory}"
+    wget -nv -N -nd -P "/usr/local/portage/packages/${directory}" \
+         "${package_url}"
+    wget_exit_code=$?
+    if [ ${wget_exit_code} -ne 0 ]; then
+      echo -e "${ERROR_S_COLOR}ERROR: Could not download package."
+      echo -e "Command wget -nv -N -nd -P /usr/local/portage/packages/" \
+              "${directory} ${package_url} failed.${ERROR_E_COLOR}"
+      exit 1
+    fi
+    # Ignore std error output about trailing garbage after EOF.
+    tar -C "/usr/local/" -xjf "${package_file}" 2>/dev/null
+    tar_exit_code=$?
+    if [ ${tar_exit_code} -ne 0 ]; then
+      echo -e "${ERROR_S_COLOR}ERROR: Could not extract package."
+      echo -e "Command tar -C /usr/local -xjf ${package_file} " \
+              "failed.${ERROR_E_COLOR}"
+      exit 1
+    fi
+  done < "${EMERGE_PACKAGES}"
 }
 
-# Install python and emerge in /usr/local.
-install_emerge() {
-  chmod +x "${WORKDIR}/${PYTHON_BINARY}"
-  # Set PYTHONPATH so python can find portage's modules.
-  PYTHONPATH="${WORKDIR}/portage/pym"
-  export PYTHONPATH
-
-  # TODO(arkaitzr): Create the directories defined in the portage config files.
-  source "/etc/make.profile/make.defaults"
-
-  # Move packages to the appropiate directory in /usr/local.
-  # TODO(arkaitzr): once the binhost is setup, get packages from there via wget
-  # so they do not have to be included in the downloaded bz2 file (now 18MB).
-  mv "${WORKDIR}/app-misc" "/usr/local/portage/packages"
-  mv "${WORKDIR}/dev-lang" "/usr/local/portage/packages"
-  mv "${WORKDIR}/dev-libs" "/usr/local/portage/packages"
-  mv "${WORKDIR}/virtual" "/usr/local/portage/packages"
-  # First, we install python using the files from the downloaded package.
-  # We can not use the binhost because the static version of python included
-  # does not have module unicodedata (necessary for http connections).
-  "${WORKDIR}/${PYTHON_BINARY}" "${WORKDIR}/portage/bin/emerge" --usepkgonly \
-                                                                dev-lang/python
-  # Now we install portage using the binary packages from the binhost.
-  python "${WORKDIR}/portage/bin/emerge" --getbinpkgonly \
-                                         --usepkgonly \
-                                         portage
-  unset PYTHONPATH
+# Configure emerge in /usr/local.
+configure_emerge() {
+  # Add LD_LIBRARY_PATH within ebuild.sh.
+  # TODO(arkaitzr): find out a cleaner way to do this.
+  sed -i '3 a\export LD_LIBRARY_PATH=/usr/local/lib' \
+      /usr/local/lib/portage/bin/ebuild.sh
 }
 
 install_optional_packages() {
-  # TODO(arkaitzr): give the user the option of installing chromeos-dev pkgs.
-  echo "Do you want to install chrome-os/dev package and dependencies? (y/n)"
+  local reply
+  read -p "Install chromeos-dev package now (y/n)?" reply
+  if [ "${reply}" = "y" ]; then
+    emerge chromeos-dev
+  else
+    echo "You can install chromeos-dev later by typing the command:"
+    echo "emerge chromeos-dev"
+  fi
 }
 
 cleanup_directories() {
   rm -r "${WORKDIR}"
 }
 
-# TODO(arkaitzr): add calls to download_bootstrap_package and install_emerge
-# when repositories are configured.
-echo -en "${S_COLOR}Starting installation of developer packages."
-echo -en "First, we download the necessary files.${E_COLOR}"
-download_bootstrap_package
+echo "Starting installation of developer packages."
+echo "First, we download the necessary files."
+download_bootstrap_packages
 
-echo -en "${S_COLOR}Files downloaded, installing python and emerge."\
-         "${E_COLOR}"
-install_emerge
+echo "Files downloaded, configuring emerge."
+configure_emerge
 
-echo -en "${S_COLOR}Emerge installation complete. Installing additional " \
-         "packages.${E_COLOR}"
+echo "Emerge installation complete. Installing additional optional packages."
 install_optional_packages
 
-echo -en "${S_COLOR}Cleaning temporary directories.${E_COLOR}"
+echo "Cleaning temporary directories."
 cleanup_directories
 
-echo -en "${S_COLOR}Dev-install.sh done. Enjoy!${E_COLOR}"
+echo "dev_install done. Enjoy!"
 
diff --git a/dev-install/make.conf b/dev-install/make.conf
new file mode 100644
index 0000000..e3e3a57
--- /dev/null
+++ b/dev-install/make.conf
@@ -0,0 +1,10 @@
+# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# Get the default configuration.
+source /usr/local/share/portage/config/make.globals
+
+# Include overrides for using /usr/local.
+source /usr/local/etc/portage/make.profile/make.defaults
+
diff --git a/dev-install/make.defaults b/dev-install/make.defaults
index da38c6a..388fe9d 100644
--- a/dev-install/make.defaults
+++ b/dev-install/make.defaults
@@ -7,24 +7,30 @@
 # us to bootstrap emerge and allow to install packages in /usr/local. Rootfs
 # will remain unmodified.
 
+# Point everything to /usr/local.
 ROOT=/usr/local
 
-PORTDIR=${ROOT}/portage
+PORTDIR="${ROOT}/portage"
 
-PKGDIR=${ROOT}/portage/packages
+PKGDIR="${ROOT}/portage/packages"
 
-DISTDIR=${ROOT}/portage/distfiles
+DISTDIR="${ROOT}/portage/distfiles"
 
-RPMDIR=${ROOT}/portage/rpm
+RPMDIR="${ROOT}/portage/rpm"
 
-PORTAGE_TMPDIR=${ROOT}/var/tmp
+PORTAGE_TMPDIR="${ROOT}/var/tmp"
 
-BUILD_PREFIX=${ROOT}/var/tmp/portage
+BUILD_PREFIX="${ROOT}/var/tmp/portage"
 
 CONFIG_PROTECT="/usr/local/share/portage"
 
-ACCEPT_KEYWORDS="arm x86 ~arm ~x86"
+# TODO(arkaitzr): Generate this in the ebuild.
+ACCEPT_KEYWORDS="x86 ~x86 arm ~arm"
 
 PORTAGE_WORKDIR_MODE="0700"
 
+EMERGE_DEFAULT_OPTS="--getbinpkg --usepkgonly"
+
+# TODO(arkaitzr): generate this during the ebuild phase.
+PORTAGE_BINHOST=http://commondatastorage.googleapis.com/chromeos-dev-installer2/board/x86-mario/full-02.08.11.130047/packages/
 
diff --git a/dev-install/repository.conf b/dev-install/repository.conf
index 5fb049c..85b18df 100644
--- a/dev-install/repository.conf
+++ b/dev-install/repository.conf
@@ -8,5 +8,5 @@
 
 # TODO(arkaitzr): These repositories are temporal and only to be used for
 # development. I'll change them when the binhosts are ready.
-BOOTSTRAP=http://commondatastore.googleapis.com/chromeos-dev-installer/
-BINHOST=http://arkaitzr.mtv.corp.google.com:8080/static/pkgroot/BOARD/packages
+BOOTSTRAP=http://commondatastorage.googleapis.com/chromeos-dev-installer2/board/BOARD/bootstrap
+BINHOST=http://commondatastorage.googleapis.com/chromeos-dev-installer2/board/BOARD/full-02.08.11.130047/