blob: 0c48b0b22a784c41ae22312a7e5a82b1db7a8b96 [file] [log] [blame]
Colin Choweb584db2010-07-08 16:02:56 -07001#!/bin/sh
2
3# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
7# /init script for use in factory install shim. Requires busybox in
8# /bin/busybox, and a symlink from /bin/sh -> busybox.
9
10# Mount point for the new tmpfs based partition.
11NEWROOT_MNT=/newroot
12# SD card partition and mount point.
13# TODO(girts): find it, instead of hardcoding.
14SD_DEV=/dev/sdb3
15SD_MNT=/sdcard
16# Size of the root ramdisk.
17TMPFS_SIZE=300M
18
19initial_mounts() {
20 mkdir /proc /sys /tmp /etc "$SD_MNT" "$NEWROOT_MNT"
21 mount -n -t proc proc /proc
22 mount -n -t sysfs sysfs /sys
23 mount -n -t devtmpfs -o mode=0755,size=20M none /dev
24 mkdir -p /dev/shm /dev/pts
25 mount -n -t tmpfs -onosuid,nodev shmfs /dev/shm
26 mount -n -t devpts -onoexec,nosuid,gid=5,mode=0620 devpts /dev/pts
27
28 # Create symlinks for all busybox programs. /proc needs to be mounted first,
29 # otherwise it will mess up the link.
30 busybox --install -s
31}
32
33drop_to_shell() {
34 echo "Dropping to shell..."
35 exec /bin/sh
36}
37
38wait_for_root() {
39 local try
40 echo -n "Waiting for $SD_DEV to appear"
41 for try in $(seq 20); do
42 echo -n "."
43 if [ -b "$SD_DEV" ]; then
44 echo "ok"
45 return 0
46 fi
47 sleep 1
48 done
49 echo
50 echo "Failed waiting for root!"
51 return 1
52}
53
54mount_sd_card() {
55 local try
56 echo -n "Mounting sd card"
57 for try in $(seq 20); do
58 echo -n "."
59 if mount "$SD_DEV" "$SD_MNT" -o ro; then
60 echo "ok"
61 return 0
62 fi
63 sleep 1
64 done
65 echo
66 echo "Failed to mount sd card!"
67 return 1
68}
69
70mount_tmpfs() {
71 echo "Mounting tmpfs..."
72 mount -t tmpfs tmpfs "$NEWROOT_MNT" -o "size=$TMPFS_SIZE"
73 return $?
74}
75
76copy_contents() {
77 echo "Copying sdcard->tmpfs..."
78 time cp -av "$SD_MNT"/* "$NEWROOT_MNT"
79 return $?
80}
81
82move_mounts() {
83 mount --move /proc "$NEWROOT_MNT/proc"
84 mount --move /dev "$NEWROOT_MNT/dev"
85 mount --move /sys "$NEWROOT_MNT/sys"
86}
87
88unmount_sd_card() {
89 umount "$SD_MNT"
90 local try
91 echo -n "$SD_DEV can now be safely removed"
92 for try in $(seq 5); do
93 echo -n "."
94 sleep 1
95 done
96 echo "continue"
97 return 0
98}
99
100main() {
101 echo Starting init...
102 PATH=/bin
103
104 initial_mounts
105 wait_for_root || drop_to_shell
106 mount_sd_card || drop_to_shell
107 mount_tmpfs || drop_to_shell
108 copy_contents || drop_to_shell
109 unmount_sd_card || drop_to_shell
110 move_mounts || drop_to_shell
111 # switch_root makes a safety check for existance of /init. Make it happy.
112 touch /init
113 # /sbin/chromeos_startup will see this and skip mounting of sysfs, proc and
114 # /dev.
115 mkdir "$NEWROOT_MNT/dev/.initramfs"
116 # Chroot into newroot, erase the contents of the old /, and exec real init.
117 exec switch_root "$NEWROOT_MNT" /sbin/init
118 # This should not really happen.
119 echo "Failed to switch root"
120 drop_to_shell
121}
122
123main