OSDN Git Service

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