[init] Migrate to /run.

In preparation for a udev uprev, we need to mount /run as a tmpfs as one
of the first things we do in the boot process.  /run is meant to replace
/var/run and related temporary storage areas, so we also symlink a
couple directories to their new names in /run.

CQ-DEPEND=Ia5d124ba81997ba40c6c6a822c22bb5c90f1b613
BUG=chromium:320868
TEST=lumpy still boots

Change-Id: If98e9b956536e1b35e22cb9ecb19fdd14974735a
Reviewed-on: https://chromium-review.googlesource.com/186240
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Tested-by: Steve Fung <stevefung@chromium.org>
Commit-Queue: Steve Fung <stevefung@chromium.org>
diff --git a/chromeos_startup b/chromeos_startup
index 22e762e..7a1a089 100755
--- a/chromeos_startup
+++ b/chromeos_startup
@@ -35,16 +35,6 @@
   cleanup_mounts
 }
 
-# The kernel and /sbin/init mount /proc, /sys and /dev before
-# we get here.
-mount -n -t tmpfs -o nodev,noexec,nosuid tmp /tmp
-# Mount debugfs as crossystem requires chromeos_arm driver interface.
-mount -n -t debugfs -o nodev,noexec,nosuid,mode=0750,uid=0,gid=debugfs-access \
-  debugfs /sys/kernel/debug
-
-# bootstat writes timings to both tmpfs and debugfs.
-bootstat pre-startup
-
 # Some startup functions are split into a separate library which may be
 # different for different targets (e.g., regular Chrome OS vs. embedded).
 . /usr/share/cros/startup_utils.sh
@@ -52,7 +42,7 @@
 # Factory related functions
 . /usr/share/cros/factory_utils.sh
 
-mkdir -p /dev/shm /dev/pts
+mkdir -p /dev/pts /dev/shm
 mount -n -t tmpfs -o nodev,noexec,nosuid shmfs /dev/shm
 mount -n -t devpts -o noexec,nosuid,gid=5,mode=0620 devpts /dev/pts
 
@@ -352,9 +342,20 @@
 remember_mount /home/chronos
 mount_var_and_home_chronos ${FACTORY_MODE} || cleanup_mounts
 
+# /run is now tmpfs used for runtime data. Make sure /var/run and /var/lock
+# are sym links to /run and /run/lock respectively for backwards compatibility.
+if [ ! -L /var/run ]; then
+  rm -rf /var/run
+  ln -s /run /var/run
+fi
+if [ ! -L /var/lock ]; then
+  rm -rf /var/lock
+  ln -s /run/lock /var/lock
+fi
+
 # Make sure required /var subdirectories exist.
-mkdir -p -m 0755 /var/cache /var/db /var/empty /var/lock /var/log/metrics \
-                 /var/run /var/tmp
+mkdir -p -m 0755 /var/cache /var/db /var/empty /var/log/metrics \
+                 /var/tmp
 
 # /var/tmp must be world-writable and sticky
 chmod 1777 /var/tmp
@@ -366,10 +367,6 @@
 chgrp syslog /var/log
 chmod 1775 /var/log
 
-mount_or_fail -t tmpfs -o mode=0755,nodev,noexec,nosuid varrun /var/run
-touch /var/run/.ramfs  # TODO: Is this needed?
-mount_or_fail -t tmpfs -o mode=1777,nodev,noexec,nosuid varlock /var/lock
-touch /var/lock/.ramfs # TODO: Is this needed?
 mount -n -t tmpfs -o nodev,noexec,nosuid media /media
 
 # Mount stateful partition for dev packages.
diff --git a/pre-startup.conf b/pre-startup.conf
new file mode 100644
index 0000000..0fc9295
--- /dev/null
+++ b/pre-startup.conf
@@ -0,0 +1,23 @@
+# Copyright 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     "System startup script"
+author          "chromium-os-dev@chromium.org"
+
+start on startup
+
+script
+  # The kernel and /sbin/init mount /proc, /sys and /dev before
+  # we get here.
+  mount -n -t tmpfs -o nodev,noexec,nosuid tmp /tmp
+  # Mount debugfs as bootstat depends on /sys/kernel/debug
+  mount -n -t debugfs -o nodev,noexec,nosuid,mode=0750,uid=0,gid=debugfs-access \
+    debugfs /sys/kernel/debug
+
+  mount -n -t tmpfs -o mode=0755,nodev,noexec,nosuid run /run
+  mkdir -m 1777 /run/lock
+
+  # bootstat writes timings to both tmpfs and debugfs.
+  bootstat pre-startup
+end script
diff --git a/startup.conf b/startup.conf
index a109ad0..c4a9e5d 100644
--- a/startup.conf
+++ b/startup.conf
@@ -5,6 +5,6 @@
 description     "System startup script"
 author          "chromium-os-dev@chromium.org"
 
-start on startup
+start on stopped pre-startup
 
 exec chromeos_startup