OSDN Git Service

5d747c54ca6ec6d523c0ff7d966095e053718a62
[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 2011/08/18
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         else
40                 echo 0 0 0 0 > /proc/sys/kernel/printk
41         fi
42         # initialise /dev (first time)
43         echo /sbin/mdev > /proc/sys/kernel/hotplug
44         mdev -s
45         # re-run this script with a controlling tty
46         exec env HAS_CTTY=Yes setsid cttyhack /bin/sh "$0" "$@"
47 fi
48
49 # now running under a controlling tty; debug output from stderr into log file
50 # boot up Android
51
52 error()
53 {
54         echo $*
55         return 1
56 }
57
58 try_mount()
59 {
60         RW=$1; shift
61         if [ "${ROOT#*:/}" != "$ROOT" ]; then
62                 # for NFS roots, use nolock to avoid dependency to portmapper
63                 RW="nolock,$RW"
64         fi
65         # FIXME: any way to mount ntfs gracefully?
66         mount -o $RW $@ || mount.ntfs-3g -o rw,force $@
67 }
68
69 check_root()
70 {
71         try_mount ro $1 /mnt && [ -e /mnt/$SRC/ramdisk.img ]
72         [ $? -ne 0 ] && return 1
73         zcat /mnt/$SRC/ramdisk.img | cpio -id > /dev/null
74         if [ -e /mnt/$SRC/system.sfs ]; then
75                 mount -o loop /mnt/$SRC/system.sfs /sfs
76                 mount -o loop /sfs/system.img system
77         elif [ -e /mnt/$SRC/system.img ]; then
78                 mount -o loop /mnt/$SRC/system.img system
79         elif [ -d /mnt/$SRC/system ]; then
80                 remount_rw
81                 mount --bind /mnt/$SRC/system system
82         else
83                 rm -rf *
84                 return 1
85         fi
86         mkdir cache mnt mnt/sdcard
87         mount -t tmpfs tmpfs cache
88         echo " found at $1"
89 }
90
91 remount_rw()
92 {
93         # "foo" as mount source is given to workaround a Busybox bug with NFS
94         # - as it's ignored anyways it shouldn't harm for other filesystems.
95         mount -o remount,rw foo /mnt
96 }
97
98 debug_shell()
99 {
100         if which mksh >/dev/null 2>&1; then
101                 echo Running MirBSD Korn Shell...
102                 USER="($1)" mksh -l 2>&1
103         else
104                 echo Running busybox ash...
105                 sh 2>&1
106         fi
107 }
108
109 echo -n Detecting Android-x86...
110
111 mount -t tmpfs tmpfs /android
112 cd /android
113 while :; do
114         for device in ${ROOT:-/dev/sr* /dev/[hs]d[a-z]*}; do
115                 check_root $device && break 2
116                 mountpoint -q /mnt && umount /mnt
117         done
118         sleep 1
119         echo -n .
120 done
121
122 ln -s mnt/$SRC /src
123 ln -s android/system /
124 ln -s ../system/lib/modules /lib
125 ln -s ../system/lib/firmware /lib
126
127 if [ -n "$INSTALL" ]; then
128         cd /
129         zcat /src/install.img | cpio -iud > /dev/null
130 fi
131
132 if [ -x system/bin/ln -a \( -n "$DEBUG" -o -n "$BUSYBOX" \) ]; then
133         mv /bin /lib .
134         system/bin/ln -s android/lib /lib
135         system/bin/ln -s android/bin /bin
136         sed -i 's|\(PATH *\)\(/sbin\)|\1/bin:\2|' init.rc
137         mv /sbin/* sbin
138         rmdir /sbin
139         ln -s android/sbin /
140 fi
141
142 # ensure keyboard driver is loaded
143 [ -n "$INSTALL" -o -n "$DEBUG" ] && modprobe atkbd
144
145 if [ -n "$DEBUG" ]; then
146         echo -e "\nType 'exit' to continue booting...\n"
147         debug_shell debug-found
148 fi
149
150 # load scripts
151 for s in `ls /scripts/* /src/scripts/*`; do
152         test -e "$s" && source $s
153 done
154
155 # A target should provide its detect_hardware function.
156 # On success, return 0 with the following values set.
157 #
158 # FB0DEV: framebuffer driver
159 # LANDEV: lan driver
160 # WIFDEV: wifi driver
161 # SNDDEV: sound driver
162 # CAMDEV: camera driver
163 # PREDEV: any module the drivers depend on but can't be loaded automatically
164 # EXTMOD: any other module
165
166 [ "$AUTO" != "1" ] && detect_hardware && FOUND=1
167
168 [ -n "$INSTALL" ] && do_install
169
170 load_modules
171 mount_data
172 mount_sdcard
173 setup_tslib
174 setup_dpi
175 post_detect
176
177 if [ -n "$DEBUG" ]; then
178         echo -e "\nUse Alt-F1/F2/F3 to switch between virtual consoles"
179         echo -e "Type 'exit' to enter Android...\n"
180
181         debug_shell debug-late
182         SWITCH=chroot
183 fi
184
185 # We must disable mdev before switching to Android
186 # since it conflicts with Android's init
187 echo > /proc/sys/kernel/hotplug
188
189 exec ${SWITCH:-switch_root} /android /init
190
191 # avoid kernel panic
192 while :; do
193         echo
194         echo '  Android-x86 console shell. Use only in emergencies.'
195         echo
196         debug_shell fatal-err
197 done