OSDN Git Service

init: do not mount /cache directly
[android-x86/bootable-newinstaller.git] / initrd / init
1 #!/bin/busybox sh
2 #
3 # By Chih-Wei Huang <cwhuang@linux.org.tw>
4 # and Thorsten Glaser <tg@mirbsd.org>
5 #
6 # Last updated 2014/01/15
7 #
8 # License: GNU Public License
9 # We explicitely grant the right to use the scripts
10 # with Android-x86 project.
11 #
12
13 PATH=/sbin:/bin:/system/bin:/system/xbin; export PATH
14
15 # configure debugging output
16 if [ -n "$DEBUG" ]; then
17         LOG=/tmp/log
18         set -x
19 else
20         LOG=/dev/null
21         test -e "$LOG" || busybox mknod $LOG c 1 3
22 fi
23 exec 2>> $LOG
24
25 # early boot
26 if test x"$HAS_CTTY" != x"Yes"; then
27         # initialise /proc and /sys
28         busybox mount -t proc proc /proc
29         busybox mount -t sysfs sys /sys
30         # let busybox install all applets as symlinks
31         busybox --install -s
32         # spawn shells on tty 2 and 3 if debug or installer
33         if test -n "$DEBUG" || test -n "$INSTALL"; then
34                 # ensure they can open a controlling tty
35                 mknod /dev/tty c 5 0
36                 # create device nodes then spawn on them
37                 mknod /dev/tty2 c 4 2 && openvt
38                 mknod /dev/tty3 c 4 3 && openvt
39         fi
40         if test -z "$DEBUG" || test -n "$INSTALL"; then
41                 echo 0 0 0 0 > /proc/sys/kernel/printk
42         fi
43         # initialise /dev (first time)
44         mkdir -p /dev/block
45         echo /sbin/mdev > /proc/sys/kernel/hotplug
46         mdev -s
47         # re-run this script with a controlling tty
48         exec env HAS_CTTY=Yes setsid cttyhack /bin/sh "$0" "$@"
49 fi
50
51 # now running under a controlling tty; debug output from stderr into log file
52 # boot up Android
53
54 error()
55 {
56         echo $*
57         return 1
58 }
59
60 try_mount()
61 {
62         RW=$1; shift
63         if [ "${ROOT#*:/}" != "$ROOT" ]; then
64                 # for NFS roots, use nolock to avoid dependency to portmapper
65                 RW="nolock,$RW"
66         fi
67         # FIXME: any way to mount ntfs gracefully?
68         mount -o $RW $@ || mount.ntfs-3g -o rw,force $@
69 }
70
71 check_root()
72 {
73         if [ "`dirname $1`" = "/dev" ]; then
74                 [ -e $1 ] || return 1
75                 blk=`basename $1`
76                 [ ! -e /dev/block/$blk ] && ln $1 /dev/block
77                 dev=/dev/block/$blk
78         else
79                 dev=$1
80         fi
81         try_mount ro $dev /mnt || return 1
82         if [ -n "$iso" -a -e /mnt/$iso ]; then
83                 mount --move /mnt /iso
84                 mkdir /mnt/iso
85                 mount -o loop /iso/$iso /mnt/iso
86                 SRC=iso
87         elif [ ! -e /mnt/$SRC/ramdisk.img ]; then
88                 return 1
89         fi
90         zcat /mnt/$SRC/ramdisk.img | cpio -id > /dev/null
91         if [ -e /mnt/$SRC/system.sfs ]; then
92                 mount -o loop /mnt/$SRC/system.sfs /sfs
93                 mount -o loop /sfs/system.img system
94         elif [ -e /mnt/$SRC/system.img ]; then
95                 remount_rw
96                 mount -o loop /mnt/$SRC/system.img system
97         elif [ -d /mnt/$SRC/system ]; then
98                 remount_rw
99                 mount --bind /mnt/$SRC/system system
100         else
101                 rm -rf *
102                 return 1
103         fi
104         mkdir mnt
105         echo " found at $1"
106 }
107
108 remount_rw()
109 {
110         # "foo" as mount source is given to workaround a Busybox bug with NFS
111         # - as it's ignored anyways it shouldn't harm for other filesystems.
112         mount -o remount,rw foo /mnt
113 }
114
115 debug_shell()
116 {
117         if which mksh >/dev/null 2>&1; then
118                 echo Running MirBSD Korn Shell...
119                 USER="($1)" mksh -l 2>&1
120         else
121                 echo Running busybox ash...
122                 sh 2>&1
123         fi
124 }
125
126 echo -n Detecting Android-x86...
127
128 [ -z "$SRC" -a -n "$BOOT_IMAGE" ] && SRC=`dirname $BOOT_IMAGE`
129
130 for c in `cat /proc/cmdline`; do
131         case $c in
132                 iso-scan/filename=*)
133                         eval `echo $c | cut -b1-3,18-`
134                         ;;
135                 *)
136                         ;;
137         esac
138 done
139
140 mount -t tmpfs tmpfs /android
141 cd /android
142 while :; do
143         for device in ${ROOT:-/dev/[hmsv][dmr][0-9a-z]*}; do
144                 check_root $device && break 2
145                 mountpoint -q /mnt && umount /mnt
146         done
147         sleep 1
148         echo -n .
149 done
150
151 ln -s mnt/$SRC /src
152 ln -s android/system /
153 ln -s ../system/lib/modules /lib
154 ln -s ../system/lib/firmware /lib
155
156 if [ -n "$INSTALL" ]; then
157         cd /
158         zcat /src/install.img | cpio -iud > /dev/null
159 fi
160
161 if [ -x system/bin/ln -a \( -n "$DEBUG" -o -n "$BUSYBOX" \) ]; then
162         mv /bin /lib .
163         system/bin/ln -s android/lib /lib
164         system/bin/ln -s android/bin /bin
165         sed -i 's|\(PATH *\)\(/sbin\)|\1/bin:\2|' init.rc
166         mv /sbin/* sbin
167         rmdir /sbin
168         ln -s android/sbin /
169 fi
170
171 # ensure keyboard driver is loaded
172 [ -n "$INSTALL" -o -n "$DEBUG" ] && modprobe atkbd
173
174 if [ 0$DEBUG -gt 0 ]; then
175         echo -e "\nType 'exit' to continue booting...\n"
176         debug_shell debug-found
177 fi
178
179 # load scripts
180 for s in `ls /scripts/* /src/scripts/*`; do
181         test -e "$s" && source $s
182 done
183
184 # A target should provide its detect_hardware function.
185 # On success, return 0 with the following values set.
186 # return 1 if it wants to use auto_detect
187 [ "$AUTO" != "1" ] && detect_hardware && FOUND=1
188
189 [ -n "$INSTALL" ] && do_install
190
191 load_modules
192 mount_data
193 mount_sdcard
194 setup_tslib
195 setup_dpi
196 post_detect
197
198 if [ 0$DEBUG -gt 1 ]; then
199         echo -e "\nUse Alt-F1/F2/F3 to switch between virtual consoles"
200         echo -e "Type 'exit' to enter Android...\n"
201
202         debug_shell debug-late
203 fi
204
205 [ -n "$DEBUG" ] && SWITCH=${SWITCH:-chroot}
206
207 # We must disable mdev before switching to Android
208 # since it conflicts with Android's init
209 echo > /proc/sys/kernel/hotplug
210
211 exec ${SWITCH:-switch_root} /android /init
212
213 # avoid kernel panic
214 while :; do
215         echo
216         echo '  Android-x86 console shell. Use only in emergencies.'
217         echo
218         debug_shell fatal-err
219 done