[moblab] Copy the files from the project-moblab overlay.

The cros-workon build is broken for the project-moblab overlay
until that is fixed move the init files to platform-moblab
so multiple changes can be in flight at the same time.

CQ-DEPEND=CL:1214290
BUG=chromium:881529
TEST= ad hoc moblab testing

Change-Id: Ic2fa7a74b452a313f75101e37c875f38ef374fdc
Reviewed-on: https://chromium-review.googlesource.com/1214286
Commit-Ready: Keith Haddow <haddowk@chromium.org>
Tested-by: Keith Haddow <haddowk@chromium.org>
Reviewed-by: Keith Haddow <haddowk@chromium.org>
diff --git a/src/upstart_init/boot-logging-moblab.conf b/src/upstart_init/boot-logging-moblab.conf
new file mode 100644
index 0000000..d9f26a1
--- /dev/null
+++ b/src/upstart_init/boot-logging-moblab.conf
@@ -0,0 +1,32 @@
+# Copyright 2018 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.
+
+description   "Log all the moblab upstart events to aid debugging."
+author        "chromium-os-dev@chromium.org"
+
+start on (starting moblab* or
+          started moblab* or
+          stopping moblab* or
+          stopped moblab*)
+
+normal exit 0
+
+script
+    mkdir -p /var/log/bootup/
+    exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+    log="$JOB\t\t\tevent(s) ${UPSTART_EVENTS}"
+    if [ "${RESULT}" = "failed" ]; then
+      log="${log} process $PROCESS"
+    fi
+    if [ -n "${RESULT}" ]; then
+      log="${log} result $RESULT"
+    fi
+    if [ -n "${EXIT_STATUS}" ]; then
+      log="${log} exit_code $EXIT_STATUS"
+    fi
+    if [ -n "${EXIT_SIGNAL}" ]; then
+      log="${log} exit_signal $EXIT_SIGNAL"
+    fi
+    echo -e "${log}"
+end script
diff --git a/src/upstart_init/job_aborter.conf b/src/upstart_init/job_aborter.conf
new file mode 100644
index 0000000..b5daf0d
--- /dev/null
+++ b/src/upstart_init/job_aborter.conf
@@ -0,0 +1,25 @@
+# Copyright 2017 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.
+
+description "lucifer Autotest job aborter daemon"
+
+start on (stopped autotest_setup and
+          started moblab-database-init)
+respawn
+
+env CROS_VENV_CACHE=/usr/local/.cros-venv-cache
+
+script
+  # Preparing dhcp's /var/{lib|run} dirs to run as user 'dhcp'.
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+
+  mkdir -p "${CROS_VENV_CACHE}"
+  chown moblab:moblab "${CROS_VENV_CACHE}"
+  exec sudo -E -u moblab /usr/local/autotest/bin/job_aborter \
+    --jobdir /usr/local/autotest/leases
+end script
+
diff --git a/src/upstart_init/moblab-apache-init.conf b/src/upstart_init/moblab-apache-init.conf
new file mode 100644
index 0000000..677545a
--- /dev/null
+++ b/src/upstart_init/moblab-apache-init.conf
@@ -0,0 +1,53 @@
+# Copyright (c) 2014 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.
+
+description   "Start apache for moblab."
+author        "chromium-os-dev@chromium.org"
+
+start on (started moblab-database-init and
+          stopped moblab-homedir-init and
+          stopped moblab-external-storage-init RESULT=ok)
+
+env CROS_CACHEDIR=/mnt/moblab/cros_cache_apache
+env BOTO_CONFIG=/home/moblab/.boto
+
+normal exit 0
+
+pre-start script
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+  logger -t "${UPSTART_JOB}" "Starting."
+  mkdir -p --mode 775 "${CROS_CACHEDIR}"
+  chown -R apache:apache "${CROS_CACHEDIR}"
+
+  APACHE_LOG_DIR=/var/log/apache2
+  mkdir -p "${APACHE_LOG_DIR}"
+  chown -R apache:apache "${APACHE_LOG_DIR}"
+
+  APACHE_RUN_DIR=/run/apache
+  mkdir -p "${APACHE_RUN_DIR}"
+  chown -R apache:apache "${APACHE_RUN_DIR}"
+
+  APACHE_SSH_DIR=/var/www/.ssh
+  if [ ! -e "${APACHE_SSH_DIR}"/id_rsa ]; then
+    mkdir -p "${APACHE_SSH_DIR}"
+    cp /root/.ssh/* "${APACHE_SSH_DIR}"
+    chown -R apache:apache "${APACHE_SSH_DIR}"
+  fi
+
+  APACHE_DIR=/var/www/
+  if [ ! -e "${APACHE_DIR}".gsutil ]; then
+    mkdir "${APACHE_DIR}".gsutil
+    chown apache:apache "${APACHE_DIR}".gsutil
+  fi
+
+  logger -t "${UPSTART_JOB}" "Ending."
+end script
+
+# apache2 will fork once and exit.
+expect fork
+
+exec apache2
diff --git a/src/upstart_init/moblab-base-container-init.conf b/src/upstart_init/moblab-base-container-init.conf
new file mode 100644
index 0000000..b5854fd
--- /dev/null
+++ b/src/upstart_init/moblab-base-container-init.conf
@@ -0,0 +1,27 @@
+# Copyright 2015 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.
+
+description   "Setup server side packaging base container."
+author        "chromium-os-dev@chromium.org"
+
+start on (stopped moblab-containers-dir-init and
+          stopped moblab-internetcheck RESULT=ok)
+
+normal exit 0
+
+script
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+  logger -t "${UPSTART_JOB}" "Starting."
+  # Sleep a second after moblab-containers-dir-init has stopped.
+  sleep 1
+  logger -t "${UPSTART_JOB}" "Setting up base container."
+  # base container needs to be created with root user for privileged container
+  # to be cloned properly.
+  sudo python /usr/local/autotest/site_utils/lxc.py -s
+  sudo chmod -R +r /usr/local/autotest/containers/moblab_base*
+  logger -t "${UPSTART_JOB}" "Ending."
+end script
diff --git a/src/upstart_init/moblab-bootup-status-init.conf b/src/upstart_init/moblab-bootup-status-init.conf
new file mode 100644
index 0000000..4d5447c
--- /dev/null
+++ b/src/upstart_init/moblab-bootup-status-init.conf
@@ -0,0 +1,17 @@
+# Copyright 2017 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.
+
+# This job is used as an indicator that all essential upstart
+# services have attempted to start, and that bootup process is
+# complete.
+start on starting mobmonitor-init
+stop on (starting moblab-network-bridge-init
+         starting moblab-host-scheduler-init and
+         starting moblab-devserver-cleanup-init and
+         starting moblab-gsoffloader-init and
+         starting moblab-gsoffloader_s-init)
+
+script
+  sleep 120 # wait 2 minutes to boot then start flagging errors
+end script
diff --git a/src/upstart_init/moblab-containers-dir-init.conf b/src/upstart_init/moblab-containers-dir-init.conf
new file mode 100644
index 0000000..84fcafb
--- /dev/null
+++ b/src/upstart_init/moblab-containers-dir-init.conf
@@ -0,0 +1,28 @@
+# Copyright 2015 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.
+
+description   "Set up the containers directory on the external storage."
+author        "chromium-os-dev@chromium.org"
+
+start on stopped moblab-external-storage-init RESULT=ok
+
+env CONTAINER_DIR=/mnt/moblab/containers
+env AUTOTEST_CONTAINER_DIR=/usr/local/autotest/containers
+
+normal exit 0
+
+script
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+  logger -t "${UPSTART_JOB}" "Starting."
+  mkdir -p "${CONTAINER_DIR}"
+  chown -R moblab:moblab "${CONTAINER_DIR}" || :
+  if [ ! -e "${AUTOTEST_CONTAINER_DIR}" ]; then
+    logger -t "${UPSTART_JOB}" "Creating autotest containers dir symlink."
+    ln -s "${CONTAINER_DIR}" "${AUTOTEST_CONTAINER_DIR}"
+  fi
+  logger -t "${UPSTART_JOB}" "Ending."
+end script
diff --git a/src/upstart_init/moblab-database-init.conf b/src/upstart_init/moblab-database-init.conf
new file mode 100644
index 0000000..5624c14
--- /dev/null
+++ b/src/upstart_init/moblab-database-init.conf
@@ -0,0 +1,137 @@
+# Copyright (c) 2014 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.
+
+description   "Configure the database for moblab"
+author        "chromium-os-dev@chromium.org"
+
+start on stopped autotest_setup
+respawn
+
+normal exit 0
+
+pre-start script
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+  logger -t "${UPSTART_JOB}" "Pre Start Starting."
+  MYSQL_LIB_DIR=/var/lib/mysql
+  if [ ! -d "${MYSQL_LIB_DIR}" ]; then
+    mkdir -p "${MYSQL_LIB_DIR}"
+    chown -R mysql:mysql "${MYSQL_LIB_DIR}"
+  fi
+
+  MYSQL_RUN_DIR=/run/mysqld
+  if [ ! -d "${MYSQL_RUN_DIR}" ]; then
+    mkdir -p "${MYSQL_RUN_DIR}"
+    chown -R mysql:mysql "${MYSQL_RUN_DIR}"
+  fi
+
+  MYSQL_LOG_DIR=/var/log/mysql
+  if [ ! -d "${MYSQL_LOG_DIR}" ]; then
+    mkdir -p "${MYSQL_LOG_DIR}"
+    chown -R mysql:mysql "${MYSQL_LOG_DIR}"
+  fi
+
+  # Clean up the binary logs if they still exist.
+  if [ -e "${MYSQL_LIB_DIR}/mysqld-bin.000001" ]; then
+    rm "${MYSQL_LIB_DIR}"/mysqld-bin.*
+  fi
+
+  # The below command won't work if the database has already been installed
+  # as it is not supplying the set password.
+  /usr/share/mysql/scripts/mysql_install_db --user=mysql \
+    --ldata="${MYSQL_LIB_DIR}" --basedir=/usr/ || :
+end script
+
+post-start script
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+  logger -t "$UPSTART_JOB" "Starting."
+  # Wait for the service to come up.
+  SLEEP_TIME=2
+  while [ ! -e /run/mysqld/mysqld.sock ]; do
+    logger -t "$UPSTART_JOB" "mysqld not up. Sleeping ${SLEEP_TIME} seconds."
+    sleep ${SLEEP_TIME}
+  done
+  passwd=$(sed -n '/^password: /{s,^[^:]*: *,,p;q}' \
+    /usr/local/autotest/shadow_config.ini)
+  if [ -z "$passwd" ]; then
+    passwd=$(sed -n '/^password = /{s,^[^=]*= *,,p;q}' \
+      /usr/local/autotest/shadow_config.ini)
+  fi
+  if mysql -u root; then
+    mysqladmin -u root password "${passwd}"
+    # In the case we have updated to a new version of the db - upgrade
+    mysqlcheck --no-defaults --fix-db-names --fix-table-names -uroot \
+      -p"${passwd}" --all-databases -v
+    mysqlcheck --no-defaults --check-upgrade --auto-repair -uroot \
+      -p"${passwd}" --all-databases -v
+  fi
+
+  PASSWD_STRING="-p${passwd}"
+
+  # Create autotest database
+  logger -t "$UPSTART_JOB" "Create database."
+  clobberdb=
+  DATABASE_LOOKUP_SQL="SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA \
+    WHERE SCHEMA_NAME = 'chromeos_autotest_db'"
+  EXISTING_DATABASE=$(mysql -u root "${PASSWD_STRING}" -e "${DATABASE_LOOKUP_SQL}")
+  if [ -z "${EXISTING_DATABASE}" ]; then
+    clobberdb=true
+  fi
+
+  SQL_COMMAND="drop database if exists chromeos_autotest_db; \
+    create database chromeos_autotest_db; \
+    grant all privileges on chromeos_autotest_db.* TO \
+    'chromeosqa-admin'@'localhost' identified by '${passwd}'; \
+    FLUSH PRIVILEGES;"
+
+  if [ "${clobberdb}" = true ]; then
+    mysql -u root "${PASSWD_STRING}" -e "${SQL_COMMAND}"
+  fi
+
+  logger -t "$UPSTART_JOB" "Database Migration."
+  AT_DIR="/usr/local/autotest"
+  python2.7 "${AT_DIR}"/database/migrate.py sync -f
+  python2.7 "${AT_DIR}"/frontend/manage.py syncdb --noinput
+  # You may have to run this twice.
+  python2.7 "${AT_DIR}"/frontend/manage.py syncdb --noinput
+
+  # Create whining database
+  logger -t "$UPSTART_JOB" "Create whining database."
+  WHINING_DIR="/whining"
+  cd "${WHINING_DIR}"/src/backend/sql
+
+  DATABASE_LOOKUP_SQL="SELECT User FROM mysql.user WHERE User='wmreader';"
+  EXISTING_WMREADER=$(mysql -u root "${PASSWD_STRING}" -e "${DATABASE_LOOKUP_SQL}")
+  DATABASE_LOOKUP_SQL="SELECT User FROM mysql.user WHERE User='wmatrix';"
+  EXISTING_WMATRIX=$(mysql -u root "${PASSWD_STRING}" -e "${DATABASE_LOOKUP_SQL}")
+  if [ -z "${EXISTING_WMREADER}" -o -z "${EXISTING_WMATRIX}" ]; then
+    sh createusers_moblab.sh "${passwd}" <<EOF
+wmreader0000
+wmatrix0000
+EOF
+  fi
+
+  DATABASE_LOOKUP_SQL="SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA \
+    WHERE SCHEMA_NAME = 'rawdb'"
+  EXISTING_RAWDB=$(mysql -u root "${PASSWD_STRING}" -e "${DATABASE_LOOKUP_SQL}")
+  if [ -z "${EXISTING_RAWDB}" ]; then
+    sh rawrecreate_moblab.sh "${passwd}" rawdb
+  fi
+
+  DATABASE_LOOKUP_SQL="SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA \
+    WHERE SCHEMA_NAME = 'wmdb'"
+  EXISTING_WMDB=$(mysql -u root "${PASSWD_STRING}" -e "${DATABASE_LOOKUP_SQL}")
+  if [ -z "${EXISTING_WMDB}" ]; then
+    sh recreate_moblab.sh "${passwd}" wmdb
+  fi
+
+  logger -t "$UPSTART_JOB" "Ending."
+end script
+
+exec /usr/sbin/mysqld \
+    --defaults-extra-file=/etc/moblab/mysql/mysql_defaults_extra.cnf
diff --git a/src/upstart_init/moblab-database-test-importer.conf b/src/upstart_init/moblab-database-test-importer.conf
new file mode 100644
index 0000000..5784bbd
--- /dev/null
+++ b/src/upstart_init/moblab-database-test-importer.conf
@@ -0,0 +1,22 @@
+# Copyright 2017 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.
+
+description   "Configure the database for moblab"
+author        "chromium-os-dev@chromium.org"
+
+start on started moblab-database-init
+
+normal exit 0
+
+script
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+  logger -t "$UPSTART_JOB" "Starting."
+  AT_DIR="/usr/local/autotest"
+  python2.7 "${AT_DIR}"/utils/test_importer.py
+
+  logger -t "${UPSTART_JOB}" "Ending."
+end script
diff --git a/src/upstart_init/moblab-devserver-cleanup-init.conf b/src/upstart_init/moblab-devserver-cleanup-init.conf
new file mode 100644
index 0000000..d89b7f3
--- /dev/null
+++ b/src/upstart_init/moblab-devserver-cleanup-init.conf
@@ -0,0 +1,35 @@
+# Copyright (c) 2014 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.
+
+description   "Clean up the devserver static directory daily."
+author        "chromium-os-dev@chromium.org"
+
+start on (started moblab-devserver-init and
+          stopped moblab-external-storage-init RESULT=ok)
+
+env STATIC_DIR=/mnt/moblab/static
+
+normal exit TERM
+
+script
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+  logger -t "${UPSTART_JOB}" "Starting."
+  if [ -z "${STATIC_DIR}" ] ; then
+    logger -t "${UPSTART_JOB}" "No static directory param passed."
+    exit 1
+  fi
+  logger -t "${UPSTART_JOB}" "Delaying first cleanup task.."
+  sleep 900  # 15 minutes
+  while true; do
+    logger -t "${UPSTART_JOB}" "Running devserver clean up."
+    sudo -u moblab \
+      /usr/local/autotest/site_utils/admin/clean_staged_images.py \
+      -v -a 168 -p 168 "${STATIC_DIR}" || true
+    logger -t "${UPSTART_JOB}" "Cleaned staged images. Sleeping..."
+    sleep 86400
+  done
+end script
diff --git a/src/upstart_init/moblab-devserver-init.conf b/src/upstart_init/moblab-devserver-init.conf
new file mode 100644
index 0000000..f875461
--- /dev/null
+++ b/src/upstart_init/moblab-devserver-init.conf
@@ -0,0 +1,39 @@
+# Copyright (c) 2014 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.
+
+description   "Start local devserver."
+author        "chromium-os-dev@chromium.org"
+
+start on (started moblab-apache-init and
+          stopped moblab-external-storage-init RESULT=ok and
+          stopped moblab-internetcheck RESULT=ok)
+
+# STATIC_DIR should be the same as in moblab-external-storage-init.conf
+# It's set here to allow devserver to be restarted without OS reboot.
+env STATIC_DIR=/mnt/moblab/static
+env LOG_DIR=/var/log/devserver
+env CROS_CACHEDIR=/mnt/moblab/cros_cache
+
+normal exit 0
+
+pre-start script
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+  logger -t "${UPSTART_JOB}" "Pre Start Starting."
+  mkdir -p "${LOG_DIR}"
+  chown -R moblab:moblab "${LOG_DIR}"
+  mkdir -p "${CROS_CACHEDIR}"
+  chown -R moblab:moblab "${CROS_CACHEDIR}"
+  for script in /usr/lib/devserver/dut-scripts/*; do
+    ln -sf "${script}" "${STATIC_DIR}"
+  done
+end script
+
+exec sudo -u moblab CROS_CACHEDIR="${CROS_CACHEDIR}" \
+  /usr/lib/devserver/devserver.py --production \
+  --static_dir="${STATIC_DIR}" --logfile "${LOG_DIR}/server.log" \
+  --android_build_credential="/home/moblab/.launch_control_key" \
+  >>"${LOG_DIR}/console.log" 2>&1
diff --git a/src/upstart_init/moblab-dhcpd-init.conf b/src/upstart_init/moblab-dhcpd-init.conf
new file mode 100644
index 0000000..6ed9be6
--- /dev/null
+++ b/src/upstart_init/moblab-dhcpd-init.conf
@@ -0,0 +1,58 @@
+# Copyright (c) 2014 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.
+
+description   "Configure DHCP server for moblab subnet"
+author        "chromium-os-dev@chromium.org"
+
+normal exit 0
+
+script
+  # Preparing dhcp's /var/{lib|run} dirs to run as user 'dhcp'.
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+
+  DHCPD_IFACE='lxcbr0'
+
+  logger -t "${UPSTART_JOB}" "preparing lease and pid files for dhcpd"
+  mkdir -m 0755 -p /run/dhcp /var/lib/dhcp
+  chmod -R u+rwX,g+rX,o+rX /var/lib/dhcp
+  chown -R dhcp:dhcp /run/dhcp /var/lib/dhcp
+
+  LEASE_FILE="/var/lib/dhcp/dhcpd.leases"
+  PID_FILE="/run/dhcp/dhcpd.pid"
+  DHCPD_CONF="/etc/dhcp/dhcpd-moblab.conf"
+  DHCPD_RESERVATIONS1="/mnt/moblab/dhcpd-reservations.conf"
+  DHCPD_RESERVATIONS2="/mnt/moblab-settings/dhcpd-reservations.conf"
+  DHCPD_EXTENSION_DIR="/var/run/moblab/"
+  DHCPD_EXTENSION_FILE="${DHCPD_EXTENSION_DIR}dhcpd-moblab.conf"
+
+
+  logger -t "${UPSTART_JOB}" "${DHCPD_IFACE} will be used for subnet."
+
+  if [ ! -f "${LEASE_FILE}" ] ; then
+    touch "${LEASE_FILE}"
+    chown dhcp:dhcp "${LEASE_FILE}"
+  fi
+
+  mkdir -p "${DHCPD_EXTENSION_DIR}" || :
+  rm -f "${DHCPD_EXTENSION_FILE}" || :
+  touch "${DHCPD_EXTENSION_FILE}" || :
+  chown moblab:moblab "${DHCPD_EXTENSION_FILE}"
+
+  # We have two possible locations whilst we move from guado to
+  # fizz based systems, TODO remove the first location when
+  # guado is no longer supported
+  if [ -e "${DHCPD_RESERVATIONS1}" ] ; then
+    echo "include \"${DHCPD_RESERVATIONS1}\";" >> "${DHCPD_EXTENSION_FILE}"
+  fi
+
+  if [ -e "${DHCPD_RESERVATIONS2}" ] ; then
+    echo "include \"${DHCPD_RESERVATIONS2}\";" >> "${DHCPD_EXTENSION_FILE}"
+  fi
+
+  logger -t "${UPSTART_JOB}" "starting DHCP service against ${DHCPD_IFACE}"
+  /usr/sbin/dhcpd -user dhcp -group dhcp -cf ${DHCPD_CONF} ${DHCPD_IFACE}
+end script
diff --git a/src/upstart_init/moblab-external-settings-init.conf b/src/upstart_init/moblab-external-settings-init.conf
new file mode 100644
index 0000000..aedddad
--- /dev/null
+++ b/src/upstart_init/moblab-external-settings-init.conf
@@ -0,0 +1,38 @@
+# Copyright 2018 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.
+
+description   "Mount and setup moblab settings external settings sd card."
+author        "chromium-os-dev@chromium.org"
+
+start on started cros-disks
+
+normal exit 0
+
+env MOUNT_DIR=/mnt/moblab-settings
+
+script
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+  logger -t "${UPSTART_JOB}" "Starting"
+  # REQUIRES A USB DRIVE WITH LABEL "MOBLAB-SETTINGS"
+  sleep 1
+  # It has been observed readlink fails occasionally
+  # If readlink fails set usb_drive to an error message so the script will
+  # continue assuming no external storage.
+  usb_drive=$(readlink -f /dev/disk/by-label/MOBLAB-SETTINGS) ||
+    echo "Readlink failed code $?"
+  if [ -e "${usb_drive}" ]; then
+    logger -t "${UPSTART_JOB}" "Mounting external settings ${usb_drive}."
+    umount -A "${usb_drive}" || :
+    /sbin/e2fsck -p "${usb_drive}" || :
+    if ! mount "${usb_drive}" "${MOUNT_DIR}" -o exec,dev,nosuid; then
+      logger -t "${UPSTART_JOB}" "Mounting of ${usb_drive} onto ${MOUNT_DIR}"
+      logger -t "${UPSTART_JOB}" "failed. Please reconnect drive and reboot."
+      exit 1
+    fi
+    chown moblab:moblab "${MOUNT_DIR}"
+  fi
+end script
diff --git a/src/upstart_init/moblab-external-storage-init.conf b/src/upstart_init/moblab-external-storage-init.conf
new file mode 100644
index 0000000..27166f4
--- /dev/null
+++ b/src/upstart_init/moblab-external-storage-init.conf
@@ -0,0 +1,87 @@
+# Copyright (c) 2014 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.
+
+description   "Mount and setup moblab external storage."
+author        "chromium-os-dev@chromium.org"
+
+start on started cros-disks and started moblab-database-init
+
+normal exit 0
+
+env MOUNT_DIR=/mnt/moblab
+env EXTERNAL_RESULTS_DIR=/mnt/moblab/results
+env ATEST_RESULTS_DIR=/usr/local/autotest/results
+export ATEST_RESULTS_DIR
+env STATIC_DIR=/mnt/moblab/static
+export STATIC_DIR
+env LOCAL_STORAGE_DIR=/usr/local/moblab-storage
+env MIN_STATEFUL_DISKSPACE=32000000
+
+script
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+  logger -t "${UPSTART_JOB}" "Starting"
+  # REQUIRES A USB DRIVE WITH LABEL "MOBLAB-STORAGE"
+  sleep 1
+  # It has been observed readlink fails occasionally, when it fails it exists the script
+  # as set -e  is set, which results in a bad boot up for moblab, /mnt/moblab is not correctly
+  # configured.
+  # If readlink fails set usb_drive to an error message so the script will
+  # continue assuming no external storage.
+  usb_drive=$(readlink -f /dev/disk/by-label/MOBLAB-STORAGE) || echo "Readlink failed code $?"
+  if [ -e "${usb_drive}" ]; then
+    logger -t "${UPSTART_JOB}" "Mounting external storage using ${usb_drive}."
+    umount -A "${usb_drive}" || :
+    /sbin/e2fsck -p "${usb_drive}" || :
+    if ! mount "${usb_drive}" "${MOUNT_DIR}" -o exec,dev,nosuid; then
+      logger -t "${UPSTART_JOB}" "Mounting of ${usb_drive} onto ${MOUNT_DIR}"
+      logger -t "${UPSTART_JOB}" "failed. Please reconnect drive and reboot."
+      exit 1
+    fi
+  else
+    logger -t "${UPSTART_JOB}" "No disk labeled MOBLAB-STORAGE found!"
+    local_part=$(dirname "${LOCAL_STORAGE_DIR}")
+    diskspace=$(df "${local_part}" | awk '!/Filesystem/ { print $4 }')
+    if [ ${diskspace} -lt ${MIN_STATEFUL_DISKSPACE} ]; then
+      logger -t "${UPSTART_JOB}" "Moblab will not launch. Please insert a"
+      logger -t "${UPSTART_JOB}" "properly configured drive and reboot."
+      exit 1
+    fi
+    logger -t "${UPSTART_JOB}" "Moblab will launch using internal disk space."
+    mkdir -p "${LOCAL_STORAGE_DIR}"
+    logger -t "${UPSTART_JOB}" "Remounting /usr/local/ with dev permissions."
+    mount -o remount,dev /usr/local
+    mount --bind "${LOCAL_STORAGE_DIR}" "${MOUNT_DIR}"
+  fi
+
+  mkdir -p "${EXTERNAL_RESULTS_DIR}"
+  chown -R moblab:moblab "${EXTERNAL_RESULTS_DIR}"
+  # If the old results directory exists copy over the files and delete it.
+  if [ -e "${ATEST_RESULTS_DIR}" -a ! -L  "${ATEST_RESULTS_DIR}" ]; then
+    logger -t "${UPSTART_JOB}" "Copying results directory."
+    cp -r "${ATEST_RESULTS_DIR}/" "${MOUNT_DIR}"
+    rm -rf "${ATEST_RESULTS_DIR}"
+  fi
+  # Create the results directory symlink if it does not exist.
+  if [ ! -e "${ATEST_RESULTS_DIR}" ]; then
+    logger -t "${UPSTART_JOB}" "Creating autotest results dir symlink."
+    ln -s "${EXTERNAL_RESULTS_DIR}" "${ATEST_RESULTS_DIR}"
+  fi
+
+  # If the database is empty clear out any old files in the results directory.
+  jobcount="$(mysql -u root -pmoblab_db_passwd -D chromeos_autotest_db \
+              -e 'select count(*) from afe_jobs' | tail -1)"
+  if [ "${jobcount}" = "0" ]; then
+    rm -rf "${EXTERNAL_RESULTS_DIR}"/*
+  fi
+
+  mkdir -p "${STATIC_DIR}"
+  chown moblab:moblab "${MOUNT_DIR}" || :
+  chown -R moblab:moblab "${MOUNT_DIR}/static" || :
+  chown -R moblab:moblab "${MOUNT_DIR}/results" || :
+
+  logger -t "${UPSTART_JOB}" "Ending."
+end script
diff --git a/src/upstart_init/moblab-gsoffloader-init.conf b/src/upstart_init/moblab-gsoffloader-init.conf
new file mode 100644
index 0000000..989f443
--- /dev/null
+++ b/src/upstart_init/moblab-gsoffloader-init.conf
@@ -0,0 +1,18 @@
+# Copyright (c) 2014 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.
+
+description   "Kick off the gs_offloader to clean up the results folder."
+author        "chromium-os-dev@chromium.org"
+
+env CROS_CACHEDIR=/mnt/moblab/cros_cache
+
+start on started moblab-scheduler-init
+respawn
+
+normal exit 0
+
+exec sudo -u moblab CROS_CACHEDIR="${CROS_CACHEDIR}" \
+     /usr/local/autotest/site_utils/gs_offloader.py \
+     --age_to_upload=0 --age_to_delete=3 -m --log_size=200
+
diff --git a/src/upstart_init/moblab-gsoffloader_s-init.conf b/src/upstart_init/moblab-gsoffloader_s-init.conf
new file mode 100644
index 0000000..6d96991
--- /dev/null
+++ b/src/upstart_init/moblab-gsoffloader_s-init.conf
@@ -0,0 +1,17 @@
+# Copyright (c) 2014 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.
+
+description   "Kick off the gs_offloader to clean up the hosts folder."
+author        "chromium-os-dev@chromium.org"
+
+env CROS_CACHEDIR=/mnt/moblab/cros_cache
+
+start on started moblab-scheduler-init
+respawn
+
+normal exit 0
+
+exec sudo -u moblab CROS_CACHEDIR="${CROS_CACHEDIR}" \
+     /usr/local/autotest/site_utils/gs_offloader.py -s --days_old=1 \
+     --log_size=200
diff --git a/src/upstart_init/moblab-homedir-init.conf b/src/upstart_init/moblab-homedir-init.conf
new file mode 100644
index 0000000..282aa22
--- /dev/null
+++ b/src/upstart_init/moblab-homedir-init.conf
@@ -0,0 +1,68 @@
+# Copyright (c) 2014 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.
+
+description   "Ensure moblab home directory exists."
+author        "chromium-os-dev@chromium.org"
+
+start on stopped autotest_setup
+
+normal exit 0
+
+script
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+  # In case stateful is wiped, recreate the moblab home directory and ensure
+  # moblab owns it.
+  MOBLAB_HOME=/home/moblab
+  mkdir -p "${MOBLAB_HOME}"
+
+  # Add aliases to simplify common commands from the shell prompt.
+  if [ ! -e "${MOBLAB_HOME}"/.bash_profile ]; then
+    cp /root/.bash_profile "${MOBLAB_HOME}"
+  fi
+
+  chown -R moblab:moblab "${MOBLAB_HOME}"
+  # Copy over the ssh keys if they don't already exist.
+  MOBLAB_SSH_DIR=/home/moblab/.ssh
+  if [ ! -e "${MOBLAB_SSH_DIR}"/id_rsa ]; then
+    mkdir -p "${MOBLAB_SSH_DIR}"
+    cp /root/.ssh/* "${MOBLAB_SSH_DIR}"
+    # since we romoved inbound ssh for root user, some files
+    # are copied from /usr/share instead.
+    cp /usr/share/chromeos-ssh-config/keys/* "${MOBLAB_SSH_DIR}"
+    chown -R moblab:moblab "${MOBLAB_SSH_DIR}"
+    if [ -e "${MOBLAB_SSH_DIR}"/id_rsa ]; then
+      chmod 600 ${MOBLAB_SSH_DIR}/id_rsa
+    fi
+    if [ -e "${MOBLAB_SSH_DIR}"/mobbase_id_rsa ]; then
+      chmod 600 ${MOBLAB_SSH_DIR}/mobbase_id_rsa
+    fi
+  fi
+
+  # Create a placeholder for the boto file.
+  MOBLAB_BOTO_FILE=/home/moblab/.boto
+  touch "${MOBLAB_BOTO_FILE}"
+  chown moblab:moblab "${MOBLAB_BOTO_FILE}"
+  chmod 660 "${MOBLAB_BOTO_FILE}"
+
+  # Create a placeholder for the service account file.
+  MOBLAB_SERVICE_ACCOUNT_FILE=/home/moblab/.service_account.json
+  touch "${MOBLAB_SERVICE_ACCOUNT_FILE}"
+  chown moblab:moblab "${MOBLAB_SERVICE_ACCOUNT_FILE}"
+  chmod 660 "${MOBLAB_SERVICE_ACCOUNT_FILE}"
+
+  # Create a placeholder for the Launch Control key file.
+  MOBLAB_LAUNCH_CONTROL_KEY_FILE=/home/moblab/.launch_control_key
+  touch "${MOBLAB_LAUNCH_CONTROL_KEY_FILE}"
+  chown moblab:moblab "${MOBLAB_LAUNCH_CONTROL_KEY_FILE}"
+  chmod 660 "${MOBLAB_LAUNCH_CONTROL_KEY_FILE}"
+
+  # Generate identifier for this instance of moblab, used with
+  # communication with the cloud service (CPCon/results offload)
+  /usr/local/autotest/site_utils/create-moblab-id.py
+
+  logger -t "${UPSTART_JOB}" "Ending."
+end script
diff --git a/src/upstart_init/moblab-host-scheduler-init.conf b/src/upstart_init/moblab-host-scheduler-init.conf
new file mode 100644
index 0000000..b5c462c
--- /dev/null
+++ b/src/upstart_init/moblab-host-scheduler-init.conf
@@ -0,0 +1,17 @@
+# Copyright 2017 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.
+
+description   "Start the autotest host-scheduler."
+author        "chromium-os-dev@chromium.org"
+
+start on (started moblab-apache-init and
+          stopped moblab-external-storage-init RESULT=ok and
+          stopped moblab-base-container-init)
+respawn
+
+normal exit 0
+
+env ATEST_RESULTS_DIR=/usr/local/autotest/results
+
+exec sudo -u moblab /usr/local/autotest/scheduler/host_scheduler.py
diff --git a/src/upstart_init/moblab-internetcheck.conf b/src/upstart_init/moblab-internetcheck.conf
new file mode 100644
index 0000000..b10e153
--- /dev/null
+++ b/src/upstart_init/moblab-internetcheck.conf
@@ -0,0 +1,37 @@
+# Copyright 2018 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.
+
+description   "Check to see we can access a DNS server."
+author        "chromium-os-dev@chromium.org"
+
+start on stopped moblab-network-bridge-init RESULT=ok
+
+normal exit 0
+
+script
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+  TEST_URL="https://storage.googleapis.com"
+  /usr/bin/dig "${TEST_URL}" || true
+  /usr/bin/nslookup "${TEST_URL}" || true
+
+  while [ -z "$(/usr/bin/dig +short -t srv ${TEST_URL})" ]; do
+    echo "DNS is unable to resolve ${TEST_URL}"
+    sleep 3
+  done
+
+  /usr/bin/dig "${TEST_URL}"
+  /usr/bin/nslookup "${TEST_URL}"
+
+  TEST_FILE="https://storage.googleapis.com/abci-ssp/autotest-containers/moblab_base_06.tar.xz"
+  until curl -I -f "${TEST_FILE}"; do
+    echo "Unable to download file from Google ${TEST_FILE}"
+    sleep 3
+  done
+
+  speedtest-cli
+
+end script
diff --git a/src/upstart_init/moblab-kill-long-running-sql-query.conf b/src/upstart_init/moblab-kill-long-running-sql-query.conf
new file mode 100644
index 0000000..da1aa8d
--- /dev/null
+++ b/src/upstart_init/moblab-kill-long-running-sql-query.conf
@@ -0,0 +1,42 @@
+# Copyright 2018 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.
+
+description   "Service to kill long running queries on moblab"
+author        "chromium-os-dev@chromium.org"
+
+start on started moblab-database-init
+
+respawn
+
+script
+  set -x -e
+  while true; do
+    passwd=$(sed -n '/^password: /{s,^[^:]*: *,,p;q}' \
+      /usr/local/autotest/shadow_config.ini)
+    if [ -z "$passwd" ]; then
+      passwd=$(sed -n '/^password = /{s,^[^=]*= *,,p;q}' \
+        /usr/local/autotest/shadow_config.ini)
+    fi
+    SECONDS_TOO_LONG=1200
+    TOO_LONG_QUERY="SELECT COUNT(1) FROM information_schema.processlist\
+                    WHERE user<>'system user' \
+                    AND time >= ${SECONDS_TOO_LONG}"
+    RUNNING_TOO_LONG=`mysql -uroot -p"${passwd}" -ANe "${TOO_LONG_QUERY}"`
+    if [ ${RUNNING_TOO_LONG} -gt 0 ]
+    then
+        mkdir -p /var/log/bootup/
+        exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+        logger -t "${UPSTART_JOB}" "Killing ${RUNNING_TOO_LONG} processes."
+        KILLPROC_SQLSTMT="SELECT GROUP_CONCAT(CONCAT('KILL QUERY ',id,';')
+                                              SEPARATOR ' ') KillQuery \
+                          FROM information_schema.processlist \
+                          WHERE user<>'system user' \
+                          AND time >= ${SECONDS_TOO_LONG}"
+        mysql -uroot -p"${passwd}" -ANe "${KILLPROC_SQLSTMT}" |
+            mysql -uroot -p"${passwd}"
+    fi
+    sleep 1200
+  done
+
+end script
diff --git a/src/upstart_init/moblab-mobmonitor-init.conf b/src/upstart_init/moblab-mobmonitor-init.conf
new file mode 100644
index 0000000..e4324b4
--- /dev/null
+++ b/src/upstart_init/moblab-mobmonitor-init.conf
@@ -0,0 +1,21 @@
+# Copyright 2015 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.
+
+description   "Launch the Mob* Monitor for moblab"
+author        "chromium-os-dev@chromium.org"
+
+start on started boot-services
+respawn
+
+normal exit 0
+
+env LOG_DIR=/var/log/mobmonitor
+
+pre-start script
+  set -e
+  mkdir -p "${LOG_DIR}"
+  chown -R moblab:moblab "${LOG_DIR}"
+end script
+
+exec python /etc/moblab/mobmonitor/mobmonitor.py
diff --git a/src/upstart_init/moblab-network-bridge-init.conf b/src/upstart_init/moblab-network-bridge-init.conf
new file mode 100644
index 0000000..cd5f919
--- /dev/null
+++ b/src/upstart_init/moblab-network-bridge-init.conf
@@ -0,0 +1,321 @@
+# Copyright (c) 2014 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.
+
+description   "Configure network bridge and disable wlan0 for moblab"
+author        "chromium-os-dev@chromium.org"
+
+start on (started network-services and
+          stopped moblab-external-storage-init RESULT=ok and
+          stopped moblab-external-settings-init RESULT=ok)
+
+normal exit 0
+
+script
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x
+  set -e
+  logger -t "$UPSTART_JOB" "Starting."
+
+  SERVER_ADDRESS=192.168.231.1
+  SERVER_NETMASK=255.255.255.0
+  DHCPD_IFACE='lxcbr0'
+  SHILL_START_LOCK_PATH="/var/lock/shill-start.lock"
+  INTERFACE_WAN='WAN'
+  INTERFACE_LAN='LAN'
+
+  # If this file exists, it must contain a single line with the mac address of
+  # the network interface to be used for private network shared by moblab with
+  # its DUTs.
+  PRIVATE_NETWORK_INTERFACE_MAC_ADDR='/mnt/moblab/private-network-macaddr.conf'
+
+  # $1: Name of the network interface to check for (e.g.: eth0)
+  # returns: 1 on success, 0 otherwise.
+  node_labeled_for_private_network() {
+    local node="$1"
+
+    local expected_addr=$(head -1 "${PRIVATE_NETWORK_INTERFACE_MAC_ADDR}" |
+                          tr -d '[:space:]')
+    local got_addr=$(ip link show "${node}" | tail -1 | awk '{print $2}')
+    if [ "${expected_addr}" = "${got_addr}" ]; then
+      return 1
+    fi
+    return 0
+  }
+
+  ensure_br_netfilter_loaded() {
+    if [ -e /proc/sys/net/bridge ]; then
+      return 1
+    fi
+
+    # Kernels older that 3.18 do not have br_netfilter as a module.
+    modprobe -n br_netfilter || return 1
+
+    logger -t "${UPSTART_JOB}" "Loading kernel module br_netfilter."
+    modprobe br_netfilter
+    local i=0
+    while [ $i -le 10 ]; do
+      if [ -e /proc/sys/net/bridge ]; then
+        return 1
+      fi
+      sleep 0.5
+      i=$(( i + 1 ))
+    done
+    return 0
+  }
+
+  # Normal case, one interface connected to a network that should have
+  # access to google, that interface should have an IP address.
+  # The other interface has the DUT's connected, no DHCP server has been
+  # started yet so no IP address on that interface.
+  find_network_interfaces_by_ip_addr() {
+    logger -t "${UPSTART_JOB}" "Finding interfaces by ip address"
+    local node
+    local server_net_prefix=$( echo "${SERVER_ADDRESS}" |
+                               awk -F '.' '{print $1 $2 $3}' )
+    for node in "$@"; do
+      local inet_addr=$( ip addr show $node |
+                         grep "inet\b" |
+                         awk '{print $2}' |
+                         cut -d/ -f1 )
+      if [ -z "${inet_addr}" ]; then
+        if [ -z ${INT_IFACE} ]; then
+          INT_IFACE=$node
+        fi
+      else
+        # Exclude the dhcp server subnet from being the external interface
+        local net_prefix=$( echo "${inet_addr}" |
+                            awk -F '.' '{print $1 $2 $3}' )
+        if [ "${net_prefix}" != "${server_net_prefix}" ]; then
+          if [ -z ${EXT_IFACE} ]; then
+            EXT_IFACE=$node
+          else
+            logger -t "${UPSTART_JOB}" "Multiple interfaces meet the criteria \
+              for External interface, please check wiring connections, only one\
+              network cable to internet required."
+          fi
+        fi
+      fi
+    done
+  }
+
+  # This is the legacy method - only works when a USB dongle is in use,
+  # and there is only a single built in ethernet
+  find_network_interfaces_by_device_type() {
+    logger -t "${UPSTART_JOB}" "Finding interfaces by device type"
+    for node in "$@"; do
+     case $(readlink -f /sys/class/net/$node) in
+       */usb*)
+           USB_IFACE=$node
+       ;;
+       */pci*)
+           EXT_IFACE=$node
+       ;;
+     esac
+    done
+  }
+
+  # Just guess what if is which so the bootup will happen.
+  find_network_interfaces_by_guess() {
+    logger -t "${UPSTART_JOB}" "Guessing interfaces"
+    INT_IFACE="$1"
+    EXT_IFACE="$2"
+  }
+
+  find_network_interfaces() {
+    # Find which network interfaces is connected to the WAN.
+    EXT_IFACE=""
+    INT_IFACE=""
+    local count=0
+    find_network_interfaces_by_ip_addr $@
+    until  [ ! -z "${EXT_IFACE}" ] && [ ! -z ${INT_IFACE} ] ||
+           [ ${count} -gt 5 ]; do
+      count=$(($count+1))
+      sleep 10
+      find_network_interfaces_by_ip_addr $@
+    done
+    logger -t "${UPSTART_JOB}" "External ${EXT_IFACE} Internal ${INT_IFACE}"
+    # TODO(haddowk) Remove this check when we are sure IP address is working
+    # correctly in the field.
+    if [ -z "${EXT_IFACE}" ] || [ -z ${INT_IFACE} ]; then
+      find_network_interfaces_by_device_type $@
+    fi
+    logger -t "${UPSTART_JOB}" "External ${EXT_IFACE} Internal ${INT_IFACE}"
+    if [ -z "${EXT_IFACE}" ] || [ -z ${INT_IFACE} ]; then
+      find_network_interfaces_by_guess $@
+    fi
+    # Ethtool exits with an error code if the device does not exist if it
+    # is not present in this hardware ( happens when a guado has no usb
+    # plugged in )
+    if [ ! -e /sys/class/net/${INT_IFACE} ]; then
+      return 0
+    fi
+    # Line below is simpley a check that ethtool exists, -e will not exit on
+    # piped statements.
+    ethtool -i ${INT_IFACE}
+    # Workaround for issue crbug.com/804977
+    # The realtek driver in the 3.14 kernel has a bug, dropping TX packets
+    # when the device is in promiscuous mode, which is required for a network
+    # bridge.
+    # The bridge is required so lxc containers virtual ethernet interface
+    # can share the network link on the physical interface of the host.
+    if [ "$(uname -r)" = "3.14.0" ]; then
+      if ethtool -i ${INT_IFACE} | grep --quiet "driver: r8152"; then
+        logger -t "${UPSTART_JOB}" "Realtek USB device detected, disable tso."
+        ethtool -K ${INT_IFACE} tso off
+      fi
+    fi
+  }
+
+  wait_for_interface() {
+    # Check the interface existence for 5 mins after boot.
+    # Taking one parameter as the interface name - either 'built-in' or 'USB'
+    local reps=0
+    while [ ${reps} -lt 300 ]; do
+      find_network_interfaces eth0 eth1
+      local intf
+      case "$1" in
+        ${INTERFACE_WAN}) intf=${EXT_IFACE}
+          ;;
+        ${INTERFACE_LAN}) intf=${INT_IFACE}
+          ;;
+       *) logger -t "${UPSTART_JOB}" "Illegal network interface $1"
+          break
+          ;;
+      esac
+      if [ -n "${intf}" ]; then
+        logger -t "${UPSTART_JOB}" "Found $1 Ethernet interface ${intf}."
+        break
+      fi
+      if [ $((reps % 30)) -eq 0 ]; then
+        logger -t "${UPSTART_JOB}" "Waiting for $1 Ethernet connection(${reps} secs)"
+      fi
+      : $((reps += 1))
+      sleep 1
+    done
+  }
+
+  set_up_forwarding() {
+    # Add delay after restart shill
+    sleep 5
+
+    # Configure NAT service. This needs to be done each time that shill
+    # is restarted as the restart will re-run the iptables setup job
+    # and thus wipe the below settings.
+    logger -t "${UPSTART_JOB}" "Configuring NAT Service."
+    iptables -t nat -A POSTROUTING -o ${EXT_IFACE} -j MASQUERADE
+    iptables -A FORWARD -i ${EXT_IFACE} -o ${DHCPD_IFACE} -m state \
+      --state RELATED,ESTABLISHED -j ACCEPT
+    iptables -A FORWARD -i ${DHCPD_IFACE} -o ${EXT_IFACE} -j ACCEPT
+
+    logger -t "${UPSTART_JOB}" "Enabling IP forwarding"
+    echo 1 > /proc/sys/net/ipv4/ip_forward
+    echo 0 > /proc/sys/net/bridge/bridge-nf-call-iptables
+    echo 0 > /proc/sys/net/bridge/bridge-nf-call-ip6tables
+  }
+
+  lock_shill() {
+    if [ -e "${SHILL_START_LOCK_PATH}" ]; then
+      lock_holder=$(readlink "${SHILL_START_LOCK_PATH}")
+      logger -t "${UPSTART_JOB}" "Warning: lock is held by $lock_holder; will retry"
+    elif [ -L "${SHILL_START_LOCK_PATH}" ]; then
+      logger -t "${UPSTART_JOB}" "Cleaning dangling shill lock held by dead process"
+      rm -f "${SHILL_START_LOCK_PATH}"
+    fi
+
+    local timeout=30
+    while [ "$timeout" -gt 0 ]; do
+      ln -s /proc/$$ "${SHILL_START_LOCK_PATH}" && return 0
+      # Failure: owner might still have the lock, so retry for a while
+      sleep 1
+      timeout=$((timeout - 1))
+    done
+    logger -t "${UPSTART_JOB}" "Error: could not grab shill restart lock after 30 seconds"
+    exit 1
+  }
+
+  unlock_shill() {
+    rm -f "${SHILL_START_LOCK_PATH}"
+  }
+
+  # Step 1 - make sure the built-in external interface exists.
+  wait_for_interface ${INTERFACE_WAN}
+  if [ -z "${EXT_IFACE}" ]; then
+    logger -t "${UPSTART_JOB}" "No Internet connection on eth0 or eth1."
+    exit 0
+  fi
+
+  # Step 2 - stop shill before creating the network bridge.
+  logger -t "${UPSTART_JOB}" "stopping shill"
+  lock_shill
+  stop shill
+
+  # Step3 - create and setup the network bridge.
+  # Step 3.1 - bring up the network bridge and set forward delay to 0.
+  logger -t "${UPSTART_JOB}" "Bringing up network bridge ${DHCPD_IFACE}"
+  # In the case of crash / restart the the bridge might already exist.
+  brctl delbr ${DHCPD_IFACE} || :
+  brctl addbr ${DHCPD_IFACE}
+  brctl setfd ${DHCPD_IFACE} 0
+
+  # Step 3.2 - configure server IP address with ${SERVER_ADDRESS}.
+  logger -t "${UPSTART_JOB}" "setting server IP address to ${SERVER_ADDRESS}"
+  ifconfig ${DHCPD_IFACE} ${SERVER_ADDRESS} netmask ${SERVER_NETMASK} up
+
+  # Step 3.3 - start the dhcpd server on MobLab.
+  logger -t "${UPSTART_JOB}" "starting moblab-dhcpd-init"
+  start moblab-dhcpd-init
+
+  # Step 4 - restart shill with the network bridge in blacklist
+  # Disable shill against the bridge interface and wlan0.
+  # Shill will automatically ignore the virtual ethernet (veth*) interfaces.
+  BLACKLISTED_DEVICES=${DHCPD_IFACE},wlan0
+  logger -t "${UPSTART_JOB}" "starting shill with ${BLACKLISTED_DEVICES} blacklisted"
+  start shill BLACKLISTED_DEVICES=${BLACKLISTED_DEVICES}
+  unlock_shill
+
+  # Step 5 - set up routing between built-in external network interface and the bridge.
+  # This module is not loaded by default in the newer kernels.
+  if ! ensure_br_netfilter_loaded; then
+    logger -t "${UPSTART_JOB}" "Verified kernel module br_netfilter"
+  else
+    logger -t "${UPSTART_JOB}" "Error: Could not load module br_netfilter"
+    exit 1
+  fi
+
+  set_up_forwarding
+
+  # Step 6 - wait for USB dongle to be ready.
+  wait_for_interface ${INTERFACE_LAN}
+  if [ -z "${INT_IFACE}" ]; then
+    logger -t "${UPSTART_JOB}" "Failed to find LAN network interface for \
+      moblab's DUTs."
+    exit 0
+  fi
+
+  # Step 7 - restarts shill with USB dongle interface in blacklist.
+  # Disable shill against USB Ethernet dongle(either eth0 or eth1) and wlan0.
+  logger -t "${UPSTART_JOB}" "stopping shill"
+  lock_shill
+  stop shill
+  logger -t "${UPSTART_JOB}" \
+    "starting shill with ${BLACKLISTED_DEVICES} and ${INT_IFACE} blacklisted"
+  start shill BLACKLISTED_DEVICES=${BLACKLISTED_DEVICES},${INT_IFACE}
+  unlock_shill
+
+  # Step 8 - finish setting  USB dongle interface into the bridge.
+  if [ -e /sys/class/net/${INT_IFACE} ]; then
+    brctl addif ${DHCPD_IFACE} ${INT_IFACE}
+  fi
+
+
+  if [ -e /sys/class/net/${INT_IFACE} ]; then
+    logger -t "${UPSTART_JOB}" "Bringing up ${INT_IFACE}"
+    ifconfig ${INT_IFACE} up
+  fi
+
+  # Set up the routing after shill restarts.
+  set_up_forwarding
+  logger -t "$UPSTART_JOB" "Ending."
+end script
diff --git a/src/upstart_init/moblab-scheduler-init.conf b/src/upstart_init/moblab-scheduler-init.conf
new file mode 100644
index 0000000..bb3250d
--- /dev/null
+++ b/src/upstart_init/moblab-scheduler-init.conf
@@ -0,0 +1,33 @@
+# Copyright (c) 2014 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.
+
+description   "Start the autotest scheduler."
+author        "chromium-os-dev@chromium.org"
+
+start on (started moblab-apache-init and
+          stopped moblab-external-storage-init RESULT=ok and
+          stopped moblab-base-container-init RESULT=ok)
+respawn
+
+env ATEST_RESULTS_DIR=/usr/local/autotest/results
+env CROS_VENV_CACHE=/usr/local/.cros-venv-cache
+
+pre-start script
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x -e
+  logger -t "${UPSTART_JOB}" "Pre start."
+  mkdir -p "${CROS_VENV_CACHE}"
+  chown moblab:moblab "${CROS_VENV_CACHE}"
+end script
+
+script
+  mkdir -p /var/log/bootup/
+  exec >>/var/log/bootup/${UPSTART_JOB}.log 2>&1
+  set -x -e
+  logger -t "${UPSTART_JOB}" "Starting."
+end script
+
+exec sudo -E -u moblab /usr/local/autotest/scheduler/monitor_db.py \
+  "${ATEST_RESULTS_DIR}"
diff --git a/src/upstart_init/moblab-whining-update-db.conf b/src/upstart_init/moblab-whining-update-db.conf
new file mode 100644
index 0000000..c557295
--- /dev/null
+++ b/src/upstart_init/moblab-whining-update-db.conf
@@ -0,0 +1,29 @@
+# Copyright 2016 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.
+
+description   "Import test results for whining/wmatrix on moblab"
+author        "chromium-os-dev@chromium.org"
+
+start on (stopped whining_setup and started moblab-database-init)
+respawn
+
+env LOG_DIR=/var/log/whining
+
+pre-start script
+  mkdir -p "${LOG_DIR}"
+  chown -R moblab:moblab "${LOG_DIR}"
+end script
+
+script
+  while true; do
+    mv "${LOG_DIR}/update.log" "${LOG_DIR}/update.log.old" || true
+    exec >> "${LOG_DIR}/update.log" 2>&1
+    # Update the wmatrix database every 5 mins (this script respawns)
+    sleep 300
+    echo "Updating wmatrix database ($(date))."
+    cd /whining
+    sudo -u moblab sh upd_moblab.sh
+    echo "Update completed."
+  done
+end script